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 | |
| 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')
| -rw-r--r-- | deep_test/deep_test.c | 80 | ||||
| -rw-r--r-- | deep_test/deeptest.lua | 53 |
2 files changed, 94 insertions, 39 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 | } |
diff --git a/deep_test/deeptest.lua b/deep_test/deeptest.lua index 3b514dd..c0bbab4 100644 --- a/deep_test/deeptest.lua +++ b/deep_test/deeptest.lua | |||
| @@ -1,22 +1,39 @@ | |||
| 1 | -- create a deep-aware full userdata while Lanes isn't loaded | 1 | local lanes = require("lanes").configure{ with_timers = false} |
| 2 | local dt = require "deep_test" | 2 | local l = lanes.linda "my linda" |
| 3 | local deep = dt.new_deep() | ||
| 4 | deep:set(666) | ||
| 5 | print( deep) | ||
| 6 | 3 | ||
| 7 | local clonable = dt.new_clonable() | 4 | -- we will transfer userdata created by this module, so we need to make Lanes aware of it |
| 5 | local dt = lanes.require "deep_test" | ||
| 8 | 6 | ||
| 9 | -- now load Lanes and see if that userdata is transferable | 7 | local test_deep = true |
| 10 | --[[ | 8 | local test_clonable = false |
| 11 | local lanes = require("lanes").configure() | 9 | |
| 12 | local l = lanes.linda "my linda" | 10 | local performTest = function( obj_) |
| 11 | obj_:set(666) | ||
| 12 | print( "immediate:", obj_) | ||
| 13 | |||
| 14 | l:set( "key", obj_) | ||
| 15 | local out = l:get( "key") | ||
| 16 | print( "out of linda:", out) | ||
| 17 | |||
| 18 | local g = lanes.gen( | ||
| 19 | "package" | ||
| 20 | , { | ||
| 21 | required = { "deep_test"} -- we will transfer userdata created by this module, so we need to make this lane aware of it | ||
| 22 | } | ||
| 23 | , function( obj_) | ||
| 24 | print( "in lane:", obj_) | ||
| 25 | return obj_ | ||
| 26 | end | ||
| 27 | ) | ||
| 28 | h = g( obj_) | ||
| 29 | local from_lane = h[1] | ||
| 30 | print( "from lane:", from_lane) | ||
| 31 | end | ||
| 13 | 32 | ||
| 14 | l:set( "key", deep) | 33 | if test_deep then |
| 15 | local deep_out = l:get( "key") | 34 | performTest( dt.new_deep()) |
| 16 | print( deep_out) | 35 | end |
| 17 | 36 | ||
| 18 | lanes.register() | 37 | if test_clonable then |
| 19 | l:set( "key", clonable) | 38 | performTest( dt.new_clonable()) |
| 20 | local clonable_out = l:get( "key") | 39 | end |
| 21 | print( clonable_out) | ||
| 22 | --]] \ No newline at end of file | ||
