2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-06-09 18:32:04 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-09-28 15:55:06 -04:00
|
|
|
#include <iostream>
|
2018-06-09 18:32:04 -04:00
|
|
|
#include <string>
|
|
|
|
|
2018-07-03 04:15:32 -04:00
|
|
|
#include "third_party/v8/include/v8.h"
|
|
|
|
#include "third_party/v8/src/base/logging.h"
|
2018-06-09 18:32:04 -04:00
|
|
|
|
2018-07-06 03:19:19 -04:00
|
|
|
#include "deno.h"
|
2018-07-06 15:00:45 -04:00
|
|
|
#include "internal.h"
|
2018-06-09 18:32:04 -04:00
|
|
|
|
2018-10-17 14:02:00 -04:00
|
|
|
#define GLOBAL_IMPORT_BUF_SIZE 1024
|
|
|
|
|
2018-06-09 20:24:34 -04:00
|
|
|
namespace deno {
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
std::vector<InternalFieldData*> deserialized_data;
|
|
|
|
|
|
|
|
void DeserializeInternalFields(v8::Local<v8::Object> holder, int index,
|
|
|
|
v8::StartupData payload, void* data) {
|
|
|
|
DCHECK_EQ(data, nullptr);
|
|
|
|
if (payload.raw_size == 0) {
|
|
|
|
holder->SetAlignedPointerInInternalField(index, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
InternalFieldData* embedder_field = new InternalFieldData{0};
|
|
|
|
memcpy(embedder_field, payload.data, payload.raw_size);
|
|
|
|
holder->SetAlignedPointerInInternalField(index, embedder_field);
|
|
|
|
deserialized_data.push_back(embedder_field);
|
2018-07-30 15:12:33 -04:00
|
|
|
}
|
|
|
|
|
2018-10-24 01:12:13 -04:00
|
|
|
v8::StartupData SerializeInternalFields(v8::Local<v8::Object> holder, int index,
|
|
|
|
void* data) {
|
|
|
|
DCHECK_EQ(data, nullptr);
|
|
|
|
InternalFieldData* embedder_field = static_cast<InternalFieldData*>(
|
|
|
|
holder->GetAlignedPointerFromInternalField(index));
|
|
|
|
if (embedder_field == nullptr) return {nullptr, 0};
|
|
|
|
int size = sizeof(*embedder_field);
|
|
|
|
char* payload = new char[size];
|
|
|
|
// We simply use memcpy to serialize the content.
|
|
|
|
memcpy(payload, embedder_field, size);
|
|
|
|
return {payload, size};
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
DenoIsolate* FromIsolate(v8::Isolate* isolate) {
|
|
|
|
return static_cast<DenoIsolate*>(isolate->GetData(0));
|
2018-09-27 17:33:10 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
void AddDataRef(DenoIsolate* d, int32_t req_id, v8::Local<v8::Value> data_v) {
|
|
|
|
d->async_data_map_.emplace(std::piecewise_construct, std::make_tuple(req_id),
|
|
|
|
std::make_tuple(d->isolate_, data_v));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteDataRef(DenoIsolate* d, int32_t req_id) {
|
2018-09-27 17:33:10 -04:00
|
|
|
// Delete persistent reference to data ArrayBuffer.
|
2018-10-23 23:58:20 -04:00
|
|
|
auto it = d->async_data_map_.find(req_id);
|
|
|
|
if (it != d->async_data_map_.end()) {
|
2018-10-18 18:20:23 -04:00
|
|
|
it->second.Reset();
|
2018-10-23 23:58:20 -04:00
|
|
|
d->async_data_map_.erase(it);
|
2018-10-18 11:44:45 -04:00
|
|
|
}
|
2018-09-27 17:33:10 -04:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-12-06 23:05:36 -05:00
|
|
|
std::string EncodeExceptionAsJSON(v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Value> exception) {
|
2018-06-10 08:24:39 -04:00
|
|
|
auto* isolate = context->GetIsolate();
|
2018-06-10 00:13:48 -04:00
|
|
|
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);
|
2018-07-05 17:33:20 -04:00
|
|
|
auto stack_trace = message->GetStackTrace();
|
2018-08-26 03:57:16 -04:00
|
|
|
|
2018-12-06 23:05:36 -05:00
|
|
|
// Encode the exception into a JS object, which we will then turn into JSON.
|
|
|
|
auto json_obj = v8::Object::New(isolate);
|
|
|
|
|
|
|
|
auto exception_str = exception->ToString(context).ToLocalChecked();
|
|
|
|
// Alternate and very similar string. Not sure which is appropriate.
|
|
|
|
// auto exception_str = message->Get();
|
|
|
|
CHECK(json_obj->Set(context, v8_str("message"), exception_str).FromJust());
|
2018-07-30 15:12:33 -04:00
|
|
|
|
2018-12-06 23:05:36 -05:00
|
|
|
v8::Local<v8::Array> frames;
|
2018-07-30 15:12:33 -04:00
|
|
|
if (!stack_trace.IsEmpty()) {
|
2018-12-06 23:05:36 -05:00
|
|
|
uint32_t count = static_cast<uint32_t>(stack_trace->GetFrameCount());
|
|
|
|
frames = v8::Array::New(isolate, count);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2018-09-17 18:57:25 -04:00
|
|
|
auto frame = stack_trace->GetFrame(isolate, i);
|
2018-12-06 23:05:36 -05:00
|
|
|
auto frame_obj = v8::Object::New(isolate);
|
|
|
|
CHECK(frames->Set(context, i, frame_obj).FromJust());
|
|
|
|
auto line = v8::Integer::New(isolate, frame->GetLineNumber());
|
|
|
|
auto column = v8::Integer::New(isolate, frame->GetColumn());
|
|
|
|
CHECK(frame_obj->Set(context, v8_str("line"), line).FromJust());
|
|
|
|
CHECK(frame_obj->Set(context, v8_str("column"), column).FromJust());
|
|
|
|
CHECK(frame_obj
|
|
|
|
->Set(context, v8_str("functionName"), frame->GetFunctionName())
|
|
|
|
.FromJust());
|
|
|
|
CHECK(frame_obj
|
|
|
|
->Set(context, v8_str("scriptName"),
|
|
|
|
frame->GetScriptNameOrSourceURL())
|
|
|
|
.FromJust());
|
|
|
|
CHECK(frame_obj
|
|
|
|
->Set(context, v8_str("isEval"),
|
|
|
|
v8::Boolean::New(isolate, frame->IsEval()))
|
|
|
|
.FromJust());
|
|
|
|
CHECK(frame_obj
|
|
|
|
->Set(context, v8_str("isConstructor"),
|
|
|
|
v8::Boolean::New(isolate, frame->IsConstructor()))
|
|
|
|
.FromJust());
|
|
|
|
CHECK(frame_obj
|
|
|
|
->Set(context, v8_str("isWasm"),
|
|
|
|
v8::Boolean::New(isolate, frame->IsWasm()))
|
|
|
|
.FromJust());
|
2018-07-05 17:33:20 -04:00
|
|
|
}
|
2018-06-09 18:32:04 -04:00
|
|
|
} else {
|
2018-12-06 23:05:36 -05:00
|
|
|
// No stack trace. We only have one stack frame of info..
|
|
|
|
frames = v8::Array::New(isolate, 1);
|
|
|
|
|
|
|
|
auto frame_obj = v8::Object::New(isolate);
|
|
|
|
CHECK(frames->Set(context, 0, frame_obj).FromJust());
|
|
|
|
|
|
|
|
auto line =
|
|
|
|
v8::Integer::New(isolate, message->GetLineNumber(context).FromJust());
|
|
|
|
auto column =
|
|
|
|
v8::Integer::New(isolate, message->GetStartColumn(context).FromJust());
|
|
|
|
|
|
|
|
CHECK(frame_obj->Set(context, v8_str("line"), line).FromJust());
|
|
|
|
CHECK(frame_obj->Set(context, v8_str("column"), column).FromJust());
|
|
|
|
CHECK(frame_obj
|
|
|
|
->Set(context, v8_str("scriptName"),
|
|
|
|
message->GetScriptResourceName())
|
|
|
|
.FromJust());
|
2018-07-30 15:12:33 -04:00
|
|
|
}
|
2018-12-06 23:05:36 -05:00
|
|
|
|
|
|
|
CHECK(json_obj->Set(context, v8_str("frames"), frames).FromJust());
|
|
|
|
|
|
|
|
auto json_string = v8::JSON::Stringify(context, json_obj).ToLocalChecked();
|
|
|
|
v8::String::Utf8Value json_string_(isolate, json_string);
|
|
|
|
return std::string(ToCString(json_string_));
|
2018-07-30 15:12:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HandleException(v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Value> exception) {
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2018-10-23 23:58:20 -04:00
|
|
|
DenoIsolate* d = FromIsolate(isolate);
|
2018-12-06 23:05:36 -05:00
|
|
|
std::string json_str = EncodeExceptionAsJSON(context, exception);
|
2018-07-30 15:12:33 -04:00
|
|
|
if (d != nullptr) {
|
2018-12-06 23:05:36 -05:00
|
|
|
d->last_exception_ = json_str;
|
2018-07-30 15:12:33 -04:00
|
|
|
} else {
|
2018-12-06 23:05:36 -05:00
|
|
|
// This shouldn't happen in normal circumstances. Added for debugging.
|
|
|
|
std::cerr << "Pre-Deno Exception " << json_str << std::endl;
|
|
|
|
CHECK(false);
|
2018-10-12 14:22:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PromiseRejectCallback(v8::PromiseRejectMessage promise_reject_message) {
|
2018-06-09 18:32:04 -04:00
|
|
|
auto* isolate = v8::Isolate::GetCurrent();
|
2018-10-23 23:58:20 -04:00
|
|
|
DenoIsolate* d = static_cast<DenoIsolate*>(isolate->GetData(0));
|
|
|
|
DCHECK_EQ(d->isolate_, isolate);
|
|
|
|
v8::HandleScope handle_scope(d->isolate_);
|
2018-12-06 23:05:36 -05:00
|
|
|
auto error = promise_reject_message.GetValue();
|
2018-10-23 23:58:20 -04:00
|
|
|
auto context = d->context_.Get(d->isolate_);
|
2018-10-12 14:22:52 -04:00
|
|
|
auto promise = promise_reject_message.GetPromise();
|
|
|
|
|
|
|
|
v8::Context::Scope context_scope(context);
|
2018-12-06 23:05:36 -05:00
|
|
|
|
|
|
|
int promise_id = promise->GetIdentityHash();
|
|
|
|
switch (promise_reject_message.GetEvent()) {
|
|
|
|
case v8::kPromiseRejectWithNoHandler:
|
|
|
|
// Insert the error into the pending_promise_map_ using the promise's id
|
|
|
|
// as the key.
|
|
|
|
d->pending_promise_map_.emplace(std::piecewise_construct,
|
|
|
|
std::make_tuple(promise_id),
|
|
|
|
std::make_tuple(d->isolate_, error));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case v8::kPromiseHandlerAddedAfterReject:
|
|
|
|
d->pending_promise_map_.erase(promise_id);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case v8::kPromiseRejectAfterResolved:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case v8::kPromiseResolveAfterResolved:
|
|
|
|
// Should not warn. See #1272
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
CHECK(false && "unreachable");
|
2018-10-12 14:22:52 -04:00
|
|
|
}
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2018-09-25 01:27:02 -04:00
|
|
|
CHECK_GE(args.Length(), 1);
|
|
|
|
CHECK_LE(args.Length(), 2);
|
2018-06-09 18:32:04 -04:00
|
|
|
auto* isolate = args.GetIsolate();
|
2018-10-23 23:58:20 -04:00
|
|
|
DenoIsolate* d = FromIsolate(isolate);
|
|
|
|
auto context = d->context_.Get(d->isolate_);
|
2018-06-09 20:02:40 -04:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::String::Utf8Value str(isolate, args[0]);
|
2018-09-25 01:27:02 -04:00
|
|
|
bool is_err =
|
|
|
|
args.Length() >= 2 ? args[1]->BooleanValue(context).ToChecked() : false;
|
2018-06-09 20:02:40 -04:00
|
|
|
const char* cstr = ToCString(str);
|
2018-09-28 15:55:06 -04:00
|
|
|
auto& stream = is_err ? std::cerr : std::cout;
|
|
|
|
stream << cstr << std::endl;
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
v8::Local<v8::Uint8Array> ImportBuf(DenoIsolate* d, deno_buf buf) {
|
2018-07-23 14:11:41 -04:00
|
|
|
if (buf.alloc_ptr == nullptr) {
|
|
|
|
// If alloc_ptr isn't set, we memcpy.
|
|
|
|
// This is currently used for flatbuffers created in Rust.
|
2018-10-17 14:02:00 -04:00
|
|
|
|
|
|
|
// To avoid excessively allocating new ArrayBuffers, we try to reuse a
|
|
|
|
// single global ArrayBuffer. The caveat is that users must extract data
|
|
|
|
// from it before the next tick. We only do this for ArrayBuffers less than
|
|
|
|
// 1024 bytes.
|
|
|
|
v8::Local<v8::ArrayBuffer> ab;
|
|
|
|
void* data;
|
|
|
|
if (buf.data_len > GLOBAL_IMPORT_BUF_SIZE) {
|
|
|
|
// Simple case. We allocate a new ArrayBuffer for this.
|
2018-10-23 23:58:20 -04:00
|
|
|
ab = v8::ArrayBuffer::New(d->isolate_, buf.data_len);
|
2018-10-17 14:02:00 -04:00
|
|
|
data = ab->GetContents().Data();
|
|
|
|
} else {
|
|
|
|
// Fast case. We reuse the global ArrayBuffer.
|
2018-10-23 23:58:20 -04:00
|
|
|
if (d->global_import_buf_.IsEmpty()) {
|
2018-10-17 14:02:00 -04:00
|
|
|
// Lazily initialize it.
|
2018-10-23 23:58:20 -04:00
|
|
|
DCHECK_EQ(d->global_import_buf_ptr_, nullptr);
|
|
|
|
ab = v8::ArrayBuffer::New(d->isolate_, GLOBAL_IMPORT_BUF_SIZE);
|
|
|
|
d->global_import_buf_.Reset(d->isolate_, ab);
|
|
|
|
d->global_import_buf_ptr_ = ab->GetContents().Data();
|
2018-10-17 14:02:00 -04:00
|
|
|
} else {
|
2018-10-23 23:58:20 -04:00
|
|
|
DCHECK(d->global_import_buf_ptr_);
|
|
|
|
ab = d->global_import_buf_.Get(d->isolate_);
|
2018-10-17 14:02:00 -04:00
|
|
|
}
|
2018-10-23 23:58:20 -04:00
|
|
|
data = d->global_import_buf_ptr_;
|
2018-10-17 14:02:00 -04:00
|
|
|
}
|
|
|
|
memcpy(data, buf.data_ptr, buf.data_len);
|
2018-07-23 14:11:41 -04:00
|
|
|
auto view = v8::Uint8Array::New(ab, 0, buf.data_len);
|
|
|
|
return view;
|
|
|
|
} else {
|
|
|
|
auto ab = v8::ArrayBuffer::New(
|
2018-10-23 23:58:20 -04:00
|
|
|
d->isolate_, reinterpret_cast<void*>(buf.alloc_ptr), buf.alloc_len,
|
2018-07-23 14:11:41 -04:00
|
|
|
v8::ArrayBufferCreationMode::kInternalized);
|
|
|
|
auto view =
|
|
|
|
v8::Uint8Array::New(ab, buf.data_ptr - buf.alloc_ptr, buf.data_len);
|
|
|
|
return view;
|
|
|
|
}
|
2018-07-08 21:35:34 -04:00
|
|
|
}
|
|
|
|
|
2018-09-19 11:24:34 -04:00
|
|
|
static deno_buf GetContents(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::ArrayBufferView> view) {
|
2018-07-08 21:35:34 -04:00
|
|
|
auto ab = view->Buffer();
|
2018-09-19 11:24:34 -04:00
|
|
|
auto contents = ab->GetContents();
|
2018-07-08 21:35:34 -04:00
|
|
|
deno_buf buf;
|
|
|
|
buf.alloc_ptr = reinterpret_cast<uint8_t*>(contents.Data());
|
|
|
|
buf.alloc_len = contents.ByteLength();
|
|
|
|
buf.data_ptr = buf.alloc_ptr + view->ByteOffset();
|
|
|
|
buf.data_len = view->ByteLength();
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
// Sets the recv_ callback.
|
2018-07-01 12:07:12 -04:00
|
|
|
void Recv(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2018-06-09 18:32:04 -04:00
|
|
|
v8::Isolate* isolate = args.GetIsolate();
|
2018-10-23 23:58:20 -04:00
|
|
|
DenoIsolate* d = FromIsolate(isolate);
|
|
|
|
DCHECK_EQ(d->isolate_, isolate);
|
2018-06-09 18:32:04 -04:00
|
|
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
if (!d->recv_.IsEmpty()) {
|
|
|
|
isolate->ThrowException(v8_str("libdeno.recv_ already called."));
|
2018-06-11 16:51:11 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-09 18:32:04 -04:00
|
|
|
v8::Local<v8::Value> v = args[0];
|
2018-06-13 18:55:40 -04:00
|
|
|
CHECK(v->IsFunction());
|
2018-06-09 18:32:04 -04:00
|
|
|
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(v);
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
d->recv_.Reset(isolate, func);
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
2018-07-01 12:07:12 -04:00
|
|
|
void Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2018-06-09 18:32:04 -04:00
|
|
|
v8::Isolate* isolate = args.GetIsolate();
|
2018-10-23 23:58:20 -04:00
|
|
|
DenoIsolate* d = FromIsolate(isolate);
|
|
|
|
DCHECK_EQ(d->isolate_, isolate);
|
2018-06-09 18:32:04 -04:00
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
v8::Locker locker(d->isolate_);
|
2018-06-09 18:32:04 -04:00
|
|
|
v8::EscapableHandleScope handle_scope(isolate);
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
CHECK_EQ(d->current_args_, nullptr); // libdeno.send re-entry forbidden.
|
|
|
|
int32_t req_id = d->next_req_id_++;
|
2018-09-27 17:33:10 -04:00
|
|
|
|
|
|
|
v8::Local<v8::Value> control_v = args[0];
|
|
|
|
CHECK(control_v->IsArrayBufferView());
|
|
|
|
deno_buf control =
|
|
|
|
GetContents(isolate, v8::Local<v8::ArrayBufferView>::Cast(control_v));
|
|
|
|
deno_buf data = {nullptr, 0u, nullptr, 0u};
|
|
|
|
v8::Local<v8::Value> data_v;
|
|
|
|
if (args.Length() == 2) {
|
|
|
|
if (args[1]->IsArrayBufferView()) {
|
|
|
|
data_v = args[1];
|
|
|
|
data = GetContents(isolate, v8::Local<v8::ArrayBufferView>::Cast(data_v));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK_EQ(args.Length(), 1);
|
|
|
|
}
|
2018-06-09 18:32:04 -04:00
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
DCHECK_EQ(d->current_args_, nullptr);
|
|
|
|
d->current_args_ = &args;
|
2018-06-13 13:38:22 -04:00
|
|
|
|
2018-12-04 18:06:20 -05:00
|
|
|
d->recv_cb_(d->user_data_, req_id, control, data);
|
2018-06-13 13:38:22 -04:00
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
if (d->current_args_ == nullptr) {
|
2018-09-27 17:33:10 -04:00
|
|
|
// This indicates that deno_repond() was called already.
|
|
|
|
} else {
|
|
|
|
// Asynchronous.
|
2018-10-23 23:58:20 -04:00
|
|
|
d->current_args_ = nullptr;
|
2018-09-27 17:33:10 -04:00
|
|
|
// If the data ArrayBuffer was given, we must maintain a strong reference
|
|
|
|
// to it until deno_respond is called.
|
|
|
|
if (!data_v.IsEmpty()) {
|
|
|
|
AddDataRef(d, req_id, data_v);
|
|
|
|
}
|
|
|
|
}
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
2018-10-24 02:17:10 -04:00
|
|
|
void Shared(v8::Local<v8::Name> property,
|
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info) {
|
|
|
|
v8::Isolate* isolate = info.GetIsolate();
|
|
|
|
DenoIsolate* d = FromIsolate(isolate);
|
|
|
|
DCHECK_EQ(d->isolate_, isolate);
|
|
|
|
v8::Locker locker(d->isolate_);
|
|
|
|
v8::EscapableHandleScope handle_scope(isolate);
|
|
|
|
if (d->shared_.data_ptr == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
v8::Local<v8::ArrayBuffer> ab;
|
|
|
|
if (d->shared_ab_.IsEmpty()) {
|
|
|
|
// Lazily initialize the persistent external ArrayBuffer.
|
|
|
|
ab = v8::ArrayBuffer::New(isolate, d->shared_.data_ptr, d->shared_.data_len,
|
|
|
|
v8::ArrayBufferCreationMode::kExternalized);
|
|
|
|
d->shared_ab_.Reset(isolate, ab);
|
|
|
|
}
|
|
|
|
info.GetReturnValue().Set(ab);
|
|
|
|
}
|
|
|
|
|
2018-07-30 15:12:33 -04:00
|
|
|
bool ExecuteV8StringSource(v8::Local<v8::Context> context,
|
|
|
|
const char* js_filename,
|
|
|
|
v8::Local<v8::String> source) {
|
2018-06-10 08:24:39 -04:00
|
|
|
auto* isolate = context->GetIsolate();
|
2018-06-10 00:13:48 -04:00
|
|
|
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
|
|
|
|
2018-06-11 14:49:57 -04:00
|
|
|
auto name = v8_str(js_filename);
|
2018-06-09 18:32:04 -04:00
|
|
|
|
|
|
|
v8::ScriptOrigin origin(name);
|
|
|
|
|
|
|
|
auto script = v8::Script::Compile(context, source, &origin);
|
|
|
|
|
|
|
|
if (script.IsEmpty()) {
|
2018-06-13 18:55:40 -04:00
|
|
|
DCHECK(try_catch.HasCaught());
|
2018-06-10 00:13:48 -04:00
|
|
|
HandleException(context, try_catch.Exception());
|
|
|
|
return false;
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
auto result = script.ToLocalChecked()->Run(context);
|
|
|
|
|
|
|
|
if (result.IsEmpty()) {
|
2018-06-13 18:55:40 -04:00
|
|
|
DCHECK(try_catch.HasCaught());
|
2018-06-10 00:13:48 -04:00
|
|
|
HandleException(context, try_catch.Exception());
|
|
|
|
return false;
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
2018-06-10 00:13:48 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-30 15:12:33 -04:00
|
|
|
bool Execute(v8::Local<v8::Context> context, const char* js_filename,
|
|
|
|
const char* js_source) {
|
|
|
|
auto* isolate = context->GetIsolate();
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
auto source = v8_str(js_source);
|
|
|
|
return ExecuteV8StringSource(context, js_filename, source);
|
|
|
|
}
|
|
|
|
|
2018-06-13 14:55:08 -04:00
|
|
|
void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context,
|
2018-12-06 23:05:36 -05:00
|
|
|
const char* js_filename, const char* js_source) {
|
2018-10-24 01:12:13 -04:00
|
|
|
CHECK_NE(js_source, nullptr);
|
|
|
|
CHECK_NE(js_filename, nullptr);
|
2018-06-13 14:55:08 -04:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
auto global = context->Global();
|
2018-06-14 08:31:31 -04:00
|
|
|
|
|
|
|
auto deno_val = v8::Object::New(isolate);
|
2018-08-06 18:37:32 -04:00
|
|
|
CHECK(global->Set(context, deno::v8_str("libdeno"), deno_val).FromJust());
|
2018-06-14 08:31:31 -04:00
|
|
|
|
2018-06-13 14:55:08 -04:00
|
|
|
auto print_tmpl = v8::FunctionTemplate::New(isolate, Print);
|
|
|
|
auto print_val = print_tmpl->GetFunction(context).ToLocalChecked();
|
2018-06-14 08:31:31 -04:00
|
|
|
CHECK(deno_val->Set(context, deno::v8_str("print"), print_val).FromJust());
|
2018-06-13 14:55:08 -04:00
|
|
|
|
2018-07-01 12:07:12 -04:00
|
|
|
auto recv_tmpl = v8::FunctionTemplate::New(isolate, Recv);
|
|
|
|
auto recv_val = recv_tmpl->GetFunction(context).ToLocalChecked();
|
|
|
|
CHECK(deno_val->Set(context, deno::v8_str("recv"), recv_val).FromJust());
|
2018-06-13 14:55:08 -04:00
|
|
|
|
2018-07-01 12:07:12 -04:00
|
|
|
auto send_tmpl = v8::FunctionTemplate::New(isolate, Send);
|
|
|
|
auto send_val = send_tmpl->GetFunction(context).ToLocalChecked();
|
|
|
|
CHECK(deno_val->Set(context, deno::v8_str("send"), send_val).FromJust());
|
2018-06-13 14:55:08 -04:00
|
|
|
|
2018-10-24 02:17:10 -04:00
|
|
|
CHECK(deno_val->SetAccessor(context, deno::v8_str("shared"), Shared)
|
|
|
|
.FromJust());
|
|
|
|
|
2018-07-30 15:12:33 -04:00
|
|
|
{
|
2018-10-25 11:00:14 -04:00
|
|
|
auto source = deno::v8_str(js_source);
|
|
|
|
|
|
|
|
bool r = deno::ExecuteV8StringSource(context, js_filename, source);
|
|
|
|
CHECK(r);
|
2018-07-30 15:12:33 -04:00
|
|
|
}
|
2018-06-13 14:55:08 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
void DenoIsolate::AddIsolate(v8::Isolate* isolate) {
|
|
|
|
isolate_ = isolate;
|
2018-06-10 08:18:15 -04:00
|
|
|
// 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->SetAbortOnUncaughtExceptionCallback(AbortOnUncaughtExceptionCallback);
|
|
|
|
// d->isolate->AddMessageListener(MessageCallback2);
|
|
|
|
// d->isolate->SetFatalErrorHandler(FatalErrorCallback2);
|
2018-12-06 23:05:36 -05:00
|
|
|
isolate_->SetCaptureStackTraceForUncaughtExceptions(
|
|
|
|
true, 10, v8::StackTrace::kDetailed);
|
2018-10-23 23:58:20 -04:00
|
|
|
isolate_->SetPromiseRejectCallback(deno::PromiseRejectCallback);
|
|
|
|
isolate_->SetData(0, this);
|
2018-06-10 08:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace deno
|