Update Lua Support

This commit is contained in:
2018-06-22 21:56:56 +03:00
parent 35cb759f40
commit aa794d7eb1

View File

@@ -15,9 +15,10 @@ type
protected type
TRTBLuaFunc=class(TRTBFunc)
protected
State:Plua_State;
Name:string;
public
constructor Create(s:string);
constructor Create(Module:TRTBLuaModule;s:string);
function Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;override;
destructor Destroy;override;
end;
@@ -77,6 +78,8 @@ type
//procedure RegisterFunction(NameSpace:string;Name:string);override;
//procedure UnRegisterFunction(NameSpace:string;Name:string);override;
class procedure AddToStack(State:Plua_State;Data:TValue);static;
function Compilate:TRTBModule;override;
destructor Destroy;override;
end;
@@ -366,17 +369,18 @@ begin
FreeLibrary(Lib);
end;*)
constructor TRTBLuaCompiler.TRTBLuaSource.TRTBLuaModule.TRTBLuaFunc.Create(s:string);
constructor TRTBLuaCompiler.TRTBLuaSource.TRTBLuaModule.TRTBLuaFunc.Create(Module:TRTBLuaModule;s:string);
begin
Name:=s;
Module:=Module
end;
function TRTBLuaCompiler.TRTBLuaSource.TRTBLuaModule.TRTBLuaFunc.Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;
var
i:TValue;
begin
//for i in args do
// TRTBLuaCompiler.TRTBLuaSource.AddToStack();
for i in args do
TRTBLuaCompiler.TRTBLuaSource.AddToStack(State,i);
end;
destructor TRTBLuaCompiler.TRTBLuaSource.TRTBLuaModule.TRTBLuaFunc.Destroy;
@@ -385,10 +389,7 @@ end;
function TRTBLuaCompiler.TRTBLuaSource.TRTBLuaModule.GetFuntion(Name:string):TRTBFunc;
begin
//lua
//lua_getglobal();
//lua_call();
//Result:=TRTBLuaFunc.Create();
Result:=TRTBLuaFunc.Create(self,Name);
end;
constructor TRTBLuaCompiler.TRTBLuaSource.TRTBLuaModule.Create(AState:Plua_State);
@@ -432,104 +433,62 @@ with libs do
Delete(IndexOf(Name));
end;
function TRTBLuaCompiler.TRTBLuaSource.Compilate:TRTBModule;
procedure VarParse(key:string;value:TValue;var PreDecl:string;var base:NativeUInt);
var
class procedure TRTBLuaCompiler.TRTBLuaSource.AddToStack(State:Plua_State;Data:TValue);
var
i:NativeUInt;
p0:PByte;
rtype:TRTTIType;
begin
with Data do
if IsType<AnsiString> or IsType<RawByteString> or IsType<UTF8String> then
lua_pushstring(State,PAnsiChar(AsType<AnsiString>))
else if IsType<ShortString> then
lua_pushstring(State,PAnsiChar(AsType<ShortString>))
else if IsType<WideString> then
lua_pushstring(State,PAnsiChar(AsType<WideString>))
else if IsType<UCS4String> then
lua_pushstring(State,PAnsiChar(AsType<UCS4String>))
else if IsOrdinal then
lua_pushinteger(State,Data.AsOrdinal)
else if IsType<Single> then
lua_pushnumber(State,AsType<Single>)
else if IsType<Double> then
lua_pushnumber(State,AsType<Double>)
else if IsType<Extended> then
lua_pushnumber(State,AsExtended)
else if IsType<Real> then
lua_pushnumber(State,AsExtended)
else if IsType<Real48> then
lua_pushnumber(State,AsType<Real48>)
else if IsType<Comp> then
lua_pushnumber(State,AsType<Comp>)
else if IsType<Currency> then
lua_pushnumber(State,AsType<Currency>)
else if Kind=tkPointer then
lua_pushlightuserdata(State,AsType<Pointer>)
else if IsArray then
begin
if Value.IsType<AnsiString> or Value.IsType<RawByteString> or Value.IsType<UTF8String> then
lua_createtable();
for i:=0 to Data.GetArrayLength-1 do
begin
PreDecl:=PreDecl+Key+' db '+#39+Value.AsType<AnsiString>+#39+',0'+sLineBreak;
inc(base,length(Value.AsType<AnsiString>));
end
else if Value.IsType<ShortString> then
begin
PreDecl:=PreDecl+Key+' db '+#39+Value.AsType<ShortString>+#39+',0'+sLineBreak;
inc(base,length(Value.AsType<ShortString>));
end
else if Value.IsType<WideString> then
begin
PreDecl:=PreDecl+Key+' db ';
p0:=Pointer(PWideChar(Value.AsType<WideString>));
for i:={$IFDEF NEXTGEN}0{$ELSE}1{$ENDIF} to length(Value.AsType<WideString>){$IFDEF NEXTGEN}-1{$ENDIF} do
begin
PreDecl:=PreDecl+inttostr(p0^)+',';
inc(p0);
PreDecl:=PreDecl+inttostr(p0^)+',';
inc(p0);
end;
PreDecl:=PreDecl+'0,0'+sLineBreak;
inc(base,length(Value.AsType<WideString>)*2);
end
else if Value.IsType<UCS4String> then
begin
PreDecl:=PreDecl+Key+' db ';
p0:=Pointer(PWideChar(Value.AsType<UCS4String>));
for i:={$IFDEF NEXTGEN}0{$ELSE}1{$ENDIF} to length(Value.AsType<UCS4String>){$IFDEF NEXTGEN}-1{$ENDIF} do
begin
PreDecl:=PreDecl+inttostr(p0^)+',';
inc(p0);
PreDecl:=PreDecl+inttostr(p0^)+',';
inc(p0);
PreDecl:=PreDecl+inttostr(p0^)+',';
inc(p0);
PreDecl:=PreDecl+inttostr(p0^)+',';
inc(p0);
end;
PreDecl:=PreDecl+'0,0,0,0'+sLineBreak;
inc(base,length(Value.AsType<UCS4String>)*4);
end
else if Value.IsOrdinal then
case Value.DataSize of
1:PreDecl:=PreDecl+Key+' db '+Value.AsOrdinal.ToString+sLineBreak;
2:PreDecl:=PreDecl+Key+' dw '+Value.AsOrdinal.ToString+sLineBreak;
4:PreDecl:=PreDecl+Key+' dd '+Value.AsOrdinal.ToString+sLineBreak;
8:PreDecl:=PreDecl+Key+' dq '+Value.AsOrdinal.ToString+sLineBreak;
end
else if Value.Kind=tkFloat then
begin
if Value.IsType<Single> then
PreDecl:=PreDecl+Key+' dd '+FloatToStrF(Value.AsType<Single>,ffFixed,1000,1000)+sLineBreak
else if Value.IsType<Double> then
PreDecl:=PreDecl+Key+' dq '+FloatToStrF(Value.AsType<Double>,ffFixed,1000,1000)+sLineBreak
else if Value.IsType<Extended> then
PreDecl:=PreDecl+Key+{$IF 8=SizeOf(Extended)}' dq '{$ELSEIF 10=SizeOf(Extended)}' dt '{$ELSE}' ddq '{$IFEND}+FloatToStrF(Value.AsType<Extended>,ffFixed,1000,1000)+sLineBreak
else if Value.IsType<Real> then
PreDecl:=PreDecl+Key+' dq '+FloatToStrF(Value.AsType<Real>,ffFixed,1000,1000)+sLineBreak
else if Value.IsType<Real48> then
PreDecl:=PreDecl+Key+' df '+FloatToStrF(Value.AsType<Real48>,ffFixed,1000,1000)+sLineBreak
else if Value.IsType<Comp> then
PreDecl:=PreDecl+Key+' dq '+FloatToStrF(Value.AsType<Comp>,ffFixed,1000,1000)+sLineBreak
else if Value.IsType<Currency> then
PreDecl:=PreDecl+Key+' dq '+FloatToStrF(Value.AsType<Currency>,ffFixed,1000,1000)+sLineBreak
else
PreDecl:=PreDecl+Key+' dd 0.0'+sLineBreak;
end
else if Value.Kind=tkPointer then
PreDecl:=PreDecl+Key+' equ '+NativeUInt(Value.AsType<Pointer>).ToString+sLineBreak
else if Value.IsArray then
begin
if Value.GetArrayLength=0 then
begin
PreDecl:=PreDecl+Key+' equ 0'+sLineBreak;
end
else
begin
PreDecl:=PreDecl+Key+':'+sLineBreak;
for i:=0 to Value.GetArrayLength-1 do
VarParse(key+'.i'+i.ToString,Value.GetArrayElement(i),PreDecl,Base);
lua_pushinteger(i);
AddToStack(State,Data.GetArrayElement(i));
lua_settable(State,-3);
end;
end
{else if Value.Kind=tkRecord then
else if Data.Kind=tkRecord then
begin
PreDecl:=PreDecl+Key+':'+sLineBreak;
for i:=1 to Value.TypeData.ManagedFldCount do
rtype:=TRTTIContext.Create.GetType(Data.TypeInfo);
end}
else //if Value.Kind=tkUnknown then
PreDecl:=PreDecl+Key+': times '+ Value.DataSize.ToString+' db 0'+sLineBreak;
end
//else //if Value.Kind=tkUnknown then
//PreDecl:=PreDecl+Key+': times '+ Value.DataSize.ToString+' db 0'+sLineBreak;
else
begin
//lua_pushlightuserdata(State,);
end;
end;
function TRTBLuaCompiler.TRTBLuaSource.Compilate:TRTBModule;
procedure ConstParse(key:string;value:TValue;var PreDecl:string;var base:NativeUInt);
var
i:NativeUInt;