diff options
author | chai <chaifix@163.com> | 2019-03-16 13:03:50 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-03-16 13:03:50 +0800 |
commit | 1ab2501db0f9e14f138292880e37120e7a6184de (patch) | |
tree | 2d1cfa82a9a6a3a0f16e516f100b2acb6f7e0e4e /Source/3rdParty/Luax/luax_state.cpp | |
parent | 95d27af75eb2aff8020e073eff8dfeb074b5bba6 (diff) |
*luax
Diffstat (limited to 'Source/3rdParty/Luax/luax_state.cpp')
-rw-r--r-- | Source/3rdParty/Luax/luax_state.cpp | 87 |
1 files changed, 70 insertions, 17 deletions
diff --git a/Source/3rdParty/Luax/luax_state.cpp b/Source/3rdParty/Luax/luax_state.cpp index 5253a2c..1414591 100644 --- a/Source/3rdParty/Luax/luax_state.cpp +++ b/Source/3rdParty/Luax/luax_state.cpp @@ -27,23 +27,47 @@ namespace Luax luaL_openlibs(mState); } + void LuaxState::PushGlobalNamespace() + { + int top = GetTop(); + + lua_newtable(mState); // pseudo namespace table + int pnt = GetTop(); + + lua_newtable(mState); // metatable + int mt = GetTop(); + + // __index = _G + // __newindex = _G + lua_pushvalue(mState, LUA_GLOBALSINDEX); + lua_pushvalue(mState, LUA_GLOBALSINDEX); + lua_setfield(mState, mt, "__index"); + lua_setfield(mState, mt, "__newindex"); + + lua_setmetatable(mState, pnt); + + // stack: + // -1 pseudo global namespace + } + void LuaxState::PushNamespace(cc8* name) { - bool isG = !lua_istable(mState, -1); - int idx = isG ? LUA_GLOBALSINDEX : -1; - lua_getfield(mState, idx, name); + assert(IsNamespace(-1)); + + int top = GetTop(); + + lua_getfield(mState, -1, name); if (lua_isnil(mState, -1)) { lua_pop(mState, 1); + lua_newtable(mState); - assert(lua_istable(mState, -1)); lua_pushvalue(mState, -1); - int t = isG ? LUA_GLOBALSINDEX : -3; - lua_setfield(mState, t, name); + lua_setfield(mState, top, name); } // stack: - // -1 namespace + // -1 namespace } void LuaxState::PopNamespace() @@ -52,6 +76,11 @@ namespace Luax lua_pop(mState, 1); } + bool LuaxState::IsNamespace(int idx) + { + return lua_istable(mState, idx); + } + void LuaxState::DoString(const std::string& code) { luaL_dostring(mState, code.c_str()); @@ -146,11 +175,6 @@ namespace Luax lua_pop(mState, n); } - void LuaxState::Settop(int idx) - { - lua_settop(mState, idx); - } - bool LuaxState::IsNil(int idx) { return lua_isnil(mState, idx); @@ -191,6 +215,11 @@ namespace Luax return (mState != 0); } + void LuaxState::Settop(int idx) + { + lua_settop(mState, idx); + } + int LuaxState::GetTop() { return lua_gettop(mState); @@ -244,11 +273,6 @@ namespace Luax return false; } - void LuaxState::RegisterMethods(const luaL_Reg *l) - { - luaL_register(mState, 0, l); - } - void LuaxState::GetField(int idx, cc8* name) { lua_getfield(mState, idx, name); @@ -646,4 +670,33 @@ namespace Luax lua_setfield(L, top, name); } + + void LuaxState::RegisterMethods(const luaL_Reg *l) + { + assert(lua_istable(mState, -1)); + // luaL_registerڶΪգ-1λעluaL_RegЩ + luaL_register(mState, 0, l); + } + + void LuaxState::RegisterMethod(cc8* fname, lua_CFunction func) + { + assert(lua_istable(mState, -1)); + lua_pushcfunction(mState, func); + lua_setfield(mState, -1, fname); + } + + void LuaxState::RegisterPreloader(cc8* libname, lua_CFunction preloader) + { + lua_getglobal(mState, "package"); + lua_getfield(mState, -1, "preload"); + lua_pushcfunction(mState, preloader); + lua_setfield(mState, -2, libname); + lua_pop(mState, 2); + } + + void LuaxState::RegisterLib(cc8* libname, const luaL_Reg* l) + { + luaL_register(mState, libname, l); + } + }
\ No newline at end of file |