From ff48eca5403140b134a3e6a772ffb6f8905fae3e Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sun, 10 Jun 2018 05:34:03 +0200 Subject: [PATCH] Expose deno::from_snapshot() constructor --- deno2/BUILD.gn | 26 +++++++++++++++++----- deno2/deno.cc | 39 +------------------------------- deno2/deno_internal.h | 32 +++++++++++++++++++++++++++ deno2/from_snapshot.cc | 50 ++++++++++++++++++++++++++++++++++++++++++ deno2/include/deno.h | 2 +- deno2/main.cc | 18 +-------------- 6 files changed, 106 insertions(+), 61 deletions(-) create mode 100644 deno2/deno_internal.h create mode 100644 deno2/from_snapshot.cc diff --git a/deno2/BUILD.gn b/deno2/BUILD.gn index e044b29a08..a7a414ad0b 100644 --- a/deno2/BUILD.gn +++ b/deno2/BUILD.gn @@ -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 = [ diff --git a/deno2/deno.cc b/deno2/deno.cc index 4618b54806..eb9b05a7fa 100644 --- a/deno2/deno.cc +++ b/deno2/deno.cc @@ -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 recv; - v8::Persistent 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 : ""; @@ -170,10 +159,6 @@ void Send(const v8::FunctionCallbackInfo& args) { } } -intptr_t external_references[] = {reinterpret_cast(Print), - reinterpret_cast(Recv), - reinterpret_cast(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 diff --git a/deno2/deno_internal.h b/deno2/deno_internal.h new file mode 100644 index 0000000000..d125f39b15 --- /dev/null +++ b/deno2/deno_internal.h @@ -0,0 +1,32 @@ +// Copyright 2018 Ryan Dahl +// All rights reserved. MIT License. +#ifndef DENO_INTERNAL_H_ +#define DENO_INTERNAL_H_ + +#include +#include "include/deno.h" +#include "v8/include/v8.h" + +namespace deno { + +void Print(const v8::FunctionCallbackInfo& args); +void Recv(const v8::FunctionCallbackInfo& args); +void Send(const v8::FunctionCallbackInfo& args); +static intptr_t external_references[] = {reinterpret_cast(Print), + reinterpret_cast(Recv), + reinterpret_cast(Send), 0}; + +// deno_s = Wrapped Isolate. +struct deno_s { + v8::Isolate* isolate; + std::string last_exception; + v8::Persistent recv; + v8::Persistent context; + RecvCallback cb; + void* data; +}; + +void deno_add_isolate(Deno* d, v8::Isolate* isolate); + +} // namespace deno +#endif // DENO_INTERNAL_H_ diff --git a/deno2/from_snapshot.cc b/deno2/from_snapshot.cc new file mode 100644 index 0000000000..b743b441fb --- /dev/null +++ b/deno2/from_snapshot.cc @@ -0,0 +1,50 @@ +// Copyright 2018 Ryan Dahl +// All rights reserved. MIT License. +#include +#include +#include +#include +#include + +#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 diff --git a/deno2/include/deno.h b/deno2/include/deno.h index 9c6a22d2ef..e5a25f2405 100644 --- a/deno2/include/deno.h +++ b/deno2/include/deno.h @@ -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, diff --git a/deno2/main.cc b/deno2/main.cc index 5139e01bea..29aacc4e62 100644 --- a/deno2/main.cc +++ b/deno2/main.cc @@ -3,31 +3,15 @@ #include #include -#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); }