要想在C中调用Lua中的函数,按照之前方法,需要先找到函数所在的位置。于是我们需要调用 lua_getglobal这个函数,而实际上lua_getglobal这个函数又是如何找到name所在的位置呢?
我们先看这个函数的实现
LUA_API int lua_getglobal (lua_State *L, const char *name) {
Table *reg = hvalue(&G(L)->l_registry);
lua_lock(L);
return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
}
继续跟进会看到调了auxgetstr这个函数
/*
** get functions (Lua -> stack)
*/
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
const TValue *slot;
TString *str = luaS_new(L, k);
if (luaV_fastget(L