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
///
/// This example shows a basic usage of the runtime
///
/// The call to Runtime::execute_module is a one liner which:
/// - Creates a new runtime with the given options
/// - Loads the modules passed in
/// - Calls the module's entrypoint function
/// - Either a default passed as an option
/// - Or the module's default export
/// - Or a call in JS to rustyscript.register_entrypoint
/// - Returns the result
///
/// Instead of just vec![], one could pass in other JS modules
/// which could be imported using `import './filename.js';`
///
use rustyscript::{json_args, Error, Module, Runtime};
fn main() -> Result<(), Error> {
let module = Module::new(
"test.js",
"
export default (string, integer) => {
console.log(`Hello world: string=${string}, integer=${integer}`);
return 4;
}
",
);
// Execute the module above as an ES module
// Do not side-load any additional modules
// Use the default Runtime options
// Pass 2 args into the entrypoint function
// And expect a usize back from it
let value: usize =
Runtime::execute_module(&module, vec![], Default::default(), json_args!("test", 5))?;
assert_eq!(value, 4);
Ok(())
}