diff options
author | chai <chaifix@163.com> | 2019-03-19 23:06:27 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-03-19 23:06:27 +0800 |
commit | 1497dccd63a84b7ee2b229b1ad9c5c02718f2a78 (patch) | |
tree | f8d1bff50da13e126d08c7345653e002e293202d /source/3rd-party/Luax/luax_enum.cpp | |
parent | 5e2a973516e0729b225da9de0b03015dc5854ac4 (diff) |
*rename
Diffstat (limited to 'source/3rd-party/Luax/luax_enum.cpp')
-rw-r--r-- | source/3rd-party/Luax/luax_enum.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/source/3rd-party/Luax/luax_enum.cpp b/source/3rd-party/Luax/luax_enum.cpp new file mode 100644 index 0000000..88bbab4 --- /dev/null +++ b/source/3rd-party/Luax/luax_enum.cpp @@ -0,0 +1,67 @@ +#include "luax_enum.h" +#include "luax_state.h" +#include "luax_runtime.h" + +namespace Luax +{ + + /// + /// ֻmetatable__index + /// + int l_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 l_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 LUAX_ENABLE_PLAIN_ENABLE + int LuaxPlainEnum::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, 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 |