]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Script.hh
foundational changes; tying up some loose ends
[chaz/yoink] / src / Moof / Script.hh
index df00619d69e59b4dafcf644d5782e2ab53ac1eac..2f285cecc2bcdd740529783fcc8d4f7ea0fa581b 100644 (file)
@@ -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<Function>     mFunctions;
This page took 0.021092 seconds and 4 git commands to generate.