diff options
Diffstat (limited to 'libdeno/new.cc')
-rw-r--r-- | libdeno/new.cc | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/libdeno/new.cc b/libdeno/new.cc new file mode 100644 index 000000000..5223f0604 --- /dev/null +++ b/libdeno/new.cc @@ -0,0 +1,67 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <string> + +#include "third_party/v8/include/v8.h" +#include "third_party/v8/src/base/logging.h" + +#include "deno.h" +#include "internal.h" + +namespace deno { + +std::vector<InternalFieldData*> deserialized_data; + +void DeserializeInternalFields(v8::Local<v8::Object> holder, int index, + v8::StartupData payload, void* data) { + DCHECK_EQ(data, nullptr); + if (payload.raw_size == 0) { + holder->SetAlignedPointerInInternalField(index, nullptr); + return; + } + InternalFieldData* embedder_field = new InternalFieldData{0}; + memcpy(embedder_field, payload.data, payload.raw_size); + holder->SetAlignedPointerInInternalField(index, embedder_field); + deserialized_data.push_back(embedder_field); +} + +} // namespace deno + +extern "C" { + +Deno* deno_new(deno_buf snapshot, deno_recv_cb cb) { + Deno* d = new Deno; + d->currentArgs = nullptr; + d->cb = cb; + d->user_data = nullptr; + v8::Isolate::CreateParams params; + params.array_buffer_allocator = + v8::ArrayBuffer::Allocator::NewDefaultAllocator(); + params.external_references = deno::external_references; + + if (snapshot.data_ptr) { + d->snapshot.data = reinterpret_cast<const char*>(snapshot.data_ptr); + d->snapshot.raw_size = static_cast<int>(snapshot.data_len); + params.snapshot_blob = &d->snapshot; + } + + v8::Isolate* isolate = v8::Isolate::New(params); + deno::AddIsolate(d, 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)); + d->context.Reset(d->isolate, context); + } + + return d; +} +} |