1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/*
* LINDAFACTORY.CPP Copyright (c) 2024-, Benoit Germain
*
* Linda deep userdata factory
*/
/*
===============================================================================
Copyright (C) 2024- benoit Germain <bnt.germain@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================================================
*/
#include "_pch.hpp"
#include "lindafactory.hpp"
#include "linda.hpp"
static constexpr std::string_view kLindaMetatableName{ "Linda" };
// #################################################################################################
void LindaFactory::createMetatable(lua_State* L_) const
{
static constexpr std::string_view kIndex{ "__index" };
STACK_CHECK_START_REL(L_, 0);
lua_newtable(L_); // L_: mt
// protect metatable from external access
luaG_pushstring(L_, kLindaMetatableName); // L_: mt "<name>"
lua_setfield(L_, -2, "__metatable"); // L_: mt
// the linda functions
luaG_registerlibfuncs(L_, mLindaMT);
// some constants
kLindaBatched.pushKey(L_); // L_: mt kLindaBatched
lua_setfield(L_, -2, "batched"); // L_: mt
kNilSentinel.pushKey(L_); // L_: mt kNilSentinel
lua_setfield(L_, -2, "null"); // L_: mt
// if the metatable contains __index, leave it as is
if (luaG_getfield(L_, kIdxTop, kIndex) != LuaType::NIL) { // L_: mt __index
lua_pop(L_, 1); // L_: mt __index
} else {
// metatable is its own index
lua_pushvalue(L_, kIdxTop); // L_: mt mt
luaG_setfield(L_, StackIndex{ -2 }, kIndex); // L_: mt
}
STACK_CHECK(L_, 1);
}
// #################################################################################################
void LindaFactory::deleteDeepObjectInternal(lua_State* L_, DeepPrelude* o_) const
{
Linda* const _linda{ static_cast<Linda*>(o_) };
LUA_ASSERT(L_, _linda && !_linda->inKeeperOperation());
Keeper* const _myKeeper{ _linda->whichKeeper() };
// if collected after the universe, keepers are already destroyed, and there is nothing to clear
if (_myKeeper) {
// if collected from my own keeper, we can't acquire/release it
// because we are already inside a protected area, and trying to do so would deadlock!
bool const _need_acquire_release{ _myKeeper->K != L_ };
// Clean associated structures in the keeper state.
Keeper* const _keeper{ _need_acquire_release ? _linda->acquireKeeper() : _myKeeper };
LUA_ASSERT(L_, _keeper == _myKeeper); // should always be the same
// hopefully this won't ever raise an error as we would jump to the closest pcall site while forgetting to release the keeper mutex...
[[maybe_unused]] KeeperCallResult const result{ keeper_call(_keeper->K, KEEPER_API(destruct), L_, _linda, kIdxNone) };
LUA_ASSERT(L_, result.has_value() && result.value() == 0);
if (_need_acquire_release) {
_linda->releaseKeeper(_keeper);
}
}
delete _linda; // operator delete overload ensures things go as expected
}
// #################################################################################################
std::string_view LindaFactory::moduleName() const
{
// linda is a special case because we know lanes must be loaded from the main lua state
// to be able to ever get here, so we know it will remain loaded as long a the main state is around
// in other words, forever.
return std::string_view{};
}
// #################################################################################################
DeepPrelude* LindaFactory::newDeepObjectInternal(lua_State* const L_) const
{
// we always expect name and group at the bottom of the stack (either can be nil). any extra stuff we ignore and keep unmodified
std::string_view _linda_name{ luaG_tostring(L_, StackIndex{ 1 }) };
LindaGroup _linda_group{ static_cast<int>(lua_tointeger(L_, 2)) };
// store in the linda the location of the script that created it
if (_linda_name == "auto") {
lua_Debug _ar;
if (lua_getstack(L_, 1, &_ar) == 1) { // 1 because we want the name of the function that called lanes.linda (where we currently are)
lua_getinfo(L_, "Sln", &_ar);
_linda_name = luaG_pushstring(L_, "%s:%d", _ar.short_src, _ar.currentline);
} else {
_linda_name = luaG_pushstring(L_, "<unresolved>");
}
// since the name is not empty, it is at slot 1, and we can replace "auto" with the result, just in case
LUA_ASSERT(L_, luaG_tostring(L_, StackIndex{ 1 }) == "auto");
lua_replace(L_, 1);
}
// The deep data is allocated separately of Lua stack; we might no longer be around when last reference to it is being released.
// 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
Universe* const _U{ Universe::Get(L_) };
Linda* const _linda{ new (_U) Linda{ _U, _linda_group, _linda_name } };
return _linda;
}
|