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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#include "lanes/src/_pch.h"
#include "lanes/src/deep.h"
#include "lanes/src/compat.h"
class MyDeepFactory : public DeepFactory
{
public:
static MyDeepFactory Instance;
private:
void createMetatable(lua_State* const L_) const override
{
luaL_getmetatable(L_, "deep");
}
void deleteDeepObjectInternal(lua_State* const L_, DeepPrelude* o_) const override;
[[nodiscard]] DeepPrelude* newDeepObjectInternal(lua_State* const L_) const override;
[[nodiscard]] std::string_view moduleName() const override { return std::string_view{ "deep_test" }; }
};
/*static*/ MyDeepFactory MyDeepFactory::Instance{};
// #################################################################################################
// a lanes-deep userdata. needs DeepPrelude and luaG_newdeepuserdata from Lanes code.
struct MyDeepUserdata : public DeepPrelude // Deep userdata MUST start with a DeepPrelude
{
std::atomic<int> inUse{};
lua_Integer val{ 0 };
};
// #################################################################################################
DeepPrelude* MyDeepFactory::newDeepObjectInternal(lua_State* const L_) const
{
MyDeepUserdata* const _deep_test{ new MyDeepUserdata{ MyDeepFactory::Instance } };
return _deep_test;
}
// #################################################################################################
void MyDeepFactory::deleteDeepObjectInternal(lua_State* const L_, DeepPrelude* const o_) const
{
MyDeepUserdata* const _deep_test{ static_cast<MyDeepUserdata*>(o_) };
delete _deep_test;
}
// #################################################################################################
[[nodiscard]] static int deep_gc(lua_State* L)
{
MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L, StackIndex{ 1 })) };
luaL_argcheck(L, 1, !_self->inUse.load(), "being collected while in use!");
return 0;
}
// #################################################################################################
[[nodiscard]] static int deep_tostring(lua_State* L)
{
MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L, StackIndex{ 1 })) };
_self->inUse.fetch_add(1, std::memory_order_seq_cst);
luaG_pushstring(L, "%p:deep(%d)", _self, _self->val);
_self->inUse.fetch_sub(1, std::memory_order_seq_cst);
return 1;
}
// #################################################################################################
// won't actually do anything as deep userdata don't have uservalue slots
[[nodiscard]] static int deep_getuv(lua_State* L)
{
MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L, StackIndex{ 1 })) };
_self->inUse.fetch_add(1, std::memory_order_seq_cst);
int _uv = (int) luaL_optinteger(L, 2, 1);
lua_getiuservalue(L, StackIndex{ 1 }, _uv);
_self->inUse.fetch_sub(1, std::memory_order_seq_cst);
return 1;
}
// #################################################################################################
[[nodiscard]] static int deep_invoke(lua_State* L)
{
MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L, StackIndex{ 1 })) };
luaL_argcheck(L, 2, lua_gettop(L) >= 2, "need something to call");
_self->inUse.fetch_add(1, std::memory_order_seq_cst);
lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
_self->inUse.fetch_sub(1, std::memory_order_seq_cst);
return 1;
}
// #################################################################################################
[[nodiscard]] static int deep_set(lua_State* const L_)
{
MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L_, StackIndex{ 1 })) };
_self->inUse.fetch_add(1, std::memory_order_seq_cst);
lua_Integer _i = lua_tointeger(L_, 2);
_self->val = _i;
_self->inUse.fetch_sub(1, std::memory_order_seq_cst);
return 0;
}
// #################################################################################################
[[nodiscard]] static int deep_setuv(lua_State* L)
{
MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L, StackIndex{ 1 })) };
_self->inUse.fetch_add(1, std::memory_order_seq_cst);
int _uv = (int) luaL_optinteger(L, 2, 1);
lua_settop(L, 3);
lua_pushboolean(L, lua_setiuservalue(L, StackIndex{ 1 }, _uv) != 0);
_self->inUse.fetch_sub(1, std::memory_order_seq_cst);
return 1;
}
// #################################################################################################
static luaL_Reg const deep_mt[] = {
{ "__gc", deep_gc },
{ "__tostring", deep_tostring },
{ "getuv", deep_getuv },
{ "invoke", deep_invoke },
{ "set", deep_set },
{ "setuv", deep_setuv },
{ nullptr, nullptr }
};
// #################################################################################################
int luaD_new_deep(lua_State* L)
{
int const nuv{ static_cast<int>(luaL_optinteger(L, 1, 0)) };
lua_settop(L, 0);
MyDeepFactory::Instance.pushDeepUserdata(DestState{ L }, nuv);
return 1;
}
// #################################################################################################
// #################################################################################################
struct MyClonableUserdata
{
lua_Integer val;
};
// #################################################################################################
[[nodiscard]] static int clonable_set(lua_State* L)
{
MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
lua_Integer i = lua_tointeger(L, 2);
self->val = i;
return 0;
}
// #################################################################################################
[[nodiscard]] static int clonable_setuv(lua_State* L)
{
MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
int uv = (int) luaL_optinteger(L, 2, 1);
lua_settop(L, 3);
lua_pushboolean(L, lua_setiuservalue(L, StackIndex{ 1 }, uv) != 0);
return 1;
}
// #################################################################################################
[[nodiscard]] static int clonable_getuv(lua_State* L)
{
MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
int uv = (int) luaL_optinteger(L, 2, 1);
lua_getiuservalue(L, StackIndex{ 1 }, uv);
return 1;
}
// #################################################################################################
[[nodiscard]] static int clonable_tostring(lua_State* L)
{
MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
luaG_pushstring(L, "%p:clonable(%d)", lua_topointer(L, 1), self->val);
return 1;
}
// #################################################################################################
[[nodiscard]] static int clonable_gc(lua_State* L)
{
MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
return 0;
}
// #################################################################################################
// this is all we need to make a userdata lanes-clonable. no dependency on Lanes code.
[[nodiscard]] static int clonable_lanesclone(lua_State* L)
{
switch (lua_gettop(L)) {
case 3:
{
MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
MyClonableUserdata* from = static_cast<MyClonableUserdata*>(lua_touserdata(L, 2));
size_t len = lua_tointeger(L, 3);
assert(len == sizeof(MyClonableUserdata));
*self = *from;
}
return 0;
default:
raise_luaL_error(L, "Lanes called clonable_lanesclone with unexpected arguments");
}
return 0;
}
// #################################################################################################
static luaL_Reg const clonable_mt[] = {
{ "__tostring", clonable_tostring },
{ "__gc", clonable_gc },
{ "__lanesclone", clonable_lanesclone },
{ "set", clonable_set },
{ "setuv", clonable_setuv },
{ "getuv", clonable_getuv },
{ nullptr, nullptr }
};
// #################################################################################################
int luaD_new_clonable(lua_State* L)
{
int const _nuv{ static_cast<int>(luaL_optinteger(L, 1, 1)) };
lua_newuserdatauv(L, sizeof(MyClonableUserdata), _nuv);
luaG_setmetatable(L, "clonable");
return 1;
}
// #################################################################################################
// #################################################################################################
static luaL_Reg const deep_module[] = {
{ "new_deep", luaD_new_deep },
{ "new_clonable", luaD_new_clonable },
{ nullptr, nullptr }
};
// #################################################################################################
LANES_API int luaopen_deep_test(lua_State* L)
{
luaG_newlib<std::size(deep_module)>(L, deep_module); // M
// preregister the metatables for the types we can instantiate so that Lanes can know about them
if (luaL_newmetatable(L, "clonable")) // M mt
{
luaG_registerlibfuncs(L, clonable_mt);
lua_pushvalue(L, -1); // M mt mt
lua_setfield(L, -2, "__index"); // M mt
}
lua_setfield(L, -2, "__clonableMT"); // M
if (luaL_newmetatable(L, "deep")) // mt
{
luaG_registerlibfuncs(L, deep_mt);
lua_pushvalue(L, -1); // mt mt
lua_setfield(L, -2, "__index"); // mt
}
lua_setfield(L, -2, "__deepMT"); // M
return 1;
}
|