//自定义lua的loader
void CCLuaStack::addLuaLoader(lua_CFunction func) { if (!func) return; // stack content after the invoking of the function // get loader table lua_getglobal(m_state, "package"); /* L: package */ lua_getfield(m_state, -1, "loaders"); /* L: package, loaders */ // insert loader into index 2 lua_pushcfunction(m_state, func); /* L: package, loaders, func */ for (int i = lua_objlen(m_state, -2) + 1; i > 2; --i) { lua_rawgeti(m_state, -2, i - 1); /* L: package, loaders, func, function */ // we call lua_rawgeti, so the loader table now is at -3 lua_rawseti(m_state, -3, i); /* L: package, loaders, func */ } lua_rawseti(m_state, -2, 2); /* L: package, loaders */ // set loaders into package lua_setfield(m_state, -2, "loaders"); /* L: package */ lua_pop(m_state, 1); }
时间: 2024-10-31 01:06:42