@@ -121,9 +121,9 @@ static int checkRegisteredModules(lua_State* L, const char* path)
121121int 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
153153int 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