forked from just-js/just
-
Notifications
You must be signed in to change notification settings - Fork 0
/
just.cc
455 lines (429 loc) · 16.3 KB
/
just.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
#include "just.h"
std::map<std::string, just::builtin*> just::builtins;
std::map<std::string, just::register_plugin> just::modules;
uint32_t scriptId = 1;
ssize_t just::process_memory_usage() {
char buf[1024];
const char* s = NULL;
ssize_t n = 0;
unsigned long val = 0;
int fd = 0;
int i = 0;
do {
fd = open("/proc/thread-self/stat", O_RDONLY);
} while (fd == -1 && errno == EINTR);
if (fd == -1) return (ssize_t)errno;
do
n = read(fd, buf, sizeof(buf) - 1);
while (n == -1 && errno == EINTR);
close(fd);
if (n == -1)
return (ssize_t)errno;
buf[n] = '\0';
s = strchr(buf, ' ');
if (s == NULL)
goto err;
s += 1;
if (*s != '(')
goto err;
s = strchr(s, ')');
if (s == NULL)
goto err;
for (i = 1; i <= 22; i++) {
s = strchr(s + 1, ' ');
if (s == NULL)
goto err;
}
errno = 0;
val = strtoul(s, NULL, 10);
if (errno != 0)
goto err;
return val * (unsigned long)getpagesize();
err:
return 0;
}
void just::builtins_add (const char* name, const char* source,
unsigned int size) {
struct builtin* b = new builtin();
b->size = size;
b->source = source;
builtins[name] = b;
}
void just::SET_METHOD(Isolate *isolate, Local<ObjectTemplate>
recv, const char *name, FunctionCallback callback) {
recv->Set(String::NewFromUtf8(isolate, name,
NewStringType::kNormal).ToLocalChecked(),
FunctionTemplate::New(isolate, callback));
}
void just::SET_MODULE(Isolate *isolate, Local<ObjectTemplate>
recv, const char *name, Local<ObjectTemplate> module) {
recv->Set(String::NewFromUtf8(isolate, name,
NewStringType::kNormal).ToLocalChecked(),
module);
}
void just::SET_VALUE(Isolate *isolate, Local<ObjectTemplate>
recv, const char *name, Local<Value> value) {
recv->Set(String::NewFromUtf8(isolate, name,
NewStringType::kNormal).ToLocalChecked(),
value);
}
void just::PrintStackTrace(Isolate* isolate, const TryCatch& try_catch) {
HandleScope handleScope(isolate);
Local<Value> exception = try_catch.Exception();
Local<Message> message = try_catch.Message();
Local<StackTrace> stack = message->GetStackTrace();
String::Utf8Value ex(isolate, exception);
Local<Value> scriptName = message->GetScriptResourceName();
String::Utf8Value scriptname(isolate, scriptName);
Local<Context> context = isolate->GetCurrentContext();
int linenum = message->GetLineNumber(context).FromJust();
fprintf(stderr, "%s in %s on line %i\n", *ex, *scriptname, linenum);
if (stack.IsEmpty()) return;
for (int i = 0; i < stack->GetFrameCount(); i++) {
Local<StackFrame> stack_frame = stack->GetFrame(isolate, i);
Local<String> functionName = stack_frame->GetFunctionName();
Local<String> scriptName = stack_frame->GetScriptName();
String::Utf8Value fn_name_s(isolate, functionName);
String::Utf8Value script_name(isolate, scriptName);
const int line_number = stack_frame->GetLineNumber();
const int column = stack_frame->GetColumn();
if (stack_frame->IsEval()) {
if (stack_frame->GetScriptId() == Message::kNoScriptIdInfo) {
fprintf(stderr, " at [eval]:%i:%i\n", line_number, column);
} else {
fprintf(stderr, " at [eval] (%s:%i:%i)\n", *script_name,
line_number, column);
}
break;
}
if (fn_name_s.length() == 0) {
fprintf(stderr, " at %s:%i:%i\n", *script_name, line_number, column);
} else {
fprintf(stderr, " at %s (%s:%i:%i)\n", *fn_name_s, *script_name,
line_number, column);
}
}
fflush(stderr);
}
v8::MaybeLocal<v8::Module> just::OnModuleInstantiate(Local<Context> context,
Local<String> specifier, Local<Module> referrer) {
return MaybeLocal<Module>();
}
void just::PromiseRejectCallback(PromiseRejectMessage data) {
if (data.GetEvent() == v8::kPromiseRejectAfterResolved ||
data.GetEvent() == v8::kPromiseResolveAfterResolved) {
// Ignore reject/resolve after resolved.
return;
}
Local<Promise> promise = data.GetPromise();
Isolate* isolate = promise->GetIsolate();
if (data.GetEvent() == v8::kPromiseHandlerAddedAfterReject) {
return;
}
Local<Value> exception = data.GetValue();
v8::Local<Message> message;
// Assume that all objects are stack-traces.
if (exception->IsObject()) {
message = v8::Exception::CreateMessage(isolate, exception);
}
if (!exception->IsNativeError() &&
(message.IsEmpty() || message->GetStackTrace().IsEmpty())) {
// If there is no real Error object, manually create a stack trace.
exception = v8::Exception::Error(
v8::String::NewFromUtf8Literal(isolate, "Unhandled Promise."));
message = Exception::CreateMessage(isolate, exception);
}
Local<Context> context = isolate->GetCurrentContext();
TryCatch try_catch(isolate);
Local<Object> globalInstance = context->Global();
Local<Value> func = globalInstance->Get(context,
String::NewFromUtf8Literal(isolate, "onUnhandledRejection",
NewStringType::kNormal)).ToLocalChecked();
if (func.IsEmpty()) {
return;
}
Local<Function> onUnhandledRejection = Local<Function>::Cast(func);
if (try_catch.HasCaught()) {
fprintf(stderr, "PromiseRejectCallback: Cast\n");
return;
}
Local<Value> argv[1] = { exception };
MaybeLocal<Value> result = onUnhandledRejection->Call(context,
globalInstance, 1, argv);
if (result.IsEmpty() && try_catch.HasCaught()) {
fprintf(stderr, "PromiseRejectCallback: Call\n");
}
}
void just::FreeMemory(void* buf, size_t length, void* data) {
free(buf);
}
int just::CreateIsolate(int argc, char** argv,
const char* main_src, unsigned int main_len,
const char* js, unsigned int js_len, struct iovec* buf, int fd) {
Isolate::CreateParams create_params;
int statusCode = 0;
create_params.array_buffer_allocator =
ArrayBuffer::Allocator::NewDefaultAllocator();
Isolate *isolate = Isolate::New(create_params);
{
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
// TODO: make this a config option
isolate->SetCaptureStackTraceForUncaughtExceptions(true, 1000,
StackTrace::kDetailed);
Local<ObjectTemplate> global = ObjectTemplate::New(isolate);
Local<ObjectTemplate> just = ObjectTemplate::New(isolate);
just::Init(isolate, just);
global->Set(String::NewFromUtf8Literal(isolate, "just",
NewStringType::kNormal), just);
Local<Context> context = Context::New(isolate, NULL, global);
Context::Scope context_scope(context);
// TODO: make this a config option
context->AllowCodeGenerationFromStrings(false);
isolate->SetPromiseRejectCallback(PromiseRejectCallback);
Local<Array> arguments = Array::New(isolate);
for (int i = 0; i < argc; i++) {
arguments->Set(context, i, String::NewFromUtf8(isolate, argv[i],
NewStringType::kNormal, strlen(argv[i])).ToLocalChecked()).Check();
}
Local<Object> globalInstance = context->Global();
globalInstance->Set(context, String::NewFromUtf8Literal(isolate,
"global",
NewStringType::kNormal), globalInstance).Check();
Local<Value> obj = globalInstance->Get(context,
String::NewFromUtf8Literal(
isolate, "just",
NewStringType::kNormal)).ToLocalChecked();
Local<Object> justInstance = Local<Object>::Cast(obj);
if (buf != NULL) {
std::unique_ptr<BackingStore> backing = SharedArrayBuffer::NewBackingStore(
buf->iov_base, buf->iov_len, [](void*, size_t, void*){}, nullptr);
Local<SharedArrayBuffer> ab = SharedArrayBuffer::New(isolate, std::move(backing));
justInstance->Set(context, String::NewFromUtf8Literal(isolate,
"buffer", NewStringType::kNormal), ab).Check();
}
if (fd != 0) {
justInstance->Set(context, String::NewFromUtf8Literal(isolate, "fd",
NewStringType::kNormal),
Integer::New(isolate, fd)).Check();
}
justInstance->Set(context, String::NewFromUtf8Literal(isolate, "args",
NewStringType::kNormal), arguments).Check();
if (js_len > 0) {
justInstance->Set(context, String::NewFromUtf8Literal(isolate,
"workerSource", NewStringType::kNormal),
String::NewFromUtf8(isolate, js, NewStringType::kNormal,
js_len).ToLocalChecked()).Check();
}
TryCatch try_catch(isolate);
ScriptOrigin baseorigin(
String::NewFromUtf8Literal(isolate, "just.js",
NewStringType::kInternalized), // resource name
Integer::New(isolate, 0), // line offset
Integer::New(isolate, 0), // column offset
False(isolate), // is shared cross-origin
Integer::New(isolate, scriptId++), // script id
Local<Value>(), // source map url
False(isolate), // is opaque
False(isolate), // is wasm
False(isolate) // is module
);
Local<Script> script;
Local<String> base;
base = String::NewFromUtf8(isolate, main_src, NewStringType::kNormal,
main_len).ToLocalChecked();
ScriptCompiler::Source basescript(base, baseorigin);
if (!ScriptCompiler::Compile(context, &basescript).ToLocal(&script)) {
PrintStackTrace(isolate, try_catch);
return 1;
}
MaybeLocal<Value> maybe_result = script->Run(context);
Local<Value> result;
if (!maybe_result.ToLocal(&result)) {
just::PrintStackTrace(isolate, try_catch);
return 2;
}
Local<Value> func = globalInstance->Get(context,
String::NewFromUtf8Literal(isolate, "onExit",
NewStringType::kNormal)).ToLocalChecked();
if (func->IsFunction()) {
Local<Function> onExit = Local<Function>::Cast(func);
Local<Value> argv[1] = {Integer::New(isolate, 0)};
MaybeLocal<Value> result = onExit->Call(context, globalInstance, 0, argv);
if (!result.IsEmpty()) {
statusCode = result.ToLocalChecked()->Uint32Value(context).ToChecked();
}
if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
just::PrintStackTrace(isolate, try_catch);
return 2;
}
statusCode = result.ToLocalChecked()->Uint32Value(context).ToChecked();
}
}
isolate->ContextDisposedNotification();
isolate->LowMemoryNotification();
isolate->ClearKeptObjects();
bool stop = false;
while(!stop) {
stop = isolate->IdleNotificationDeadline(1);
}
isolate->Dispose();
delete create_params.array_buffer_allocator;
isolate = nullptr;
return statusCode;
}
int just::CreateIsolate(int argc, char** argv, const char* main_src,
unsigned int main_len) {
return CreateIsolate(argc, argv, main_src, main_len, NULL, 0, NULL, 0);
}
void just::Print(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
if (args[0].IsEmpty()) return;
String::Utf8Value str(args.GetIsolate(), args[0]);
int endline = 1;
if (args.Length() > 1) {
endline = static_cast<int>(args[1]->BooleanValue(isolate));
}
const char *cstr = *str;
if (endline == 1) {
fprintf(stdout, "%s\n", cstr);
} else {
fprintf(stdout, "%s", cstr);
}
}
void just::Error(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
if (args[0].IsEmpty()) return;
String::Utf8Value str(args.GetIsolate(), args[0]);
int endline = 1;
if (args.Length() > 1) {
endline = static_cast<int>(args[1]->BooleanValue(isolate));
}
const char *cstr = *str;
if (endline == 1) {
fprintf(stderr, "%s\n", cstr);
} else {
fprintf(stderr, "%s", cstr);
}
}
void just::Load(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<ObjectTemplate> exports = ObjectTemplate::New(isolate);
if (args[0]->IsString()) {
String::Utf8Value name(isolate, args[0]);
auto iter = just::modules.find(*name);
if (iter == just::modules.end()) {
return;
} else {
register_plugin _init = (*iter->second);
auto _register = reinterpret_cast<InitializerCallback>(_init());
_register(isolate, exports);
}
} else {
Local<BigInt> address64 = Local<BigInt>::Cast(args[0]);
void* ptr = reinterpret_cast<void*>(address64->Uint64Value());
register_plugin _init = reinterpret_cast<register_plugin>(ptr);
auto _register = reinterpret_cast<InitializerCallback>(_init());
_register(isolate, exports);
}
args.GetReturnValue().Set(exports->NewInstance(context).ToLocalChecked());
}
void just::Builtin(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
String::Utf8Value name(isolate, args[0]);
just::builtin* b = builtins[*name];
if (b == nullptr) {
args.GetReturnValue().Set(Null(isolate));
return;
}
if (args.Length() == 1) {
args.GetReturnValue().Set(String::NewFromUtf8(isolate, b->source,
NewStringType::kNormal, b->size).ToLocalChecked());
return;
}
std::unique_ptr<BackingStore> backing = SharedArrayBuffer::NewBackingStore(
(void*)b->source, b->size, [](void*, size_t, void*){}, nullptr);
Local<SharedArrayBuffer> ab = SharedArrayBuffer::New(isolate, std::move(backing));
//Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, (void*)b->source, b->size, v8::ArrayBufferCreationMode::kExternalized);
args.GetReturnValue().Set(ab);
}
void just::Sleep(const FunctionCallbackInfo<Value> &args) {
sleep(Local<Integer>::Cast(args[0])->Value());
}
void just::MemoryUsage(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
ssize_t rss = just::process_memory_usage();
HeapStatistics v8_heap_stats;
isolate->GetHeapStatistics(&v8_heap_stats);
Local<BigUint64Array> array;
Local<ArrayBuffer> ab;
if (args.Length() > 0) {
array = args[0].As<BigUint64Array>();
ab = array->Buffer();
} else {
ab = ArrayBuffer::New(isolate, 16 * 8);
array = BigUint64Array::New(ab, 0, 16);
}
std::shared_ptr<BackingStore> backing = ab->GetBackingStore();
uint64_t *fields = static_cast<uint64_t *>(backing->Data());
fields[0] = rss;
fields[1] = v8_heap_stats.total_heap_size();
fields[2] = v8_heap_stats.used_heap_size();
fields[3] = v8_heap_stats.external_memory();
fields[4] = v8_heap_stats.does_zap_garbage();
fields[5] = v8_heap_stats.heap_size_limit();
fields[6] = v8_heap_stats.malloced_memory();
fields[7] = v8_heap_stats.number_of_detached_contexts();
fields[8] = v8_heap_stats.number_of_native_contexts();
fields[9] = v8_heap_stats.peak_malloced_memory();
fields[10] = v8_heap_stats.total_available_size();
fields[11] = v8_heap_stats.total_heap_size_executable();
fields[12] = v8_heap_stats.total_physical_size();
fields[13] = isolate->AdjustAmountOfExternalAllocatedMemory(0);
args.GetReturnValue().Set(array);
}
void just::Exit(const FunctionCallbackInfo<Value>& args) {
exit(Local<Integer>::Cast(args[0])->Value());
}
void just::PID(const FunctionCallbackInfo<Value> &args) {
args.GetReturnValue().Set(Integer::New(args.GetIsolate(), getpid()));
}
void just::Chdir(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
String::Utf8Value path(isolate, args[0]);
args.GetReturnValue().Set(Integer::New(isolate, chdir(*path)));
}
void just::Init(Isolate* isolate, Local<ObjectTemplate> target) {
Local<ObjectTemplate> version = ObjectTemplate::New(isolate);
SET_VALUE(isolate, version, "just", String::NewFromUtf8Literal(isolate,
JUST_VERSION));
SET_VALUE(isolate, version, "v8", String::NewFromUtf8(isolate,
v8::V8::GetVersion()).ToLocalChecked());
Local<ObjectTemplate> kernel = ObjectTemplate::New(isolate);
utsname kernel_rec;
int rc = uname(&kernel_rec);
if (rc == 0) {
kernel->Set(String::NewFromUtf8Literal(isolate, "os",
NewStringType::kNormal), String::NewFromUtf8(isolate,
kernel_rec.sysname).ToLocalChecked());
kernel->Set(String::NewFromUtf8Literal(isolate, "release",
NewStringType::kNormal), String::NewFromUtf8(isolate,
kernel_rec.release).ToLocalChecked());
kernel->Set(String::NewFromUtf8Literal(isolate, "version",
NewStringType::kNormal), String::NewFromUtf8(isolate,
kernel_rec.version).ToLocalChecked());
}
version->Set(String::NewFromUtf8Literal(isolate, "kernel",
NewStringType::kNormal), kernel);
SET_METHOD(isolate, target, "print", Print);
SET_METHOD(isolate, target, "error", Error);
SET_METHOD(isolate, target, "load", Load);
SET_METHOD(isolate, target, "exit", Exit);
SET_METHOD(isolate, target, "pid", PID);
SET_METHOD(isolate, target, "chdir", Chdir);
SET_METHOD(isolate, target, "sleep", Sleep);
SET_METHOD(isolate, target, "builtin", Builtin);
SET_METHOD(isolate, target, "memoryUsage", MemoryUsage);
SET_MODULE(isolate, target, "version", version);
}