Skip to content

Commit 42281aa

Browse files
authored
(regression) Fix lua_requireinternal to use variable number of stack args (luau-lang#1804)
Since 0.672 the `lua_proxyrequire` function is broken and attempt to use it fails with the error message `module must return a single value`. This function takes two stack arguments (`lua_require` takes one), but `lua_requireinternal` expects only one extra argument and pushes another one (`cacheKey`). As a result, the continuation function `lua_requirecont` (that has hardcoded number of stack args = 2) thinks that module returned more than one result and throw an exception. The fix instead of hardcoding number of arguments in the continuation function, pass this number in a first stack argument. This will allow both `lua_require` and `lua_proxyrequire` work at the same time.
1 parent 72f6c8b commit 42281aa

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Require/Runtime/src/RequireImpl.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ static int checkRegisteredModules(lua_State* L, const char* path)
121121
int lua_requirecont(lua_State* L, int status)
122122
{
123123
// Number of stack arguments present before this continuation is called.
124-
const int numStackArgs = 2;
124+
const int numStackArgs = lua_tointeger(L, 1);
125125
const int numResults = lua_gettop(L) - numStackArgs;
126-
const char* cacheKey = luaL_checkstring(L, 2);
126+
const char* cacheKey = luaL_checkstring(L, numStackArgs);
127127

128128
if (numResults > 1)
129129
luaL_error(L, "module must return a single value");
@@ -152,6 +152,8 @@ int lua_requirecont(lua_State* L, int status)
152152

153153
int lua_requireinternal(lua_State* L, const char* requirerChunkname)
154154
{
155+
int stackTop = lua_gettop(L);
156+
155157
// If modifying the state of the stack, please update numStackArgs in the
156158
// lua_requirecont continuation function.
157159

@@ -171,10 +173,14 @@ int lua_requireinternal(lua_State* L, const char* requirerChunkname)
171173
if (resolvedRequire.status == ResolvedRequire::Status::Cached)
172174
return 1;
173175

174-
// (1) path, (2) cacheKey
176+
// (1) path, ..., cacheKey
175177
lua_pushstring(L, resolvedRequire.cacheKey.c_str());
176178

177-
int numArgsBeforeLoad = lua_gettop(L);
179+
// Insert number of arguments before the continuation to check the results.
180+
int numArgsBeforeLoad = stackTop + 2;
181+
lua_pushinteger(L, numArgsBeforeLoad);
182+
lua_insert(L, 1);
183+
178184
int numResults = lrc->load(L, ctx, path, resolvedRequire.chunkname.c_str(), resolvedRequire.contents.c_str());
179185
if (numResults == -1)
180186
{

0 commit comments

Comments
 (0)