mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 08:09:08 -05:00
70 lines
2 KiB
C++
70 lines
2 KiB
C++
// 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"
|
|
|
|
#ifdef DENO_MOCK_RUNTIME
|
|
#include "snapshot_mock_runtime.cc"
|
|
#else
|
|
#include "snapshot_deno.cc"
|
|
#endif
|
|
|
|
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);
|
|
}
|
|
|
|
Deno* NewFromSnapshot(void* data, deno_recv_cb cb) {
|
|
Deno* d = new Deno;
|
|
d->currentArgs = nullptr;
|
|
d->cb = cb;
|
|
d->data = data;
|
|
v8::Isolate::CreateParams params;
|
|
params.array_buffer_allocator =
|
|
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
|
params.external_references = external_references;
|
|
params.snapshot_blob = StartupBlob_snapshot();
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
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(
|
|
DeserializeInternalFields, nullptr));
|
|
d->context.Reset(d->isolate, context);
|
|
}
|
|
|
|
return d;
|
|
}
|
|
|
|
} // namespace deno
|
|
|
|
extern "C" {
|
|
Deno* deno_new(void* data, deno_recv_cb cb) {
|
|
return deno::NewFromSnapshot(data, cb);
|
|
}
|
|
}
|