diff options
| author | Benoit Germain <bnt.germain@gmail.com> | 2021-06-23 14:44:43 +0200 |
|---|---|---|
| committer | Benoit Germain <bnt.germain@gmail.com> | 2021-06-23 14:44:43 +0200 |
| commit | 54e788bc121218f8f1a978ee136e4d0faa67370d (patch) | |
| tree | abbb96bbe5d683aabc85ddfd803734550221db73 | |
| parent | d1ef9b97e356805c622e4832ec76289bae391a6e (diff) | |
| download | lanes-54e788bc121218f8f1a978ee136e4d0faa67370d.tar.gz lanes-54e788bc121218f8f1a978ee136e4d0faa67370d.tar.bz2 lanes-54e788bc121218f8f1a978ee136e4d0faa67370d.zip | |
updated deep userdata unit test to expose issue #189
| -rw-r--r-- | deep_test/deep_test.c | 25 | ||||
| -rw-r--r-- | deep_test/deeptest.lua | 35 |
2 files changed, 51 insertions, 9 deletions
diff --git a/deep_test/deep_test.c b/deep_test/deep_test.c index 61c81c5..4bee7fe 100644 --- a/deep_test/deep_test.c +++ b/deep_test/deep_test.c | |||
| @@ -35,6 +35,29 @@ static int deep_set( lua_State* L) | |||
| 35 | 35 | ||
| 36 | // ################################################################################################ | 36 | // ################################################################################################ |
| 37 | 37 | ||
| 38 | // won't actually do anything as deep userdata don't have uservalue slots | ||
| 39 | static int deep_setuv( lua_State* L) | ||
| 40 | { | ||
| 41 | struct s_MyDeepUserdata* self = luaG_todeep( L, deep_test_id, 1); | ||
| 42 | int uv = (int) luaL_optinteger( L, 2, 1); | ||
| 43 | lua_settop( L, 3); | ||
| 44 | lua_pushboolean( L, lua_setiuservalue( L, 1, uv) != 0); | ||
| 45 | return 1; | ||
| 46 | } | ||
| 47 | |||
| 48 | // ################################################################################################ | ||
| 49 | |||
| 50 | // won't actually do anything as deep userdata don't have uservalue slots | ||
| 51 | static int deep_getuv( lua_State* L) | ||
| 52 | { | ||
| 53 | struct s_MyDeepUserdata* self = luaG_todeep( L, deep_test_id, 1); | ||
| 54 | int uv = (int) luaL_optinteger( L, 2, 1); | ||
| 55 | lua_getiuservalue( L, 1, uv); | ||
| 56 | return 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | // ################################################################################################ | ||
| 60 | |||
| 38 | static int deep_tostring( lua_State* L) | 61 | static int deep_tostring( lua_State* L) |
| 39 | { | 62 | { |
| 40 | struct s_MyDeepUserdata* self = luaG_todeep( L, deep_test_id, 1); | 63 | struct s_MyDeepUserdata* self = luaG_todeep( L, deep_test_id, 1); |
| @@ -57,6 +80,8 @@ static luaL_Reg const deep_mt[] = | |||
| 57 | { "__tostring", deep_tostring}, | 80 | { "__tostring", deep_tostring}, |
| 58 | { "__gc", deep_gc}, | 81 | { "__gc", deep_gc}, |
| 59 | { "set", deep_set}, | 82 | { "set", deep_set}, |
| 83 | { "setuv", deep_setuv}, | ||
| 84 | { "getuv", deep_getuv}, | ||
| 60 | { NULL, NULL } | 85 | { NULL, NULL } |
| 61 | }; | 86 | }; |
| 62 | 87 | ||
diff --git a/deep_test/deeptest.lua b/deep_test/deeptest.lua index 16059d9..bc183a0 100644 --- a/deep_test/deeptest.lua +++ b/deep_test/deeptest.lua | |||
| @@ -5,25 +5,32 @@ local l = lanes.linda "my linda" | |||
| 5 | local dt = lanes.require "deep_test" | 5 | local dt = lanes.require "deep_test" |
| 6 | 6 | ||
| 7 | local test_deep = false | 7 | local test_deep = false |
| 8 | local test_clonable = true | 8 | local test_clonable = false |
| 9 | local test_clonable_as_upvalue = true | ||
| 9 | 10 | ||
| 10 | local performTest = function( obj_) | 11 | local performTest = function( obj_, uservalue_) |
| 12 | -- setup the userdata with some value and a uservalue | ||
| 11 | obj_:set( 666) | 13 | obj_:set( 666) |
| 12 | obj_:setuv( 1, "my uservalue") | 14 | obj_:setuv( 1, uservalue_) |
| 13 | print( "immediate:", obj_) | ||
| 14 | 15 | ||
| 16 | -- read back the contents of the object | ||
| 17 | print( "immediate:", obj_, obj_:getuv( 1)) | ||
| 18 | |||
| 19 | -- send the object in a linda, get it back out, read the contents | ||
| 15 | l:set( "key", obj_) | 20 | l:set( "key", obj_) |
| 16 | local out = l:get( "key") | 21 | local out = l:get( "key") |
| 17 | print( "out of linda:", out, out:getuv( 1)) | 22 | print( "out of linda:", out, out:getuv( 1)) |
| 18 | 23 | ||
| 24 | -- send the object in a lane through parameter passing, the lane body returns it as return value, read the contents | ||
| 19 | local g = lanes.gen( | 25 | local g = lanes.gen( |
| 20 | "package" | 26 | "package" |
| 21 | , { | 27 | , { |
| 22 | required = { "deep_test"} -- we will transfer userdata created by this module, so we need to make this lane aware of it | 28 | required = { "deep_test"} -- we will transfer userdata created by this module, so we need to make this lane aware of it |
| 23 | } | 29 | } |
| 24 | , function( obj_) | 30 | , function( param_) |
| 25 | print( "in lane:", obj_, obj_:getuv( 1)) | 31 | -- read contents inside lane |
| 26 | return obj_ | 32 | print( "in lane:", param_, param_:getuv( 1)) |
| 33 | return param_ | ||
| 27 | end | 34 | end |
| 28 | ) | 35 | ) |
| 29 | h = g( obj_) | 36 | h = g( obj_) |
| @@ -32,9 +39,19 @@ local performTest = function( obj_) | |||
| 32 | end | 39 | end |
| 33 | 40 | ||
| 34 | if test_deep then | 41 | if test_deep then |
| 35 | performTest( dt.new_deep()) | 42 | performTest( dt.new_deep(), "some uservalue") |
| 36 | end | 43 | end |
| 37 | 44 | ||
| 38 | if test_clonable then | 45 | if test_clonable then |
| 39 | performTest( dt.new_clonable()) | 46 | performTest( dt.new_clonable(), "my uservalue") |
| 40 | end | 47 | end |
| 48 | |||
| 49 | if test_clonable_as_upvalue then | ||
| 50 | local clonable = dt.new_clonable() | ||
| 51 | -- pull clonable as upvalue in a function | ||
| 52 | local f = function() | ||
| 53 | print( clonable) | ||
| 54 | end | ||
| 55 | -- set function as uservalue of clonable (thus, clonable is referenced as upvalue in its function uservalue) | ||
| 56 | performTest( clonable, f) | ||
| 57 | end \ No newline at end of file | ||
