-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcommon.cc
More file actions
550 lines (502 loc) · 20.1 KB
/
common.cc
File metadata and controls
550 lines (502 loc) · 20.1 KB
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
#include "common.h"
#include <functional>
#include <iostream>
#include <regex>
#define SKCONTEXT void*
namespace skbinding {
using v8::Array;
using v8::Boolean;
using v8::Context;
using v8::Exception;
using v8::External;
using v8::Function;
using v8::FunctionCallback;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Number;
using v8::Object;
using v8::ObjectTemplate;
using v8::Persistent;
using v8::PropertyDescriptor;
using v8::String;
using v8::Symbol;
using v8::TryCatch;
using v8::Value;
extern "C" {
char* sk_get_exception_message(SKError skExn);
void sk_string_check_c_safe(char* str);
char* SkipRuntime_getExceptionStack(SKError error);
char* SkipRuntime_getExceptionType(SKError error);
SKObstack SKIP_new_Obstack();
void SKIP_destroy_Obstack(SKObstack obstack);
}
Local<String> FromUtf8(Isolate* isolate, const char* str) {
return String::NewFromUtf8(isolate, str).ToLocalChecked();
}
void AddFunction(Isolate* isolate, Local<Object> handler, const char* name,
FunctionCallback callback) {
Local<Context> context = isolate->GetCurrentContext();
Local<FunctionTemplate> v8Tpl = FunctionTemplate::New(isolate, callback);
Local<Function> v8Fn = v8Tpl->GetFunction(context).ToLocalChecked();
handler->Set(context, FromUtf8(isolate, name), v8Fn).FromJust();
}
Local<Value> CallGlobalStaticMethod(Isolate* isolate, const char* className,
const char* methodName, int argc,
Local<Value> argv[]) {
Local<Context> context = isolate->GetCurrentContext();
Local<Object> global = context->Global();
Local<Object> classRef = global->Get(context, FromUtf8(isolate, className))
.ToLocalChecked()
.As<Object>();
Local<Function> method = classRef->Get(context, FromUtf8(isolate, methodName))
.ToLocalChecked()
.As<Function>();
return method->Call(context, classRef, argc, argv).ToLocalChecked();
}
void Print(Isolate* isolate, const char* prefix, Local<Value> value) {
v8::String::Utf8Value jsValue(isolate, value);
const char* val = *jsValue;
std::cout << prefix << ": " << val << std::endl;
}
Local<Value> JSONStringify(Isolate* isolate, Local<Value> value) {
Local<Value> argv[1] = {value};
return CallGlobalStaticMethod(isolate, "JSON", "stringify", 1, argv);
}
extern "C" {
[[noreturn]] void SkipRuntime_throwExternalException(char*, char*, char*);
char* sk_string_create(const char* buffer, uint32_t size);
}
void* SKTryCatch(Isolate* isolate, Local<Function> fn, Local<Value> recv,
int argc, Local<Value> argv[],
std::function<void*(Isolate*, Local<Value>)> success,
std::function<void(Isolate*)> failure) {
Local<Context> context = isolate->GetCurrentContext();
TryCatch tryCatch(isolate);
MaybeLocal<Value> optResult = fn->Call(context, recv, argc, argv);
if (!tryCatch.HasCaught()) {
return success(isolate, optResult.ToLocalChecked());
} else {
failure(isolate);
Local<Value> exception = tryCatch.Exception();
if (exception->IsNativeError()) {
Local<Context> context = isolate->GetCurrentContext();
Local<Object> jsObj = exception->ToObject(context).ToLocalChecked();
char* sktype = ToSKString(isolate, jsObj->GetConstructorName());
char* skmessage = ToSKString(
isolate,
jsObj->Get(context, FromUtf8(isolate, "message")).ToLocalChecked());
char* skstack = ToSKString(
isolate,
jsObj->Get(context, FromUtf8(isolate, "stack")).ToLocalChecked());
SkipRuntime_throwExternalException(sktype, skmessage, skstack);
} else if (exception->IsObject()) {
MaybeLocal<Value> message =
exception.As<Object>()->Get(context, FromUtf8(isolate, "message"));
char* skempty = sk_string_create("", 0);
char* skmessage;
if (!message.IsEmpty()) {
skmessage = ToSKString(isolate, message.ToLocalChecked());
} else {
Local<Value> jsmessage = JSONStringify(isolate, exception);
skmessage = ToSKString(isolate, jsmessage);
}
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
} else if (exception->IsString()) {
char* skempty = sk_string_create("", 0);
char* skmessage = ToSKString(isolate, exception);
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
} else {
char* skempty = sk_string_create("", 0);
Local<Value> jsmessage = JSONStringify(isolate, exception);
char* skmessage = ToSKString(isolate, jsmessage);
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
}
return nullptr;
}
}
void SKTryCatchVoid(Isolate* isolate, Local<Function> fn, Local<Value> recv,
int argc, Local<Value> argv[],
std::function<void(Isolate*)> success,
std::function<void(Isolate*)> failure) {
Local<Context> context = isolate->GetCurrentContext();
TryCatch tryCatch(isolate);
auto result = fn->Call(context, recv, argc, argv);
(void)result;
if (!tryCatch.HasCaught()) {
success(isolate);
} else {
failure(isolate);
Local<Value> exception = tryCatch.Exception();
if (exception->IsNativeError()) {
Local<Context> context = isolate->GetCurrentContext();
Local<Object> jsObj = exception->ToObject(context).ToLocalChecked();
char* sktype = ToSKString(isolate, jsObj->GetConstructorName());
char* skmessage = ToSKString(
isolate,
jsObj->Get(context, FromUtf8(isolate, "message")).ToLocalChecked());
char* skstack = ToSKString(
isolate,
jsObj->Get(context, FromUtf8(isolate, "stack")).ToLocalChecked());
SkipRuntime_throwExternalException(sktype, skmessage, skstack);
} else if (exception->IsObject()) {
MaybeLocal<Value> message =
exception.As<Object>()->Get(context, FromUtf8(isolate, "message"));
char* skempty = sk_string_create("", 0);
char* skmessage;
if (!message.IsEmpty()) {
skmessage = ToSKString(isolate, message.ToLocalChecked());
} else {
Local<Value> jsmessage = JSONStringify(isolate, exception);
skmessage = ToSKString(isolate, jsmessage);
}
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
} else if (exception->IsString()) {
char* skempty = sk_string_create("", 0);
char* skmessage = ToSKString(isolate, exception);
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
} else {
char* skempty = sk_string_create("", 0);
Local<Value> jsmessage = JSONStringify(isolate, exception);
char* skmessage = ToSKString(isolate, jsmessage);
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
}
}
}
bool JSCheckFunction(Isolate* isolate, Local<Object> binding,
const char* name) {
Local<Context> context = isolate->GetCurrentContext();
MaybeLocal<Value> optValue = binding->Get(context, FromUtf8(isolate, name));
if (optValue.IsEmpty()) {
std::ostringstream error;
error << "Missing function"
<< ": " << name;
isolate->ThrowException(
Exception::Error(FromUtf8(isolate, error.str().c_str())));
return false;
}
Local<Value> value = optValue.ToLocalChecked();
if (!value->IsFunction()) {
std::ostringstream error;
error << name << " Is not a function";
isolate->ThrowException(
Exception::Error(FromUtf8(isolate, error.str().c_str())));
return false;
}
return true;
}
Local<Function> CheckFunction(Isolate* isolate, Local<Object> binding,
const char* name) {
Local<Context> context = isolate->GetCurrentContext();
MaybeLocal<Value> optValue = binding->Get(context, FromUtf8(isolate, name));
if (optValue.IsEmpty()) {
char* skempty = sk_string_create("", 0);
std::ostringstream error;
error << "Undefined function"
<< ": " << name;
std::string strerror(error.str());
char* skmessage = sk_string_create(strerror.c_str(), strerror.size());
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
}
Local<Value> value = optValue.ToLocalChecked();
if (!value->IsFunction()) {
char* skempty = sk_string_create("", 0);
std::ostringstream error;
error << "Invalid function"
<< ": " << name;
std::string strerror(error.str());
char* skmessage = sk_string_create(strerror.c_str(), strerror.size());
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
}
return value.As<Function>();
}
void CallJSVoidFunction(Isolate* isolate, Local<Object> binding,
const char* name, int argc, Local<Value> argv[]) {
Local<Function> function = CheckFunction(isolate, binding, name);
SKTryCatchVoid(
isolate, function, binding, argc, argv, [](Isolate* _i) {},
[](Isolate* _i) {});
}
void* CallJSFunction(Isolate* isolate, Local<Object> binding, const char* name,
int argc, Local<Value> argv[]) {
Local<Function> function = CheckFunction(isolate, binding, name);
return SKTryCatch(
isolate, function, binding, argc, argv,
[](Isolate* i, Local<Value> result) {
if (!result->IsExternal()) {
const char* message = "Invalid function return type";
char* skempty = sk_string_create("", 0);
char* skmessage = sk_string_create(message, strlen(message));
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
}
return result.As<External>()->Value();
},
[](Isolate* _i) {});
}
void* CallJSNullableFunction(Isolate* isolate, Local<Object> binding,
const char* name, int argc, Local<Value> argv[]) {
Local<Function> function = CheckFunction(isolate, binding, name);
return SKTryCatch(
isolate, function, binding, argc, argv,
[](Isolate* _i, Local<Value> result) {
if (!result->IsExternal() && !result->IsNull()) {
const char* message = "Invalid function return type";
char* skempty = sk_string_create("", 0);
char* skmessage = sk_string_create(message, strlen(message));
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
}
if (result->IsExternal()) {
return result.As<External>()->Value();
}
return (void*)nullptr;
},
[](Isolate* _i) {});
}
double CallJSNumberFunction(Isolate* isolate, Local<Object> binding,
const char* name, int argc, Local<Value> argv[]) {
Local<Function> function = CheckFunction(isolate, binding, name);
double returnValue = 0.0;
SKTryCatch(
isolate, function, binding, argc, argv,
[&returnValue](Isolate* _i, Local<Value> result) {
if (!result->IsNumber()) {
const char* message = "Invalid function return type";
char* skempty = sk_string_create("", 0);
char* skmessage = sk_string_create(message, strlen(message));
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
}
returnValue = result.As<Number>()->Value();
return nullptr;
},
[](Isolate* _i) {});
return returnValue;
}
char* CallJSStringFunction(Isolate* isolate, Local<Object> binding,
const char* name, int argc, Local<Value> argv[]) {
Local<Function> function = CheckFunction(isolate, binding, name);
return (char*)SKTryCatch(
isolate, function, binding, argc, argv,
[](Isolate* isolate, Local<Value> result) {
if (!result->IsString()) {
const char* message = "Invalid function return type";
char* skempty = sk_string_create("", 0);
char* skmessage = sk_string_create(message, strlen(message));
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
}
String::Utf8Value v8Str(isolate, result.As<String>());
std::string cppStr(*v8Str);
return sk_string_create(cppStr.c_str(), cppStr.size());
},
[](Isolate* _i) {});
}
char* CallJSNullableStringFunction(Isolate* isolate, Local<Object> binding,
const char* name, int argc,
Local<Value> argv[]) {
Local<Function> function = CheckFunction(isolate, binding, name);
return (char*)SKTryCatch(
isolate, function, binding, argc, argv,
[](Isolate* isolate, Local<Value> result) {
if (!result->IsString() && !result->IsNull()) {
const char* message = "Invalid function return type";
char* skempty = sk_string_create("", 0);
char* skmessage = sk_string_create(message, strlen(message));
SkipRuntime_throwExternalException(skempty, skmessage, skempty);
}
if (result->IsNull()) {
return (char*)nullptr;
}
String::Utf8Value v8Str(isolate, result.As<String>());
std::string cppStr(*v8Str);
return sk_string_create(cppStr.c_str(), cppStr.size());
},
[](Isolate* _i) {});
}
void NatTryCatch(Isolate* isolate, std::function<void(Isolate*)> run) {
try {
run(isolate);
} catch (skbinding::SkipException& e) {
std::ostringstream error;
error << e.name() << ": " << e.what();
Local<Value> v8Error =
Exception::Error(FromUtf8(isolate, error.str().c_str()));
if (v8Error->IsNativeError()) {
char* stack = SkipRuntime_getExceptionStack(e.m_skipException);
if (stack != nullptr && strlen(stack) > 0) {
sk_string_check_c_safe(stack);
Local<Context> context = isolate->GetCurrentContext();
Local<Object> exc = v8Error.As<Object>();
exc->Set(context, FromUtf8(isolate, "stack"), FromUtf8(isolate, stack))
.FromJust();
}
}
isolate->ThrowException(v8Error);
} catch (std::exception& e) {
const char* what = e.what();
isolate->ThrowException(Exception::Error(FromUtf8(isolate, what)));
}
}
void RunWithGC(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
if (args.Length() != 1) {
isolate->ThrowException(
Exception::TypeError(FromUtf8(isolate, "Must have one parameters.")));
return;
};
if (!args[0]->IsFunction()) {
isolate->ThrowException(Exception::TypeError(
FromUtf8(isolate, "The parameter must be a function.")));
return;
}
SKObstack obstack = SKIP_new_Obstack();
Local<Context> context = isolate->GetCurrentContext();
TryCatch tryCatch(isolate);
MaybeLocal<Value> optResult =
args[0].As<Function>()->Call(context, Null(isolate), 0, nullptr);
if (!tryCatch.HasCaught()) {
args.GetReturnValue().Set(optResult.ToLocalChecked());
} else {
tryCatch.ReThrow();
}
SKIP_destroy_Obstack(obstack);
}
void UnsafeAsyncRunWithGC(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
if (args.Length() != 1) {
isolate->ThrowException(
Exception::TypeError(FromUtf8(isolate, "Must have one parameters.")));
return;
};
if (!args[0]->IsFunction()) {
isolate->ThrowException(Exception::TypeError(
FromUtf8(isolate, "The parameter must be a function.")));
return;
}
SKObstack obstack = SKIP_new_Obstack();
Local<Context> context = isolate->GetCurrentContext();
TryCatch tryCatch(isolate);
MaybeLocal<Value> optResult =
args[0].As<Function>()->Call(context, Null(isolate), 0, nullptr);
if (tryCatch.HasCaught()) {
SKIP_destroy_Obstack(obstack);
tryCatch.ReThrow();
return;
}
Local<Value> result = optResult.ToLocalChecked();
if (result->IsPromise()) {
Local<v8::Promise> promise = result.As<v8::Promise>();
Local<External> obstackData = External::New(isolate, obstack);
Local<v8::Promise::Resolver> resolver =
v8::Promise::Resolver::New(context).ToLocalChecked();
Local<v8::Promise> wrapperPromise = resolver->GetPromise();
// Create handler data array with obstack and resolver
Local<Array> handlerData = Array::New(isolate, 2);
handlerData->Set(context, 0, obstackData).ToChecked();
handlerData->Set(context, 1, resolver).ToChecked();
// Create success handler that cleans up and resolves
Local<Function> onFulfilled =
Function::New(
context,
[](const v8::FunctionCallbackInfo<v8::Value>& info) {
Isolate* isolate = info.GetIsolate();
HandleScope scope(isolate);
Local<Context> context = isolate->GetCurrentContext();
Local<Array> data = info.Data().As<Array>();
SKObstack obstack = data->Get(context, 0)
.ToLocalChecked()
.As<External>()
->Value();
Local<v8::Promise::Resolver> resolver =
data->Get(context, 1)
.ToLocalChecked()
.As<v8::Promise::Resolver>();
SKIP_destroy_Obstack(obstack);
Local<Value> value = info[0];
resolver->Resolve(context, value).ToChecked();
},
handlerData)
.ToLocalChecked();
Local<Function> onRejected =
Function::New(
context,
[](const v8::FunctionCallbackInfo<v8::Value>& info) {
Isolate* isolate = info.GetIsolate();
HandleScope scope(isolate);
Local<Context> context = isolate->GetCurrentContext();
Local<Array> data = info.Data().As<Array>();
SKObstack obstack = data->Get(context, 0)
.ToLocalChecked()
.As<External>()
->Value();
Local<v8::Promise::Resolver> resolver =
data->Get(context, 1)
.ToLocalChecked()
.As<v8::Promise::Resolver>();
SKIP_destroy_Obstack(obstack);
Local<Value> error = info[0];
resolver->Reject(context, error).ToChecked();
},
handlerData)
.ToLocalChecked();
promise->Then(context, onFulfilled, onRejected).ToLocalChecked();
args.GetReturnValue().Set(wrapperPromise);
} else {
SKIP_destroy_Obstack(obstack);
args.GetReturnValue().Set(result);
}
}
void GetErrorObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
if (args.Length() != 1) {
isolate->ThrowException(
Exception::TypeError(FromUtf8(isolate, "Must have one parameters.")));
return;
};
if (!args[0]->IsExternal()) {
isolate->ThrowException(Exception::TypeError(
FromUtf8(isolate, "The parameter must be a pointer.")));
return;
}
SKError skExn = args[0].As<External>()->Value();
std::ostringstream error;
char* type = SkipRuntime_getExceptionType(skExn);
sk_string_check_c_safe(type);
if (strlen(type) > 0) {
error << type << ": ";
}
error << (char*)sk_get_exception_message(skExn);
Local<Value> v8Error =
Exception::Error(FromUtf8(isolate, error.str().c_str()));
if (v8Error->IsNativeError()) {
char* stack = SkipRuntime_getExceptionStack(skExn);
if (stack != nullptr && strlen(stack) > 0) {
sk_string_check_c_safe(stack);
Local<Context> context = isolate->GetCurrentContext();
Local<Object> exc = v8Error.As<Object>();
exc->Set(context, FromUtf8(isolate, "stack"), FromUtf8(isolate, stack))
.FromJust();
}
}
args.GetReturnValue().Set(v8Error);
}
char* ToSKString(Isolate* isolate, Local<Value> value) {
String::Utf8Value jsValue(isolate, value);
const char* strValue = *jsValue;
return sk_string_create(strValue, strlen(strValue));
}
const char* SkipException::what() const noexcept {
char* str = (char*)sk_get_exception_message(m_skipException);
sk_string_check_c_safe(str);
return str;
}
const char* SkipException::name() const noexcept {
char* str = (char*)SkipRuntime_getExceptionType(m_skipException);
sk_string_check_c_safe(str);
return str;
}
} // namespace skbinding