forked from ry/v8worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinding.cc
283 lines (220 loc) · 7.25 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
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string>
#include "v8.h"
#include "libplatform/libplatform.h"
#include "binding.h"
using namespace v8;
struct worker_s {
int x;
void* data;
worker_recv_cb cb;
Isolate* isolate;
std::string last_exception;
Persistent<Function> recv;
Persistent<Context> context;
};
// Extracts a C string from a V8 Utf8Value.
const char* ToCString(const String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
class ArrayBufferAllocator : public ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
virtual void Free(void* data, size_t) { free(data); }
};
// Exception details will be appended to the first argument.
std::string ExceptionString(Isolate* isolate, TryCatch* try_catch) {
std::string out;
size_t scratchSize = 20;
char scratch[scratchSize]; // just some scratch space for sprintf
HandleScope handle_scope(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();
for (int i = 0; i < start; i++) {
out.append(" ");
}
int end = message->GetEndColumn();
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" {
#include "_cgo_export.h"
void go_recv_cb(const char* msg, void* data) {
recvCb((char*)msg, data);
}
const char* worker_version() {
return V8::GetVersion();
}
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;
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->isolate, &try_catch);
return 1;
}
Handle<Value> result = script->Run();
if (result.IsEmpty()) {
assert(try_catch.HasCaught());
w->last_exception = ExceptionString(w->isolate, &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);
Local<Context> context = Local<Context>::New(w->isolate, w->context);
Context::Scope context_scope(context);
Local<Value> v = args[0];
assert(v->IsFunction());
Local<Function> func = Local<Function>::Cast(v);
w->recv.Reset(isolate, func);
}
// Called from javascript. Must route message to golang.
void Send(const FunctionCallbackInfo<Value>& args) {
std::string msg;
worker* w = NULL;
{
Isolate* isolate = args.GetIsolate();
w = static_cast<worker*>(isolate->GetData(0));
assert(w->isolate == isolate);
Locker locker(w->isolate);
HandleScope handle_scope(isolate);
Local<Context> context = Local<Context>::New(w->isolate, w->context);
Context::Scope context_scope(context);
Local<Value> v = args[0];
assert(v->IsString());
String::Utf8Value str(v);
msg = ToCString(str);
}
// XXX should we use Unlocker?
w->cb(msg.c_str(), w->data);
}
// Called from golang. Must route message to javascript lang.
// non-zero return value indicates error. check worker_last_exception().
int worker_send(worker* w, const char* msg) {
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;
Local<Function> recv = Local<Function>::New(w->isolate, w->recv);
if (recv.IsEmpty()) {
w->last_exception = "$recv not called";
return 1;
}
Local<Value> args[1];
args[0] = String::NewFromUtf8(w->isolate, msg);
assert(!try_catch.HasCaught());
recv->Call(context->Global(), 1, args);
if (try_catch.HasCaught()) {
w->last_exception = ExceptionString(w->isolate, &try_catch);
return 2;
}
return 0;
}
static ArrayBufferAllocator array_buffer_allocator;
void v8_init() {
V8::Initialize();
Platform* platform = platform::CreateDefaultPlatform();
V8::InitializePlatform(platform);
V8::SetArrayBufferAllocator(&array_buffer_allocator);
}
worker* worker_new(worker_recv_cb cb, void* data) {
Isolate* isolate = Isolate::New();
Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
worker* w = new(worker);
w->isolate = isolate;
w->isolate->SetCaptureStackTraceForUncaughtExceptions(true);
w->isolate->SetData(0, w);
w->data = data;
w->cb = cb;
Local<ObjectTemplate> global = ObjectTemplate::New(w->isolate);
global->Set(String::NewFromUtf8(w->isolate, "$print"),
FunctionTemplate::New(w->isolate, Print));
global->Set(String::NewFromUtf8(w->isolate, "$recv"),
FunctionTemplate::New(w->isolate, Recv));
global->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;
}
}