aboutsummaryrefslogtreecommitdiff
path: root/src/lindafactory.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lindafactory.cpp')
-rw-r--r--src/lindafactory.cpp29
1 files changed, 14 insertions, 15 deletions
diff --git a/src/lindafactory.cpp b/src/lindafactory.cpp
index 6e3f759..483037f 100644
--- a/src/lindafactory.cpp
+++ b/src/lindafactory.cpp
@@ -47,26 +47,22 @@ void LindaFactory::createMetatable(lua_State* L_) const
47 lua_newtable(L_); // L_: mt 47 lua_newtable(L_); // L_: mt
48 48
49 // protect metatable from external access 49 // protect metatable from external access
50 luaG_pushstring(L_, kLindaMetatableName); // L_: mt "<name>" 50 luaW_pushstring(L_, kLindaMetatableName); // L_: mt "<name>"
51 lua_setfield(L_, -2, "__metatable"); // L_: mt 51 lua_setfield(L_, -2, "__metatable"); // L_: mt
52 52
53 // the linda functions 53 // the linda functions
54 luaG_registerlibfuncs(L_, mLindaMT); 54 luaW_registerlibfuncs(L_, mLindaMT);
55
56 // some constants
57 kLindaBatched.pushKey(L_); // L_: mt kLindaBatched
58 lua_setfield(L_, -2, "batched"); // L_: mt
59 55
60 kNilSentinel.pushKey(L_); // L_: mt kNilSentinel 56 kNilSentinel.pushKey(L_); // L_: mt kNilSentinel
61 lua_setfield(L_, -2, "null"); // L_: mt 57 lua_setfield(L_, -2, "null"); // L_: mt
62 58
63 // if the metatable contains __index, leave it as is 59 // if the metatable contains __index, leave it as is
64 if (luaG_getfield(L_, kIdxTop, kIndex) != LuaType::NIL) { // L_: mt __index 60 if (luaW_getfield(L_, kIdxTop, kIndex) != LuaType::NIL) { // L_: mt __index
65 lua_pop(L_, 1); // L_: mt __index 61 lua_pop(L_, 1); // L_: mt __index
66 } else { 62 } else {
67 // metatable is its own index 63 // metatable is its own index
68 lua_pushvalue(L_, kIdxTop); // L_: mt mt 64 lua_pushvalue(L_, kIdxTop); // L_: mt mt
69 luaG_setfield(L_, StackIndex{ -2 }, kIndex); // L_: mt 65 luaW_setfield(L_, StackIndex{ -2 }, kIndex); // L_: mt
70 } 66 }
71 67
72 STACK_CHECK(L_, 1); 68 STACK_CHECK(L_, 1);
@@ -112,27 +108,30 @@ std::string_view LindaFactory::moduleName() const
112 108
113DeepPrelude* LindaFactory::newDeepObjectInternal(lua_State* const L_) const 109DeepPrelude* LindaFactory::newDeepObjectInternal(lua_State* const L_) const
114{ 110{
115 // we always expect name and group at the bottom of the stack (either can be nil). any extra stuff we ignore and keep unmodified 111 STACK_CHECK_START_REL(L_, 0);
116 std::string_view _linda_name{ luaG_tostring(L_, StackIndex{ 1 }) }; 112 // we always expect name, wake_period, group at the bottom of the stack (either can be nil). any extra stuff we ignore and keep unmodified
117 LindaGroup _linda_group{ static_cast<int>(lua_tointeger(L_, 2)) }; 113 std::string_view _linda_name{ luaW_tostring(L_, StackIndex{ 1 }) };
114 auto const _wake_period{ static_cast<lua_Duration>(lua_tonumber(L_, 2)) };
115 LindaGroup const _linda_group{ static_cast<int>(lua_tointeger(L_, 3)) };
118 116
119 // store in the linda the location of the script that created it 117 // store in the linda the location of the script that created it
120 if (_linda_name == "auto") { 118 if (_linda_name == "auto") {
121 lua_Debug _ar; 119 lua_Debug _ar;
122 if (lua_getstack(L_, 1, &_ar) == 1) { // 1 because we want the name of the function that called lanes.linda (where we currently are) 120 if (lua_getstack(L_, 1, &_ar) == 1) { // 1 because we want the name of the function that called lanes.linda (where we currently are)
123 lua_getinfo(L_, "Sln", &_ar); 121 lua_getinfo(L_, "Sln", &_ar);
124 _linda_name = luaG_pushstring(L_, "%s:%d", _ar.short_src, _ar.currentline); 122 _linda_name = luaW_pushstring(L_, "%s:%d", _ar.short_src, _ar.currentline);
125 } else { 123 } else {
126 _linda_name = luaG_pushstring(L_, "<unresolved>"); 124 _linda_name = luaW_pushstring(L_, "<unresolved>");
127 } 125 }
128 // since the name is not empty, it is at slot 1, and we can replace "auto" with the result, just in case 126 // since the name is not empty, it is at slot 1, and we can replace "auto" with the result, just in case
129 LUA_ASSERT(L_, luaG_tostring(L_, StackIndex{ 1 }) == "auto"); 127 LUA_ASSERT(L_, luaW_tostring(L_, StackIndex{ 1 }) == "auto");
130 lua_replace(L_, 1); 128 lua_replace(L_, 1);
131 } 129 }
132 130
133 // The deep data is allocated separately of Lua stack; we might no longer be around when last reference to it is being released. 131 // The deep data is allocated separately of Lua stack; we might no longer be around when last reference to it is being released.
134 // One can use any memory allocation scheme. Just don't use L's allocF because we don't know which state will get the honor of GCing the linda 132 // One can use any memory allocation scheme. Just don't use L's allocF because we don't know which state will get the honor of GCing the linda
135 Universe* const _U{ Universe::Get(L_) }; 133 Universe* const _U{ Universe::Get(L_) };
136 Linda* const _linda{ new (_U) Linda{ _U, _linda_group, _linda_name } }; 134 Linda* const _linda{ new (_U) Linda{ _U, _linda_name, _wake_period, _linda_group } };
135 STACK_CHECK(L_, 0);
137 return _linda; 136 return _linda;
138} 137}