diff --git a/Cargo.toml b/Cargo.toml index 7437b53e..e6a70183 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ members = [ [package] name = "rhai" -version = "0.19.1" +version = "0.19.2" edition = "2018" authors = ["Jonathan Turner", "Lukáš Hozda", "Stephen Chung", "jhwgh1968"] description = "Embedded scripting for Rust" diff --git a/RELEASES.md b/RELEASES.md index 63c761f4..19a0e65d 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -13,7 +13,6 @@ Breaking changes * `AST::iter_functions` now returns an iterator instead of taking a closure. * `Module::get_script_function_by_signature` renamed to `Module::get_script_fn` and returns `&>`. * `Module::num_fn`, `Module::num_var` and `Module::num_iter` are removed and merged into `Module::count`. -* `Module::set_iter` is renamed to `Module::set_iter_raw`. * The `merge_namespaces` parameter to `Module::eval_ast_as_new` is removed and now defaults to `true`. * `GlobalFileModuleResolver` is removed because its performance gain over the `FileModuleResolver` is no longer very significant. * The following `EvalAltResult` variants are removed and merged into `EvalAltResult::ErrorMismatchDataType`: `ErrorCharMismatch`, `ErrorNumericIndexExpr`, `ErrorStringIndexExpr`, `ErrorImportExpr`, `ErrorLogicGuard`, `ErrorBooleanArgMismatch` diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index 8a2d3456..c1abf4bf 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rhai_codegen" -version = "0.1.0" +version = "0.1.1" edition = "2018" authors = ["jhwgh1968"] description = "Procedural macro support package for Rhai, a scripting language for Rust" diff --git a/codegen/README.md b/codegen/README.md index 0625bff9..8f276264 100644 --- a/codegen/README.md +++ b/codegen/README.md @@ -1,4 +1,5 @@ Procedural Macros for Plugins ============================ -This crate holds procedural macros for code generation, supporting Rhai's plugins system. +This crate holds procedural macros for code generation, supporting the plugins system +for [Rhai](https://github.com/jonathandturner/rhai). diff --git a/doc/src/context.json b/doc/src/context.json index d451bd41..945d1b1c 100644 --- a/doc/src/context.json +++ b/doc/src/context.json @@ -1,5 +1,5 @@ { - "version": "0.19.1", + "version": "0.19.2", "repoHome": "https://github.com/jonathandturner/rhai/blob/master", "repoTree": "https://github.com/jonathandturner/rhai/tree/master", "rootUrl": "", diff --git a/doc/src/language/arrays.md b/doc/src/language/arrays.md index 7016a19c..d687e002 100644 --- a/doc/src/language/arrays.md +++ b/doc/src/language/arrays.md @@ -54,7 +54,6 @@ The following methods (mostly defined in the [`BasicArrayPackage`][packages] but | `reduce` | 1) [function pointer] to accumulator function (usually a [closure]),
2) _(optional)_ [function pointer] to function (usually a [closure]) that provides the initial value | reduces the array into a single value via the accumulator function:
1st parameter: accumulated value ([`()`] initially),
2nd parameter: array item,
3rd parameter: _(optional)_ offset index | | `reduce_rev` | 1) [function pointer] to accumulator function (usually a [closure]),
2) _(optional)_ [function pointer] to function (usually a [closure]) that provides the initial value | reduces the array (in reverse order) into a single value via the accumulator function:
1st parameter: accumulated value ([`()`] initially),
2nd parameter: array item,
3rd parameter: _(optional)_ offset index | | `some` | [function pointer] to predicate (usually a [closure]) | returns `true` if any item returns `true` when called with the predicate function:
1st parameter: array item,
2nd parameter: _(optional)_ offset index | -| `none` | [function pointer] to predicate (usually a [closure]) | returns `true` if no item returns `true` when called with the predicate function:
1st parameter: array item,
2nd parameter: _(optional)_ offset index | | `all` | [function pointer] to predicate (usually a [closure]) | returns `true` if all items return `true` when called with the predicate function:
1st parameter: array item,
2nd parameter: _(optional)_ offset index | | `sort` | [function pointer] to a comparison function (usually a [closure]) | sorts the array with a comparison function:
1st parameter: first item,
2nd parameter: second item,
return value: `INT` < 0 if first < second, > 0 if first > second, 0 if first == second | diff --git a/src/api.rs b/src/api.rs index 8018e447..573730e5 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1652,9 +1652,7 @@ impl Engine { ensure_no_data_race(name, args, false)?; } - self.call_script_fn( - scope, &mut mods, &mut state, lib, this_ptr, name, fn_def, args, 0, - ) + self.call_script_fn(scope, &mut mods, &mut state, lib, this_ptr, fn_def, args, 0) } /// Optimize the `AST` with constants defined in an external Scope. diff --git a/src/fn_call.rs b/src/fn_call.rs index 915985d4..e0cf13e0 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -347,7 +347,6 @@ impl Engine { state: &mut State, lib: &Module, this_ptr: &mut Option<&mut Dynamic>, - fn_name: &str, fn_def: &ScriptFnDef, args: &mut FnCallArgs, level: usize, @@ -386,7 +385,7 @@ impl Engine { let mut lib_merged; let unified_lib = if let Some(ref env_lib) = fn_def.lib { - if !lib.is_empty() { + if lib.is_empty() { // In the special case of the main script not defining any function env_lib } else { @@ -408,14 +407,18 @@ impl Engine { EvalAltResult::Return(x, _) => Ok(x), EvalAltResult::ErrorInFunctionCall(name, err, _) => { EvalAltResult::ErrorInFunctionCall( - format!("{} > {}", fn_name, name), + format!("{} > {}", fn_def.name, name), err, Position::none(), ) .into() } - _ => EvalAltResult::ErrorInFunctionCall(fn_name.to_string(), err, Position::none()) - .into(), + _ => EvalAltResult::ErrorInFunctionCall( + fn_def.name.to_string(), + err, + Position::none(), + ) + .into(), }); // Remove all local variables @@ -594,7 +597,6 @@ impl Engine { state, lib, &mut Some(*first), - fn_name, func, rest, _level, @@ -605,9 +607,8 @@ impl Engine { let mut backup: ArgBackup = Default::default(); backup.change_first_arg_to_copy(is_ref, args); - let result = self.call_script_fn( - scope, mods, state, lib, &mut None, fn_name, func, args, _level, - ); + let result = self + .call_script_fn(scope, mods, state, lib, &mut None, func, args, _level); // Restore the original reference backup.restore_first_arg(args); @@ -1195,14 +1196,12 @@ impl Engine { } let args = args.as_mut(); - let func = f.get_fn_def(); + let fn_def = f.get_fn_def(); let new_scope = &mut Scope::new(); let mods = &mut Imports::new(); - self.call_script_fn( - new_scope, mods, state, lib, &mut None, name, func, args, level, - ) + self.call_script_fn(new_scope, mods, state, lib, &mut None, fn_def, args, level) } Some(f) if f.is_plugin_fn() => f.get_plugin_fn().call(args.as_mut()), Some(f) if f.is_native() => { diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index de5bdfcc..826a2196 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -79,7 +79,6 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { lib.set_raw_fn("reduce_rev", &[TypeId::of::(), TypeId::of::(), TypeId::of::()], reduce_rev_with_initial); lib.set_raw_fn("some", &[TypeId::of::(), TypeId::of::()], some); lib.set_raw_fn("all", &[TypeId::of::(), TypeId::of::()], all); - lib.set_raw_fn("none", &[TypeId::of::(), TypeId::of::()], none); lib.set_raw_fn("sort", &[TypeId::of::(), TypeId::of::()], sort); // Merge in the module at the end to override `+=` for arrays @@ -364,40 +363,6 @@ fn all( Ok(true.into()) } -fn none( - engine: &Engine, - lib: &Module, - args: &mut [&mut Dynamic], -) -> Result> { - let list = args[0].read_lock::().unwrap(); - let filter = args[1].read_lock::().unwrap(); - - for (i, item) in list.iter().enumerate() { - if filter - .call_dynamic(engine, lib, None, [item.clone()]) - .or_else(|err| match *err { - EvalAltResult::ErrorFunctionNotFound(_, _) => { - filter.call_dynamic(engine, lib, None, [item.clone(), (i as INT).into()]) - } - _ => Err(err), - }) - .map_err(|err| { - Box::new(EvalAltResult::ErrorInFunctionCall( - "filter".to_string(), - err, - Position::none(), - )) - })? - .as_bool() - .unwrap_or(false) - { - return Ok(false.into()); - } - } - - Ok(true.into()) -} - fn reduce( engine: &Engine, lib: &Module, diff --git a/tests/modules.rs b/tests/modules.rs index d2ee5e1c..e43b8e71 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -399,3 +399,36 @@ fn test_module_ast_namespace() -> Result<(), Box> { Ok(()) } + +#[cfg(not(feature = "no_function"))] +#[test] +fn test_module_ast_namespace2() -> Result<(), Box> { + use rhai::{Engine, Module, Scope}; + + const MODULE_TEXT: &str = r#" + fn run_function(function) { + call(function) + } + "#; + + const SCRIPT: &str = r#" + import "test_module" as test; + + fn foo() { + print("foo"); + } + + test::run_function(Fn("foo")); + "#; + + let mut engine = Engine::new(); + let module_ast = engine.compile(MODULE_TEXT)?; + let module = Module::eval_ast_as_new(Scope::new(), &module_ast, &engine)?; + let mut static_modules = rhai::module_resolvers::StaticModuleResolver::new(); + static_modules.insert("test_module", module); + engine.set_module_resolver(Some(static_modules)); + + engine.consume(SCRIPT)?; + + Ok(()) +}