diff options
| -rw-r--r-- | src/tools.c | 49 |
1 files changed, 34 insertions, 15 deletions
diff --git a/src/tools.c b/src/tools.c index 49194d0..da6ffe5 100644 --- a/src/tools.c +++ b/src/tools.c | |||
| @@ -467,12 +467,12 @@ static void populate_func_lookup_table_recur( lua_State* L, int _ctx_base, int _ | |||
| 467 | /* | 467 | /* |
| 468 | * create a "fully.qualified.name" <-> function equivalence database | 468 | * create a "fully.qualified.name" <-> function equivalence database |
| 469 | */ | 469 | */ |
| 470 | void populate_func_lookup_table( lua_State* L, int _i, char const* _name) | 470 | void populate_func_lookup_table( lua_State* L, int _i, char const* name_) |
| 471 | { | 471 | { |
| 472 | int const ctx_base = lua_gettop( L) + 1; | 472 | int const ctx_base = lua_gettop( L) + 1; |
| 473 | int const in_base = lua_absindex( L, _i); | 473 | int const in_base = lua_absindex( L, _i); |
| 474 | int const start_depth = _name ? 1 : 0; | 474 | int const start_depth = name_ ? 1 : 0; |
| 475 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L, _name ? _name : "NULL")); | 475 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L, name_ ? name_ : "NULL")); |
| 476 | DEBUGSPEW_CODE( ++ debugspew_indent_depth); | 476 | DEBUGSPEW_CODE( ++ debugspew_indent_depth); |
| 477 | STACK_GROW( L, 3); | 477 | STACK_GROW( L, 3); |
| 478 | STACK_CHECK( L); | 478 | STACK_CHECK( L); |
| @@ -484,22 +484,41 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* _name) | |||
| 484 | lua_pushvalue( L, -1); // {} {} | 484 | lua_pushvalue( L, -1); // {} {} |
| 485 | lua_setfield( L, LUA_REGISTRYINDEX, LOOKUP_KEY); // {} | 485 | lua_setfield( L, LUA_REGISTRYINDEX, LOOKUP_KEY); // {} |
| 486 | } | 486 | } |
| 487 | lua_newtable( L); // {} {fqn} | 487 | if( lua_type( L, in_base) == LUA_TFUNCTION) // for example when a module is a simple function |
| 488 | if( _name) | ||
| 489 | { | 488 | { |
| 490 | lua_pushstring( L, _name); // {} {fqn} "name" | 489 | name_ = name_ ? name_ : "NULL"; |
| 491 | lua_rawseti( L, -2, start_depth); // {} {fqn} | 490 | lua_pushvalue( L, in_base); // {} f |
| 491 | lua_pushstring( L, name_); // {} f _name | ||
| 492 | lua_rawset( L, -3); // {} | ||
| 493 | lua_pushstring( L, name_); // {} _name | ||
| 494 | lua_pushvalue( L, in_base); // {} _name f | ||
| 495 | lua_rawset( L, -3); // {} | ||
| 496 | lua_pop( L, 1); // | ||
| 497 | } | ||
| 498 | else if( lua_type( L, in_base) == LUA_TTABLE) | ||
| 499 | { | ||
| 500 | lua_newtable( L); // {} {fqn} | ||
| 501 | if( name_) | ||
| 502 | { | ||
| 503 | lua_pushstring( L, name_); // {} {fqn} "name" | ||
| 504 | lua_rawseti( L, -2, start_depth); // {} {fqn} | ||
| 505 | } | ||
| 506 | lua_getfield( L, LUA_REGISTRYINDEX, LOOKUP_KEY_CACHE); // {} {fqn} {cache}? | ||
| 507 | if( lua_isnil( L, -1)) | ||
| 508 | { | ||
| 509 | lua_pop( L, 1); // {} {fqn} | ||
| 510 | lua_newtable( L); // {} {fqn} {cache} | ||
| 511 | lua_pushvalue( L, -1); // {} {fqn} {cache} {cache} | ||
| 512 | lua_setfield( L, LUA_REGISTRYINDEX, LOOKUP_KEY_CACHE); // {} {fqn} {cache} | ||
| 513 | } | ||
| 514 | populate_func_lookup_table_recur( L, ctx_base, in_base, start_depth); // {...} {fqn} {cache} | ||
| 515 | lua_pop( L, 3); | ||
| 492 | } | 516 | } |
| 493 | lua_getfield( L, LUA_REGISTRYINDEX, LOOKUP_KEY_CACHE); // {} {fqn} {cache}? | 517 | else |
| 494 | if( lua_isnil( L, -1)) | ||
| 495 | { | 518 | { |
| 496 | lua_pop( L, 1); // {} {fqn} | 519 | lua_pop( L, 1); // |
| 497 | lua_newtable( L); // {} {fqn} {cache} | 520 | (void) luaL_error( L, "unsupported module type %s", lua_typename( L, lua_type( L, in_base))); |
| 498 | lua_pushvalue( L, -1); // {} {fqn} {cache} {cache} | ||
| 499 | lua_setfield( L, LUA_REGISTRYINDEX, LOOKUP_KEY_CACHE); // {} {fqn} {cache} | ||
| 500 | } | 521 | } |
| 501 | populate_func_lookup_table_recur( L, ctx_base, in_base, start_depth); // {...} {fqn} {cache} | ||
| 502 | lua_pop( L, 3); | ||
| 503 | STACK_END( L, 0); | 522 | STACK_END( L, 0); |
| 504 | DEBUGSPEW_CODE( -- debugspew_indent_depth); | 523 | DEBUGSPEW_CODE( -- debugspew_indent_depth); |
| 505 | } | 524 | } |
