@@ -762,10 +762,13 @@ static Helper *helper_create( lua_State *L, Handle *handle, const char *funcname
762
762
Helper * h = ( Helper * )lua_newuserdata ( L , sizeof ( Helper ) );
763
763
luaL_getmetatable ( L , "rpc.helper" );
764
764
lua_setmetatable ( L , -2 );
765
+
766
+ lua_pushvalue ( L , 1 ); // push parent handle
767
+ h -> pref = luaL_ref ( L , LUA_REGISTRYINDEX ); // put ref into struct
765
768
h -> handle = handle ;
766
769
h -> parent = NULL ;
767
770
h -> nparents = 0 ;
768
- strncpy ( h -> funcname , funcname , NUM_FUNCNAME_CHARS );
771
+ strncpy ( h -> funcname , funcname , NUM_FUNCNAME_CHARS );
769
772
return h ;
770
773
}
771
774
@@ -775,7 +778,7 @@ static int handle_index (lua_State *L)
775
778
{
776
779
const char * s ;
777
780
778
- MYASSERT ( lua_gettop ( L ) == 2 );
781
+ check_num_args ( L , 2 );
779
782
MYASSERT ( lua_isuserdata ( L , 1 ) && ismetatable_type ( L , 1 , "rpc.handle" ) );
780
783
781
784
if ( lua_type ( L , 2 ) != LUA_TSTRING )
@@ -797,7 +800,7 @@ static int handle_newindex( lua_State *L )
797
800
{
798
801
const char * s ;
799
802
800
- MYASSERT ( lua_gettop ( L ) == 3 );
803
+ check_num_args ( L , 3 );
801
804
MYASSERT ( lua_isuserdata ( L , 1 ) && ismetatable_type ( L , 1 , "rpc.handle" ) );
802
805
803
806
if ( lua_type ( L , 2 ) != LUA_TSTRING )
@@ -928,11 +931,10 @@ static int helper_call (lua_State *L)
928
931
int freturn = 0 ;
929
932
Helper * h ;
930
933
Transport * tpt ;
931
- MYASSERT ( lua_gettop ( L ) >= 1 );
932
- MYASSERT ( lua_isuserdata ( L , 1 ) && ismetatable_type ( L , 1 , "rpc.helper" ) );
933
-
934
- // get helper object and its transport
935
- h = ( Helper * )lua_touserdata ( L , 1 );
934
+
935
+ h = ( Helper * )luaL_checkudata (L , 1 , "rpc.helper" );
936
+ luaL_argcheck (L , h , 1 , "helper expected" );
937
+
936
938
tpt = & h -> handle -> tpt ;
937
939
938
940
// capture special calls, otherwise execute normal remote call
@@ -1001,26 +1003,25 @@ static int helper_call (lua_State *L)
1001
1003
return freturn ;
1002
1004
}
1003
1005
1004
-
1005
-
1006
-
1006
+ // __newindex even on helper
1007
1007
static int helper_newindex ( lua_State * L )
1008
1008
{
1009
1009
struct exception e ;
1010
1010
int freturn = 0 ;
1011
1011
int ret_code ;
1012
1012
Helper * h ;
1013
1013
Transport * tpt ;
1014
- MYASSERT ( lua_isuserdata ( L , -3 ) && ismetatable_type ( L , -3 , "rpc.helper" ) );
1015
- MYASSERT ( lua_isstring ( L , -2 ) );
1016
-
1017
- // get helper object and its transport
1018
- h = ( Helper * )lua_touserdata ( L , -3 );
1014
+
1015
+ h = ( Helper * )luaL_checkudata (L , -3 , "rpc.helper" );
1016
+ luaL_argcheck (L , h , -3 , "helper expected" );
1017
+
1018
+ luaL_checktype (L , -2 , LUA_TSTRING );
1019
+
1019
1020
tpt = & h -> handle -> tpt ;
1020
1021
1021
1022
Try
1022
1023
{
1023
- // write function name
1024
+ // index destination on remote side
1024
1025
helper_wait_ready ( tpt , RPC_CMD_NEWINDEX );
1025
1026
helper_remote_index ( h );
1026
1027
@@ -1055,19 +1056,22 @@ static Helper *helper_append( lua_State *L, Helper *helper, const char *funcname
1055
1056
Helper * h = ( Helper * )lua_newuserdata ( L , sizeof ( Helper ) );
1056
1057
luaL_getmetatable ( L , "rpc.helper" );
1057
1058
lua_setmetatable ( L , -2 );
1059
+
1060
+ lua_pushvalue ( L , 1 ); // push parent
1061
+ h -> pref = luaL_ref ( L , LUA_REGISTRYINDEX ); // put ref into struct
1058
1062
h -> handle = helper -> handle ;
1059
1063
h -> parent = helper ;
1060
1064
h -> nparents = helper -> nparents + 1 ;
1061
1065
strncpy ( h -> funcname , funcname , NUM_FUNCNAME_CHARS );
1062
1066
return h ;
1063
1067
}
1064
1068
1065
- // indexing a handle returns a helper
1069
+ // indexing a helper returns a helper
1066
1070
static int helper_index ( lua_State * L )
1067
1071
{
1068
1072
const char * s ;
1069
1073
1070
- MYASSERT ( lua_gettop ( L ) == 2 );
1074
+ check_num_args ( L , 2 );
1071
1075
MYASSERT ( lua_isuserdata ( L , 1 ) && ismetatable_type ( L , 1 , "rpc.helper" ) );
1072
1076
1073
1077
if ( lua_type ( L , 2 ) != LUA_TSTRING )
@@ -1081,6 +1085,17 @@ static int helper_index( lua_State *L )
1081
1085
return 1 ;
1082
1086
}
1083
1087
1088
+ static int helper_close (lua_State * L )
1089
+ {
1090
+ Helper * h = ( Helper * )luaL_checkudata (L , 1 , "rpc.helper" );
1091
+ luaL_argcheck (L , h , 1 , "helper expected" );
1092
+
1093
+ luaL_unref (L , LUA_REGISTRYINDEX , h -> pref );
1094
+ h -> pref = LUA_REFNIL ;
1095
+ return 0 ;
1096
+ }
1097
+
1098
+
1084
1099
// **************************************************************************
1085
1100
// server side handle userdata objects.
1086
1101
@@ -1543,8 +1558,8 @@ static int rpc_dispatch( lua_State *L )
1543
1558
ServerHandle * handle ;
1544
1559
check_num_args ( L , 1 );
1545
1560
1546
- if ( ! ( lua_isuserdata ( L , 1 ) && ismetatable_type ( L , 1 , "rpc.server_handle" ) ) )
1547
- return luaL_error ( L , "arg must be server handle" );
1561
+ handle = ( ServerHandle * ) luaL_checkudata ( L , 1 , "rpc.server_handle" );
1562
+ luaL_argcheck ( L , handle , 1 , " server handle expected" );
1548
1563
1549
1564
handle = ( ServerHandle * )lua_touserdata ( L , 1 );
1550
1565
@@ -1625,6 +1640,7 @@ const LUA_REG_TYPE rpc_helper[] =
1625
1640
{ LSTRKEY ( "__call" ), LFUNCVAL ( helper_call ) },
1626
1641
{ LSTRKEY ( "__index" ), LFUNCVAL ( helper_index ) },
1627
1642
{ LSTRKEY ( "__newindex" ), LFUNCVAL ( helper_newindex ) },
1643
+ { LSTRKEY ( "__gc" ), LFUNCVAL ( helper_close ) },
1628
1644
{ LNILKEY , LNILVAL }
1629
1645
};
1630
1646
@@ -1686,6 +1702,7 @@ static const luaL_reg rpc_helper[] =
1686
1702
{ "__call" , helper_call },
1687
1703
{ "__index" , helper_index },
1688
1704
{ "__newindex" , helper_newindex },
1705
+ { "__gc" , helper_close },
1689
1706
{ NULL , NULL }
1690
1707
};
1691
1708
0 commit comments