From 2a5138a5166f0945d5fda68c89fa8e23c66fb681 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 12 Jun 2019 10:53:24 -0700 Subject: [PATCH] Remove Config struct from core (#2502) It's unnecessary indirection and is preventing the ability to easily pass isolate references into the dispatch and dyn_import closures. Note: this changes how StartupData::Script is executed. It's no longer done during Isolate::new() but rather lazily on first poll or execution. --- cli/worker.rs | 25 ++++--- core/examples/http_bench.rs | 5 +- core/isolate.rs | 126 ++++++++++++++++++------------------ 3 files changed, 78 insertions(+), 78 deletions(-) diff --git a/cli/worker.rs b/cli/worker.rs index f856ca0c0c..94d33e3919 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -5,7 +5,6 @@ use crate::js_errors; use crate::state::ThreadSafeState; use crate::tokio_util; use deno; -use deno::Config; use deno::JSError; use deno::StartupData; use futures::Async; @@ -18,7 +17,7 @@ use url::Url; /// high-level module loading #[derive(Clone)] pub struct Worker { - inner: Arc>, + isolate: Arc>, pub state: ThreadSafeState, } @@ -29,14 +28,14 @@ impl Worker { state: ThreadSafeState, ) -> Worker { let state_ = state.clone(); - let mut config = Config::default(); - config.dispatch(move |control_buf, zero_copy_buf| { - state_.dispatch(control_buf, zero_copy_buf) - }); - Self { - inner: Arc::new(Mutex::new(deno::Isolate::new(startup_data, config))), - state, + let isolate = Arc::new(Mutex::new(deno::Isolate::new(startup_data, false))); + { + let mut i = isolate.lock().unwrap(); + i.set_dispatch(move |control_buf, zero_copy_buf| { + state_.dispatch(control_buf, zero_copy_buf) + }); } + Self { isolate, state } } /// Same as execute2() but the filename defaults to "". @@ -51,7 +50,7 @@ impl Worker { js_filename: &str, js_source: &str, ) -> Result<(), JSError> { - let mut isolate = self.inner.lock().unwrap(); + let mut isolate = self.isolate.lock().unwrap(); isolate.execute(js_filename, js_source) } @@ -64,7 +63,7 @@ impl Worker { let worker = self.clone(); let worker_ = worker.clone(); let loader = self.state.clone(); - let isolate = self.inner.clone(); + let isolate = self.isolate.clone(); let modules = self.state.modules.clone(); let recursive_load = deno::RecursiveLoad::new(js_url.as_str(), loader, isolate, modules); @@ -74,7 +73,7 @@ impl Worker { if is_prefetch { Ok(()) } else { - let mut isolate = worker.inner.lock().unwrap(); + let mut isolate = worker.isolate.lock().unwrap(); let result = isolate.mod_evaluate(id); if let Err(err) = result { Err(deno::JSErrorOr::JSError(err)) @@ -166,7 +165,7 @@ impl Future for Worker { type Error = JSError; fn poll(&mut self) -> Result, Self::Error> { - let mut isolate = self.inner.lock().unwrap(); + let mut isolate = self.isolate.lock().unwrap(); isolate.poll().map_err(|err| self.apply_source_map(err)) } } diff --git a/core/examples/http_bench.rs b/core/examples/http_bench.rs index 757e9a3b71..e8c5ec1b73 100644 --- a/core/examples/http_bench.rs +++ b/core/examples/http_bench.rs @@ -179,9 +179,8 @@ fn main() { filename: "http_bench.js", }); - let mut config = deno::Config::default(); - config.dispatch(dispatch); - let isolate = deno::Isolate::new(startup_data, config); + let mut isolate = deno::Isolate::new(startup_data, false); + isolate.set_dispatch(dispatch); isolate.then(|r| { js_check(r); diff --git a/core/isolate.rs b/core/isolate.rs index 18ac381838..14e1b88aad 100644 --- a/core/isolate.rs +++ b/core/isolate.rs @@ -42,6 +42,22 @@ pub struct Script<'a> { pub filename: &'a str, } +// TODO(ry) It's ugly that we have both Script and OwnedScript. Ideally we +// wouldn't expose such twiddly complexity. +struct OwnedScript { + pub source: String, + pub filename: String, +} + +impl<'a> From> for OwnedScript { + fn from(s: Script<'a>) -> OwnedScript { + OwnedScript { + source: s.source.to_string(), + filename: s.filename.to_string(), + } + } +} + /// Represents data used to initialize isolate at startup /// either a binary snapshot or a javascript source file /// in the form of the StartupScript struct. @@ -77,32 +93,6 @@ impl Future for DynImport { } } -#[derive(Default)] -pub struct Config { - dispatch: Option>, - dyn_import: Option>, - pub will_snapshot: bool, -} - -impl Config { - /// Defines the how Deno.core.dispatch() acts. - /// Called whenever Deno.core.dispatch() is called in JavaScript. zero_copy_buf - /// corresponds to the second argument of Deno.core.dispatch(). - pub fn dispatch(&mut self, f: F) - where - F: Fn(&[u8], Option) -> Op + Send + Sync + 'static, - { - self.dispatch = Some(Arc::new(f)); - } - - pub fn dyn_import(&mut self, f: F) - where - F: Fn(&str, &str) -> DynImportFuture + Send + Sync + 'static, - { - self.dyn_import = Some(Arc::new(f)); - } -} - /// A single execution context of JavaScript. Corresponds roughly to the "Web /// Worker" concept in the DOM. An Isolate is a Future that can be used with /// Tokio. The Isolate future complete when there is an error or when all @@ -114,12 +104,14 @@ impl Config { pub struct Isolate { libdeno_isolate: *const libdeno::isolate, shared_libdeno_isolate: Arc>>, - config: Config, + dispatch: Option>, + dyn_import: Option>, needs_init: bool, shared: SharedQueue, pending_ops: FuturesUnordered, pending_dyn_imports: FuturesUnordered, have_unpolled_ops: bool, + startup_script: Option, } unsafe impl Send for Isolate {} @@ -138,9 +130,7 @@ static DENO_INIT: Once = ONCE_INIT; impl Isolate { /// startup_data defines the snapshot or script used at startup to initalize /// the isolate. - // TODO(ry) move startup_data into Config. Ideally without introducing a - // generic lifetime into the Isolate struct... - pub fn new(startup_data: StartupData, config: Config) -> Self { + pub fn new(startup_data: StartupData, will_snapshot: bool) -> Self { DENO_INIT.call_once(|| { unsafe { libdeno::deno_init() }; }); @@ -149,19 +139,20 @@ impl Isolate { let needs_init = true; - let mut startup_script: Option