-
Notifications
You must be signed in to change notification settings - Fork 3
/
psrt.pas
99 lines (80 loc) · 2.25 KB
/
psrt.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
unit psrt;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, uPSRuntime, uPSUtils;
type
{ TPSRuntime }
TPSRuntime = class(TComponent)
private
FExec: TPSExec;
function GetExecErrorString: tbtstring;
public
property ExecErrorString: tbtstring read GetExecErrorString;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function LoadFromFile(const AFilename: string): boolean;
function LoadFromStream(s: TStream): boolean;
function RunProgram: boolean;
function ExecFunction(const Params: array of Variant; const ProcName: tbtstring): Variant;
procedure RegisterDelphiFunction(ProcPtr: Pointer; const ProcName: tbtstring; CC: TPSCallingConvention);
procedure RegisterDelphiMethod(Slf, ProcPtr: Pointer; const ProcName: tbtstring; CC: TPSCallingConvention);
end;
implementation
{ TPSRuntime }
function TPSRuntime.GetExecErrorString: tbtstring;
begin
Result:=TIFErrorToString(FExec.ExceptionCode, FExec.ExceptionString);
end;
constructor TPSRuntime.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FExec:=TPSExec.Create;
end;
destructor TPSRuntime.Destroy;
begin
FExec.Free;
inherited Destroy;
end;
function TPSRuntime.LoadFromFile(const AFilename: string): boolean;
var
bc: string;
f: TFileStream;
begin
Result:=False;
f:=TFileStream.Create(AFilename, fmOpenRead);
try
SetLength(bc, f.Size);
f.Read(bc[1], f.Size);
if FExec.LoadData(bc) then
Result:=True;
finally
f.Free;
end;
end;
function TPSRuntime.LoadFromStream(s: TStream): boolean;
begin
Result:=False;
if FExec.LoadData(s.ReadAnsiString) then
Result:=True;
end;
function TPSRuntime.RunProgram: boolean;
begin
Result:=FExec.RunScript;
end;
function TPSRuntime.ExecFunction(const Params: array of Variant;
const ProcName: tbtstring): Variant;
begin
Result:=FExec.RunProcPN(Params, ProcName);
end;
procedure TPSRuntime.RegisterDelphiFunction(ProcPtr: Pointer;
const ProcName: tbtstring; CC: TPSCallingConvention);
begin
FExec.RegisterDelphiFunction(ProcPtr, ProcName, CC);
end;
procedure TPSRuntime.RegisterDelphiMethod(Slf, ProcPtr: Pointer;
const ProcName: tbtstring; CC: TPSCallingConvention);
begin
FExec.RegisterDelphiMethod(Slf, ProcPtr, ProcName, CC);
end;
end.