-
Notifications
You must be signed in to change notification settings - Fork 5
/
ScriptEngine.pas
8410 lines (7945 loc) · 252 KB
/
ScriptEngine.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit ScriptEngine;
{$mode objfpc}
{$ifdef CPUX86_64}
{$asmmode intel}
{$endif}
{$H+}
{$macro on}
{$modeswitch nestedprocvars}
{$modeswitch advancedrecords}
// enable this if you want to handle UTF-8 strings (requires LCL)
{.$define SE_STRING_UTF8}
// use computed goto instead of case of
{$ifndef AARCH64}
{$define SE_COMPUTED_GOTO}
{$endif}
// enable this if you want to use libffi to handle dynamic function calls
{.$define SE_LIBFFI}
{$if defined(CPU32) or defined(CPU64) or defined(SE_LIBFFI)}
{$define SE_DYNLIBS}
{$endif}
// enable this if you have access to LCL's FileUtil
{.$define SE_HAS_FILEUTIL}
// enable this if you want to print logs to terminal
{.$define SE_LOG}
// enable this if you need json support
{$define SE_HAS_JSON}
{$align 16}
interface
uses
SysUtils, Classes, Generics.Collections, StrUtils, Types, DateUtils, RegExpr,
base64
{$ifdef SE_HAS_JSON}, fpjson, jsonparser{$endif}
{$ifdef SE_HAS_FILEUTIL}, FileUtil{$endif}
{$ifdef SE_LIBFFI}, ffi{$endif}
{$ifdef SE_STRING_UTF8},LazUTF8{$endif}{$ifdef SE_DYNLIBS}, dynlibs{$endif};
const
// Maximum memory in bytes before GC starts acting aggressive
SE_MEM_CEIL = 1024 * 1024 * 256;
type
TSENumber = Double;
TSEOpcode = (
opPushConst,
opPushConstString,
opPushGlobalVar,
opPushLocalVar,
opPushArrayPop,
opPopConst,
opPopFrame,
opAssignGlobalVar,
opAssignGlobalArray,
opAssignLocalVar,
opAssignLocalArray,
opJumpEqual,
opJumpUnconditional,
opJumpEqualOrGreater,
opJumpEqualOrLesser,
opOperatorAdd2,
opOperatorSub2,
opOperatorMul2,
opOperatorDiv2,
opOperatorAdd,
opOperatorSub,
opOperatorMul,
opOperatorDiv,
opOperatorMod,
opOperatorPow,
opOperatorNegative,
opOperatorLesser,
opOperatorLesserOrEqual,
opOperatorGreater,
opOperatorGreaterOrEqual,
opOperatorEqual,
opOperatorNotEqual,
opOperatorAnd,
opOperatorOr,
opOperatorXor,
opOperatorNot,
opOperatorShiftLeft,
opOperatorShiftRight,
opCallRef,
opCallNative,
opCallScript,
opCallImport,
opYield,
opHlt,
opPushTrap,
opPopTrap,
opThrow
);
TSEOpcodes = set of TSEOpcode;
TSEOpcodeInfo = record
Op: TSEOpcode;
Pos: Integer;
Binary: Pointer;
Size: Integer;
end;
PSEOpcodeInfo = ^TSEOpcodeInfo;
TSEOpcodeInfoListAncestor = specialize TList<TSEOpcodeInfo>;
TSEOpcodeInfoList = class(TSEOpcodeInfoListAncestor)
public
function Ptr(const P: Integer): PSEOpcodeInfo;
end;
TSENestedProc = procedure is nested;
TSEValueKind = (
sevkNull,
sevkNumber,
sevkString,
sevkMap,
sevkBuffer,
sevkPointer,
sevkBoolean,
sevkFunction,
sevkPascalObject
);
PSECommonString = ^String;
TSEBuffer = record
Base: Pointer;
Ptr: Pointer;
end;
PSEBuffer = ^TSEBuffer;
TSEPascalObject = record
Value: TObject;
IsManaged: Boolean;
end;
PSEPascalObject = ^TSEPascalObject;
TSEFuncKind = (sefkNative, sefkScript, sefkImport);
PSEStackTraceSymbol = ^TSEStackTraceSymbol;
TSEStackTraceSymbol = record
Name,
Value: String;
Kind: TSEValueKind;
Childs: array of TSEStackTraceSymbol;
end;
TSEStackTraceSymbolArray = array of TSEStackTraceSymbol;
TSEStackTraceSymbolProc = procedure(Message: String; Nodes: TSEStackTraceSymbolArray) of object;
{$mode delphi}
PSEValue = ^TSEValue;
TSEValue = record
Size, Ref: Cardinal;
case Kind: TSEValueKind of
sevkNumber:
(
VarNumber: TSENumber;
);
sevkString:
(
VarString: PSECommonString;
);
sevkMap:
(
VarMap: TObject;
);
sevkBuffer:
(
VarBuffer: PSEBuffer;
);
sevkPointer:
(
VarPointer: Pointer;
);
sevkNull:
(
VarNull: Pointer;
);
sevkBoolean:
(
VarBoolean: Boolean;
);
sevkFunction:
(
VarFuncKind: TSEFuncKind;
VarFuncIndx: QWord;
);
sevkPascalObject:
(
VarPascalObject: PSEPascalObject;
);
end;
{$mode objfpc}
TSEValueList = specialize TList<TSEValue>;
TSEValueMap = class(specialize TDictionary<String, TSEValue>)
private
FIsValidArray: Boolean;
FList: TSEValueList;
public
constructor Create;
destructor Destroy; override;
procedure ToMap;
procedure Set2(const Key: String; const AValue: TSEValue);
procedure Del2(const Key: String);
function Get2(const Key: String): TSEValue;
property List: TSEValueList read FList;
property IsValidArray: Boolean read FIsValidArray;
end;
TSEValueArray = array of TSEValue;
PPSEValue = ^PSEValue;
TSEGCValue = record
Value: TSEValue;
Garbage: Boolean;
Lock: Boolean;
end;
TSEGCValueList = specialize TList<TSEGCValue>;
TSEGCValueAvailStack = specialize TStack<Integer>;
TSEGarbageCollector = class
private
FAllocatedMem: Int64;
FValueList: TSEGCValueList;
FValueAvailStack: TSEGCValueAvailStack;
FTicks: QWord;
procedure Sweep;
public
CeilMem: QWord;
constructor Create;
destructor Destroy; override;
procedure AddToList(const PValue: PSEValue);
procedure CheckForGC;
procedure GC;
procedure AllocBuffer(const PValue: PSEValue; const Size: Integer);
procedure AllocMap(const PValue: PSEValue);
procedure AllocString(const PValue: PSEValue; const S: String);
procedure AllocPascalObject(const PValue: PSEValue; const Obj: TObject; const IsManaged: Boolean = True);
procedure Lock(const PValue: PSEValue);
procedure Unlock(const PValue: PSEValue);
property ValueList: TSEGCValueList read FValueList;
property AllocatedMem: Int64 read FAllocatedMem write FAllocatedMem;
end;
TSECallingConvention = (
seccAuto,
seccStdcall,
seccCdecl
);
TSEAtomKind = (
seakVoid,
seakI8,
seakI16,
seakI32,
seakI64,
seakU8,
seakU16,
seakU32,
seakU64,
seakF32,
seakF64,
seakBuffer,
seakWBuffer
);
TSEAtomKindArray = array of TSEAtomKind;
TSEVM = class;
TSEVMList = specialize TList<TSEVM>;
TSEFuncNativeKind = (sefnkNormal, sefnkSelf);
TSEFunc = function(const VM: TSEVM; const Args: array of TSEValue): TSEValue of object;
TSEFuncWithSelf = function(const VM: TSEVM; const Args: array of TSEValue; const This: TSEValue): TSEValue of object;
TSEFuncNativeInfo = record
Name: String;
Func: TSEFunc;
ArgCount: Integer;
Kind: TSEFuncNativeKind;
end;
PSEFuncNativeInfo = ^TSEFuncNativeInfo;
TSEFuncScriptInfo = record
Name: String;
BinaryPos: Integer;
ArgCount: Integer;
VarCount: Integer;
VarSymbols: TStrings;
end;
PSEFuncScriptInfo = ^TSEFuncScriptInfo;
TSEFuncImportInfo = record
Name: String;
Func: Pointer;
Args: TSEAtomKindArray;
Return: TSEAtomKind;
CallingConvention: TSECallingConvention;
end;
PSEFuncImportInfo = ^TSEFuncImportInfo;
TSEFuncNativeListAncestor = specialize TList<TSEFuncNativeInfo>;
TSEFuncNativeList = class(TSEFuncNativeListAncestor)
public
function Ptr(const P: Integer): PSEFuncNativeInfo;
end;
TSEFuncScriptListAncestor = specialize TList<TSEFuncScriptInfo>;
TSEFuncScriptList = class(TSEFuncScriptListAncestor)
public
function Ptr(const P: Integer): PSEFuncScriptInfo;
end;
TSEFuncImportListAncestor = specialize TList<TSEFuncImportInfo>;
TSEFuncImportList = class(TSEFuncImportListAncestor)
public
function Ptr(const P: Integer): PSEFuncImportInfo;
end;
TSEBinaryAncestor = specialize TList<TSEValue>;
TSEBinary = class(TSEBinaryAncestor)
public
BinaryName: String;
function Ptr(const P: Integer): PSEValue;
end;
TSELineOfCode = record
BinaryCount: Integer;
BinaryPtr: Integer;
Line: Integer;
Module: String;
end;
TSELineOfCodeList = specialize TList<TSELineOfCode>;
TSEConstMap = specialize TDictionary<String, TSEValue>;
TSEStack = TSEBinaryAncestor;
TSEVarMap = TSEConstMap;
TSEListStack = specialize TStack<TList>;
TSEScopeStack = specialize TStack<Integer>;
TSEIntegerList = specialize TList<Integer>;
TSEFrame = record
Code: Integer;
Stack: PSEValue;
Binary: Integer;
Func: PSEFuncScriptInfo;
end;
PSEFrame = ^TSEFrame;
TSETrap = record
FramePtr: PSEFrame;
Stack: PSEValue;
Binary: Integer;
CatchCode: Integer;
end;
PSETrap = ^TSETrap;
TScriptEngine = class;
TSEVM = class
public
IsPaused: Boolean;
IsDone: Boolean;
IsYielded: Boolean;
Global: array of TSEValue;
Stack: array of TSEValue;
Frame: array of TSEFrame;
Trap: array of TSETrap;
ConstStrings: TStringList;
CodePtr: Integer;
StackPtr: PSEValue;
BinaryPtr: Integer;
FramePtr: PSEFrame;
TrapPtr: PSETrap;
StackSize: Integer;
FrameSize: Integer;
TrapSize: Integer;
Parent: TScriptEngine;
Binaries: array of TSEBinary;
WaitTime: LongWord;
constructor Create;
destructor Destroy; override;
function IsWaited: Boolean;
procedure Reset;
procedure Exec;
procedure BinaryClear;
end;
TSECache = record
Binaries: array of TSEBinary;
GlobalVarCount: Cardinal;
GlobalVarSymbols: TStrings;
LineOfCodeList: TSELineOfCodeList;
FuncScriptList: TSEFuncScriptList;
FuncImportList: TSEFuncImportList;
ConstStrings: TStringList;
end;
TSECacheMapAncestor = specialize TDictionary<String, TSECache>;
TSECacheMap = class(TSECacheMapAncestor)
public
procedure ClearSingle(const AName: String);
procedure Clear; override;
end;
TSETokenKind = (
tkEOF,
tkDot,
tkAdd,
tkSub,
tkMul,
tkDiv,
tkMod,
tkPow,
tkShiftLeft,
tkShiftRight,
tkOpAssign,
tkEqual,
tkNotEqual,
tkSmaller,
tkGreater,
tkSmallerOrEqual,
tkGreaterOrEqual,
tkBegin,
tkEnd,
tkColon,
tkBracketOpen,
tkBracketClose,
tkNegative,
tkNumber,
tkString,
tkComma,
tkIf,
tkSwitch,
tkCase,
tkDefault,
tkIdent,
tkFunction,
tkFunctionDecl,
tkVariable,
tkConst,
tkUnknown,
tkElse,
tkWhile,
tkBreak,
tkContinue,
tkYield,
tkSquareBracketOpen,
tkSquareBracketClose,
tkAnd,
tkOr,
tkXor,
tkNot,
tkFor,
tkIn,
tkTo,
tkDownto,
tkReturn,
tkAtom,
tkImport,
tkDo,
tkTry,
tkCatch,
tkThrow
);
TSETokenKinds = set of TSETokenKind;
const
TokenNames: array[TSETokenKind] of String = (
'EOF', '.', '+', '-', '*', 'div', 'mod', '^', '<<', '>>', 'operator assign', '=', '!=', '<',
'>', '<=', '>=', '{', '}', ':', '(', ')', 'neg', 'number', 'string',
',', 'if', 'switch', 'case', 'default', 'identity', 'function', 'fn', 'variable', 'const',
'unknown', 'else', 'while', 'break', 'continue', 'yield',
'[', ']', 'and', 'or', 'xor', 'not', 'for', 'in', 'to', 'downto', 'return',
'atom', 'import', 'do', 'try', 'catch', 'throw'
);
ValueKindNames: array[TSEValueKind] of String = (
'null', 'number', 'string', 'map', 'buffer', 'pointer', 'boolean', 'function', 'pasobject'
);
OpcodeSizes: array[TSEOpcode] of Byte = (
2, // opPushConst,
2, // opPushConstString,
2, // opPushGlobalVar,
3, // opPushLocalVar,
1, // opPushArrayPop,
1, // opPopConst,
1, // opPopFrame,
2, // opAssignGlobalVar,
3, // opAssignGlobalArray,
3, // opAssignLocalVar,
4, // opAssignLocalArray,
2, // opJumpEqual,
2, // opJumpUnconditional,
2, // opJumpEqualOrGreater,
2, // opJumpEqualOrLesser,
4, // opOperatorAdd2,
4, // opOperatorSub2,
4, // opOperatorMul2,
4, // opOperatorDiv2,
1, // opOperatorAdd,
1, // opOperatorSub,
1, // opOperatorMul,
1, // opOperatorDiv,
1, // opOperatorMod,
1, // opOperatorPow,
1, // opOperatorNegative,
1, // opOperatorLesser,
1, // opOperatorLesserOrEqual,
1, // opOperatorGreater,
1, // opOperatorGreaterOrEqual,
1, // opOperatorEqual,
1, // opOperatorNotEqual,
1, // opOperatorAnd,
1, // opOperatorOr,
1, // opOperatorXor,
1, // opOperatorNot,
1, // opOperatorShiftLeft,
1, // opOperatorShiftRight,
4, // opCallRef,
4, // opCallNative,
4, // opCallScript,
4, // opCallImport,
1, // opYield,
1, // opHlt,
2, // opPushTrap,
1, // opPopTrap,
1 // opThrow
);
type
TSEIdentKind = (
ikVariable,
ikFunc
);
TSEIdent = record
Kind: TSEIdentKind;
Addr: Integer;
IsUsed: Boolean;
Local: Integer;
Ln: Integer;
Col: Integer;
Name: String;
end;
PSEIdent = ^TSEIdent;
TSEIdentListAncestor = specialize TList<TSEIdent>;
TSEIdentList = class(TSEIdentListAncestor)
public
function Ptr(const P: Integer): PSEIdent;
end;
TSEToken = record
Kind: TSETokenKind;
BelongedFileName,
Value: String;
Ln, Col: Integer;
end;
PSEToken = ^TSEToken;
TSETokenList = specialize TList<TSEToken>;
TScriptEngine = class
private
FSource: String;
FInternalIdentCount: QWord;
procedure SetSource(V: String);
function InternalIdent: String;
public
OptimizePeephole, // True = enable peephole optimization, default is true
OptimizeConstantFolding, // True = enable constant folding optimization, default is true
OptimizeAsserts: Boolean; // True = ignore assert, default is true
ErrorLn, ErrorCol: Integer;
VM: TSEVM;
IncludePathList,
IncludeList: TStrings;
TokenList: TSETokenList;
OpcodeInfoList: TSEOpcodeInfoList;
LocalVarCountList: TSEIntegerList;
GlobalVarCount: Integer;
GlobalVarSymbols: TStrings;
VarList: TSEIdentList;
FuncNativeList: TSEFuncNativeList;
FuncScriptList: TSEFuncScriptList;
FuncImportList: TSEFuncImportList;
ConstMap: TSEConstMap;
ScopeStack: TSEScopeStack;
ScopeFunc: TSEScopeStack;
LineOfCodeList: TSELineOfCodeList;
StackTraceHandler: TSEStackTraceSymbolProc;
IsLex,
IsParsed: Boolean;
IsDone: Boolean;
FuncCurrent: Integer;
FuncTraversal: Integer;
CurrentFileList: TStrings;
BinaryPos: Integer; // This is mainly for storing line of code for runtime
Binary: TSEBinary; // Current working binary
constructor Create;
destructor Destroy; override;
procedure AddDefaultConsts;
function IsWaited: Boolean;
function GetIsPaused: Boolean;
procedure SetIsPaused(V: Boolean);
function IsYielded: Boolean;
procedure Lex(const IsIncluded: Boolean = False);
procedure Parse;
procedure Reset;
function Exec: TSEValue;
// Execute a function only, currently this does not support yield!
function ExecFuncOnly(const Name: String; const Args: array of TSEValue): TSEValue;
// This method is equivalent of calling Exec(), then ExecFuncOnly()
function ExecFunc(const Name: String; const Args: array of TSEValue): TSEValue;
procedure RegisterFunc(const Name: String; const Func: TSEFunc; const ArgCount: Integer);
procedure RegisterFuncWithSElf(const Name: String; const Func: TSEFuncWithSelf; const ArgCount: Integer);
function RegisterScriptFunc(const Name: String; const ArgCount: Integer): PSEFuncScriptInfo;
procedure RegisterImportFunc(const Name, ActualName, LibName: String; const Args: TSEAtomKindArray; const Return: TSEAtomKind; const CC: TSECallingConvention = seccAuto);
function Backup: TSECache;
procedure Restore(const Cache: TSECache);
property IsPaused: Boolean read GetIsPaused write SetIsPaused;
property Source: String read FSource write SetSource;
end;
function SEValueToText(const Value: TSEValue; const IsRoot: Boolean = True): String;
function SESize(constref Value: TSEValue): Cardinal; inline;
procedure SEValidateType(V: PSEValue; Expected: TSEValueKind; At: DWord; const FuncName: String); inline;
procedure SEMapDelete(constref V: TSEValue; constref I: Integer); inline; overload;
procedure SEMapDelete(constref V: TSEValue; constref S: String); inline; overload;
procedure SEMapDelete(constref V, I: TSEValue); inline; overload;
function SEMapGet(constref V: TSEValue; constref I: Integer): TSEValue; inline; overload;
function SEMapGet(constref V: TSEValue; constref S: String): TSEValue; inline; overload;
function SEMapGet(constref V, I: TSEValue): TSEValue; inline; overload;
procedure SEMapSet(constref V: TSEValue; constref I: Integer; const A: TSEValue); inline; overload;
procedure SEMapSet(constref V: TSEValue; constref S: String; const A: TSEValue); inline; overload;
procedure SEMapSet(constref V, I: TSEValue; const A: TSEValue); inline; overload;
function SEMapIsValidArray(constref V: TSEValue): Boolean; inline;
procedure SEDisAsm(const VM: TSEVM; var Res: String);
operator := (V: TSENumber) R: TSEValue;
operator := (V: String) R: TSEValue;
operator := (V: Boolean) R: TSEValue;
operator := (V: TSEValueArray) R: TSEValue;
operator := (V: Pointer) R: TSEValue;
operator := (V: TSEValue) R: Integer;
{$ifdef CPU64}
operator := (V: TSEValue) R: Int64;
{$endif}
operator := (V: TSEValue) R: Boolean;
operator := (V: TSEValue) R: TSENumber;
operator := (V: TSEValue) R: String;
operator := (V: TSEValue) R: TSEValueArray;
operator := (V: TSEValue) R: Pointer;
operator + (V1: TSEValue; V2: TSENumber) R: TSEValue;
operator + (V1: TSEValue; V2: String) R: TSEValue;
operator + (V1: TSEValue; V2: Pointer) R: TSEValue;
operator - (V1: TSEValue; V2: TSENumber) R: TSEValue;
operator - (V1: TSEValue; V2: Pointer) R: TSEValue;
operator * (V1: TSEValue; V2: TSENumber) R: TSEValue;
operator / (V1: TSEValue; V2: TSENumber) R: TSEValue;
operator + (V1, V2: TSEValue) R: TSEValue;
operator - (V1, V2: TSEValue) R: TSEValue;
operator - (V: TSEValue) R: TSEValue;
operator * (V1, V2: TSEValue) R: TSEValue;
operator / (V1, V2: TSEValue) R: TSEValue;
operator < (V1: TSEValue; V2: TSENumber) R: Boolean;
operator > (V1: TSEValue; V2: TSENumber) R: Boolean;
operator <= (V1: TSEValue; V2: TSENumber) R: Boolean;
operator >= (V1: TSEValue; V2: TSENumber) R: Boolean;
operator = (V1: TSEValue; V2: TSENumber) R: Boolean;
operator <> (V1: TSEValue; V2: String) R: Boolean;
operator < (V1, V2: TSEValue) R: Boolean;
operator > (V1, V2: TSEValue) R: Boolean;
operator <= (V1, V2: TSEValue) R: Boolean;
operator >= (V1, V2: TSEValue) R: Boolean;
operator = (V1, V2: TSEValue) R: Boolean;
operator <> (V1, V2: TSEValue) R: Boolean;
var
ScriptVarMap: TSEVarMap;
GC: TSEGarbageCollector;
ScriptCacheMap: TSECacheMap;
SENull: TSEValue;
JumpTable: array[TSEOpcode] of Pointer;
implementation
uses
Math, Strings;
type
TBuiltInFunction = class
class function SEBufferCreate(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferLength(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferCopy(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferFillU8(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferFillU16(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferFillU32(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferFillU64(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferFillI8(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferFillI16(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferFillI32(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferFillI64(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferFillF32(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferFillF64(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferGetU8(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferGetU16(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferGetU32(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferGetU64(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferGetI8(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferGetI16(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferGetI32(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferGetI64(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferGetF32(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferGetF64(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferSetU8(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferSetU16(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferSetU32(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferSetU64(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferSetI8(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferSetI16(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferSetI32(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferSetI64(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferSetF32(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferSetF64(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringToBuffer(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferToString(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEWBufferToString(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEArrayToBufferF32(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEArrayToBufferF64(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferToArrayF32(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBufferToArrayF64(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SETypeOf(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEWrite(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEWriteln(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SERandom(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SERnd(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SERound(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFloor(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SECeil(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEGet(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SESet(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEString(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SENumber(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEWait(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SELength(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEMapCreate(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEMapKeyDelete(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEMapKeysGet(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEArrayResize(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEArrayToMap(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SELerp(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SESLerp(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SESign(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SESin(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SECos(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SETan(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SECot(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SESqrt(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEAbs(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFrac(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SERange(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEMin(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEMax(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEPow(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringEmpty(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringGrep(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringSplit(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringFind(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringInsert(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringDelete(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringConcat(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringReplace(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringReplaceIgnoreCase(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringFormat(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringUpperCase(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringLowerCase(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringFindRegex(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringTrim(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringTrimLeft(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEStringTrimRight(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEEaseInQuad(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEEaseOutQuad(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEEaseInOutQuad(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEEaseInCubic(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEEaseOutCubic(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEEaseInOutCubic(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEGetTickCount(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDTNow(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDTSetDate(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDTSetTime(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDTDayAdd(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDTMonthAdd(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDTYearAdd(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDTGetYear(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDTGetMonth(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDTGetDay(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDTGetHour(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDTGetMinute(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEGCObjectCount(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEGCUsed(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEGCCollect(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEAssert(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEChar(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEOrd(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFileReadText(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFileReadBinary(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFileWriteText(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFileWriteBinary(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFileCopy(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFileExists(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFileDelete(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFileRename(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFileFindAll(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFileGetSize(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEFileGetAge(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDirectoryCreate(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDirectoryDelete(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDirectoryFindAll(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEDirectoryExists(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBase64Encode(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEBase64Decode(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
{$ifdef SE_HAS_JSON}
class function SEJSONParse(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
class function SEJSONStringify(const VM: TSEVM; const Args: array of TSEValue): TSEValue;
{$endif}
end;
TDynlibMap = specialize TDictionary<String, TLibHandle>;
var
DynlibMap: TDynlibMap;
VMList: TSEVMList;
CS: TRTLCriticalSection;
FS: TFormatSettings;
function PointStrToFloat(S: String): Double; inline;
begin
Result := StrToFloat(S, FS);
end;
function PointFloatToStr(X: Double): String; inline;
begin
Result := FloatToStr(X, FS);
end;
function ReadFileAsString(const Name: String): String; overload;
var
MS: TMemoryStream;
begin
if not FileExists(Name) then
Exit;
MS := TMemoryStream.Create;
try
MS.LoadFromFile(Name);
if MS.Size > 0 then
begin
SetLength(Result, MS.Size div SizeOf(Char));
MS.ReadBuffer(Pointer(Result)^, MS.Size div SizeOf(Char));
end;
finally
MS.Free;
end;
end;
procedure ReadFileAsString(const Name: String; var Str: String); overload;
var
MS: TMemoryStream;
begin
if not FileExists(Name) then
Exit;
MS := TMemoryStream.Create;
try
MS.LoadFromFile(Name);
if MS.Size > 0 then
begin
SetLength(Str, MS.Size div SizeOf(Char));
MS.ReadBuffer(Pointer(Str)^, MS.Size div SizeOf(Char));
end;
finally
MS.Free;
end;
end;
function GetOS: String; inline;
begin
{$if defined(WINDOWS)}
Result := 'windows';
{$elseif defined(LINUX)}
Result := 'linux';
{$elseif defined(DARWIN)}
Result := 'darwin';
{$else}
Result := 'unknown';
{$endif}
end;
procedure SEValidateType(V: PSEValue; Expected: TSEValueKind; At: DWord; const FuncName: String); inline;
var
S1, S2: String;
begin
if V^.Kind <> Expected then
begin
WriteStr(S1, Expected);
WriteStr(S2, V^.Kind);
raise Exception.Create(Format('[%s] Parameter #%d: Expected %s, got %s', [FuncName, At, S1, S2]));
end;
end;
function StringIndexOf(S, P: String): Integer; inline;
begin
{$ifdef SE_STRING_UTF8}
Result := UTF8Pos(P, S);
Dec(Result);
{$else}
Result := S.IndexOf(P);
{$endif}
end;
function SEValueToText(const Value: TSEValue; const IsRoot: Boolean = True): String;
var
Key, S: String;
IsValidArray: Boolean;
I: Integer = 0;
begin
case Value.Kind of
sevkString:
begin
if IsRoot then
Result := Value.VarString^
else
Result := '"' + Value.VarString^ + '"';
end;
sevkNumber:
Result := PointFloatToStr(Value.VarNumber);
sevkBoolean:
Result := BoolToStr(Boolean(Round(Value.VarNumber)), 'true', 'false');
sevkMap:
begin
Result := '[';
IsValidArray := SEMapIsValidArray(Value);
if IsValidArray then
begin
for I := 0 to TSEValueMap(Value.VarMap).List.Count - 1 do
begin
if I > 0 then
Result := Result + ', ';
Result := Result + SEValueToText(SEMapGet(Value, I), False);
end;
end else
begin
for Key in TSEValueMap(Value.VarMap).Keys do
begin
if I > 0 then
Result := Result + ', ';
Result := Result + '"' + Key + '": ' + SEValueToText(SEMapGet(Value, Key), False);
Inc(I);
end;
end;
Result := Result + ']'
end;
sevkFunction:
begin
WriteStr(S, Value.VarFuncKind);
Result := 'fn@' + S + ':' + IntToStr(Value.VarFuncIndx);
end;
sevkNull:
Result := 'null';
sevkBuffer:
begin
Result := 'buffer@' + IntToStr(QWord(Value.VarBuffer^.Ptr));
if Value.VarBuffer^.Base <> nil then
begin
Result := Result + ' <' + IntToStr(MemSize(Value.VarBuffer^.Base) - 16) + ' bytes>';
end;
end;
sevkPointer:
begin
Result := IntToStr(Integer(Value.VarPointer));
end
else
Result := Value;
end;
end;
function SESize(constref Value: TSEValue): Cardinal; inline;
begin
case Value.Kind of
sevkMap:
begin
if SEMapIsValidArray(Value) then
Result := TSEValueMap(Value.VarMap).List.Count
else
Result := TSEValueMap(Value.VarMap).Count;
end;
sevkBuffer:
begin
Result := MemSize(Value.VarBuffer^.Base) - 16;
end;