Added Get/Set/AddRequirePath methods

This commit is contained in:
Mark van Renswoude 2017-06-04 11:16:55 +02:00
parent 84fe7f993f
commit 745c8b5a35
1 changed files with 52 additions and 0 deletions

52
Lua.pas
View File

@ -398,6 +398,13 @@ type
procedure OpenLibraries(ALibraries: TLuaLibraries); virtual; procedure OpenLibraries(ALibraries: TLuaLibraries); virtual;
{ Get or set the current path(s) used for require calls. Paths must be separated
by semicolons and questions marks will be replaced with the requested file name,
as per the Lua documentation at: https://www.lua.org/pil/8.1.html }
function GetRequirePath: string;
procedure SetRequirePath(const APath: string);
procedure AddRequirePath(const APath: string);
{ These methods should only be called right after one of the { These methods should only be called right after one of the
LoadFrom methods, which must have AutoRun set to False. } LoadFrom methods, which must have AutoRun set to False. }
procedure Run; virtual; procedure Run; virtual;
@ -1400,6 +1407,51 @@ begin
end; end;
function TLua.GetRequirePath: string;
begin
CheckState;
lua_getglobal(State, 'package');
try
lua_getfield(State, -1, 'path');
try
Result := TLuaHelpers.LuaToString(State, -1);
finally
lua_pop(State, 1);
end;
finally
lua_pop(State, 1);
end;
end;
procedure TLua.SetRequirePath(const APath: string);
begin
CheckState;
lua_getglobal(State, 'package');
try
TLuaHelpers.PushString(State, APath);
lua_setfield(State, -2, 'path');
finally
lua_pop(State, 1);
end;
end;
procedure TLua.AddRequirePath(const APath: string);
var
path: string;
begin
path := GetRequirePath;
if (Length(path) > 0) and (path[Length(path)] <> ';') then
path := path + ';';
SetRequirePath(path + APath);
end;
procedure TLua.Run; procedure TLua.Run;
begin begin
CheckIsFunction; CheckIsFunction;