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