forked from synopse/mORMot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.core.script.pas
228 lines (212 loc) · 6.37 KB
/
test.core.script.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/// regression tests for the framework Scripting abilities
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit test.core.script;
interface
{$I ..\src\mormot.defines.inc}
uses
sysutils,
mormot.core.base,
mormot.core.os,
mormot.core.text,
mormot.core.buffers,
mormot.core.unicode,
mormot.core.datetime,
mormot.core.rtti,
mormot.crypt.core,
mormot.core.data,
mormot.core.variants,
mormot.core.json,
mormot.core.log,
mormot.core.perf,
mormot.core.test,
mormot.lib.quickjs;
type
/// this test case will validate several low-level protocols
TTestCoreScript = class(TSynTestCase)
published
/// QuickJS low-level direct API tests
procedure QuickJSLowLevel;
end;
implementation
{ TTestCoreScript }
var
output: RawUtf8;
function dolog(ctx: JSContext; this_val: JSValueRaw; argc: integer;
argv: PJSValues): JSValueRaw; cdecl;
var
i: integer;
begin
for i := 0 to argc - 1 do
AddToCsv(ctx.ToUtf8(argv[i]), output);
result := JS_UNDEFINED;
end;
procedure TTestCoreScript.QuickJSLowLevel;
var
rt: JSRuntime;
cx: JSContext;
function Run(const js: RawUtf8): RawUtf8;
var
v: JSValue;
res: variant;
begin
Check(cx <> nil, 'cx');
v := cx.Eval(js, '', JS_EVAL_TYPE_GLOBAL, result);
if result = '' then
begin
Check(cx.ToVariant(v, res), 'tovar');
cx.ToUtf8(v, result);
if v.IsString or
v.IsInt32 then
CheckEqual(VariantToUtf8(res), result, 'istring/isint32')
else if v.IsFloat then
CheckSame(VariantToDoubleDef(res), GetExtended(pointer(result)),
DOUBLE_SAME, 'isfloat');
end;
cx.Free(v);
//writeln(js,' = ',result);
end;
var
v, v2: RawUtf8;
j: JSValue;
i: integer;
d: double;
i64: Int64;
va: variant;
begin
rt := JS_NewRuntime;
try
// validate numbers/text to/from JSValue conversion
cx := rt.New;
try
cx.SetFunction([], 'log', @dolog, 1);
for i := -100 to 100 do
begin
// 32-bit integer
j.From32(i * 777);
Check(j.IsNumber);
Check(not j.IsNull);
Check(not j.IsRefCounted);
Check(not j.IsFloat);
Check(not j.IsString);
Check(not j.IsNan);
Check(j.IsInt32);
CheckEqual(j.Int32, i * 777);
Check(cx.ToVariantFree(j, va));
Check(va = i * 777);
cx.Free(j); // do-nothing
// float
d := i * 777777777.77;
j.FromNum(d);
Check(j.IsNumber);
Check(not j.IsRefCounted);
Check(not j.IsString);
Check(not j.IsNan);
Check(j.IsFloat = (i <> 0));
if i = 0 then
CheckEqual(j.Int32, 0)
else
CheckSame(j.F64, d);
i64 := i * Int64(77777777777);
cx.Free(j);
// 32-bit and 64-bit integer
j.From64(i64);
Check(j.IsNumber);
Check(not j.IsRefCounted);
Check(not j.IsString);
Check(not j.IsNan);
Check(j.IsInt32 = (abs(i64) < maxInt));
if j.IsInt32 then
CheckEqual(j.Int32, i64)
else
Check(j.IsFloat);
CheckEqual(j.Int64, i64);
cx.Free(j);
// UTF-8 text
if i and 7 = 0 then
v := Int32ToUtf8(i) // Ascii
else
v := RandomUtf8(i + 100); // UTF-8
j := cx.From(v);
Check(not j.IsNumber);
Check(not j.IsNull);
Check(not j.IsFloat);
Check(j.IsString);
Check(j.IsRefCounted);
cx.ToUtf8(j, v2);
CheckEqual(v, v2, v);
cx.Free(j);
Check(j.Equals(j));
end;
j.From(true);
Check(not j.IsRefCounted);
Check(j.Bool);
j.From(false);
Check(not j.IsRefCounted);
Check(not J.Bool);
for i := 1 to 1000 do
begin
// basic runtime execution
CheckEqual(Run(
'2+2'),
'4');
CheckEqual(Run(
'a=1234567; a.toString()'),
'1234567');
CheckEqual(Run(
'function add(x, y) { return x + y; } add(434,343)'),
'777');
CheckEqual(Run(
'add(434.732,343.045)'),
'777.777');
CheckEqual(Run(
'function fn(x,y) { return (Math.log(y) / Math.log(x)).toString(); }'#10 +
'fn(5,625)'),
'4'); // 5 x 5 x 5 x 5 = 625
v := Run(
'Date.now()');
CheckUtf8(abs(UnixMSTimeUtcFast - round(GetExtended(pointer(v)))) < 100,
'timestamp - may fail during slow debugging [%]', [v]);
{ v := Run('new Date().toString();');
Check(PosEx(' ' +UInt32ToUtf8(CurrentYear), v) > 0); }
v := Run(
'console.log("Hello World");');
Check(PosEx('''console'' is not defined', v) > 0);
j := cx.Call('', 'add', [777, i]);
Check(cx.ToVariantFree(j, va));
Check(va = i + 777);
j := cx.Call('', 'add', ['777', i]);
Check(cx.ToVariantFree(j, va));
Check(va = '777' + UInt32ToUtf8(i));
j := cx.Call('', 'add', ['3', i]);
Check(cx.ToVariantFree(j, va));
Check(va = '3' + UInt32ToUtf8(i));
va := cx.CallVariant('', 'add', [777.777, i]);
CheckSame(double(va), 777.777 + i);
va := cx.CallVariant('', 'add', ['777', i]);
Check(va = '777' + UInt32ToUtf8(i));
end;
CheckEqual(Run('JSON.stringify({ x: 5, y: 6 })'), '{"x":5,"y":6}');
CheckEqual(Run('[3, 4, 5].map(x => x ** 10).forEach(x => log(x))'), 'undefined');
CheckEqual(output, '59049,1048576,9765625');
Check(not cx.GetValue('notexisting', j));
CheckEqual(cx.EvalGlobal('var car = {type:"Fiat", model:"500", color:"white"};'), '');
v := '{"type":"Fiat","model":"500","color":"white"}';
CheckEqual(Run('JSON.stringify(car)'), v);
Check(cx.GetValue(['car'], j));
CheckEqual(cx.ToUtf8(j), v);
Check(cx.ToVariantFree(j, va));
Check(_Safe(va, dvObject)^.Count = 3);
Check(cx.GetValue('add', j));
Check(cx.ToVariant(j, va));
v := cx.ToUtf8Free(j);
CheckEqual(v, 'function add(x, y) { return x + y; }');
Check(v = va);
finally
cx.Done;
end;
finally
Check(rt.DoneSafe = '');
end;
end;
end.