Add threading example.
This commit is contained in:
parent
ab317bec4e
commit
e902c74073
51
examples/threading.rs
Normal file
51
examples/threading.rs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
use rhai::{Engine, RegisterFn, INT};
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
// Spawn thread with Engine
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
// Create Engine
|
||||||
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
|
// Register API
|
||||||
|
// Notice that the API functions are blocking
|
||||||
|
engine
|
||||||
|
.register_fn("get", move || rx_script.recv().unwrap())
|
||||||
|
.register_fn("put", move |v: INT| tx_script.send(v).unwrap());
|
||||||
|
|
||||||
|
// Run script
|
||||||
|
engine
|
||||||
|
.consume(
|
||||||
|
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: INT = 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();
|
||||||
|
}
|
||||||
|
}
|
@ -19,7 +19,7 @@ macro_rules! impl_args {
|
|||||||
($($p:ident),*) => {
|
($($p:ident),*) => {
|
||||||
impl<$($p: Variant + Clone),*> FuncArgs for ($($p,)*)
|
impl<$($p: Variant + Clone),*> FuncArgs for ($($p,)*)
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline(always)]
|
||||||
fn into_vec(self) -> StaticVec<Dynamic> {
|
fn into_vec(self) -> StaticVec<Dynamic> {
|
||||||
let ($($p,)*) = self;
|
let ($($p,)*) = self;
|
||||||
|
|
||||||
|
@ -2870,7 +2870,9 @@ fn parse_fn(
|
|||||||
|
|
||||||
/// Creates a curried expression from a list of external variables
|
/// Creates a curried expression from a list of external variables
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
#[cfg(not(feature = "no_closure"))]
|
||||||
fn make_curry_from_externals(fn_expr: Expr, externals: StaticVec<Ident>, pos: Position) -> Expr {
|
fn make_curry_from_externals(fn_expr: Expr, externals: StaticVec<Ident>, pos: Position) -> Expr {
|
||||||
|
// If there are no captured variables, no need to curry
|
||||||
if externals.is_empty() {
|
if externals.is_empty() {
|
||||||
return fn_expr;
|
return fn_expr;
|
||||||
}
|
}
|
||||||
@ -2880,14 +2882,8 @@ fn make_curry_from_externals(fn_expr: Expr, externals: StaticVec<Ident>, pos: Po
|
|||||||
|
|
||||||
args.push(fn_expr);
|
args.push(fn_expr);
|
||||||
|
|
||||||
#[cfg(not(feature = "no_closure"))]
|
|
||||||
externals.iter().for_each(|x| {
|
externals.iter().for_each(|x| {
|
||||||
args.push(Expr::Variable(Box::new((None, None, x.clone().into()))));
|
args.push(Expr::Variable(Box::new((None, None, x.clone()))));
|
||||||
});
|
|
||||||
|
|
||||||
#[cfg(feature = "no_closure")]
|
|
||||||
externals.into_iter().for_each(|x| {
|
|
||||||
args.push(Expr::Variable(Box::new((None, None, x.clone().into()))));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let curry_func = crate::engine::KEYWORD_FN_PTR_CURRY;
|
let curry_func = crate::engine::KEYWORD_FN_PTR_CURRY;
|
||||||
@ -2904,21 +2900,12 @@ fn make_curry_from_externals(fn_expr: Expr, externals: StaticVec<Ident>, pos: Po
|
|||||||
pos,
|
pos,
|
||||||
);
|
);
|
||||||
|
|
||||||
// If there are captured variables, convert the entire expression into a statement block,
|
// Convert the entire expression into a statement block, then insert the relevant
|
||||||
// then insert the relevant `Share` statements.
|
// [`Share`][Stmt::Share] statements.
|
||||||
#[cfg(not(feature = "no_closure"))]
|
|
||||||
{
|
|
||||||
// Statement block
|
|
||||||
let mut statements: StaticVec<_> = Default::default();
|
let mut statements: StaticVec<_> = Default::default();
|
||||||
// Insert `Share` statements
|
statements.extend(externals.into_iter().map(Stmt::Share));
|
||||||
statements.extend(externals.into_iter().map(|x| Stmt::Share(x)));
|
|
||||||
// Final expression
|
|
||||||
statements.push(Stmt::Expr(expr));
|
statements.push(Stmt::Expr(expr));
|
||||||
Expr::Stmt(Box::new(statements), pos)
|
Expr::Stmt(Box::new(statements), pos)
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "no_closure")]
|
|
||||||
return expr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an anonymous function definition.
|
/// Parse an anonymous function definition.
|
||||||
@ -3029,11 +3016,8 @@ fn parse_anon_fn(
|
|||||||
|
|
||||||
let expr = Expr::FnPointer(fn_name, settings.pos);
|
let expr = Expr::FnPointer(fn_name, settings.pos);
|
||||||
|
|
||||||
let expr = if cfg!(not(feature = "no_closure")) {
|
#[cfg(not(feature = "no_closure"))]
|
||||||
make_curry_from_externals(expr, externals, settings.pos)
|
let expr = make_curry_from_externals(expr, externals, settings.pos);
|
||||||
} else {
|
|
||||||
expr
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok((expr, script))
|
Ok((expr, script))
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use rhai::{Dynamic, Engine, EvalAltResult, NativeCallContext, RegisterFn, INT};
|
use rhai::{Dynamic, Engine, EvalAltResult, NativeCallContext, INT};
|
||||||
use std::any::TypeId;
|
use std::any::TypeId;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user