1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-12 10:37:52 -05:00
denoland-deno/libdeno/from_snapshot.cc

85 lines
2.5 KiB
C++
Raw Normal View History

// Copyright 2018 the Deno authors. All rights reserved. MIT license.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
2018-07-03 04:15:32 -04:00
#include "third_party/v8/include/v8.h"
#include "third_party/v8/src/base/logging.h"
#include "deno.h"
2018-07-06 15:00:45 -04:00
#include "internal.h"
extern const char deno_snapshot_start asm("deno_snapshot_start");
extern const char deno_snapshot_end asm("deno_snapshot_end");
#ifdef LIBDENO_TEST
asm(".data\n"
"deno_snapshot_start: .incbin \"gen/snapshot_libdeno_test.bin\"\n"
"deno_snapshot_end:\n"
".globl deno_snapshot_start;\n"
".globl deno_snapshot_end;");
2018-06-11 11:01:35 -04:00
#else
asm(".data\n"
"deno_snapshot_start: .incbin \"gen/snapshot_deno.bin\"\n"
"deno_snapshot_end:\n"
".globl deno_snapshot_start;\n"
".globl deno_snapshot_end;");
#endif // LIBDENO_TEST
namespace deno {
2018-06-12 00:36:01 -04:00
std::vector<InternalFieldData*> deserialized_data;
void DeserializeInternalFields(v8::Local<v8::Object> holder, int index,
v8::StartupData payload, void* data) {
DCHECK_EQ(data, nullptr);
2018-06-12 00:36:01 -04:00
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(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 = external_references;
CHECK_NE(&deno_snapshot_start, nullptr);
int snapshot_len =
static_cast<int>(&deno_snapshot_end - &deno_snapshot_start);
static v8::StartupData snapshot = {&deno_snapshot_start, snapshot_len};
params.snapshot_blob = &snapshot;
v8::Isolate* isolate = v8::Isolate::New(params);
2018-06-10 08:18:15 -04:00
AddIsolate(d, isolate);
2018-06-11 11:01:35 -04:00
v8::Locker locker(isolate);
v8::Isolate::Scope isolate_scope(isolate);
{
v8::HandleScope handle_scope(isolate);
2018-06-12 00:36:01 -04:00
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
2018-06-10 08:18:15 -04:00
extern "C" {
Deno* deno_new(deno_recv_cb cb) { return deno::NewFromSnapshot(cb); }
2018-06-10 08:18:15 -04:00
}