diff options
Diffstat (limited to 'deep_test')
-rw-r--r-- | deep_test/deep_test.c | 63 | ||||
-rw-r--r-- | deep_test/deep_test.lua | 17 |
2 files changed, 74 insertions, 6 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 | |||
15 | struct s_MyDeepUserdata | 18 | struct s_MyDeepUserdata |
16 | { | 19 | { |
17 | lua_Integer val; | 20 | lua_Integer val; |
@@ -33,7 +36,7 @@ static int deep_set( lua_State* L) | |||
33 | static int deep_tostring( lua_State* L) | 36 | static 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 | |||
111 | struct s_MyClonableUserdata | ||
112 | { | ||
113 | lua_Integer val; | ||
114 | }; | ||
115 | |||
116 | // ################################################################################################ | ||
117 | |||
118 | static 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 | |||
127 | static 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 | |||
138 | static 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 | |||
149 | int 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 | ||
107 | static luaL_Reg const deep_module[] = | 167 | static 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 | ||
diff --git a/deep_test/deep_test.lua b/deep_test/deep_test.lua index fd78115..3b514dd 100644 --- a/deep_test/deep_test.lua +++ b/deep_test/deep_test.lua | |||
@@ -4,12 +4,19 @@ local deep = dt.new_deep() | |||
4 | deep:set(666) | 4 | deep:set(666) |
5 | print( deep) | 5 | print( deep) |
6 | 6 | ||
7 | local clonable = dt.new_clonable() | ||
8 | |||
7 | -- now load Lanes and see if that userdata is transferable | 9 | -- now load Lanes and see if that userdata is transferable |
8 | --[[ | 10 | --[[ |
9 | local lanes = require("lanes").configure() | 11 | local lanes = require("lanes").configure() |
10 | |||
11 | local l = lanes.linda "my linda" | 12 | local l = lanes.linda "my linda" |
12 | l.put( "key", deep) | 13 | |
13 | local out = l.get( "key") | 14 | l:set( "key", deep) |
14 | print( out) | 15 | local deep_out = l:get( "key") |
15 | ]] \ No newline at end of file | 16 | print( deep_out) |
17 | |||
18 | lanes.register() | ||
19 | l:set( "key", clonable) | ||
20 | local clonable_out = l:get( "key") | ||
21 | print( clonable_out) | ||
22 | --]] \ No newline at end of file | ||