Last active
May 29, 2021 02:01
-
-
Save losophy/65926301c7d74e4503580117a26b9722 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static Table *getcurrenv (lua_State *L) { | |
if (L->ci == L->base_ci) /* no enclosing function? *///如果该函数不是内嵌函数,那么直接返回G表 | |
return hvalue(gt(L)); /* use global table as environment */ | |
else {//再则,如果是内嵌函数,就返回其母函数的`env`表 | |
Closure *func = curr_func(L); | |
return func->c.env; | |
} | |
} | |
LUALIB_API int luaL_ref (lua_State *L, int t) { | |
int ref; | |
t = abs_index(L, t); | |
if (lua_isnil(L, -1)) { | |
lua_pop(L, 1); /* remove from stack */ | |
return LUA_REFNIL; /* `nil' has a unique fixed reference */ | |
} | |
lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ | |
ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ | |
lua_pop(L, 1); /* remove it from stack */ | |
if (ref != 0) { /* any free element? */ | |
lua_rawgeti(L, t, ref); /* remove it from list */ | |
lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ | |
} | |
else { /* no free elements */ | |
ref = (int)lua_objlen(L, t); | |
ref++; /* create new reference */ | |
} | |
lua_rawseti(L, t, ref); | |
return ref; | |
} | |
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { | |
if (ref >= 0) { | |
t = abs_index(L, t); | |
lua_rawgeti(L, t, FREELIST_REF); | |
lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ | |
lua_pushinteger(L, ref); | |
lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ | |
} | |
} | |
static TValue *index2adr (lua_State *L, int idx) { | |
if (idx > 0) { | |
TValue *o = L->base + (idx - 1); | |
api_check(L, idx <= L->ci->top - L->base); | |
if (o >= L->top) return cast(TValue *, luaO_nilobject); | |
else return o; | |
} | |
else if (idx > LUA_REGISTRYINDEX) { | |
api_check(L, idx != 0 && -idx <= L->top - L->base); | |
return L->top + idx; | |
} | |
else switch (idx) { /* pseudo-indices */ | |
case LUA_REGISTRYINDEX: return registry(L); | |
case LUA_ENVIRONINDEX: { | |
Closure *func = curr_func(L); | |
sethvalue(L, &L->env, func->c.env); | |
return &L->env; | |
} | |
case LUA_GLOBALSINDEX: return gt(L); | |
default: { | |
Closure *func = curr_func(L); | |
idx = LUA_GLOBALSINDEX - idx; | |
return (idx <= func->c.nupvalues) | |
? &func->c.upvalue[idx-1] | |
: cast(TValue *, luaO_nilobject); | |
} | |
} | |
} | |
static const luaL_Reg lualibs[] = { | |
{"", luaopen_base}, | |
{LUA_LOADLIBNAME, luaopen_package}, | |
{LUA_TABLIBNAME, luaopen_table}, | |
{LUA_IOLIBNAME, luaopen_io}, | |
{LUA_OSLIBNAME, luaopen_os}, | |
{LUA_STRLIBNAME, luaopen_string}, | |
{LUA_MATHLIBNAME, luaopen_math}, | |
{LUA_DBLIBNAME, luaopen_debug}, | |
{NULL, NULL} | |
}; | |
LUALIB_API void luaL_openlibs (lua_State *L) { | |
const luaL_Reg *lib = lualibs; | |
for (; lib->func; lib++) { | |
lua_pushcfunction(L, lib->func); | |
lua_pushstring(L, lib->name); | |
lua_call(L, 1, 0); | |
} | |
} | |
static void base_open (lua_State *L) { | |
/* set global _G */ | |
lua_pushvalue(L, LUA_GLOBALSINDEX); | |
lua_setglobal(L, "_G"); | |
/* open lib into global table */ | |
luaL_register(L, "_G", base_funcs); | |
lua_pushliteral(L, LUA_VERSION); | |
lua_setglobal(L, "_VERSION"); /* set global _VERSION */ | |
/* `ipairs' and `pairs' need auxiliary functions as upvalues */ | |
auxopen(L, "ipairs", luaB_ipairs, ipairsaux); | |
auxopen(L, "pairs", luaB_pairs, luaB_next); | |
/* `newproxy' needs a weaktable as upvalue */ | |
lua_createtable(L, 0, 1); /* new table `w' */ | |
lua_pushvalue(L, -1); /* `w' will be its own metatable */ | |
lua_setmetatable(L, -2); | |
lua_pushliteral(L, "kv"); | |
lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ | |
lua_pushcclosure(L, luaB_newproxy, 1); | |
lua_setglobal(L, "newproxy"); /* set global `newproxy' */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ | |
(lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) | |
#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) | |
#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) | |
/* | |
** pseudo-indices | |
*/ | |
#define LUA_REGISTRYINDEX (-10000) | |
#define LUA_ENVIRONINDEX (-10001) | |
#define LUA_GLOBALSINDEX (-10002) | |
#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment