diff options
author | Benoit Germain <bnt.germain@gmail.com> | 2021-09-22 15:39:13 +0200 |
---|---|---|
committer | Benoit Germain <bnt.germain@gmail.com> | 2021-09-22 15:39:13 +0200 |
commit | 9a53a224283fb1d41bae229ad34d61fe6dbc1b76 (patch) | |
tree | 8b21350377c3fdf2844871a01b1106c5ea6a0589 /src | |
parent | 53e6c545448a9763cabc0b22ac56f6dc8b9cf96d (diff) | |
download | lanes-9a53a224283fb1d41bae229ad34d61fe6dbc1b76.tar.gz lanes-9a53a224283fb1d41bae229ad34d61fe6dbc1b76.tar.bz2 lanes-9a53a224283fb1d41bae229ad34d61fe6dbc1b76.zip |
fix require() wrapper to return all values returned by original require()
Diffstat (limited to 'src')
-rw-r--r-- | src/macros_and_utils.h | 2 | ||||
-rw-r--r-- | src/state.c | 28 |
2 files changed, 13 insertions, 17 deletions
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index ae3a6c9..dba0010 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h | |||
@@ -60,7 +60,7 @@ extern char const* debugspew_indent; | |||
60 | int const L##_oldtop = 0 | 60 | int const L##_oldtop = 0 |
61 | 61 | ||
62 | #define STACK_MID( L, change) \ | 62 | #define STACK_MID( L, change) \ |
63 | do \ | 63 | do if( change != LUA_MULTRET) \ |
64 | { \ | 64 | { \ |
65 | int stack_check_a = lua_gettop( L) - L##_oldtop; \ | 65 | int stack_check_a = lua_gettop( L) - L##_oldtop; \ |
66 | int stack_check_b = (change); \ | 66 | int stack_check_b = (change); \ |
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 | /* |