Hide shell functions from _G using upvalues on the main chunk
Store __command, __interactive, __getenv, __setenv as upvalues populated by lua_load() from the registry, keeping them invisible to user code while accessible to the parser's codegen.
This commit is contained in:
22
lapi.c
22
lapi.c
@@ -29,6 +29,7 @@
|
||||
#include "ltm.h"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
#include "lcmd.h"
|
||||
|
||||
|
||||
|
||||
@@ -1135,6 +1136,27 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
|
||||
setobj(L, f->upvals[0]->v.p, >);
|
||||
luaC_barrier(L, f->upvals[0], >);
|
||||
}
|
||||
if (f->nupvalues >= LUSH_NUM_UPVALS) { /* has lush upvalues? */
|
||||
/* populate shell function upvalues from registry */
|
||||
Table *regt = hvalue(&G(L)->l_registry);
|
||||
TValue lushtv;
|
||||
lu_byte tag = luaH_getint(regt, LUA_RIDX_LUSH, &lushtv);
|
||||
if (novariant(tag) == LUA_TTABLE) {
|
||||
static const char *const fields[] = {
|
||||
"command", "interactive", "getenv", "setenv"
|
||||
};
|
||||
Table *lusht = hvalue(&lushtv);
|
||||
int i;
|
||||
for (i = 0; i < 4; i++) {
|
||||
TValue val;
|
||||
TString *key = luaS_new(L, fields[i]);
|
||||
if (luaH_getstr(lusht, key, &val) != LUA_TNIL) {
|
||||
setobj(L, f->upvals[i + 1]->v.p, &val);
|
||||
luaC_barrier(L, f->upvals[i + 1], &val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_unlock(L);
|
||||
return APIstatus(status);
|
||||
|
||||
Reference in New Issue
Block a user