-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove dependency of isolate on modules
- Loading branch information
Showing
4 changed files
with
103 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// This is code shared between isolate and modules. | ||
// This is to keep with the goal that isolate should not depend on modules. | ||
// TODO(ry) rename this module to dyn_import.rs | ||
|
||
// Do not add any dependency to modules.rs! | ||
// modules.rs is complex and should remain decoupled from isolate.rs to keep the | ||
// Isolate struct from becoming too bloating for users who do not need | ||
// asynchronous module loading. | ||
|
||
use crate::any_error::ErrBox; | ||
use crate::isolate::Isolate; | ||
use crate::isolate::SourceCodeInfo; | ||
use crate::libdeno::deno_dyn_import_id; | ||
use crate::libdeno::deno_mod; | ||
use futures::stream::Stream; | ||
use futures::Async::*; | ||
use futures::Poll; | ||
use std::fmt; | ||
|
||
#[derive(Debug, Eq, PartialEq)] | ||
pub enum RecursiveLoadEvent { | ||
Fetch(SourceCodeInfo), | ||
Instantiate(deno_mod), | ||
} | ||
|
||
pub trait ImportStream: Stream { | ||
fn register( | ||
&mut self, | ||
source_code_info: SourceCodeInfo, | ||
isolate: &mut Isolate, | ||
) -> Result<(), ErrBox>; | ||
} | ||
|
||
pub type DynImportStream = | ||
Box<dyn ImportStream<Item = RecursiveLoadEvent, Error = ErrBox> + Send>; | ||
|
||
pub type DynImportFn = Fn(deno_dyn_import_id, &str, &str) -> DynImportStream; | ||
|
||
/// Wraps DynImportStream to include the deno_dyn_import_id, so that it doesn't | ||
/// need to be exposed. | ||
#[derive(Debug)] | ||
pub struct DynImport { | ||
pub id: deno_dyn_import_id, | ||
pub inner: DynImportStream, | ||
} | ||
|
||
impl fmt::Debug for DynImportStream { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "DynImportStream(..)") | ||
} | ||
} | ||
|
||
impl Stream for DynImport { | ||
type Item = (deno_dyn_import_id, RecursiveLoadEvent); | ||
type Error = (deno_dyn_import_id, ErrBox); | ||
|
||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { | ||
match self.inner.poll() { | ||
Ok(Ready(Some(event))) => Ok(Ready(Some((self.id, event)))), | ||
Ok(Ready(None)) => Ok(Ready(None)), // Should never happen. | ||
Err(e) => Err((self.id, e)), | ||
Ok(NotReady) => Ok(NotReady), | ||
} | ||
} | ||
} | ||
|
||
impl ImportStream for DynImport { | ||
fn register( | ||
&mut self, | ||
source_code_info: SourceCodeInfo, | ||
isolate: &mut Isolate, | ||
) -> Result<(), ErrBox> { | ||
self.inner.register(source_code_info, isolate) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters