core: snapshot improvements (#2052)

* Moves how snapshots are supplied to the Isolate. Previously they were
  given by Behavior::startup_data() but it was only called once at
  startup. It makes more sense (and simplifies Behavior) to pass it to the
  constructor of Isolate.
* Adds new libdeno type deno_snapshot instead of overloading
  deno_buf.
* Adds new libdeno method to delete snapshot deno_snapshot_delete().
* Renames deno_get_snapshot() to deno_snapshot_new().
* Makes StartupData hold references to snapshots. This was implicit when
  it previously held a deno_buf but is made explicit now. Note that
  include_bytes!() returns a &'static [u8] and we want to avoid
  copying that.
This commit is contained in:
Ryan Dahl 2019-04-08 10:12:43 -04:00 committed by GitHub
parent cdb72afd8d
commit f7fdb90fd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 182 additions and 121 deletions

View file

@ -4,24 +4,16 @@ use crate::ops;
use deno::deno_buf; use deno::deno_buf;
use deno::Behavior; use deno::Behavior;
use deno::Op; use deno::Op;
use deno::StartupData;
use std::sync::Arc; use std::sync::Arc;
/// Implements deno::Behavior for the main Deno command-line. /// Implements deno::Behavior for the main Deno command-line.
pub struct CliBehavior { pub struct CliBehavior {
startup_data: Option<StartupData>,
pub state: Arc<IsolateState>, pub state: Arc<IsolateState>,
} }
impl CliBehavior { impl CliBehavior {
pub fn new( pub fn new(state: Arc<IsolateState>) -> Self {
startup_data: Option<StartupData>, Self { state }
state: Arc<IsolateState>,
) -> Self {
Self {
startup_data,
state,
}
} }
} }
@ -38,10 +30,6 @@ impl IsolateStateContainer for CliBehavior {
} }
impl Behavior for CliBehavior { impl Behavior for CliBehavior {
fn startup_data(&mut self) -> Option<StartupData> {
self.startup_data.take()
}
fn dispatch( fn dispatch(
&mut self, &mut self,
control: &[u8], control: &[u8],

View file

@ -17,7 +17,6 @@ use deno::Behavior;
use deno::Buf; use deno::Buf;
use deno::JSError; use deno::JSError;
use deno::Op; use deno::Op;
use deno::StartupData;
use futures::future::*; use futures::future::*;
use futures::sync::oneshot; use futures::sync::oneshot;
use futures::Future; use futures::Future;
@ -70,10 +69,6 @@ impl IsolateStateContainer for &CompilerBehavior {
} }
impl Behavior for CompilerBehavior { impl Behavior for CompilerBehavior {
fn startup_data(&mut self) -> Option<StartupData> {
Some(startup_data::compiler_isolate_init())
}
fn dispatch( fn dispatch(
&mut self, &mut self,
control: &[u8], control: &[u8],
@ -148,6 +143,7 @@ fn lazy_start(parent_state: Arc<IsolateState>) -> ResourceId {
cell cell
.get_or_insert_with(|| { .get_or_insert_with(|| {
let worker_result = workers::spawn( let worker_result = workers::spawn(
startup_data::compiler_isolate_init(),
CompilerBehavior::new( CompilerBehavior::new(
parent_state.flags.clone(), parent_state.flags.clone(),
parent_state.argv.clone(), parent_state.argv.clone(),

View file

@ -13,6 +13,7 @@ use deno;
use deno::deno_mod; use deno::deno_mod;
use deno::Behavior; use deno::Behavior;
use deno::JSError; use deno::JSError;
use deno::StartupData;
use futures::future::Either; use futures::future::Either;
use futures::Async; use futures::Async;
use futures::Future; use futures::Future;
@ -32,10 +33,10 @@ pub struct Isolate<B: Behavior> {
} }
impl<B: DenoBehavior> Isolate<B> { impl<B: DenoBehavior> Isolate<B> {
pub fn new(behavior: B) -> Isolate<B> { pub fn new(startup_data: StartupData, behavior: B) -> Isolate<B> {
let state = behavior.state().clone(); let state = behavior.state().clone();
Self { Self {
inner: CoreIsolate::new(behavior), inner: CoreIsolate::new(startup_data, behavior),
state, state,
} }
} }
@ -270,8 +271,8 @@ mod tests {
let state = Arc::new(IsolateState::new(flags, rest_argv, None, false)); let state = Arc::new(IsolateState::new(flags, rest_argv, None, false));
let state_ = state.clone(); let state_ = state.clone();
tokio_util::run(lazy(move || { tokio_util::run(lazy(move || {
let cli = CliBehavior::new(None, state.clone()); let cli = CliBehavior::new(state.clone());
let mut isolate = Isolate::new(cli); let mut isolate = Isolate::new(StartupData::None, cli);
if let Err(err) = isolate.execute_mod(&filename, false) { if let Err(err) = isolate.execute_mod(&filename, false) {
eprintln!("execute_mod err {:?}", err); eprintln!("execute_mod err {:?}", err);
} }
@ -293,8 +294,8 @@ mod tests {
let state = Arc::new(IsolateState::new(flags, rest_argv, None, false)); let state = Arc::new(IsolateState::new(flags, rest_argv, None, false));
let state_ = state.clone(); let state_ = state.clone();
tokio_util::run(lazy(move || { tokio_util::run(lazy(move || {
let cli = CliBehavior::new(None, state.clone()); let cli = CliBehavior::new(state.clone());
let mut isolate = Isolate::new(cli); let mut isolate = Isolate::new(StartupData::None, cli);
if let Err(err) = isolate.execute_mod(&filename, false) { if let Err(err) = isolate.execute_mod(&filename, false) {
eprintln!("execute_mod err {:?}", err); eprintln!("execute_mod err {:?}", err);
} }

View file

@ -107,9 +107,8 @@ fn main() {
let state = Arc::new(IsolateState::new(flags, rest_argv, None, false)); let state = Arc::new(IsolateState::new(flags, rest_argv, None, false));
let state_ = state.clone(); let state_ = state.clone();
let startup_data = startup_data::deno_isolate_init(); let cli = CliBehavior::new(state_);
let cli = CliBehavior::new(Some(startup_data), state_); let mut isolate = Isolate::new(startup_data::deno_isolate_init(), cli);
let mut isolate = Isolate::new(cli);
let main_future = lazy(move || { let main_future = lazy(move || {
// Setup runtime. // Setup runtime.

View file

@ -15,6 +15,7 @@ use crate::resolve_addr::resolve_addr;
use crate::resources; use crate::resources;
use crate::resources::table_entries; use crate::resources::table_entries;
use crate::resources::Resource; use crate::resources::Resource;
use crate::startup_data;
use crate::tokio_util; use crate::tokio_util;
use crate::tokio_write; use crate::tokio_write;
use crate::version; use crate::version;
@ -1865,6 +1866,7 @@ fn op_create_worker(
parent_state.argv.clone(), parent_state.argv.clone(),
); );
match workers::spawn( match workers::spawn(
startup_data::deno_isolate_init(),
behavior, behavior,
&format!("USER-WORKER-{}", specifier), &format!("USER-WORKER-{}", specifier),
workers::WorkerInit::Module(specifier.to_string()), workers::WorkerInit::Module(specifier.to_string()),

View file

@ -1,8 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use deno::deno_buf;
use deno::{Script, StartupData}; use deno::{Script, StartupData};
pub fn deno_isolate_init() -> StartupData { pub fn deno_isolate_init() -> StartupData<'static> {
if cfg!(feature = "no-snapshot-init") { if cfg!(feature = "no-snapshot-init") {
debug!("Deno isolate init without snapshots."); debug!("Deno isolate init without snapshots.");
#[cfg(not(feature = "check-only"))] #[cfg(not(feature = "check-only"))]
@ -12,8 +11,8 @@ pub fn deno_isolate_init() -> StartupData {
let source_bytes = vec![]; let source_bytes = vec![];
StartupData::Script(Script { StartupData::Script(Script {
filename: "gen/cli/bundle/main.js".to_string(), filename: "gen/cli/bundle/main.js",
source: std::str::from_utf8(&source_bytes[..]).unwrap().to_string(), source: std::str::from_utf8(&source_bytes[..]).unwrap(),
}) })
} else { } else {
debug!("Deno isolate init with snapshots."); debug!("Deno isolate init with snapshots.");
@ -23,13 +22,11 @@ pub fn deno_isolate_init() -> StartupData {
#[cfg(any(feature = "check-only", feature = "no-snapshot-init"))] #[cfg(any(feature = "check-only", feature = "no-snapshot-init"))]
let data = vec![]; let data = vec![];
unsafe { StartupData::Snapshot(data)
StartupData::Snapshot(deno_buf::from_raw_parts(data.as_ptr(), data.len()))
}
} }
} }
pub fn compiler_isolate_init() -> StartupData { pub fn compiler_isolate_init() -> StartupData<'static> {
if cfg!(feature = "no-snapshot-init") { if cfg!(feature = "no-snapshot-init") {
debug!("Compiler isolate init without snapshots."); debug!("Compiler isolate init without snapshots.");
#[cfg(not(feature = "check-only"))] #[cfg(not(feature = "check-only"))]
@ -41,8 +38,8 @@ pub fn compiler_isolate_init() -> StartupData {
let source_bytes = vec![]; let source_bytes = vec![];
StartupData::Script(Script { StartupData::Script(Script {
filename: "gen/cli/bundle/compiler.js".to_string(), filename: "gen/cli/bundle/compiler.js",
source: std::str::from_utf8(&source_bytes[..]).unwrap().to_string(), source: std::str::from_utf8(&source_bytes[..]).unwrap(),
}) })
} else { } else {
debug!("Deno isolate init with snapshots."); debug!("Deno isolate init with snapshots.");
@ -54,8 +51,6 @@ pub fn compiler_isolate_init() -> StartupData {
#[cfg(any(feature = "check-only", feature = "no-snapshot-init"))] #[cfg(any(feature = "check-only", feature = "no-snapshot-init"))]
let data = vec![]; let data = vec![];
unsafe { StartupData::Snapshot(data)
StartupData::Snapshot(deno_buf::from_raw_parts(data.as_ptr(), data.len()))
}
} }
} }

View file

@ -7,7 +7,6 @@ use crate::isolate_state::IsolateStateContainer;
use crate::isolate_state::WorkerChannels; use crate::isolate_state::WorkerChannels;
use crate::ops; use crate::ops;
use crate::resources; use crate::resources;
use crate::startup_data;
use deno::deno_buf; use deno::deno_buf;
use deno::Behavior; use deno::Behavior;
use deno::Buf; use deno::Buf;
@ -44,10 +43,6 @@ impl IsolateStateContainer for &UserWorkerBehavior {
} }
impl Behavior for UserWorkerBehavior { impl Behavior for UserWorkerBehavior {
fn startup_data(&mut self) -> Option<StartupData> {
Some(startup_data::deno_isolate_init())
}
fn dispatch( fn dispatch(
&mut self, &mut self,
control: &[u8], control: &[u8],
@ -83,7 +78,7 @@ pub struct Worker<B: WorkerBehavior> {
} }
impl<B: WorkerBehavior> Worker<B> { impl<B: WorkerBehavior> Worker<B> {
pub fn new(mut behavior: B) -> Self { pub fn new(startup_data: StartupData, mut behavior: B) -> Self {
let (worker_in_tx, worker_in_rx) = mpsc::channel::<Buf>(1); let (worker_in_tx, worker_in_rx) = mpsc::channel::<Buf>(1);
let (worker_out_tx, worker_out_rx) = mpsc::channel::<Buf>(1); let (worker_out_tx, worker_out_rx) = mpsc::channel::<Buf>(1);
@ -92,7 +87,7 @@ impl<B: WorkerBehavior> Worker<B> {
behavior.set_internal_channels(internal_channels); behavior.set_internal_channels(internal_channels);
let isolate = Isolate::new(behavior); let isolate = Isolate::new(startup_data, behavior);
Worker { Worker {
isolate, isolate,
@ -129,12 +124,13 @@ pub enum WorkerInit {
} }
pub fn spawn<B: WorkerBehavior + 'static>( pub fn spawn<B: WorkerBehavior + 'static>(
startup_data: StartupData,
behavior: B, behavior: B,
worker_debug_name: &str, worker_debug_name: &str,
init: WorkerInit, init: WorkerInit,
) -> Result<Worker<B>, RustOrJsError> { ) -> Result<Worker<B>, RustOrJsError> {
let state = behavior.state().clone(); let state = behavior.state().clone();
let mut worker = Worker::new(behavior); let mut worker = Worker::new(startup_data, behavior);
worker worker
.execute(&format!("denoMain('{}')", worker_debug_name)) .execute(&format!("denoMain('{}')", worker_debug_name))
@ -172,6 +168,7 @@ mod tests {
use crate::compiler::CompilerBehavior; use crate::compiler::CompilerBehavior;
use crate::isolate_state::IsolateState; use crate::isolate_state::IsolateState;
use crate::js_errors::JSErrorColor; use crate::js_errors::JSErrorColor;
use crate::startup_data;
use crate::tokio_util; use crate::tokio_util;
use futures::future::lazy; use futures::future::lazy;
use std::thread; use std::thread;
@ -180,6 +177,7 @@ mod tests {
fn test_spawn() { fn test_spawn() {
tokio_util::init(|| { tokio_util::init(|| {
let worker_result = spawn( let worker_result = spawn(
startup_data::compiler_isolate_init(),
CompilerBehavior::new( CompilerBehavior::new(
IsolateState::mock().flags.clone(), IsolateState::mock().flags.clone(),
IsolateState::mock().argv.clone(), IsolateState::mock().argv.clone(),
@ -242,6 +240,7 @@ mod tests {
fn removed_from_resource_table_on_close() { fn removed_from_resource_table_on_close() {
tokio_util::init(|| { tokio_util::init(|| {
let worker_result = spawn( let worker_result = spawn(
startup_data::compiler_isolate_init(),
CompilerBehavior::new( CompilerBehavior::new(
IsolateState::mock().flags.clone(), IsolateState::mock().flags.clone(),
IsolateState::mock().argv.clone(), IsolateState::mock().argv.clone(),

View file

@ -99,15 +99,6 @@ pub type HttpBenchOp = dyn Future<Item = i32, Error = std::io::Error> + Send;
struct HttpBench(); struct HttpBench();
impl Behavior for HttpBench { impl Behavior for HttpBench {
fn startup_data(&mut self) -> Option<StartupData> {
let js_source = include_str!("http_bench.js");
Some(StartupData::Script(Script {
source: js_source.to_string(),
filename: "http_bench.js".to_string(),
}))
}
fn dispatch( fn dispatch(
&mut self, &mut self,
control: &[u8], control: &[u8],
@ -168,7 +159,15 @@ fn main() {
// TODO currently isolate.execute() must be run inside tokio, hence the // TODO currently isolate.execute() must be run inside tokio, hence the
// lazy(). It would be nice to not have that contraint. Probably requires // lazy(). It would be nice to not have that contraint. Probably requires
// using v8::MicrotasksPolicy::kExplicit // using v8::MicrotasksPolicy::kExplicit
let isolate = deno::Isolate::new(HttpBench());
let js_source = include_str!("http_bench.js");
let startup_data = StartupData::Script(Script {
source: js_source,
filename: "http_bench.js",
});
let isolate = deno::Isolate::new(startup_data, HttpBench());
isolate.then(|r| { isolate.then(|r| {
js_check(r); js_check(r);

View file

@ -8,6 +8,8 @@ use crate::js_errors::JSError;
use crate::libdeno; use crate::libdeno;
use crate::libdeno::deno_buf; use crate::libdeno::deno_buf;
use crate::libdeno::deno_mod; use crate::libdeno::deno_mod;
use crate::libdeno::Snapshot1;
use crate::libdeno::Snapshot2;
use crate::shared_queue::SharedQueue; use crate::shared_queue::SharedQueue;
use crate::shared_queue::RECOMMENDED_SIZE; use crate::shared_queue::RECOMMENDED_SIZE;
use futures::Async; use futures::Async;
@ -16,6 +18,7 @@ use futures::Poll;
use libc::c_void; use libc::c_void;
use std::ffi::CStr; use std::ffi::CStr;
use std::ffi::CString; use std::ffi::CString;
use std::ptr::null;
use std::sync::{Arc, Mutex, Once, ONCE_INIT}; use std::sync::{Arc, Mutex, Once, ONCE_INIT};
pub type Buf = Box<[u8]>; pub type Buf = Box<[u8]>;
@ -48,26 +51,24 @@ impl Future for PendingOp {
} }
/// Stores a script used to initalize a Isolate /// Stores a script used to initalize a Isolate
pub struct Script { pub struct Script<'a> {
pub source: String, pub source: &'a str,
pub filename: String, pub filename: &'a str,
} }
/// Represents data used to initialize isolate at startup /// Represents data used to initialize isolate at startup
/// either a binary snapshot or a javascript source file /// either a binary snapshot or a javascript source file
/// in the form of the StartupScript struct. /// in the form of the StartupScript struct.
pub enum StartupData { pub enum StartupData<'a> {
Script(Script), Script(Script<'a>),
Snapshot(deno_buf), Snapshot(&'a [u8]),
None,
} }
/// Defines the behavior of an Isolate. /// Defines the behavior of an Isolate.
// TODO(ry) Now that Behavior only has the dispatch method, it should be renamed
// to Dispatcher.
pub trait Behavior { pub trait Behavior {
/// Allow for a behavior to define the snapshot or script used at
/// startup to initalize the isolate. Called exactly once when an
/// Isolate is created.
fn startup_data(&mut self) -> Option<StartupData>;
/// Called whenever Deno.core.dispatch() is called in JavaScript. zero_copy_buf /// Called whenever Deno.core.dispatch() is called in JavaScript. zero_copy_buf
/// corresponds to the second argument of Deno.core.dispatch(). /// corresponds to the second argument of Deno.core.dispatch().
fn dispatch( fn dispatch(
@ -109,7 +110,9 @@ impl<B: Behavior> Drop for Isolate<B> {
static DENO_INIT: Once = ONCE_INIT; static DENO_INIT: Once = ONCE_INIT;
impl<B: Behavior> Isolate<B> { impl<B: Behavior> Isolate<B> {
pub fn new(mut behavior: B) -> Self { /// startup_data defines the snapshot or script used at startup to initalize
/// the isolate.
pub fn new(startup_data: StartupData, behavior: B) -> Self {
DENO_INIT.call_once(|| { DENO_INIT.call_once(|| {
unsafe { libdeno::deno_init() }; unsafe { libdeno::deno_init() };
}); });
@ -118,16 +121,16 @@ impl<B: Behavior> Isolate<B> {
let needs_init = true; let needs_init = true;
// Seperate into Option values for eatch startup type // Seperate into Option values for eatch startup type
let (startup_snapshot, startup_script) = match behavior.startup_data() { let (startup_snapshot, startup_script) = match startup_data {
Some(StartupData::Snapshot(d)) => (Some(d), None), StartupData::Snapshot(d) => (Some(d), None),
Some(StartupData::Script(d)) => (None, Some(d)), StartupData::Script(d) => (None, Some(d)),
None => (None, None), StartupData::None => (None, None),
}; };
let config = libdeno::deno_config { let config = libdeno::deno_config {
will_snapshot: 0, will_snapshot: 0,
load_snapshot: match startup_snapshot { load_snapshot: match startup_snapshot {
Some(s) => s, Some(s) => Snapshot2::from(s),
None => libdeno::deno_buf::empty(), None => Snapshot2::empty(),
}, },
shared: shared.as_deno_buf(), shared: shared.as_deno_buf(),
recv_cb: Self::pre_dispatch, recv_cb: Self::pre_dispatch,
@ -146,9 +149,7 @@ impl<B: Behavior> Isolate<B> {
// If we want to use execute this has to happen here sadly. // If we want to use execute this has to happen here sadly.
if let Some(s) = startup_script { if let Some(s) = startup_script {
core_isolate core_isolate.execute(s.filename, s.source).unwrap()
.execute(s.filename.as_str(), s.source.as_str())
.unwrap()
}; };
core_isolate core_isolate
@ -329,6 +330,18 @@ impl<B: Behavior> Isolate<B> {
} }
out out
} }
pub fn snapshot_new(&self) -> Result<Snapshot1, JSError> {
let snapshot = unsafe { libdeno::deno_snapshot_new(self.libdeno_isolate) };
if let Some(js_error) = self.last_exception() {
assert_eq!(snapshot.data_ptr, null());
assert_eq!(snapshot.data_len, 0);
return Err(js_error);
}
assert_ne!(snapshot.data_ptr, null());
assert_ne!(snapshot.data_len, 0);
Ok(snapshot)
}
} }
/// Called during mod_instantiate() to resolve imports. /// Called during mod_instantiate() to resolve imports.
@ -546,10 +559,13 @@ pub mod tests {
impl TestBehavior { impl TestBehavior {
pub fn setup(mode: TestBehaviorMode) -> Isolate<Self> { pub fn setup(mode: TestBehaviorMode) -> Isolate<Self> {
let mut isolate = Isolate::new(TestBehavior { let mut isolate = Isolate::new(
StartupData::None,
TestBehavior {
dispatch_count: 0, dispatch_count: 0,
mode, mode,
}); },
);
js_check(isolate.execute( js_check(isolate.execute(
"setup.js", "setup.js",
r#" r#"
@ -566,10 +582,6 @@ pub mod tests {
} }
impl Behavior for TestBehavior { impl Behavior for TestBehavior {
fn startup_data(&mut self) -> Option<StartupData> {
None
}
fn dispatch( fn dispatch(
&mut self, &mut self,
control: &[u8], control: &[u8],

View file

@ -4,6 +4,7 @@ use libc::c_char;
use libc::c_int; use libc::c_int;
use libc::c_void; use libc::c_void;
use libc::size_t; use libc::size_t;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::ptr::null; use std::ptr::null;
@ -108,6 +109,56 @@ impl AsMut<[u8]> for deno_buf {
} }
} }
#[repr(C)]
pub struct deno_snapshot<'a> {
pub data_ptr: *const u8,
pub data_len: usize,
_marker: PhantomData<&'a [u8]>,
}
/// `deno_snapshot` can not clone, and there is no interior mutability.
/// This type satisfies Send bound.
unsafe impl Send for deno_snapshot<'_> {}
// TODO(ry) Snapshot1 and Snapshot2 are not very good names and need to be
// reconsidered. The entire snapshotting interface is still under construction.
/// The type returned from deno_snapshot_new. Needs to be dropped.
pub type Snapshot1<'a> = deno_snapshot<'a>;
// TODO Does this make sense?
impl Drop for Snapshot1<'_> {
fn drop(&mut self) {
unsafe { deno_snapshot_delete(self) }
}
}
/// The type created from slice. Used for loading.
pub type Snapshot2<'a> = deno_snapshot<'a>;
/// Converts Rust &Buf to libdeno `deno_buf`.
impl<'a> From<&'a [u8]> for Snapshot2<'a> {
#[inline]
fn from(x: &'a [u8]) -> Self {
Self {
data_ptr: x.as_ref().as_ptr(),
data_len: x.len(),
_marker: PhantomData,
}
}
}
impl Snapshot2<'_> {
#[inline]
pub fn empty() -> Self {
Self {
data_ptr: null(),
data_len: 0,
_marker: PhantomData,
}
}
}
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
type deno_recv_cb = unsafe extern "C" fn( type deno_recv_cb = unsafe extern "C" fn(
user_data: *mut c_void, user_data: *mut c_void,
@ -126,9 +177,9 @@ type deno_resolve_cb = unsafe extern "C" fn(
) -> deno_mod; ) -> deno_mod;
#[repr(C)] #[repr(C)]
pub struct deno_config { pub struct deno_config<'a> {
pub will_snapshot: c_int, pub will_snapshot: c_int,
pub load_snapshot: deno_buf, pub load_snapshot: Snapshot2<'a>,
pub shared: deno_buf, pub shared: deno_buf,
pub recv_cb: deno_recv_cb, pub recv_cb: deno_recv_cb,
} }
@ -214,4 +265,9 @@ extern "C" {
user_data: *const c_void, user_data: *const c_void,
id: deno_mod, id: deno_mod,
); );
pub fn deno_snapshot_new(i: *const isolate) -> Snapshot1<'static>;
#[allow(dead_code)]
pub fn deno_snapshot_delete(s: &mut deno_snapshot);
} }

View file

@ -93,7 +93,7 @@ void deno_unlock(Deno* d_) {
d->locker_ = nullptr; d->locker_ = nullptr;
} }
deno_buf deno_get_snapshot(Deno* d_) { deno_snapshot deno_snapshot_new(Deno* d_) {
auto* d = unwrap(d_); auto* d = unwrap(d_);
CHECK_NOT_NULL(d->snapshot_creator_); CHECK_NOT_NULL(d->snapshot_creator_);
d->ClearModules(); d->ClearModules();
@ -101,8 +101,12 @@ deno_buf deno_get_snapshot(Deno* d_) {
auto blob = d->snapshot_creator_->CreateBlob( auto blob = d->snapshot_creator_->CreateBlob(
v8::SnapshotCreator::FunctionCodeHandling::kKeep); v8::SnapshotCreator::FunctionCodeHandling::kKeep);
return {nullptr, 0, reinterpret_cast<uint8_t*>(const_cast<char*>(blob.data)), return {reinterpret_cast<uint8_t*>(const_cast<char*>(blob.data)),
blob.raw_size, 0}; blob.raw_size};
}
void deno_snapshot_delete(deno_snapshot snapshot) {
delete[] snapshot.data_ptr;
} }
static std::unique_ptr<v8::Platform> platform; static std::unique_ptr<v8::Platform> platform;

View file

@ -18,6 +18,11 @@ typedef struct {
size_t zero_copy_id; // 0 = normal, 1 = must call deno_zero_copy_release. size_t zero_copy_id; // 0 = normal, 1 = must call deno_zero_copy_release.
} deno_buf; } deno_buf;
typedef struct {
uint8_t* data_ptr;
size_t data_len;
} deno_snapshot;
typedef struct deno_s Deno; typedef struct deno_s Deno;
// A callback to receive a message from a libdeno.send() javascript call. // A callback to receive a message from a libdeno.send() javascript call.
@ -31,23 +36,28 @@ const char* deno_v8_version();
void deno_set_v8_flags(int* argc, char** argv); void deno_set_v8_flags(int* argc, char** argv);
typedef struct { typedef struct {
int will_snapshot; // Default 0. If calling deno_get_snapshot 1. int will_snapshot; // Default 0. If calling deno_snapshot_new 1.
deno_buf load_snapshot; // Optionally: A deno_buf from deno_get_snapshot. deno_snapshot load_snapshot; // A startup snapshot to use.
deno_buf shared; // Shared buffer to be mapped to libdeno.shared deno_buf shared; // Shared buffer to be mapped to libdeno.shared
deno_recv_cb recv_cb; // Maps to libdeno.send() calls. deno_recv_cb recv_cb; // Maps to libdeno.send() calls.
} deno_config; } deno_config;
// Create a new deno isolate. // Create a new deno isolate.
// Warning: If config.will_snapshot is set, deno_get_snapshot() must be called // Warning: If config.will_snapshot is set, deno_snapshot_new() must be called
// or an error will result. // or an error will result.
Deno* deno_new(deno_config config); Deno* deno_new(deno_config config);
// Generate a snapshot. The resulting buf can be used with deno_new.
// The caller must free the returned data by calling delete[] buf.data_ptr.
deno_buf deno_get_snapshot(Deno* d);
void deno_delete(Deno* d); void deno_delete(Deno* d);
// Generate a snapshot. The resulting buf can be used in as the load_snapshot
// member in deno_confg.
// When calling this function, the caller must have created the isolate "d" with
// "will_snapshot" set to 1.
// The caller must free the returned data with deno_snapshot_delete().
deno_snapshot deno_snapshot_new(Deno* d);
// Only for use with data returned from deno_snapshot_new.
void deno_snapshot_delete(deno_snapshot);
void deno_lock(Deno* d); void deno_lock(Deno* d);
void deno_unlock(Deno* d); void deno_unlock(Deno* d);

View file

@ -169,6 +169,7 @@ static intptr_t external_references[] = {
0}; 0};
static const deno_buf empty_buf = {nullptr, 0, nullptr, 0, 0}; static const deno_buf empty_buf = {nullptr, 0, nullptr, 0, 0};
static const deno_snapshot empty_snapshot = {nullptr, 0};
Deno* NewFromSnapshot(void* user_data, deno_recv_cb cb); Deno* NewFromSnapshot(void* user_data, deno_recv_cb cb);

View file

@ -10,10 +10,10 @@ TEST(LibDenoTest, InitializesCorrectly) {
} }
TEST(LibDenoTest, Snapshotter) { TEST(LibDenoTest, Snapshotter) {
Deno* d1 = deno_new(deno_config{1, empty, empty, nullptr}); Deno* d1 = deno_new(deno_config{1, empty_snapshot, empty, nullptr});
deno_execute(d1, nullptr, "a.js", "a = 1 + 2"); deno_execute(d1, nullptr, "a.js", "a = 1 + 2");
EXPECT_EQ(nullptr, deno_last_exception(d1)); EXPECT_EQ(nullptr, deno_last_exception(d1));
deno_buf test_snapshot = deno_get_snapshot(d1); deno_snapshot test_snapshot = deno_snapshot_new(d1);
deno_delete(d1); deno_delete(d1);
Deno* d2 = deno_new(deno_config{0, test_snapshot, empty, nullptr}); Deno* d2 = deno_new(deno_config{0, test_snapshot, empty, nullptr});
@ -21,7 +21,7 @@ TEST(LibDenoTest, Snapshotter) {
EXPECT_EQ(nullptr, deno_last_exception(d2)); EXPECT_EQ(nullptr, deno_last_exception(d2));
deno_delete(d2); deno_delete(d2);
delete[] test_snapshot.data_ptr; deno_snapshot_delete(test_snapshot);
} }
TEST(LibDenoTest, CanCallFunction) { TEST(LibDenoTest, CanCallFunction) {
@ -249,7 +249,7 @@ TEST(LibDenoTest, CheckPromiseErrors) {
} }
TEST(LibDenoTest, LastException) { TEST(LibDenoTest, LastException) {
Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); Deno* d = deno_new(deno_config{0, empty_snapshot, empty, nullptr});
EXPECT_EQ(deno_last_exception(d), nullptr); EXPECT_EQ(deno_last_exception(d), nullptr);
deno_execute(d, nullptr, "a.js", "\n\nthrow Error('boo');\n\n"); deno_execute(d, nullptr, "a.js", "\n\nthrow Error('boo');\n\n");
EXPECT_STREQ(deno_last_exception(d), EXPECT_STREQ(deno_last_exception(d),
@ -264,7 +264,7 @@ TEST(LibDenoTest, LastException) {
} }
TEST(LibDenoTest, EncodeErrorBug) { TEST(LibDenoTest, EncodeErrorBug) {
Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); Deno* d = deno_new(deno_config{0, empty_snapshot, empty, nullptr});
EXPECT_EQ(deno_last_exception(d), nullptr); EXPECT_EQ(deno_last_exception(d), nullptr);
deno_execute(d, nullptr, "a.js", "eval('a')"); deno_execute(d, nullptr, "a.js", "eval('a')");
EXPECT_STREQ( EXPECT_STREQ(
@ -293,7 +293,7 @@ TEST(LibDenoTest, Shared) {
} }
TEST(LibDenoTest, Utf8Bug) { TEST(LibDenoTest, Utf8Bug) {
Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); Deno* d = deno_new(deno_config{0, empty_snapshot, empty, nullptr});
// The following is a valid UTF-8 javascript which just defines a string // The following is a valid UTF-8 javascript which just defines a string
// literal. We had a bug where libdeno would choke on this. // literal. We had a bug where libdeno would choke on this.
deno_execute(d, nullptr, "a.js", "x = \"\xEF\xBF\xBD\""); deno_execute(d, nullptr, "a.js", "x = \"\xEF\xBF\xBD\"");
@ -318,7 +318,7 @@ TEST(LibDenoTest, LibDenoEvalContextError) {
TEST(LibDenoTest, SharedAtomics) { TEST(LibDenoTest, SharedAtomics) {
int32_t s[] = {0, 1, 2}; int32_t s[] = {0, 1, 2};
deno_buf shared = {nullptr, 0, reinterpret_cast<uint8_t*>(s), sizeof s, 0}; deno_buf shared = {nullptr, 0, reinterpret_cast<uint8_t*>(s), sizeof s, 0};
Deno* d = deno_new(deno_config{0, empty, shared, nullptr}); Deno* d = deno_new(deno_config{0, empty_snapshot, shared, nullptr});
deno_execute(d, nullptr, "a.js", deno_execute(d, nullptr, "a.js",
"Atomics.add(new Int32Array(Deno.core.shared), 0, 1)"); "Atomics.add(new Int32Array(Deno.core.shared), 0, 1)");
EXPECT_EQ(nullptr, deno_last_exception(d)); EXPECT_EQ(nullptr, deno_last_exception(d));

View file

@ -13,7 +13,7 @@ void recv_cb(void* user_data, deno_buf buf, deno_buf zero_copy_buf) {
TEST(ModulesTest, Resolution) { TEST(ModulesTest, Resolution) {
exec_count = 0; // Reset exec_count = 0; // Reset
Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb});
EXPECT_EQ(0, exec_count); EXPECT_EQ(0, exec_count);
static deno_mod a = deno_mod_new(d, true, "a.js", static deno_mod a = deno_mod_new(d, true, "a.js",
@ -66,7 +66,7 @@ TEST(ModulesTest, Resolution) {
TEST(ModulesTest, ResolutionError) { TEST(ModulesTest, ResolutionError) {
exec_count = 0; // Reset exec_count = 0; // Reset
Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb});
EXPECT_EQ(0, exec_count); EXPECT_EQ(0, exec_count);
static deno_mod a = deno_mod_new(d, true, "a.js", static deno_mod a = deno_mod_new(d, true, "a.js",
@ -99,7 +99,7 @@ TEST(ModulesTest, ResolutionError) {
TEST(ModulesTest, ImportMetaUrl) { TEST(ModulesTest, ImportMetaUrl) {
exec_count = 0; // Reset exec_count = 0; // Reset
Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb});
EXPECT_EQ(0, exec_count); EXPECT_EQ(0, exec_count);
static deno_mod a = static deno_mod a =
@ -119,7 +119,7 @@ TEST(ModulesTest, ImportMetaUrl) {
} }
TEST(ModulesTest, ImportMetaMain) { TEST(ModulesTest, ImportMetaMain) {
Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); Deno* d = deno_new(deno_config{0, empty_snapshot, empty, recv_cb});
const char* throw_not_main_src = "if (!import.meta.main) throw 'err'"; const char* throw_not_main_src = "if (!import.meta.main) throw 'err'";
static deno_mod throw_not_main = static deno_mod throw_not_main =

View file

@ -8,8 +8,6 @@
#include "third_party/v8/include/v8.h" #include "third_party/v8/include/v8.h"
#include "third_party/v8/src/base/logging.h" #include "third_party/v8/src/base/logging.h"
namespace deno {} // namespace deno
int main(int argc, char** argv) { int main(int argc, char** argv) {
const char* snapshot_out_bin = argv[1]; const char* snapshot_out_bin = argv[1];
const char* js_fn = argv[2]; const char* js_fn = argv[2];
@ -23,7 +21,7 @@ int main(int argc, char** argv) {
CHECK(deno::ReadFileToString(js_fn, &js_source)); CHECK(deno::ReadFileToString(js_fn, &js_source));
deno_init(); deno_init();
deno_config config = {1, deno::empty_buf, deno::empty_buf, nullptr}; deno_config config = {1, deno::empty_snapshot, deno::empty_buf, nullptr};
Deno* d = deno_new(config); Deno* d = deno_new(config);
deno_execute(d, nullptr, js_fn, js_source.c_str()); deno_execute(d, nullptr, js_fn, js_source.c_str());
@ -34,13 +32,13 @@ int main(int argc, char** argv) {
return 1; return 1;
} }
auto snapshot = deno_get_snapshot(d); auto snapshot = deno_snapshot_new(d);
std::ofstream file_(snapshot_out_bin, std::ios::binary); std::ofstream file_(snapshot_out_bin, std::ios::binary);
file_.write(reinterpret_cast<char*>(snapshot.data_ptr), snapshot.data_len); file_.write(reinterpret_cast<char*>(snapshot.data_ptr), snapshot.data_len);
file_.close(); file_.close();
delete[] snapshot.data_ptr; deno_snapshot_delete(snapshot);
deno_delete(d); deno_delete(d);
return file_.bad(); return file_.bad();

View file

@ -3,7 +3,7 @@
#include <string> #include <string>
#include "file_util.h" #include "file_util.h"
deno_buf snapshot = {nullptr, 0, nullptr, 0, 0}; deno_snapshot snapshot = {nullptr, 0};
int main(int argc, char** argv) { int main(int argc, char** argv) {
// Locate the snapshot. // Locate the snapshot.

View file

@ -5,7 +5,8 @@
#include "deno.h" #include "deno.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
extern deno_buf snapshot; // Loaded in libdeno/test.cc extern deno_snapshot snapshot; // Loaded in libdeno/test.cc
const deno_buf empty = {nullptr, 0, nullptr, 0, 0}; const deno_buf empty = {nullptr, 0, nullptr, 0, 0};
const deno_snapshot empty_snapshot = {nullptr, 0};
#endif // TEST_H_ #endif // TEST_H_