aboutsummaryrefslogtreecommitdiff
path: root/deep_test
diff options
context:
space:
mode:
authorBenoit Germain <bnt.germain@gmail.com>2024-04-14 18:27:10 +0200
committerBenoit Germain <bnt.germain@gmail.com>2024-04-14 18:27:10 +0200
commit69d40c81d8343a1af7e0fe61fbf20a4cf5880c25 (patch)
treecbf7aa525868040820ce6743f1a30fbb59926407 /deep_test
parent0d9c9bae120f92274e1c68f7abdebfcf2c24405d (diff)
parent00970610dc8fbd00a11d3b69e4702933a592ce9f (diff)
downloadlanes-69d40c81d8343a1af7e0fe61fbf20a4cf5880c25.tar.gz
lanes-69d40c81d8343a1af7e0fe61fbf20a4cf5880c25.tar.bz2
lanes-69d40c81d8343a1af7e0fe61fbf20a4cf5880c25.zip
Merge branch 'master' of https://github.com/LuaLanes/lanes
Diffstat (limited to 'deep_test')
-rw-r--r--deep_test/deep_test.cpp278
1 files changed, 139 insertions, 139 deletions
diff --git a/deep_test/deep_test.cpp b/deep_test/deep_test.cpp
index 7c8180f..3467939 100644
--- a/deep_test/deep_test.cpp
+++ b/deep_test/deep_test.cpp
@@ -10,114 +10,114 @@
10// a lanes-deep userdata. needs DeepPrelude and luaG_newdeepuserdata from Lanes code. 10// a lanes-deep userdata. needs DeepPrelude and luaG_newdeepuserdata from Lanes code.
11struct MyDeepUserdata : public DeepPrelude // Deep userdata MUST start with a DeepPrelude 11struct MyDeepUserdata : public DeepPrelude // Deep userdata MUST start with a DeepPrelude
12{ 12{
13 lua_Integer val{ 0 }; 13 lua_Integer val{ 0 };
14}; 14};
15 15
16// ################################################################################################ 16// ################################################################################################
17 17
18static void* deep_test_id( lua_State* L, DeepOp op_) 18[[nodiscard]] static void* deep_test_id(lua_State* L, DeepOp op_)
19{ 19{
20 switch( op_) 20 switch( op_)
21 { 21 {
22 case DeepOp::New: 22 case DeepOp::New:
23 { 23 {
24 MyDeepUserdata* deep_test = new MyDeepUserdata; 24 MyDeepUserdata* deep_test = new MyDeepUserdata;
25 return deep_test; 25 return deep_test;
26 } 26 }
27 27
28 case DeepOp::Delete: 28 case DeepOp::Delete:
29 { 29 {
30 MyDeepUserdata* deep_test = static_cast<MyDeepUserdata*>(lua_touserdata( L, 1)); 30 MyDeepUserdata* deep_test = static_cast<MyDeepUserdata*>(lua_touserdata( L, 1));
31 delete deep_test; 31 delete deep_test;
32 return nullptr; 32 return nullptr;
33 } 33 }
34 34
35 case DeepOp::Metatable: 35 case DeepOp::Metatable:
36 { 36 {
37 luaL_getmetatable( L, "deep"); // mt 37 luaL_getmetatable( L, "deep"); // mt
38 return nullptr; 38 return nullptr;
39 } 39 }
40 40
41 case DeepOp::Module: 41 case DeepOp::Module:
42 return (void*)"deep_test"; 42 return (void*)"deep_test";
43 43
44 default: 44 default:
45 { 45 {
46 return nullptr; 46 return nullptr;
47 } 47 }
48 } 48 }
49} 49}
50 50
51// ################################################################################################ 51// ################################################################################################
52 52
53static int deep_set( lua_State* L) 53[[nodiscard]] static int deep_set(lua_State* L)
54{ 54{
55 MyDeepUserdata* self = static_cast<MyDeepUserdata*>(luaG_todeep(L, deep_test_id, 1)); 55 MyDeepUserdata* self = static_cast<MyDeepUserdata*>(luaG_todeep(L, deep_test_id, 1));
56 lua_Integer i = lua_tointeger( L, 2); 56 lua_Integer i = lua_tointeger( L, 2);
57 self->val = i; 57 self->val = i;
58 return 0; 58 return 0;
59} 59}
60 60
61// ################################################################################################ 61// ################################################################################################
62 62
63// won't actually do anything as deep userdata don't have uservalue slots 63// won't actually do anything as deep userdata don't have uservalue slots
64static int deep_setuv( lua_State* L) 64[[nodiscard]] static int deep_setuv(lua_State* L)
65{ 65{
66 MyDeepUserdata* self = static_cast<MyDeepUserdata*>(luaG_todeep(L, deep_test_id, 1)); 66 MyDeepUserdata* self = static_cast<MyDeepUserdata*>(luaG_todeep(L, deep_test_id, 1));
67 int uv = (int) luaL_optinteger(L, 2, 1); 67 int uv = (int) luaL_optinteger(L, 2, 1);
68 lua_settop( L, 3); 68 lua_settop( L, 3);
69 lua_pushboolean( L, lua_setiuservalue( L, 1, uv) != 0); 69 lua_pushboolean( L, lua_setiuservalue( L, 1, uv) != 0);
70 return 1; 70 return 1;
71} 71}
72 72
73// ################################################################################################ 73// ################################################################################################
74 74
75// won't actually do anything as deep userdata don't have uservalue slots 75// won't actually do anything as deep userdata don't have uservalue slots
76static int deep_getuv( lua_State* L) 76[[nodiscard]] static int deep_getuv(lua_State* L)
77{ 77{
78 MyDeepUserdata* self = static_cast<MyDeepUserdata*>(luaG_todeep(L, deep_test_id, 1)); 78 MyDeepUserdata* self = static_cast<MyDeepUserdata*>(luaG_todeep(L, deep_test_id, 1));
79 int uv = (int) luaL_optinteger(L, 2, 1); 79 int uv = (int) luaL_optinteger(L, 2, 1);
80 lua_getiuservalue( L, 1, uv); 80 lua_getiuservalue( L, 1, uv);
81 return 1; 81 return 1;
82} 82}
83 83
84// ################################################################################################ 84// ################################################################################################
85 85
86static int deep_tostring( lua_State* L) 86[[nodiscard]] static int deep_tostring(lua_State* L)
87{ 87{
88 MyDeepUserdata* self = static_cast<MyDeepUserdata*>(luaG_todeep(L, deep_test_id, 1)); 88 MyDeepUserdata* self = static_cast<MyDeepUserdata*>(luaG_todeep(L, deep_test_id, 1));
89 lua_pushfstring(L, "%p:deep(%d)", lua_topointer(L, 1), self->val); 89 lua_pushfstring(L, "%p:deep(%d)", lua_topointer(L, 1), self->val);
90 return 1; 90 return 1;
91} 91}
92 92
93// ################################################################################################ 93// ################################################################################################
94 94
95static int deep_gc( lua_State* L) 95[[nodiscard]] static int deep_gc(lua_State* L)
96{ 96{
97 MyDeepUserdata* self = static_cast<MyDeepUserdata*>(luaG_todeep(L, deep_test_id, 1)); 97 MyDeepUserdata* self = static_cast<MyDeepUserdata*>(luaG_todeep(L, deep_test_id, 1));
98 return 0; 98 return 0;
99} 99}
100 100
101// ################################################################################################ 101// ################################################################################################
102 102
103static luaL_Reg const deep_mt[] = 103static luaL_Reg const deep_mt[] =
104{ 104{
105 { "__tostring", deep_tostring}, 105 { "__tostring", deep_tostring},
106 { "__gc", deep_gc}, 106 { "__gc", deep_gc},
107 { "set", deep_set}, 107 { "set", deep_set},
108 { "setuv", deep_setuv}, 108 { "setuv", deep_setuv},
109 { "getuv", deep_getuv}, 109 { "getuv", deep_getuv},
110 { nullptr, nullptr } 110 { nullptr, nullptr }
111}; 111};
112 112
113// ################################################################################################ 113// ################################################################################################
114 114
115int luaD_new_deep( lua_State* L) 115int luaD_new_deep( lua_State* L)
116{ 116{
117 int nuv = (int) luaL_optinteger( L, 1, 0); 117 int const nuv{ static_cast<int>(luaL_optinteger(L, 1, 0)) };
118 // no additional parameter to luaG_newdeepuserdata! 118 // no additional parameter to luaG_newdeepuserdata!
119 lua_settop( L, 0); 119 lua_settop(L, 0);
120 return luaG_newdeepuserdata( L, deep_test_id, nuv); 120 return luaG_newdeepuserdata(Dest{ L }, deep_test_id, nuv);
121} 121}
122 122
123// ################################################################################################ 123// ################################################################################################
@@ -125,101 +125,101 @@ int luaD_new_deep( lua_State* L)
125 125
126struct MyClonableUserdata 126struct MyClonableUserdata
127{ 127{
128 lua_Integer val; 128 lua_Integer val;
129}; 129};
130 130
131// ################################################################################################ 131// ################################################################################################
132 132
133static int clonable_set( lua_State* L) 133[[nodiscard]] static int clonable_set(lua_State* L)
134{ 134{
135 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)); 135 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
136 lua_Integer i = lua_tointeger(L, 2); 136 lua_Integer i = lua_tointeger(L, 2);
137 self->val = i; 137 self->val = i;
138 return 0; 138 return 0;
139} 139}
140 140
141// ################################################################################################ 141// ################################################################################################
142 142
143static int clonable_setuv( lua_State* L) 143[[nodiscard]] static int clonable_setuv(lua_State* L)
144{ 144{
145 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)); 145 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
146 int uv = (int) luaL_optinteger(L, 2, 1); 146 int uv = (int) luaL_optinteger(L, 2, 1);
147 lua_settop( L, 3); 147 lua_settop( L, 3);
148 lua_pushboolean( L, lua_setiuservalue( L, 1, uv) != 0); 148 lua_pushboolean( L, lua_setiuservalue( L, 1, uv) != 0);
149 return 1; 149 return 1;
150} 150}
151 151
152// ################################################################################################ 152// ################################################################################################
153 153
154static int clonable_getuv( lua_State* L) 154[[nodiscard]] static int clonable_getuv(lua_State* L)
155{ 155{
156 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)); 156 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
157 int uv = (int) luaL_optinteger(L, 2, 1); 157 int uv = (int) luaL_optinteger(L, 2, 1);
158 lua_getiuservalue( L, 1, uv); 158 lua_getiuservalue( L, 1, uv);
159 return 1; 159 return 1;
160} 160}
161 161
162// ################################################################################################ 162// ################################################################################################
163 163
164static int clonable_tostring(lua_State* L) 164[[nodiscard]] static int clonable_tostring(lua_State* L)
165{ 165{
166 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)); 166 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
167 lua_pushfstring(L, "%p:clonable(%d)", lua_topointer(L, 1), self->val); 167 lua_pushfstring(L, "%p:clonable(%d)", lua_topointer(L, 1), self->val);
168 return 1; 168 return 1;
169} 169}
170 170
171// ################################################################################################ 171// ################################################################################################
172 172
173static int clonable_gc( lua_State* L) 173[[nodiscard]] static int clonable_gc(lua_State* L)
174{ 174{
175 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)); 175 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
176 return 0; 176 return 0;
177} 177}
178 178
179// ################################################################################################ 179// ################################################################################################
180 180
181// this is all we need to make a userdata lanes-clonable. no dependency on Lanes code. 181// this is all we need to make a userdata lanes-clonable. no dependency on Lanes code.
182static int clonable_lanesclone( lua_State* L) 182[[nodiscard]] static int clonable_lanesclone(lua_State* L)
183{ 183{
184 switch( lua_gettop( L)) 184 switch( lua_gettop( L))
185 { 185 {
186 case 3: 186 case 3:
187 { 187 {
188 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)); 188 MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1));
189 MyClonableUserdata* from = static_cast<MyClonableUserdata*>(lua_touserdata(L, 2)); 189 MyClonableUserdata* from = static_cast<MyClonableUserdata*>(lua_touserdata(L, 2));
190 size_t len = lua_tointeger( L, 3); 190 size_t len = lua_tointeger( L, 3);
191 assert( len == sizeof(MyClonableUserdata)); 191 assert( len == sizeof(MyClonableUserdata));
192 *self = *from; 192 *self = *from;
193 } 193 }
194 return 0; 194 return 0;
195 195
196 default: 196 default:
197 (void) luaL_error( L, "Lanes called clonable_lanesclone with unexpected parameters"); 197 (void) luaL_error( L, "Lanes called clonable_lanesclone with unexpected parameters");
198 } 198 }
199 return 0; 199 return 0;
200} 200}
201 201
202// ################################################################################################ 202// ################################################################################################
203 203
204static luaL_Reg const clonable_mt[] = 204static luaL_Reg const clonable_mt[] =
205{ 205{
206 { "__tostring", clonable_tostring}, 206 { "__tostring", clonable_tostring},
207 { "__gc", clonable_gc}, 207 { "__gc", clonable_gc},
208 { "__lanesclone", clonable_lanesclone}, 208 { "__lanesclone", clonable_lanesclone},
209 { "set", clonable_set}, 209 { "set", clonable_set},
210 { "setuv", clonable_setuv}, 210 { "setuv", clonable_setuv},
211 { "getuv", clonable_getuv}, 211 { "getuv", clonable_getuv},
212 { nullptr, nullptr } 212 { nullptr, nullptr }
213}; 213};
214 214
215// ################################################################################################ 215// ################################################################################################
216 216
217int luaD_new_clonable( lua_State* L) 217int luaD_new_clonable( lua_State* L)
218{ 218{
219 int nuv = (int) luaL_optinteger( L, 1, 1); 219 int const nuv{ static_cast<int>(luaL_optinteger(L, 1, 1)) };
220 lua_newuserdatauv( L, sizeof(MyClonableUserdata), nuv); 220 lua_newuserdatauv( L, sizeof(MyClonableUserdata), nuv);
221 luaL_setmetatable( L, "clonable"); 221 luaL_setmetatable( L, "clonable");
222 return 1; 222 return 1;
223} 223}
224 224
225// ################################################################################################ 225// ################################################################################################
@@ -227,33 +227,33 @@ int luaD_new_clonable( lua_State* L)
227 227
228static luaL_Reg const deep_module[] = 228static luaL_Reg const deep_module[] =
229{ 229{
230 { "new_deep", luaD_new_deep}, 230 { "new_deep", luaD_new_deep},
231 { "new_clonable", luaD_new_clonable}, 231 { "new_clonable", luaD_new_clonable},
232 { nullptr, nullptr } 232 { nullptr, nullptr }
233}; 233};
234 234
235// ################################################################################################ 235// ################################################################################################
236 236
237LANES_API int luaopen_deep_test(lua_State* L) 237LANES_API int luaopen_deep_test(lua_State* L)
238{ 238{
239 luaL_newlib( L, deep_module); // M 239 luaL_newlib( L, deep_module); // M
240 240
241 // preregister the metatables for the types we can instantiate so that Lanes can know about them 241 // preregister the metatables for the types we can instantiate so that Lanes can know about them
242 if( luaL_newmetatable( L, "clonable")) // M mt 242 if( luaL_newmetatable( L, "clonable")) // M mt
243 { 243 {
244 luaL_setfuncs( L, clonable_mt, 0); 244 luaL_setfuncs( L, clonable_mt, 0);
245 lua_pushvalue(L, -1); // M mt mt 245 lua_pushvalue(L, -1); // M mt mt
246 lua_setfield(L, -2, "__index"); // M mt 246 lua_setfield(L, -2, "__index"); // M mt
247 } 247 }
248 lua_setfield(L, -2, "__clonableMT"); // M 248 lua_setfield(L, -2, "__clonableMT"); // M
249 249
250 if( luaL_newmetatable( L, "deep")) // mt 250 if( luaL_newmetatable( L, "deep")) // mt
251 { 251 {
252 luaL_setfuncs( L, deep_mt, 0); 252 luaL_setfuncs( L, deep_mt, 0);
253 lua_pushvalue(L, -1); // mt mt 253 lua_pushvalue(L, -1); // mt mt
254 lua_setfield(L, -2, "__index"); // mt 254 lua_setfield(L, -2, "__index"); // mt
255 } 255 }
256 lua_setfield(L, -2, "__deepMT"); // M 256 lua_setfield(L, -2, "__deepMT"); // M
257 257
258 return 1; 258 return 1;
259} 259}