-
Notifications
You must be signed in to change notification settings - Fork 0
/
v8.pas
852 lines (649 loc) · 24.9 KB
/
v8.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
(*
* Delphi wrapper for V8 JavaScript Engine
*
* Usage allowed under the restrictions of the Lesser GNU General Public License
* or alternatively the restrictions of the Mozilla Public License 1.1
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* Author : Ryan Zhou <[email protected]>
* Web site : https://github.com/zolagiggszhou
* Repository : https://github.com/zolagiggszhou/v8delphiwrapper
*
* Embarcadero Technologies, Inc is not permitted to use or redistribute
* this source code without explicit permission.
*
*)
unit v8;
interface
uses
windows, SysUtils, Classes, TypInfo, Rtti;
const
engine_lib = 'engine.dll';
V8_ERROR = 0;
V8_RANGE_ERROR = 1;
V8_REFERENCE_ERROR = 2;
V8_SYNTAX_ERROR = 3;
V8_TYPE_ERROR = 4;
type
Pv8Engine = ^Tv8Engine;
PUInt32 = ^UInt32;
V8FunctionCallbackInfo = type Pointer;
V8Isolate = type Pointer;
V8Context = type Pointer;
V8String = type Pointer;
V8Object = type Pointer;
V8ObjectTemplate = type Pointer;
V8FunctionCallback = procedure(info: V8FunctionCallbackInfo); cdecl;
Iv8Object = interface;
Tv8Object = class;
Tv8ObjectTemplate = class;
Tv8Base = class
protected
FInternalDataPointer: Pointer;
public
function GetInternalDataPointer: Pointer;
end;
///
/// V8 engine (executing context)
///
Tv8Engine = class
private
FIsolate: V8Isolate;
FContext: V8Context;
public
constructor Create;
destructor Destroy; override;
///
/// Tv8Engine.enter should be called before use in a thread
///
procedure enter;
///
/// Tv8Engine.leave should be called after use in a thread
///
procedure leave;
///
/// get the global object
///
function GlobalObject: Iv8Object;
///
/// execute code and cast the return value as string
///
function eval(const code: string; const silent: boolean = false): string;
///
/// register a delphi function for use in javascript code
///
function RegisterNativeFunction(const name: RawByteString; func: V8FunctionCallback; data: Pointer): Boolean;
///
/// register a delphi class as an V8 object template
///
function RegisterRttiClass(_ClassType: TClass): Tv8ObjectTemplate;
end;
///
/// V8 Javascipt function argument
///
Tv8FunctionArg = record
private
FInternalDataPointer: V8FunctionCallbackInfo;
FIndex: Integer;
public
constructor Create(_internal: V8FunctionCallbackInfo; _index: Integer);
function AsString: string;
function AsInteger: Integer;
function AsUInt32: UInt32;
function AsInt64: Int64;
function AsFloat: Double;
function AsObject: Iv8Object;
end;
///
/// V8 Javascript function calling context (arguments, this object, etc.)
///
Tv8FunctionCallbackInfo = record
private
FInternalDataPointer: V8FunctionCallbackInfo;
function GetArgs(index: Integer): Tv8FunctionArg;
public
constructor Create(_InternalData: V8FunctionCallbackInfo);
function ArgCount: Integer;
function GetInternalField(idx: Integer = 0): Pointer;
function this: Iv8Object;
property args[index: Integer]: Tv8FunctionArg read GetArgs;
end;
///
/// V8 Javascript Object
/// you can bind several pointers to an object, called "internal fields"
///
Iv8Object = interface
function GetInternalObject: V8Object;
function GetInternalFieldCount: Integer;
procedure SetInternalField(idx: Integer; value: Pointer);
function GetInternalField(idx: Integer): Pointer;
///
/// set an object property
///
procedure SetObject(const name: UnicodeString; value: Iv8Object);
///
/// get an string property
///
function GetStr(const name: UnicodeString): UnicodeString;
///
/// get an integer property
///
function GetInt32(const name: UnicodeString): Int32;
///
/// get an unsigned integer property
///
function GetUInt32(const name: UnicodeString): UInt32;
///
/// get an 64bit integer property
///
function GetInt64(const name: UnicodeString): Int64;
///
/// get an float property
///
function GetFloat(const name: UnicodeString): Double;
///
/// get an object property
///
function GetObject(const name: UnicodeString): Iv8Object;
end;
Tv8Object = class(TInterfacedObject, Iv8Object)
private
FInternalObject: V8Object;
public
constructor Create(_obj: V8Object);
destructor Destroy; override;
function GetInternalObject: V8Object;
function GetInternalFieldCount: Integer;
procedure SetInternalField(idx: Integer; value: Pointer);
function GetInternalField(idx: Integer): Pointer;
procedure SetObject(const name: UnicodeString; value: Iv8Object);
function GetStr(const name: UnicodeString): UnicodeString;
function GetInt32(const name: UnicodeString): Int32;
function GetUInt32(const name: UnicodeString): UInt32;
function GetInt64(const name: UnicodeString): Int64;
function GetFloat(const name: UnicodeString): Double;
function GetObject(const name: UnicodeString): Iv8Object;
end;
///
/// V8 Javascript Object Template
///
Tv8ObjectTemplate = class(Tv8Base)
public
constructor Create(InternalFieldCount: Integer);
destructor Destroy; override;
///
/// add a method to object template
///
function AddMethod(const name: RawByteString; func: V8FunctionCallback; data: Pointer): Boolean;
///
/// create an object with object template
///
function CreateInstance(FirstInternalField: Pointer): Iv8Object;
end;
///
/// initialize v8 library, should be called before use of any other api
///
function v8_init: LongBool; stdcall;
///
/// cleanup v8 library
///
procedure v8_cleanup; stdcall;
function v8_new_isolate: V8Isolate; stdcall;
procedure v8_destroy_isolate(isolate: V8Isolate); stdcall;
procedure v8_enter_isolate(isolate: V8Isolate); stdcall;
procedure v8_leave_isolate(isolate: V8Isolate); stdcall;
procedure v8_throw_exception(_type: Integer; errmsg: PWideChar); stdcall;
function v8_new_context(isolate: V8Isolate): V8Context; stdcall;
procedure v8_enter_context(context: V8Context); stdcall;
procedure v8_leave_context(context: V8Context); stdcall;
procedure v8_destroy_context(context: V8Context); stdcall;
function v8_global_object(context: V8Context): V8Object; stdcall;
procedure v8_destroy_string(str: V8String); stdcall;
function v8_eval_asstr(isolate: V8Isolate; context: V8Context; code: PWideChar): V8String; stdcall;
function v8_strinfo(str: V8String; len: PInteger): PWideChar; stdcall;
function v8_set_object(isolate: V8Isolate; context: V8Context; propName: PWideChar; owner, propValue: V8Object): LongBool; stdcall;
function v8_register_native_function(isolate: V8Isolate; context: V8Context; funcname: PAnsiChar; func: V8FunctionCallback; data: Pointer): LongBool; stdcall;
function v8_FunctionCallbackInfo_data(info: V8FunctionCallbackInfo): Pointer; stdcall;
function v8_FunctionCallbackInfo_this(info: V8FunctionCallbackInfo): V8Object; stdcall;
function v8_FunctionCallbackInfo_internal_field(info: V8FunctionCallbackInfo; idx: Integer): Pointer; stdcall;
function v8_FunctionCallbackInfo_arg_count(info: V8FunctionCallbackInfo): Integer; stdcall;
function v8_FunctionCallbackInfo_arg_as_str(info: V8FunctionCallbackInfo; idx: Integer): V8String; stdcall;
function v8_FunctionCallbackInfo_arg_as_int32(info: V8FunctionCallbackInfo; idx: Integer; value: PInteger): LongBool; stdcall;
function v8_FunctionCallbackInfo_arg_as_uint32(info: V8FunctionCallbackInfo; idx: Integer; value: PUInt32): LongBool; stdcall;
function v8_FunctionCallbackInfo_arg_as_int64(info: V8FunctionCallbackInfo; idx: Integer; value: PInt64): LongBool; stdcall;
function v8_FunctionCallbackInfo_arg_as_float(info: V8FunctionCallbackInfo; idx: Integer; value: PDouble): LongBool; stdcall;
function v8_FunctionCallbackInfo_arg_as_object(info: V8FunctionCallbackInfo; idx: Integer): V8Object; stdcall;
procedure v8_FunctionCallbackInfo_return_int32(info: V8FunctionCallbackInfo; value: Integer); stdcall;
procedure v8_FunctionCallbackInfo_return_uint32(info: V8FunctionCallbackInfo; value: UInt32); stdcall;
procedure v8_FunctionCallbackInfo_return_float(info: V8FunctionCallbackInfo; value: Double); stdcall;
procedure v8_FunctionCallbackInfo_return_string(info: V8FunctionCallbackInfo; value: PWideChar); stdcall;
function v8_new_object_template(isolate: V8Isolate; InternalFieldCount: Integer): V8ObjectTemplate; stdcall;
procedure v8_destroy_object_template(objTemplate: V8ObjectTemplate); stdcall;
function v8_object_template_add_method(isolate: V8Isolate; context: V8Context; objTemplate: V8ObjectTemplate; name: PAnsiChar; func: V8FunctionCallback; data: Pointer): LongBool; stdcall;
function v8_new_object(isolate: V8Isolate; context: V8Context; objTemplate: V8ObjectTemplate; FirstInternalField: Pointer): V8Object; stdcall;
procedure v8_destroy_object(obj: V8Object); stdcall;
function v8_object_internal_field_count(obj: V8Object): Integer; stdcall;
function v8_object_get_internal_field(obj: V8Object; idx: Integer): Pointer; stdcall;
procedure v8_object_set_internal_field(obj: V8Object; idx: Integer; value: Pointer); stdcall;
function v8_object_get_int32_field(_obj: V8Object; name: PWideChar; defValue: Int32): Int32; stdcall;
function v8_object_get_uint32_field(_obj: V8Object; name: PWideChar; defValue: UInt32): UInt32; stdcall;
function v8_object_get_float_field(_obj: V8Object; name: PWideChar; value: PDouble): LongBool; stdcall;
function v8_object_get_int64_field(_obj: V8Object; name: PWideChar; value: PInt64): LongBool; stdcall;
function v8_object_get_string_field(_obj: V8Object; name: PWideChar): V8String; stdcall;
function v8_object_get_object_field(_obj: V8Object; name: PWideChar): V8Object; stdcall;
implementation
uses
console, entrypoint;
function v8_init: LongBool; external engine_lib;
procedure v8_cleanup; external engine_lib;
function v8_new_isolate: V8Isolate; stdcall; external engine_lib;
procedure v8_destroy_isolate(isolate: V8Isolate); stdcall; external engine_lib;
procedure v8_enter_isolate(isolate: V8Isolate); stdcall; external engine_lib;
procedure v8_leave_isolate(isolate: V8Isolate); stdcall; external engine_lib;
procedure v8_throw_exception; external engine_lib;
function v8_new_context(isolate: V8Isolate): V8Context; stdcall; external engine_lib;
procedure v8_enter_context(context: V8Context); stdcall; external engine_lib;
procedure v8_leave_context(context: V8Context); stdcall; external engine_lib;
procedure v8_destroy_context(context: V8Context); stdcall; external engine_lib;
function v8_global_object; external engine_lib;
procedure v8_destroy_string(str: V8String); stdcall; external engine_lib;
function v8_eval_asstr; external engine_lib;
function v8_strinfo(str: V8String; len: PInteger): PWideChar; stdcall; external engine_lib;
function v8_set_object(isolate: V8Isolate; context: V8Context; propName: PWideChar; owner, propValue: V8Object): LongBool; stdcall; external engine_lib;
function v8_register_native_function(isolate: V8Isolate; context: V8Context; funcname: PAnsiChar; func: V8FunctionCallback; data: Pointer): LongBool; stdcall; external engine_lib;
function v8_FunctionCallbackInfo_data(info: V8FunctionCallbackInfo): Pointer; stdcall; external engine_lib;
function v8_FunctionCallbackInfo_this; external engine_lib;
function v8_FunctionCallbackInfo_internal_field(info: V8FunctionCallbackInfo; idx: Integer): Pointer; stdcall; external engine_lib;
function v8_FunctionCallbackInfo_arg_count(info: V8FunctionCallbackInfo): Integer; stdcall; external engine_lib;
function v8_FunctionCallbackInfo_arg_as_str(info: V8FunctionCallbackInfo; idx: Integer): V8String; stdcall; external engine_lib;
function v8_FunctionCallbackInfo_arg_as_int32(info: V8FunctionCallbackInfo; idx: Integer; value: PInteger): LongBool; stdcall; external engine_lib;
function v8_FunctionCallbackInfo_arg_as_uint32(info: V8FunctionCallbackInfo; idx: Integer; value: PUInt32): LongBool; stdcall; external engine_lib;
function v8_FunctionCallbackInfo_arg_as_int64(info: V8FunctionCallbackInfo; idx: Integer; value: PInt64): LongBool; stdcall; external engine_lib;
function v8_FunctionCallbackInfo_arg_as_float(info: V8FunctionCallbackInfo; idx: Integer; value: PDouble): LongBool; stdcall; external engine_lib;
function v8_FunctionCallbackInfo_arg_as_object; external engine_lib;
procedure v8_FunctionCallbackInfo_return_int32; external engine_lib;
procedure v8_FunctionCallbackInfo_return_uint32; external engine_lib;
procedure v8_FunctionCallbackInfo_return_float; external engine_lib;
procedure v8_FunctionCallbackInfo_return_string; external engine_lib;
function v8_new_object_template(isolate: V8Isolate; InternalFieldCount: Integer): V8ObjectTemplate; stdcall; external engine_lib;
procedure v8_destroy_object_template(objTemplate: V8ObjectTemplate); stdcall; external engine_lib;
function v8_object_template_add_method(isolate: V8Isolate; context: V8Context; objTemplate: V8ObjectTemplate; name: PAnsiChar; func: V8FunctionCallback; data: Pointer): LongBool; stdcall; external engine_lib;
function v8_new_object(isolate: V8Isolate; context: V8Context; objTemplate: V8ObjectTemplate; FirstInternalField: Pointer): V8Object; stdcall; external engine_lib;
procedure v8_destroy_object(obj: V8Object); stdcall; external engine_lib;
function v8_object_internal_field_count; external engine_lib;
function v8_object_get_internal_field; external engine_lib;
procedure v8_object_set_internal_field; external engine_lib;
function v8_object_get_int32_field; external engine_lib;
function v8_object_get_uint32_field; external engine_lib;
function v8_object_get_float_field; external engine_lib;
function v8_object_get_int64_field; external engine_lib;
function v8_object_get_string_field; external engine_lib;
function v8_object_get_object_field;
begin
Result := nil;
end;
function ConvertInternalString(v8InternalStr: V8String): string;
var
s: PWideChar;
len: Integer;
begin
s := v8_strinfo(v8InternalStr, @len);
SetLength(Result, len);
Move(s^, Pointer(Result)^, len * 2);
end;
{ Tv8Base }
function Tv8Base.GetInternalDataPointer: Pointer;
begin
Result := FInternalDataPointer;
end;
{ Tv8FunctionCallbackInfo }
function Tv8FunctionCallbackInfo.ArgCount: Integer;
begin
Result := v8_FunctionCallbackInfo_arg_count(FInternalDataPointer);
end;
constructor Tv8FunctionCallbackInfo.Create(_InternalData: V8FunctionCallbackInfo);
begin
FInternalDataPointer := _InternalData;
end;
function Tv8FunctionCallbackInfo.GetArgs(index: Integer): Tv8FunctionArg;
begin
Result.FInternalDataPointer := FInternalDataPointer;
Result.FIndex := index;
end;
function Tv8FunctionCallbackInfo.GetInternalField;
var
jsobj: Iv8Object;
begin
jsobj := this;
Result := jsobj.GetInternalField(idx);
end;
function Tv8FunctionCallbackInfo.this: Iv8Object;
var
tmp: V8Object;
begin
tmp := v8_FunctionCallbackInfo_this(FInternalDataPointer);
if Assigned(tmp) then
Result := Tv8Object.Create(tmp)
else
Result := nil;
end;
{ Tv8FunctionArg }
function Tv8FunctionArg.AsFloat: Double;
begin
if not v8_FunctionCallbackInfo_arg_as_float(FInternalDataPointer, FIndex, @Result) then
Result := 0;
end;
function Tv8FunctionArg.AsInt64: Int64;
begin
if not v8_FunctionCallbackInfo_arg_as_int64(FInternalDataPointer, FIndex, @Result) then
Result := 0;
end;
function Tv8FunctionArg.AsInteger: Integer;
begin
if not v8_FunctionCallbackInfo_arg_as_int32(FInternalDataPointer, FIndex, @Result) then
Result := 0;
end;
function Tv8FunctionArg.AsObject: Iv8Object;
var
obj: V8Object;
begin
obj := v8_FunctionCallbackInfo_arg_as_object(FInternalDataPointer, FIndex);
if Assigned(obj) then
Result := Tv8Object.Create(obj)
else
Result := nil;
end;
function Tv8FunctionArg.AsString: string;
var
v8str: Pointer;
begin
v8str := v8_FunctionCallbackInfo_arg_as_str(FInternalDataPointer, FIndex);
Result := ConvertInternalString(v8str);
v8_destroy_string(v8str);
end;
function Tv8FunctionArg.AsUInt32: UInt32;
begin
if not v8_FunctionCallbackInfo_arg_as_uint32(FInternalDataPointer, FIndex, @Result) then
Result := 0;
end;
constructor Tv8FunctionArg.Create(_internal: V8FunctionCallbackInfo; _index: Integer);
begin
FInternalDataPointer := _internal;
FIndex := _index;
end;
{ Tv8ObjectTemplate }
function Tv8ObjectTemplate.AddMethod(const name: RawByteString; func: V8FunctionCallback; data: Pointer): Boolean;
begin
Result := v8_object_template_add_method(nil, nil, FInternalDataPointer, PAnsiChar(name), func, data);
end;
constructor Tv8ObjectTemplate.Create(InternalFieldCount: Integer);
begin
inherited Create;
FInternalDataPointer := v8_new_object_template(nil, InternalFieldCount);
end;
function Tv8ObjectTemplate.CreateInstance(FirstInternalField: Pointer): Iv8Object;
var
obj: V8Object;
begin
obj := v8_new_object(nil, nil, FInternalDataPointer, FirstInternalField);
if Assigned(obj) then
Result := Tv8Object.Create(obj)
else
Result := nil;
end;
destructor Tv8ObjectTemplate.Destroy;
begin
v8_destroy_object_template(FInternalDataPointer);
inherited;
end;
{ Tv8Object }
constructor Tv8Object.Create(_obj: V8Object);
begin
inherited Create;
FInternalObject := _obj;
end;
destructor Tv8Object.Destroy;
begin
v8_destroy_object(FInternalObject);
inherited;
end;
function Tv8Object.GetFloat(const name: UnicodeString): Double;
begin
v8_object_get_float_field(FInternalObject, PWideChar(name), @Result);
end;
function Tv8Object.GetInt32(const name: UnicodeString): Int32;
begin
Result := v8_object_get_int32_field(FInternalObject, PWideChar(name), 0);
end;
function Tv8Object.GetInt64(const name: UnicodeString): Int64;
begin
v8_object_get_int64_field(FInternalObject, PWideChar(name), @Result);
end;
function Tv8Object.GetInternalField(idx: Integer): Pointer;
begin
Result := v8_object_get_internal_field(FInternalObject, idx);
end;
function Tv8Object.GetInternalFieldCount: Integer;
begin
Result := v8_object_internal_field_count(FInternalObject);
end;
function Tv8Object.GetInternalObject: V8Object;
begin
Result := FInternalObject;
end;
function Tv8Object.GetObject(const name: UnicodeString): Iv8Object;
var
v8boj: V8Object;
begin
v8boj := v8_object_get_object_field(FInternalObject, PWideChar(name));
if Assigned(v8boj) then
Result := Tv8Object.Create(v8boj)
else
Result := nil;
end;
function Tv8Object.GetStr(const name: UnicodeString): UnicodeString;
var
v8s: V8String;
begin
v8s := v8_object_get_string_field(FInternalObject, PWideChar(name));
if Assigned(v8s) then
begin
Result := ConvertInternalString(v8s);
v8_destroy_string(v8s);
end
else
Result := '';
end;
function Tv8Object.GetUInt32(const name: UnicodeString): UInt32;
begin
Result := v8_object_get_uint32_field(FInternalObject, PWideChar(name), 0);
end;
procedure Tv8Object.SetInternalField(idx: Integer; value: Pointer);
begin
v8_object_set_internal_field(FInternalObject, idx, value);
end;
procedure Tv8Object.SetObject(const name: UnicodeString; value: Iv8Object);
begin
v8_set_object(nil, nil, PWideChar(name), FInternalObject, value.GetInternalObject);
end;
{ Tv8Engine }
constructor Tv8Engine.Create;
begin
FIsolate := v8_new_isolate;
v8_enter_isolate(FIsolate);
FContext := v8_new_context(FIsolate);
v8_leave_isolate(FIsolate);
end;
destructor Tv8Engine.Destroy;
begin
v8_destroy_context(FContext);
v8_destroy_isolate(FIsolate);
inherited;
end;
procedure Tv8Engine.enter;
begin
v8_enter_isolate(FIsolate);
v8_enter_context(FContext);
end;
function Tv8Engine.eval(const code: string; const silent: boolean = false): string;
var
v8result: V8String;
begin
v8result := v8_eval_asstr(FIsolate, FContext, PWideChar(code));
if Assigned(v8result) then
begin
Result := ConvertInternalString(v8result);
v8_destroy_string(v8result);
end
else
Result := '';
if (result <> '') and (result <> 'undefined') then
begin
if assigned(log) then
log.write(result);
if pos('ReferenceError', result) > 0 then
begin
WriteColoredText(result, FOREGROUND_YELLOW);
// OutputDebugString(pchar(result));
exit;
end;
if pos('SyntaxError', result) > 0 then
begin
WriteColoredText(result, FOREGROUND_RED);
// OutputDebugString(pchar(result));
exit;
end;
if pos('TypeError', result) > 0 then
begin
WriteColoredText(result, FOREGROUND_BLUE);
// OutputDebugString(pchar(result));
exit;
end;
if silent = true then
exit;
WriteColoredText(result, FOREGROUND_LIME);
//OutputDebugString(pchar(result));
end;
end;
function Tv8Engine.GlobalObject: Iv8Object;
var
obj: V8Object;
begin
obj := v8_global_object(FContext);
Result := Tv8Object.Create(obj);
end;
procedure Tv8Engine.leave;
begin
v8_leave_context(FContext);
v8_leave_isolate(FIsolate);
end;
function Tv8Engine.RegisterNativeFunction(const name: RawByteString; func: V8FunctionCallback; data: Pointer): Boolean;
begin
Result := v8_register_native_function(FIsolate, FContext, PAnsiChar(name), func, data);
end;
procedure CallDelphiMethod(_info: V8FunctionCallbackInfo); cdecl;
var
info: Tv8FunctionCallbackInfo;
methods: TArray<TRttiMethod>;
method: TRttiMethod;
dobj: TObject;
parameters: TArray<TRttiParameter>;
parameter: TRttiParameter;
ParamType: TRttiType;
i, nParams: Integer;
values: TArray<TValue>;
ReturnValue: TValue;
str: string;
rttictx: TRttiContext;
rttiType: TRttiType;
CodeAddress: Pointer;
procedure getMethodByCodeAddress;
var
tmp: TRttiMethod;
begin
methods := rttiType.GetDeclaredMethods;
for tmp in methods do
if tmp.CodeAddress = CodeAddress then
begin
method := tmp;
Break;
end;
end;
begin
method := nil;
info := Tv8FunctionCallbackInfo.Create(_info);
dobj := TObject(info.GetInternalField);
rttictx := TRttiContext.Create;
rttiType := rttictx.GetType(dobj.ClassType);
CodeAddress := v8_FunctionCallbackInfo_data(_info);
getMethodByCodeAddress;
if not Assigned(method) then
begin
v8_throw_exception(V8_REFERENCE_ERROR, 'method not found!');
Exit;
end;
parameters := method.GetParameters;
nParams := Length(parameters);
if nParams > info.ArgCount then
begin
v8_throw_exception(V8_TYPE_ERROR, 'no enough parameters!');
Exit;
end;
SetLength(values, nParams);
for i := 0 to nParams - 1 do
begin
parameter := parameters[i];
ParamType := parameter.ParamType;
case ParamType.TypeKind of
tkInteger:
values[i] := TValue.From(info.args[i].AsInteger);
tkFloat:
values[i] := TValue.From(info.args[i].AsFloat);
tkString, tkLString, tkWString, tkUString, tkVariant:
values[i] := TValue.From(info.args[i].AsString);
tkInt64:
values[i] := TValue.From(info.args[i].AsInt64);
else
begin
v8_throw_exception(V8_TYPE_ERROR, 'parameter type dismatch!');
Exit;
end;
end;
end;
ReturnValue := method.Invoke(dobj, values);
if Assigned(method.ReturnType) then
begin
case method.ReturnType.TypeKind of
tkInteger:
v8_FunctionCallbackInfo_return_int32(_info, ReturnValue.AsInteger);
tkFloat:
v8_FunctionCallbackInfo_return_float(_info, ReturnValue.AsExtended);
tkString, tkLString, tkWString, tkUString, tkVariant:
begin
str := ReturnValue.AsString;
v8_FunctionCallbackInfo_return_string(_info, PWideChar(str));
end;
tkInt64:
v8_FunctionCallbackInfo_return_float(_info, ReturnValue.AsExtended);
end;
end;
end;
function Tv8Engine.RegisterRttiClass(_ClassType: TClass): Tv8ObjectTemplate;
var
rttictx: TRttiContext;
rttiType: TRttiType;
methods: TArray<TRttiMethod>;
method: TRttiMethod;
begin
rttictx := TRttiContext.Create;
rttiType := rttictx.GetType(_ClassType);
Result := Tv8ObjectTemplate.Create(1);
methods := rttiType.GetDeclaredMethods;
for method in methods do
if method.MethodKind in [mkProcedure, mkFunction, mkOperatorOverload] then
Result.AddMethod(RawByteString(method.Name), CallDelphiMethod, method.CodeAddress);
end;
end.