2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-06-09 23:34:03 -04:00
|
|
|
#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"
|
2018-06-09 23:34:03 -04:00
|
|
|
|
2018-07-06 03:19:19 -04:00
|
|
|
#include "deno.h"
|
2018-07-06 15:00:45 -04:00
|
|
|
#include "internal.h"
|
2018-06-09 23:34:03 -04:00
|
|
|
|
2018-07-26 23:21:10 -04:00
|
|
|
extern const char deno_snapshot_start asm("deno_snapshot_start");
|
|
|
|
extern const char deno_snapshot_end asm("deno_snapshot_end");
|
2018-08-10 15:09:28 -04:00
|
|
|
#ifdef LIBDENO_TEST
|
2018-07-26 23:21:10 -04:00
|
|
|
asm(".data\n"
|
2018-08-10 15:09:28 -04:00
|
|
|
"deno_snapshot_start: .incbin \"gen/snapshot_libdeno_test.bin\"\n"
|
2018-07-26 23:21:10 -04:00
|
|
|
"deno_snapshot_end:\n"
|
|
|
|
".globl deno_snapshot_start;\n"
|
|
|
|
".globl deno_snapshot_end;");
|
2018-06-11 11:01:35 -04:00
|
|
|
#else
|
2018-07-26 23:21:10 -04:00
|
|
|
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;");
|
2018-08-10 15:09:28 -04:00
|
|
|
#endif // LIBDENO_TEST
|
2018-06-09 23:34:03 -04:00
|
|
|
|
2018-06-18 10:02:08 -04:00
|
|
|
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) {
|
2018-06-18 10:02:08 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-10-08 11:49:48 -04:00
|
|
|
Deno* NewFromSnapshot(void* user_data, deno_recv_cb cb) {
|
2018-06-09 23:34:03 -04:00
|
|
|
Deno* d = new Deno;
|
2018-06-13 13:38:22 -04:00
|
|
|
d->currentArgs = nullptr;
|
2018-06-09 23:34:03 -04:00
|
|
|
d->cb = cb;
|
2018-10-08 11:49:48 -04:00
|
|
|
d->user_data = user_data;
|
2018-06-09 23:34:03 -04:00
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.array_buffer_allocator =
|
|
|
|
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
|
|
|
params.external_references = external_references;
|
2018-07-26 23:21:10 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-06-09 23:34:03 -04:00
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
2018-06-10 08:18:15 -04:00
|
|
|
AddIsolate(d, isolate);
|
2018-06-09 23:34:03 -04:00
|
|
|
|
2018-06-11 11:01:35 -04:00
|
|
|
v8::Locker locker(isolate);
|
2018-06-09 23:34:03 -04:00
|
|
|
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));
|
2018-06-09 23:34:03 -04:00
|
|
|
d->context.Reset(d->isolate, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace deno
|
2018-06-10 08:18:15 -04:00
|
|
|
|
|
|
|
extern "C" {
|
2018-10-08 11:49:48 -04:00
|
|
|
Deno* deno_new(void* user_data, deno_recv_cb cb) {
|
|
|
|
return deno::NewFromSnapshot(user_data, cb);
|
2018-06-10 08:18:15 -04:00
|
|
|
}
|
|
|
|
}
|