Skip to content

Commit

Permalink
Merge #636
Browse files Browse the repository at this point in the history
636: feat(vm): Allow module loaders to contain state r=Marwes a=Marwes

Closes #634

Co-authored-by: Markus Westerlind <[email protected]>
  • Loading branch information
bors[bot] and Marwes committed Sep 22, 2018
2 parents 516bf1c + bee61dd commit 4c28179
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 19 additions & 11 deletions src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ use base::pos::{self, BytePos, Span};
use base::symbol::Symbol;
use base::types::ArcType;

use vm::macros::{Error as MacroError, Macro, MacroExpander, MacroFuture};
use vm::thread::{Thread, ThreadInternal};
use vm::{ExternLoader, ExternModule};
use vm::{
self,
macros::{Error as MacroError, Macro, MacroExpander, MacroFuture},
thread::{Thread, ThreadInternal},
ExternLoader, ExternModule,
};

use super::Compiler;

Expand Down Expand Up @@ -177,8 +180,8 @@ impl<I> Import<I> {
Some(tup) => UnloadedModule::Source(Cow::Borrowed(tup.1)),
None => {
{
let loaders = self.loaders.read().unwrap();
if let Some(loader) = loaders.get(module) {
let mut loaders = self.loaders.write().unwrap();
if let Some(loader) = loaders.get_mut(module) {
let value = loader(vm)?;
return Ok(UnloadedModule::Extern(value));
}
Expand All @@ -192,8 +195,7 @@ impl<I> Import<I> {
Ok(file) => Some(file),
Err(_) => None,
}
})
.next();
}).next();
let mut file = file.ok_or_else(|| {
Error::String(format!(
"Could not find module '{}'. Searched {}.",
Expand Down Expand Up @@ -392,7 +394,14 @@ impl<I> Import<I> {
/// }
/// }
/// ```
pub fn add_extern_module(thread: &Thread, name: &str, loader: ExternLoader) {
pub fn add_extern_module<F>(thread: &Thread, name: &str, loader: F)
where
F: FnMut(&Thread) -> vm::Result<ExternModule> + Send + Sync + 'static,
{
add_extern_module_(thread, name, Box::new(loader))
}

fn add_extern_module_(thread: &Thread, name: &str, loader: ExternLoader) {
let opt_macro = thread.get_macros().get("import");
let import = opt_macro
.as_ref()
Expand All @@ -408,7 +417,7 @@ pub fn add_extern_module(thread: &Thread, name: &str, loader: ExternLoader) {

macro_rules! add_extern_module_if {
(
#[cfg($($features: tt)*)],
#[cfg($($features: tt)*)],
available_if = $msg: expr,
args($vm: expr, $mod_name: expr, $loader: path)
) => {{
Expand Down Expand Up @@ -437,8 +446,7 @@ fn get_state<'m>(macros: &'m mut MacroExpander) -> &'m mut State {
visited: Vec::new(),
modules_with_errors: FnvMap::default(),
})
})
.downcast_mut::<State>()
}).downcast_mut::<State>()
.unwrap()
}

Expand Down
2 changes: 1 addition & 1 deletion vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl<'a> fmt::Display for Panic<'a> {
}
}

pub type ExternLoader = fn(&Thread) -> Result<ExternModule>;
pub type ExternLoader = Box<FnMut(&Thread) -> Result<ExternModule> + Send + Sync>;

pub struct ExternModule {
pub metadata: Metadata,
Expand Down

0 comments on commit 4c28179

Please sign in to comment.