Skip to content

Commit 650487c

Browse files
authored
Fix env for lua5.1/luajit (#347)
1 parent 7fdac73 commit 650487c

6 files changed

Lines changed: 57 additions & 1 deletion

File tree

extension/script/backend/worker/eval/readonly.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ do
3333
i = i + 1
3434
end
3535
end
36+
if not env and getfenv then
37+
env = getfenv(f)
38+
end
3639
do
3740
local i = 1
3841
while true do
@@ -76,6 +79,9 @@ end
7679
local compiled = env
7780
and assert(_load(full_source, '=(EVAL)', "t", env))
7881
or assert(_load(full_source, '=(EVAL)'))
82+
if env and setfenv then
83+
setfenv(compiled, env)
84+
end
7985
local func = compiled()
8086
do
8187
local i = 1

extension/script/backend/worker/eval/readwrite.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ do
4242
i = i + 1
4343
end
4444
end
45+
if not env and getfenv then
46+
env = getfenv(f)
47+
end
4548
do
4649
local i = 1
4750
while true do
@@ -90,6 +93,9 @@ end
9093
local compiled = env
9194
and assert(_load(full_source, '=(EVAL)', "t", env))
9295
or assert(_load(full_source, '=(EVAL)'))
96+
if env and setfenv then
97+
setfenv(compiled, env)
98+
end
9399
local func, update = compiled()
94100
local found = {}
95101
do

extension/script/backend/worker/variables.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ function special_has.Global(frameId)
148148
if eval ~= "_ENV" then
149149
eval = "_G"
150150
value = rdebug._G
151+
if LUAVERSION == 51 then
152+
local fenv = rdebug.getfenv(info.func)
153+
if fenv and not rdebug.equal(fenv, rdebug._G) then
154+
eval = nil
155+
value = fenv
156+
end
157+
end
151158
end
152159
if eval == "_G" and globalCache._G then
153160
local t = globalCache._G

src/luadebug/rdebug_visitor.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,19 @@ namespace luadebug::visitor {
792792
return 2;
793793
}
794794

795+
static int visitor_getfenv(luadbg_State* L, lua_State* hL, protected_area& area) {
796+
#if LUA_VERSION_NUM == 501
797+
if (!copy_from_dbg(L, hL, area, 1, LUADBG_TFUNCTION)) {
798+
return 0;
799+
}
800+
lua_pop(hL, 1);
801+
refvalue::create(L, 1, refvalue::FENV {});
802+
return 1;
803+
#else
804+
return 0;
805+
#endif
806+
}
807+
795808
template <bool getref = true>
796809
static int visitor_getmetatable(luadbg_State* L, lua_State* hL, protected_area& area) {
797810
area.check_client_stack(2);
@@ -1120,6 +1133,7 @@ namespace luadebug::visitor {
11201133
{ "getlocalv", protected_call<visitor_getlocal<false>> },
11211134
{ "getupvalue", protected_call<visitor_getupvalue> },
11221135
{ "getupvaluev", protected_call<visitor_getupvalue<false>> },
1136+
{ "getfenv", protected_call<visitor_getfenv> },
11231137
{ "getmetatable", protected_call<visitor_getmetatable> },
11241138
{ "getmetatablev", protected_call<visitor_getmetatable<false>> },
11251139
{ "getuservalue", protected_call<visitor_getuservalue> },

src/luadebug/util/refvalue.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,25 @@ namespace luadebug::refvalue {
328328
}
329329
}
330330

331+
template <>
332+
int eval<FENV>(FENV& v, lua_State* hL, value* parent) {
333+
int t = eval(parent, hL);
334+
if (t == LUA_TNONE)
335+
return LUA_TNONE;
336+
if (t != LUA_TFUNCTION) {
337+
lua_pop(hL, 1);
338+
return LUA_TNONE;
339+
}
340+
#if LUA_VERSION_NUM == 501
341+
lua_getfenv(hL, -1);
342+
lua_replace(hL, -2);
343+
return lua_type(hL, -1);
344+
#else
345+
lua_pop(hL, 1);
346+
return LUA_TNONE;
347+
#endif
348+
}
349+
331350
int eval(value* v, lua_State* hL) {
332351
return visit([hL, v](auto&& arg) { return eval(arg, hL, v + 1); }, *v);
333352
}

src/luadebug/util/refvalue.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace luadebug::refvalue {
5353
struct USERDATA_VAL {
5454
unsigned int index;
5555
};
56+
struct FENV {};
5657
using value = variant<
5758
FRAME_LOCAL,
5859
FRAME_FUNC,
@@ -66,7 +67,8 @@ namespace luadebug::refvalue {
6667
TABLE_HASH_KEY,
6768
TABLE_HASH_VAL,
6869
USERDATA_KEY,
69-
USERDATA_VAL>;
70+
USERDATA_VAL,
71+
FENV>;
7072
static_assert(std::is_trivially_copyable_v<value>);
7173

7274
template <typename T>
@@ -102,6 +104,8 @@ namespace luadebug::refvalue {
102104
struct allow_as_child<USERDATA_KEY> : public std::true_type {};
103105
template <>
104106
struct allow_as_child<USERDATA_VAL> : public std::true_type {};
107+
template <>
108+
struct allow_as_child<FENV> : public std::true_type {};
105109

106110
int eval(value* v, lua_State* hL);
107111
bool assign(value* v, lua_State* hL);

0 commit comments

Comments
 (0)