Function gluon::import::add_extern_module
source · pub fn add_extern_module<F>(thread: &Thread, name: &str, loader: F)
Expand description
Adds an extern module to thread
, letting it be loaded with import! name
from gluon code.
use gluon::vm::{self, ExternModule};
use gluon::{primitive, record, Thread, ThreadExt};
use gluon::import::add_extern_module;
fn yell(s: &str) -> String {
s.to_uppercase()
}
fn my_module(thread: &Thread) -> vm::Result<ExternModule> {
ExternModule::new(
thread,
record!{
message => "Hello World!",
yell => primitive!(1, yell)
}
)
}
#[tokio::main]
async fn main() -> gluon::Result<()> {
let thread = gluon::new_vm_async().await;
add_extern_module(&thread, "my_module", my_module);
let script = r#"
let module = import! "my_module"
module.yell module.message
"#;
let (result, _) = thread.run_expr_async::<String>("example", script).await?;
assert_eq!(result, "HELLO WORLD!");
Ok(())
}