diff options
author | Benoit Germain <b n t DOT g e r m a i n AT g m a i l DOT c o m> | 2018-11-07 19:16:36 +0100 |
---|---|---|
committer | Benoit Germain <b n t DOT g e r m a i n AT g m a i l DOT c o m> | 2018-11-07 19:16:36 +0100 |
commit | a142eb1e1ee81919d10b55bb7fa2e33636098d85 (patch) | |
tree | 21ef5c830ce4b4e845454af4274beabd073cc720 /deep_test/deep_test.c | |
parent | 91155c74fc10fa98ad6257d5309bfd13d4a61cf0 (diff) | |
download | lanes-a142eb1e1ee81919d10b55bb7fa2e33636098d85.tar.gz lanes-a142eb1e1ee81919d10b55bb7fa2e33636098d85.tar.bz2 lanes-a142eb1e1ee81919d10b55bb7fa2e33636098d85.zip |
__lanesclone mechanism should actually work now
Diffstat (limited to 'deep_test/deep_test.c')
-rw-r--r-- | deep_test/deep_test.c | 80 |
1 files changed, 59 insertions, 21 deletions
diff --git a/deep_test/deep_test.c b/deep_test/deep_test.c index 8f34fe5..4aac586 100644 --- a/deep_test/deep_test.c +++ b/deep_test/deep_test.c | |||
@@ -80,10 +80,7 @@ static void* deep_test_id( lua_State* L, enum eDeepOp op_) | |||
80 | 80 | ||
81 | case eDO_metatable: | 81 | case eDO_metatable: |
82 | { | 82 | { |
83 | lua_newtable( L); // mt | 83 | luaL_getmetatable( L, "deep"); // mt |
84 | luaL_setfuncs( L, deep_mt, 0); // mt | ||
85 | lua_pushvalue( L, -1); // mt mt | ||
86 | lua_setfield( L, -2, "__index"); // mt | ||
87 | luaG_pushdeepversion( L); // mt version | 84 | luaG_pushdeepversion( L); // mt version |
88 | return NULL; | 85 | return NULL; |
89 | } | 86 | } |
@@ -115,6 +112,16 @@ struct s_MyClonableUserdata | |||
115 | 112 | ||
116 | // ################################################################################################ | 113 | // ################################################################################################ |
117 | 114 | ||
115 | static int clonable_set( lua_State* L) | ||
116 | { | ||
117 | struct s_MyClonableUserdata* self = (struct s_MyClonableUserdata*) lua_touserdata( L, 1); | ||
118 | lua_Integer i = lua_tointeger( L, 2); | ||
119 | self->val = i; | ||
120 | return 0; | ||
121 | } | ||
122 | |||
123 | // ################################################################################################ | ||
124 | |||
118 | static int clonable_tostring(lua_State* L) | 125 | static int clonable_tostring(lua_State* L) |
119 | { | 126 | { |
120 | struct s_MyClonableUserdata* self = (struct s_MyClonableUserdata*) lua_touserdata( L, 1); | 127 | struct s_MyClonableUserdata* self = (struct s_MyClonableUserdata*) lua_touserdata( L, 1); |
@@ -124,13 +131,34 @@ static int clonable_tostring(lua_State* L) | |||
124 | 131 | ||
125 | // ################################################################################################ | 132 | // ################################################################################################ |
126 | 133 | ||
134 | static int clonable_gc( lua_State* L) | ||
135 | { | ||
136 | struct s_MyClonableUserdata* self = (struct s_MyClonableUserdata*) lua_touserdata( L, 1); | ||
137 | return 0; | ||
138 | } | ||
139 | |||
140 | // ################################################################################################ | ||
141 | |||
127 | static int clonable_lanesclone( lua_State* L) | 142 | static int clonable_lanesclone( lua_State* L) |
128 | { | 143 | { |
129 | // no need to set the metatable, the Lane copying mechanism will take care of it | 144 | switch( lua_gettop( L)) |
130 | struct s_MyClonableUserdata* self = lua_touserdata( L, 1); | 145 | { |
131 | struct s_MyClonableUserdata* to = lua_newuserdata( L, sizeof( struct s_MyClonableUserdata)); | 146 | case 0: |
132 | memcpy( to, self, sizeof(struct s_MyClonableUserdata)); | 147 | lua_pushinteger( L, sizeof( struct s_MyClonableUserdata)); |
133 | return 1; | 148 | return 1; |
149 | |||
150 | case 2: | ||
151 | { | ||
152 | struct s_MyClonableUserdata* self = lua_touserdata( L, 1); | ||
153 | struct s_MyClonableUserdata* from = lua_touserdata( L, 2); | ||
154 | *self = *from; | ||
155 | return 0; | ||
156 | } | ||
157 | |||
158 | default: | ||
159 | (void) luaL_error( L, "Lanes called clonable_lanesclone with unexpected parameters"); | ||
160 | } | ||
161 | return 0; | ||
134 | } | 162 | } |
135 | 163 | ||
136 | // ################################################################################################ | 164 | // ################################################################################################ |
@@ -138,9 +166,9 @@ static int clonable_lanesclone( lua_State* L) | |||
138 | static luaL_Reg const clonable_mt[] = | 166 | static luaL_Reg const clonable_mt[] = |
139 | { | 167 | { |
140 | { "__tostring", clonable_tostring}, | 168 | { "__tostring", clonable_tostring}, |
141 | //{ "__gc", deep_gc}, | 169 | { "__gc", clonable_gc}, |
142 | { "__lanesclone", clonable_lanesclone}, | 170 | { "__lanesclone", clonable_lanesclone}, |
143 | //{ "set", deep_set}, | 171 | { "set", clonable_set}, |
144 | { NULL, NULL } | 172 | { NULL, NULL } |
145 | }; | 173 | }; |
146 | 174 | ||
@@ -149,15 +177,7 @@ static luaL_Reg const clonable_mt[] = | |||
149 | int luaD_new_clonable( lua_State* L) | 177 | int luaD_new_clonable( lua_State* L) |
150 | { | 178 | { |
151 | lua_newuserdata( L, sizeof( struct s_MyClonableUserdata)); | 179 | lua_newuserdata( L, sizeof( struct s_MyClonableUserdata)); |
152 | if( luaL_getmetatable( L, "clonable") == LUA_TNIL) // u mt? | 180 | luaL_setmetatable( L, "clonable"); |
153 | { | ||
154 | lua_pop( L, 1); // u | ||
155 | lua_newtable( L); // u mt | ||
156 | luaL_setfuncs( L, clonable_mt, 0); | ||
157 | lua_pushvalue(L, -1); // u mt mt | ||
158 | lua_setfield(L, -2, "__index"); // u mt | ||
159 | } | ||
160 | lua_setmetatable( L, -2); // u | ||
161 | return 1; | 181 | return 1; |
162 | } | 182 | } |
163 | 183 | ||
@@ -175,6 +195,24 @@ static luaL_Reg const deep_module[] = | |||
175 | 195 | ||
176 | extern int __declspec(dllexport) luaopen_deep_test(lua_State* L) | 196 | extern int __declspec(dllexport) luaopen_deep_test(lua_State* L) |
177 | { | 197 | { |
178 | luaL_newlib( L, deep_module); | 198 | luaL_newlib( L, deep_module); // M |
199 | |||
200 | // preregister the metatables for the types we can instanciate so that Lanes can know about them | ||
201 | if( luaL_newmetatable( L, "clonable")) // M mt | ||
202 | { | ||
203 | luaL_setfuncs( L, clonable_mt, 0); | ||
204 | lua_pushvalue(L, -1); // M mt mt | ||
205 | lua_setfield(L, -2, "__index"); // M mt | ||
206 | } | ||
207 | lua_setfield(L, -2, "__clonableMT"); // M | ||
208 | |||
209 | if( luaL_newmetatable( L, "deep")) // mt | ||
210 | { | ||
211 | luaL_setfuncs( L, deep_mt, 0); | ||
212 | lua_pushvalue(L, -1); // mt mt | ||
213 | lua_setfield(L, -2, "__index"); // mt | ||
214 | } | ||
215 | lua_setfield(L, -2, "__deepMT"); // M | ||
216 | |||
179 | return 1; | 217 | return 1; |
180 | } | 218 | } |