Skip to content

Commit

Permalink
feat: collectGarbage method
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Sep 20, 2023
1 parent adb6978 commit 2c79115
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/lua/scripts/lua_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,19 @@ void LuaEnvironment::executeTimerEvent(uint32_t eventIndex) {
luaL_unref(luaState, LUA_REGISTRYINDEX, parameter);
}
}

void LuaEnvironment::collectGarbage() const {
// prevents recursive collects
static bool collecting = false;
if (!collecting) {
collecting = true;

// we must collect two times because __gc metamethod
// is called on uservalues only the second time
for (int i = -1; ++i < 2;) {
lua_gc(luaState, LUA_GCCOLLECT, 0);
}

collecting = false;
}
}
2 changes: 2 additions & 0 deletions src/lua/scripts/lua_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class LuaEnvironment : public LuaScriptInterface {
return shuttingDown;
}

void collectGarbage() const;

private:
void executeTimerEvent(uint32_t eventIndex);

Expand Down

0 comments on commit 2c79115

Please sign in to comment.