X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FScript.hh;h=2f285cecc2bcdd740529783fcc8d4f7ea0fa581b;hp=df00619d69e59b4dafcf644d5782e2ab53ac1eac;hb=e495074443d9fd7bc16137084cf9de3d031b75c4;hpb=c9e20ac06383b20ceb5404c9237e319c2e90d157 diff --git a/src/Moof/Script.hh b/src/Moof/Script.hh index df00619..2f285ce 100644 --- a/src/Moof/Script.hh +++ b/src/Moof/Script.hh @@ -471,15 +471,14 @@ public: Script() : - mState(luaL_newstate()) + mState(0) { - lua_pushlightuserdata(mState, this); - lua_setfield(mState, LUA_REGISTRYINDEX, "_script_obj"); + reset(); } ~Script() { - if (mIsMainThread) lua_close(mState); + destroy(); } @@ -488,12 +487,77 @@ public: return ScriptP(new Script); } + void reset() + { + if (mState) destroy(); + mState = luaL_newstate(); + lua_pushlightuserdata(mState, this); + lua_setfield(mState, LUA_REGISTRYINDEX, "_script_obj"); + } + void importStandardLibraries() { luaL_openlibs(mState); } + void importBaseLibrary() + { + lua_pushcfunction(mState, luaopen_base); + push(LUA_COLIBNAME); + call(1, 0); + } + + void importPackageLibrary() + { + lua_pushcfunction(mState, luaopen_package); + push(LUA_LOADLIBNAME); + call(1, 0); + } + + void importStringLibrary() + { + lua_pushcfunction(mState, luaopen_string); + push(LUA_STRLIBNAME); + call(1, 0); + } + + void importTableLibrary() + { + lua_pushcfunction(mState, luaopen_table); + push(LUA_TABLIBNAME); + call(1, 0); + } + + void importMathLibrary() + { + lua_pushcfunction(mState, luaopen_math); + push(LUA_MATHLIBNAME); + call(1, 0); + } + + void importIoLibrary() + { + lua_pushcfunction(mState, luaopen_io); + push(LUA_IOLIBNAME); + call(1, 0); + } + + void importOsLibrary() + { + lua_pushcfunction(mState, luaopen_os); + push(LUA_OSLIBNAME); + call(1, 0); + } + + void importDebugLibrary() + { + lua_pushcfunction(mState, luaopen_debug); + push(LUA_DBLIBNAME); + call(1, 0); + } + + void importFunction(const std::string& name, const Function& function) { push(function); @@ -815,6 +879,11 @@ private: return (*function)(*script); } + void destroy() + { + if (mIsMainThread) lua_close(mState); + } + lua_State* mState; bool mIsMainThread; std::list mFunctions;