diff --git a/libdeno/api.cc b/libdeno/api.cc index c059bb534a..eac6bdb619 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(d_); - d->isolate_->Dispose(); delete d; } diff --git a/libdeno/deno.h b/libdeno/deno.h index 96d1298c14..4941709a99 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 a996a00086..0113946d54 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(snapshot.data_ptr); snapshot_.raw_size = static_cast(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* current_args_; v8::SnapshotCreator* snapshot_creator_; diff --git a/libdeno/libdeno_test.cc b/libdeno/libdeno_test.cc index 7e7cd966c3..3af51a5a98 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) {