This repository has been archived by the owner on Oct 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
binding.cc
468 lines (375 loc) · 14.1 KB
/
binding.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
/*
Copyright 2018 Ryan Dahl <[email protected]>. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <map>
#include "binding.h"
#include "libplatform/libplatform.h"
#include "v8.h"
#include "_cgo_export.h"
using namespace v8;
struct worker_s {
int table_index;
Isolate* isolate;
std::string last_exception;
Persistent<Function> recv;
Persistent<Context> context;
std::map<std::string, Eternal<Module>> modules;
};
// Extracts a C string from a V8 Utf8Value.
const char* ToCString(const String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
/*
bool AbortOnUncaughtExceptionCallback(Isolate* isolate) {
return true;
}
void MessageCallback2(Local<Message> message, Local<Value> data) {
printf("MessageCallback2\n\n");
}
void FatalErrorCallback2(const char* location, const char* message) {
printf("FatalErrorCallback2\n");
}
*/
void ExitOnPromiseRejectCallback(PromiseRejectMessage promise_reject_message) {
auto isolate = Isolate::GetCurrent();
worker* w = (worker*)isolate->GetData(0);
assert(w->isolate == isolate);
HandleScope handle_scope(w->isolate);
auto context = w->context.Get(w->isolate);
auto exception = promise_reject_message.GetValue();
auto message = Exception::CreateMessage(isolate, exception);
auto onerrorStr = String::NewFromUtf8(w->isolate, "onerror");
auto onerror = context->Global()->Get(onerrorStr);
if (onerror->IsFunction()) {
Local<Function> func = Local<Function>::Cast(onerror);
Local<Value> args[5];
auto origin = message->GetScriptOrigin();
args[0] = exception->ToString();
args[1] = message->GetScriptResourceName();
args[2] = origin.ResourceLineOffset();
args[3] = origin.ResourceColumnOffset();
args[4] = exception;
func->Call(context->Global(), 5, args);
/* message, source, lineno, colno, error */
} else {
printf("Unhandled Promise\n");
message->PrintCurrentStackTrace(isolate, stdout);
}
exit(1);
}
MaybeLocal<Module> ResolveCallback(Local<Context> context,
Local<String> specifier,
Local<Module> referrer) {
auto isolate = Isolate::GetCurrent();
worker* w = (worker*)isolate->GetData(0);
HandleScope handle_scope(isolate);
String::Utf8Value str(specifier);
const char* moduleName = *str;
if (w->modules.count(moduleName) == 0) {
std::string out;
out.append("Module (");
out.append(moduleName);
out.append(") has not been loaded");
out.append("\n");
w->last_exception = out;
return MaybeLocal<Module>();
}
return w->modules[moduleName].Get(isolate);
}
// Exception details will be appended to the first argument.
std::string ExceptionString(worker* w, TryCatch* try_catch) {
std::string out;
size_t scratchSize = 20;
char scratch[scratchSize]; // just some scratch space for sprintf
HandleScope handle_scope(w->isolate);
Local<Context> context = w->context.Get(w->isolate);
String::Utf8Value exception(try_catch->Exception());
const char* exception_string = ToCString(exception);
Handle<Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
out.append(exception_string);
out.append("\n");
} else {
// Print (filename):(line number)
String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
const char* filename_string = ToCString(filename);
int linenum = message->GetLineNumber();
snprintf(scratch, scratchSize, "%i", linenum);
out.append(filename_string);
out.append(":");
out.append(scratch);
out.append("\n");
// Print line of source code.
String::Utf8Value sourceline(message->GetSourceLine());
const char* sourceline_string = ToCString(sourceline);
out.append(sourceline_string);
out.append("\n");
// Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn(context).FromJust();
for (int i = 0; i < start; i++) {
out.append(" ");
}
int end = message->GetEndColumn(context).FromJust();
for (int i = start; i < end; i++) {
out.append("^");
}
out.append("\n");
String::Utf8Value stack_trace(try_catch->StackTrace());
if (stack_trace.length() > 0) {
const char* stack_trace_string = ToCString(stack_trace);
out.append(stack_trace_string);
out.append("\n");
} else {
out.append(exception_string);
out.append("\n");
}
}
return out;
}
extern "C" {
const char* worker_version() { return V8::GetVersion(); }
void worker_set_flags(int* argc, char** argv) {
V8::SetFlagsFromCommandLine(argc, argv, true);
}
const char* worker_last_exception(worker* w) {
return w->last_exception.c_str();
}
int worker_load(worker* w, char* name_s, char* source_s) {
Locker locker(w->isolate);
Isolate::Scope isolate_scope(w->isolate);
HandleScope handle_scope(w->isolate);
Local<Context> context = Local<Context>::New(w->isolate, w->context);
Context::Scope context_scope(context);
TryCatch try_catch(w->isolate);
Local<String> name = String::NewFromUtf8(w->isolate, name_s);
Local<String> source = String::NewFromUtf8(w->isolate, source_s);
ScriptOrigin origin(name);
Local<Script> script = Script::Compile(source, &origin);
if (script.IsEmpty()) {
assert(try_catch.HasCaught());
w->last_exception = ExceptionString(w, &try_catch);
return 1;
}
Handle<Value> result = script->Run();
if (result.IsEmpty()) {
assert(try_catch.HasCaught());
w->last_exception = ExceptionString(w, &try_catch);
return 2;
}
return 0;
}
int worker_load_module(worker* w, char* name_s, char* source_s, int callback_index) {
Locker locker(w->isolate);
Isolate::Scope isolate_scope(w->isolate);
HandleScope handle_scope(w->isolate);
Local<Context> context = Local<Context>::New(w->isolate, w->context);
Context::Scope context_scope(context);
TryCatch try_catch(w->isolate);
Local<String> name = String::NewFromUtf8(w->isolate, name_s);
Local<String> source_text = String::NewFromUtf8(w->isolate, source_s);
Local<Integer> line_offset = Integer::New(w->isolate, 0);
Local<Integer> column_offset = Integer::New(w->isolate, 0);
Local<Boolean> is_cross_origin = True(w->isolate);
Local<Integer> script_id = Local<Integer>();
Local<Value> source_map_url = Local<Value>();
Local<Boolean> is_opaque = False(w->isolate);
Local<Boolean> is_wasm = False(w->isolate);
Local<Boolean> is_module = True(w->isolate);
ScriptOrigin origin(name, line_offset, column_offset, is_cross_origin,
script_id, source_map_url, is_opaque, is_wasm, is_module);
ScriptCompiler::Source source(source_text, origin);
Local<Module> module;
if (!ScriptCompiler::CompileModule(w->isolate, &source).ToLocal(&module)) {
assert(try_catch.HasCaught());
w->last_exception = ExceptionString(w, &try_catch);
return 1;
}
for (int i = 0; i < module->GetModuleRequestsLength(); i++) {
Local<String> dependency = module->GetModuleRequest(i);
String::Utf8Value str(dependency);
char* dependencySpecifier = *str;
// If we've already loaded the module, skip resolving it.
// TODO: Is there ever a time when the specifier would be the same
// but would need to be resolved again?
if (w->modules.count(dependencySpecifier) != 0) {
continue;
}
int ret = ResolveModule(dependencySpecifier, name_s, callback_index);
if (ret != 0) {
// TODO: Use module->GetModuleRequestLocation() to get source locations
std::string out;
out.append("Module (");
out.append(dependencySpecifier);
out.append(") has not been loaded");
out.append("\n");
w->last_exception = out;
return ret;
}
}
Eternal<Module> persModule(w->isolate, module);
w->modules[name_s] = persModule;
Maybe<bool> ok = module->InstantiateModule(context, ResolveCallback);
if (!ok.FromMaybe(false)) {
// TODO: I'm not sure if this is needed
if (try_catch.HasCaught()) {
assert(try_catch.HasCaught());
w->last_exception = ExceptionString(w, &try_catch);
}
return 2;
}
MaybeLocal<Value> result = module->Evaluate(context);
if (result.IsEmpty()) {
assert(try_catch.HasCaught());
w->last_exception = ExceptionString(w, &try_catch);
return 2;
}
return 0;
}
void Print(const FunctionCallbackInfo<Value>& args) {
bool first = true;
for (int i = 0; i < args.Length(); i++) {
HandleScope handle_scope(args.GetIsolate());
if (first) {
first = false;
} else {
printf(" ");
}
String::Utf8Value str(args[i]);
const char* cstr = ToCString(str);
printf("%s", cstr);
}
printf("\n");
fflush(stdout);
}
// Sets the recv callback.
void Recv(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
worker* w = (worker*)isolate->GetData(0);
assert(w->isolate == isolate);
HandleScope handle_scope(isolate);
auto context = w->context.Get(w->isolate);
Local<Value> v = args[0];
assert(v->IsFunction());
Local<Function> func = Local<Function>::Cast(v);
w->recv.Reset(isolate, func);
}
// Called from JavaScript, routes message to golang.
void Send(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
worker* w = static_cast<worker*>(isolate->GetData(0));
assert(w->isolate == isolate);
Locker locker(w->isolate);
EscapableHandleScope handle_scope(isolate);
auto context = w->context.Get(w->isolate);
Local<Value> v = args[0];
assert(v->IsArrayBuffer());
auto ab = Local<ArrayBuffer>::Cast(v);
auto contents = ab->GetContents();
void* buf = contents.Data();
int buflen = static_cast<int>(contents.ByteLength());
auto retbuf = recvCb(buf, buflen, w->table_index);
if (retbuf.data) {
auto ab = ArrayBuffer::New(w->isolate, retbuf.data, retbuf.len,
ArrayBufferCreationMode::kInternalized);
/*
// I'm slightly worried the above ArrayBuffer construction leaks memory
// the following might be a safer way to do it.
auto ab = ArrayBuffer::New(w->isolate, retbuf.len);
auto contents = ab->GetContents();
memcpy(contents.Data(), retbuf.data, retbuf.len);
free(retbuf.data);
*/
args.GetReturnValue().Set(handle_scope.Escape(ab));
}
}
// Called from golang. Must route message to javascript lang.
// non-zero return value indicates error. check worker_last_exception().
int worker_send_bytes(worker* w, void* data, size_t len) {
Locker locker(w->isolate);
Isolate::Scope isolate_scope(w->isolate);
HandleScope handle_scope(w->isolate);
auto context = w->context.Get(w->isolate);
TryCatch try_catch(w->isolate);
Local<Function> recv = Local<Function>::New(w->isolate, w->recv);
if (recv.IsEmpty()) {
w->last_exception = "V8Worker2.recv has not been called.";
return 1;
}
Local<Value> args[1];
args[0] = ArrayBuffer::New(w->isolate, data, len,
ArrayBufferCreationMode::kInternalized);
assert(!args[0].IsEmpty());
assert(!try_catch.HasCaught());
recv->Call(context->Global(), 1, args);
if (try_catch.HasCaught()) {
w->last_exception = ExceptionString(w, &try_catch);
return 2;
}
return 0;
}
void v8_init() {
Platform* platform = platform::CreateDefaultPlatform();
V8::InitializePlatform(platform);
V8::Initialize();
}
worker* worker_new(int table_index) {
worker* w = new (worker);
Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
ArrayBuffer::Allocator::NewDefaultAllocator();
Isolate* isolate = Isolate::New(create_params);
Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
w->isolate = isolate;
// Leaving this code here because it will probably be useful later on, but
// disabling it now as I haven't got tests for the desired behavior.
// w->isolate->SetCaptureStackTraceForUncaughtExceptions(true);
// w->isolate->SetAbortOnUncaughtExceptionCallback(AbortOnUncaughtExceptionCallback);
// w->isolate->AddMessageListener(MessageCallback2);
// w->isolate->SetFatalErrorHandler(FatalErrorCallback2);
w->isolate->SetPromiseRejectCallback(ExitOnPromiseRejectCallback);
w->isolate->SetData(0, w);
w->table_index = table_index;
Local<ObjectTemplate> global = ObjectTemplate::New(w->isolate);
Local<ObjectTemplate> v8worker2 = ObjectTemplate::New(w->isolate);
global->Set(String::NewFromUtf8(w->isolate, "V8Worker2"), v8worker2);
v8worker2->Set(String::NewFromUtf8(w->isolate, "print"),
FunctionTemplate::New(w->isolate, Print));
v8worker2->Set(String::NewFromUtf8(w->isolate, "recv"),
FunctionTemplate::New(w->isolate, Recv));
v8worker2->Set(String::NewFromUtf8(w->isolate, "send"),
FunctionTemplate::New(w->isolate, Send));
Local<Context> context = Context::New(w->isolate, NULL, global);
w->context.Reset(w->isolate, context);
context->Enter();
return w;
}
void worker_dispose(worker* w) {
w->isolate->Dispose();
delete (w);
}
void worker_terminate_execution(worker* w) { w->isolate->TerminateExecution(); }
}