mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 12:58:54 -05:00
Merge deno_new_snapshotter behavior into deno_new (#1318)
This commit is contained in:
parent
2cb52cc4d1
commit
f986eb25c5
8 changed files with 85 additions and 80 deletions
|
@ -13,13 +13,40 @@
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
Deno* deno_new(deno_buf snapshot, deno_config config) {
|
Deno* deno_new_snapshotter(deno_config config) {
|
||||||
deno::DenoIsolate* d = new deno::DenoIsolate(snapshot, config);
|
CHECK(config.will_snapshot);
|
||||||
|
// TODO Support loading snapshots before snapshotting.
|
||||||
|
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);
|
||||||
v8::Isolate::CreateParams params;
|
v8::Isolate::CreateParams params;
|
||||||
params.array_buffer_allocator = d->array_buffer_allocator_;
|
params.array_buffer_allocator = d->array_buffer_allocator_;
|
||||||
params.external_references = deno::external_references;
|
params.external_references = deno::external_references;
|
||||||
|
|
||||||
if (snapshot.data_ptr) {
|
if (config.load_snapshot.data_ptr) {
|
||||||
params.snapshot_blob = &d->snapshot_;
|
params.snapshot_blob = &d->snapshot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,10 +62,10 @@ Deno* deno_new(deno_buf snapshot, deno_config config) {
|
||||||
v8::MaybeLocal<v8::Value>(),
|
v8::MaybeLocal<v8::Value>(),
|
||||||
v8::DeserializeInternalFieldsCallback(
|
v8::DeserializeInternalFieldsCallback(
|
||||||
deno::DeserializeInternalFields, nullptr));
|
deno::DeserializeInternalFields, nullptr));
|
||||||
if (!snapshot.data_ptr) {
|
if (!config.load_snapshot.data_ptr) {
|
||||||
// If no snapshot is provided, we initialize the context with empty
|
// If no snapshot is provided, we initialize the context with empty
|
||||||
// main source code and source maps.
|
// main source code and source maps.
|
||||||
deno::InitializeContext(isolate, context, "", "");
|
deno::InitializeContext(isolate, context);
|
||||||
}
|
}
|
||||||
d->context_.Reset(isolate, context);
|
d->context_.Reset(isolate, context);
|
||||||
}
|
}
|
||||||
|
@ -46,26 +73,6 @@ Deno* deno_new(deno_buf snapshot, deno_config config) {
|
||||||
return reinterpret_cast<Deno*>(d);
|
return reinterpret_cast<Deno*>(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
Deno* deno_new_snapshotter(deno_config config, const char* js_filename,
|
|
||||||
const char* js_source) {
|
|
||||||
auto* creator = new v8::SnapshotCreator(deno::external_references);
|
|
||||||
auto* isolate = creator->GetIsolate();
|
|
||||||
auto* d = new deno::DenoIsolate(deno::empty_buf, 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);
|
|
||||||
creator->SetDefaultContext(context,
|
|
||||||
v8::SerializeInternalFieldsCallback(
|
|
||||||
deno::SerializeInternalFields, nullptr));
|
|
||||||
deno::InitializeContext(isolate, context, js_filename, js_source);
|
|
||||||
}
|
|
||||||
return reinterpret_cast<Deno*>(d);
|
|
||||||
}
|
|
||||||
|
|
||||||
deno::DenoIsolate* unwrap(Deno* d_) {
|
deno::DenoIsolate* unwrap(Deno* d_) {
|
||||||
return reinterpret_cast<deno::DenoIsolate*>(d_);
|
return reinterpret_cast<deno::DenoIsolate*>(d_);
|
||||||
}
|
}
|
||||||
|
@ -73,6 +80,7 @@ deno::DenoIsolate* unwrap(Deno* d_) {
|
||||||
deno_buf deno_get_snapshot(Deno* d_) {
|
deno_buf deno_get_snapshot(Deno* d_) {
|
||||||
auto* d = unwrap(d_);
|
auto* d = unwrap(d_);
|
||||||
CHECK_NE(d->snapshot_creator_, nullptr);
|
CHECK_NE(d->snapshot_creator_, nullptr);
|
||||||
|
d->context_.Reset();
|
||||||
auto blob = d->snapshot_creator_->CreateBlob(
|
auto blob = d->snapshot_creator_->CreateBlob(
|
||||||
v8::SnapshotCreator::FunctionCodeHandling::kClear);
|
v8::SnapshotCreator::FunctionCodeHandling::kClear);
|
||||||
return {nullptr, 0, reinterpret_cast<uint8_t*>(const_cast<char*>(blob.data)),
|
return {nullptr, 0, reinterpret_cast<uint8_t*>(const_cast<char*>(blob.data)),
|
||||||
|
@ -111,6 +119,7 @@ int deno_execute(Deno* d_, void* user_data, const char* js_filename,
|
||||||
v8::Isolate::Scope isolate_scope(isolate);
|
v8::Isolate::Scope isolate_scope(isolate);
|
||||||
v8::HandleScope handle_scope(isolate);
|
v8::HandleScope handle_scope(isolate);
|
||||||
auto context = d->context_.Get(d->isolate_);
|
auto context = d->context_.Get(d->isolate_);
|
||||||
|
CHECK(!context.IsEmpty());
|
||||||
return deno::Execute(context, js_filename, js_source) ? 1 : 0;
|
return deno::Execute(context, js_filename, js_source) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -154,13 +154,8 @@ void HandleException(v8::Local<v8::Context> context,
|
||||||
v8::Isolate* isolate = context->GetIsolate();
|
v8::Isolate* isolate = context->GetIsolate();
|
||||||
DenoIsolate* d = FromIsolate(isolate);
|
DenoIsolate* d = FromIsolate(isolate);
|
||||||
std::string json_str = EncodeExceptionAsJSON(context, exception);
|
std::string json_str = EncodeExceptionAsJSON(context, exception);
|
||||||
if (d != nullptr) {
|
CHECK(d != nullptr);
|
||||||
d->last_exception_ = json_str;
|
d->last_exception_ = json_str;
|
||||||
} else {
|
|
||||||
// This shouldn't happen in normal circumstances. Added for debugging.
|
|
||||||
std::cerr << "Pre-Deno Exception " << json_str << std::endl;
|
|
||||||
CHECK(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PromiseRejectCallback(v8::PromiseRejectMessage promise_reject_message) {
|
void PromiseRejectCallback(v8::PromiseRejectMessage promise_reject_message) {
|
||||||
|
@ -396,10 +391,7 @@ bool Execute(v8::Local<v8::Context> context, const char* js_filename,
|
||||||
return ExecuteV8StringSource(context, js_filename, source);
|
return ExecuteV8StringSource(context, js_filename, source);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context,
|
void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context) {
|
||||||
const char* js_filename, const char* js_source) {
|
|
||||||
CHECK_NE(js_source, nullptr);
|
|
||||||
CHECK_NE(js_filename, nullptr);
|
|
||||||
v8::HandleScope handle_scope(isolate);
|
v8::HandleScope handle_scope(isolate);
|
||||||
v8::Context::Scope context_scope(context);
|
v8::Context::Scope context_scope(context);
|
||||||
|
|
||||||
|
@ -422,13 +414,6 @@ void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context,
|
||||||
|
|
||||||
CHECK(deno_val->SetAccessor(context, deno::v8_str("shared"), Shared)
|
CHECK(deno_val->SetAccessor(context, deno::v8_str("shared"), Shared)
|
||||||
.FromJust());
|
.FromJust());
|
||||||
|
|
||||||
{
|
|
||||||
auto source = deno::v8_str(js_source);
|
|
||||||
|
|
||||||
bool r = deno::ExecuteV8StringSource(context, js_filename, source);
|
|
||||||
CHECK(r);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DenoIsolate::AddIsolate(v8::Isolate* isolate) {
|
void DenoIsolate::AddIsolate(v8::Isolate* isolate) {
|
||||||
|
|
|
@ -30,14 +30,13 @@ const char* deno_v8_version();
|
||||||
void deno_set_v8_flags(int* argc, char** argv);
|
void deno_set_v8_flags(int* argc, char** argv);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
deno_buf shared; // Shared buffer to be mapped to libdeno.shared
|
int will_snapshot; // Default 0. If calling deno_get_snapshot 1.
|
||||||
deno_recv_cb recv_cb; // Maps to libdeno.send() calls.
|
deno_buf load_snapshot; // Optionally: A deno_buf from deno_get_snapshot.
|
||||||
|
deno_buf shared; // Shared buffer to be mapped to libdeno.shared
|
||||||
|
deno_recv_cb recv_cb; // Maps to libdeno.send() calls.
|
||||||
} deno_config;
|
} deno_config;
|
||||||
|
|
||||||
Deno* deno_new(deno_buf snapshot, deno_config config);
|
Deno* deno_new(deno_config config);
|
||||||
|
|
||||||
Deno* deno_new_snapshotter(deno_config config, const char* js_filename,
|
|
||||||
const char* js_source);
|
|
||||||
|
|
||||||
// Generate a snapshot. The resulting buf can be used with deno_new.
|
// Generate a snapshot. The resulting buf can be used with deno_new.
|
||||||
// The caller must free the returned data by calling delete[] buf.data_ptr.
|
// The caller must free the returned data by calling delete[] buf.data_ptr.
|
||||||
|
|
|
@ -13,7 +13,7 @@ namespace deno {
|
||||||
// deno_s = Wrapped Isolate.
|
// deno_s = Wrapped Isolate.
|
||||||
class DenoIsolate {
|
class DenoIsolate {
|
||||||
public:
|
public:
|
||||||
DenoIsolate(deno_buf snapshot, deno_config config)
|
DenoIsolate(deno_config config)
|
||||||
: isolate_(nullptr),
|
: isolate_(nullptr),
|
||||||
shared_(config.shared),
|
shared_(config.shared),
|
||||||
current_args_(nullptr),
|
current_args_(nullptr),
|
||||||
|
@ -23,9 +23,10 @@ class DenoIsolate {
|
||||||
next_req_id_(0),
|
next_req_id_(0),
|
||||||
user_data_(nullptr) {
|
user_data_(nullptr) {
|
||||||
array_buffer_allocator_ = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
array_buffer_allocator_ = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
||||||
if (snapshot.data_ptr) {
|
if (config.load_snapshot.data_ptr) {
|
||||||
snapshot_.data = reinterpret_cast<const char*>(snapshot.data_ptr);
|
snapshot_.data =
|
||||||
snapshot_.raw_size = static_cast<int>(snapshot.data_len);
|
reinterpret_cast<const char*>(config.load_snapshot.data_ptr);
|
||||||
|
snapshot_.raw_size = static_cast<int>(config.load_snapshot.data_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,8 +99,7 @@ static const deno_buf empty_buf = {nullptr, 0, nullptr, 0};
|
||||||
|
|
||||||
Deno* NewFromSnapshot(void* user_data, deno_recv_cb cb);
|
Deno* NewFromSnapshot(void* user_data, deno_recv_cb cb);
|
||||||
|
|
||||||
void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context,
|
void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context);
|
||||||
const char* js_filename, const char* js_source);
|
|
||||||
|
|
||||||
void HandleException(v8::Local<v8::Context> context,
|
void HandleException(v8::Local<v8::Context> context,
|
||||||
v8::Local<v8::Value> exception);
|
v8::Local<v8::Value> exception);
|
||||||
|
|
|
@ -3,30 +3,29 @@
|
||||||
|
|
||||||
TEST(LibDenoTest, InitializesCorrectly) {
|
TEST(LibDenoTest, InitializesCorrectly) {
|
||||||
EXPECT_NE(snapshot.data_ptr, nullptr);
|
EXPECT_NE(snapshot.data_ptr, nullptr);
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, nullptr});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr});
|
||||||
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "1 + 2"));
|
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "1 + 2"));
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LibDenoTest, InitializesCorrectlyWithoutSnapshot) {
|
TEST(LibDenoTest, InitializesCorrectlyWithoutSnapshot) {
|
||||||
Deno* d = deno_new(empty, deno_config{empty, nullptr});
|
Deno* d = deno_new(deno_config{0, empty, empty, nullptr});
|
||||||
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "1 + 2"));
|
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "1 + 2"));
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LibDenoTest, SnapshotterInitializesCorrectly) {
|
TEST(LibDenoTest, SnapshotterInitializesCorrectly) {
|
||||||
Deno* d =
|
Deno* d = deno_new(deno_config{1, empty, empty, nullptr});
|
||||||
deno_new_snapshotter(deno_config{empty, nullptr}, "a.js", "a = 1 + 2");
|
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LibDenoTest, Snapshotter) {
|
TEST(LibDenoTest, Snapshotter) {
|
||||||
Deno* d1 =
|
Deno* d1 = deno_new(deno_config{1, empty, empty, nullptr});
|
||||||
deno_new_snapshotter(deno_config{empty, nullptr}, "a.js", "a = 1 + 2");
|
EXPECT_TRUE(deno_execute(d1, nullptr, "a.js", "a = 1 + 2"));
|
||||||
deno_buf test_snapshot = deno_get_snapshot(d1);
|
deno_buf test_snapshot = deno_get_snapshot(d1);
|
||||||
deno_delete(d1);
|
deno_delete(d1);
|
||||||
|
|
||||||
Deno* d2 = deno_new(test_snapshot, deno_config{empty, nullptr});
|
Deno* d2 = deno_new(deno_config{0, test_snapshot, empty, nullptr});
|
||||||
EXPECT_TRUE(
|
EXPECT_TRUE(
|
||||||
deno_execute(d2, nullptr, "b.js", "if (a != 3) throw Error('x');"));
|
deno_execute(d2, nullptr, "b.js", "if (a != 3) throw Error('x');"));
|
||||||
deno_delete(d2);
|
deno_delete(d2);
|
||||||
|
@ -35,14 +34,14 @@ TEST(LibDenoTest, Snapshotter) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LibDenoTest, CanCallFunction) {
|
TEST(LibDenoTest, CanCallFunction) {
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, nullptr});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr});
|
||||||
EXPECT_TRUE(deno_execute(d, nullptr, "a.js",
|
EXPECT_TRUE(deno_execute(d, nullptr, "a.js",
|
||||||
"if (CanCallFunction() != 'foo') throw Error();"));
|
"if (CanCallFunction() != 'foo') throw Error();"));
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LibDenoTest, ErrorsCorrectly) {
|
TEST(LibDenoTest, ErrorsCorrectly) {
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, nullptr});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr});
|
||||||
EXPECT_FALSE(deno_execute(d, nullptr, "a.js", "throw Error()"));
|
EXPECT_FALSE(deno_execute(d, nullptr, "a.js", "throw Error()"));
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
}
|
}
|
||||||
|
@ -87,7 +86,7 @@ TEST(LibDenoTest, RecvReturnEmpty) {
|
||||||
EXPECT_EQ(buf.data_ptr[1], 'b');
|
EXPECT_EQ(buf.data_ptr[1], 'b');
|
||||||
EXPECT_EQ(buf.data_ptr[2], 'c');
|
EXPECT_EQ(buf.data_ptr[2], 'c');
|
||||||
};
|
};
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, recv_cb});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb});
|
||||||
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "RecvReturnEmpty()"));
|
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "RecvReturnEmpty()"));
|
||||||
EXPECT_EQ(count, 2);
|
EXPECT_EQ(count, 2);
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
|
@ -105,14 +104,14 @@ TEST(LibDenoTest, RecvReturnBar) {
|
||||||
EXPECT_EQ(buf.data_ptr[2], 'c');
|
EXPECT_EQ(buf.data_ptr[2], 'c');
|
||||||
deno_respond(d, user_data, req_id, strbuf("bar"));
|
deno_respond(d, user_data, req_id, strbuf("bar"));
|
||||||
};
|
};
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, recv_cb});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb});
|
||||||
EXPECT_TRUE(deno_execute(d, d, "a.js", "RecvReturnBar()"));
|
EXPECT_TRUE(deno_execute(d, d, "a.js", "RecvReturnBar()"));
|
||||||
EXPECT_EQ(count, 1);
|
EXPECT_EQ(count, 1);
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LibDenoTest, DoubleRecvFails) {
|
TEST(LibDenoTest, DoubleRecvFails) {
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, nullptr});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr});
|
||||||
EXPECT_FALSE(deno_execute(d, nullptr, "a.js", "DoubleRecvFails()"));
|
EXPECT_FALSE(deno_execute(d, nullptr, "a.js", "DoubleRecvFails()"));
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
}
|
}
|
||||||
|
@ -146,7 +145,7 @@ TEST(LibDenoTest, SendRecvSlice) {
|
||||||
// Send back.
|
// Send back.
|
||||||
deno_respond(d, user_data, req_id, buf2);
|
deno_respond(d, user_data, req_id, buf2);
|
||||||
};
|
};
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, recv_cb});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb});
|
||||||
EXPECT_TRUE(deno_execute(d, d, "a.js", "SendRecvSlice()"));
|
EXPECT_TRUE(deno_execute(d, d, "a.js", "SendRecvSlice()"));
|
||||||
EXPECT_EQ(count, 5);
|
EXPECT_EQ(count, 5);
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
|
@ -163,26 +162,26 @@ TEST(LibDenoTest, JSSendArrayBufferViewTypes) {
|
||||||
EXPECT_EQ(buf.alloc_len, 4321u);
|
EXPECT_EQ(buf.alloc_len, 4321u);
|
||||||
EXPECT_EQ(buf.data_ptr[0], count);
|
EXPECT_EQ(buf.data_ptr[0], count);
|
||||||
};
|
};
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, recv_cb});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb});
|
||||||
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "JSSendArrayBufferViewTypes()"));
|
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "JSSendArrayBufferViewTypes()"));
|
||||||
EXPECT_EQ(count, 3);
|
EXPECT_EQ(count, 3);
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LibDenoTest, TypedArraySnapshots) {
|
TEST(LibDenoTest, TypedArraySnapshots) {
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, nullptr});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr});
|
||||||
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "TypedArraySnapshots()"));
|
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "TypedArraySnapshots()"));
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LibDenoTest, SnapshotBug) {
|
TEST(LibDenoTest, SnapshotBug) {
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, nullptr});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr});
|
||||||
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "SnapshotBug()"));
|
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "SnapshotBug()"));
|
||||||
deno_delete(d);
|
deno_delete(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LibDenoTest, GlobalErrorHandling) {
|
TEST(LibDenoTest, GlobalErrorHandling) {
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, nullptr});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr});
|
||||||
EXPECT_FALSE(deno_execute(d, nullptr, "a.js", "GlobalErrorHandling()"));
|
EXPECT_FALSE(deno_execute(d, nullptr, "a.js", "GlobalErrorHandling()"));
|
||||||
// We only check that it starts with this string, so we don't have to check
|
// We only check that it starts with this string, so we don't have to check
|
||||||
// the second frame, which contains line numbers in libdeno_test.js and may
|
// the second frame, which contains line numbers in libdeno_test.js and may
|
||||||
|
@ -210,7 +209,7 @@ TEST(LibDenoTest, DataBuf) {
|
||||||
EXPECT_EQ(buf.data_ptr[0], 1);
|
EXPECT_EQ(buf.data_ptr[0], 1);
|
||||||
EXPECT_EQ(buf.data_ptr[1], 2);
|
EXPECT_EQ(buf.data_ptr[1], 2);
|
||||||
};
|
};
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, recv_cb});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb});
|
||||||
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "DataBuf()"));
|
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "DataBuf()"));
|
||||||
EXPECT_EQ(count, 1);
|
EXPECT_EQ(count, 1);
|
||||||
// data_buf was subsequently changed in JS, let's check that our copy reflects
|
// data_buf was subsequently changed in JS, let's check that our copy reflects
|
||||||
|
@ -223,7 +222,7 @@ TEST(LibDenoTest, DataBuf) {
|
||||||
TEST(LibDenoTest, CheckPromiseErrors) {
|
TEST(LibDenoTest, CheckPromiseErrors) {
|
||||||
static int count = 0;
|
static int count = 0;
|
||||||
auto recv_cb = [](auto _, int req_id, auto buf, auto data_buf) { count++; };
|
auto recv_cb = [](auto _, int req_id, auto buf, auto data_buf) { count++; };
|
||||||
Deno* d = deno_new(snapshot, deno_config{empty, recv_cb});
|
Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb});
|
||||||
EXPECT_EQ(deno_last_exception(d), nullptr);
|
EXPECT_EQ(deno_last_exception(d), nullptr);
|
||||||
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "CheckPromiseErrors()"));
|
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "CheckPromiseErrors()"));
|
||||||
EXPECT_EQ(deno_last_exception(d), nullptr);
|
EXPECT_EQ(deno_last_exception(d), nullptr);
|
||||||
|
@ -236,7 +235,7 @@ TEST(LibDenoTest, CheckPromiseErrors) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(LibDenoTest, LastException) {
|
TEST(LibDenoTest, LastException) {
|
||||||
Deno* d = deno_new(empty, deno_config{empty, nullptr});
|
Deno* d = deno_new(deno_config{0, empty, empty, nullptr});
|
||||||
EXPECT_EQ(deno_last_exception(d), nullptr);
|
EXPECT_EQ(deno_last_exception(d), nullptr);
|
||||||
EXPECT_FALSE(deno_execute(d, nullptr, "a.js", "\n\nthrow Error('boo');\n\n"));
|
EXPECT_FALSE(deno_execute(d, nullptr, "a.js", "\n\nthrow Error('boo');\n\n"));
|
||||||
EXPECT_STREQ(deno_last_exception(d),
|
EXPECT_STREQ(deno_last_exception(d),
|
||||||
|
@ -251,7 +250,7 @@ TEST(LibDenoTest, LastException) {
|
||||||
TEST(LibDenoTest, Shared) {
|
TEST(LibDenoTest, Shared) {
|
||||||
uint8_t s[] = {0, 1, 2};
|
uint8_t s[] = {0, 1, 2};
|
||||||
deno_buf shared = {nullptr, 0, s, 3};
|
deno_buf shared = {nullptr, 0, s, 3};
|
||||||
Deno* d = deno_new(snapshot, deno_config{shared, nullptr});
|
Deno* d = deno_new(deno_config{0, snapshot, shared, nullptr});
|
||||||
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "Shared()"));
|
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "Shared()"));
|
||||||
EXPECT_EQ(s[0], 42);
|
EXPECT_EQ(s[0], 42);
|
||||||
EXPECT_EQ(s[1], 43);
|
EXPECT_EQ(s[1], 43);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||||
// Hint: --trace_serializer is a useful debugging flag.
|
// Hint: --trace_serializer is a useful debugging flag.
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
#include "deno.h"
|
#include "deno.h"
|
||||||
#include "file_util.h"
|
#include "file_util.h"
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
|
@ -22,8 +23,16 @@ int main(int argc, char** argv) {
|
||||||
CHECK(deno::ReadFileToString(js_fn, &js_source));
|
CHECK(deno::ReadFileToString(js_fn, &js_source));
|
||||||
|
|
||||||
deno_init();
|
deno_init();
|
||||||
deno_config config = {deno::empty_buf, nullptr};
|
deno_config config = {1, deno::empty_buf, deno::empty_buf, nullptr};
|
||||||
Deno* d = deno_new_snapshotter(config, js_fn, js_source.c_str());
|
Deno* d = deno_new(config);
|
||||||
|
|
||||||
|
int r = deno_execute(d, nullptr, js_fn, js_source.c_str());
|
||||||
|
if (!r) {
|
||||||
|
std::cerr << "Snapshot Exception " << std::endl;
|
||||||
|
std::cerr << deno_last_exception(d) << std::endl;
|
||||||
|
deno_delete(d);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
auto snapshot = deno_get_snapshot(d);
|
auto snapshot = deno_get_snapshot(d);
|
||||||
|
|
||||||
|
|
|
@ -144,10 +144,12 @@ impl Isolate {
|
||||||
unsafe { libdeno::deno_init() };
|
unsafe { libdeno::deno_init() };
|
||||||
});
|
});
|
||||||
let config = libdeno::deno_config {
|
let config = libdeno::deno_config {
|
||||||
|
will_snapshot: 0,
|
||||||
|
load_snapshot: snapshot,
|
||||||
shared: libdeno::deno_buf::empty(), // TODO Use for message passing.
|
shared: libdeno::deno_buf::empty(), // TODO Use for message passing.
|
||||||
recv_cb: pre_dispatch,
|
recv_cb: pre_dispatch,
|
||||||
};
|
};
|
||||||
let libdeno_isolate = unsafe { libdeno::deno_new(snapshot, config) };
|
let libdeno_isolate = unsafe { libdeno::deno_new(config) };
|
||||||
// This channel handles sending async messages back to the runtime.
|
// This channel handles sending async messages back to the runtime.
|
||||||
let (tx, rx) = mpsc::channel::<(i32, Buf)>();
|
let (tx, rx) = mpsc::channel::<(i32, Buf)>();
|
||||||
|
|
||||||
|
|
|
@ -111,6 +111,8 @@ type DenoRecvCb = unsafe extern "C" fn(
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct deno_config {
|
pub struct deno_config {
|
||||||
|
pub will_snapshot: c_int,
|
||||||
|
pub load_snapshot: deno_buf,
|
||||||
pub shared: deno_buf,
|
pub shared: deno_buf,
|
||||||
pub recv_cb: DenoRecvCb,
|
pub recv_cb: DenoRecvCb,
|
||||||
}
|
}
|
||||||
|
@ -119,7 +121,7 @@ extern "C" {
|
||||||
pub fn deno_init();
|
pub fn deno_init();
|
||||||
pub fn deno_v8_version() -> *const c_char;
|
pub fn deno_v8_version() -> *const c_char;
|
||||||
pub fn deno_set_v8_flags(argc: *mut c_int, argv: *mut *mut c_char);
|
pub fn deno_set_v8_flags(argc: *mut c_int, argv: *mut *mut c_char);
|
||||||
pub fn deno_new(snapshot: deno_buf, config: deno_config) -> *const isolate;
|
pub fn deno_new(config: deno_config) -> *const isolate;
|
||||||
pub fn deno_delete(i: *const isolate);
|
pub fn deno_delete(i: *const isolate);
|
||||||
pub fn deno_last_exception(i: *const isolate) -> *const c_char;
|
pub fn deno_last_exception(i: *const isolate) -> *const c_char;
|
||||||
pub fn deno_check_promise_errors(i: *const isolate);
|
pub fn deno_check_promise_errors(i: *const isolate);
|
||||||
|
|
Loading…
Reference in a new issue