From aa794d7eb11d94450b9bb6f1870c603cc42ede58 Mon Sep 17 00:00:00 2001 From: Artem3213212 Date: Fri, 22 Jun 2018 21:56:56 +0300 Subject: [PATCH] Update Lua Support --- Source/RuntimeBuilder.Lua.pas | 165 +++++++++++++--------------------- 1 file changed, 62 insertions(+), 103 deletions(-) diff --git a/Source/RuntimeBuilder.Lua.pas b/Source/RuntimeBuilder.Lua.pas index 6898552..c4b9b8d 100644 --- a/Source/RuntimeBuilder.Lua.pas +++ b/Source/RuntimeBuilder.Lua.pas @@ -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;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;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 - i:NativeUInt; - p0:PByte; +class procedure TRTBLuaCompiler.TRTBLuaSource.AddToStack(State:Plua_State;Data:TValue); +var + i:NativeUInt; + rtype:TRTTIType; +begin +with Data do + if IsType or IsType or IsType then + lua_pushstring(State,PAnsiChar(AsType)) + else if IsType then + lua_pushstring(State,PAnsiChar(AsType)) + else if IsType then + lua_pushstring(State,PAnsiChar(AsType)) + else if IsType then + lua_pushstring(State,PAnsiChar(AsType)) + else if IsOrdinal then + lua_pushinteger(State,Data.AsOrdinal) + else if IsType then + lua_pushnumber(State,AsType) + else if IsType then + lua_pushnumber(State,AsType) + else if IsType then + lua_pushnumber(State,AsExtended) + else if IsType then + lua_pushnumber(State,AsExtended) + else if IsType then + lua_pushnumber(State,AsType) + else if IsType then + lua_pushnumber(State,AsType) + else if IsType then + lua_pushnumber(State,AsType) + else if Kind=tkPointer then + lua_pushlightuserdata(State,AsType) + else if IsArray then begin - if Value.IsType or Value.IsType or Value.IsType then - begin - PreDecl:=PreDecl+Key+' db '+#39+Value.AsType+#39+',0'+sLineBreak; - inc(base,length(Value.AsType)); - end - else if Value.IsType then - begin - PreDecl:=PreDecl+Key+' db '+#39+Value.AsType+#39+',0'+sLineBreak; - inc(base,length(Value.AsType)); - end - else if Value.IsType then - begin - PreDecl:=PreDecl+Key+' db '; - p0:=Pointer(PWideChar(Value.AsType)); - for i:={$IFDEF NEXTGEN}0{$ELSE}1{$ENDIF} to length(Value.AsType){$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)*2); - end - else if Value.IsType then - begin - PreDecl:=PreDecl+Key+' db '; - p0:=Pointer(PWideChar(Value.AsType)); - for i:={$IFDEF NEXTGEN}0{$ELSE}1{$ENDIF} to length(Value.AsType){$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)*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 then - PreDecl:=PreDecl+Key+' dd '+FloatToStrF(Value.AsType,ffFixed,1000,1000)+sLineBreak - else if Value.IsType then - PreDecl:=PreDecl+Key+' dq '+FloatToStrF(Value.AsType,ffFixed,1000,1000)+sLineBreak - else if Value.IsType then - PreDecl:=PreDecl+Key+{$IF 8=SizeOf(Extended)}' dq '{$ELSEIF 10=SizeOf(Extended)}' dt '{$ELSE}' ddq '{$IFEND}+FloatToStrF(Value.AsType,ffFixed,1000,1000)+sLineBreak - else if Value.IsType then - PreDecl:=PreDecl+Key+' dq '+FloatToStrF(Value.AsType,ffFixed,1000,1000)+sLineBreak - else if Value.IsType then - PreDecl:=PreDecl+Key+' df '+FloatToStrF(Value.AsType,ffFixed,1000,1000)+sLineBreak - else if Value.IsType then - PreDecl:=PreDecl+Key+' dq '+FloatToStrF(Value.AsType,ffFixed,1000,1000)+sLineBreak - else if Value.IsType then - PreDecl:=PreDecl+Key+' dq '+FloatToStrF(Value.AsType,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).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); - end; - end - {else if Value.Kind=tkRecord then - begin - PreDecl:=PreDecl+Key+':'+sLineBreak; - for i:=1 to Value.TypeData.ManagedFldCount do + lua_createtable(); + for i:=0 to Data.GetArrayLength-1 do + begin + lua_pushinteger(i); + AddToStack(State,Data.GetArrayElement(i)); + lua_settable(State,-3); + end; + end + else if Data.Kind=tkRecord then + begin + 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;