2018-06-09 18:32:04 -04:00
|
|
|
/*
|
|
|
|
Copyright 2018 Ryan Dahl <ry@tinyclouds.org>. 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 "v8/include/libplatform/libplatform.h"
|
|
|
|
#include "v8/include/v8.h"
|
|
|
|
|
2018-06-10 08:18:15 -04:00
|
|
|
#include "./deno_internal.h"
|
2018-06-09 22:55:31 -04:00
|
|
|
#include "include/deno.h"
|
2018-06-09 18:32:04 -04:00
|
|
|
|
|
|
|
#define CHECK(x) assert(x) // TODO(ry) use V8's CHECK.
|
|
|
|
|
2018-06-09 20:24:34 -04:00
|
|
|
namespace deno {
|
|
|
|
|
2018-06-09 18:32:04 -04:00
|
|
|
// Extracts a C string from a v8::V8 Utf8Value.
|
|
|
|
const char* ToCString(const v8::String::Utf8Value& value) {
|
|
|
|
return *value ? *value : "<string conversion failed>";
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline v8::Local<v8::String> v8_str(const char* x) {
|
|
|
|
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x,
|
|
|
|
v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exits the process.
|
2018-06-10 00:13:48 -04:00
|
|
|
void HandleException(v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Value> exception) {
|
|
|
|
auto isolate = context->GetIsolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2018-06-09 18:32:04 -04:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
2018-06-10 00:13:48 -04:00
|
|
|
auto message = v8::Exception::CreateMessage(isolate, exception);
|
|
|
|
auto onerrorStr = v8::String::NewFromUtf8(isolate, "onerror");
|
2018-06-09 18:32:04 -04:00
|
|
|
auto onerror = context->Global()->Get(onerrorStr);
|
|
|
|
|
|
|
|
if (onerror->IsFunction()) {
|
|
|
|
auto func = v8::Local<v8::Function>::Cast(onerror);
|
|
|
|
v8::Local<v8::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 {
|
2018-06-10 00:13:48 -04:00
|
|
|
v8::String::Utf8Value exceptionStr(isolate, exception);
|
2018-06-09 18:32:04 -04:00
|
|
|
printf("Unhandled Exception %s\n", ToCString(exceptionStr));
|
2018-06-10 00:13:48 -04:00
|
|
|
message->PrintCurrentStackTrace(isolate, stdout);
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
bool AbortOnUncaughtExceptionCallback(v8::Isolate* isolate) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageCallback2(Local<Message> message, v8::Local<v8::Value> data) {
|
|
|
|
printf("MessageCallback2\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void FatalErrorCallback2(const char* location, const char* message) {
|
|
|
|
printf("FatalErrorCallback2\n");
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ExitOnPromiseRejectCallback(
|
|
|
|
v8::PromiseRejectMessage promise_reject_message) {
|
|
|
|
auto* isolate = v8::Isolate::GetCurrent();
|
|
|
|
Deno* d = static_cast<Deno*>(isolate->GetData(0));
|
|
|
|
assert(d->isolate == isolate);
|
|
|
|
v8::HandleScope handle_scope(d->isolate);
|
|
|
|
auto exception = promise_reject_message.GetValue();
|
2018-06-10 00:13:48 -04:00
|
|
|
auto context = d->context.Get(d->isolate);
|
|
|
|
HandleException(context, exception);
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2018-06-09 20:02:40 -04:00
|
|
|
assert(args.Length() == 1);
|
2018-06-09 18:32:04 -04:00
|
|
|
auto* isolate = args.GetIsolate();
|
2018-06-09 20:02:40 -04:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::String::Utf8Value str(isolate, args[0]);
|
|
|
|
const char* cstr = ToCString(str);
|
|
|
|
printf("%s\n", cstr);
|
2018-06-09 18:32:04 -04:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the recv callback.
|
|
|
|
void Recv(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
v8::Isolate* isolate = args.GetIsolate();
|
|
|
|
Deno* d = reinterpret_cast<Deno*>(isolate->GetData(0));
|
|
|
|
assert(d->isolate == isolate);
|
|
|
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
|
|
|
|
v8::Local<v8::Value> v = args[0];
|
|
|
|
assert(v->IsFunction());
|
|
|
|
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(v);
|
|
|
|
|
|
|
|
d->recv.Reset(isolate, func);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from JavaScript, routes message to golang.
|
|
|
|
void Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
v8::Isolate* isolate = args.GetIsolate();
|
|
|
|
Deno* d = static_cast<Deno*>(isolate->GetData(0));
|
|
|
|
assert(d->isolate == isolate);
|
|
|
|
|
|
|
|
v8::Locker locker(d->isolate);
|
|
|
|
v8::EscapableHandleScope handle_scope(isolate);
|
|
|
|
|
|
|
|
v8::Local<v8::Value> v = args[0];
|
|
|
|
assert(v->IsArrayBuffer());
|
|
|
|
|
|
|
|
auto ab = v8::Local<v8::ArrayBuffer>::Cast(v);
|
|
|
|
auto contents = ab->GetContents();
|
|
|
|
|
|
|
|
void* buf = contents.Data();
|
|
|
|
int buflen = static_cast<int>(contents.ByteLength());
|
|
|
|
|
2018-06-10 08:18:15 -04:00
|
|
|
auto retbuf = d->cb(d, deno_buf{buf, buflen});
|
2018-06-09 18:32:04 -04:00
|
|
|
if (retbuf.data) {
|
|
|
|
auto ab = v8::ArrayBuffer::New(d->isolate, retbuf.data, retbuf.len,
|
|
|
|
v8::ArrayBufferCreationMode::kInternalized);
|
|
|
|
/*
|
|
|
|
// I'm slightly worried the above v8::ArrayBuffer construction leaks memory
|
|
|
|
// the following might be a safer way to do it.
|
|
|
|
auto ab = v8::ArrayBuffer::New(d->isolate, retbuf.len);
|
|
|
|
auto contents = ab->GetContents();
|
|
|
|
memcpy(contents.Data(), retbuf.data, retbuf.len);
|
|
|
|
free(retbuf.data);
|
|
|
|
*/
|
|
|
|
args.GetReturnValue().Set(handle_scope.Escape(ab));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 08:18:15 -04:00
|
|
|
bool Load(v8::Local<v8::Context> context, const char* name_s,
|
2018-06-10 00:13:48 -04:00
|
|
|
const char* source_s) {
|
|
|
|
auto isolate = context->GetIsolate();
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2018-06-09 18:32:04 -04:00
|
|
|
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
2018-06-10 00:13:48 -04:00
|
|
|
v8::TryCatch try_catch(isolate);
|
2018-06-09 18:32:04 -04:00
|
|
|
|
|
|
|
auto name = v8_str(name_s);
|
|
|
|
auto source = v8_str(source_s);
|
|
|
|
|
|
|
|
v8::ScriptOrigin origin(name);
|
|
|
|
|
|
|
|
auto script = v8::Script::Compile(context, source, &origin);
|
|
|
|
|
|
|
|
if (script.IsEmpty()) {
|
|
|
|
assert(try_catch.HasCaught());
|
2018-06-10 00:13:48 -04:00
|
|
|
HandleException(context, try_catch.Exception());
|
2018-06-09 18:32:04 -04:00
|
|
|
assert(false);
|
2018-06-10 00:13:48 -04:00
|
|
|
return false;
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
auto result = script.ToLocalChecked()->Run(context);
|
|
|
|
|
|
|
|
if (result.IsEmpty()) {
|
|
|
|
assert(try_catch.HasCaught());
|
2018-06-10 00:13:48 -04:00
|
|
|
HandleException(context, try_catch.Exception());
|
2018-06-09 18:32:04 -04:00
|
|
|
assert(false);
|
2018-06-10 00:13:48 -04:00
|
|
|
return false;
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
2018-06-10 00:13:48 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-10 08:18:15 -04:00
|
|
|
v8::StartupData MakeSnapshot(v8::StartupData* prev_natives_blob,
|
|
|
|
v8::StartupData* prev_snapshot_blob,
|
|
|
|
const char* js_filename, const char* js_source) {
|
|
|
|
v8::V8::SetNativesDataBlob(prev_natives_blob);
|
|
|
|
v8::V8::SetSnapshotDataBlob(prev_snapshot_blob);
|
|
|
|
|
|
|
|
auto creator = new v8::SnapshotCreator(external_references);
|
|
|
|
auto* isolate = creator->GetIsolate();
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
auto context = v8::Context::New(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
auto global = context->Global();
|
|
|
|
|
|
|
|
auto print_tmpl = v8::FunctionTemplate::New(isolate, Print);
|
|
|
|
auto print_val = print_tmpl->GetFunction(context).ToLocalChecked();
|
|
|
|
CHECK(
|
|
|
|
global->Set(context, deno::v8_str("deno_print"), print_val).FromJust());
|
|
|
|
|
|
|
|
auto recv_tmpl = v8::FunctionTemplate::New(isolate, Recv);
|
|
|
|
auto recv_val = recv_tmpl->GetFunction(context).ToLocalChecked();
|
|
|
|
CHECK(global->Set(context, deno::v8_str("deno_recv"), recv_val).FromJust());
|
|
|
|
|
|
|
|
auto send_tmpl = v8::FunctionTemplate::New(isolate, Send);
|
|
|
|
auto send_val = send_tmpl->GetFunction(context).ToLocalChecked();
|
|
|
|
CHECK(global->Set(context, deno::v8_str("deno_send"), send_val).FromJust());
|
|
|
|
|
|
|
|
bool r = Load(context, js_filename, js_source);
|
|
|
|
assert(r);
|
|
|
|
|
|
|
|
creator->SetDefaultContext(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto snapshot_blob =
|
|
|
|
creator->CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kKeep);
|
|
|
|
|
|
|
|
return snapshot_blob;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddIsolate(Deno* d, v8::Isolate* isolate) {
|
|
|
|
d->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.
|
|
|
|
// d->isolate->SetCaptureStackTraceForUncaughtExceptions(true);
|
|
|
|
// d->isolate->SetAbortOnUncaughtExceptionCallback(AbortOnUncaughtExceptionCallback);
|
|
|
|
// d->isolate->AddMessageListener(MessageCallback2);
|
|
|
|
// d->isolate->SetFatalErrorHandler(FatalErrorCallback2);
|
|
|
|
d->isolate->SetPromiseRejectCallback(deno::ExitOnPromiseRejectCallback);
|
|
|
|
d->isolate->SetData(0, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace deno
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
void deno_init() {
|
|
|
|
// v8::V8::InitializeICUDefaultLocation(argv[0]);
|
|
|
|
// v8::V8::InitializeExternalStartupData(argv[0]);
|
|
|
|
auto p = v8::platform::CreateDefaultPlatform();
|
|
|
|
v8::V8::InitializePlatform(p);
|
|
|
|
v8::V8::Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* v8_version() { return v8::V8::GetVersion(); }
|
|
|
|
|
|
|
|
void v8_set_flags(int* argc, char** argv) {
|
|
|
|
v8::V8::SetFlagsFromCommandLine(argc, argv, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* deno_last_exception(Deno* d) { return d->last_exception.c_str(); }
|
|
|
|
|
2018-06-10 00:13:48 -04:00
|
|
|
int deno_load(Deno* d, const char* name_s, const char* source_s) {
|
|
|
|
auto isolate = d->isolate;
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
auto context = d->context.Get(d->isolate);
|
2018-06-10 08:18:15 -04:00
|
|
|
return deno::Load(context, name_s, source_s) ? 0 : 1;
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called from golang. Must route message to javascript lang.
|
|
|
|
// non-zero return value indicates error. check deno_last_exception().
|
2018-06-10 08:18:15 -04:00
|
|
|
int deno_send(Deno* d, deno_buf buf) {
|
2018-06-09 18:32:04 -04:00
|
|
|
v8::Locker locker(d->isolate);
|
|
|
|
v8::Isolate::Scope isolate_scope(d->isolate);
|
|
|
|
v8::HandleScope handle_scope(d->isolate);
|
|
|
|
|
|
|
|
auto context = d->context.Get(d->isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
v8::TryCatch try_catch(d->isolate);
|
|
|
|
|
|
|
|
v8::Local<v8::Function> recv =
|
|
|
|
v8::Local<v8::Function>::New(d->isolate, d->recv);
|
|
|
|
if (recv.IsEmpty()) {
|
|
|
|
d->last_exception = "V8Deno2.recv has not been called.";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Value> args[1];
|
|
|
|
args[0] = v8::ArrayBuffer::New(d->isolate, buf.data, buf.len,
|
|
|
|
v8::ArrayBufferCreationMode::kInternalized);
|
|
|
|
assert(!args[0].IsEmpty());
|
|
|
|
assert(!try_catch.HasCaught());
|
|
|
|
|
|
|
|
recv->Call(context->Global(), 1, args);
|
|
|
|
|
|
|
|
if (try_catch.HasCaught()) {
|
2018-06-10 08:18:15 -04:00
|
|
|
deno::HandleException(context, try_catch.Exception());
|
2018-06-09 18:32:04 -04:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void deno_dispose(Deno* d) {
|
|
|
|
d->isolate->Dispose();
|
|
|
|
delete (d);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deno_terminate_execution(Deno* d) { d->isolate->TerminateExecution(); }
|
2018-06-09 20:24:34 -04:00
|
|
|
|
2018-06-10 08:18:15 -04:00
|
|
|
} // extern "C"
|