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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! An advanced example showing how to communicate with an `Engine` running in a separate thread via
//! an MPSC channel.
use rhai::Engine;
#[cfg(feature = "sync")]
use std::sync::Mutex;
fn main() {
// Channel: Script -> Master
let (tx_script, rx_master) = std::sync::mpsc::channel();
// Channel: Master -> Script
let (tx_master, rx_script) = std::sync::mpsc::channel();
#[cfg(feature = "sync")]
let (tx_script, rx_script) = (Mutex::new(tx_script), Mutex::new(rx_script));
// Spawn thread with Engine
std::thread::spawn(move || {
// Create Engine
let mut engine = Engine::new();
// Register API
// Notice that the API functions are blocking
#[cfg(not(feature = "sync"))]
engine
.register_fn("get", move || rx_script.recv().unwrap_or_default())
.register_fn("put", move |v: i64| tx_script.send(v).unwrap());
#[cfg(feature = "sync")]
engine
.register_fn("get", move || rx_script.lock().unwrap().recv().unwrap())
.register_fn("put", move |v: i64| {
tx_script.lock().unwrap().send(v).unwrap()
});
// Run script
engine
.run(
r#"
print("Starting script loop...");
loop {
let x = get();
print(`Script Read: ${x}`);
x += 1;
print(`Script Write: ${x}`);
put(x);
}
"#,
)
.unwrap();
});
// This is the main processing thread
println!("Starting main loop...");
let mut value: i64 = 0;
while value < 10 {
println!("Value: {}", value);
// Send value to script
tx_master.send(value).unwrap();
// Receive value from script
value = rx_master.recv().unwrap();
}
}