Skip to content

Commit

Permalink
Merge pull request #854 from hoehermann/lua_require_return
Browse files Browse the repository at this point in the history
Have TLua_CustomRequire put the module table on the Lua stack.
  • Loading branch information
barbeque-squared authored Jul 13, 2024
2 parents d08d293 + 6c3a5c2 commit 7901585
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/lua/ULuaCore.pas
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function TLua_CustomPanic (L: Plua_State): integer; cdecl;
can be called with more than one parameter to require
some modules at once. e.g.: require('math', 'Usdx.Log')
modules are loaded from right to left
unlike standard require the module tables are not returned
the module table of the last module is returned
the standard require function in _require is called by
this function }
function TLua_CustomRequire(L: PLua_State): integer; cdecl;
Expand Down Expand Up @@ -957,26 +957,36 @@ function TLua_CustomPanic (L: Plua_State): integer; cdecl;
can be called with more than one parameter to require
some modules at once. e.g.: require('math', 'Usdx.Log')
modules are loaded from right to left
unlike standard require the module tables are not returned
the module table of the last module is returned
the standard require function in _require is called by
this function }
function TLua_CustomRequire(L: PLua_State): integer; cdecl;
begin
// no results
// how many values to return (this is to become 1 for the last iteration)
Result := 0;

// move through parameters
while (lua_getTop(L) >= 1) do
while ((lua_getTop(L) >= 1) and (Result = 0)) do
begin
if lua_getTop(L) = 1 then
begin
// this is the last in the list of modules to be loaded
// prepare to return its table
Result := 1;
end;

// make sure there is room on the stack for the require function
lua_checkstack(L,1);

// get luas require function
lua_getglobal(L, PChar('_require'));

// move it under the top param
lua_insert(L, -2);

// call it with next param (function + param are poped from stack)
lua_call(L, 1, 0);
// all return values are ignored (Result is set to 0) except for the last iteration (Result is set to 1)
lua_call(L, 1, Result);
end;
end;

Expand Down

0 comments on commit 7901585

Please sign in to comment.