aboutsummaryrefslogtreecommitdiff
path: root/src/lindafactory.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lindafactory.cpp30
1 files changed, 19 insertions, 11 deletions
diff --git a/src/lindafactory.cpp b/src/lindafactory.cpp
index cb801dd..11e2cff 100644
--- a/src/lindafactory.cpp
+++ b/src/lindafactory.cpp
@@ -41,25 +41,33 @@ static constexpr std::string_view kLindaMetatableName{ "Linda" };
41 41
42void LindaFactory::createMetatable(lua_State* L_) const 42void LindaFactory::createMetatable(lua_State* L_) const
43{ 43{
44 static constexpr std::string_view kIndex{ "__index" };
45
44 STACK_CHECK_START_REL(L_, 0); 46 STACK_CHECK_START_REL(L_, 0);
45 lua_newtable(L_); 47 lua_newtable(L_); // L_: mt
46 // metatable is its own index
47 lua_pushvalue(L_, -1);
48 lua_setfield(L_, -2, "__index");
49 48
50 // protect metatable from external access 49 // protect metatable from external access
51 luaG_pushstring(L_, kLindaMetatableName); 50 luaG_pushstring(L_, kLindaMetatableName); // L_: mt "<name>"
52 lua_setfield(L_, -2, "__metatable"); 51 lua_setfield(L_, -2, "__metatable"); // L_: mt
53 52
54 // the linda functions 53 // the linda functions
55 luaG_registerlibfuncs(L_, mLindaMT); 54 luaG_registerlibfuncs(L_, mLindaMT);
56 55
57 // some constants 56 // some constants
58 kLindaBatched.pushKey(L_); 57 kLindaBatched.pushKey(L_); // L_: mt kLindaBatched
59 lua_setfield(L_, -2, "batched"); 58 lua_setfield(L_, -2, "batched"); // L_: mt
60 59
61 kNilSentinel.pushKey(L_); 60 kNilSentinel.pushKey(L_); // L_: mt kNilSentinel
62 lua_setfield(L_, -2, "null"); 61 lua_setfield(L_, -2, "null"); // L_: mt
62
63 // if the metatable contains __index, leave it as is
64 if (luaG_getfield(L_, kIdxTop, kIndex) != LuaType::NIL) { // L_: mt __index
65 lua_pop(L_, 1); // L_: mt __index
66 } else {
67 // metatable is its own index
68 lua_pushvalue(L_, kIdxTop); // L_: mt mt
69 luaG_setfield(L_, StackIndex{ -2 }, kIndex); // L_: mt
70 }
63 71
64 STACK_CHECK(L_, 1); 72 STACK_CHECK(L_, 1);
65} 73}