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> | 2017-08-02 14:39:44 +0200 |
|---|---|---|
| committer | Benoit Germain <b n t DOT g e r m a i n AT g m a i l DOT c o m> | 2017-08-02 14:39:44 +0200 |
| commit | c5d772fd0dd9120885be96255d5dd4a3ed6b8aac (patch) | |
| tree | 4f92866eddac0788ad3c060af041c6945ffe7797 /deep_test | |
| parent | 00fa87f8460f32259737d0285d7855e68b55ab31 (diff) | |
| download | lanes-c5d772fd0dd9120885be96255d5dd4a3ed6b8aac.tar.gz lanes-c5d772fd0dd9120885be96255d5dd4a3ed6b8aac.tar.bz2 lanes-c5d772fd0dd9120885be96255d5dd4a3ed6b8aac.zip | |
Deep userdata improvements
Support for user-provided __gc
Improved example
Diffstat (limited to 'deep_test')
| -rw-r--r-- | deep_test/deep_test.c | 54 | ||||
| -rw-r--r-- | deep_test/deep_test.lua | 6 |
2 files changed, 45 insertions, 15 deletions
diff --git a/deep_test/deep_test.c b/deep_test/deep_test.c index a48ecb2..abe9fdc 100644 --- a/deep_test/deep_test.c +++ b/deep_test/deep_test.c | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | #include <malloc.h> | ||
| 2 | |||
| 1 | #include "lua.h" | 3 | #include "lua.h" |
| 2 | #include "lualib.h" | 4 | #include "lualib.h" |
| 3 | #include "lauxlib.h" | 5 | #include "lauxlib.h" |
| @@ -10,19 +12,46 @@ | |||
| 10 | #define LANES_API | 12 | #define LANES_API |
| 11 | #endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) | 13 | #endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) |
| 12 | 14 | ||
| 15 | struct s_MyDeepUserdata | ||
| 16 | { | ||
| 17 | lua_Integer val; | ||
| 18 | }; | ||
| 19 | static void* deep_test_id( lua_State* L, enum eDeepOp op_); | ||
| 20 | |||
| 21 | // ################################################################################################ | ||
| 22 | |||
| 23 | static int deep_set( lua_State* L) | ||
| 24 | { | ||
| 25 | struct s_MyDeepUserdata* self = luaG_todeep( L, deep_test_id, 1); | ||
| 26 | lua_Integer i = lua_tointeger( L, 2); | ||
| 27 | self->val = i; | ||
| 28 | return 0; | ||
| 29 | } | ||
| 30 | |||
| 13 | // ################################################################################################ | 31 | // ################################################################################################ |
| 14 | 32 | ||
| 15 | static int deep_tostring(lua_State* L) | 33 | static int deep_tostring( lua_State* L) |
| 16 | { | 34 | { |
| 17 | lua_pushliteral( L, "I am a deep_test"); | 35 | struct s_MyDeepUserdata* self = luaG_todeep( L, deep_test_id, 1); |
| 36 | lua_pushfstring( L, "deep_test(%d)", self->val); | ||
| 18 | return 1; | 37 | return 1; |
| 19 | } | 38 | } |
| 20 | 39 | ||
| 21 | // ################################################################################################ | 40 | // ################################################################################################ |
| 22 | 41 | ||
| 42 | static int deep_gc( lua_State* L) | ||
| 43 | { | ||
| 44 | struct s_MyDeepUserdata* self = luaG_todeep( L, deep_test_id, 1); | ||
| 45 | return 0; | ||
| 46 | } | ||
| 47 | |||
| 48 | // ################################################################################################ | ||
| 49 | |||
| 23 | static luaL_Reg const deep_mt[] = | 50 | static luaL_Reg const deep_mt[] = |
| 24 | { | 51 | { |
| 25 | { "__tostring", deep_tostring}, | 52 | { "__tostring", deep_tostring}, |
| 53 | { "__gc", deep_gc}, | ||
| 54 | { "set", deep_set}, | ||
| 26 | { NULL, NULL } | 55 | { NULL, NULL } |
| 27 | }; | 56 | }; |
| 28 | 57 | ||
| @@ -34,26 +63,25 @@ static void* deep_test_id( lua_State* L, enum eDeepOp op_) | |||
| 34 | { | 63 | { |
| 35 | case eDO_new: | 64 | case eDO_new: |
| 36 | { | 65 | { |
| 37 | void* allocUD; | 66 | struct s_MyDeepUserdata* deep_test = (struct s_MyDeepUserdata*) malloc( sizeof(struct s_MyDeepUserdata)); |
| 38 | lua_Alloc allocF = lua_getallocf( L, &allocUD); | 67 | deep_test->val = 0; |
| 39 | void* deep_test = allocF( allocUD, NULL, 0, sizeof(void*)); | ||
| 40 | return deep_test; | 68 | return deep_test; |
| 41 | } | 69 | } |
| 42 | 70 | ||
| 43 | case eDO_delete: | 71 | case eDO_delete: |
| 44 | { | 72 | { |
| 45 | void* allocUD; | 73 | struct s_MyDeepUserdata* deep_test = (struct s_MyDeepUserdata*) lua_touserdata( L, 1); |
| 46 | lua_Alloc allocF = lua_getallocf( L, &allocUD); | 74 | free( deep_test); |
| 47 | void* deep_test = lua_touserdata( L, 1); | ||
| 48 | allocF( allocUD, deep_test, sizeof(void*), 0); | ||
| 49 | return NULL; | 75 | return NULL; |
| 50 | } | 76 | } |
| 51 | 77 | ||
| 52 | case eDO_metatable: | 78 | case eDO_metatable: |
| 53 | { | 79 | { |
| 54 | lua_newtable( L); | 80 | lua_newtable( L); // mt |
| 55 | luaL_setfuncs( L, deep_mt, 0); | 81 | luaL_setfuncs( L, deep_mt, 0); // mt |
| 56 | luaG_pushdeepversion( L); | 82 | lua_pushvalue( L, -1); // mt mt |
| 83 | lua_setfield( L, -2, "__index"); // mt | ||
| 84 | luaG_pushdeepversion( L); // mt version | ||
| 57 | return NULL; | 85 | return NULL; |
| 58 | } | 86 | } |
| 59 | 87 | ||
| @@ -69,7 +97,7 @@ static void* deep_test_id( lua_State* L, enum eDeepOp op_) | |||
| 69 | 97 | ||
| 70 | // ################################################################################################ | 98 | // ################################################################################################ |
| 71 | 99 | ||
| 72 | int luaD_new_deep(lua_State* L) | 100 | int luaD_new_deep( lua_State* L) |
| 73 | { | 101 | { |
| 74 | return luaG_newdeepuserdata( L, deep_test_id); | 102 | return luaG_newdeepuserdata( L, deep_test_id); |
| 75 | } | 103 | } |
diff --git a/deep_test/deep_test.lua b/deep_test/deep_test.lua index 034c07d..fd78115 100644 --- a/deep_test/deep_test.lua +++ b/deep_test/deep_test.lua | |||
| @@ -1,13 +1,15 @@ | |||
| 1 | -- create a deep-aware full userdata while Lanes isn't loaded | 1 | -- create a deep-aware full userdata while Lanes isn't loaded |
| 2 | local dt = require "deep_test" | 2 | local dt = require "deep_test" |
| 3 | local deep = dt.new_deep() | 3 | local deep = dt.new_deep() |
| 4 | deep:set(666) | ||
| 4 | print( deep) | 5 | print( deep) |
| 5 | 6 | ||
| 6 | -- now load Lanes and see if that userdata is transferable | 7 | -- now load Lanes and see if that userdata is transferable |
| 7 | 8 | --[[ | |
| 8 | local lanes = require("lanes").configure() | 9 | local lanes = require("lanes").configure() |
| 9 | 10 | ||
| 10 | local l = lanes.linda "my linda" | 11 | local l = lanes.linda "my linda" |
| 11 | l.put( "key", deep) | 12 | l.put( "key", deep) |
| 12 | local out = l.get( "key") | 13 | local out = l.get( "key") |
| 13 | print( out) \ No newline at end of file | 14 | print( out) |
| 15 | ]] \ No newline at end of file | ||
