2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 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>
|
2019-03-03 22:07:25 -05:00
|
|
|
#include <algorithm>
|
2018-09-28 15:55:06 -04:00
|
|
|
#include <iostream>
|
2018-06-09 18:32:04 -04:00
|
|
|
#include <string>
|
|
|
|
|
2019-03-03 22:07:25 -05:00
|
|
|
#ifdef _WIN32
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
#include <io.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#endif // _WIN32
|
|
|
|
|
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"
|
2019-01-29 11:32:40 -05:00
|
|
|
#include "exceptions.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) {
|
2019-02-04 11:53:40 -05:00
|
|
|
DCHECK_NULL(data);
|
2018-10-23 23:58:20 -04:00
|
|
|
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) {
|
2019-02-04 11:53:40 -05:00
|
|
|
DCHECK_NULL(data);
|
2018-10-24 01:12:13 -04:00
|
|
|
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-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>";
|
|
|
|
}
|
|
|
|
|
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);
|
2019-01-06 16:34:52 -05:00
|
|
|
CHECK_LE(args.Length(), 3);
|
2018-06-09 18:32:04 -04:00
|
|
|
auto* isolate = args.GetIsolate();
|
2019-01-29 11:32:40 -05:00
|
|
|
DenoIsolate* d = DenoIsolate::FromIsolate(isolate);
|
2018-10-23 23:58:20 -04:00
|
|
|
auto context = d->context_.Get(d->isolate_);
|
2018-06-09 20:02:40 -04:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
2018-09-25 01:27:02 -04:00
|
|
|
bool is_err =
|
|
|
|
args.Length() >= 2 ? args[1]->BooleanValue(context).ToChecked() : false;
|
2018-12-30 14:58:23 -05:00
|
|
|
FILE* file = is_err ? stderr : stdout;
|
2019-03-03 22:07:25 -05:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
int fd = _fileno(file);
|
|
|
|
if (fd < 0) return;
|
|
|
|
|
|
|
|
HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
|
|
|
|
if (h == INVALID_HANDLE_VALUE) return;
|
|
|
|
|
|
|
|
DWORD mode;
|
|
|
|
if (GetConsoleMode(h, &mode)) {
|
|
|
|
// Print to Windows console. Since the Windows API generally doesn't support
|
|
|
|
// UTF-8 encoded text, we have to use `WriteConsoleW()` which uses UTF-16.
|
|
|
|
v8::String::Value str(isolate, args[0]);
|
|
|
|
auto str_len = static_cast<size_t>(str.length());
|
|
|
|
auto str_wchars = reinterpret_cast<WCHAR*>(*str);
|
|
|
|
|
|
|
|
// WriteConsoleW has some limit to how many characters can be written at
|
|
|
|
// once, which is unspecified but low enough to be encountered in practice.
|
|
|
|
// Therefore we break up the write into chunks of 8kb if necessary.
|
|
|
|
size_t chunk_start = 0;
|
|
|
|
while (chunk_start < str_len) {
|
|
|
|
size_t chunk_end = std::min(chunk_start + 8192, str_len);
|
|
|
|
|
|
|
|
// Do not break in the middle of a surrogate pair. Note that `chunk_end`
|
|
|
|
// points to the start of the next chunk, so we check whether it contains
|
|
|
|
// the second half of a surrogate pair (a.k.a. "low surrogate").
|
|
|
|
if (chunk_end < str_len && str_wchars[chunk_end] >= 0xdc00 &&
|
|
|
|
str_wchars[chunk_end] <= 0xdfff) {
|
|
|
|
--chunk_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the console.
|
|
|
|
DWORD chunk_len = static_cast<DWORD>(chunk_end - chunk_start);
|
|
|
|
DWORD _;
|
|
|
|
WriteConsoleW(h, &str_wchars[chunk_start], chunk_len, &_, nullptr);
|
|
|
|
|
|
|
|
chunk_start = chunk_end;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
v8::String::Utf8Value str(isolate, args[0]);
|
2018-12-30 14:58:23 -05:00
|
|
|
fwrite(*str, sizeof(**str), str.length(), file);
|
|
|
|
fflush(file);
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
2019-02-09 16:55:40 -05:00
|
|
|
void ErrorToJSON(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
CHECK_EQ(args.Length(), 1);
|
|
|
|
auto* isolate = args.GetIsolate();
|
|
|
|
DenoIsolate* d = DenoIsolate::FromIsolate(isolate);
|
|
|
|
auto context = d->context_.Get(d->isolate_);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
auto json_string = EncodeExceptionAsJSON(context, args[0]);
|
|
|
|
args.GetReturnValue().Set(v8_str(json_string.c_str()));
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
v8::Local<v8::Uint8Array> ImportBuf(DenoIsolate* d, deno_buf buf) {
|
2019-02-26 17:36:05 -05:00
|
|
|
if (buf.data_ptr == nullptr) {
|
|
|
|
return v8::Local<v8::Uint8Array>();
|
|
|
|
}
|
|
|
|
|
2019-05-01 18:18:18 -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.
|
|
|
|
ab = v8::ArrayBuffer::New(d->isolate_, buf.data_len);
|
|
|
|
data = ab->GetContents().Data();
|
|
|
|
} else {
|
|
|
|
// Fast case. We reuse the global ArrayBuffer.
|
|
|
|
if (d->global_import_buf_.IsEmpty()) {
|
|
|
|
// Lazily initialize it.
|
|
|
|
DCHECK_NULL(d->global_import_buf_ptr_);
|
|
|
|
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 {
|
2019-05-01 18:18:18 -04:00
|
|
|
DCHECK(d->global_import_buf_ptr_);
|
|
|
|
ab = d->global_import_buf_.Get(d->isolate_);
|
2018-10-17 14:02:00 -04:00
|
|
|
}
|
2019-05-01 18:18:18 -04:00
|
|
|
data = d->global_import_buf_ptr_;
|
2018-07-23 14:11:41 -04:00
|
|
|
}
|
2019-05-01 18:18:18 -04:00
|
|
|
memcpy(data, buf.data_ptr, buf.data_len);
|
|
|
|
auto view = v8::Uint8Array::New(ab, 0, buf.data_len);
|
|
|
|
return view;
|
2018-07-08 21:35:34 -04:00
|
|
|
}
|
|
|
|
|
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();
|
2019-01-29 11:32:40 -05:00
|
|
|
DenoIsolate* d = DenoIsolate::FromIsolate(isolate);
|
2018-10-23 23:58:20 -04:00
|
|
|
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()) {
|
2019-03-26 08:22:07 -04:00
|
|
|
isolate->ThrowException(v8_str("Deno.core.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();
|
2019-01-29 11:32:40 -05:00
|
|
|
DenoIsolate* d = DenoIsolate::FromIsolate(isolate);
|
2018-10-23 23:58:20 -04:00
|
|
|
DCHECK_EQ(d->isolate_, isolate);
|
2018-06-09 18:32:04 -04:00
|
|
|
|
2018-11-29 15:40:04 -05:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
2018-06-09 18:32:04 -04:00
|
|
|
|
2019-05-01 18:18:18 -04:00
|
|
|
deno_buf control = {nullptr, 0};
|
|
|
|
if (args[0]->IsArrayBufferView()) {
|
|
|
|
auto view = v8::Local<v8::ArrayBufferView>::Cast(args[0]);
|
|
|
|
auto data =
|
|
|
|
reinterpret_cast<uint8_t*>(view->Buffer()->GetContents().Data());
|
|
|
|
control = {data + view->ByteOffset(), view->ByteLength()};
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
2018-09-27 17:33:10 -04:00
|
|
|
|
2019-04-28 15:31:10 -04:00
|
|
|
PinnedBuf zero_copy =
|
|
|
|
args[1]->IsArrayBufferView()
|
|
|
|
? PinnedBuf(v8::Local<v8::ArrayBufferView>::Cast(args[1]))
|
|
|
|
: PinnedBuf();
|
2018-06-09 18:32:04 -04:00
|
|
|
|
2019-02-04 11:53:40 -05:00
|
|
|
DCHECK_NULL(d->current_args_);
|
2018-10-23 23:58:20 -04:00
|
|
|
d->current_args_ = &args;
|
2018-06-13 13:38:22 -04:00
|
|
|
|
2019-04-28 15:31:10 -04:00
|
|
|
d->recv_cb_(d->user_data_, control, zero_copy.IntoRaw());
|
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
|
|
|
}
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
|
|
|
|
2019-01-30 17:21:31 -05:00
|
|
|
v8::ScriptOrigin ModuleOrigin(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> resource_name) {
|
|
|
|
return v8::ScriptOrigin(resource_name, v8::Local<v8::Integer>(),
|
|
|
|
v8::Local<v8::Integer>(), v8::Local<v8::Boolean>(),
|
|
|
|
v8::Local<v8::Integer>(), v8::Local<v8::Value>(),
|
|
|
|
v8::Local<v8::Boolean>(), v8::Local<v8::Boolean>(),
|
|
|
|
v8::True(isolate));
|
|
|
|
}
|
|
|
|
|
2019-02-26 13:29:45 -05:00
|
|
|
deno_mod DenoIsolate::RegisterModule(bool main, const char* name,
|
|
|
|
const char* source) {
|
2019-01-30 17:21:31 -05:00
|
|
|
v8::Isolate::Scope isolate_scope(isolate_);
|
|
|
|
v8::Locker locker(isolate_);
|
|
|
|
v8::HandleScope handle_scope(isolate_);
|
|
|
|
auto context = context_.Get(isolate_);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
2019-02-01 23:26:33 -05:00
|
|
|
v8::Local<v8::String> name_str = v8_str(name);
|
|
|
|
v8::Local<v8::String> source_str = v8_str(source);
|
2019-01-30 17:21:31 -05:00
|
|
|
|
|
|
|
auto origin = ModuleOrigin(isolate_, name_str);
|
|
|
|
v8::ScriptCompiler::Source source_(source_str, origin);
|
|
|
|
|
|
|
|
v8::TryCatch try_catch(isolate_);
|
|
|
|
|
|
|
|
auto maybe_module = v8::ScriptCompiler::CompileModule(isolate_, &source_);
|
|
|
|
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
CHECK(maybe_module.IsEmpty());
|
|
|
|
HandleException(context, try_catch.Exception());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto module = maybe_module.ToLocalChecked();
|
|
|
|
|
|
|
|
int id = module->GetIdentityHash();
|
|
|
|
|
|
|
|
std::vector<std::string> import_specifiers;
|
|
|
|
|
|
|
|
for (int i = 0; i < module->GetModuleRequestsLength(); ++i) {
|
|
|
|
v8::Local<v8::String> specifier = module->GetModuleRequest(i);
|
|
|
|
v8::String::Utf8Value specifier_utf8(isolate_, specifier);
|
|
|
|
import_specifiers.push_back(*specifier_utf8);
|
|
|
|
}
|
|
|
|
|
2019-02-26 13:29:45 -05:00
|
|
|
mods_.emplace(
|
|
|
|
std::piecewise_construct, std::make_tuple(id),
|
|
|
|
std::make_tuple(isolate_, module, main, name, import_specifiers));
|
2019-01-30 17:21:31 -05:00
|
|
|
mods_by_name_[name] = id;
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
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();
|
2019-01-29 11:32:40 -05:00
|
|
|
DenoIsolate* d = DenoIsolate::FromIsolate(isolate);
|
2018-10-24 02:17:10 -04:00
|
|
|
DCHECK_EQ(d->isolate_, isolate);
|
|
|
|
v8::Locker locker(d->isolate_);
|
|
|
|
v8::EscapableHandleScope handle_scope(isolate);
|
|
|
|
if (d->shared_.data_ptr == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-08 23:32:17 -05:00
|
|
|
v8::Local<v8::SharedArrayBuffer> ab;
|
2018-10-24 02:17:10 -04:00
|
|
|
if (d->shared_ab_.IsEmpty()) {
|
|
|
|
// Lazily initialize the persistent external ArrayBuffer.
|
2019-02-08 23:32:17 -05:00
|
|
|
ab = v8::SharedArrayBuffer::New(isolate, d->shared_.data_ptr,
|
|
|
|
d->shared_.data_len,
|
|
|
|
v8::ArrayBufferCreationMode::kExternalized);
|
2018-10-24 02:17:10 -04:00
|
|
|
d->shared_ab_.Reset(isolate, ab);
|
|
|
|
}
|
2019-02-08 23:32:17 -05:00
|
|
|
info.GetReturnValue().Set(d->shared_ab_);
|
2018-10-24 02:17:10 -04:00
|
|
|
}
|
|
|
|
|
2018-11-29 15:40:04 -05:00
|
|
|
void DenoIsolate::ClearModules() {
|
2019-01-30 17:21:31 -05:00
|
|
|
for (auto it = mods_.begin(); it != mods_.end(); it++) {
|
|
|
|
it->second.handle.Reset();
|
2018-06-09 18:32:04 -04:00
|
|
|
}
|
2019-01-30 17:21:31 -05:00
|
|
|
mods_.clear();
|
|
|
|
mods_by_name_.clear();
|
2018-06-10 00:13:48 -04:00
|
|
|
}
|
|
|
|
|
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);
|
2018-11-29 15:40:04 -05:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
2019-02-01 23:26:33 -05:00
|
|
|
auto source = v8_str(js_source);
|
|
|
|
auto name = v8_str(js_filename);
|
2018-11-29 15:40:04 -05:00
|
|
|
|
|
|
|
v8::TryCatch try_catch(isolate);
|
|
|
|
|
|
|
|
v8::ScriptOrigin origin(name);
|
|
|
|
|
|
|
|
auto script = v8::Script::Compile(context, source, &origin);
|
|
|
|
|
|
|
|
if (script.IsEmpty()) {
|
|
|
|
DCHECK(try_catch.HasCaught());
|
|
|
|
HandleException(context, try_catch.Exception());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = script.ToLocalChecked()->Run(context);
|
|
|
|
|
|
|
|
if (result.IsEmpty()) {
|
|
|
|
DCHECK(try_catch.HasCaught());
|
|
|
|
HandleException(context, try_catch.Exception());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-07-30 15:12:33 -04:00
|
|
|
}
|
|
|
|
|
2019-02-09 16:55:40 -05:00
|
|
|
static inline v8::Local<v8::Boolean> v8_bool(bool v) {
|
|
|
|
return v8::Boolean::New(v8::Isolate::GetCurrent(), v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EvalContext(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
v8::Isolate* isolate = args.GetIsolate();
|
|
|
|
DenoIsolate* d = DenoIsolate::FromIsolate(isolate);
|
|
|
|
v8::EscapableHandleScope handleScope(isolate);
|
|
|
|
auto context = d->context_.Get(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
CHECK(args[0]->IsString());
|
|
|
|
auto source = args[0].As<v8::String>();
|
|
|
|
|
|
|
|
auto output = v8::Array::New(isolate, 2);
|
|
|
|
/**
|
|
|
|
* output[0] = result
|
|
|
|
* output[1] = ErrorInfo | null
|
|
|
|
* ErrorInfo = {
|
|
|
|
* thrown: Error | any,
|
|
|
|
* isNativeError: boolean,
|
|
|
|
* isCompileError: boolean,
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
v8::TryCatch try_catch(isolate);
|
|
|
|
|
|
|
|
auto name = v8_str("<unknown>");
|
|
|
|
v8::ScriptOrigin origin(name);
|
|
|
|
auto script = v8::Script::Compile(context, source, &origin);
|
|
|
|
|
|
|
|
if (script.IsEmpty()) {
|
|
|
|
DCHECK(try_catch.HasCaught());
|
|
|
|
auto exception = try_catch.Exception();
|
|
|
|
|
|
|
|
output->Set(0, v8::Null(isolate));
|
|
|
|
|
|
|
|
auto errinfo_obj = v8::Object::New(isolate);
|
|
|
|
errinfo_obj->Set(v8_str("isCompileError"), v8_bool(true));
|
|
|
|
errinfo_obj->Set(v8_str("isNativeError"),
|
|
|
|
v8_bool(exception->IsNativeError()));
|
|
|
|
errinfo_obj->Set(v8_str("thrown"), exception);
|
|
|
|
|
|
|
|
output->Set(1, errinfo_obj);
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = script.ToLocalChecked()->Run(context);
|
|
|
|
|
|
|
|
if (result.IsEmpty()) {
|
|
|
|
DCHECK(try_catch.HasCaught());
|
|
|
|
auto exception = try_catch.Exception();
|
|
|
|
|
|
|
|
output->Set(0, v8::Null(isolate));
|
|
|
|
|
|
|
|
auto errinfo_obj = v8::Object::New(isolate);
|
|
|
|
errinfo_obj->Set(v8_str("isCompileError"), v8_bool(false));
|
|
|
|
errinfo_obj->Set(v8_str("isNativeError"),
|
|
|
|
v8_bool(exception->IsNativeError()));
|
|
|
|
errinfo_obj->Set(v8_str("thrown"), exception);
|
|
|
|
|
|
|
|
output->Set(1, errinfo_obj);
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
output->Set(0, result.ToLocalChecked());
|
|
|
|
output->Set(1, v8::Null(isolate));
|
|
|
|
args.GetReturnValue().Set(output);
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:25:42 -05:00
|
|
|
void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context) {
|
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);
|
2019-03-26 08:22:07 -04:00
|
|
|
CHECK(global->Set(context, deno::v8_str("Deno"), deno_val).FromJust());
|
|
|
|
|
|
|
|
auto core_val = v8::Object::New(isolate);
|
|
|
|
CHECK(deno_val->Set(context, deno::v8_str("core"), core_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();
|
2019-03-26 08:22:07 -04:00
|
|
|
CHECK(core_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();
|
2019-03-26 08:22:07 -04:00
|
|
|
CHECK(core_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();
|
2019-03-26 08:22:07 -04:00
|
|
|
CHECK(core_val->Set(context, deno::v8_str("send"), send_val).FromJust());
|
2018-06-13 14:55:08 -04:00
|
|
|
|
2019-02-09 16:55:40 -05:00
|
|
|
auto eval_context_tmpl = v8::FunctionTemplate::New(isolate, EvalContext);
|
|
|
|
auto eval_context_val =
|
|
|
|
eval_context_tmpl->GetFunction(context).ToLocalChecked();
|
2019-03-26 08:22:07 -04:00
|
|
|
CHECK(core_val->Set(context, deno::v8_str("evalContext"), eval_context_val)
|
2019-02-09 16:55:40 -05:00
|
|
|
.FromJust());
|
|
|
|
|
|
|
|
auto error_to_json_tmpl = v8::FunctionTemplate::New(isolate, ErrorToJSON);
|
|
|
|
auto error_to_json_val =
|
|
|
|
error_to_json_tmpl->GetFunction(context).ToLocalChecked();
|
2019-03-26 08:22:07 -04:00
|
|
|
CHECK(core_val->Set(context, deno::v8_str("errorToJSON"), error_to_json_val)
|
2019-02-09 16:55:40 -05:00
|
|
|
.FromJust());
|
|
|
|
|
2019-03-26 08:22:07 -04:00
|
|
|
CHECK(core_val->SetAccessor(context, deno::v8_str("shared"), Shared)
|
2018-10-24 02:17:10 -04:00
|
|
|
.FromJust());
|
2018-06-13 14:55:08 -04:00
|
|
|
}
|
|
|
|
|
2019-01-29 11:32:40 -05:00
|
|
|
void MessageCallback(v8::Local<v8::Message> message,
|
|
|
|
v8::Local<v8::Value> data) {
|
|
|
|
auto* isolate = message->GetIsolate();
|
|
|
|
DenoIsolate* d = static_cast<DenoIsolate*>(isolate->GetData(0));
|
|
|
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
auto context = d->context_.Get(isolate);
|
|
|
|
HandleExceptionMessage(context, message);
|
|
|
|
}
|
|
|
|
|
2019-01-30 17:21:31 -05:00
|
|
|
void HostInitializeImportMetaObjectCallback(v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Module> module,
|
|
|
|
v8::Local<v8::Object> meta) {
|
|
|
|
auto* isolate = context->GetIsolate();
|
|
|
|
DenoIsolate* d = DenoIsolate::FromIsolate(isolate);
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
|
|
|
|
CHECK(!module.IsEmpty());
|
|
|
|
|
|
|
|
deno_mod id = module->GetIdentityHash();
|
|
|
|
CHECK_NE(id, 0);
|
|
|
|
|
|
|
|
auto* info = d->GetModuleInfo(id);
|
|
|
|
|
|
|
|
const char* url = info->name.c_str();
|
2019-02-26 13:29:45 -05:00
|
|
|
const bool main = info->main;
|
2019-01-30 17:21:31 -05:00
|
|
|
|
2019-02-01 23:26:33 -05:00
|
|
|
meta->CreateDataProperty(context, v8_str("url"), v8_str(url)).ToChecked();
|
2019-02-26 13:29:45 -05:00
|
|
|
meta->CreateDataProperty(context, v8_str("main"), v8_bool(main)).ToChecked();
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
void DenoIsolate::AddIsolate(v8::Isolate* isolate) {
|
|
|
|
isolate_ = isolate;
|
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);
|
2019-01-29 11:32:40 -05:00
|
|
|
isolate_->AddMessageListener(MessageCallback);
|
2019-01-30 17:21:31 -05:00
|
|
|
isolate->SetHostInitializeImportMetaObjectCallback(
|
|
|
|
HostInitializeImportMetaObjectCallback);
|
2018-06-10 08:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace deno
|