[oden-js] Get module exports

This commit is contained in:
John Doty 2023-06-19 13:24:35 -07:00
parent 0c8367d29f
commit 16e6f1304c
4 changed files with 56 additions and 2 deletions

View file

@ -106,6 +106,14 @@ impl ContextRef {
unsafe { sys::JS_EnableBignumExt(self.ctx, if enable { 1 } else { 0 }) }
}
pub fn load_module(&self, input: &str, filename: &str) -> Result<Value> {
let val = self.eval(input, filename, EvalType::Module, EvalFlags::COMPILE_ONLY)?;
assert!(val.is_module());
val.eval_function(self)?;
Ok(val)
}
/// Evaluate the specified JavaScript code.
pub fn eval(
&self,
@ -482,4 +490,23 @@ mod tests {
.unwrap();
assert_eq!(String::from("UNSAFE"), result.to_string(&ctx).unwrap());
}
#[test]
fn modules_with_exports() {
let ctx = Context::new(Runtime::new());
let module = ctx
.load_module("const foo = 123; export { foo };", "main.js")
.expect("Could not load!");
assert_eq!(module.value_type(), ValueType::Module);
let foo = module
.get_module_export(&ctx, "foo")
.expect("Could not get export");
assert_eq!(String::from("123"), foo.to_string(&ctx).unwrap());
let bar = module
.get_module_export(&ctx, "bar")
.expect("Could not get export");
assert!(bar.is_undefined());
}
}