mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
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:
parent
cdb72afd8d
commit
f7fdb90fd5
18 changed files with 182 additions and 121 deletions
|
@ -4,24 +4,16 @@ use crate::ops;
|
|||
use deno::deno_buf;
|
||||
use deno::Behavior;
|
||||
use deno::Op;
|
||||
use deno::StartupData;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Implements deno::Behavior for the main Deno command-line.
|
||||
pub struct CliBehavior {
|
||||
startup_data: Option<StartupData>,
|
||||
pub state: Arc<IsolateState>,
|
||||
}
|
||||
|
||||
impl CliBehavior {
|
||||
pub fn new(
|
||||
startup_data: Option<StartupData>,
|
||||
state: Arc<IsolateState>,
|
||||
) -> Self {
|
||||
Self {
|
||||
startup_data,
|
||||
state,
|
||||
}
|
||||
pub fn new(state: Arc<IsolateState>) -> Self {
|
||||
Self { state }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,10 +30,6 @@ impl IsolateStateContainer for CliBehavior {
|
|||
}
|
||||
|
||||
impl Behavior for CliBehavior {
|
||||
fn startup_data(&mut self) -> Option<StartupData> {
|
||||
self.startup_data.take()
|
||||
}
|
||||
|
||||
fn dispatch(
|
||||
&mut self,
|
||||
control: &[u8],
|
||||
|
|
|
@ -17,7 +17,6 @@ use deno::Behavior;
|
|||
use deno::Buf;
|
||||
use deno::JSError;
|
||||
use deno::Op;
|
||||
use deno::StartupData;
|
||||
use futures::future::*;
|
||||
use futures::sync::oneshot;
|
||||
use futures::Future;
|
||||
|
@ -70,10 +69,6 @@ impl IsolateStateContainer for &CompilerBehavior {
|
|||
}
|
||||
|
||||
impl Behavior for CompilerBehavior {
|
||||
fn startup_data(&mut self) -> Option<StartupData> {
|
||||
Some(startup_data::compiler_isolate_init())
|
||||
}
|
||||
|
||||
fn dispatch(
|
||||
&mut self,
|
||||
control: &[u8],
|
||||
|
@ -148,6 +143,7 @@ fn lazy_start(parent_state: Arc<IsolateState>) -> ResourceId {
|
|||
cell
|
||||
.get_or_insert_with(|| {
|
||||
let worker_result = workers::spawn(
|
||||
startup_data::compiler_isolate_init(),
|
||||
CompilerBehavior::new(
|
||||
parent_state.flags.clone(),
|
||||
parent_state.argv.clone(),
|
||||
|
|
|
@ -13,6 +13,7 @@ use deno;
|
|||
use deno::deno_mod;
|
||||
use deno::Behavior;
|
||||
use deno::JSError;
|
||||
use deno::StartupData;
|
||||
use futures::future::Either;
|
||||
use futures::Async;
|
||||
use futures::Future;
|
||||
|
@ -32,10 +33,10 @@ pub struct Isolate<B: Behavior> {
|
|||
}
|
||||
|
||||
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();
|
||||
Self {
|
||||
inner: CoreIsolate::new(behavior),
|
||||
inner: CoreIsolate::new(startup_data, behavior),
|
||||
state,
|
||||
}
|
||||
}
|
||||
|
@ -270,8 +271,8 @@ mod tests {
|
|||
let state = Arc::new(IsolateState::new(flags, rest_argv, None, false));
|
||||
let state_ = state.clone();
|
||||
tokio_util::run(lazy(move || {
|
||||
let cli = CliBehavior::new(None, state.clone());
|
||||
let mut isolate = Isolate::new(cli);
|
||||
let cli = CliBehavior::new(state.clone());
|
||||
let mut isolate = Isolate::new(StartupData::None, cli);
|
||||
if let Err(err) = isolate.execute_mod(&filename, false) {
|
||||
eprintln!("execute_mod err {:?}", err);
|
||||
}
|
||||
|
@ -293,8 +294,8 @@ mod tests {
|
|||
let state = Arc::new(IsolateState::new(flags, rest_argv, None, false));
|
||||
let state_ = state.clone();
|
||||
tokio_util::run(lazy(move || {
|
||||
let cli = CliBehavior::new(None, state.clone());
|
||||
let mut isolate = Isolate::new(cli);
|
||||
let cli = CliBehavior::new(state.clone());
|
||||
let mut isolate = Isolate::new(StartupData::None, cli);
|
||||
if let Err(err) = isolate.execute_mod(&filename, false) {
|
||||
eprintln!("execute_mod err {:?}", err);
|
||||
}
|
||||
|
|
|
@ -107,9 +107,8 @@ fn main() {
|
|||
|
||||
let state = Arc::new(IsolateState::new(flags, rest_argv, None, false));
|
||||
let state_ = state.clone();
|
||||
let startup_data = startup_data::deno_isolate_init();
|
||||
let cli = CliBehavior::new(Some(startup_data), state_);
|
||||
let mut isolate = Isolate::new(cli);
|
||||
let cli = CliBehavior::new(state_);
|
||||
let mut isolate = Isolate::new(startup_data::deno_isolate_init(), cli);
|
||||
|
||||
let main_future = lazy(move || {
|
||||
// Setup runtime.
|
||||
|
|
|
@ -15,6 +15,7 @@ use crate::resolve_addr::resolve_addr;
|
|||
use crate::resources;
|
||||
use crate::resources::table_entries;
|
||||
use crate::resources::Resource;
|
||||
use crate::startup_data;
|
||||
use crate::tokio_util;
|
||||
use crate::tokio_write;
|
||||
use crate::version;
|
||||
|
@ -1865,6 +1866,7 @@ fn op_create_worker(
|
|||
parent_state.argv.clone(),
|
||||
);
|
||||
match workers::spawn(
|
||||
startup_data::deno_isolate_init(),
|
||||
behavior,
|
||||
&format!("USER-WORKER-{}", specifier),
|
||||
workers::WorkerInit::Module(specifier.to_string()),
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
use deno::deno_buf;
|
||||
use deno::{Script, StartupData};
|
||||
|
||||
pub fn deno_isolate_init() -> StartupData {
|
||||
pub fn deno_isolate_init() -> StartupData<'static> {
|
||||
if cfg!(feature = "no-snapshot-init") {
|
||||
debug!("Deno isolate init without snapshots.");
|
||||
#[cfg(not(feature = "check-only"))]
|
||||
|
@ -12,8 +11,8 @@ pub fn deno_isolate_init() -> StartupData {
|
|||
let source_bytes = vec![];
|
||||
|
||||
StartupData::Script(Script {
|
||||
filename: "gen/cli/bundle/main.js".to_string(),
|
||||
source: std::str::from_utf8(&source_bytes[..]).unwrap().to_string(),
|
||||
filename: "gen/cli/bundle/main.js",
|
||||
source: std::str::from_utf8(&source_bytes[..]).unwrap(),
|
||||
})
|
||||
} else {
|
||||
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"))]
|
||||
let data = vec![];
|
||||
|
||||
unsafe {
|
||||
StartupData::Snapshot(deno_buf::from_raw_parts(data.as_ptr(), data.len()))
|
||||
}
|
||||
StartupData::Snapshot(data)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compiler_isolate_init() -> StartupData {
|
||||
pub fn compiler_isolate_init() -> StartupData<'static> {
|
||||
if cfg!(feature = "no-snapshot-init") {
|
||||
debug!("Compiler isolate init without snapshots.");
|
||||
#[cfg(not(feature = "check-only"))]
|
||||
|
@ -41,8 +38,8 @@ pub fn compiler_isolate_init() -> StartupData {
|
|||
let source_bytes = vec![];
|
||||
|
||||
StartupData::Script(Script {
|
||||
filename: "gen/cli/bundle/compiler.js".to_string(),
|
||||
source: std::str::from_utf8(&source_bytes[..]).unwrap().to_string(),
|
||||
filename: "gen/cli/bundle/compiler.js",
|
||||
source: std::str::from_utf8(&source_bytes[..]).unwrap(),
|
||||
})
|
||||
} else {
|
||||
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"))]
|
||||
let data = vec![];
|
||||
|
||||
unsafe {
|
||||
StartupData::Snapshot(deno_buf::from_raw_parts(data.as_ptr(), data.len()))
|
||||
}
|
||||
StartupData::Snapshot(data)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ use crate::isolate_state::IsolateStateContainer;
|
|||
use crate::isolate_state::WorkerChannels;
|
||||
use crate::ops;
|
||||
use crate::resources;
|
||||
use crate::startup_data;
|
||||
use deno::deno_buf;
|
||||
use deno::Behavior;
|
||||
use deno::Buf;
|
||||
|
@ -44,10 +43,6 @@ impl IsolateStateContainer for &UserWorkerBehavior {
|
|||
}
|
||||
|
||||
impl Behavior for UserWorkerBehavior {
|
||||
fn startup_data(&mut self) -> Option<StartupData> {
|
||||
Some(startup_data::deno_isolate_init())
|
||||
}
|
||||
|
||||
fn dispatch(
|
||||
&mut self,
|
||||
control: &[u8],
|
||||
|
@ -83,7 +78,7 @@ pub struct Worker<B: WorkerBehavior> {
|
|||
}
|
||||
|
||||
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_out_tx, worker_out_rx) = mpsc::channel::<Buf>(1);
|
||||
|
||||
|
@ -92,7 +87,7 @@ impl<B: WorkerBehavior> Worker<B> {
|
|||
|
||||
behavior.set_internal_channels(internal_channels);
|
||||
|
||||
let isolate = Isolate::new(behavior);
|
||||
let isolate = Isolate::new(startup_data, behavior);
|
||||
|
||||
Worker {
|
||||
isolate,
|
||||
|
@ -129,12 +124,13 @@ pub enum WorkerInit {
|
|||
}
|
||||
|
||||
pub fn spawn<B: WorkerBehavior + 'static>(
|
||||
startup_data: StartupData,
|
||||
behavior: B,
|
||||
worker_debug_name: &str,
|
||||
init: WorkerInit,
|
||||
) -> Result<Worker<B>, RustOrJsError> {
|
||||
let state = behavior.state().clone();
|
||||
let mut worker = Worker::new(behavior);
|
||||
let mut worker = Worker::new(startup_data, behavior);
|
||||
|
||||
worker
|
||||
.execute(&format!("denoMain('{}')", worker_debug_name))
|
||||
|
@ -172,6 +168,7 @@ mod tests {
|
|||
use crate::compiler::CompilerBehavior;
|
||||
use crate::isolate_state::IsolateState;
|
||||
use crate::js_errors::JSErrorColor;
|
||||
use crate::startup_data;
|
||||
use crate::tokio_util;
|
||||
use futures::future::lazy;
|
||||
use std::thread;
|
||||
|
@ -180,6 +177,7 @@ mod tests {
|
|||
fn test_spawn() {
|
||||
tokio_util::init(|| {
|
||||
let worker_result = spawn(
|
||||
startup_data::compiler_isolate_init(),
|
||||
CompilerBehavior::new(
|
||||
IsolateState::mock().flags.clone(),
|
||||
IsolateState::mock().argv.clone(),
|
||||
|
@ -242,6 +240,7 @@ mod tests {
|
|||
fn removed_from_resource_table_on_close() {
|
||||
tokio_util::init(|| {
|
||||
let worker_result = spawn(
|
||||
startup_data::compiler_isolate_init(),
|
||||
CompilerBehavior::new(
|
||||
IsolateState::mock().flags.clone(),
|
||||
IsolateState::mock().argv.clone(),
|
||||
|
|
|
@ -99,15 +99,6 @@ pub type HttpBenchOp = dyn Future<Item = i32, Error = std::io::Error> + Send;
|
|||
struct 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(
|
||||
&mut self,
|
||||
control: &[u8],
|
||||
|
@ -168,7 +159,15 @@ fn main() {
|
|||
// TODO currently isolate.execute() must be run inside tokio, hence the
|
||||
// lazy(). It would be nice to not have that contraint. Probably requires
|
||||
// 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| {
|
||||
js_check(r);
|
||||
|
|
|
@ -8,6 +8,8 @@ use crate::js_errors::JSError;
|
|||
use crate::libdeno;
|
||||
use crate::libdeno::deno_buf;
|
||||
use crate::libdeno::deno_mod;
|
||||
use crate::libdeno::Snapshot1;
|
||||
use crate::libdeno::Snapshot2;
|
||||
use crate::shared_queue::SharedQueue;
|
||||
use crate::shared_queue::RECOMMENDED_SIZE;
|
||||
use futures::Async;
|
||||
|
@ -16,6 +18,7 @@ use futures::Poll;
|
|||
use libc::c_void;
|
||||
use std::ffi::CStr;
|
||||
use std::ffi::CString;
|
||||
use std::ptr::null;
|
||||
use std::sync::{Arc, Mutex, Once, ONCE_INIT};
|
||||
|
||||
pub type Buf = Box<[u8]>;
|
||||
|
@ -48,26 +51,24 @@ impl Future for PendingOp {
|
|||
}
|
||||
|
||||
/// Stores a script used to initalize a Isolate
|
||||
pub struct Script {
|
||||
pub source: String,
|
||||
pub filename: String,
|
||||
pub struct Script<'a> {
|
||||
pub source: &'a str,
|
||||
pub filename: &'a str,
|
||||
}
|
||||
|
||||
/// Represents data used to initialize isolate at startup
|
||||
/// either a binary snapshot or a javascript source file
|
||||
/// in the form of the StartupScript struct.
|
||||
pub enum StartupData {
|
||||
Script(Script),
|
||||
Snapshot(deno_buf),
|
||||
pub enum StartupData<'a> {
|
||||
Script(Script<'a>),
|
||||
Snapshot(&'a [u8]),
|
||||
None,
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
/// 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
|
||||
/// corresponds to the second argument of Deno.core.dispatch().
|
||||
fn dispatch(
|
||||
|
@ -109,7 +110,9 @@ impl<B: Behavior> Drop for Isolate<B> {
|
|||
static DENO_INIT: Once = ONCE_INIT;
|
||||
|
||||
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(|| {
|
||||
unsafe { libdeno::deno_init() };
|
||||
});
|
||||
|
@ -118,16 +121,16 @@ impl<B: Behavior> Isolate<B> {
|
|||
|
||||
let needs_init = true;
|
||||
// Seperate into Option values for eatch startup type
|
||||
let (startup_snapshot, startup_script) = match behavior.startup_data() {
|
||||
Some(StartupData::Snapshot(d)) => (Some(d), None),
|
||||
Some(StartupData::Script(d)) => (None, Some(d)),
|
||||
None => (None, None),
|
||||
let (startup_snapshot, startup_script) = match startup_data {
|
||||
StartupData::Snapshot(d) => (Some(d), None),
|
||||
StartupData::Script(d) => (None, Some(d)),
|
||||
StartupData::None => (None, None),
|
||||
};
|
||||
let config = libdeno::deno_config {
|
||||
will_snapshot: 0,
|
||||
load_snapshot: match startup_snapshot {
|
||||
Some(s) => s,
|
||||
None => libdeno::deno_buf::empty(),
|
||||
Some(s) => Snapshot2::from(s),
|
||||
None => Snapshot2::empty(),
|
||||
},
|
||||
shared: shared.as_deno_buf(),
|
||||
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 let Some(s) = startup_script {
|
||||
core_isolate
|
||||
.execute(s.filename.as_str(), s.source.as_str())
|
||||
.unwrap()
|
||||
core_isolate.execute(s.filename, s.source).unwrap()
|
||||
};
|
||||
|
||||
core_isolate
|
||||
|
@ -329,6 +330,18 @@ impl<B: Behavior> Isolate<B> {
|
|||
}
|
||||
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.
|
||||
|
@ -546,10 +559,13 @@ pub mod tests {
|
|||
|
||||
impl TestBehavior {
|
||||
pub fn setup(mode: TestBehaviorMode) -> Isolate<Self> {
|
||||
let mut isolate = Isolate::new(TestBehavior {
|
||||
dispatch_count: 0,
|
||||
mode,
|
||||
});
|
||||
let mut isolate = Isolate::new(
|
||||
StartupData::None,
|
||||
TestBehavior {
|
||||
dispatch_count: 0,
|
||||
mode,
|
||||
},
|
||||
);
|
||||
js_check(isolate.execute(
|
||||
"setup.js",
|
||||
r#"
|
||||
|
@ -566,10 +582,6 @@ pub mod tests {
|
|||
}
|
||||
|
||||
impl Behavior for TestBehavior {
|
||||
fn startup_data(&mut self) -> Option<StartupData> {
|
||||
None
|
||||
}
|
||||
|
||||
fn dispatch(
|
||||
&mut self,
|
||||
control: &[u8],
|
||||
|
|
|
@ -4,6 +4,7 @@ use libc::c_char;
|
|||
use libc::c_int;
|
||||
use libc::c_void;
|
||||
use libc::size_t;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
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)]
|
||||
type deno_recv_cb = unsafe extern "C" fn(
|
||||
user_data: *mut c_void,
|
||||
|
@ -126,9 +177,9 @@ type deno_resolve_cb = unsafe extern "C" fn(
|
|||
) -> deno_mod;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct deno_config {
|
||||
pub struct deno_config<'a> {
|
||||
pub will_snapshot: c_int,
|
||||
pub load_snapshot: deno_buf,
|
||||
pub load_snapshot: Snapshot2<'a>,
|
||||
pub shared: deno_buf,
|
||||
pub recv_cb: deno_recv_cb,
|
||||
}
|
||||
|
@ -214,4 +265,9 @@ extern "C" {
|
|||
user_data: *const c_void,
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -93,7 +93,7 @@ void deno_unlock(Deno* d_) {
|
|||
d->locker_ = nullptr;
|
||||
}
|
||||
|
||||
deno_buf deno_get_snapshot(Deno* d_) {
|
||||
deno_snapshot deno_snapshot_new(Deno* d_) {
|
||||
auto* d = unwrap(d_);
|
||||
CHECK_NOT_NULL(d->snapshot_creator_);
|
||||
d->ClearModules();
|
||||
|
@ -101,8 +101,12 @@ deno_buf deno_get_snapshot(Deno* d_) {
|
|||
|
||||
auto blob = d->snapshot_creator_->CreateBlob(
|
||||
v8::SnapshotCreator::FunctionCodeHandling::kKeep);
|
||||
return {nullptr, 0, reinterpret_cast<uint8_t*>(const_cast<char*>(blob.data)),
|
||||
blob.raw_size, 0};
|
||||
return {reinterpret_cast<uint8_t*>(const_cast<char*>(blob.data)),
|
||||
blob.raw_size};
|
||||
}
|
||||
|
||||
void deno_snapshot_delete(deno_snapshot snapshot) {
|
||||
delete[] snapshot.data_ptr;
|
||||
}
|
||||
|
||||
static std::unique_ptr<v8::Platform> platform;
|
||||
|
|
|
@ -18,6 +18,11 @@ typedef struct {
|
|||
size_t zero_copy_id; // 0 = normal, 1 = must call deno_zero_copy_release.
|
||||
} deno_buf;
|
||||
|
||||
typedef struct {
|
||||
uint8_t* data_ptr;
|
||||
size_t data_len;
|
||||
} deno_snapshot;
|
||||
|
||||
typedef struct deno_s Deno;
|
||||
|
||||
// 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);
|
||||
|
||||
typedef struct {
|
||||
int will_snapshot; // Default 0. If calling deno_get_snapshot 1.
|
||||
deno_buf load_snapshot; // Optionally: A deno_buf from deno_get_snapshot.
|
||||
deno_buf shared; // Shared buffer to be mapped to libdeno.shared
|
||||
deno_recv_cb recv_cb; // Maps to libdeno.send() calls.
|
||||
int will_snapshot; // Default 0. If calling deno_snapshot_new 1.
|
||||
deno_snapshot load_snapshot; // A startup snapshot to use.
|
||||
deno_buf shared; // Shared buffer to be mapped to libdeno.shared
|
||||
deno_recv_cb recv_cb; // Maps to libdeno.send() calls.
|
||||
} deno_config;
|
||||
|
||||
// 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.
|
||||
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);
|
||||
|
||||
// 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_unlock(Deno* d);
|
||||
|
||||
|
|
|
@ -169,6 +169,7 @@ static intptr_t external_references[] = {
|
|||
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);
|
||||
|
||||
|
|
|
@ -10,10 +10,10 @@ TEST(LibDenoTest, InitializesCorrectly) {
|
|||
}
|
||||
|
||||
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");
|
||||
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* d2 = deno_new(deno_config{0, test_snapshot, empty, nullptr});
|
||||
|
@ -21,7 +21,7 @@ TEST(LibDenoTest, Snapshotter) {
|
|||
EXPECT_EQ(nullptr, deno_last_exception(d2));
|
||||
deno_delete(d2);
|
||||
|
||||
delete[] test_snapshot.data_ptr;
|
||||
deno_snapshot_delete(test_snapshot);
|
||||
}
|
||||
|
||||
TEST(LibDenoTest, CanCallFunction) {
|
||||
|
@ -249,7 +249,7 @@ TEST(LibDenoTest, CheckPromiseErrors) {
|
|||
}
|
||||
|
||||
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);
|
||||
deno_execute(d, nullptr, "a.js", "\n\nthrow Error('boo');\n\n");
|
||||
EXPECT_STREQ(deno_last_exception(d),
|
||||
|
@ -264,7 +264,7 @@ TEST(LibDenoTest, LastException) {
|
|||
}
|
||||
|
||||
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);
|
||||
deno_execute(d, nullptr, "a.js", "eval('a')");
|
||||
EXPECT_STREQ(
|
||||
|
@ -293,7 +293,7 @@ TEST(LibDenoTest, Shared) {
|
|||
}
|
||||
|
||||
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
|
||||
// literal. We had a bug where libdeno would choke on this.
|
||||
deno_execute(d, nullptr, "a.js", "x = \"\xEF\xBF\xBD\"");
|
||||
|
@ -318,7 +318,7 @@ TEST(LibDenoTest, LibDenoEvalContextError) {
|
|||
TEST(LibDenoTest, SharedAtomics) {
|
||||
int32_t s[] = {0, 1, 2};
|
||||
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",
|
||||
"Atomics.add(new Int32Array(Deno.core.shared), 0, 1)");
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
|
|
|
@ -13,7 +13,7 @@ void recv_cb(void* user_data, deno_buf buf, deno_buf zero_copy_buf) {
|
|||
|
||||
TEST(ModulesTest, Resolution) {
|
||||
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);
|
||||
|
||||
static deno_mod a = deno_mod_new(d, true, "a.js",
|
||||
|
@ -66,7 +66,7 @@ TEST(ModulesTest, Resolution) {
|
|||
|
||||
TEST(ModulesTest, ResolutionError) {
|
||||
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);
|
||||
|
||||
static deno_mod a = deno_mod_new(d, true, "a.js",
|
||||
|
@ -99,7 +99,7 @@ TEST(ModulesTest, ResolutionError) {
|
|||
|
||||
TEST(ModulesTest, ImportMetaUrl) {
|
||||
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);
|
||||
|
||||
static deno_mod a =
|
||||
|
@ -119,7 +119,7 @@ TEST(ModulesTest, ImportMetaUrl) {
|
|||
}
|
||||
|
||||
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'";
|
||||
static deno_mod throw_not_main =
|
||||
|
|
|
@ -8,8 +8,6 @@
|
|||
#include "third_party/v8/include/v8.h"
|
||||
#include "third_party/v8/src/base/logging.h"
|
||||
|
||||
namespace deno {} // namespace deno
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
const char* snapshot_out_bin = argv[1];
|
||||
const char* js_fn = argv[2];
|
||||
|
@ -23,7 +21,7 @@ int main(int argc, char** argv) {
|
|||
CHECK(deno::ReadFileToString(js_fn, &js_source));
|
||||
|
||||
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_execute(d, nullptr, js_fn, js_source.c_str());
|
||||
|
@ -34,13 +32,13 @@ int main(int argc, char** argv) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
auto snapshot = deno_get_snapshot(d);
|
||||
auto snapshot = deno_snapshot_new(d);
|
||||
|
||||
std::ofstream file_(snapshot_out_bin, std::ios::binary);
|
||||
file_.write(reinterpret_cast<char*>(snapshot.data_ptr), snapshot.data_len);
|
||||
file_.close();
|
||||
|
||||
delete[] snapshot.data_ptr;
|
||||
deno_snapshot_delete(snapshot);
|
||||
deno_delete(d);
|
||||
|
||||
return file_.bad();
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include <string>
|
||||
#include "file_util.h"
|
||||
|
||||
deno_buf snapshot = {nullptr, 0, nullptr, 0, 0};
|
||||
deno_snapshot snapshot = {nullptr, 0};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// Locate the snapshot.
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
#include "deno.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_snapshot empty_snapshot = {nullptr, 0};
|
||||
|
||||
#endif // TEST_H_
|
||||
|
|
Loading…
Reference in a new issue