diff options
author | chai <chaifix@163.com> | 2021-10-18 19:56:41 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-18 19:56:41 +0800 |
commit | 45328cbadd8a946c19a77301f218efbf650e2f28 (patch) | |
tree | 8ec4f3a9737b2cbb9744f8347a56783743be2a4c /Runtime/Lua/LuaBind/LuaBindEnum.cpp | |
parent | b5702ece4c2cf751c252e76fb885a7ec41ccabe8 (diff) |
*misc
Diffstat (limited to 'Runtime/Lua/LuaBind/LuaBindEnum.cpp')
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindEnum.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/Runtime/Lua/LuaBind/LuaBindEnum.cpp b/Runtime/Lua/LuaBind/LuaBindEnum.cpp new file mode 100644 index 0000000..63e2567 --- /dev/null +++ b/Runtime/Lua/LuaBind/LuaBindEnum.cpp @@ -0,0 +1,67 @@ +#include "LuaBindEnum.h" +#include "LuaBindState.h" +#include "LuaBindVM.h" + +namespace LuaBind +{ + + // + // Ö»¶ÁmetatableµÄ__index + // + int _rmt__index(lua_State* L) + { + // params: + // 1: enum table + // 2: key + + // upvalues: + // 1: metatable + + int mt = lua_upvalueindex(1); + lua_pushvalue(L, 2); + lua_rawget(L, mt); + + return 1; + } + + int _rmt__newindex(lua_State* L) + { + // upvalue: + // 1: enum table name + + cc8* name = lua_tostring(L, lua_upvalueindex(1)); + + return luaL_error(L, "Enum called \"%s\" is readonly.", name); + } + + //--------------------------------------------------------------------------------// +#if LUA_BIND_ENABLE_PLAIN_ENUM + int PlainEnum::registry(lua_State* L) + { + // params: + // 1: enum name + // 2: metatable + + cc8* name = luaL_checkstring(L, 1); + + if (!lua_istable(L, 2)) + { + return luaL_error(L, "Create plain enum failed. Require table, but get %s", luaL_typename(L, 2)); + } + + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + + lua_pushstring(L, name); + lua_pushcclosure(L, _rmt__newindex, 1); + lua_setfield(L, -2, "__newindex"); + + lua_newtable(L); // enum table + + lua_pushvalue(L, -2); // metatable + lua_setmetatable(L, -2); + + return 1; + } +#endif +}
\ No newline at end of file |