From 2a5138a5166f0945d5fda68c89fa8e23c66fb681 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 12 Jun 2019 10:53:24 -0700 Subject: 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. --- core/isolate.rs | 126 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 64 insertions(+), 62 deletions(-) (limited to 'core/isolate.rs') diff --git a/core/isolate.rs b/core/isolate.rs index 18ac38183..14e1b88aa 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