Skip to content

Commit

Permalink
feat: Print a hint/warning towards necessary feature gates when impor…
Browse files Browse the repository at this point in the history
…ting modules
  • Loading branch information
Laegluin committed Sep 2, 2018
1 parent e1f7ddf commit 9b96e51
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
22 changes: 22 additions & 0 deletions src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,28 @@ pub fn add_extern_module(thread: &Thread, name: &str, loader: ExternLoader) {
import.add_loader(name, loader);
}

macro_rules! add_extern_module_if {
(
#[cfg($($features: tt)*)],
available_if = $msg: expr,
args($vm: expr, $mod_name: expr, $loader: path)
) => {{
#[cfg($($features)*)]
$crate::import::add_extern_module($vm, $mod_name, $loader);

#[cfg(not($($features)*))]
$crate::import::add_extern_module($vm, $mod_name, |_: &::vm::thread::Thread| -> ::vm::Result<::vm::ExternModule> {
Err(::vm::Error::Message(
format!(
"{} is only available if {}",
$mod_name,
$msg
)
))
});
}};
}

fn get_state<'m>(macros: &'m mut MacroExpander) -> &'m mut State {
macros
.state
Expand Down
35 changes: 17 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub extern crate gluon_parser as parser;
pub extern crate gluon_vm as vm;

pub mod compiler_pipeline;
#[macro_use]
pub mod import;
pub mod io;
#[cfg(all(feature = "rand", not(target_arch = "wasm32")))]
Expand Down Expand Up @@ -692,11 +693,23 @@ impl VmBuilder {
add_extern_module(&vm, "std.debug.prim", ::vm::debug::load);
add_extern_module(&vm, "std.io.prim", ::io::load);

#[cfg(feature = "serialization")]
add_extern_module(&vm, "std.json.prim", ::vm::api::json::load);
add_extern_module_if!(
#[cfg(feature = "serialization")],
available_if = "gluon is compiled with the 'serialization' feature",
args(&vm, "std.json.prim", ::vm::api::json::load)
);

add_extern_module_if!(
#[cfg(feature = "regex")],
available_if = "gluon is compiled with the 'regex' feature",
args(&vm, "std.regex", ::regex_bind::load)
);

load_regex(&vm);
load_random(&vm);
add_extern_module_if!(
#[cfg(all(feature = "rand", not(target_arch = "wasm32")))],
available_if = "gluon is compiled with the 'rand' feature and is not targeting WASM",
args(&vm, "std.random.prim", ::rand_bind::load)
);

vm
}
Expand All @@ -708,20 +721,6 @@ pub fn new_vm() -> RootedThread {
VmBuilder::default().build()
}

#[cfg(feature = "regex")]
fn load_regex(vm: &Thread) {
add_extern_module(&vm, "std.regex", ::regex_bind::load);
}
#[cfg(not(feature = "regex"))]
fn load_regex(_: &Thread) {}

#[cfg(all(feature = "rand", not(target_arch = "wasm32")))]
fn load_random(vm: &Thread) {
add_extern_module(&vm, "std.random.prim", ::rand_bind::load);
}
#[cfg(any(not(feature = "rand"), target_arch = "wasm32"))]
fn load_random(_: &Thread) {}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 9b96e51

Please sign in to comment.