-
Notifications
You must be signed in to change notification settings - Fork 78
/
v8_c_bridge.cc
669 lines (565 loc) · 23.5 KB
/
v8_c_bridge.cc
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
#include "v8_c_bridge.h"
#include "libplatform/libplatform.h"
#include "v8.h"
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <stdio.h>
#define ISOLATE_SCOPE(iso) \
v8::Isolate* isolate = (iso); \
v8::Locker locker(isolate); /* Lock to current thread. */ \
v8::Isolate::Scope isolate_scope(isolate); /* Assign isolate to this thread. */
#define VALUE_SCOPE(ctxptr) \
ISOLATE_SCOPE(static_cast<Context*>(ctxptr)->isolate) \
v8::HandleScope handle_scope(isolate); /* Create a scope for handles. */ \
v8::Local<v8::Context> ctx(static_cast<Context*>(ctxptr)->ptr.Get(isolate)); \
v8::Context::Scope context_scope(ctx); /* Scope to this context. */
extern "C" ValueTuple go_callback_handler(
String id, CallerInfo info, int argc, ValueTuple* argv);
// We only need one, it's stateless.
auto allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
typedef struct {
v8::Persistent<v8::Context> ptr;
v8::Isolate* isolate;
} Context;
typedef v8::Persistent<v8::Value> Value;
String DupString(const v8::String::Utf8Value& src) {
char* data = static_cast<char*>(malloc(src.length()));
memcpy(data, *src, src.length());
return (String){data, src.length()};
}
String DupString(const v8::Local<v8::Value>& val) {
return DupString(v8::String::Utf8Value(val));
}
String DupString(const char* msg) {
const char* data = strdup(msg);
return (String){data, int(strlen(msg))};
}
String DupString(const std::string& src) {
char* data = static_cast<char*>(malloc(src.length()));
memcpy(data, src.data(), src.length());
return (String){data, int(src.length())};
}
KindMask v8_Value_KindsFromLocal(v8::Local<v8::Value> value) {
KindMask kinds = 0;
if (value->IsUndefined()) kinds |= (1ULL << Kind::kUndefined );
if (value->IsNull()) kinds |= (1ULL << Kind::kNull );
if (value->IsName()) kinds |= (1ULL << Kind::kName );
if (value->IsString()) kinds |= (1ULL << Kind::kString );
if (value->IsSymbol()) kinds |= (1ULL << Kind::kSymbol );
if (value->IsObject()) kinds |= (1ULL << Kind::kObject );
if (value->IsArray()) kinds |= (1ULL << Kind::kArray );
if (value->IsBoolean()) kinds |= (1ULL << Kind::kBoolean );
if (value->IsNumber()) kinds |= (1ULL << Kind::kNumber );
if (value->IsExternal()) kinds |= (1ULL << Kind::kExternal );
if (value->IsInt32()) kinds |= (1ULL << Kind::kInt32 );
if (value->IsUint32()) kinds |= (1ULL << Kind::kUint32 );
if (value->IsDate()) kinds |= (1ULL << Kind::kDate );
if (value->IsArgumentsObject()) kinds |= (1ULL << Kind::kArgumentsObject );
if (value->IsBooleanObject()) kinds |= (1ULL << Kind::kBooleanObject );
if (value->IsNumberObject()) kinds |= (1ULL << Kind::kNumberObject );
if (value->IsStringObject()) kinds |= (1ULL << Kind::kStringObject );
if (value->IsSymbolObject()) kinds |= (1ULL << Kind::kSymbolObject );
if (value->IsNativeError()) kinds |= (1ULL << Kind::kNativeError );
if (value->IsRegExp()) kinds |= (1ULL << Kind::kRegExp );
if (value->IsFunction()) kinds |= (1ULL << Kind::kFunction );
if (value->IsAsyncFunction()) kinds |= (1ULL << Kind::kAsyncFunction );
if (value->IsGeneratorFunction()) kinds |= (1ULL << Kind::kGeneratorFunction);
if (value->IsGeneratorObject()) kinds |= (1ULL << Kind::kGeneratorObject );
if (value->IsPromise()) kinds |= (1ULL << Kind::kPromise );
if (value->IsMap()) kinds |= (1ULL << Kind::kMap );
if (value->IsSet()) kinds |= (1ULL << Kind::kSet );
if (value->IsMapIterator()) kinds |= (1ULL << Kind::kMapIterator );
if (value->IsSetIterator()) kinds |= (1ULL << Kind::kSetIterator );
if (value->IsWeakMap()) kinds |= (1ULL << Kind::kWeakMap );
if (value->IsWeakSet()) kinds |= (1ULL << Kind::kWeakSet );
if (value->IsArrayBuffer()) kinds |= (1ULL << Kind::kArrayBuffer );
if (value->IsArrayBufferView()) kinds |= (1ULL << Kind::kArrayBufferView );
if (value->IsTypedArray()) kinds |= (1ULL << Kind::kTypedArray );
if (value->IsUint8Array()) kinds |= (1ULL << Kind::kUint8Array );
if (value->IsUint8ClampedArray()) kinds |= (1ULL << Kind::kUint8ClampedArray);
if (value->IsInt8Array()) kinds |= (1ULL << Kind::kInt8Array );
if (value->IsUint16Array()) kinds |= (1ULL << Kind::kUint16Array );
if (value->IsInt16Array()) kinds |= (1ULL << Kind::kInt16Array );
if (value->IsUint32Array()) kinds |= (1ULL << Kind::kUint32Array );
if (value->IsInt32Array()) kinds |= (1ULL << Kind::kInt32Array );
if (value->IsFloat32Array()) kinds |= (1ULL << Kind::kFloat32Array );
if (value->IsFloat64Array()) kinds |= (1ULL << Kind::kFloat64Array );
if (value->IsDataView()) kinds |= (1ULL << Kind::kDataView );
if (value->IsSharedArrayBuffer()) kinds |= (1ULL << Kind::kSharedArrayBuffer);
if (value->IsProxy()) kinds |= (1ULL << Kind::kProxy );
if (value->IsWebAssemblyCompiledModule())
kinds |= (1ULL << Kind::kWebAssemblyCompiledModule);
return kinds;
}
std::string str(v8::Local<v8::Value> value) {
v8::String::Utf8Value s(value);
if (s.length() == 0) {
return "";
}
return *s;
}
std::string report_exception(v8::Isolate* isolate, v8::Local<v8::Context> ctx, v8::TryCatch& try_catch) {
std::stringstream ss;
ss << "Uncaught exception: ";
std::string exceptionStr = str(try_catch.Exception());
ss << exceptionStr; // TODO(aroman) JSON-ify objects?
if (!try_catch.Message().IsEmpty()) {
if (!try_catch.Message()->GetScriptResourceName()->IsUndefined()) {
ss << std::endl
<< "at " << str(try_catch.Message()->GetScriptResourceName());
v8::Maybe<int> line_no = try_catch.Message()->GetLineNumber(ctx);
v8::Maybe<int> start = try_catch.Message()->GetStartColumn(ctx);
v8::Maybe<int> end = try_catch.Message()->GetEndColumn(ctx);
v8::MaybeLocal<v8::String> sourceLine = try_catch.Message()->GetSourceLine(ctx);
if (line_no.IsJust()) {
ss << ":" << line_no.ToChecked();
}
if (start.IsJust()) {
ss << ":" << start.ToChecked();
}
if (!sourceLine.IsEmpty()) {
ss << std::endl
<< " " << str(sourceLine.ToLocalChecked());
}
if (start.IsJust() && end.IsJust()) {
ss << std::endl
<< " ";
for (int i = 0; i < start.ToChecked(); i++) {
ss << " ";
}
for (int i = start.ToChecked(); i < end.ToChecked(); i++) {
ss << "^";
}
}
}
}
if (!try_catch.StackTrace().IsEmpty()) {
ss << std::endl << "Stack trace: " << str(try_catch.StackTrace());
}
return ss.str();
}
extern "C" {
Version version = {V8_MAJOR_VERSION, V8_MINOR_VERSION, V8_BUILD_NUMBER, V8_PATCH_LEVEL};
void v8_init() {
v8::Platform *platform = v8::platform::CreateDefaultPlatform(
0, // thread_pool_size
v8::platform::IdleTaskSupport::kDisabled,
v8::platform::InProcessStackDumping::kDisabled);
v8::V8::InitializePlatform(platform);
v8::V8::Initialize();
return;
}
StartupData v8_CreateSnapshotDataBlob(const char* js) {
v8::StartupData data = v8::V8::CreateSnapshotDataBlob(js);
return StartupData{data.data, data.raw_size};
}
IsolatePtr v8_Isolate_New(StartupData startup_data) {
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = allocator;
if (startup_data.len > 0 && startup_data.ptr != nullptr) {
v8::StartupData* data = new v8::StartupData;
data->data = startup_data.ptr;
data->raw_size = startup_data.len;
create_params.snapshot_blob = data;
}
return static_cast<IsolatePtr>(v8::Isolate::New(create_params));
}
ContextPtr v8_Isolate_NewContext(IsolatePtr isolate_ptr) {
ISOLATE_SCOPE(static_cast<v8::Isolate*>(isolate_ptr));
v8::HandleScope handle_scope(isolate);
isolate->SetCaptureStackTraceForUncaughtExceptions(true);
v8::Local<v8::ObjectTemplate> globals = v8::ObjectTemplate::New(isolate);
Context* ctx = new Context;
ctx->ptr.Reset(isolate, v8::Context::New(isolate, nullptr, globals));
ctx->isolate = isolate;
return static_cast<ContextPtr>(ctx);
}
void v8_Isolate_Terminate(IsolatePtr isolate_ptr) {
v8::Isolate* isolate = static_cast<v8::Isolate*>(isolate_ptr);
isolate->TerminateExecution();
}
void v8_Isolate_Release(IsolatePtr isolate_ptr) {
if (isolate_ptr == nullptr) {
return;
}
v8::Isolate* isolate = static_cast<v8::Isolate*>(isolate_ptr);
isolate->Dispose();
}
ValueTuple v8_Context_Run(ContextPtr ctxptr, const char* code, const char* filename) {
Context* ctx = static_cast<Context*>(ctxptr);
v8::Isolate* isolate = ctx->isolate;
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(ctx->ptr.Get(isolate));
v8::TryCatch try_catch(isolate);
try_catch.SetVerbose(false);
filename = filename ? filename : "(no file)";
ValueTuple res = { nullptr, 0, nullptr };
v8::Local<v8::Script> script = v8::Script::Compile(
v8::String::NewFromUtf8(isolate, code),
v8::String::NewFromUtf8(isolate, filename));
if (script.IsEmpty()) {
res.error_msg = DupString(report_exception(isolate, ctx->ptr.Get(isolate), try_catch));
return res;
}
v8::Local<v8::Value> result = script->Run();
if (result.IsEmpty()) {
res.error_msg = DupString(report_exception(isolate, ctx->ptr.Get(isolate), try_catch));
} else {
res.Value = static_cast<PersistentValuePtr>(new Value(isolate, result));
res.Kinds = v8_Value_KindsFromLocal(result);
}
return res;
}
void go_callback(const v8::FunctionCallbackInfo<v8::Value>& args);
PersistentValuePtr v8_Context_RegisterCallback(
ContextPtr ctxptr,
const char* name,
const char* id
) {
VALUE_SCOPE(ctxptr);
v8::Local<v8::FunctionTemplate> cb =
v8::FunctionTemplate::New(isolate, go_callback,
v8::String::NewFromUtf8(isolate, id));
cb->SetClassName(v8::String::NewFromUtf8(isolate, name));
return new Value(isolate, cb->GetFunction());
}
void go_callback(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* iso = args.GetIsolate();
v8::HandleScope scope(iso);
std::string id = str(args.Data());
std::string src_file, src_func;
int line_number = 0, column = 0;
v8::Local<v8::StackTrace> trace(v8::StackTrace::CurrentStackTrace(iso, 1));
if (trace->GetFrameCount() == 1) {
v8::Local<v8::StackFrame> frame(trace->GetFrame(0));
src_file = str(frame->GetScriptName());
src_func = str(frame->GetFunctionName());
line_number = frame->GetLineNumber();
column = frame->GetColumn();
}
int argc = args.Length();
ValueTuple argv[argc];
for (int i = 0; i < argc; i++) {
argv[i] = (ValueTuple){new Value(iso, args[i]), v8_Value_KindsFromLocal(args[i])};
}
ValueTuple result =
go_callback_handler(
(String){id.data(), int(id.length())},
(CallerInfo){
(String){src_func.data(), int(src_func.length())},
(String){src_file.data(), int(src_file.length())},
line_number,
column
},
argc, argv);
if (result.error_msg.ptr != nullptr) {
v8::Local<v8::Value> err = v8::Exception::Error(
v8::String::NewFromUtf8(iso, result.error_msg.ptr, v8::NewStringType::kNormal, result.error_msg.len).ToLocalChecked());
iso->ThrowException(err);
} else if (result.Value == NULL) {
args.GetReturnValue().Set(v8::Undefined(iso));
} else {
args.GetReturnValue().Set(*static_cast<Value*>(result.Value));
}
}
PersistentValuePtr v8_Context_Global(ContextPtr ctxptr) {
VALUE_SCOPE(ctxptr);
return new Value(isolate, ctx->Global());
}
void v8_Context_Release(ContextPtr ctxptr) {
if (ctxptr == nullptr) {
return;
}
Context* ctx = static_cast<Context*>(ctxptr);
ISOLATE_SCOPE(ctx->isolate);
ctx->ptr.Reset();
}
PersistentValuePtr v8_Context_Create(ContextPtr ctxptr, ImmediateValue val) {
VALUE_SCOPE(ctxptr);
switch (val.Type) {
case tARRAY: return new Value(isolate, v8::Array::New(isolate, val.Mem.len)); break;
case tARRAYBUFFER: {
v8::Local<v8::ArrayBuffer> buf = v8::ArrayBuffer::New(isolate, val.Mem.len);
memcpy(buf->GetContents().Data(), val.Mem.ptr, val.Mem.len);
return new Value(isolate, buf);
break;
}
case tBOOL: return new Value(isolate, v8::Boolean::New(isolate, val.Bool == 1)); break;
case tDATE: return new Value(isolate, v8::Date::New(isolate, val.Float64)); break;
case tFLOAT64: return new Value(isolate, v8::Number::New(isolate, val.Float64)); break;
// For now, this is converted to a double on entry.
// TODO(aroman) Consider using BigInt, but only if the V8 version supports
// it. Check to see what V8 versions support BigInt.
case tINT64: return new Value(isolate, v8::Number::New(isolate, double(val.Int64))); break;
case tOBJECT: return new Value(isolate, v8::Object::New(isolate)); break;
case tSTRING: {
return new Value(isolate, v8::String::NewFromUtf8(
isolate, val.Mem.ptr, v8::NewStringType::kNormal, val.Mem.len).ToLocalChecked());
break;
}
case tUNDEFINED: return new Value(isolate, v8::Undefined(isolate)); break;
}
return nullptr;
}
ValueTuple v8_Value_Get(ContextPtr ctxptr, PersistentValuePtr valueptr, const char* field) {
VALUE_SCOPE(ctxptr);
Value* value = static_cast<Value*>(valueptr);
v8::Local<v8::Value> maybeObject = value->Get(isolate);
if (!maybeObject->IsObject()) {
return (ValueTuple){nullptr, 0, DupString("Not an object")};
}
// We can safely call `ToLocalChecked`, because
// we've just created the local object above.
v8::Local<v8::Object> object = maybeObject->ToObject(ctx).ToLocalChecked();
v8::Local<v8::Value> localValue = object->Get(ctx, v8::String::NewFromUtf8(isolate, field)).ToLocalChecked();
return (ValueTuple){
new Value(isolate, localValue),
v8_Value_KindsFromLocal(localValue),
nullptr,
};
}
ValueTuple v8_Value_GetIdx(ContextPtr ctxptr, PersistentValuePtr valueptr, int idx) {
VALUE_SCOPE(ctxptr);
Value* value = static_cast<Value*>(valueptr);
v8::Local<v8::Value> maybeObject = value->Get(isolate);
if (!maybeObject->IsObject()) {
return (ValueTuple){nullptr, 0, DupString("Not an object")};
}
v8::Local<v8::Value> obj;
if (maybeObject->IsArrayBuffer()) {
v8::ArrayBuffer* bufPtr = v8::ArrayBuffer::Cast(*maybeObject);
if (idx < bufPtr->GetContents().ByteLength()) {
obj = v8::Number::New(isolate, ((unsigned char*)bufPtr->GetContents().Data())[idx]);
} else {
obj = v8::Undefined(isolate);
}
} else {
// We can safely call `ToLocalChecked`, because
// we've just created the local object above.
v8::Local<v8::Object> object = maybeObject->ToObject(ctx).ToLocalChecked();
obj = object->Get(ctx, uint32_t(idx)).ToLocalChecked();
}
return (ValueTuple){new Value(isolate, obj), v8_Value_KindsFromLocal(obj), nullptr};
}
Error v8_Value_Set(ContextPtr ctxptr, PersistentValuePtr valueptr,
const char* field, PersistentValuePtr new_valueptr) {
VALUE_SCOPE(ctxptr);
Value* value = static_cast<Value*>(valueptr);
v8::Local<v8::Value> maybeObject = value->Get(isolate);
if (!maybeObject->IsObject()) {
return DupString("Not an object");
}
// We can safely call `ToLocalChecked`, because
// we've just created the local object above.
v8::Local<v8::Object> object =
maybeObject->ToObject(ctx).ToLocalChecked();
Value* new_value = static_cast<Value*>(new_valueptr);
v8::Local<v8::Value> new_value_local = new_value->Get(isolate);
v8::Maybe<bool> res =
object->Set(ctx, v8::String::NewFromUtf8(isolate, field), new_value_local);
if (res.IsNothing()) {
return DupString("Something went wrong -- set returned nothing.");
} else if (!res.FromJust()) {
return DupString("Something went wrong -- set failed.");
}
return (Error){nullptr, 0};
}
Error v8_Value_SetIdx(ContextPtr ctxptr, PersistentValuePtr valueptr,
int idx, PersistentValuePtr new_valueptr) {
VALUE_SCOPE(ctxptr);
Value* value = static_cast<Value*>(valueptr);
v8::Local<v8::Value> maybeObject = value->Get(isolate);
if (!maybeObject->IsObject()) {
return DupString("Not an object");
}
Value* new_value = static_cast<Value*>(new_valueptr);
v8::Local<v8::Value> new_value_local = new_value->Get(isolate);
if (maybeObject->IsArrayBuffer()) {
v8::ArrayBuffer* bufPtr = v8::ArrayBuffer::Cast(*maybeObject);
if (!new_value_local->IsNumber()) {
return DupString("Cannot assign non-number into array buffer");
} else if (idx >= bufPtr->GetContents().ByteLength()) {
return DupString("Cannot assign to an index beyond the size of an array buffer");
} else {
((unsigned char*)bufPtr->GetContents().Data())[idx] = new_value_local->ToNumber(ctx).ToLocalChecked()->Value();
}
} else {
// We can safely call `ToLocalChecked`, because
// we've just created the local object above.
v8::Local<v8::Object> object = maybeObject->ToObject(ctx).ToLocalChecked();
v8::Maybe<bool> res = object->Set(ctx, uint32_t(idx), new_value_local);
if (res.IsNothing()) {
return DupString("Something went wrong -- set returned nothing.");
} else if (!res.FromJust()) {
return DupString("Something went wrong -- set failed.");
}
}
return (Error){nullptr, 0};
}
ValueTuple v8_Value_Call(ContextPtr ctxptr,
PersistentValuePtr funcptr,
PersistentValuePtr selfptr,
int argc, PersistentValuePtr* argvptr) {
VALUE_SCOPE(ctxptr);
v8::TryCatch try_catch(isolate);
try_catch.SetVerbose(false);
v8::Local<v8::Value> func_val = static_cast<Value*>(funcptr)->Get(isolate);
if (!func_val->IsFunction()) {
return (ValueTuple){nullptr, 0, DupString("Not a function")};
}
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(func_val);
v8::Local<v8::Value> self;
if (selfptr == nullptr) {
self = ctx->Global();
} else {
self = static_cast<Value*>(selfptr)->Get(isolate);
}
v8::Local<v8::Value>* argv = new v8::Local<v8::Value>[argc];
for (int i = 0; i < argc; i++) {
argv[i] = static_cast<Value*>(argvptr[i])->Get(isolate);
}
v8::MaybeLocal<v8::Value> result = func->Call(ctx, self, argc, argv);
delete[] argv;
if (result.IsEmpty()) {
return (ValueTuple){nullptr, 0, DupString(report_exception(isolate, ctx, try_catch))};
}
v8::Local<v8::Value> value = result.ToLocalChecked();
return (ValueTuple){
static_cast<PersistentValuePtr>(new Value(isolate, value)),
v8_Value_KindsFromLocal(value),
nullptr
};
}
ValueTuple v8_Value_New(ContextPtr ctxptr,
PersistentValuePtr funcptr,
int argc, PersistentValuePtr* argvptr) {
VALUE_SCOPE(ctxptr);
v8::TryCatch try_catch(isolate);
try_catch.SetVerbose(false);
v8::Local<v8::Value> func_val = static_cast<Value*>(funcptr)->Get(isolate);
if (!func_val->IsFunction()) {
return (ValueTuple){nullptr, 0, DupString("Not a function")};
}
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(func_val);
v8::Local<v8::Value>* argv = new v8::Local<v8::Value>[argc];
for (int i = 0; i < argc; i++) {
argv[i] = static_cast<Value*>(argvptr[i])->Get(isolate);
}
v8::MaybeLocal<v8::Object> result = func->NewInstance(ctx, argc, argv);
delete[] argv;
if (result.IsEmpty()) {
return (ValueTuple){nullptr, 0, DupString(report_exception(isolate, ctx, try_catch))};
}
v8::Local<v8::Value> value = result.ToLocalChecked();
return (ValueTuple){
static_cast<PersistentValuePtr>(new Value(isolate, value)),
v8_Value_KindsFromLocal(value),
nullptr
};
}
void v8_Value_Release(ContextPtr ctxptr, PersistentValuePtr valueptr) {
if (valueptr == nullptr || ctxptr == nullptr) {
return;
}
ISOLATE_SCOPE(static_cast<Context*>(ctxptr)->isolate);
Value* value = static_cast<Value*>(valueptr);
value->Reset();
delete value;
}
String v8_Value_String(ContextPtr ctxptr, PersistentValuePtr valueptr) {
VALUE_SCOPE(ctxptr);
v8::Local<v8::Value> value = static_cast<Value*>(valueptr)->Get(isolate);
return DupString(value->ToString());
}
double v8_Value_Float64(ContextPtr ctxptr, PersistentValuePtr valueptr) {
VALUE_SCOPE(ctxptr);
v8::Local<v8::Value> value = static_cast<Value*>(valueptr)->Get(isolate);
v8::Maybe<double> val = value->NumberValue(ctx);
if (val.IsNothing()) {
return 0;
}
return val.ToChecked();
}
int64_t v8_Value_Int64(ContextPtr ctxptr, PersistentValuePtr valueptr) {
VALUE_SCOPE(ctxptr);
v8::Local<v8::Value> value = static_cast<Value*>(valueptr)->Get(isolate);
v8::Maybe<int64_t> val = value->IntegerValue(ctx);
if (val.IsNothing()) {
return 0;
}
return val.ToChecked();
}
int v8_Value_Bool(ContextPtr ctxptr, PersistentValuePtr valueptr) {
VALUE_SCOPE(ctxptr);
v8::Local<v8::Value> value = static_cast<Value*>(valueptr)->Get(isolate);
v8::Maybe<bool> val = value->BooleanValue(ctx);
if (val.IsNothing()) {
return 0;
}
return val.ToChecked() ? 1 : 0;
}
ByteArray v8_Value_Bytes(ContextPtr ctxptr, PersistentValuePtr valueptr) {
VALUE_SCOPE(ctxptr);
v8::Local<v8::Value> value = static_cast<Value*>(valueptr)->Get(isolate);
v8::ArrayBuffer* bufPtr;
if (value->IsTypedArray()) {
bufPtr = *v8::TypedArray::Cast(*value)->Buffer();
} else if (value->IsArrayBuffer()) {
bufPtr = v8::ArrayBuffer::Cast(*value);
} else {
return (ByteArray){ nullptr, 0 };
}
if (bufPtr == NULL) {
return (ByteArray){ nullptr, 0 };
}
return (ByteArray){
static_cast<const char*>(bufPtr->GetContents().Data()),
static_cast<int>(bufPtr->GetContents().ByteLength()),
};
}
HeapStatistics v8_Isolate_GetHeapStatistics(IsolatePtr isolate_ptr) {
if (isolate_ptr == nullptr) {
return HeapStatistics{0};
}
ISOLATE_SCOPE(static_cast<v8::Isolate*>(isolate_ptr));
v8::HeapStatistics hs;
isolate->GetHeapStatistics(&hs);
return HeapStatistics{
hs.total_heap_size(),
hs.total_heap_size_executable(),
hs.total_physical_size(),
hs.total_available_size(),
hs.used_heap_size(),
hs.heap_size_limit(),
hs.malloced_memory(),
hs.peak_malloced_memory(),
hs.does_zap_garbage()
};
}
void v8_Isolate_LowMemoryNotification(IsolatePtr isolate_ptr) {
if (isolate_ptr == nullptr) {
return;
}
ISOLATE_SCOPE(static_cast<v8::Isolate*>(isolate_ptr));
isolate->LowMemoryNotification();
}
ValueTuple v8_Value_PromiseInfo(ContextPtr ctxptr, PersistentValuePtr valueptr,
int* promise_state) {
VALUE_SCOPE(ctxptr);
v8::Local<v8::Value> value = static_cast<Value*>(valueptr)->Get(isolate);
if (!value->IsPromise()) { // just in case
return (ValueTuple){nullptr, 0, DupString("Not a promise")};
}
v8::Promise* prom = v8::Promise::Cast(*value);
*promise_state = prom->State();
if (prom->State() == v8::Promise::PromiseState::kPending) {
return (ValueTuple){nullptr, 0, nullptr};
}
v8::Local<v8::Value> res = prom->Result();
return (ValueTuple){new Value(isolate, res), v8_Value_KindsFromLocal(res), nullptr};
}
} // extern "C"