summaryrefslogtreecommitdiff
path: root/libdeno/api.cc
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-12-13 16:25:42 -0500
committerGitHub <noreply@github.com>2018-12-13 16:25:42 -0500
commitf986eb25c575747efddcbbcbd8ab429a32312983 (patch)
tree644e550afc88ae370bb0d0eb010b8dff21315156 /libdeno/api.cc
parent2cb52cc4d19b212c4a1095ca4f668fe62b237c7a (diff)
Merge deno_new_snapshotter behavior into deno_new (#1318)
Diffstat (limited to 'libdeno/api.cc')
-rw-r--r--libdeno/api.cc59
1 files changed, 34 insertions, 25 deletions
diff --git a/libdeno/api.cc b/libdeno/api.cc
index 388ab6146..472181819 100644
--- a/libdeno/api.cc
+++ b/libdeno/api.cc
@@ -13,13 +13,40 @@
extern "C" {
-Deno* deno_new(deno_buf snapshot, deno_config config) {
- deno::DenoIsolate* d = new deno::DenoIsolate(snapshot, config);
+Deno* deno_new_snapshotter(deno_config 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;
params.array_buffer_allocator = d->array_buffer_allocator_;
params.external_references = deno::external_references;
- if (snapshot.data_ptr) {
+ if (config.load_snapshot.data_ptr) {
params.snapshot_blob = &d->snapshot_;
}
@@ -35,10 +62,10 @@ Deno* deno_new(deno_buf snapshot, deno_config config) {
v8::MaybeLocal<v8::Value>(),
v8::DeserializeInternalFieldsCallback(
deno::DeserializeInternalFields, nullptr));
- if (!snapshot.data_ptr) {
+ if (!config.load_snapshot.data_ptr) {
// If no snapshot is provided, we initialize the context with empty
// main source code and source maps.
- deno::InitializeContext(isolate, context, "", "");
+ deno::InitializeContext(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);
}
-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_) {
return reinterpret_cast<deno::DenoIsolate*>(d_);
}
@@ -73,6 +80,7 @@ deno::DenoIsolate* unwrap(Deno* d_) {
deno_buf deno_get_snapshot(Deno* d_) {
auto* d = unwrap(d_);
CHECK_NE(d->snapshot_creator_, nullptr);
+ d->context_.Reset();
auto blob = d->snapshot_creator_->CreateBlob(
v8::SnapshotCreator::FunctionCodeHandling::kClear);
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::HandleScope handle_scope(isolate);
auto context = d->context_.Get(d->isolate_);
+ CHECK(!context.IsEmpty());
return deno::Execute(context, js_filename, js_source) ? 1 : 0;
}