2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-23 23:58:20 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-03-04 18:09:35 -05:00
|
|
|
#include <iostream>
|
2018-10-23 23:58:20 -04:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "third_party/v8/include/libplatform/libplatform.h"
|
|
|
|
#include "third_party/v8/include/v8.h"
|
|
|
|
#include "third_party/v8/src/base/logging.h"
|
|
|
|
|
|
|
|
#include "deno.h"
|
2019-02-09 16:55:40 -05:00
|
|
|
#include "exceptions.h"
|
2019-03-04 18:09:35 -05:00
|
|
|
#include "file_util.h"
|
2018-10-23 23:58:20 -04:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2018-12-13 16:25:42 -05:00
|
|
|
Deno* deno_new_snapshotter(deno_config config) {
|
|
|
|
CHECK(config.will_snapshot);
|
2019-01-29 21:31:59 -05:00
|
|
|
// TODO(ry) Support loading snapshots before snapshotting.
|
2018-12-13 16:25:42 -05:00
|
|
|
CHECK_NULL(config.load_snapshot.data_ptr);
|
|
|
|
auto* creator = new v8::SnapshotCreator(deno::external_references);
|
|
|
|
auto* isolate = creator->GetIsolate();
|
|
|
|
auto* d = new deno::DenoIsolate(config);
|
|
|
|
d->snapshot_creator_ = creator;
|
|
|
|
d->AddIsolate(isolate);
|
|
|
|
{
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
auto context = v8::Context::New(isolate);
|
|
|
|
d->context_.Reset(isolate, context);
|
|
|
|
|
|
|
|
creator->SetDefaultContext(context,
|
|
|
|
v8::SerializeInternalFieldsCallback(
|
|
|
|
deno::SerializeInternalFields, nullptr));
|
|
|
|
deno::InitializeContext(isolate, context);
|
|
|
|
}
|
|
|
|
return reinterpret_cast<Deno*>(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno* deno_new(deno_config config) {
|
|
|
|
if (config.will_snapshot) {
|
|
|
|
return deno_new_snapshotter(config);
|
|
|
|
}
|
|
|
|
deno::DenoIsolate* d = new deno::DenoIsolate(config);
|
2018-10-23 23:58:20 -04:00
|
|
|
v8::Isolate::CreateParams params;
|
2018-12-03 14:22:26 -05:00
|
|
|
params.array_buffer_allocator = d->array_buffer_allocator_;
|
2018-10-23 23:58:20 -04:00
|
|
|
params.external_references = deno::external_references;
|
|
|
|
|
2018-12-13 16:25:42 -05:00
|
|
|
if (config.load_snapshot.data_ptr) {
|
2018-10-23 23:58:20 -04:00
|
|
|
params.snapshot_blob = &d->snapshot_;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
d->AddIsolate(isolate);
|
|
|
|
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
auto context =
|
|
|
|
v8::Context::New(isolate, nullptr, v8::MaybeLocal<v8::ObjectTemplate>(),
|
|
|
|
v8::MaybeLocal<v8::Value>(),
|
|
|
|
v8::DeserializeInternalFieldsCallback(
|
|
|
|
deno::DeserializeInternalFields, nullptr));
|
2018-12-13 16:25:42 -05:00
|
|
|
if (!config.load_snapshot.data_ptr) {
|
2018-11-26 14:20:16 -05:00
|
|
|
// If no snapshot is provided, we initialize the context with empty
|
|
|
|
// main source code and source maps.
|
2018-12-13 16:25:42 -05:00
|
|
|
deno::InitializeContext(isolate, context);
|
2018-11-26 14:20:16 -05:00
|
|
|
}
|
2018-10-23 23:58:20 -04:00
|
|
|
d->context_.Reset(isolate, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<Deno*>(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
deno::DenoIsolate* unwrap(Deno* d_) {
|
|
|
|
return reinterpret_cast<deno::DenoIsolate*>(d_);
|
|
|
|
}
|
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
void deno_lock(Deno* d_) {
|
|
|
|
auto* d = unwrap(d_);
|
|
|
|
CHECK_NULL(d->locker_);
|
|
|
|
d->locker_ = new v8::Locker(d->isolate_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deno_unlock(Deno* d_) {
|
|
|
|
auto* d = unwrap(d_);
|
|
|
|
CHECK_NOT_NULL(d->locker_);
|
|
|
|
delete d->locker_;
|
|
|
|
d->locker_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-24 01:12:13 -04:00
|
|
|
deno_buf deno_get_snapshot(Deno* d_) {
|
|
|
|
auto* d = unwrap(d_);
|
2019-02-04 11:53:40 -05:00
|
|
|
CHECK_NOT_NULL(d->snapshot_creator_);
|
2018-11-29 15:40:04 -05:00
|
|
|
d->ClearModules();
|
2018-12-13 16:25:42 -05:00
|
|
|
d->context_.Reset();
|
2018-11-29 15:40:04 -05:00
|
|
|
|
2018-10-24 01:12:13 -04:00
|
|
|
auto blob = d->snapshot_creator_->CreateBlob(
|
2019-02-06 23:43:31 -05:00
|
|
|
v8::SnapshotCreator::FunctionCodeHandling::kKeep);
|
2018-10-24 01:12:13 -04:00
|
|
|
return {nullptr, 0, reinterpret_cast<uint8_t*>(const_cast<char*>(blob.data)),
|
2019-02-26 17:36:05 -05:00
|
|
|
blob.raw_size, 0};
|
2018-10-24 01:12:13 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:23:17 -05:00
|
|
|
static std::unique_ptr<v8::Platform> platform;
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
void deno_init() {
|
2019-03-04 18:09:35 -05:00
|
|
|
if (platform.get() == nullptr) {
|
|
|
|
platform = v8::platform::NewDefaultPlatform();
|
|
|
|
v8::V8::InitializePlatform(platform.get());
|
|
|
|
v8::V8::Initialize();
|
|
|
|
}
|
2018-10-23 23:58:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* deno_v8_version() { return v8::V8::GetVersion(); }
|
|
|
|
|
|
|
|
void deno_set_v8_flags(int* argc, char** argv) {
|
|
|
|
v8::V8::SetFlagsFromCommandLine(argc, argv, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* deno_last_exception(Deno* d_) {
|
|
|
|
auto* d = unwrap(d_);
|
2018-12-06 23:05:36 -05:00
|
|
|
if (d->last_exception_.length() > 0) {
|
|
|
|
return d->last_exception_.c_str();
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-23 23:58:20 -04:00
|
|
|
}
|
|
|
|
|
2019-02-01 19:46:54 -05:00
|
|
|
void deno_execute(Deno* d_, void* user_data, const char* js_filename,
|
|
|
|
const char* js_source) {
|
2018-10-23 23:58:20 -04:00
|
|
|
auto* d = unwrap(d_);
|
2018-12-04 18:06:20 -05:00
|
|
|
deno::UserDataScope user_data_scope(d, user_data);
|
2018-10-23 23:58:20 -04:00
|
|
|
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-12-13 16:25:42 -05:00
|
|
|
CHECK(!context.IsEmpty());
|
2019-02-01 19:46:54 -05:00
|
|
|
deno::Execute(context, js_filename, js_source);
|
2018-10-23 23:58:20 -04:00
|
|
|
}
|
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
void deno_zero_copy_release(Deno* d_, size_t zero_copy_id) {
|
|
|
|
auto* d = unwrap(d_);
|
|
|
|
v8::Isolate::Scope isolate_scope(d->isolate_);
|
|
|
|
v8::Locker locker(d->isolate_);
|
|
|
|
v8::HandleScope handle_scope(d->isolate_);
|
|
|
|
d->DeleteZeroCopyRef(zero_copy_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deno_respond(Deno* d_, void* user_data, deno_buf buf) {
|
2018-10-23 23:58:20 -04:00
|
|
|
auto* d = unwrap(d_);
|
|
|
|
if (d->current_args_ != nullptr) {
|
|
|
|
// Synchronous response.
|
2019-02-26 17:36:05 -05:00
|
|
|
if (buf.data_ptr != nullptr) {
|
|
|
|
DCHECK_EQ(buf.zero_copy_id, 0);
|
|
|
|
auto ab = deno::ImportBuf(d, buf);
|
|
|
|
d->current_args_->GetReturnValue().Set(ab);
|
|
|
|
}
|
2018-10-23 23:58:20 -04:00
|
|
|
d->current_args_ = nullptr;
|
2019-02-01 19:46:54 -05:00
|
|
|
return;
|
2018-10-23 23:58:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Asynchronous response.
|
2018-12-04 18:06:20 -05:00
|
|
|
deno::UserDataScope user_data_scope(d, user_data);
|
2018-10-23 23:58:20 -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_);
|
|
|
|
|
|
|
|
auto recv_ = d->recv_.Get(d->isolate_);
|
|
|
|
if (recv_.IsEmpty()) {
|
2019-03-26 08:22:07 -04:00
|
|
|
d->last_exception_ = "Deno.core.recv has not been called.";
|
2019-02-01 19:46:54 -05:00
|
|
|
return;
|
2018-10-23 23:58:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Value> args[1];
|
2019-02-26 17:36:05 -05:00
|
|
|
int argc = 0;
|
|
|
|
|
|
|
|
// You cannot use zero_copy_buf with deno_respond(). Use
|
|
|
|
// deno_zero_copy_release() instead.
|
|
|
|
DCHECK_EQ(buf.zero_copy_id, 0);
|
|
|
|
if (buf.data_ptr != nullptr) {
|
|
|
|
args[0] = deno::ImportBuf(d, buf);
|
|
|
|
argc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto v = recv_->Call(context, context->Global(), argc, args);
|
2018-10-23 23:58:20 -04:00
|
|
|
|
|
|
|
if (try_catch.HasCaught()) {
|
2018-12-23 17:08:08 -05:00
|
|
|
CHECK(v.IsEmpty());
|
2018-10-23 23:58:20 -04:00
|
|
|
deno::HandleException(context, try_catch.Exception());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void deno_check_promise_errors(Deno* d_) {
|
|
|
|
auto* d = unwrap(d_);
|
2018-12-06 23:05:36 -05:00
|
|
|
if (d->pending_promise_map_.size() > 0) {
|
2018-10-23 23:58:20 -04:00
|
|
|
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_);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
2018-12-06 23:05:36 -05:00
|
|
|
auto it = d->pending_promise_map_.begin();
|
|
|
|
while (it != d->pending_promise_map_.end()) {
|
|
|
|
auto error = it->second.Get(isolate);
|
|
|
|
deno::HandleException(context, error);
|
|
|
|
it = d->pending_promise_map_.erase(it);
|
2018-10-23 23:58:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void deno_delete(Deno* d_) {
|
|
|
|
deno::DenoIsolate* d = reinterpret_cast<deno::DenoIsolate*>(d_);
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void deno_terminate_execution(Deno* d_) {
|
|
|
|
deno::DenoIsolate* d = reinterpret_cast<deno::DenoIsolate*>(d_);
|
|
|
|
d->isolate_->TerminateExecution();
|
|
|
|
}
|
|
|
|
}
|