1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

Expose deno::from_snapshot() constructor

This commit is contained in:
Ryan Dahl 2018-06-10 05:34:03 +02:00
parent 3062039ffe
commit ff48eca540
6 changed files with 106 additions and 61 deletions

View file

@ -92,7 +92,7 @@ v8_executable("snapshot_creator") {
]
configs = [ "v8:libplatform_config" ]
deps = [
":libdeno",
":deno_nosnapshot",
]
}
@ -100,17 +100,23 @@ v8_executable("deno") {
sources = [
"main.cc",
]
include_dirs = [ target_gen_dir ]
configs = [ "v8:libplatform_config" ]
deps = [
":create_snapshot_deno",
":libdeno",
]
}
v8_component("libdeno") {
configs = [ "v8:libplatform_config" ]
deps = [
":deno_snapshot",
]
}
v8_source_set("deno_nosnapshot") {
sources = [
"deno.cc",
"deno_internal.h",
"include/deno.h",
]
include_dirs = [ "include/" ]
@ -121,11 +127,21 @@ v8_component("libdeno") {
"v8:v8_libbase",
"v8:v8_libplatform",
"v8:v8_libsampler",
"//build/config:exe_and_shlib_deps",
"//build/win:default_exe_manifest",
]
}
v8_source_set("deno_snapshot") {
sources = [
"from_snapshot.cc",
]
deps = [
":create_snapshot_deno",
":deno_nosnapshot",
]
include_dirs = [ target_gen_dir ]
configs = [ "v8:libplatform_config" ]
}
executable("deno_test") {
testonly = true
sources = [

View file

@ -28,24 +28,13 @@ IN THE SOFTWARE.
#include "v8/include/libplatform/libplatform.h"
#include "v8/include/v8.h"
#include "deno_internal.h"
#include "include/deno.h"
#define CHECK(x) assert(x) // TODO(ry) use V8's CHECK.
namespace deno {
// deno_s = Wrapped Isolate.
struct deno_s {
v8::Isolate* isolate;
std::string last_exception;
v8::Persistent<v8::Function> recv;
v8::Persistent<v8::Context> context;
RecvCallback cb;
void* data;
};
void deno_add_isolate(Deno* d, v8::Isolate* isolate);
// Extracts a C string from a v8::V8 Utf8Value.
const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
@ -170,10 +159,6 @@ void Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
}
}
intptr_t external_references[] = {reinterpret_cast<intptr_t>(Print),
reinterpret_cast<intptr_t>(Recv),
reinterpret_cast<intptr_t>(Send), 0};
const char* v8_version() { return v8::V8::GetVersion(); }
void v8_set_flags(int* argc, char** argv) {
@ -261,28 +246,6 @@ void v8_init() {
v8::V8::Initialize();
}
Deno* deno_from_snapshot(v8::StartupData* blob, void* data, RecvCallback cb) {
Deno* d = new Deno;
d->cb = cb;
d->data = data;
v8::Isolate::CreateParams params;
params.snapshot_blob = blob;
params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
params.external_references = external_references;
v8::Isolate* isolate = v8::Isolate::New(params);
deno_add_isolate(d, isolate);
v8::Isolate::Scope isolate_scope(isolate);
{
v8::HandleScope handle_scope(isolate);
auto context = v8::Context::New(isolate);
d->context.Reset(d->isolate, context);
}
return d;
}
void deno_add_isolate(Deno* d, v8::Isolate* isolate) {
d->isolate = isolate;
// Leaving this code here because it will probably be useful later on, but

32
deno2/deno_internal.h Normal file
View file

@ -0,0 +1,32 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
#ifndef DENO_INTERNAL_H_
#define DENO_INTERNAL_H_
#include <string>
#include "include/deno.h"
#include "v8/include/v8.h"
namespace deno {
void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
void Recv(const v8::FunctionCallbackInfo<v8::Value>& args);
void Send(const v8::FunctionCallbackInfo<v8::Value>& args);
static intptr_t external_references[] = {reinterpret_cast<intptr_t>(Print),
reinterpret_cast<intptr_t>(Recv),
reinterpret_cast<intptr_t>(Send), 0};
// deno_s = Wrapped Isolate.
struct deno_s {
v8::Isolate* isolate;
std::string last_exception;
v8::Persistent<v8::Function> recv;
v8::Persistent<v8::Context> context;
RecvCallback cb;
void* data;
};
void deno_add_isolate(Deno* d, v8::Isolate* isolate);
} // namespace deno
#endif // DENO_INTERNAL_H_

50
deno2/from_snapshot.cc Normal file
View file

@ -0,0 +1,50 @@
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "v8/include/libplatform/libplatform.h"
#include "v8/include/v8.h"
#include "./deno_internal.h"
#include "include/deno.h"
namespace deno {
#include "natives_deno.cc"
#include "snapshot_deno.cc"
Deno* from_snapshot(void* data, RecvCallback cb) {
auto natives_blob = *StartupBlob_natives();
printf("natives_blob %d bytes\n", natives_blob.raw_size);
auto snapshot_blob = *StartupBlob_snapshot();
printf("snapshot_blob %d bytes\n", snapshot_blob.raw_size);
v8::V8::SetNativesDataBlob(&natives_blob);
v8::V8::SetSnapshotDataBlob(&snapshot_blob);
Deno* d = new Deno;
d->cb = cb;
d->data = data;
v8::Isolate::CreateParams params;
params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
params.external_references = external_references;
v8::Isolate* isolate = v8::Isolate::New(params);
deno_add_isolate(d, isolate);
v8::Isolate::Scope isolate_scope(isolate);
{
v8::HandleScope handle_scope(isolate);
auto context = v8::Context::New(isolate);
d->context.Reset(d->isolate, context);
}
return d;
}
} // namespace deno

View file

@ -26,7 +26,7 @@ const char* v8_version();
void v8_set_flags(int* argc, char** argv);
// Constructors:
Deno* deno_from_snapshot(v8::StartupData* blob, void* data, RecvCallback cb);
Deno* from_snapshot(void* data, RecvCallback cb);
v8::StartupData make_snapshot(v8::StartupData* prev_natives_blob,
v8::StartupData* prev_snapshot_blob,

View file

@ -3,31 +3,15 @@
#include <assert.h>
#include <stdio.h>
#include "v8/include/v8.h"
#include "include/deno.h"
#include "natives_deno.cc"
#include "snapshot_deno.cc"
int main(int argc, char** argv) {
deno::v8_init();
auto natives_blob = *StartupBlob_natives();
printf("natives_blob %d bytes\n", natives_blob.raw_size);
auto snapshot_blob = *StartupBlob_snapshot();
printf("snapshot_blob %d bytes\n", snapshot_blob.raw_size);
v8::V8::SetNativesDataBlob(&natives_blob);
v8::V8::SetSnapshotDataBlob(&snapshot_blob);
deno::Deno* d = deno::deno_from_snapshot(&snapshot_blob, NULL, NULL);
deno::Deno* d = deno::from_snapshot(NULL, NULL);
int r = deno::deno_load(d, "main2.js", "foo();");
if (r != 0) {
printf("Error! %s\n", deno::deno_last_exception(d));
exit(1);
}
const char* v = v8::V8::GetVersion();
printf("Hello World. V8 version %s\n", v);
}