From 0326aa6636b1dce2b2fc4c9db53b023534ca0512 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 8 Jul 2014 14:35:32 +0200 Subject: fix lookup of globals created by on_state_create * Postponed _G scan for function lookup database to after on_state_create invocation * Fixed a crash when USE_DEBUG_SPEW == 1 --- CHANGES | 4 ++++ src/lanes.c | 16 ++++++++++++---- src/tools.c | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 3d8015d..bba16d3 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,9 @@ CHANGES: +CHANGE 114: BGe 8-Jul-14 + * Postponed _G scan for function lookup database to after on_state_create invocation + * Fixed a crash when USE_DEBUG_SPEW == 1 + CHANGE 113: BGe 17-Jun-14 * bumped version to 3.9.6 * separate deep userdata code in a dedicated file to allow external modules to implement Lanes-compatible deep userdata without requiring a binary dependency against the Lanes module diff --git a/src/lanes.c b/src/lanes.c index 6acc711..1d60700 100644 --- a/src/lanes.c +++ b/src/lanes.c @@ -3000,6 +3000,7 @@ static volatile long s_initCount = 0; LUAG_FUNC( configure) { struct s_Universe* U = get_universe( L); + bool_t const from_master_state = (U == NULL); char const* name = luaL_checkstring( L, lua_upvalueindex( 1)); _ASSERT_L( L, lua_type( L, 1) == LUA_TTABLE); @@ -3045,7 +3046,7 @@ LUAG_FUNC( configure) STACK_CHECK( L); DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END, L)); - DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); + DEBUGSPEW_CODE( if( U) ++ U->debugspew_indent_depth); lua_getfield( L, 1, "protect_allocator"); // settings protect_allocator if( lua_toboolean( L, -1)) @@ -3070,6 +3071,7 @@ LUAG_FUNC( configure) lua_pushlightuserdata( L, UNIVERSE_REGKEY); // settings UNIVERSE_REGKEY U = (struct s_Universe*) lua_newuserdata( L, sizeof( struct s_Universe)); // settings UNIVERSE_REGKEY universe memset( U, 0, sizeof( struct s_Universe)); + DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); lua_newtable( L); // settings UNIVERSE_REGKEY universe mt lua_getfield( L, 1, "shutdown_timeout"); // settings UNIVERSE_REGKEY universe mt shutdown_timeout lua_pushcclosure( L, selfdestruct_gc, 1); // settings UNIVERSE_REGKEY universe mt selfdestruct_gc @@ -3195,9 +3197,15 @@ LUAG_FUNC( configure) // record all existing C/JIT-fast functions // Lua 5.2 no longer has LUA_GLOBALSINDEX: we must push globals table on the stack - lua_pushglobaltable( L); // settings M _G - populate_func_lookup_table( L, -1, NULL); - lua_pop( L, 1); // settings M + if( from_master_state) + { + // don't do this when called during the initialization of a new lane, + // because we will do it after on_state_create() is called, + // and we don't want skip _G because of caching in case globals are created then + lua_pushglobaltable( L); // settings M _G + populate_func_lookup_table( L, -1, NULL); + lua_pop( L, 1); // settings M + } // set _R[CONFIG_REGKEY] = settings lua_pushvalue( L, -2); // settings M settings lua_setfield( L, LUA_REGISTRYINDEX, CONFIG_REGKEY); // settings M diff --git a/src/tools.c b/src/tools.c index 628b277..e0f4c40 100644 --- a/src/tools.c +++ b/src/tools.c @@ -446,6 +446,8 @@ static void populate_func_lookup_table_recur( lua_State* L, int _ctx_base, int _ // prepare the stack for database feed lua_pushvalue( L, -1); // ... {_i} {bfc} k func "f.q.n" "f.q.n" lua_pushvalue( L, -3); // ... {_i} {bfc} k func "f.q.n" "f.q.n" func + ASSERT_L( lua_rawequal( L, -1, -4)); + ASSERT_L( lua_rawequal( L, -2, -3)); // t["f.q.n"] = func lua_rawset( L, dest); // ... {_i} {bfc} k func "f.q.n" // t[func] = "f.q.n" @@ -704,6 +706,23 @@ lua_State* luaG_newstate( struct s_Universe* U, lua_State* from_, char const* li // after all this, register everything we find in our name<->function database lua_pushglobaltable( L); // Lua 5.2 no longer has LUA_GLOBALSINDEX: we must push globals table on the stack populate_func_lookup_table( L, -1, NULL); + +#if 0 && USE_DEBUG_SPEW + // dump the lookup database contents + lua_getfield( L, LUA_REGISTRYINDEX, LOOKUP_REGKEY); // {} + lua_pushnil( L); // {} nil + while( lua_next( L, -2)) // {} k v + { + lua_getglobal( L, "print"); // {} k v print + lua_pushlstring( L, debugspew_indent, U->debugspew_indent_depth); // {} k v print " " + lua_pushvalue( L, -4); // {} k v print " " k + lua_pushvalue( L, -4); // {} k v print " " k v + lua_call( L, 3, 0); // {} k v + lua_pop( L, 1); // {} k + } + lua_pop( L, 1); // {} +#endif // USE_DEBUG_SPEW + lua_pop( L, 1); STACK_END( L, 0); DEBUGSPEW_CODE( -- U->debugspew_indent_depth); -- cgit v1.2.3-55-g6feb