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) {
self.x += 1000;
}
pub fn new() -> Self {
Self { x: 1 }
}
@ -25,17 +24,30 @@ fn main() -> Result<(), Box<EvalAltResult>> {
.register_fn("new_ts", TestStruct::new)
.register_fn("update", TestStruct::update);
println!(
"{:?}",
engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?
);
println!(
"{:?}",
engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]")?
);
let result = engine.eval::<TestStruct>(
r"
let x = new_ts();
x.update();
x
",
)?;
println!("{:?}", result);
let result = engine.eval::<TestStruct>(
r"
let x = [ new_ts() ];
x[0].update();
x[0]
",
)?;
println!("{:?}", result);
Ok(())
}
#[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("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
@ -32,4 +38,6 @@ fn main() -> Result<(), Box<EvalAltResult>> {
}
#[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>> {
let engine = Engine::new();
engine.consume(r#"print("hello, world!")"#)?;
let result = engine.eval::<INT>("40 + 2")?;
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")?;
let result = engine.eval_with_scope::<INT>(&mut scope, "x")?;
println!("x = {}", scope.get_value::<INT>("x").unwrap());
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(())
}

View File

@ -1,6 +1,6 @@
#[cfg(not(feature = "serde"))]
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");
}

View File

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

View File

@ -1,5 +1,8 @@
use rhai::{Engine, RegisterFn, INT};
#[cfg(feature = "sync")]
use std::sync::Mutex;
fn main() {
// Channel: Script -> Master
let (tx_script, rx_master) = std::sync::mpsc::channel();
@ -7,10 +10,7 @@ fn main() {
let (tx_master, rx_script) = std::sync::mpsc::channel();
#[cfg(feature = "sync")]
let (tx_script, rx_script) = (
std::sync::Mutex::new(tx_script),
std::sync::Mutex::new(rx_script),
);
let (tx_script, rx_script) = (Mutex::new(tx_script), Mutex::new(rx_script));
// Spawn thread with Engine
std::thread::spawn(move || {
@ -22,7 +22,7 @@ fn main() {
#[cfg(not(feature = "sync"))]
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());
#[cfg(feature = "sync")]