summaryrefslogtreecommitdiff
path: root/libdeno
diff options
context:
space:
mode:
Diffstat (limited to 'libdeno')
-rw-r--r--libdeno/api.cc7
-rw-r--r--libdeno/deno.h2
-rw-r--r--libdeno/internal.h11
-rw-r--r--libdeno/libdeno_test.cc9
4 files changed, 24 insertions, 5 deletions
diff --git a/libdeno/api.cc b/libdeno/api.cc
index c059bb534..eac6bdb61 100644
--- a/libdeno/api.cc
+++ b/libdeno/api.cc
@@ -16,8 +16,7 @@ extern "C" {
Deno* deno_new(deno_buf snapshot, deno_buf shared, deno_recv_cb cb) {
deno::DenoIsolate* d = new deno::DenoIsolate(snapshot, cb, shared);
v8::Isolate::CreateParams params;
- params.array_buffer_allocator =
- v8::ArrayBuffer::Allocator::NewDefaultAllocator();
+ params.array_buffer_allocator = d->array_buffer_allocator_;
params.external_references = deno::external_references;
if (snapshot.data_ptr) {
@@ -55,8 +54,9 @@ Deno* deno_new_snapshotter(deno_buf shared, deno_recv_cb cb,
auto* d = new deno::DenoIsolate(deno::empty_buf, cb, shared);
d->snapshot_creator_ = creator;
d->AddIsolate(isolate);
- v8::Isolate::Scope isolate_scope(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,
@@ -186,7 +186,6 @@ void deno_check_promise_errors(Deno* d_) {
void deno_delete(Deno* d_) {
deno::DenoIsolate* d = reinterpret_cast<deno::DenoIsolate*>(d_);
- d->isolate_->Dispose();
delete d;
}
diff --git a/libdeno/deno.h b/libdeno/deno.h
index 96d1298c1..4941709a9 100644
--- a/libdeno/deno.h
+++ b/libdeno/deno.h
@@ -34,6 +34,8 @@ Deno* deno_new(deno_buf snapshot, deno_buf shared, deno_recv_cb cb);
Deno* deno_new_snapshotter(deno_buf shared, deno_recv_cb cb,
const char* js_filename, const char* js_source,
const char* source_map);
+// 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.
deno_buf deno_get_snapshot(Deno* d);
void deno_delete(Deno* d);
diff --git a/libdeno/internal.h b/libdeno/internal.h
index a996a0008..0113946d5 100644
--- a/libdeno/internal.h
+++ b/libdeno/internal.h
@@ -23,15 +23,26 @@ class DenoIsolate {
cb_(cb),
next_req_id_(0),
user_data_(nullptr) {
+ array_buffer_allocator_ = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
if (snapshot.data_ptr) {
snapshot_.data = reinterpret_cast<const char*>(snapshot.data_ptr);
snapshot_.raw_size = static_cast<int>(snapshot.data_len);
}
}
+ ~DenoIsolate() {
+ if (snapshot_creator_) {
+ delete snapshot_creator_;
+ } else {
+ isolate_->Dispose();
+ }
+ delete array_buffer_allocator_;
+ }
+
void AddIsolate(v8::Isolate* isolate);
v8::Isolate* isolate_;
+ v8::ArrayBuffer::Allocator* array_buffer_allocator_;
deno_buf shared_;
const v8::FunctionCallbackInfo<v8::Value>* current_args_;
v8::SnapshotCreator* snapshot_creator_;
diff --git a/libdeno/libdeno_test.cc b/libdeno/libdeno_test.cc
index 7e7cd966c..3af51a5a9 100644
--- a/libdeno/libdeno_test.cc
+++ b/libdeno/libdeno_test.cc
@@ -14,15 +14,22 @@ TEST(LibDenoTest, InitializesCorrectlyWithoutSnapshot) {
deno_delete(d);
}
+TEST(LibDenoTest, SnapshotterInitializesCorrectly) {
+ Deno* d = deno_new_snapshotter(empty, nullptr, "a.js", "a = 1 + 2", nullptr);
+ deno_delete(d);
+}
+
TEST(LibDenoTest, Snapshotter) {
Deno* d1 = deno_new_snapshotter(empty, nullptr, "a.js", "a = 1 + 2", nullptr);
deno_buf test_snapshot = deno_get_snapshot(d1);
- // TODO(ry) deno_delete(d1);
+ deno_delete(d1);
Deno* d2 = deno_new(test_snapshot, empty, nullptr);
EXPECT_TRUE(
deno_execute(d2, nullptr, "b.js", "if (a != 3) throw Error('x');"));
deno_delete(d2);
+
+ delete[] test_snapshot.data_ptr;
}
TEST(LibDenoTest, CanCallFunction) {