Files
pip/libs/lua/piluaprogram.cpp
andrey 4d2c268710 fix(piluaprogram): add destructor to close Lua state
luaL_newstate() allocates a Lua state (~2-4 MB with libraries)
in the constructor, but there was no destructor to call lua_close().
Every PILuaProgram instance leaked its entire Lua state.
Add ~PILuaProgram() that calls lua_close(PRIVATE->lua_state).
2026-08-10 16:26:13 +03:00

58 lines
1.6 KiB
C++

/*
PIP - Platform Independent Primitives
PILuaProgram
Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piluaprogram.h"
PRIVATE_DEFINITION_START(PILuaProgram)
lua_State * lua_state;
PRIVATE_DEFINITION_END(PILuaProgram)
PILuaProgram::PILuaProgram() {
PRIVATE->lua_state = luaL_newstate();
luaL_openlibs(PRIVATE->lua_state);
}
PILuaProgram::~PILuaProgram() {
lua_close(PRIVATE->lua_state);
}
bool PILuaProgram::load(const PIString & script) {
int ret = luaL_dostring(PRIVATE->lua_state, script.dataUTF8());
if (ret != 0) return false;
return true;
}
bool PILuaProgram::prepare() {
return (lua_pcall(PRIVATE->lua_state, 0, 0, 0) == 0);
}
luabridge::LuaRef PILuaProgram::getGlobal(const PIString & name) {
return luabridge::getGlobal(PRIVATE->lua_state, name.dataUTF8());
}
luabridge::Namespace PILuaProgram::getGlobalNamespace() {
return luabridge::getGlobalNamespace(PRIVATE->lua_state);
}