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
51
52
53
54
55
use rustyscript::{json_args, module, Error, Module, Runtime, RuntimeOptions, Undefined};
// This time we will embed this module into the executable directly.
// After all, it is a very small file we will always need - why
// take the extra overhead for the filesystem!
const API_MODULE: Module = module!(
"examples/get_value.ts",
"
let my_internal_value:number;
export function getValue():number {
return my_internal_value;
}
export function setValue(value:number) {
my_internal_value = value * 2;
}
"
);
fn main() -> Result<(), Error> {
// First we need a runtime. There are a handful of options available
// here but the one we need right now is default_entrypoint.
// This tells the runtime that a function is needed for initial
// setup of our runtime.
let mut runtime = Runtime::new(RuntimeOptions {
default_entrypoint: Some("setValue".to_string()),
..Default::default()
})?;
// Now we can include our static module - `to_module()` is needed
// to pull into into a form we can use.
//
// The `call_entrypoint` function will call our module's setValue
// function for us - the function was found and a reference to it
// stored in advance on load so that this function call can be
// made with less overhead.
// Just like before, `::<Undefined` means we do not care if the
// function returns a result.
let module_handle = runtime.load_module(&API_MODULE)?;
runtime.call_entrypoint::<Undefined>(&module_handle, json_args!(2))?;
// Now we can load our new module from the filesystem
// The handle that load_module returns is used to give context to future calls
let use_value_handle =
runtime.load_module(&Module::load("examples/javascript/multiple_modules.js")?)?;
// We use the returned handle to extract the const that it exports!
// We tell the compiler we'd like it as a string, and give the name of the value
// We'd like to retrieve!
let final_value: String = runtime.get_value(Some(&use_value_handle), "final_value")?;
println!("The received value was {final_value}");
Ok(())
}