aboutsummaryrefslogtreecommitdiff
path: root/deep_test
diff options
context:
space:
mode:
Diffstat (limited to 'deep_test')
-rw-r--r--deep_test/deep_test.c63
-rw-r--r--deep_test/deep_test.lua17
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
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
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()
4deep:set(666) 4deep:set(666)
5print( deep) 5print( deep)
6 6
7local 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--[[
9local lanes = require("lanes").configure() 11local lanes = require("lanes").configure()
10
11local l = lanes.linda "my linda" 12local l = lanes.linda "my linda"
12l.put( "key", deep) 13
13local out = l.get( "key") 14l:set( "key", deep)
14print( out) 15local deep_out = l:get( "key")
15]] \ No newline at end of file 16print( deep_out)
17
18lanes.register()
19l:set( "key", clonable)
20local clonable_out = l:get( "key")
21print( clonable_out)
22--]] \ No newline at end of file