-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathV8Simple.cpp
984 lines (871 loc) · 27.5 KB
/
V8Simple.cpp
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
#include "V8Simple.h"
#include <include/v8.h>
#include <include/v8-debug.h>
#include <include/libplatform/libplatform.h>
#include <vector>
#include <cstdlib>
#include <atomic>
struct RefCounted
{
std::atomic_int _refCount;
RefCounted()
{
_refCount = 1;
}
void Retain()
{
++_refCount;
}
void Release()
{
auto newRefCount = --_refCount;
if (newRefCount == 0)
{
delete this;
}
}
virtual ~RefCounted() { }
};
struct ArrayBufferAllocator: v8::ArrayBuffer::Allocator
{
virtual void* Allocate(size_t length)
{
return calloc(length, 1);
}
virtual void* AllocateUninitialized(size_t length)
{
return malloc(length);
}
virtual void Free(void* data, size_t)
{
free(data);
}
};
v8::Platform* _platform = nullptr;
// Using this and not plain v8::Persistents ensures that the references are
// reset in the destructor.
template<class T>
using ResettingPersistent = v8::Persistent<T, v8::CopyablePersistentTraits<T>>;
struct JSContext : RefCounted
{
const JSCallbackFinalizer CallbackFinalizer;
const JSExternalFinalizer ExternalFinalizer;
v8::Isolate* Isolate;
ResettingPersistent<v8::Context> Handle;
JSDebugMessageHandler DebugMessageHandler;
void* DebugMessageHandlerData;
JSContext(
JSCallbackFinalizer callbackFinalizer,
JSExternalFinalizer externalFinalizer)
: CallbackFinalizer(callbackFinalizer)
, ExternalFinalizer(externalFinalizer)
, DebugMessageHandler(nullptr)
, DebugMessageHandlerData(nullptr)
{
if (_platform == nullptr)
{
v8::V8::InitializeICU();
_platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(_platform);
v8::V8::Initialize();
}
static ArrayBufferAllocator arrayBufferAllocator;
v8::Isolate::CreateParams createParams;
createParams.array_buffer_allocator = &arrayBufferAllocator;
Isolate = v8::Isolate::New(createParams);
v8::Locker locker(Isolate);
v8::Isolate::Scope isolateScope(Isolate);
v8::HandleScope handleScope(Isolate);
auto localContext = v8::Context::New(Isolate);
v8::Context::Scope contextScope(localContext);
Handle.Reset(Isolate, localContext);
}
virtual ~JSContext() override
{
auto oldData = DebugMessageHandlerData;
DebugMessageHandler = nullptr;
DebugMessageHandlerData = nullptr;
if (ExternalFinalizer != nullptr && oldData != nullptr)
ExternalFinalizer(oldData);
Handle.Reset();
Isolate->Dispose();
Isolate = nullptr;
}
inline v8::Local<v8::Context> LocalHandle() { return Handle.Get(Isolate); }
};
struct V8Scope
{
V8Scope(v8::Isolate* isolate, const ResettingPersistent<v8::Context>& context)
: Locker(isolate)
, IsolateScope(isolate)
, HandleScope(isolate)
, ContextScope(context.Get(isolate))
{
}
V8Scope(JSContext* context)
: V8Scope(context->Isolate, context->Handle)
{
}
v8::Locker Locker;
v8::Isolate::Scope IsolateScope;
v8::HandleScope HandleScope;
v8::Context::Scope ContextScope;
};
struct JSValue : RefCounted
{
virtual JSType Type() const = 0;
};
struct JSInt : JSValue
{
virtual JSType Type() const override { return JSType::Int; }
const int Value;
JSInt(int value) : Value(value) { }
};
struct JSDouble : JSValue
{
virtual JSType Type() const override { return JSType::Double; }
const double Value;
JSDouble(double value) : Value(value) { }
};
struct JSString : JSValue
{
virtual JSType Type() const override { return JSType::String; }
const ResettingPersistent<v8::String> Handle;
JSString(v8::Isolate* isolate, const v8::Local<v8::String>& handle)
: Handle(isolate, handle)
{
}
inline v8::Local<v8::String> LocalHandle(v8::Isolate* isolate) { return Handle.Get(isolate); }
inline v8::Local<v8::String> LocalHandle(JSContext* context) { return Handle.Get(context->Isolate); }
};
struct JSBool : JSValue
{
virtual JSType Type() const override { return JSType::Bool; }
const bool Value;
JSBool(bool value) : Value(value) { }
};
struct JSObject : JSValue
{
virtual JSType Type() const override { return JSType::Object; }
const ResettingPersistent<v8::Object> Handle;
JSObject(v8::Isolate* isolate, const v8::Local<v8::Object>& handle)
: Handle(isolate, handle)
{
}
inline v8::Local<v8::Object> LocalHandle(v8::Isolate* isolate) { return Handle.Get(isolate); }
inline v8::Local<v8::Object> LocalHandle(JSContext* context) { return Handle.Get(context->Isolate); }
};
struct JSArray : JSValue
{
virtual JSType Type() const override { return JSType::Array; }
ResettingPersistent<v8::Array> Handle;
JSArray(v8::Isolate* isolate, const v8::Local<v8::Array>& handle)
: Handle(isolate, handle)
{
}
inline v8::Local<v8::Array> LocalHandle(v8::Isolate* isolate) { return Handle.Get(isolate); }
inline v8::Local<v8::Array> LocalHandle(JSContext* context) { return Handle.Get(context->Isolate); }
};
struct JSFunction : JSValue
{
virtual JSType Type() const override { return JSType::Function; }
ResettingPersistent<v8::Function> Handle;
JSFunction(v8::Isolate* isolate, const v8::Local<v8::Function>& handle)
: Handle(isolate, handle)
{
}
inline v8::Local<v8::Function> LocalHandle(v8::Isolate* isolate) { return Handle.Get(isolate); }
inline v8::Local<v8::Function> LocalHandle(JSContext* context) { return Handle.Get(context->Isolate); }
};
struct JSExternal : JSValue
{
virtual JSType Type() const override { return JSType::External; }
ResettingPersistent<v8::External> Handle;
JSExternal(v8::Isolate* isolate, const v8::Local<v8::External>& handle)
: Handle(isolate, handle)
{
}
inline v8::Local<v8::External> LocalHandle(v8::Isolate* isolate) { return Handle.Get(isolate); }
inline v8::Local<v8::External> LocalHandle(JSContext* context) { return Handle.Get(context->Isolate); }
};
struct JSScriptException : RefCounted
{
JSValue* Exception;
JSString* ErrorMessage;
JSString* FileName;
int LineNumber;
JSString* StackTrace;
JSString* SourceLine;
// Note: Assumes that all arguments are retained
JSScriptException(
JSValue* exception,
JSString* errorMessage,
JSString* fileName,
int lineNumber,
JSString* stackTrace,
JSString* sourceLine)
: Exception(exception)
, ErrorMessage(errorMessage)
, FileName(fileName)
, LineNumber(lineNumber)
, StackTrace(stackTrace)
, SourceLine(sourceLine)
{
}
~JSScriptException()
{
if (Exception != nullptr) Exception->Release();
if (ErrorMessage != nullptr) ErrorMessage->Release();
if (FileName != nullptr) FileName->Release();
if (StackTrace != nullptr) StackTrace->Release();
if (SourceLine != nullptr) SourceLine->Release();
}
};
template<typename T>
inline static auto TryCatch(
JSScriptException** outError,
const V8Scope& scope,
T inner) -> decltype(inner((v8::TryCatch&)*(v8::TryCatch*)nullptr))
{
*outError = nullptr;
try
{
v8::TryCatch tryCatch;
return inner(tryCatch);
}
catch (JSScriptException* exception)
{
*outError = exception;
return decltype(inner((v8::TryCatch&)*(v8::TryCatch*)nullptr))();
}
}
template<typename T>
inline static auto TryCatch(
JSScriptException** outError,
JSContext* context,
T inner) -> decltype(inner((v8::TryCatch&)*(v8::TryCatch*)nullptr))
{
V8Scope scope(context);
return TryCatch(outError, scope, inner);
}
static JSValue* Wrap(JSContext* context, const v8::TryCatch& tryCatch, v8::Local<v8::Value> value);
static void Throw(JSContext* context, const v8::TryCatch& tryCatch)
{
v8::Local<v8::String> emptyString = v8::String::Empty(context->Isolate);
v8::Local<v8::Message> message = tryCatch.Message();
v8::Local<v8::String> sourceLine(emptyString);
v8::Local<v8::String> messageStr(emptyString);
v8::Local<v8::String> fileName(emptyString);
int lineNumber = -1;
auto localContext = context->LocalHandle();
if (!message.IsEmpty())
{
sourceLine = message->GetSourceLine(localContext).FromMaybe(emptyString);
auto messageStrLocal = message->Get();
if (!messageStrLocal.IsEmpty())
{
messageStr = messageStrLocal;
}
fileName = message->GetScriptResourceName()->ToString(localContext).FromMaybe(emptyString);
lineNumber = message->GetLineNumber(localContext).FromMaybe(-1);
}
JSValue* exception = nullptr;
if (!tryCatch.Exception().IsEmpty())
{
v8::TryCatch innerTryCatch;
exception = Wrap(context, innerTryCatch, tryCatch.Exception());
}
v8::Local<v8::String> stackTrace(
tryCatch
.StackTrace(localContext)
.FromMaybe(emptyString.As<v8::Value>())
->ToString(localContext)
.FromMaybe(emptyString));
throw new JSScriptException(
exception,
new JSString(context->Isolate, messageStr),
new JSString(context->Isolate, fileName),
lineNumber,
new JSString(context->Isolate, stackTrace),
new JSString(context->Isolate, sourceLine));
}
template<class A>
inline static v8::Local<A> FromJust(
JSContext* context,
const v8::TryCatch& tryCatch,
v8::MaybeLocal<A> a)
{
if (tryCatch.HasCaught() || a.IsEmpty())
{
Throw(context, tryCatch);
}
return a.ToLocalChecked();
}
template<class A>
inline static A FromJust(
JSContext* context,
const v8::TryCatch& tryCatch,
v8::Maybe<A> a)
{
if (tryCatch.HasCaught() || a.IsNothing())
{
Throw(context, tryCatch);
}
return a.FromJust();
}
static JSValue* Wrap(JSContext* context, const v8::TryCatch& tryCatch, v8::Local<v8::Value> value)
{
if (value->IsUndefined() || value->IsNull())
return nullptr;
if (value->IsInt32())
return new JSInt(FromJust(context, tryCatch, value->Int32Value(context->LocalHandle())));
if (value->IsNumber())
return new JSDouble(FromJust(context, tryCatch, value->NumberValue(context->LocalHandle())));
if (value->IsBoolean())
return new JSBool(FromJust(context, tryCatch, value->BooleanValue(context->LocalHandle())));
if (value->IsString())
return new JSString(context->Isolate, FromJust(context, tryCatch, value->ToString(context->LocalHandle())));
if (value->IsArray())
return new JSArray(context->Isolate, FromJust(context, tryCatch, value->ToObject(context->LocalHandle())).As<v8::Array>());
if (value->IsFunction())
return new JSFunction(context->Isolate, FromJust(context, tryCatch, value->ToObject(context->LocalHandle())).As<v8::Function>());
if (value->IsExternal())
return new JSExternal(context->Isolate, value.As<v8::External>());
if (value->IsObject())
return new JSObject(context->Isolate, FromJust(context, tryCatch, value->ToObject(context->LocalHandle())));
return nullptr; // TODO do something good here
}
static v8::Local<v8::Value> Unwrap(v8::Isolate* isolate, JSValue* value)
{
switch (GetJSValueType(value))
{
case JSType::Null:
return v8::Null(isolate).As<v8::Value>();
case JSType::Int:
return v8::Int32::New(isolate, static_cast<JSInt*>(value)->Value);
case JSType::Double:
return v8::Number::New(isolate, static_cast<JSDouble*>(value)->Value);
case JSType::Bool:
return v8::Boolean::New(isolate, static_cast<JSBool*>(value)->Value);
case JSType::String:
return static_cast<JSString*>(value)->LocalHandle(isolate);
case JSType::Array:
return static_cast<JSArray*>(value)->LocalHandle(isolate);
case JSType::Function:
return static_cast<JSFunction*>(value)->LocalHandle(isolate);
case JSType::External:
return static_cast<JSExternal*>(value)->LocalHandle(isolate);
case JSType::Object:
return static_cast<JSObject*>(value)->LocalHandle(isolate);
default: break;
}
return v8::Null(isolate).As<v8::Value>(); // TODO do something good here
}
static inline JSValue* WrapMaybe(JSContext* context, const v8::TryCatch& tryCatch, v8::MaybeLocal<v8::Value> value)
{
return Wrap(context, tryCatch, FromJust(context, tryCatch, value));
}
template<typename T>
inline static T const* data_ptr(const std::vector<T>& v)
{
return v.size() > 0
? &*v.begin()
: nullptr;
}
template<typename T>
inline static T* data_ptr(std::vector<T>& v)
{
return v.size() > 0
? &*v.begin()
: nullptr;
}
// -------------------------------------------------------------------------
// Context
DllPublic void CDecl RetainJSContext(JSContext* context)
{
if (context != nullptr)
{
context->Retain();
}
}
DllPublic void CDecl ReleaseJSContext(JSContext* context)
{
if (context != nullptr)
{
v8::Locker(context->Isolate);
context->Release();
}
}
DllPublic JSContext* CDecl CreateJSContext(
JSCallbackFinalizer callbackFinalizer,
JSExternalFinalizer externalFinalizer)
{
return new JSContext(callbackFinalizer, externalFinalizer);
}
DllPublic JSValue* CDecl JSContextEvaluateCreate(JSContext* context, JSString* fileName, JSString* code, JSScriptException** outError)
{
return TryCatch(outError, context, [&] (v8::TryCatch& tryCatch)
{
v8::ScriptOrigin origin(fileName->LocalHandle(context));
auto script = FromJust(
context,
tryCatch,
v8::Script::Compile(
context->LocalHandle(),
code->LocalHandle(context),
&origin));
return WrapMaybe(context, tryCatch, script->Run(context->LocalHandle()));
});
}
DllPublic JSObject* CDecl JSContextCopyGlobalObject(JSContext* context)
{
V8Scope scope(context);
return new JSObject(context->Isolate, context->LocalHandle()->Global());
}
DllPublic const char* CDecl GetV8Version() { return v8::V8::GetVersion(); }
// -------------------------------------------------------------------------
// Debug
DllPublic void CDecl SetJSDebugMessageHandler(JSContext* context, void* data, JSDebugMessageHandler messageHandler)
{
static JSContext* debugContext;
debugContext = context;
if (context->DebugMessageHandlerData != data || context->DebugMessageHandler != messageHandler)
{
V8Scope scope(context);
auto oldData = context->DebugMessageHandlerData;
if (messageHandler == nullptr)
{
context->DebugMessageHandler = nullptr;
context->DebugMessageHandlerData = nullptr;
v8::Debug::SetMessageHandler(context->Isolate, nullptr);
}
else
{
context->DebugMessageHandlerData = data;
context->DebugMessageHandler = messageHandler;
v8::Debug::SetMessageHandler(context->Isolate, [] (const v8::Debug::Message& message)
{
auto isolate = message.GetIsolate();
v8::HandleScope handleScope(isolate);
debugContext->DebugMessageHandler(debugContext->DebugMessageHandlerData, new JSString(isolate, message.GetJSON()));
});
}
if (context->ExternalFinalizer != nullptr && oldData != nullptr)
context->ExternalFinalizer(oldData);
}
}
DllPublic void CDecl SendJSDebugCommand(JSContext* context, const uint16_t* command, int length)
{
v8::Debug::SendCommand(context->Isolate, command, length);
}
DllPublic void CDecl ProcessJSDebugMessages(JSContext* context)
{
V8Scope scope(context);
v8::Debug::ProcessDebugMessages(context->Isolate);
}
// -------------------------------------------------------------------------
// Value
DllPublic JSType CDecl GetJSValueType(JSValue* value) { return value == nullptr ? JSType::Null : value->Type(); }
DllPublic void CDecl RetainJSValue(JSContext* context, JSValue* value)
{
if (value != nullptr)
value->Retain();
}
DllPublic void CDecl ReleaseJSValue(JSContext* context, JSValue* value)
{
if (value != nullptr && context != nullptr)
{
v8::Locker(context->Isolate);
value->Release();
}
else
{
// Leak
}
}
DllPublic int CDecl JSValueAsInt(JSValue* value, JSRuntimeError* outError)
{
*outError = JSRuntimeError::NoError;
if (GetJSValueType(value) != JSType::Int)
{
*outError = JSRuntimeError::InvalidCast;
return 0;
}
return static_cast<JSInt*>(value)->Value;
}
DllPublic double CDecl JSValueAsDouble(JSValue* value, JSRuntimeError* outError)
{
*outError = JSRuntimeError::NoError;
if (GetJSValueType(value) != JSType::Double)
{
*outError = JSRuntimeError::InvalidCast;
return 0.0;
}
return static_cast<JSDouble*>(value)->Value;
}
DllPublic JSString* CDecl JSValueAsString(JSValue* value, JSRuntimeError* outError)
{
*outError = JSRuntimeError::NoError;
if (GetJSValueType(value) != JSType::String)
{
*outError = JSRuntimeError::InvalidCast;
return nullptr;
}
return static_cast<JSString*>(value);
}
DllPublic bool CDecl JSValueAsBool(JSValue* value, JSRuntimeError* outError)
{
*outError = JSRuntimeError::NoError;
if (GetJSValueType(value) != JSType::Bool)
{
*outError = JSRuntimeError::InvalidCast;
return false;
}
return static_cast<JSBool*>(value)->Value;
}
DllPublic JSObject* CDecl JSValueAsObject(JSValue* value, JSRuntimeError* outError)
{
*outError = JSRuntimeError::NoError;
auto type = GetJSValueType(value);
if (type != JSType::Object && type != JSType::Null)
{
*outError = JSRuntimeError::InvalidCast;
return nullptr;
}
return static_cast<JSObject*>(value);
}
DllPublic JSArray* CDecl JSValueAsArray(JSValue* value, JSRuntimeError* outError)
{
*outError = JSRuntimeError::NoError;
if (GetJSValueType(value) != JSType::Array)
{
*outError = JSRuntimeError::InvalidCast;
return nullptr;
}
return static_cast<JSArray*>(value);
}
DllPublic JSFunction* CDecl JSValueAsFunction(JSValue* value, JSRuntimeError* outError)
{
*outError = JSRuntimeError::NoError;
if (GetJSValueType(value) != JSType::Function)
{
*outError = JSRuntimeError::InvalidCast;
return nullptr;
}
return static_cast<JSFunction*>(value);
}
DllPublic JSExternal* CDecl JSValueAsExternal(JSValue* value, JSRuntimeError* outError)
{
*outError = JSRuntimeError::NoError;
if (GetJSValueType(value) != JSType::External)
{
*outError = JSRuntimeError::InvalidCast;
return nullptr;
}
return static_cast<JSExternal*>(value);
}
DllPublic bool CDecl JSValueStrictEquals(JSContext* context, JSValue* obj1, JSValue* obj2)
{
V8Scope scope(context);
return Unwrap(context->Isolate, obj1)->StrictEquals(Unwrap(context->Isolate, obj2));
}
// --------------------------------------------------------------------------
// Primitives
DllPublic JSValue* CDecl JSNull() { return nullptr; }
DllPublic JSValue* CDecl CreateJSInt(int value) { return new JSInt(value); }
DllPublic JSValue* CDecl CreateJSDouble(double value) { return new JSDouble(value); }
DllPublic JSValue* CDecl CreateJSBool(bool value) { return new JSBool(value); }
DllPublic JSObject* CDecl CreateExternalJSArrayBuffer(JSContext* context, void* data, int byteLength)
{
V8Scope scope(context);
return new JSObject(context->Isolate, v8::ArrayBuffer::New(context->Isolate, data, (size_t)byteLength));
}
DllPublic JSFunction* CDecl CreateJSCallback(JSContext* context, void* data, JSCallback callback, JSScriptException** outError)
{
return TryCatch(outError, context, [&] (v8::TryCatch& tryCatch)
{
struct Closure
{
JSContext* context;
ResettingPersistent<v8::External> finalizer;
void* data;
JSCallback callback;
};
auto closure = new Closure{context, {}, data, callback};
auto localClosure = v8::External::New(context->Isolate, closure);
closure->finalizer.Reset(context->Isolate, localClosure);
closure->finalizer.SetWeak(
closure,
[] (const v8::WeakCallbackInfo<Closure>& data)
{
auto closure = data.GetParameter();
auto f = closure->context->CallbackFinalizer;
if (f != nullptr)
f(closure->data);
closure->finalizer.Reset();
delete closure;
},
v8::WeakCallbackType::kParameter);
struct AutoReleaser
{
const std::vector<JSValue*>& _values;
~AutoReleaser()
{
for (auto v : _values)
{
if (v != nullptr)
v->Release();
}
}
};
return new JSFunction(context->Isolate,
FromJust(context, tryCatch, v8::Function::New(
context->LocalHandle(),
[] (const v8::FunctionCallbackInfo<v8::Value>& info)
{
auto isolate = info.GetIsolate();
v8::HandleScope handleScope(isolate);
Closure* closure =
static_cast<Closure*>(info.Data().
As<v8::External>()
->Value());
auto numArgs = info.Length();
std::vector<JSValue*> args(numArgs);
AutoReleaser autoRelease{args};
try
{
{
v8::TryCatch tryCatch;
for (int i = 0; i < numArgs; ++i)
args[i] = Wrap(closure->context, tryCatch, info[i]);
}
JSValue* error = nullptr;
JSValue* result = closure->callback(closure->context, closure->data, data_ptr(args), numArgs, &error);
info.GetReturnValue().Set(Unwrap(isolate, result));
if (result != nullptr)
result->Release();
if (error != nullptr)
{
auto unwrappedError = Unwrap(isolate, error);
error->Release();
isolate->ThrowException(unwrappedError);
}
}
catch (JSScriptException* error)
{
auto unwrappedError = Unwrap(isolate, error->Exception);
error->Release();
isolate->ThrowException(unwrappedError);
}
},
localClosure.As<v8::Value>())));
});
}
// --------------------------------------------------------------------------
// String
DllPublic JSString* CDecl CreateJSString(JSContext* context, const uint16_t* buffer, int length, JSRuntimeError* outError)
{
*outError = JSRuntimeError::NoError;
V8Scope scope(context);
auto mstr = v8::String::NewFromTwoByte(context->Isolate, buffer, v8::NewStringType::kNormal, length);
if (mstr.IsEmpty())
{
*outError = JSRuntimeError::StringTooLong;
return nullptr;
}
return new JSString(context->Isolate, mstr.ToLocalChecked());
}
DllPublic int CDecl JSStringLength(JSContext* context, JSString* string)
{
V8Scope scope(context);
return string->LocalHandle(context)->Length();
}
DllPublic void CDecl WriteJSStringBuffer(JSContext* context, JSString* string, uint16_t* outBuffer, bool nullTerminate)
{
V8Scope scope(context);
string->LocalHandle(context)->Write(outBuffer, 0, -1, nullTerminate ? v8::String::NO_OPTIONS : v8::String::NO_NULL_TERMINATION);
}
DllPublic JSValue* CDecl JSStringAsValue(JSString* string) { return static_cast<JSValue*>(string); }
// -------------------------------------------------------------------------
// Object
DllPublic JSValue* CDecl CopyJSObjectProperty(JSContext* context, JSObject* obj, JSString* key, JSScriptException** outError)
{
return TryCatch(outError, context, [&] (v8::TryCatch& tryCatch)
{
return WrapMaybe(
context,
tryCatch,
obj->LocalHandle(context)->Get(
context->LocalHandle(),
key->LocalHandle(context)));
});
}
DllPublic void CDecl SetJSObjectProperty(JSContext* context, JSObject* obj, JSString* key, JSValue* value, JSScriptException** outError)
{
TryCatch(outError, context, [&] (v8::TryCatch& tryCatch)
{
FromJust(context, tryCatch, obj->LocalHandle(context)->Set(
context->LocalHandle(),
key->LocalHandle(context),
Unwrap(context->Isolate, value)));
});
}
DllPublic JSArray* CDecl CopyJSObjectOwnPropertyNames(JSContext* context, JSObject* obj, JSScriptException** outError)
{
return TryCatch(outError, context, [&] (v8::TryCatch& tryCatch)
{
return new JSArray(
context->Isolate,
FromJust(
context,
tryCatch,
obj->LocalHandle(context)->GetOwnPropertyNames(context->LocalHandle())));
});
}
DllPublic bool CDecl JSObjectHasProperty(JSContext* context, JSObject* obj, JSString* key, JSScriptException** outError)
{
return TryCatch(outError, context, [&] (v8::TryCatch& tryCatch)
{
return FromJust(
context,
tryCatch,
obj->LocalHandle(context)->Has(context->LocalHandle(), key->LocalHandle(context)));
});
}
DllPublic void* CDecl GetJSObjectArrayBufferData(JSContext* context, JSObject* obj, JSRuntimeError* outError)
{
*outError = JSRuntimeError::NoError;
V8Scope scope(context);
auto localObj = obj->LocalHandle(context);
if (!localObj->IsArrayBuffer())
{
*outError = JSRuntimeError::TypeError;
return nullptr;
}
return localObj.As<v8::ArrayBuffer>()->GetContents().Data();
}
DllPublic JSValue* CDecl JSObjectAsValue(JSObject* obj) { return static_cast<JSValue*>(obj); }
// -------------------------------------------------------------------------
// Array
DllPublic JSValue* CDecl CopyJSArrayPropertyAtIndex(JSContext* context, JSArray* arr, int index, JSScriptException** outError)
{
return TryCatch(outError, context, [&] (v8::TryCatch& tryCatch)
{
return WrapMaybe(
context,
tryCatch,
arr->LocalHandle(context)->Get(context->LocalHandle(), index));
});
}
DllPublic void CDecl SetJSArrayPropertyAtIndex(JSContext* context, JSArray* arr, int index, JSValue* value, JSScriptException** outError)
{
return TryCatch(outError, context, [&] (v8::TryCatch& tryCatch)
{
FromJust(
context,
tryCatch,
arr->LocalHandle(context)->Set(
context->LocalHandle(),
static_cast<uint32_t>(index),
Unwrap(context->Isolate, value)));
});
}
DllPublic int CDecl JSArrayLength(JSContext* context, JSArray* arr)
{
V8Scope scope(context);
return static_cast<int>(arr->LocalHandle(context)->Length());
}
DllPublic JSValue* CDecl JSArrayAsValue(JSArray* arr) { return static_cast<JSValue*>(arr); }
// -------------------------------------------------------------------------
// Function
DllPublic JSValue* CDecl CallJSFunctionCreate(JSContext* context, JSFunction* function, JSObject* thisObject, JSValue* const* args, int numArgs, JSScriptException** outError)
{
return TryCatch(outError, context, [&] (v8::TryCatch& tryCatch)
{
std::vector<v8::Local<v8::Value>> unwrappedArgs(numArgs);
for (int i = 0; i < numArgs; ++i)
unwrappedArgs[i] = Unwrap(context->Isolate, args[i]);
return WrapMaybe(
context,
tryCatch,
function->LocalHandle(context)->Call(
context->LocalHandle(),
Unwrap(context->Isolate, thisObject),
numArgs,
data_ptr(unwrappedArgs)));
});
}
DllPublic JSObject* CDecl ConstructJSFunctionCreate(JSContext* context, JSFunction* function, JSValue* const* args, int numArgs, JSScriptException** outError)
{
return TryCatch(outError, context, [&] (v8::TryCatch& tryCatch)
{
std::vector<v8::Local<v8::Value>> unwrappedArgs(numArgs);
for (int i = 0; i < numArgs; ++i)
unwrappedArgs[i] = Unwrap(context->Isolate, args[i]);
return new JSObject(
context->Isolate,
FromJust(context, tryCatch, function->LocalHandle(context)->NewInstance(
context->LocalHandle(),
numArgs,
data_ptr(unwrappedArgs))));
});
}
DllPublic JSValue* CDecl JSFunctionAsValue(JSFunction* fun) { return static_cast<JSValue*>(fun); }
// -------------------------------------------------------------------------
// External
DllPublic JSExternal* CDecl CreateJSExternal(JSContext* context, void* value)
{
V8Scope scope(context);
auto localExternal = v8::External::New(context->Isolate, value);
struct Closure
{
ResettingPersistent<v8::External> finalizer;
JSExternalFinalizer externalFinalizer;
void* value;
};
auto closure = new Closure{{}, context->ExternalFinalizer, value};
closure->finalizer.Reset(context->Isolate, localExternal);
closure->finalizer.SetWeak(
closure,
[] (const v8::WeakCallbackInfo<Closure>& data)
{
auto closure = data.GetParameter();
if (closure->externalFinalizer != nullptr)
closure->externalFinalizer(closure->value);
closure->finalizer.Reset();
delete closure;
},
v8::WeakCallbackType::kParameter);
return new JSExternal(context->Isolate, localExternal);
}
DllPublic void* CDecl GetJSExternalValue(JSContext* context, JSExternal* external)
{
V8Scope scope(context);
return external->LocalHandle(context)->Value();
}
DllPublic JSValue* CDecl JSExternalAsValue(JSExternal* external) { return static_cast<JSValue*>(external); }
// -------------------------------------------------------------------------
// Exceptions
DllPublic void CDecl RetainJSScriptException(JSContext* context, JSScriptException* e)
{
if (e != nullptr)
{
v8::Locker(context->Isolate);
e->Retain();
}
}
DllPublic void CDecl ReleaseJSScriptException(JSContext* context, JSScriptException* e)
{
if (e != nullptr)
{
v8::Locker(context->Isolate);
e->Release();
}
}
DllPublic JSValue* CDecl GetJSScriptException(JSScriptException* e) { return e->Exception; }
DllPublic JSString* CDecl GetJSScriptExceptionMessage(JSScriptException* e) { return e->ErrorMessage; }
DllPublic JSString* CDecl GetJSScriptExceptionFileName(JSScriptException* e) { return e->FileName; }
DllPublic int CDecl GetJSScriptExceptionLineNumber(JSScriptException* e) { return e->LineNumber; }
DllPublic JSString* CDecl GetJSScriptExceptionStackTrace(JSScriptException* e) { return e->StackTrace; }
DllPublic JSString* CDecl GetJSScriptExceptionSourceLine(JSScriptException* e) { return e->SourceLine; }
/// }