Merge pull request #338 from schungx/master

Fix bug and FuncArgs enhancement.
This commit is contained in:
Stephen Chung 2021-01-29 23:27:46 +08:00 committed by GitHub
commit 38266f3bd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 269 additions and 92 deletions

View File

@ -17,9 +17,10 @@ license = "MIT OR Apache-2.0"
include = [
"**/*.rs",
"scripts/*.rhai",
"**/*.md",
"Cargo.toml"
]
keywords = [ "scripting" ]
keywords = [ "scripting", "scripting-engine", "scripting language", "embedded" ]
categories = [ "no-std", "embedded", "wasm", "parser-implementations" ]
[dependencies]

View File

@ -8,17 +8,20 @@ This version streamlines compiling for WASM.
Rust compiler minimum version is raised to 1.49.
Breaking changes
----------------
* Rust compiler requirement raised to 1.49.
* `NativeCallContext::new` taker an additional parameter containing the name of the function called.
Bug fixes
---------
* Parameters passed to plugin module functions were sometimes erroneously consumed. This is now fixed.
* Fixes compilation errors in `metadata` feature build.
* Stacking `!` operators now work properly.
* Off-by-one error in `insert` method for arrays is fixed.
Breaking changes
----------------
* Rust compiler requirement raised to 1.49.
* `NativeCallContext::new` taker an additional parameter containing the name of the function called.
* `Engine::set_doc_comments` is renamed `Engine::enable_doc_comments`.
New features
------------

66
examples/threading.rs Normal file
View File

@ -0,0 +1,66 @@
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();
#[cfg(feature = "sync")]
let (tx_script, rx_script) = (
std::sync::Mutex::new(tx_script),
std::sync::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())
.register_fn("put", move |v: INT| tx_script.send(v).unwrap());
#[cfg(feature = "sync")]
engine
.register_fn("get", move || rx_script.lock().unwrap().recv().unwrap())
.register_fn("put", move |v: INT| {
tx_script.lock().unwrap().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();
}
}

View File

@ -1337,22 +1337,14 @@ impl Dynamic {
#[cfg(not(feature = "no_closure"))]
Union::Shared(cell, _) => {
#[cfg(not(feature = "sync"))]
{
let inner = cell.borrow();
match &inner.0 {
Union::Str(s, _) => Ok(s.clone()),
Union::FnPtr(f, _) => Ok(f.clone().take_data().0),
_ => Err((*inner).type_name()),
}
}
let data = cell.borrow();
#[cfg(feature = "sync")]
{
let inner = cell.read().unwrap();
match &inner.0 {
let data = cell.read().unwrap();
match &data.0 {
Union::Str(s, _) => Ok(s.clone()),
Union::FnPtr(f, _) => Ok(f.clone().take_data().0),
_ => Err((*inner).type_name()),
}
Union::FnPtr(f, _) => Ok(f.get_fn_name().clone()),
_ => Err((*data).type_name()),
}
}
_ => Err(self.type_name()),

View File

@ -545,32 +545,37 @@ impl State {
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Limits {
/// Maximum levels of call-stack to prevent infinite recursion.
/// Not available under `no_function`.
///
/// Set to zero to effectively disable function calls.
///
/// Not available under `no_function`.
#[cfg(not(feature = "no_function"))]
pub max_call_stack_depth: usize,
/// Maximum depth of statements/expressions at global level.
pub max_expr_depth: Option<NonZeroUsize>,
/// Maximum depth of statements/expressions in functions.
///
/// Not available under `no_function`.
#[cfg(not(feature = "no_function"))]
pub max_function_expr_depth: Option<NonZeroUsize>,
/// Maximum number of operations allowed to run.
pub max_operations: Option<NonZeroU64>,
/// Maximum number of [modules][Module] allowed to load.
/// Not available under `no_module`.
///
/// Set to zero to effectively disable loading any [module][Module].
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
pub max_modules: usize,
/// Maximum length of a [string][ImmutableString].
pub max_string_size: Option<NonZeroUsize>,
/// Maximum length of an [array][Array].
///
/// Not available under `no_index`.
#[cfg(not(feature = "no_index"))]
pub max_array_size: Option<NonZeroUsize>,
/// Maximum number of properties in an [object map][Map].
///
/// Not available under `no_object`.
#[cfg(not(feature = "no_object"))]
pub max_map_size: Option<NonZeroUsize>,

View File

@ -1650,7 +1650,8 @@ impl Engine {
name: &str,
args: impl crate::fn_args::FuncArgs,
) -> Result<T, Box<EvalAltResult>> {
let mut arg_values = args.into_vec();
let mut arg_values: crate::StaticVec<_> = Default::default();
args.parse(&mut arg_values);
let mut args: crate::StaticVec<_> = arg_values.as_mut().iter_mut().collect();
let result =

View File

@ -34,7 +34,7 @@ impl Engine {
}
/// Enable/disable doc-comments.
#[inline(always)]
pub fn set_doc_comments(&mut self, enable: bool) -> &mut Self {
pub fn enable_doc_comments(&mut self, enable: bool) -> &mut Self {
self.disable_doc_comments = !enable;
self
}

View File

@ -1,16 +1,63 @@
//! Helper module which defines [`FuncArgs`] to make function calling easier.
#![cfg(not(feature = "no_function"))]
#![allow(non_snake_case)]
use crate::dynamic::Variant;
use crate::stdlib::vec::Vec;
use crate::{Dynamic, StaticVec};
/// Trait that represents arguments to a function call.
/// Any data type that can be converted into a [`Vec`]`<`[`Dynamic`]`>` can be used
/// as arguments to a function call.
/// Trait that parses arguments to a function call.
///
/// Any data type can implement this trait in order to pass arguments to a function call.
pub trait FuncArgs {
/// Convert to a [`StaticVec`]`<`[`Dynamic`]`>` of the function call arguments.
fn into_vec(self) -> StaticVec<Dynamic>;
/// Parse function call arguments into a container.
///
/// # Example
///
/// ```
/// use rhai::{Engine, Dynamic, FuncArgs, Scope};
///
/// // A struct containing function arguments
/// struct Options {
/// pub foo: bool,
/// pub bar: String,
/// pub baz: i64,
/// }
///
/// impl FuncArgs for Options {
/// fn parse<C: Extend<Dynamic>>(self, container: &mut C) {
/// container.extend(std::iter::once(self.foo.into()));
/// container.extend(std::iter::once(self.bar.into()));
/// container.extend(std::iter::once(self.baz.into()));
/// }
/// }
///
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// let options = Options { foo: false, bar: "world".to_string(), baz: 42 };
///
/// let engine = Engine::new();
/// let mut scope = Scope::new();
///
/// let ast = engine.compile(r#"
/// fn hello(x, y, z) {
/// if x { "hello " + y } else { y + z }
/// }
/// "#)?;
///
/// let result: String = engine.call_fn(&mut scope, &ast, "hello", options)?;
///
/// assert_eq!(result, "world42");
/// # Ok(())
/// # }
/// ```
fn parse<T: Extend<Dynamic>>(self, container: &mut T);
}
impl<T: Variant + Clone> FuncArgs for Vec<T> {
fn parse<C: Extend<Dynamic>>(self, container: &mut C) {
container.extend(self.into_iter().map(Variant::into_dynamic));
}
}
/// Macro to implement [`FuncArgs`] for tuples of standard types (each can be
@ -19,14 +66,14 @@ macro_rules! impl_args {
($($p:ident),*) => {
impl<$($p: Variant + Clone),*> FuncArgs for ($($p,)*)
{
#[inline]
fn into_vec(self) -> StaticVec<Dynamic> {
#[inline(always)]
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) {
let ($($p,)*) = self;
let mut _v = StaticVec::new();
$(_v.push($p.into_dynamic());)*
_v
container.extend(_v.into_iter());
}
}

View File

@ -8,6 +8,8 @@ use crate::stdlib::{boxed::Box, string::ToString};
use crate::{Engine, EvalAltResult, ParseError, Scope, AST};
/// Trait to create a Rust closure from a script.
///
/// Not available under `no_function`.
pub trait Func<ARGS, RET> {
type Output;

View File

@ -146,6 +146,9 @@ pub use rhai_codegen::*;
#[cfg(not(feature = "no_function"))]
pub use fn_func::Func;
#[cfg(not(feature = "no_function"))]
pub use fn_args::FuncArgs;
/// Variable-sized array of [`Dynamic`] values.
///
/// Not available under `no_index`.
@ -163,7 +166,7 @@ pub use module::ModuleResolver;
/// Module containing all built-in _module resolvers_ available to Rhai.
#[cfg(not(feature = "no_module"))]
pub use crate::module::resolvers as module_resolvers;
pub use module::resolvers as module_resolvers;
/// _(SERDE)_ Serialization and deserialization support for [`serde`](https://crates.io/crates/serde).
/// Exported under the `serde` feature.

View File

@ -29,7 +29,7 @@ macro_rules! gen_array_functions {
pub fn insert(list: &mut Array, position: INT, item: $arg_type) {
if position <= 0 {
list.insert(0, Dynamic::from(item));
} else if (position as usize) >= list.len() - 1 {
} else if (position as usize) >= list.len() {
push(list, item);
} else {
list.insert(position as usize, Dynamic::from(item));

View File

@ -1338,7 +1338,7 @@ fn parse_unary(
Token::Bang => {
let pos = eat_token(input, Token::Bang);
let mut args = StaticVec::new();
let expr = parse_primary(input, state, lib, settings.level_up())?;
let expr = parse_unary(input, state, lib, settings.level_up())?;
args.push(expr);
let op = "!";
@ -2870,7 +2870,9 @@ fn parse_fn(
/// Creates a curried expression from a list of external variables
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_closure"))]
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() {
return fn_expr;
}
@ -2880,14 +2882,8 @@ fn make_curry_from_externals(fn_expr: Expr, externals: StaticVec<Ident>, pos: Po
args.push(fn_expr);
#[cfg(not(feature = "no_closure"))]
externals.iter().for_each(|x| {
args.push(Expr::Variable(Box::new((None, None, x.clone().into()))));
});
#[cfg(feature = "no_closure")]
externals.into_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()))));
});
let curry_func = crate::engine::KEYWORD_FN_PTR_CURRY;
@ -2904,23 +2900,14 @@ fn make_curry_from_externals(fn_expr: Expr, externals: StaticVec<Ident>, pos: Po
pos,
);
// If there are captured variables, convert the entire expression into a statement block,
// then insert the relevant `Share` statements.
#[cfg(not(feature = "no_closure"))]
{
// Statement block
// Convert the entire expression into a statement block, then insert the relevant
// [`Share`][Stmt::Share] statements.
let mut statements: StaticVec<_> = Default::default();
// Insert `Share` statements
statements.extend(externals.into_iter().map(|x| Stmt::Share(x)));
// Final expression
statements.extend(externals.into_iter().map(Stmt::Share));
statements.push(Stmt::Expr(expr));
Expr::Stmt(Box::new(statements), pos)
}
#[cfg(feature = "no_closure")]
return expr;
}
/// Parse an anonymous function definition.
#[cfg(not(feature = "no_function"))]
fn parse_anon_fn(
@ -3029,11 +3016,8 @@ fn parse_anon_fn(
let expr = Expr::FnPointer(fn_name, settings.pos);
let expr = if cfg!(not(feature = "no_closure")) {
make_curry_from_externals(expr, externals, settings.pos)
} else {
expr
};
#[cfg(not(feature = "no_closure"))]
let expr = make_curry_from_externals(expr, externals, settings.pos);
Ok((expr, script))
}

View File

@ -1,22 +1,6 @@
#![cfg(not(feature = "no_function"))]
use rhai::{Engine, EvalAltResult, FnPtr, Func, ParseErrorType, RegisterFn, Scope, INT};
use std::any::TypeId;
#[test]
fn test_fn() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
// Expect duplicated parameters error
assert_eq!(
*engine
.compile("fn hello(x, x) { x }")
.expect_err("should be error")
.0,
ParseErrorType::FnDuplicatedParam("hello".to_string(), "x".to_string())
);
Ok(())
}
use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Func, FuncArgs, RegisterFn, Scope, INT};
use std::{any::TypeId, iter::once};
#[test]
fn test_call_fn() -> Result<(), Box<EvalAltResult>> {
@ -69,6 +53,46 @@ fn test_call_fn() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
struct Options {
pub foo: bool,
pub bar: String,
pub baz: INT,
}
impl FuncArgs for Options {
fn parse<C: Extend<Dynamic>>(self, container: &mut C) {
container.extend(once(self.foo.into()));
container.extend(once(self.bar.into()));
container.extend(once(self.baz.into()));
}
}
#[test]
fn test_call_fn_args() -> Result<(), Box<EvalAltResult>> {
let options = Options {
foo: false,
bar: "world".to_string(),
baz: 42,
};
let engine = Engine::new();
let mut scope = Scope::new();
let ast = engine.compile(
r#"
fn hello(x, y, z) {
if x { "hello " + y } else { y + z }
}
"#,
)?;
let result: String = engine.call_fn(&mut scope, &ast, "hello", options)?;
assert_eq!(result, "world42");
Ok(())
}
#[test]
fn test_call_fn_private() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();

View File

@ -88,7 +88,7 @@ fn test_comments_doc() -> Result<(), Box<EvalAltResult>> {
)
.is_err());
engine.set_doc_comments(false);
engine.enable_doc_comments(false);
engine.compile(
r"

View File

@ -51,6 +51,22 @@ fn test_functions() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_functions_params() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
// Expect duplicated parameters error
assert_eq!(
*engine
.compile("fn hello(x, x) { x }")
.expect_err("should be error")
.0,
ParseErrorType::FnDuplicatedParam("hello".to_string(), "x".to_string())
);
Ok(())
}
#[cfg(not(feature = "no_function"))]
#[test]
fn test_functions_namespaces() -> Result<(), Box<EvalAltResult>> {

34
tests/native.rs Normal file
View File

@ -0,0 +1,34 @@
use rhai::{Dynamic, Engine, EvalAltResult, NativeCallContext, INT};
use std::any::TypeId;
#[test]
fn test_native_context() -> Result<(), Box<EvalAltResult>> {
fn add_double(
context: NativeCallContext,
args: &mut [&mut Dynamic],
) -> Result<Dynamic, Box<EvalAltResult>> {
let x = args[0].as_int().unwrap();
let y = args[1].as_int().unwrap();
Ok(format!("{}_{}", context.fn_name(), x + 2 * y).into())
}
let mut engine = Engine::new();
engine
.register_raw_fn(
"add_double",
&[TypeId::of::<INT>(), TypeId::of::<INT>()],
add_double,
)
.register_raw_fn(
"adbl",
&[TypeId::of::<INT>(), TypeId::of::<INT>()],
add_double,
);
assert_eq!(engine.eval::<String>("add_double(40, 1)")?, "add_double_42");
assert_eq!(engine.eval::<String>("adbl(40, 1)")?, "adbl_42");
Ok(())
}

View File

@ -12,8 +12,7 @@ fn test_not() -> Result<(), Box<EvalAltResult>> {
#[cfg(not(feature = "no_function"))]
assert_eq!(engine.eval::<bool>("fn not(x) { !x } not(false)")?, true);
// TODO - do we allow stacking unary operators directly? e.g '!!!!!!!true'
assert_eq!(engine.eval::<bool>("!(!(!(!(true))))")?, true);
assert_eq!(engine.eval::<bool>("!!!!true")?, true);
Ok(())
}