diff options
Diffstat (limited to '')
-rw-r--r-- | src/tools.c | 48 |
1 files changed, 10 insertions, 38 deletions
diff --git a/src/tools.c b/src/tools.c index c2491ba..f935c26 100644 --- a/src/tools.c +++ b/src/tools.c | |||
@@ -32,6 +32,7 @@ THE SOFTWARE. | |||
32 | */ | 32 | */ |
33 | 33 | ||
34 | #include "compat.h" | 34 | #include "compat.h" |
35 | #include "universe.h" | ||
35 | #include "tools.h" | 36 | #include "tools.h" |
36 | #include "keeper.h" | 37 | #include "keeper.h" |
37 | #include "lanes.h" | 38 | #include "lanes.h" |
@@ -48,21 +49,6 @@ THE SOFTWARE. | |||
48 | extern luaG_IdFunction copydeep( struct s_Universe* U, lua_State* L, lua_State* L2, int index, enum eLookupMode mode_); | 49 | extern luaG_IdFunction copydeep( struct s_Universe* U, lua_State* L, lua_State* L2, int index, enum eLookupMode mode_); |
49 | extern void push_registry_subtable( lua_State* L, void* key_); | 50 | extern void push_registry_subtable( lua_State* L, void* key_); |
50 | 51 | ||
51 | void* const UNIVERSE_REGKEY = (void*) luaopen_lanes_core; | ||
52 | |||
53 | /* | ||
54 | * ############################################################################################### | ||
55 | * ########################################### ASSERT ############################################ | ||
56 | * ############################################################################################### | ||
57 | */ | ||
58 | void ASSERT_IMPL( lua_State* L, bool_t cond_, char const* file_, int const line_, char const* text_) | ||
59 | { | ||
60 | if ( !cond_) | ||
61 | { | ||
62 | (void) luaL_error( L, "ASSERT failed: %s:%d '%s'", file_, line_, text_); | ||
63 | } | ||
64 | } | ||
65 | |||
66 | char const* const CONFIG_REGKEY = "ee932492-a654-4506-9da8-f16540bdb5d4"; | 52 | char const* const CONFIG_REGKEY = "ee932492-a654-4506-9da8-f16540bdb5d4"; |
67 | char const* const LOOKUP_REGKEY = "ddea37aa-50c7-4d3f-8e0b-fb7a9d62bac5"; | 53 | char const* const LOOKUP_REGKEY = "ddea37aa-50c7-4d3f-8e0b-fb7a9d62bac5"; |
68 | 54 | ||
@@ -150,19 +136,7 @@ void initialize_on_state_create( struct s_Universe* U, lua_State* L) | |||
150 | STACK_END( L, 0); | 136 | STACK_END( L, 0); |
151 | } | 137 | } |
152 | 138 | ||
153 | 139 | // ################################################################################################ | |
154 | struct s_Universe* get_universe( lua_State* L) | ||
155 | { | ||
156 | struct s_Universe* universe; | ||
157 | STACK_GROW( L, 2); | ||
158 | STACK_CHECK( L); | ||
159 | lua_pushlightuserdata( L, UNIVERSE_REGKEY); | ||
160 | lua_rawget( L, LUA_REGISTRYINDEX); | ||
161 | universe = lua_touserdata( L, -1); // NULL if nil | ||
162 | lua_pop( L, 1); | ||
163 | STACK_END( L, 0); | ||
164 | return universe; | ||
165 | } | ||
166 | 140 | ||
167 | // just like lua_xmove, args are (from, to) | 141 | // just like lua_xmove, args are (from, to) |
168 | void luaG_copy_one_time_settings( struct s_Universe* U, lua_State* L, lua_State* L2) | 142 | void luaG_copy_one_time_settings( struct s_Universe* U, lua_State* L, lua_State* L2) |
@@ -367,7 +341,7 @@ static void update_lookup_entry( lua_State* L, int _ctx_base, int _depth) | |||
367 | size_t prevNameLength, newNameLength; | 341 | size_t prevNameLength, newNameLength; |
368 | char const* prevName; | 342 | char const* prevName; |
369 | DEBUGSPEW_CODE( char const *newName); | 343 | DEBUGSPEW_CODE( char const *newName); |
370 | DEBUGSPEW_CODE( struct s_Universe* U = get_universe( L)); | 344 | DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); |
371 | 345 | ||
372 | STACK_CHECK( L); | 346 | STACK_CHECK( L); |
373 | // first, raise an error if the function is already known | 347 | // first, raise an error if the function is already known |
@@ -438,7 +412,7 @@ static void populate_func_lookup_table_recur( lua_State* L, int _ctx_base, int _ | |||
438 | int const cache = _ctx_base + 2; | 412 | int const cache = _ctx_base + 2; |
439 | // we need to remember subtables to process them after functions encountered at the current depth (breadth-first search) | 413 | // we need to remember subtables to process them after functions encountered at the current depth (breadth-first search) |
440 | int const breadth_first_cache = lua_gettop( L) + 1; | 414 | int const breadth_first_cache = lua_gettop( L) + 1; |
441 | DEBUGSPEW_CODE( struct s_Universe* U = get_universe( L)); | 415 | DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); |
442 | 416 | ||
443 | STACK_GROW( L, 6); | 417 | STACK_GROW( L, 6); |
444 | // slot _i contains a table where we search for functions (or a full userdata with a metatable) | 418 | // slot _i contains a table where we search for functions (or a full userdata with a metatable) |
@@ -556,7 +530,7 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* name_) | |||
556 | int const ctx_base = lua_gettop( L) + 1; | 530 | int const ctx_base = lua_gettop( L) + 1; |
557 | int const in_base = lua_absindex( L, _i); | 531 | int const in_base = lua_absindex( L, _i); |
558 | int start_depth = 0; | 532 | int start_depth = 0; |
559 | DEBUGSPEW_CODE( struct s_Universe* U = get_universe( L)); | 533 | DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); |
560 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L, name_ ? name_ : "NULL")); | 534 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L, name_ ? name_ : "NULL")); |
561 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); | 535 | DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); |
562 | STACK_GROW( L, 3); | 536 | STACK_GROW( L, 3); |
@@ -670,14 +644,12 @@ lua_State* luaG_newstate( struct s_Universe* U, lua_State* from_, char const* li | |||
670 | } | 644 | } |
671 | 645 | ||
672 | STACK_GROW( L, 2); | 646 | STACK_GROW( L, 2); |
673 | STACK_CHECK( L); | ||
674 | 647 | ||
675 | // copy the universe as a light userdata (only the master state holds the full userdata) | 648 | // copy the universe as a light userdata (only the master state holds the full userdata) |
676 | // that way, if Lanes is required in this new state, we'll know we are part of this universe | 649 | // that way, if Lanes is required in this new state, we'll know we are part of this universe |
677 | lua_pushlightuserdata( L, UNIVERSE_REGKEY); | 650 | universe_store( L, U); |
678 | lua_pushlightuserdata( L, U); | 651 | |
679 | lua_rawset( L, LUA_REGISTRYINDEX); | 652 | STACK_CHECK( L); |
680 | STACK_MID( L, 0); | ||
681 | 653 | ||
682 | // we'll need this every time we transfer some C function from/to this state | 654 | // we'll need this every time we transfer some C function from/to this state |
683 | lua_newtable( L); | 655 | lua_newtable( L); |
@@ -858,7 +830,7 @@ static int table_lookup_sentinel( lua_State* L) | |||
858 | */ | 830 | */ |
859 | static char const* find_lookup_name( lua_State* L, uint_t i, enum eLookupMode mode_, char const* upName_, size_t* len_) | 831 | static char const* find_lookup_name( lua_State* L, uint_t i, enum eLookupMode mode_, char const* upName_, size_t* len_) |
860 | { | 832 | { |
861 | DEBUGSPEW_CODE( struct s_Universe* const U = get_universe( L)); | 833 | DEBUGSPEW_CODE( struct s_Universe* const U = universe_get( L)); |
862 | char const* fqn; | 834 | char const* fqn; |
863 | ASSERT_L( lua_isfunction( L, i) || lua_istable( L, i)); // ... v ... | 835 | ASSERT_L( lua_isfunction( L, i) || lua_istable( L, i)); // ... v ... |
864 | STACK_CHECK( L); | 836 | STACK_CHECK( L); |
@@ -1950,7 +1922,7 @@ int luaG_new_require( lua_State* L) | |||
1950 | { | 1922 | { |
1951 | int rc, i; | 1923 | int rc, i; |
1952 | int args = lua_gettop( L); | 1924 | int args = lua_gettop( L); |
1953 | struct s_Universe* U = get_universe( L); | 1925 | struct s_Universe* U = universe_get( L); |
1954 | //char const* modname = luaL_checkstring( L, 1); | 1926 | //char const* modname = luaL_checkstring( L, 1); |
1955 | 1927 | ||
1956 | STACK_GROW( L, args + 1); | 1928 | STACK_GROW( L, args + 1); |