API Remake

This commit is contained in:
2018-04-01 01:23:44 +03:00
parent d4eb19affa
commit 08d7761837
10 changed files with 269 additions and 47 deletions

View File

@@ -2,6 +2,8 @@ unit RuntimeBuilder.Fasm;
interface
//{$INLINE auto}
uses
System.Classes,System.TypInfo,System.IOUtils,System.Rtti,Winapi.Windows,
RuntimeBuilder,FasmOnDelphi;
@@ -35,23 +37,23 @@ type
constructor Create(Name:string);
destructor Destroy;override;
end;
protected
libs:TStringList;
function GetIncLibs():string;
protected
FText:string;
function GetText:string;override;
procedure SetText(S:string);override;
public
function LoadLib(Name:string):TRTBLib;override;
constructor Create(Compiler:TRTBFasmCompiler);
function CompilateAsFunc:TRTBFunc;override;
function CompilateAsLib:TRTBLib;override;
end;
protected
function GetIncLibs():string;
public
CompilerMem:NativeUInt;
MaxSteps:word;
libs:TStringList;
constructor Create(FasmPath:String=FasmPath;AsDll:boolean=false);
function LoadLib(Name:string):TRTBLib;
function GenNewSrc():TRTBSource;override;
end;
@@ -68,9 +70,6 @@ FreeMem(p);
end;
function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmFunc.Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;
{$IFDEF CPUX64}
//function()
{$ENDIF}
begin
{$IFDEF CPUX64}
Result:=Invoke(p,args,ccReg,OutType);
@@ -127,10 +126,25 @@ begin
FText:=S;
end;
function TRTBFasmCompiler.TRTBFasmSource.GetIncLibs():string;
var
i:integer;
begin
Result:='';
for i:=0 to libs.count-1 do
Result:=Result+sLineBreak+'include '+#39+libs.Strings[i]+#39;
end;
constructor TRTBFasmCompiler.TRTBFasmSource.Create(Compiler:TRTBFasmCompiler);
begin
inherited Create(Compiler);
FText:='';
libs:=TStringList.Create;
end;
function TRTBFasmCompiler.TRTBFasmSource.LoadLib(Name:string):TRTBLib;
begin
libs.Add(Name);
end;
function TRTBFasmCompiler.TRTBFasmSource.CompilateAsFunc:TRTBFunc;
@@ -162,21 +176,6 @@ begin
CompilerMem:=1024*1024*16;
MaxSteps:=65535;
OpenFASM(FasmPath,AsDll);
libs:=TStringList.Create;
end;
function TRTBFasmCompiler.LoadLib(Name:string):TRTBLib;
begin
libs.Add(Name);
end;
function TRTBFasmCompiler.GetIncLibs():string;
var
i:integer;
begin
Result:='';
for i:=0 to libs.count-1 do
Result:=Result+sLineBreak+'include '+#39+libs.Strings[i]+#39;
end;
function TRTBFasmCompiler.GenNewSrc():TRTBSource;

View File

@@ -0,0 +1,166 @@
unit RuntimeBuilder.Lua;
interface
uses
System.Classes,System.TypInfo,System.IOUtils,System.Rtti,Winapi.Windows,
pLuaObject,pLuaRecord,pLuaTable,uWordList,Lua,LuaObject,LuaWrapper,pLua,RuntimeBuilder;
type
TRTBLuaCompiler=class(TRTBCompiler)
protected type
TRTBLuaSource=class(TRTBSource)
protected type
{TRTBLuaFunc=class(TRTBFunc)
protected
p:Pointer;
sb:NativeUInt;
public
constructor Create(p:Pointer;sb:NativeUInt);
function Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;override;
destructor Destroy;override;
end;
TRTBLuaLib=class(TRTBLib)
private type
TRTBLuaLibFunc=class(TRTBLuaFunc)
public
constructor Create(p:Pointer);
destructor Destroy;override;
end;
private
filename:string;
Lib:NativeUInt;
function GetFuntion(Name:string):TRTBFunc;override;
public
constructor Create(Name:string);
destructor Destroy;override;
end;}
protected
FText:string;
function GetText:string;override;
procedure SetText(S:string);override;
public
constructor Create(Compiler:TRTBLuaCompiler);
function CompilateAsFunc:TRTBFunc;override;
//function CompilateAsLib:TRTBLib;override;
end;
protected
Lua:Plua_state;
//function GetIncLibs():string;
public
libs:TStringList;
constructor Create();
//function LoadLib(Name:string):TRTBLib;
function GenNewSrc():TRTBSource;override;
end;
implementation
uses System.SysUtils;
{constructor TRTBLuaCompiler.TRTBLuaSource.TRTBLuaFunc.Create(p:Pointer;sb:NativeUInt);
begin
Self.p:=VirtualAlloc(nil,sb,MEM_COMMIT ,PAGE_EXECUTE_READWRITE);
CopyMemory(Self.p,p,sb);
Self.sb:=sb;
FreeMem(p);
end;
function TRTBLuaCompiler.TRTBLuaSource.TRTBLuaFunc.Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;
begin
end;
destructor TRTBLuaCompiler.TRTBLuaSource.TRTBLuaFunc.Destroy;
begin
VirtualFree(p,sb,MEM_RELEASE);
p:=nil;
end;
constructor TRTBLuaCompiler.TRTBLuaSource.TRTBLuaLib.TRTBLuaLibFunc.Create(p:Pointer);
begin
Self.p:=p;
end;
destructor TRTBLuaCompiler.TRTBLuaSource.TRTBLuaLib.TRTBLuaLibFunc.Destroy;
begin
p:=nil;
end;
function TRTBLuaCompiler.TRTBLuaSource.TRTBLuaLib.GetFuntion(Name:string):TRTBFunc;
begin
Result:=TRTBLuaLibFunc.Create(GetProcAddress(Lib,pwidechar(Name)));
end;
constructor TRTBLuaCompiler.TRTBLuaSource.TRTBLuaLib.Create(Name:string);
begin
filename:=Name;
Lib:=LoadLibrary(pwidechar(Name));
end;
destructor TRTBLuaCompiler.TRTBLuaSource.TRTBLuaLib.Destroy;
begin
FreeLibrary(Lib);
end;}
function TRTBLuaCompiler.TRTBLuaSource.GetText:string;
begin
Result:=FText;
end;
procedure TRTBLuaCompiler.TRTBLuaSource.SetText(S:string);
begin
FText:=S;
end;
constructor TRTBLuaCompiler.TRTBLuaSource.Create(Compiler:TRTBLuaCompiler);
begin
inherited Create(Compiler);
FText:='';
end;
function TRTBLuaCompiler.TRTBLuaSource.CompilateAsFunc:TRTBFunc;
begin
//lua_(lua_tocfunction());
//(Compiler as TRTBLuaCompiler).Lua.RegisterFunction();
end;
(*function TRTBLuaCompiler.TRTBLuaSource.CompilateAsLib:TRTBLib;
var
templib:string;
Res:TLuaResult;
begin
templib:=TPath.GetTempFileName;
Res:=LuaAssembleToFile({$IFDEF CPUX64}'format PE64 DLL'{$ELSE}'format PE DLL'{$ENDIF}+
(Compiler as TRTBLuaCompiler).GetIncLibs+sLineBreak+Text,templib,
(Compiler as TRTBLuaCompiler).CompilerMem,(Compiler as TRTBLuaCompiler).MaxSteps);
if Res.Error<>Lua_OK then
raise Exception.Create(Res.OutStr);
Result:=TRTBLuaLib.Create(templib);
end;*)
{function TRTBLuaCompiler.GetIncLibs():string;
var
i:integer;
begin
Result:='';
for i:=0 to libs.count-1 do
Result:=Result+sLineBreak+'include '+#39+libs.Strings[i]+#39;
end;}
constructor TRTBLuaCompiler.Create();
begin
Lua:=luaL_newstate;
libs:=TStringList.Create;
end;
{function TRTBLuaCompiler.LoadLib(Name:string):TRTBLib;
begin
libs.Add(Name);
end;}
function TRTBLuaCompiler.GenNewSrc():TRTBSource;
begin
Result:=TRTBLuaSource.Create(Self);
end;
end.

View File

@@ -2,26 +2,38 @@ unit RuntimeBuilder;
interface
uses System.TypInfo,System.Rtti;
uses
System.TypInfo,System.Rtti;
const
CRTBCallTypeNil=$0;
CRTBCallTypeRegister=$1;
CRTBCallTypeStdCall=$2;
CRTBCallTypeCdecl=$3;
CRTBCallTypePascal=$4;
CRTBCallTypeSafeCall=$5;
CRTBCallType64Call=$40;
CRTBCallTypeDefault=$80;
CRTBCallTypeNil=ccReg;
CRTBCallTypeRegister=ccReg;
CRTBCallTypeStdCall=ccStdCall;
CRTBCallTypeCdecl=ccCdecl;
CRTBCallTypePascal=ccPascal;
CRTBCallTypeSafeCall=ccSafeCall;
CRTBCallType64Call=CRTBCallTypeNil;
CRTBCallTypeScript=CRTBCallTypeNil;
CRTBCallTypeDefault=CRTBCallTypeNil;
type
TRTBCompiler=class;
TRTBCallType=CRTBCallTypeNil..CRTBCallTypeDefault;
TRTBCallType=TCallConv;
TRTBType=PTypeInfo;
TRTBFunc=class abstract
public
function Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;virtual;abstract;
function Call(Name:string;OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;virtual;abstract;
end;
TRTBVar=class abstract
protected
function SetVal(TValue);virtual;abstract;
function GetVal:TValue;virtual;abstract;
public
property Val:TValue read GetVal write SetVal;
end;
TRTBLib=class abstract
@@ -29,6 +41,7 @@ type
function GetFuntion(Name:string):TRTBFunc;virtual;abstract;
public
property Funtion[Name:string]:TRTBFunc read GetFuntion;
property &Var[Name:string]:TRTBVar read GetFuntion;
end;
TRTBSource=class abstract
@@ -38,8 +51,29 @@ type
procedure SetText(S:string);virtual;abstract;
public
constructor Create(Compiler:TRTBCompiler);
function CompilateAsFunc:TRTBFunc;virtual;abstract;
function CompilateAsLib:TRTBLib;virtual;abstract;
procedure LoadLib(Name:string);virtual;abstract;
procedure UnLoadLib(Name:string);virtual;abstract;
procedure AddNameSpace(Name:string);virtual;abstract;
procedure DelNameSpace(Name:string);virtual;abstract;
procedure AddType(NameSpace:string;Name:string;&Type:TRTBType);virtual;abstract;
procedure ExportType(NameSpace:string;Name:string;&Type:TRTBType);virtual;abstract;
procedure DelType(NameSpace:string;Name:string);virtual;abstract;
procedure AddConst(NameSpace:string;Name:string;Val:TValue);virtual;abstract;
procedure ExportConst(NameSpace:string;Name:string;Val:TValue);virtual;abstract;
procedure DelConst(NameSpace:string;Name:string);virtual;abstract;
procedure AddVariable(NameSpace:string;Name:string;var Data);virtual;abstract;
procedure ExportVariable(NameSpace:string;Name:string;var Data);virtual;abstract;
procedure DelVariable(NameSpace:string;Name:string);virtual;abstract;
procedure &Register(NameSpace:string;Name:string;&Type:TRTBType);virtual;abstract;
procedure &RegisterFunction(NameSpace:string;Name:string);virtual;abstract;
function Compilate:TRTBLib;virtual;abstract;
procedure LoadFromFile(&File:string);
procedure SaveToFile(&File:string);
property Text:string read GetText write SetText;
@@ -47,7 +81,6 @@ type
TRTBCompiler=class abstract
public
function LoadLib(Name:string):TRTBLib;virtual;abstract;
function GenNewSrc():TRTBSource;virtual;abstract;
end;