2018-06-10 05:34:03 +02:00
|
|
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
|
|
|
// All rights reserved. MIT License.
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <string>
|
|
|
|
|
2018-07-03 17:15:32 +09:00
|
|
|
#include "third_party/v8/include/v8.h"
|
|
|
|
#include "third_party/v8/src/base/logging.h"
|
2018-06-10 05:34:03 +02:00
|
|
|
|
|
|
|
#include "./deno_internal.h"
|
|
|
|
#include "include/deno.h"
|
|
|
|
|
2018-06-11 17:01:35 +02:00
|
|
|
#ifdef DENO_MOCK_RUNTIME
|
|
|
|
#include "snapshot_mock_runtime.cc"
|
|
|
|
#else
|
2018-06-10 05:34:03 +02:00
|
|
|
#include "snapshot_deno.cc"
|
2018-06-11 17:01:35 +02:00
|
|
|
#endif
|
2018-06-10 05:34:03 +02:00
|
|
|
|
2018-06-18 16:02:08 +02:00
|
|
|
namespace deno {
|
|
|
|
|
2018-06-12 06:36:01 +02:00
|
|
|
std::vector<InternalFieldData*> deserialized_data;
|
|
|
|
|
|
|
|
void DeserializeInternalFields(v8::Local<v8::Object> holder, int index,
|
|
|
|
v8::StartupData payload, void* data) {
|
2018-06-18 16:02:08 +02:00
|
|
|
DCHECK_EQ(data, nullptr);
|
2018-06-12 06:36:01 +02: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);
|
|
|
|
}
|
|
|
|
|
2018-07-01 18:07:12 +02:00
|
|
|
Deno* NewFromSnapshot(void* data, deno_recv_cb cb) {
|
2018-06-10 05:34:03 +02:00
|
|
|
Deno* d = new Deno;
|
2018-06-13 19:38:22 +02:00
|
|
|
d->currentArgs = nullptr;
|
2018-06-10 05:34:03 +02:00
|
|
|
d->cb = cb;
|
|
|
|
d->data = data;
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.array_buffer_allocator =
|
|
|
|
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
|
|
|
params.external_references = external_references;
|
2018-06-18 16:02:08 +02:00
|
|
|
params.snapshot_blob = StartupBlob_snapshot();
|
2018-06-10 05:34:03 +02:00
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
2018-06-10 14:18:15 +02:00
|
|
|
AddIsolate(d, isolate);
|
2018-06-10 05:34:03 +02:00
|
|
|
|
2018-06-11 17:01:35 +02:00
|
|
|
v8::Locker locker(isolate);
|
2018-06-10 05:34:03 +02:00
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2018-06-12 06:36:01 +02:00
|
|
|
auto context =
|
|
|
|
v8::Context::New(isolate, nullptr, v8::MaybeLocal<v8::ObjectTemplate>(),
|
|
|
|
v8::MaybeLocal<v8::Value>(),
|
|
|
|
v8::DeserializeInternalFieldsCallback(
|
|
|
|
DeserializeInternalFields, nullptr));
|
2018-06-10 05:34:03 +02:00
|
|
|
d->context.Reset(d->isolate, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace deno
|
2018-06-10 14:18:15 +02:00
|
|
|
|
|
|
|
extern "C" {
|
2018-07-01 18:07:12 +02:00
|
|
|
Deno* deno_new(void* data, deno_recv_cb cb) {
|
2018-06-10 14:18:15 +02:00
|
|
|
return deno::NewFromSnapshot(data, cb);
|
|
|
|
}
|
|
|
|
}
|