-
Notifications
You must be signed in to change notification settings - Fork 5
/
Test.pas
161 lines (142 loc) · 3.61 KB
/
Test.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
program Test;
{$mode objfpc}
{$H+}
uses
SysUtils, ScriptEngine;
const
HelloWorld = 'writeln(''Hello, World!'')';
IfTest = 'i = 5.1 if i = 5.1 writeln(''True'') else writeln(''Something is wrong!'')';
StringTest = 's = ''This is a string!'' writeln(s)';
PerformanceTest = 'i = 0 while i < 999 { i = i + 1 j = 0 while j < 999 { j = j + 1 k = i * j } }';
ArrayTest = 'a = [] i = 0 while i < 2 { a[i] = 1 + i * 2 i = i + 1 } a[2] = ''text'' writeln(a[0], '' '', a[1], '' '', a[2])';
CustomFunctionTest = 'writeln(hello(''Satania''))';
CustomFunctionWithSelfTest = 'a = [ func: add, value: 1 ] writeln(a.func(3).value)';
YieldTest = 'i = 0 while i < 3 { i = i + 1 yield }';
FibTest = 'fn fib(n) { if n < 2 result = n else result = fib(n-1) + fib(n-2) } writeln(fib(36))';
AssertTest = 'assert(false, "Assert triggered")';
ResultTest = 'result = 5';
type
TCustomFunctions = class
public
class function Hello(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function Add(const VM: TSEVM; const Args: array of TSEValue; const This: TSEValue): TSEValue;
end;
class function TCustomFunctions.Hello(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
begin
Exit('Hello, ' + Args[0]);
end;
class function TCustomFunctions.Add(const VM: TSEVM; const Args: array of TSEValue; const This: TSEValue): TSEValue;
var
V: TSEValue;
begin
V := SEMapGet(This, 'value'); // Get data from "value" property
V := V + Args[0];
SEMapSet(This, 'value', V); // Set new data to "value" property
Exit(This);
end;
var
SE: TScriptEngine;
procedure HelloWorldRun;
begin
Writeln('--- HelloWorldRun ---');
SE.Source := HelloWorld;
SE.Exec;
end;
procedure IfTestRun;
begin
Writeln('--- IfTestRun ---');
SE.Source := IfTest;
SE.Exec;
end;
procedure StringTestRun;
begin
Writeln('--- StringTestRun ---');
SE.Source := StringTest;
SE.Exec;
end;
procedure PerformanceTestRun;
var
S: Integer;
begin
Writeln('--- PerformanceTestRun ---');
SE.Source := PerformanceTest;
S := GetTickCount;
SE.Exec;
Writeln(GetTickCount - S, 'ms');
end;
procedure ArrayTestRun;
begin
Writeln('--- ArrayTestRun ---');
SE.Source := ArrayTest;
SE.Exec;
end;
procedure CustomFunctionTestRun;
begin
Writeln('--- CustomFunctionTestRun ---');
SE.RegisterFunc('hello', @TCustomFunctions(nil).Hello, 1);
SE.Source := CustomFunctionTest;
SE.Exec;
end;
procedure CustomFunctionWithSelfTestRun;
begin
Writeln('--- CustomFunctionWithSelfTestRun ---');
SE.RegisterFuncWithSelf('add', @TCustomFunctions(nil).Add, 1);
SE.Source := CustomFunctionWithSelfTest;
SE.Exec;
end;
procedure YieldTestRun;
begin
Writeln('--- YieldTestRun ---');
SE.Source := YieldTest;
repeat
SE.Exec;
if SE.IsYielded then
Writeln('Yield!');
until SE.IsDone;
end;
procedure FibTestRun;
var
S: Integer;
begin
Writeln('--- FibTestRun ---');
SE.Source := FibTest;
S := GetTickCount;
SE.Exec;
Writeln(GetTickCount - S, 'ms');
end;
procedure AssertTestRun;
begin
try
Writeln('--- AssertTestRun ---');
SE.OptimizeAsserts := False;
SE.Source := AssertTest;
SE.Exec;
except
on E: Exception do
begin
Writeln(E.Message);
end;
end;
end;
procedure ResultTestRun;
begin
Writeln('--- ResultTestRun ---');
SE.Source := ResultTest;
SE.Exec;
Writeln(SE.VM.Global[0].VarNumber); // The first global variable is always "result"
end;
begin
SE := TScriptEngine.Create;
HelloWorldRun;
IfTestRun;
StringTestRun;
PerformanceTestRun;
ArrayTestRun;
CustomFunctionTestRun;
CustomFunctionWithSelfTestRun;
YieldTestRun;
FibTestRun;
AssertTestRun;
ResultTestRun;
SE.Free;
end.