From a1f9fc650fe84f99e7cb2438c1262868af3e6dbc Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 15 Apr 2019 18:21:22 -0400 Subject: [PATCH] Reintroduce bert's fixups --- cli/worker.rs | 16 ++------ core/modules.rs | 97 +++++++++++++++++++++---------------------------- 2 files changed, 45 insertions(+), 68 deletions(-) diff --git a/cli/worker.rs b/cli/worker.rs index 51e1c37ca5c74a..31af67fdff44dc 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -126,18 +126,10 @@ impl Loader for Worker { ) } - fn use_isolate) -> R>( - &mut self, - mut cb: F, - ) -> R { - cb(&mut self.inner) - } - - fn use_modules R>( - &mut self, - mut cb: F, - ) -> R { - cb(&mut self.modules) + fn isolate_and_modules<'a: 'b + 'c, 'b, 'c>( + &'a mut self, + ) -> (&'b mut deno::Isolate, &'c mut deno::Modules) { + (&mut self.inner, &mut self.modules) } } diff --git a/core/modules.rs b/core/modules.rs index 49d2e6c98dac28..b268a73976f8e8 100644 --- a/core/modules.rs +++ b/core/modules.rs @@ -17,8 +17,6 @@ use std::collections::HashSet; use std::error::Error; use std::fmt; use std::marker::PhantomData; -use std::sync::Arc; -use std::sync::Mutex; pub type SourceCodeFuture = dyn Future + Send; @@ -35,12 +33,17 @@ pub trait Loader { /// Given an absolute url, load its source code. fn load(&mut self, url: &str) -> Box>; - fn use_isolate) -> R>( - &mut self, - cb: F, - ) -> R; + fn isolate_and_modules<'a: 'b + 'c, 'b, 'c>( + &'a mut self, + ) -> (&'b mut Isolate, &'c mut Modules); + + fn isolate<'a: 'b, 'b>(&'a mut self) -> &'b mut Isolate { + self.isolate_and_modules().0 + } - fn use_modules R>(&mut self, cb: F) -> R; + fn modules<'a: 'b, 'b>(&'a mut self) -> &'b mut Modules { + self.isolate_and_modules().1 + } } struct PendingLoad { @@ -91,7 +94,8 @@ impl RecursiveLoad { let is_root = if let Some(parent_id) = parent_id { let loader = self.loader.as_mut().unwrap(); - loader.use_modules(|modules| modules.add_child(parent_id, &url)); + let modules = loader.modules(); + modules.add_child(parent_id, &url); false } else { true @@ -156,9 +160,8 @@ impl Future for RecursiveLoad { let result = { let loader = self.loader.as_mut().unwrap(); - loader.use_isolate(|isolate: &mut Isolate| { - isolate.mod_new(completed.is_root, &completed.url, &source_code) - }) + let isolate = loader.isolate(); + isolate.mod_new(completed.is_root, &completed.url, &source_code) }; if let Err(err) = result { return Err((Either::JSError(err), self.take_loader())); @@ -173,15 +176,15 @@ impl Future for RecursiveLoad { { let loader = self.loader.as_mut().unwrap(); - loader.use_modules(|modules: &mut Modules| { - modules.register(mod_id, &completed.url) - }); + let modules = loader.modules(); + modules.register(mod_id, &completed.url); } // Now we must iterate over all imports of the module and load them. let imports = { let loader = self.loader.as_mut().unwrap(); - loader.use_isolate(|isolate| isolate.mod_get_imports(mod_id)) + let isolate = loader.isolate(); + isolate.mod_get_imports(mod_id) }; for specifier in imports { self @@ -196,40 +199,27 @@ impl Future for RecursiveLoad { return Ok(Async::NotReady); } - // The resolve_cb below needs to reference the loader and it may be called - // from multiple threads. Therefore we wrap the loader as an - // Arc> below. - let loader = Arc::new(Mutex::new(self.take_loader())); - let loader_ = loader.clone(); - - let mut resolve_cb = move |specifier: &str, - referrer_id: deno_mod| - -> deno_mod { - let mut l = loader_.lock().unwrap(); - let referrer = - l.use_modules(|modules| modules.get_name(referrer_id).unwrap().clone()); - match L::resolve(specifier, &referrer) { - Err(err) => { - eprintln!("potentially uncaught err {}", err.to_string()); - 0 - } - Ok(url) => l.use_modules(|modules| match modules.get_id(&url) { - Some(id) => id, - None => 0, - }), - } - }; - let root_id = self.root_id.unwrap().clone(); + let mut loader = self.take_loader(); + let (isolate, modules) = loader.isolate_and_modules(); + let result = { + let mut resolve_cb = + |specifier: &str, referrer_id: deno_mod| -> deno_mod { + let referrer = modules.get_name(referrer_id).unwrap(); + match L::resolve(specifier, &referrer) { + Err(err) => { + eprintln!("potentially uncaught err {}", err.to_string()); + 0 + } + Ok(url) => match modules.get_id(&url) { + Some(id) => id, + None => 0, + }, + } + }; - // Consume / unwrap Arc>. - let mut loader: L = Arc::try_unwrap(loader) - .map(|mutex| mutex.into_inner().unwrap()) - .ok() - .unwrap(); - - let result = loader - .use_isolate(|isolate| isolate.mod_instantiate(root_id, &mut resolve_cb)); + isolate.mod_instantiate(root_id, &mut resolve_cb) + }; match result { Err(err) => Err((Either::JSError(err), loader)), @@ -504,15 +494,10 @@ mod tests { Box::new(DelayedSourceCodeFuture { url, counter: 0 }) } - fn use_isolate) -> R>( - &mut self, - mut cb: F, - ) -> R { - cb(&mut self.isolate) - } - - fn use_modules R>(&mut self, mut cb: F) -> R { - cb(&mut self.modules) + fn isolate_and_modules<'a: 'b + 'c, 'b, 'c>( + &'a mut self, + ) -> (&'b mut Isolate, &'c mut Modules) { + (&mut self.isolate, &mut self.modules) } }