aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Germain <bnt.germain@gmail.com>2018-11-23 17:42:49 +0100
committerBenoit Germain <bnt.germain@gmail.com>2018-11-23 17:42:49 +0100
commit60e5d94af372471c2a3bab91963588ca650bff7b (patch)
tree2543e07bd34b7fe48ce953039b4b76c6dcc6e1dc /src
parentcb0f60d743fb4a5420d1e6cca0404507c4e50642 (diff)
downloadlanes-60e5d94af372471c2a3bab91963588ca650bff7b.tar.gz
lanes-60e5d94af372471c2a3bab91963588ca650bff7b.tar.bz2
lanes-60e5d94af372471c2a3bab91963588ca650bff7b.zip
finish registry access revamp
Diffstat (limited to 'src')
-rw-r--r--src/lanes.c52
-rw-r--r--src/tools.c12
2 files changed, 28 insertions, 36 deletions
diff --git a/src/lanes.c b/src/lanes.c
index f32cf6e..c8e012c 100644
--- a/src/lanes.c
+++ b/src/lanes.c
@@ -154,8 +154,8 @@ static void cancel_hook( lua_State* L, lua_Debug* ar)
154 154
155#if ERROR_FULL_STACK 155#if ERROR_FULL_STACK
156static int lane_error( lua_State* L); 156static int lane_error( lua_State* L);
157// crc64/we of string "STACK_TRACE_KEY" generated at http://www.nitrxgen.net/hashgen/ 157// crc64/we of string "STACKTRACE_REGKEY" generated at http://www.nitrxgen.net/hashgen/
158static DECLARE_CONST_UNIQUE_KEY( STACK_TRACE_KEY, 0x024d5411677ce879); 158static DECLARE_CONST_UNIQUE_KEY( STACKTRACE_REGKEY, 0x534af7d3226a429f);
159#endif // ERROR_FULL_STACK 159#endif // ERROR_FULL_STACK
160 160
161/* 161/*
@@ -166,8 +166,8 @@ static DECLARE_CONST_UNIQUE_KEY( STACK_TRACE_KEY, 0x024d5411677ce879);
166* error (and maybe stack trace) parameters to the finalizer functions would 166* error (and maybe stack trace) parameters to the finalizer functions would
167* anyways complicate that approach. 167* anyways complicate that approach.
168*/ 168*/
169// crc64/we of string "STACK_TRACE_KEY" generated at http://www.nitrxgen.net/hashgen/ 169// crc64/we of string "FINALIZER_REGKEY" generated at http://www.nitrxgen.net/hashgen/
170static DECLARE_CONST_UNIQUE_KEY( FINALIZER_REG_KEY, 0x7902972781c7e365); 170static DECLARE_CONST_UNIQUE_KEY( FINALIZER_REGKEY, 0x188fccb8bf348e09);
171 171
172struct s_Linda; 172struct s_Linda;
173 173
@@ -209,9 +209,7 @@ static bool_t push_registry_table( lua_State* L, UniqueKey key, bool_t create)
209 } 209 }
210 210
211 lua_newtable( L); // t 211 lua_newtable( L); // t
212 push_unique_key( L, key); // t key 212 REGISTRY_SET( L, key, lua_pushvalue( L, -2));
213 lua_pushvalue( L, -2); // t key t
214 lua_rawset( L, LUA_REGISTRYINDEX); // t
215 } 213 }
216 STACK_END( L, 1); 214 STACK_END( L, 1);
217 return TRUE; // table pushed 215 return TRUE; // table pushed
@@ -318,7 +316,7 @@ LUAG_FUNC( set_finalizer)
318 luaL_argcheck( L, lua_isfunction( L, 1), 1, "finalizer should be a function"); 316 luaL_argcheck( L, lua_isfunction( L, 1), 1, "finalizer should be a function");
319 luaL_argcheck( L, lua_gettop( L) == 1, 1, "too many arguments"); 317 luaL_argcheck( L, lua_gettop( L) == 1, 1, "too many arguments");
320 // Get the current finalizer table (if any) 318 // Get the current finalizer table (if any)
321 push_registry_table( L, FINALIZER_REG_KEY, TRUE /*do create if none*/); // finalizer {finalisers} 319 push_registry_table( L, FINALIZER_REGKEY, TRUE /*do create if none*/); // finalizer {finalisers}
322 STACK_GROW( L, 2); 320 STACK_GROW( L, 2);
323 lua_pushinteger( L, lua_rawlen( L, -1) + 1); // finalizer {finalisers} idx 321 lua_pushinteger( L, lua_rawlen( L, -1) + 1); // finalizer {finalisers} idx
324 lua_pushvalue( L, 1); // finalizer {finalisers} idx finalizer 322 lua_pushvalue( L, 1); // finalizer {finalisers} idx finalizer
@@ -349,7 +347,7 @@ static int run_finalizers( lua_State* L, int lua_rc)
349 int n; 347 int n;
350 int err_handler_index = 0; 348 int err_handler_index = 0;
351 int rc = LUA_OK; // ... 349 int rc = LUA_OK; // ...
352 if( !push_registry_table( L, FINALIZER_REG_KEY, FALSE)) // ... finalizers? 350 if( !push_registry_table( L, FINALIZER_REGKEY, FALSE)) // ... finalizers?
353 { 351 {
354 return 0; // no finalizers 352 return 0; // no finalizers
355 } 353 }
@@ -382,7 +380,7 @@ static int run_finalizers( lua_State* L, int lua_rc)
382 args = finalizers_index - 1; 380 args = finalizers_index - 1;
383 } 381 }
384 382
385 // if no error from the main body, finlizer doesn't receive any argument, else it gets the error message and optional stack trace 383 // if no error from the main body, finalizer doesn't receive any argument, else it gets the error message and optional stack trace
386 rc = lua_pcall( L, args, 0, err_handler_index); // ... finalizers lane_error err_msg2? 384 rc = lua_pcall( L, args, 0, err_handler_index); // ... finalizers lane_error err_msg2?
387 if( rc != LUA_OK) 385 if( rc != LUA_OK)
388 { 386 {
@@ -832,8 +830,8 @@ LUAG_FUNC( set_singlethreaded)
832*/ 830*/
833#if ERROR_FULL_STACK 831#if ERROR_FULL_STACK
834 832
835// crc64/we of string "EXTENDED_STACK_TRACE_REGKEY" generated at http://www.nitrxgen.net/hashgen/ 833// crc64/we of string "EXTENDED_STACKTRACE_REGKEY" generated at http://www.nitrxgen.net/hashgen/
836static DECLARE_CONST_UNIQUE_KEY( EXTENDED_STACK_TRACE_REGKEY, 0x7a59821071066e49); // used as registry key 834static DECLARE_CONST_UNIQUE_KEY( EXTENDED_STACKTRACE_REGKEY, 0x2357c69a7c92c936); // used as registry key
837 835
838LUAG_FUNC( set_error_reporting) 836LUAG_FUNC( set_error_reporting)
839{ 837{
@@ -854,9 +852,7 @@ LUAG_FUNC( set_error_reporting)
854 return luaL_error( L, "unsupported error reporting model"); 852 return luaL_error( L, "unsupported error reporting model");
855 } 853 }
856done: 854done:
857 push_unique_key( L, EXTENDED_STACK_TRACE_REGKEY); 855 REGISTRY_SET( L, EXTENDED_STACKTRACE_REGKEY, lua_pushboolean( L, equal));
858 lua_pushboolean( L, equal);
859 lua_rawset( L, LUA_REGISTRYINDEX);
860 return 0; 856 return 0;
861} 857}
862 858
@@ -867,7 +863,7 @@ static int lane_error( lua_State* L)
867 bool_t extended; 863 bool_t extended;
868 864
869 // error message (any type) 865 // error message (any type)
870 assert( lua_gettop( L) == 1); // some_error 866 STACK_CHECK_ABS( L, 1); // some_error
871 867
872 // Don't do stack survey for cancelled lanes. 868 // Don't do stack survey for cancelled lanes.
873 // 869 //
@@ -877,8 +873,7 @@ static int lane_error( lua_State* L)
877 } 873 }
878 874
879 STACK_GROW( L, 3); 875 STACK_GROW( L, 3);
880 push_unique_key( L, EXTENDED_STACK_TRACE_REGKEY); // some_error estk 876 REGISTRY_GET( L, EXTENDED_STACKTRACE_REGKEY); // some_error basic|extended
881 lua_rawget( L, LUA_REGISTRYINDEX); // some_error basic|extended
882 extended = lua_toboolean( L, -1); 877 extended = lua_toboolean( L, -1);
883 lua_pop( L, 1); // some_error 878 lua_pop( L, 1); // some_error
884 879
@@ -931,12 +926,9 @@ static int lane_error( lua_State* L)
931 lua_rawseti( L, -2, (lua_Integer) n); // some_error {} 926 lua_rawseti( L, -2, (lua_Integer) n); // some_error {}
932 } 927 }
933 928
934 push_unique_key( L, STACK_TRACE_KEY); // some_error {} stk 929 REGISTRY_SET( L, STACKTRACE_REGKEY, lua_insert( L, -2)); // some_error
935 lua_insert( L, -2); // some_error stk {}
936 lua_rawset( L, LUA_REGISTRYINDEX); // some_error
937
938 assert( lua_gettop( L) == 1);
939 930
931 STACK_END( L, 1);
940 return 1; // the untouched error value 932 return 1; // the untouched error value
941} 933}
942#endif // ERROR_FULL_STACK 934#endif // ERROR_FULL_STACK
@@ -952,12 +944,12 @@ static void push_stack_trace( lua_State* L, int rc_, int stk_base_)
952 case LUA_ERRRUN: // cancellation or a runtime error 944 case LUA_ERRRUN: // cancellation or a runtime error
953#if ERROR_FULL_STACK // when ERROR_FULL_STACK, we installed a handler 945#if ERROR_FULL_STACK // when ERROR_FULL_STACK, we installed a handler
954 { 946 {
947 STACK_CHECK( L, 0);
955 // fetch the call stack table from the registry where the handler stored it 948 // fetch the call stack table from the registry where the handler stored it
956 STACK_GROW( L, 1); 949 STACK_GROW( L, 1);
957 push_unique_key( L, STACK_TRACE_KEY); // err STACK_TRACE_KEY
958 // yields nil if no stack was generated (in case of cancellation for example) 950 // yields nil if no stack was generated (in case of cancellation for example)
959 lua_rawget( L, LUA_REGISTRYINDEX); // err trace|nil 951 REGISTRY_GET( L, STACKTRACE_REGKEY); // err trace|nil
960 ASSERT_L( lua_gettop( L) == 1 + stk_base_); 952 STACK_END( L, 1);
961 953
962 // For cancellation the error message is CANCEL_ERROR, and a stack trace isn't placed 954 // For cancellation the error message is CANCEL_ERROR, and a stack trace isn't placed
963 // For other errors, the message can be whatever was thrown, and we should have a stack trace table 955 // For other errors, the message can be whatever was thrown, and we should have a stack trace table
@@ -982,15 +974,17 @@ LUAG_FUNC( set_debug_threadname)
982 // C s_lane structure is a light userdata upvalue 974 // C s_lane structure is a light userdata upvalue
983 Lane* s = lua_touserdata( L, lua_upvalueindex( 1)); 975 Lane* s = lua_touserdata( L, lua_upvalueindex( 1));
984 luaL_checktype( L, -1, LUA_TSTRING); // "name" 976 luaL_checktype( L, -1, LUA_TSTRING); // "name"
977 lua_settop( L, 1);
978 STACK_CHECK_ABS( L, 1);
985 // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... 979 // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global...
986 push_unique_key( L, hidden_regkey); // "name" lud 980 REGISTRY_SET( L, hidden_regkey, lua_pushvalue( L, -2));
987 lua_pushvalue( L, -2); // "name" lud "name" 981 STACK_MID( L, 1);
988 lua_rawset( L, LUA_REGISTRYINDEX); // "name"
989 s->debug_name = lua_tostring( L, -1); 982 s->debug_name = lua_tostring( L, -1);
990 // keep a direct pointer on the string 983 // keep a direct pointer on the string
991 THREAD_SETNAME( s->debug_name); 984 THREAD_SETNAME( s->debug_name);
992 // to see VM name in Decoda debugger Virtual Machine window 985 // to see VM name in Decoda debugger Virtual Machine window
993 lua_setglobal( L, "decoda_name"); // 986 lua_setglobal( L, "decoda_name"); //
987 STACK_END( L, 0);
994 return 0; 988 return 0;
995} 989}
996 990
diff --git a/src/tools.c b/src/tools.c
index ecd86b8..f8fc342 100644
--- a/src/tools.c
+++ b/src/tools.c
@@ -300,10 +300,8 @@ static lua_CFunction luaG_tocfunction( lua_State *L, int _i, FuncSubType *_out)
300 return p; 300 return p;
301} 301}
302 302
303 303// crc64/we of string "LOOKUPCACHE_REGKEY" generated at http://www.nitrxgen.net/hashgen/
304#define LOOKUP_KEY "ddea37aa-50c7-4d3f-8e0b-fb7a9d62bac5" 304static DECLARE_CONST_UNIQUE_KEY( LOOKUPCACHE_REGKEY, 0x837a68dfc6fcb716);
305#define LOOKUP_KEY_CACHE "d1059270-4976-4193-a55b-c952db5ab7cd"
306
307 305
308// inspired from tconcat() in ltablib.c 306// inspired from tconcat() in ltablib.c
309static char const* luaG_pushFQN( lua_State* L, int t, int last, size_t* length) 307static char const* luaG_pushFQN( lua_State* L, int t, int last, size_t* length)
@@ -576,13 +574,13 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* name_)
576 STACK_MID( L, 2); 574 STACK_MID( L, 2);
577 } 575 }
578 // retrieve the cache, create it if we haven't done it yet 576 // retrieve the cache, create it if we haven't done it yet
579 lua_getfield( L, LUA_REGISTRYINDEX, LOOKUP_KEY_CACHE); // {} {fqn} {cache}? 577 REGISTRY_GET( L, LOOKUPCACHE_REGKEY); // {} {fqn} {cache}?
580 if( lua_isnil( L, -1)) 578 if( lua_isnil( L, -1))
581 { 579 {
582 lua_pop( L, 1); // {} {fqn} 580 lua_pop( L, 1); // {} {fqn}
583 lua_newtable( L); // {} {fqn} {cache} 581 lua_newtable( L); // {} {fqn} {cache}
584 lua_pushvalue( L, -1); // {} {fqn} {cache} {cache} 582 REGISTRY_SET( L, LOOKUPCACHE_REGKEY, lua_pushvalue( L, -2));
585 lua_setfield( L, LUA_REGISTRYINDEX, LOOKUP_KEY_CACHE); // {} {fqn} {cache} 583 STACK_MID( L, 3);
586 } 584 }
587 // process everything we find in that table, filling in lookup data for all functions and tables we see there 585 // process everything we find in that table, filling in lookup data for all functions and tables we see there
588 populate_func_lookup_table_recur( DEBUGSPEW_PARAM_COMMA( U) L, ctx_base, in_base, start_depth); 586 populate_func_lookup_table_recur( DEBUGSPEW_PARAM_COMMA( U) L, ctx_base, in_base, start_depth);