Skip to content

Commit 94e6d45

Browse files
committed
lua: cleanup
1 parent 845910f commit 94e6d45

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/lua/lua_environment.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,30 +155,33 @@ static int loader(lua_State *L)
155155
#if CROWN_DEBUG
156156
static int require_internal(lua_State *L)
157157
{
158-
const char *module_name = lua_tostring(L, 1);
159158
bool already_loaded = false;
159+
const char *module_name = lua_tostring(L, 1);
160+
lua_settop(L, 1);
160161

161162
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
162163
lua_getfield(L, -1, module_name);
163164
already_loaded = lua_toboolean(L, -1);
164165
lua_pop(L, 2);
165166

166167
lua_getglobal(L, "original_require");
167-
lua_pushvalue(L, -2);
168-
lua_remove(L, -3);
168+
lua_pushvalue(L, 1);
169169
lua_call(L, 1, 1);
170170

171171
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
172-
lua_getfield(L, 2, module_name);
172+
lua_getfield(L, -1, module_name);
173173
if (lua_toboolean(L, -1) && !already_loaded) {
174174
lua_pop(L, 2);
175175
lua_getglobal(L, "package");
176176
lua_getfield(L, -1, "load_order");
177177
lua_pushstring(L, module_name);
178178
lua_rawseti(L, -2, int(lua_objlen(L, -2) + 1));
179179
lua_pop(L, 2); // Pop "package.load_order" and "package"
180+
} else {
181+
lua_pop(L, 2); // Pop "_LOADED" and "_LOADED[module]"
180182
}
181183

184+
lua_remove(L, 1);
182185
return 1;
183186
}
184187
#endif // if CROWN_DEBUG
@@ -404,7 +407,7 @@ void LuaEnvironment::add_module_metafunction(const char *module, const char *nam
404407
lua_getglobal(L, module);
405408
lua_pushvalue(L, -2);
406409
lua_setmetatable(L, -2);
407-
lua_pop(L, -1);
410+
lua_pop(L, 2);
408411
}
409412

410413
void LuaEnvironment::set_module_number(const char *module, const char *name, f64 value)

0 commit comments

Comments
 (0)