#pragma once #include "lua/lua.h" #include "lua/lualib.h" #include "lua/lauxlib.h" #include class LuaVM { public: LuaVM() { _L = luaL_newstate(); luaL_openlibs(_L); _managed = true; } LuaVM(lua_State* L, bool managed=false) { _L = L; _managed = managed; } /// NonMoveable,NonCopyable LuaVM(const LuaVM&) = delete; LuaVM& operator = (const LuaVM&) = delete; LuaVM(LuaVM&&) = delete; LuaVM& operator = (LuaVM&&) = delete; ~LuaVM() { if (_managed) { lua_close(_L); } } operator lua_State* () { return _L; } private: lua_State* _L; bool _managed; }; template T& getuserdata(lua_State* L, int index) // LUA_TUSERDATA { return *(T*)luaL_checkudata(L, index, T::tname); } template T getvalue(lua_State* L, int index); template T& pushvalue(lua_State* L) { return *(T*)lua_newuserdata(L, sizeof(T)); } void pushvalue(lua_State* L, int val); void pushvalue(lua_State* L, double val); void pushvalue(lua_State* L, const char* str); void pushvalue(lua_State* L, const char* str, int len); void pushvalue(lua_State* L, const std::string& str); void pushvalue(lua_State* L, lua_CFunction fn); void pushvalue(lua_State* L, lua_CFunction fn, int nup);