Skip to content
Permalink
Browse files

`use-snapshots` build option for cross compile support. (#1852)

  • Loading branch information...
afinch7 authored and ry committed Mar 4, 2019
1 parent 77d7ad6 commit 75fe80d5a4992ddad89160c2e0113a1af8d3d24a
Showing with 209 additions and 66 deletions.
  1. +7 −0 build.rs
  2. +7 −4 libdeno/api.cc
  3. +3 −0 src/compiler.rs
  4. +56 −21 src/isolate.rs
  5. +86 −0 src/isolate_init.rs
  6. +3 −3 src/main.rs
  7. +37 −8 src/ops.rs
  8. +0 −26 src/snapshot.rs
  9. +10 −4 src/workers.rs
@@ -76,6 +76,13 @@ fn main() {
}
}

// Enable snapshots for x64 builds
if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "x86_64" {
// Not related to v8_use_snapshot
// This only enables using pregenerated snapshots for isolate init
println!("cargo:rustc-cfg=feature=\"use-snapshot-init\"");
}

if !gn_out_path.join("build.ninja").exists() {
let status = Command::new("python")
.env("DENO_BUILD_PATH", &gn_out_dir)
@@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>

#include "third_party/v8/include/libplatform/libplatform.h"
@@ -10,6 +11,7 @@

#include "deno.h"
#include "exceptions.h"
#include "file_util.h"
#include "internal.h"

extern "C" {
@@ -106,10 +108,11 @@ deno_buf deno_get_snapshot(Deno* d_) {
static std::unique_ptr<v8::Platform> platform;

void deno_init() {
CHECK_NULL(platform.get());
platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
if (platform.get() == nullptr) {
platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
}
}

const char* deno_v8_version() { return v8::V8::GetVersion(); }
@@ -1,6 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::isolate::Buf;
use crate::isolate::IsolateState;
use crate::isolate_init;
use crate::msg;
use crate::permissions::DenoPermissions;
use crate::resources;
@@ -50,6 +51,7 @@ impl ModuleMetaData {

fn lazy_start(parent_state: &Arc<IsolateState>) -> Resource {
let mut cell = C_RID.lock().unwrap();
let isolate_init = isolate_init::compiler_isolate_init();
let permissions = DenoPermissions {
allow_read: AtomicBool::new(true),
allow_write: AtomicBool::new(true),
@@ -59,6 +61,7 @@ fn lazy_start(parent_state: &Arc<IsolateState>) -> Resource {
};
let rid = cell.get_or_insert_with(|| {
let resource = workers::spawn(
isolate_init,
parent_state.clone(),
"compilerMain()".to_string(),
permissions,
@@ -12,6 +12,7 @@ use crate::errors::DenoError;
use crate::errors::DenoResult;
use crate::errors::RustOrJsError;
use crate::flags;
use crate::isolate_init::IsolateInit;
use crate::js_errors::apply_source_map;
use crate::libdeno;
use crate::modules::Modules;
@@ -166,7 +167,7 @@ static DENO_INIT: Once = ONCE_INIT;

impl Isolate {
pub fn new(
snapshot: libdeno::deno_buf,
init: IsolateInit,
state: Arc<IsolateState>,
dispatch: Dispatch,
permissions: DenoPermissions,
@@ -176,15 +177,18 @@ impl Isolate {
});
let config = libdeno::deno_config {
will_snapshot: 0,
load_snapshot: snapshot,
load_snapshot: match init.snapshot {
Some(s) => s,
None => libdeno::deno_buf::empty(),
},
shared: libdeno::deno_buf::empty(), // TODO Use for message passing.
recv_cb: pre_dispatch,
};
let libdeno_isolate = unsafe { libdeno::deno_new(config) };
// This channel handles sending async messages back to the runtime.
let (tx, rx) = mpsc::channel::<(usize, Buf)>();

Self {
let new_isolate = Self {
libdeno_isolate,
dispatch,
rx,
@@ -194,7 +198,17 @@ impl Isolate {
modules: RefCell::new(Modules::new()),
state,
permissions: Arc::new(permissions),
}
};

// Run init script if present.
match init.init_script {
Some(init_script) => new_isolate
.execute2(init_script.filename.as_str(), init_script.source.as_str())
.unwrap(),
None => {}
};

new_isolate
}

#[inline]
@@ -618,9 +632,12 @@ mod tests {
#[test]
fn test_dispatch_sync() {
let state = IsolateState::mock();
let snapshot = libdeno::deno_buf::empty();
let permissions = DenoPermissions::default();
let isolate = Isolate::new(snapshot, state, dispatch_sync, permissions);
let init = IsolateInit {
snapshot: None,
init_script: None,
};
let isolate =
Isolate::new(init, state, dispatch_sync, DenoPermissions::default());
tokio_util::init(|| {
isolate
.execute(
@@ -658,10 +675,16 @@ mod tests {
#[test]
fn test_metrics_sync() {
let state = IsolateState::mock();
let snapshot = libdeno::deno_buf::empty();
let permissions = DenoPermissions::default();
let isolate =
Isolate::new(snapshot, state, metrics_dispatch_sync, permissions);
let init = IsolateInit {
snapshot: None,
init_script: None,
};
let isolate = Isolate::new(
init,
state,
metrics_dispatch_sync,
DenoPermissions::default(),
);
tokio_util::init(|| {
// Verify that metrics have been properly initialized.
{
@@ -694,10 +717,16 @@ mod tests {
#[test]
fn test_metrics_async() {
let state = IsolateState::mock();
let snapshot = libdeno::deno_buf::empty();
let permissions = DenoPermissions::default();
let isolate =
Isolate::new(snapshot, state, metrics_dispatch_async, permissions);
let init = IsolateInit {
snapshot: None,
init_script: None,
};
let isolate = Isolate::new(
init,
state,
metrics_dispatch_async,
DenoPermissions::default(),
);
tokio_util::init(|| {
// Verify that metrics have been properly initialized.
{
@@ -784,9 +813,12 @@ mod tests {
let (flags, rest_argv, _) = flags::set_flags(argv).unwrap();

let state = Arc::new(IsolateState::new(flags, rest_argv, None));
let snapshot = libdeno::deno_buf::empty();
let permissions = DenoPermissions::default();
let mut isolate = Isolate::new(snapshot, state, dispatch_sync, permissions);
let init = IsolateInit {
snapshot: None,
init_script: None,
};
let mut isolate =
Isolate::new(init, state, dispatch_sync, DenoPermissions::default());
tokio_util::init(|| {
isolate
.execute_mod(filename, false)
@@ -807,9 +839,12 @@ mod tests {
let (flags, rest_argv, _) = flags::set_flags(argv).unwrap();

let state = Arc::new(IsolateState::new(flags, rest_argv, None));
let snapshot = libdeno::deno_buf::empty();
let permissions = DenoPermissions::default();
let mut isolate = Isolate::new(snapshot, state, dispatch_sync, permissions);
let init = IsolateInit {
snapshot: None,
init_script: None,
};
let mut isolate =
Isolate::new(init, state, dispatch_sync, DenoPermissions::default());
tokio_util::init(|| {
isolate
.execute_mod(filename, false)
@@ -0,0 +1,86 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::libdeno::deno_buf;

pub struct IsolateInitScript {
pub source: String,
pub filename: String,
}

pub struct IsolateInit {
pub snapshot: Option<deno_buf>,
pub init_script: Option<IsolateInitScript>,
}

pub fn deno_isolate_init() -> IsolateInit {
if cfg!(not(feature = "check-only")) {
if cfg!(feature = "use-snapshot-init") {
let data =
include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/snapshot_deno.bin"));

unsafe {
IsolateInit {
snapshot: Some(deno_buf::from_raw_parts(data.as_ptr(), data.len())),
init_script: None,
}
}
} else {
#[cfg(not(feature = "check-only"))]
let source_bytes =
include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/bundle/main.js"));

#[cfg(feature = "check-only")]
let source_bytes = vec![];

IsolateInit {
snapshot: None,
init_script: Some(IsolateInitScript {
filename: "gen/bundle/main.js".to_string(),
source: std::str::from_utf8(source_bytes).unwrap().to_string(),
}),
}
}
} else {
IsolateInit {
snapshot: None,
init_script: None,
}
}
}

pub fn compiler_isolate_init() -> IsolateInit {
if cfg!(not(feature = "check-only")) {
if cfg!(feature = "use-snapshot-init") {
let data = include_bytes!(concat!(
env!("GN_OUT_DIR"),
"/gen/snapshot_compiler.bin"
));

unsafe {
IsolateInit {
snapshot: Some(deno_buf::from_raw_parts(data.as_ptr(), data.len())),
init_script: None,
}
}
} else {
#[cfg(not(feature = "check-only"))]
let source_bytes =
include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/bundle/compiler.js"));

#[cfg(feature = "check-only")]
let source_bytes = vec![];

IsolateInit {
snapshot: None,
init_script: Some(IsolateInitScript {
filename: "gen/bundle/compiler.js".to_string(),
source: std::str::from_utf8(source_bytes).unwrap().to_string(),
}),
}
}
} else {
IsolateInit {
snapshot: None,
init_script: None,
}
}
}
@@ -17,6 +17,7 @@ mod fs;
mod http_body;
mod http_util;
pub mod isolate;
pub mod isolate_init;
pub mod js_errors;
pub mod libdeno;
pub mod modules;
@@ -27,7 +28,6 @@ pub mod permissions;
mod repl;
pub mod resolve_addr;
pub mod resources;
pub mod snapshot;
mod tokio_util;
mod tokio_write;
pub mod version;
@@ -95,10 +95,10 @@ fn main() {
let should_display_info = flags.info;

let state = Arc::new(isolate::IsolateState::new(flags, rest_argv, None));
let snapshot = snapshot::deno_snapshot();
let isolate_init = isolate_init::deno_isolate_init();
let permissions = permissions::DenoPermissions::from_flags(&state.flags);
let mut isolate =
isolate::Isolate::new(snapshot, state, ops::dispatch, permissions);
isolate::Isolate::new(isolate_init, state, ops::dispatch, permissions);

tokio_util::init(|| {
// Setup runtime.
Oops, something went wrong.

0 comments on commit 75fe80d

Please sign in to comment.
You can’t perform that action at this time.