diff options
Diffstat (limited to 'src/state.c')
| -rw-r--r-- | src/state.c | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/src/state.c b/src/state.c index cbbc0d8..81371b7 100644 --- a/src/state.c +++ b/src/state.c | |||
| @@ -51,7 +51,7 @@ THE SOFTWARE. | |||
| 51 | */ | 51 | */ |
| 52 | 52 | ||
| 53 | //--- | 53 | //--- |
| 54 | // [val]= new_require( ... ) | 54 | // [val,...]= new_require( ... ) |
| 55 | // | 55 | // |
| 56 | // Call 'old_require' but only one lane at a time. | 56 | // Call 'old_require' but only one lane at a time. |
| 57 | // | 57 | // |
| @@ -59,36 +59,32 @@ THE SOFTWARE. | |||
| 59 | // | 59 | // |
| 60 | static int luaG_new_require( lua_State* L) | 60 | static int luaG_new_require( lua_State* L) |
| 61 | { | 61 | { |
| 62 | int rc, i; | 62 | int rc; |
| 63 | int args = lua_gettop( L); | 63 | int const args = lua_gettop( L); // args |
| 64 | Universe* U = universe_get( L); | 64 | Universe* U = universe_get( L); |
| 65 | //char const* modname = luaL_checkstring( L, 1); | 65 | //char const* modname = luaL_checkstring( L, 1); |
| 66 | 66 | ||
| 67 | STACK_GROW( L, args + 1); | 67 | STACK_GROW( L, 1); |
| 68 | STACK_CHECK( L, 0); | ||
| 69 | 68 | ||
| 70 | lua_pushvalue( L, lua_upvalueindex( 1)); | 69 | lua_pushvalue( L, lua_upvalueindex( 1)); // args require |
| 71 | for( i = 1; i <= args; ++ i) | 70 | lua_insert( L, 1); // require args |
| 72 | { | ||
| 73 | lua_pushvalue( L, i); | ||
| 74 | } | ||
| 75 | 71 | ||
| 76 | // Using 'lua_pcall()' to catch errors; otherwise a failing 'require' would | 72 | // Using 'lua_pcall()' to catch errors; otherwise a failing 'require' would |
| 77 | // leave us locked, blocking any future 'require' calls from other lanes. | 73 | // leave us locked, blocking any future 'require' calls from other lanes. |
| 78 | // | 74 | |
| 79 | MUTEX_LOCK( &U->require_cs); | 75 | MUTEX_LOCK( &U->require_cs); |
| 80 | rc = lua_pcall( L, args, 1 /*retvals*/, 0 /*errfunc*/ ); | 76 | // starting with Lua 5.4, require may return a second optional value, so we need LUA_MULTRET |
| 77 | rc = lua_pcall( L, args, LUA_MULTRET, 0 /*errfunc*/ ); // err|result(s) | ||
| 81 | MUTEX_UNLOCK( &U->require_cs); | 78 | MUTEX_UNLOCK( &U->require_cs); |
| 82 | 79 | ||
| 83 | // the required module (or an error message) is left on the stack as returned value by original require function | 80 | // the required module (or an error message) is left on the stack as returned value by original require function |
| 84 | STACK_END( L, 1); | ||
| 85 | 81 | ||
| 86 | if( rc != LUA_OK) // LUA_ERRRUN / LUA_ERRMEM ? | 82 | if( rc != LUA_OK) // LUA_ERRRUN / LUA_ERRMEM ? |
| 87 | { | 83 | { |
| 88 | return lua_error( L); // error message already at [-1] | 84 | return lua_error( L); |
| 89 | } | 85 | } |
| 90 | 86 | // should be 1 for Lua <= 5.3, 1 or 2 starting with Lua 5.4 | |
| 91 | return 1; | 87 | return lua_gettop(L); // result(s) |
| 92 | } | 88 | } |
| 93 | 89 | ||
| 94 | /* | 90 | /* |
