Refine examples.

This commit is contained in:
Stephen Chung 2021-02-20 23:46:25 +08:00
parent 54e26059dc
commit 03c31a969a
7 changed files with 53 additions and 25 deletions

View File

@ -9,7 +9,6 @@ impl TestStruct {
pub fn update(&mut self) { pub fn update(&mut self) {
self.x += 1000; self.x += 1000;
} }
pub fn new() -> Self { pub fn new() -> Self {
Self { x: 1 } Self { x: 1 }
} }
@ -25,17 +24,30 @@ fn main() -> Result<(), Box<EvalAltResult>> {
.register_fn("new_ts", TestStruct::new) .register_fn("new_ts", TestStruct::new)
.register_fn("update", TestStruct::update); .register_fn("update", TestStruct::update);
println!( let result = engine.eval::<TestStruct>(
"{:?}", r"
engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")? let x = new_ts();
); x.update();
println!( x
"{:?}", ",
engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]")? )?;
);
println!("{:?}", result);
let result = engine.eval::<TestStruct>(
r"
let x = [ new_ts() ];
x[0].update();
x[0]
",
)?;
println!("{:?}", result);
Ok(()) Ok(())
} }
#[cfg(any(feature = "no_index", feature = "no_object"))] #[cfg(any(feature = "no_index", feature = "no_object"))]
fn main() {} fn main() {
panic!("This example does not run under 'no_index' or 'no_object'.")
}

View File

@ -24,7 +24,13 @@ fn main() -> Result<(), Box<EvalAltResult>> {
.register_fn("new_ts", TestStruct::new) .register_fn("new_ts", TestStruct::new)
.register_fn("update", TestStruct::update); .register_fn("update", TestStruct::update);
let result = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?; let result = engine.eval::<TestStruct>(
r"
let x = new_ts();
x.update();
x
",
)?;
println!("result: {}", result.x); // prints 1001 println!("result: {}", result.x); // prints 1001
@ -32,4 +38,6 @@ fn main() -> Result<(), Box<EvalAltResult>> {
} }
#[cfg(feature = "no_object")] #[cfg(feature = "no_object")]
fn main() {} fn main() {
panic!("This example does not run under 'no_object'.");
}

View File

@ -3,6 +3,8 @@ use rhai::{Engine, EvalAltResult, INT};
fn main() -> Result<(), Box<EvalAltResult>> { fn main() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new(); let engine = Engine::new();
engine.consume(r#"print("hello, world!")"#)?;
let result = engine.eval::<INT>("40 + 2")?; let result = engine.eval::<INT>("40 + 2")?;
println!("Answer: {}", result); // prints 42 println!("Answer: {}", result); // prints 42

View File

@ -6,9 +6,15 @@ fn main() -> Result<(), Box<EvalAltResult>> {
engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5")?; engine.eval_with_scope::<()>(&mut scope, "let x = 4 + 5")?;
let result = engine.eval_with_scope::<INT>(&mut scope, "x")?; println!("x = {}", scope.get_value::<INT>("x").unwrap());
println!("result: {}", result); for _ in 0..10 {
let result = engine.eval_with_scope::<INT>(&mut scope, "x += 1; x")?;
println!("result: {}", result);
}
println!("x = {}", scope.get_value::<INT>("x").unwrap());
Ok(()) Ok(())
} }

View File

@ -1,6 +1,6 @@
#[cfg(not(feature = "serde"))] #[cfg(not(feature = "serde"))]
fn main() { fn main() {
println!(r#"This example requires the "serde" feature which is not enabled by default."#); println!("This example requires the 'serde' feature to run.");
println!("Try: cargo run --features serde --example serde"); println!("Try: cargo run --features serde --example serde");
} }

View File

@ -33,12 +33,12 @@ fn main() -> Result<(), Box<EvalAltResult>> {
.register_fn("trim", trim_string) .register_fn("trim", trim_string)
.register_fn("len", count_string_bytes) .register_fn("len", count_string_bytes)
.register_fn("index_of", find_substring) .register_fn("index_of", find_substring)
.register_fn("display", |label: &str, x: INT| { // Register string functions using closures
// Register string functions using closures .register_fn("display", |label: &str, value: INT| {
println!("{}: {}", label, x) println!("{}: {}", label, value)
}) })
.register_fn("display", |label: ImmutableString, x: &str| { .register_fn("display", |label: ImmutableString, value: &str| {
println!(r#"{}: "{}""#, label, x) // Quote the input string println!(r#"{}: "{}""#, label, value) // Quote the input string
}); });
let mut scope = Scope::new(); let mut scope = Scope::new();

View File

@ -1,5 +1,8 @@
use rhai::{Engine, RegisterFn, INT}; use rhai::{Engine, RegisterFn, INT};
#[cfg(feature = "sync")]
use std::sync::Mutex;
fn main() { fn main() {
// Channel: Script -> Master // Channel: Script -> Master
let (tx_script, rx_master) = std::sync::mpsc::channel(); let (tx_script, rx_master) = std::sync::mpsc::channel();
@ -7,10 +10,7 @@ fn main() {
let (tx_master, rx_script) = std::sync::mpsc::channel(); let (tx_master, rx_script) = std::sync::mpsc::channel();
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
let (tx_script, rx_script) = ( let (tx_script, rx_script) = (Mutex::new(tx_script), Mutex::new(rx_script));
std::sync::Mutex::new(tx_script),
std::sync::Mutex::new(rx_script),
);
// Spawn thread with Engine // Spawn thread with Engine
std::thread::spawn(move || { std::thread::spawn(move || {
@ -22,7 +22,7 @@ fn main() {
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
engine engine
.register_fn("get", move || rx_script.recv().unwrap()) .register_fn("get", move || rx_script.recv().unwrap_or_default())
.register_fn("put", move |v: INT| tx_script.send(v).unwrap()); .register_fn("put", move |v: INT| tx_script.send(v).unwrap());
#[cfg(feature = "sync")] #[cfg(feature = "sync")]