You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2620 lines
68 KiB

{
Wrapper classes for Lua API
Created by M. van Renswoude, April 2014:
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
}
unit Lua;
9 years ago
interface
uses
System.Classes,
System.Generics.Collections,
System.Generics.Defaults,
System.Rtti,
9 years ago
System.SysUtils,
Lua.API;
9 years ago
type
ELuaException = class(Exception);
ELuaInitException = class(ELuaException);
ELuaUnsupportedParameterException = class(ELuaException);
ELuaUnsupportedVariableException = class(ELuaException);
ELuaNoFunctionException = class(ELuaException);
ELuaNativeCodeException = class(ELuaException);
9 years ago
TLuaLibrary = (Base, Coroutine, Table, IO, OS, StringLib, Bit32, Math, Debug, Package, All);
TLuaLibraries = set of TLuaLibrary;
TLuaDataType = (LuaNone, LuaNil, LuaNumber, LuaBoolean, LuaString, LuaTable,
LuaFunction, LuaUserData, LuaThread, LuaLightUserData, LuaCClosure);
9 years ago
TLuaVariableType = (VariableNone, VariableBoolean, VariableInteger,
VariableNumber, VariableUserData, VariableString,
VariableTable, VariableFunction, VariableCFunction);
ILuaTable = interface;
ILuaFunction = interface;
ILuaContext = interface;
TLuaCFunction = reference to procedure(Context: ILuaContext);
TLuaCMethod = procedure(Context: ILuaContext) of object;
TLuaPushFunction = reference to procedure(AFunction: TLuaCFunction);
ILuaVariable = interface
9 years ago
['{ADA0D4FB-F0FB-4493-8FEC-6FC92C80117F}']
function GetVariableType: TLuaVariableType;
function GetDataType: TLuaDataType;
9 years ago
function GetAsBoolean: Boolean;
function GetAsInteger: Integer;
function GetAsNumber: Double;
function GetAsUserData: Pointer;
function GetAsString: string;
function GetAsTable: ILuaTable;
function GetAsFunction: ILuaFunction;
function GetAsCFunction: TLuaCFunction;
procedure SetAsBoolean(ABoolean: Boolean);
procedure SetAsInteger(AInteger: Integer);
procedure SetAsNumber(ANumber: Double);
procedure SetAsUserData(AUserData: Pointer);
procedure SetAsString(AString: string);
procedure SetAsTable(ATable: ILuaTable);
property VariableType: TLuaVariableType read GetVariableType;
9 years ago
property DataType: TLuaDataType read GetDataType;
property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
property AsInteger: Integer read GetAsInteger write SetAsInteger;
property AsNumber: Double read GetAsNumber write SetAsNumber;
property AsUserData: Pointer read GetAsUserData write SetAsUserData;
property AsString: string read GetAsString write SetAsString;
property AsTable: ILuaTable read GetAsTable write SetAsTable;
property AsFunction: ILuaFunction read GetAsFunction;
property AsCFunction: TLuaCFunction read GetAsCFunction;
end;
TLuaImplicitVariable = record
Variable: ILuaVariable;
class operator Implicit(AValue: ILuaVariable): TLuaImplicitVariable;
class operator Implicit(AValue: Boolean): TLuaImplicitVariable;
class operator Implicit(AValue: Integer): TLuaImplicitVariable;
class operator Implicit(AValue: Double): TLuaImplicitVariable;
class operator Implicit(AValue: Pointer): TLuaImplicitVariable;
class operator Implicit(const AValue: string): TLuaImplicitVariable;
class operator Implicit(AValue: ILuaTable): TLuaImplicitVariable;
class operator Implicit(AValue: TLuaCFunction): TLuaImplicitVariable;
class operator Implicit(AValue: TLuaImplicitVariable): ILuaVariable;
class operator Implicit(AValue: TLuaImplicitVariable): Boolean;
class operator Implicit(AValue: TLuaImplicitVariable): Integer;
class operator Implicit(AValue: TLuaImplicitVariable): Double;
class operator Implicit(AValue: TLuaImplicitVariable): Pointer;
class operator Implicit(AValue: TLuaImplicitVariable): string;
class operator Implicit(AValue: TLuaImplicitVariable): ILuaTable;
end;
TLuaKeyValuePair = record
Key: ILuaVariable;
Value: ILuaVariable;
end;
ILuaTableEnumerator = interface
['{4C3F4E20-F9E7-42E6-9446-78C535AF2E30}']
function GetCurrent: TLuaKeyValuePair;
function MoveNext: Boolean;
property Current: TLuaKeyValuePair read GetCurrent;
end;
ILuaTable = interface
['{57FD52A1-7D53-485B-A630-29841C498387}']
function GetEnumerator: ILuaTableEnumerator;
function HasValue(AKey: TLuaImplicitVariable): Boolean;
function GetValue(AKey: TLuaImplicitVariable): ILuaVariable;
procedure SetValue(AKey: TLuaImplicitVariable; AValue: TLuaImplicitVariable);
end;
TLuaTableEnumerator = class(TInterfacedObject, ILuaTableEnumerator)
private
FEnumerator: TEnumerator<TPair<ILuaVariable, ILuaVariable>>;
public
constructor Create(AEnumerator: TEnumerator<TPair<ILuaVariable, ILuaVariable>>);
destructor Destroy; override;
{ ILuaTableEnumerator }
function GetCurrent: TLuaKeyValuePair;
function MoveNext: Boolean;
property Current: TLuaKeyValuePair read GetCurrent;
end;
TLuaVariableEqualityComparer = class(TInterfacedObject, IEqualityComparer<ILuaVariable>)
public
{ IEqualityComparer<ILuaVariable> }
function Equals(const Left, Right: ILuaVariable): Boolean; reintroduce;
function GetHashCode(const Value: ILuaVariable): Integer; reintroduce;
end;
TLuaTable = class(TInterfacedObject, ILuaTable)
private
FTable: TDictionary<ILuaVariable, ILuaVariable>;
public
constructor Create;
destructor Destroy; override;
{ ILuaTable }
function GetEnumerator: ILuaTableEnumerator;
function HasValue(AKey: TLuaImplicitVariable): Boolean;
function GetValue(AKey: TLuaImplicitVariable): ILuaVariable;
procedure SetValue(AKey: TLuaImplicitVariable; AValue: TLuaImplicitVariable);
9 years ago
end;
ILuaParametersEnumerator = interface
function GetCurrent: ILuaVariable;
function MoveNext: Boolean;
property Current: ILuaVariable read GetCurrent;
end;
ILuaReadParameters = interface(ILuaVariable)
['{FB611D9E-B51D-460B-B5AB-B567EF853222}']
function GetCount: Integer;
function GetItem(Index: Integer): ILuaVariable;
9 years ago
function GetEnumerator: ILuaParametersEnumerator;
9 years ago
function ToString: string;
property Count: Integer read GetCount;
property Items[Index: Integer]: ILuaVariable read GetItem; default;
9 years ago
end;
ILuaWriteParameters = interface
9 years ago
['{5CEEB16B-158E-44BE-8CAD-DC2C330A244A}']
function GetCount: Integer;
9 years ago
procedure Push(ABoolean: Boolean); overload;
procedure Push(AInteger: Integer); overload;
procedure Push(ANumber: Double); overload;
procedure Push(AUserData: Pointer); overload;
procedure Push(const AString: string); overload;
procedure Push(ATable: ILuaTable); overload;
property Count: Integer read GetCount;
end;
TCustomLuaParameters = class(TInterfacedObject, ILuaVariable, ILuaReadParameters)
protected
function GetDefaultVariable: ILuaVariable;
public
{ ILuaVariable }
function GetVariableType: TLuaVariableType;
function GetDataType: TLuaDataType;
function GetAsBoolean: Boolean;
function GetAsInteger: Integer;
function GetAsNumber: Double;
function GetAsUserData: Pointer;
function GetAsString: string;
function GetAsTable: ILuaTable;
function GetAsFunction: ILuaFunction;
function GetAsCFunction: TLuaCFunction;
procedure SetAsBoolean(ABoolean: Boolean);
procedure SetAsInteger(AInteger: Integer);
procedure SetAsNumber(ANumber: Double);
procedure SetAsUserData(AUserData: Pointer);
procedure SetAsString(AString: string);
procedure SetAsTable(ATable: ILuaTable);
{ ILuaReadParameters }
function GetCount: Integer; virtual; abstract;
function GetItem(Index: Integer): ILuaVariable; virtual; abstract;
function GetEnumerator: ILuaParametersEnumerator;
function ToString: string; override;
end;
TLuaParameters = class(TCustomLuaParameters, ILuaWriteParameters)
private
FParameters: TList<ILuaVariable>;
public
constructor Create;
destructor Destroy; override;
function GetCount: Integer; override;
function GetItem(Index: Integer): ILuaVariable; override;
{ ILuaWriteParameters }
procedure Push(ABoolean: Boolean); overload;
procedure Push(AInteger: Integer); overload;
procedure Push(ANumber: Double); overload;
procedure Push(AUserData: Pointer); overload;
procedure Push(const AString: string); overload;
procedure Push(ATable: ILuaTable); overload;
end;
ILuaFunction = interface
['{1BE5E470-0318-410E-8D5B-94BFE04A3DBE}']
function Call(): ILuaReadParameters; overload;
function Call(AParameters: array of const): ILuaReadParameters; overload;
function Call(AParameters: ILuaReadParameters): ILuaReadParameters; overload;
9 years ago
end;
ILuaContext = interface
['{1F999593-E3D1-4195-9463-A42025AE9830}']
function GetParameters: ILuaReadParameters;
function GetResult: ILuaWriteParameters;
9 years ago
property Parameters: ILuaReadParameters read GetParameters;
property Result: ILuaWriteParameters read GetResult;
9 years ago
end;
TCustomLuaRegistration = class(TObject)
private
FName: string;
protected
property Name: string read FName write FName;
public
constructor Create(const AName: string);
procedure Apply(AState: lua_State; APushFunction: TLuaPushFunction); virtual; abstract;
end;
TLuaFunctionRegistration = class(TCustomLuaRegistration)
private
FCallback: TLuaCFunction;
protected
property Callback: TLuaCFunction read FCallback;
public
constructor Create(const AName: string; ACallback: TLuaCFunction);
procedure Apply(AState: lua_State; APushFunction: TLuaPushFunction); override;
end;
TLuaFunctionTable = TDictionary<string, TLuaCFunction>;
TLuaTableRegistration = class(TCustomLuaRegistration)
private
FFunctionTable: TLuaFunctionTable;
protected
property FunctionTable: TLuaFunctionTable read FFunctionTable;
public
constructor Create(const AName: string);
destructor Destroy; override;
procedure RegisterFunction(const AName: string; AFunction: TLuaCFunction); virtual;
procedure Apply(AState: lua_State; APushFunction: TLuaPushFunction); override;
9 years ago
end;
TLuaRegistrationList = TObjectList<TCustomLuaRegistration>;
TLuaRegisteredFunctionDictionary = TDictionary<Integer, TLuaCFunction>;
9 years ago
TLuaScript = class(TObject)
private
FStream: TStream;
FStreamOwnership: TStreamOwnership;
FBuffer: PAnsiChar;
public
constructor Create(const AData: string); overload;
constructor Create(const AStream: TStream; AOwnership: TStreamOwnership = soReference); overload;
destructor Destroy; override;
function GetNextChunk(out ASize: NativeUint): PAnsiChar; virtual;
end;
9 years ago
TLua = class(TObject)
private
FState: lua_State;
FLoaded: Boolean;
FRegistrations: TLuaRegistrationList;
9 years ago
FRegisteredFunctions: TLuaRegisteredFunctionDictionary;
FRegisteredFunctionCookie: Integer;
FAutoOpenLibraries: TLuaLibraries;
FHasRun: Boolean;
FRttiContext: TRttiContext;
9 years ago
protected
function GetHasState: Boolean; virtual;
function GetState: lua_State; virtual;
function DoAlloc(APointer: Pointer; AOldSize, ANewSize: NativeUint): Pointer; virtual;
9 years ago
procedure DoNewState; virtual;
procedure DoClose; virtual;
procedure DoRegistration(ARegistration: TCustomLuaRegistration); virtual;
9 years ago
procedure SetAutoOpenLibraries(const Value: TLuaLibraries); virtual;
protected
procedure CheckState; virtual;
procedure CheckIsFunction; virtual;
9 years ago
procedure AfterLoad; virtual;
procedure AddRegistration(ARegistration: TCustomLuaRegistration); virtual;
9 years ago
function GetRegisteredFunctionCookie: Integer; virtual;
function RunRegisteredFunction(ACookie: Integer): Integer; virtual;
property Loaded: Boolean read FLoaded write FLoaded;
property HasRun: Boolean read FHasRun write FHasRun;
property Registrations: TLuaRegistrationList read FRegistrations;
property RegisteredFunctions: TLuaRegisteredFunctionDictionary read FRegisteredFunctions;
9 years ago
public
constructor Create;
destructor Destroy; override;
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;
9 years ago
function GetGlobalVariable(const AName: string): ILuaVariable; virtual;
procedure SetGlobalVariable(const AName: string; AVariable: TLuaImplicitVariable); virtual;
procedure RegisterFunction(const AName: string; AFunction: TLuaCFunction); virtual;
// procedure UnregisterFunction();
{ Registers all published methods of the specified object. If ATableName
is provided, the methods will be bundled in a global table and accessible as
ATableName.Method(). If not provided, the methods are accessible directly
as global functions. }
procedure RegisterFunctions(AObject: TObject; const ATableName: string = ''; AIncludePublicVisibility: Boolean = False); virtual;
// procedure UnregisterFunctions();
9 years ago
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
LoadFrom methods, which must have AutoRun set to False. }
procedure Run; virtual;
procedure GetByteCode(AStream: TStream; APop: Boolean = False); virtual;
procedure Capture(const AName: string); virtual;
function Call(const AFunctionName: string): ILuaReadParameters; overload; virtual;
function Call(const AFunctionName: string; AParameters: array of const): ILuaReadParameters; overload; virtual;
function Call(const AFunctionName: string; AParameters: ILuaReadParameters): ILuaReadParameters; overload; virtual;
9 years ago
property HasState: Boolean read GetHasState;
property State: lua_State read GetState;
property AutoOpenLibraries: TLuaLibraries read FAutoOpenLibraries write SetAutoOpenLibraries default [TLuaLibrary.All];
end;
TLuaHelpers = class(TObject)
private
class var RegistryKeyCounter: Int64;
public
class function GetLuaDataType(AType: Integer): TLuaDataType;
class function GetLuaVariableType(ADataType: TLuaDataType): TLuaVariableType;
class function CreateParameters(AParameters: array of const): ILuaReadParameters;
class procedure PushVariable(AState: lua_State; AVariable: ILuaVariable); overload;
class procedure PushVariable(AState: lua_State; AVariable: ILuaVariable; AVariableType: TLuaVariableType); overload;
class procedure PushString(AState: lua_State; const AValue: string);
class procedure PushTable(AState: lua_State; ATable: ILuaTable);
class procedure PushCFunction(AState: lua_State; AClosure: TLuaCFunction);
class function AllocLuaString(const AValue: string): PAnsiChar;
class procedure FreeLuaString(AValue: PAnsiChar);
class procedure RaiseLastLuaError(AState: lua_State);
class function LuaToString(AState: lua_State; AIndex: Integer): string;
class function CallFunction(AState: lua_State; AParameters: ILuaReadParameters): ILuaReadParameters;
class function NewRegistryKey: string;
end;
9 years ago
implementation
uses
System.Math,
System.SyncObjs,
System.TypInfo;
9 years ago
type
PLuaScript = ^TLuaScript;
TLuaParametersEnumerator = class(TInterfacedObject, ILuaParametersEnumerator)
private
FParameters: ILuaReadParameters;
FIndex: Integer;
protected
property Parameters: ILuaReadParameters read FParameters;
public
constructor Create(AParameters: ILuaReadParameters);
function GetCurrent: ILuaVariable;
function MoveNext: Boolean;
end;
TLuaStackParameters = class(TCustomLuaParameters)
9 years ago
private
FState: lua_State;
FCount: Integer;
protected
property State: lua_State read FState;
public
constructor Create(AState: lua_State; ACount: Integer = -1);
9 years ago
{ ILuaReadParameters }
function GetCount: Integer; override;
function GetItem(Index: Integer): ILuaVariable; override;
9 years ago
end;
TLuaStackVariable = class(TInterfacedObject, ILuaVariable)
9 years ago
private
FState: lua_State;
FIndex: Integer;
FTable: ILuaTable;
9 years ago
protected
property State: lua_State read FState;
property Index: Integer read FIndex;
9 years ago
public
constructor Create(AState: lua_State; AIndex: Integer);
{ ILuaVariable }
function GetDataType: TLuaDataType;
function GetVariableType: TLuaVariableType;
9 years ago
function GetAsBoolean: Boolean;
function GetAsInteger: Integer;
function GetAsNumber: Double;
function GetAsUserData: Pointer;
function GetAsString: string;
function GetAsTable: ILuaTable;
function GetAsFunction: ILuaFunction;
function GetAsCFunction: TLuaCFunction;
procedure SetAsBoolean(ABoolean: Boolean);
procedure SetAsInteger(AInteger: Integer);
procedure SetAsNumber(ANumber: Double);
procedure SetAsUserData(AUserData: Pointer);
procedure SetAsString(AString: string);
procedure SetAsTable(ATable: ILuaTable);
9 years ago
end;
TLuaResultParameters = class(TCustomLuaParameters)
private
FParameters: TList<ILuaVariable>;
public
constructor Create(AState: lua_State; ACount: Integer);
destructor Destroy; override;
function GetCount: Integer; override;
function GetItem(Index: Integer): ILuaVariable; override;
end;
TLuaVariable = class(TInterfacedObject, ILuaVariable)
private
FVariableType: TLuaVariableType;
FDataType: TLuaDataType;
FBooleanValue: Boolean;
FIntegerValue: Integer;
FNumberValue: Double;
FUserDataValue: Pointer;
FStringValue: string;
FTableValue: ILuaTable;
FFunctionValue: ILuaFunction;
FCFunctionValue: TLuaCFunction;
protected
property VariableType: TLuaVariableType read FVariableType write FVariableType;
property DataType: TLuaDataType read FDataType write FDataType;
property BooleanValue: Boolean read FBooleanValue write FBooleanValue;
property IntegerValue: Integer read FIntegerValue write FIntegerValue;
property NumberValue: Double read FNumberValue write FNumberValue;
property UserDataValue: Pointer read FUserDataValue write FUserDataValue;
property StringValue: string read FStringValue write FStringValue;
property TableValue: ILuaTable read FTableValue write FTableValue;
property FunctionValue: ILuaFunction read FFunctionValue write FFunctionValue;
property CFunctionValue: TLuaCFunction read FCFunctionValue write FCFunctionValue;
public
constructor Create; overload;
constructor Create(ABoolean: Boolean); overload;
constructor Create(AInteger: Integer); overload;
constructor Create(ANumber: Double); overload;
constructor Create(AUserData: Pointer); overload;
constructor Create(const AString: string); overload;
constructor Create(ATable: ILuaTable); overload;
constructor Create(AFunction: ILuaFunction); overload;
constructor Create(AFunction: TLuaCFunction); overload;
{ ILuaParameter }
function GetVariableType: TLuaVariableType;
function GetDataType: TLuaDataType;
function GetAsBoolean: Boolean;
function GetAsInteger: Integer;
function GetAsNumber: Double;
function GetAsUserData: Pointer;
function GetAsString: string;
function GetAsTable: ILuaTable;
function GetAsFunction: ILuaFunction;
function GetAsCFunction: TLuaCFunction;
procedure SetAsBoolean(ABoolean: Boolean);
procedure SetAsInteger(AInteger: Integer);
procedure SetAsNumber(ANumber: Double);
procedure SetAsUserData(AUserData: Pointer);
procedure SetAsString(AString: string);
procedure SetAsTable(ATable: ILuaTable);
end;
TLuaCachedVariable = class(TLuaVariable)
public
constructor Create(AState: lua_State; AIndex: Integer);
end;
TLuaCachedTable = class(TLuaTable)
public
constructor Create(AState: lua_State; AIndex: Integer);
end;
TLuaStackWriteParameters = class(TInterfacedObject, ILuaWriteParameters)
9 years ago
private
FState: lua_State;
FCount: Integer;
protected
procedure Pushed;
property State: lua_State read FState;
public
constructor Create(AState: lua_State);
{ ILuaWriteParameters }
9 years ago
function GetCount: Integer;
procedure Push(ABoolean: Boolean); overload;
procedure Push(AInteger: Integer); overload;
procedure Push(ANumber: Double); overload;
procedure Push(AUserData: Pointer); overload;
procedure Push(const AString: string); overload;
procedure Push(ATable: ILuaTable); overload;
9 years ago
end;
TLuaFunction = class(TInterfacedObject, ILuaFunction)
private
FState: lua_State;
FRegistryKey: string;
protected
property State: lua_State read FState;
property RegistryKey: string read FRegistryKey;
public
constructor Create(AState: lua_State; AIndex: Integer);
destructor Destroy; override;
{ ILuaFunction }
function Call(): ILuaReadParameters; overload;
function Call(AParameters: array of const): ILuaReadParameters; overload;
function Call(AParameters: ILuaReadParameters): ILuaReadParameters; overload;
end;
9 years ago
TLuaContext = class(TInterfacedObject, ILuaContext)
private
FParameters: ILuaReadParameters;
FResult: ILuaWriteParameters;
9 years ago
public
constructor Create(AState: lua_State);
{ ILuaContext }
function GetParameters: ILuaReadParameters;
function GetResult: ILuaWriteParameters;
end;
{ TLuaHelpers }
class function TLuaHelpers.GetLuaDataType(AType: Integer): TLuaDataType;
begin
case AType of
LUA_TNIL: Result := LuaNil;
LUA_TNUMBER: Result := LuaNumber;
LUA_TBOOLEAN: Result := LuaBoolean;
LUA_TSTRING: Result := LuaString;
LUA_TTABLE: Result := LuaTable;
LUA_TFUNCTION: Result := LuaFunction;
LUA_TUSERDATA: Result := LuaUserData;
LUA_TTHREAD: Result := LuaThread;
LUA_TLIGHTUSERDATA: Result := LuaLightUserData;
else
Result := LuaNone;
end;
end;
class function TLuaHelpers.GetLuaVariableType(ADataType: TLuaDataType): TLuaVariableType;
begin
case ADataType of
LuaNumber: Result := VariableNumber;
LuaBoolean: Result := VariableBoolean;
LuaString: Result := VariableString;
LuaTable: Result := VariableTable;
LuaFunction: Result := VariableFunction;
LuaUserData: Result := VariableUserData;
LuaLightUserData: Result := VariableUserData;
LuaCClosure: Result := VariableCFunction;
else
Result := VariableNone;
end;
end;
class function TLuaHelpers.CreateParameters(AParameters: array of const): ILuaReadParameters;
var
parameterIndex: Integer;
parameter: TVarRec;
resultParameters: TLuaParameters;
table: ILuaTable;
begin
resultParameters := TLuaParameters.Create;
for parameterIndex := Low(AParameters) to High(AParameters) do
begin
parameter := AParameters[parameterIndex];
case parameter.VType of
vtInteger: resultParameters.Push(parameter.VInteger);
vtBoolean: resultParameters.Push(parameter.VBoolean);
vtChar: resultParameters.Push(string(parameter.VChar));
vtExtended: resultParameters.Push(parameter.VExtended^);
vtString: resultParameters.Push(string(parameter.VString));
vtPointer: resultParameters.Push(parameter.VPointer);
vtPChar: resultParameters.Push(string(parameter.VPChar));
vtObject:
if parameter.VObject is TLuaTable then
resultParameters.Push(TLuaTable(parameter.VObject));
vtWideChar: resultParameters.Push(string(parameter.VWideChar));
vtPWideChar: resultParameters.Push(string(parameter.VPWideChar));
vtAnsiString: resultParameters.Push(string(PAnsiChar(parameter.VAnsiString)));
vtCurrency: resultParameters.Push(parameter.VCurrency^);
// vtVariant: resultParameters.Push(parameter.VVariant);
vtInterface:
if Supports(IInterface(parameter.VInterface), ILuaTable, table) then
resultParameters.Push(table);
vtWideString: resultParameters.Push(string(PWideString(parameter.VWideString)));
vtInt64: resultParameters.Push(parameter.VInt64^);
vtUnicodeString: resultParameters.Push(string(PUnicodeString(parameter.VUnicodeString)));
else
raise ELuaUnsupportedParameterException.CreateFmt('Parameter type %d not supported (index: %d)', [parameter.VType, parameterIndex]);
end;
9 years ago
end;
Result := resultParameters;
end;
9 years ago
class procedure TLuaHelpers.PushVariable(AState: lua_State; AVariable: ILuaVariable);
begin
PushVariable(AState, AVariable, AVariable.VariableType);
end;
class procedure TLuaHelpers.PushVariable(AState: lua_State; AVariable: ILuaVariable; AVariableType: TLuaVariableType);
begin
case AVariableType of
VariableNone: lua_pushnil(AState);
VariableBoolean: lua_pushboolean(AState, IfThen(AVariable.AsBoolean, 1, 0));
VariableInteger: lua_pushinteger(AState, AVariable.AsInteger);
VariableNumber: lua_pushnumber(AState, AVariable.AsNumber);
VariableUserData:
if AVariable.AsUserData = nil then
lua_pushnil(AState)
else
lua_pushlightuserdata(AState, AVariable.AsUserData);
VariableString: PushString(AState, AVariable.AsString);
VariableTable: PushTable(AState, AVariable.AsTable);
VariableCFunction: PushCFunction(AState, AVariable.AsCFunction);
else
raise ELuaUnsupportedVariableException.CreateFmt('Variable type not supported: %d', [Ord(AVariableType)]);
end;
end;
class procedure TLuaHelpers.PushString(AState: lua_State; const AValue: string);
var
stringValue: PAnsiChar;
begin
stringValue := AllocLuaString(AValue);
try
lua_pushlstring(AState, stringValue, Length(AValue));
finally
FreeLuaString(stringValue);
end;
end;
class procedure TLuaHelpers.PushTable(AState: lua_State; ATable: ILuaTable);
var
pair: TLuaKeyValuePair;
begin
lua_newtable(AState);
for pair in ATable do
begin
PushVariable(AState, pair.Key);
PushVariable(AState, pair.Value);
lua_settable(AState, -3);
end;
end;
function LuaWrapperClosure(L: lua_State): Integer; cdecl;
var
method: TMethod;
closure: TLuaCFunction absolute method;
context: ILuaContext;
begin
method.Code := lua_touserdata(L, lua_upvalueindex(1));
method.Data := lua_touserdata(L, lua_upvalueindex(2));
context := TLuaContext.Create(L);
try
closure(context);
Result := context.Result.Count;
except
on E:Exception do
Result := luaL_error(L, PAnsiChar(AnsiString(E.Message)), nil);
end;
end;
class procedure TLuaHelpers.PushCFunction(AState: lua_State; AClosure: TLuaCFunction);
var
method: TMethod absolute AClosure;
begin
{ Assume the reference to AClosure is being kept alive by the caller for as long
as it's needed, for example by the ILuaVariable that contains it. That way we
don't need to keep our own reference, as we can't determine when it's ok to go out
of scope, which would result in memory increase on every use. }
lua_pushlightuserdata(AState, method.Code);
lua_pushlightuserdata(AState, method.Data);
lua_pushcclosure(AState, @LuaWrapperClosure, 2);
end;
// Casting strings directly to PAnsiChar (via AnsiString) causes corruption
// with table values, at least in Delphi XE2. Can't really explain why, seems
// the input string goes out of scope, so let's just go through the motions
// to create a copy and be safe.
class function TLuaHelpers.AllocLuaString(const AValue: string): PAnsiChar;
begin
if Length(AValue) > 0 then
begin
GetMem(Result, Length(AValue) + 1);
StrPCopy(Result, AnsiString(AValue));
end else
Result := nil;
end;
class procedure TLuaHelpers.FreeLuaString(AValue: PAnsiChar);
begin
FreeMem(AValue);
end;
// If someone cares to reproduce this issue and optimize the code, use these
// two and the TableLuaFunction test should fail with a corrupted value
// (#11#0#0#0#11#0#0#0#11#0#0#0#11#0#0#0#11).
(*
class function TLuaHelpers.AllocLuaString(const AValue: string): PAnsiChar;
begin
if Length(AValue) > 0 then
Result := PAnsiChar(AnsiString(AValue))
else
Result := nil;
end;
class procedure TLuaHelpers.FreeLuaString(AValue: PAnsiChar);
begin
end;
*)
class procedure TLuaHelpers.RaiseLastLuaError(AState: lua_State);
var
errorMessage: string;
begin
errorMessage := LuaToString(AState, -1);
lua_pop(AState, 1);
raise ELuaException.Create(errorMessage);
end;
class function TLuaHelpers.LuaToString(AState: lua_State; AIndex: Integer): string;
var
len: NativeUint;
value: PAnsiChar;
stringValue: RawByteString;
begin
value := lua_tolstring(AState, AIndex, @len);
SetString(stringValue, value, len);
Result := string(stringValue);
end;
class function TLuaHelpers.CallFunction(AState: lua_State; AParameters: ILuaReadParameters): ILuaReadParameters;
var
stackIndex: Integer;
parameterCount: Integer;
parameter: ILuaVariable;
begin
{ Assumption: the function to call is the top item on the stack }
stackIndex := Pred(lua_gettop(AState));
parameterCount := 0;
if Assigned(AParameters) then
begin
parameterCount := AParameters.Count;
for parameter in AParameters do
PushVariable(AState, parameter);
end;
if lua_pcall(AState, parameterCount, LUA_MULTRET, 0) <> 0 then
RaiseLastLuaError(AState);
Result := TLuaResultParameters.Create(AState, lua_gettop(AState) - stackIndex);
end;
class function TLuaHelpers.NewRegistryKey: string;
begin
// This could be incremented on a per-State basis, but this'll do for now.
Result := Format('DelphiLuaWrapper_%d', [TInterlocked.Increment(RegistryKeyCounter)]);
end;
{ TLuaImplicitVariable }
class operator TLuaImplicitVariable.Implicit(AValue: ILuaVariable): TLuaImplicitVariable;
begin
Result.Variable := AValue;
end;
class operator TLuaImplicitVariable.Implicit(AValue: Boolean): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(AValue: Integer): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(AValue: Double): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(AValue: Pointer): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(const AValue: string): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(AValue: ILuaTable): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(AValue: TLuaCFunction): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;