Added: AutoRun parameter (default True)

Added: unit tests / showcases for loading and running multiple scripts
This commit is contained in:
Mark van Renswoude 2015-12-30 13:33:15 +00:00
parent af0786dbf5
commit 859780bc7a
2 changed files with 59 additions and 22 deletions

View File

@ -250,10 +250,10 @@ type
constructor Create;
destructor Destroy; override;
procedure LoadFromString(const AData: string; const AChunkName: string = ''); virtual;
procedure LoadFromStream(AStream: TStream; AOwnership: TStreamOwnership = soReference; const AChunkName: string = ''); virtual;
procedure LoadFromFile(const AFileName: string; const AChunkName: string = ''); virtual;
procedure LoadFromScript(AScript: TLuaScript; AOwnership: TStreamOwnership = soReference; const AChunkName: string = ''); virtual;
procedure LoadFromString(const AData: string; AAutoRun: Boolean = True; const AChunkName: string = ''); virtual;
procedure LoadFromStream(AStream: TStream; AOwnership: TStreamOwnership = soReference; AAutoRun: Boolean = True; const AChunkName: string = ''); virtual;
procedure LoadFromFile(const AFileName: string; AAutoRun: Boolean = True; const AChunkName: string = ''); virtual;
procedure LoadFromScript(AScript: TLuaScript; AOwnership: TStreamOwnership = soReference; AAutoRun: Boolean = True; const AChunkName: string = ''); virtual;
function GetGlobalVariable(const AName: string): ILuaVariable;
procedure SetGlobalVariable(const AName: string; AVariable: TLuaImplicitVariable);
@ -991,25 +991,25 @@ begin
end;
procedure TLua.LoadFromString(const AData: string; const AChunkName: string);
procedure TLua.LoadFromString(const AData: string; AAutoRun: Boolean; const AChunkName: string);
begin
LoadFromScript(TLuaScript.Create(AData), soOwned, AChunkName);
LoadFromScript(TLuaScript.Create(AData), soOwned, AAutoRun, AChunkName);
end;
procedure TLua.LoadFromStream(AStream: TStream; AOwnership: TStreamOwnership; const AChunkName: string);
procedure TLua.LoadFromStream(AStream: TStream; AOwnership: TStreamOwnership; AAutoRun: Boolean; const AChunkName: string);
begin
LoadFromScript(TLuaScript.Create(AStream, AOwnership), soOwned, AChunkName);
LoadFromScript(TLuaScript.Create(AStream, AOwnership), soOwned, AAutoRun, AChunkName);
end;
procedure TLua.LoadFromFile(const AFileName, AChunkName: string);
procedure TLua.LoadFromFile(const AFileName: string; AAutoRun: Boolean; const AChunkName: string);
begin
LoadFromScript(TLuaScript.Create(TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone), soOwned), soOwned, AChunkName);
LoadFromScript(TLuaScript.Create(TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone), soOwned), soOwned, AAutoRun, AChunkName);
end;
procedure TLua.LoadFromScript(AScript: TLuaScript; AOwnership: TStreamOwnership; const AChunkName: string);
procedure TLua.LoadFromScript(AScript: TLuaScript; AOwnership: TStreamOwnership; AAutoRun: Boolean; const AChunkName: string);
var
chunkName: PAnsiChar;
@ -1023,7 +1023,11 @@ begin
FreeLuaString(chunkName);
end;
AfterLoad;
if not Loaded then
AfterLoad;
if AAutoRun then
Run;
finally
if AOwnership = soOwned then
FreeAndNil(AScript);
@ -1139,9 +1143,14 @@ end;
procedure TLua.RaiseLastLuaError;
var
errorMessage: string;
begin
// TODO shouldn't we pop the error messag efrom the stack?
raise ELuaException.Create(string(lua_tolstring(State, -1, nil)));
errorMessage := LuaToString(State, -1);
lua_pop(State, 1);
raise ELuaException.Create(errorMessage);
end;

View File

@ -24,6 +24,8 @@ type
procedure NewState;
procedure LoadAndRunFromString;
procedure LoadAndRunFromStream;
procedure LoadMultiple;
procedure ChunkNameInException;
procedure Input;
procedure Output;
@ -86,7 +88,6 @@ end;
procedure TTestWrapper.LoadAndRunFromString;
begin
Lua.LoadFromString('print("Hello world!")');
Lua.Run;
CheckEquals('Hello world!', Printed.ToString);
end;
@ -94,16 +95,23 @@ end;
procedure TTestWrapper.LoadAndRunFromStream;
begin
Lua.LoadFromStream(TStringStream.Create('print("Hello world!")'), soOwned);
Lua.Run;
CheckEquals('Hello world!', Printed.ToString);
end;
procedure TTestWrapper.LoadMultiple;
begin
Lua.LoadFromString('print "Hello world!"', True, 'Script1');
Lua.LoadFromString('print "Goodbye world!"', True, 'Script2');
CheckEquals('Hello world!Goodbye world!', Printed.ToString);
end;
procedure TTestWrapper.Input;
begin
Lua.SetGlobalVariable('thingy', 'world');
Lua.LoadFromString('print("Hello "..thingy.."!")');
Lua.Run;
CheckEquals('Hello world!', Printed.ToString);
end;
@ -115,7 +123,6 @@ var
begin
Lua.LoadFromString('output = "Hello world!"');
Lua.Run;
output := lua.GetGlobalVariable('output');
CheckNotNull(output, 'output is nil');
@ -123,6 +130,26 @@ begin
end;
procedure TTestWrapper.ChunkNameInException;
begin
Lua.LoadFromString('print("This one''s alright")', True, 'Script1');
try
Lua.LoadFromString('print("This one isn''t"', True, 'Script2');
Fail('ELuaException expected');
except
on E:Exception do
begin
CheckIs(E, ELuaException);
CheckEquals('[string "Script2"]:1: '')'' expected near <eof>', E.Message);
end;
end;
Lua.LoadFromString('print("Fine again!")', True, 'Script3');
CheckEquals('This one''s alrightFine again!', Printed.ToString);
end;
procedure TTestWrapper.DelphiFunction;
begin
Lua.RegisterFunction('myuppercase',
@ -132,7 +159,6 @@ begin
end);
Lua.LoadFromString('print(myuppercase("Hello world!"))');
Lua.Run;
CheckEquals('HELLO WORLD!', Printed.ToString);
end;
@ -149,6 +175,10 @@ begin
returnValues := Lua.Call('sum', [1, 2]);
CheckEquals(1, returnValues.Count, 'returnValues Count');
CheckEquals(3, returnValues[0].AsInteger, 'returnValues[0]');
returnValues := Lua.Call('sum', [4, 12]);
CheckEquals(1, returnValues.Count, 'returnValues Count');
CheckEquals(16, returnValues[0].AsInteger, 'returnValues[0]');
end;
@ -211,7 +241,7 @@ begin
input := TLuaTable.Create;
input.SetValue('text', 'Hello world!');
Lua.LoadFromString('print(message.text)');
Lua.LoadFromString('print(message.text)', False);
Lua.SetGlobalVariable('message', input);
Lua.Run;
@ -226,7 +256,6 @@ var
begin
Lua.LoadFromString('output = { answer = 42 }');
Lua.Run;
output := lua.GetGlobalVariable('output');
CheckNotNull(output, 'output is nil');
@ -259,7 +288,6 @@ begin
Lua.LoadFromString('table = invertTable({ value = "key" })'#13#10 +
'print(table.key)');
Lua.Run;
CheckEquals('value', Printed.ToString);
end;