1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
///
/// This example is meant to demonstrate the use of the import utility
///
/// It acts as a wrapper around a runtime with a single loaded module
/// and is meant to simplify usecases where multiple JS sources isn't
/// needed
///
use rustyscript::{js_value::Function, json_args, Error, Undefined};
fn main() -> Result<(), Error> {
let mut module = rustyscript::import("examples/javascript/example_module.js")?;
// We can list all of this module's exports
assert_eq!(
module.keys(),
vec!["MY_FAVOURITE_FOOD", "addBook", "listBooks"]
);
// Or ensure a given export is a function
assert!(module.is_callable("addBook"));
// We can grab constants
let value: String = module.get("MY_FAVOURITE_FOOD")?;
assert_eq!(value, "saskatoonberries");
// We can call functions - the Undefined here just means we don't care
// what type it returns
module.call::<Undefined>("addBook", json_args!("My Favorite Martian"))?;
module.call::<Undefined>(
"addBook",
json_args!("The Ultimate Saskatoon Berry Cookbook"),
)?;
// Functions can even be stored for later!
// They can only be used on the runtime that made them, however
let function: Function = module.get("listBooks")?;
// The stored function can then be called!
// Any serializable type can be retrieved as a function result or value
let books: Vec<String> = module.call_stored(&function, json_args!())?;
assert_eq!(
books,
vec![
"My Favorite Martian",
"The Ultimate Saskatoon Berry Cookbook"
]
);
Ok(())
}