aboutsummaryrefslogtreecommitdiff
path: root/deep_test/deep_test.c
diff options
context:
space:
mode:
authorBenoit Germain <b n t DOT g e r m a i n AT g m a i l DOT c o m>2018-10-29 13:30:41 +0100
committerBenoit Germain <b n t DOT g e r m a i n AT g m a i l DOT c o m>2018-10-29 13:30:41 +0100
commite2908369e92b14e661b16401b6389d6d4a026278 (patch)
tree2df10d504e3129d7a4006e9a83287853d41b4ccc /deep_test/deep_test.c
parent95ca27b5658c9b4da4ec67f6e922ff370eae05e5 (diff)
downloadlanes-e2908369e92b14e661b16401b6389d6d4a026278.tar.gz
lanes-e2908369e92b14e661b16401b6389d6d4a026278.tar.bz2
lanes-e2908369e92b14e661b16401b6389d6d4a026278.zip
add support for deep userdata cloning
Diffstat (limited to 'deep_test/deep_test.c')
-rw-r--r--deep_test/deep_test.c63
1 files changed, 62 insertions, 1 deletions
diff --git a/deep_test/deep_test.c b/deep_test/deep_test.c
index abe9fdc..8f34fe5 100644
--- a/deep_test/deep_test.c
+++ b/deep_test/deep_test.c
@@ -1,4 +1,5 @@
1#include <malloc.h> 1#include <malloc.h>
2#include <memory.h>
2 3
3#include "lua.h" 4#include "lua.h"
4#include "lualib.h" 5#include "lualib.h"
@@ -12,6 +13,8 @@
12#define LANES_API 13#define LANES_API
13#endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) 14#endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
14 15
16// ################################################################################################
17
15struct s_MyDeepUserdata 18struct s_MyDeepUserdata
16{ 19{
17 lua_Integer val; 20 lua_Integer val;
@@ -33,7 +36,7 @@ static int deep_set( lua_State* L)
33static int deep_tostring( lua_State* L) 36static int deep_tostring( lua_State* L)
34{ 37{
35 struct s_MyDeepUserdata* self = luaG_todeep( L, deep_test_id, 1); 38 struct s_MyDeepUserdata* self = luaG_todeep( L, deep_test_id, 1);
36 lua_pushfstring( L, "deep_test(%d)", self->val); 39 lua_pushfstring( L, "deep(%d)", self->val);
37 return 1; 40 return 1;
38} 41}
39 42
@@ -103,10 +106,68 @@ int luaD_new_deep( lua_State* L)
103} 106}
104 107
105// ################################################################################################ 108// ################################################################################################
109// ################################################################################################
110
111struct s_MyClonableUserdata
112{
113 lua_Integer val;
114};
115
116// ################################################################################################
117
118static int clonable_tostring(lua_State* L)
119{
120 struct s_MyClonableUserdata* self = (struct s_MyClonableUserdata*) lua_touserdata( L, 1);
121 lua_pushfstring( L, "clonable(%d)", self->val);
122 return 1;
123}
124
125// ################################################################################################
126
127static int clonable_lanesclone( lua_State* L)
128{
129 // no need to set the metatable, the Lane copying mechanism will take care of it
130 struct s_MyClonableUserdata* self = lua_touserdata( L, 1);
131 struct s_MyClonableUserdata* to = lua_newuserdata( L, sizeof( struct s_MyClonableUserdata));
132 memcpy( to, self, sizeof(struct s_MyClonableUserdata));
133 return 1;
134}
135
136// ################################################################################################
137
138static luaL_Reg const clonable_mt[] =
139{
140 { "__tostring", clonable_tostring},
141 //{ "__gc", deep_gc},
142 { "__lanesclone", clonable_lanesclone},
143 //{ "set", deep_set},
144 { NULL, NULL }
145};
146
147// ################################################################################################
148
149int luaD_new_clonable( lua_State* L)
150{
151 lua_newuserdata( L, sizeof( struct s_MyClonableUserdata));
152 if( luaL_getmetatable( L, "clonable") == LUA_TNIL) // u mt?
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;
162}
163
164// ################################################################################################
165// ################################################################################################
106 166
107static luaL_Reg const deep_module[] = 167static luaL_Reg const deep_module[] =
108{ 168{
109 { "new_deep", luaD_new_deep}, 169 { "new_deep", luaD_new_deep},
170 { "new_clonable", luaD_new_clonable},
110 { NULL, NULL} 171 { NULL, NULL}
111}; 172};
112 173