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-01 09:40: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-01 09:40:44 +0200 |
commit | e0dbd33c2d4776d6b2213dd82f344166eafde438 (patch) | |
tree | bab4483f9f5b0d802bdbfc8641fe44ff9649382f /deep_test | |
parent | fcd08030b6b6af81a8aa2672082a55f602006f78 (diff) | |
download | lanes-e0dbd33c2d4776d6b2213dd82f344166eafde438.tar.gz lanes-e0dbd33c2d4776d6b2213dd82f344166eafde438.tar.bz2 lanes-e0dbd33c2d4776d6b2213dd82f344166eafde438.zip |
Fix for deep-aware modules
Don't crash when using a module that creates Lanes-compatible deep
userdata.
Added a sample deep-aware module.
Diffstat (limited to 'deep_test')
-rw-r--r-- | deep_test/deep_test.c | 91 | ||||
-rw-r--r-- | deep_test/deep_test.lua | 13 |
2 files changed, 104 insertions, 0 deletions
diff --git a/deep_test/deep_test.c b/deep_test/deep_test.c new file mode 100644 index 0000000..a48ecb2 --- /dev/null +++ b/deep_test/deep_test.c | |||
@@ -0,0 +1,91 @@ | |||
1 | #include "lua.h" | ||
2 | #include "lualib.h" | ||
3 | #include "lauxlib.h" | ||
4 | |||
5 | #include "deep.h" | ||
6 | |||
7 | #if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) | ||
8 | #define LANES_API __declspec(dllexport) | ||
9 | #else | ||
10 | #define LANES_API | ||
11 | #endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) | ||
12 | |||
13 | // ################################################################################################ | ||
14 | |||
15 | static int deep_tostring(lua_State* L) | ||
16 | { | ||
17 | lua_pushliteral( L, "I am a deep_test"); | ||
18 | return 1; | ||
19 | } | ||
20 | |||
21 | // ################################################################################################ | ||
22 | |||
23 | static luaL_Reg const deep_mt[] = | ||
24 | { | ||
25 | { "__tostring", deep_tostring}, | ||
26 | { NULL, NULL } | ||
27 | }; | ||
28 | |||
29 | // ################################################################################################ | ||
30 | |||
31 | static void* deep_test_id( lua_State* L, enum eDeepOp op_) | ||
32 | { | ||
33 | switch( op_) | ||
34 | { | ||
35 | case eDO_new: | ||
36 | { | ||
37 | void* allocUD; | ||
38 | lua_Alloc allocF = lua_getallocf( L, &allocUD); | ||
39 | void* deep_test = allocF( allocUD, NULL, 0, sizeof(void*)); | ||
40 | return deep_test; | ||
41 | } | ||
42 | |||
43 | case eDO_delete: | ||
44 | { | ||
45 | void* allocUD; | ||
46 | lua_Alloc allocF = lua_getallocf( L, &allocUD); | ||
47 | void* deep_test = lua_touserdata( L, 1); | ||
48 | allocF( allocUD, deep_test, sizeof(void*), 0); | ||
49 | return NULL; | ||
50 | } | ||
51 | |||
52 | case eDO_metatable: | ||
53 | { | ||
54 | lua_newtable( L); | ||
55 | luaL_setfuncs( L, deep_mt, 0); | ||
56 | luaG_pushdeepversion( L); | ||
57 | return NULL; | ||
58 | } | ||
59 | |||
60 | case eDO_module: | ||
61 | return "deep_test"; | ||
62 | |||
63 | default: | ||
64 | { | ||
65 | return NULL; | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | |||
70 | // ################################################################################################ | ||
71 | |||
72 | int luaD_new_deep(lua_State* L) | ||
73 | { | ||
74 | return luaG_newdeepuserdata( L, deep_test_id); | ||
75 | } | ||
76 | |||
77 | // ################################################################################################ | ||
78 | |||
79 | static luaL_Reg const deep_module[] = | ||
80 | { | ||
81 | { "new_deep", luaD_new_deep}, | ||
82 | { NULL, NULL} | ||
83 | }; | ||
84 | |||
85 | // ################################################################################################ | ||
86 | |||
87 | extern int __declspec(dllexport) luaopen_deep_test(lua_State* L) | ||
88 | { | ||
89 | luaL_newlib( L, deep_module); | ||
90 | return 1; | ||
91 | } | ||
diff --git a/deep_test/deep_test.lua b/deep_test/deep_test.lua new file mode 100644 index 0000000..034c07d --- /dev/null +++ b/deep_test/deep_test.lua | |||
@@ -0,0 +1,13 @@ | |||
1 | -- create a deep-aware full userdata while Lanes isn't loaded | ||
2 | local dt = require "deep_test" | ||
3 | local deep = dt.new_deep() | ||
4 | print( deep) | ||
5 | |||
6 | -- now load Lanes and see if that userdata is transferable | ||
7 | |||
8 | local lanes = require("lanes").configure() | ||
9 | |||
10 | local l = lanes.linda "my linda" | ||
11 | l.put( "key", deep) | ||
12 | local out = l.get( "key") | ||
13 | print( out) \ No newline at end of file | ||