Merge pull request #261 from schungx/master

Fix bug with 0.19.1.
This commit is contained in:
Stephen Chung 2020-10-16 21:32:59 +08:00 committed by GitHub
commit bd90f3cc26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 51 additions and 57 deletions

View File

@ -6,7 +6,7 @@ members = [
[package] [package]
name = "rhai" name = "rhai"
version = "0.19.1" version = "0.19.2"
edition = "2018" edition = "2018"
authors = ["Jonathan Turner", "Lukáš Hozda", "Stephen Chung", "jhwgh1968"] authors = ["Jonathan Turner", "Lukáš Hozda", "Stephen Chung", "jhwgh1968"]
description = "Embedded scripting for Rust" description = "Embedded scripting for Rust"

View File

@ -13,7 +13,6 @@ Breaking changes
* `AST::iter_functions` now returns an iterator instead of taking a closure. * `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 `&<Shared<ScriptFnDef>>`. * `Module::get_script_function_by_signature` renamed to `Module::get_script_fn` and returns `&<Shared<ScriptFnDef>>`.
* `Module::num_fn`, `Module::num_var` and `Module::num_iter` are removed and merged into `Module::count`. * `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`. * 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. * `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` * The following `EvalAltResult` variants are removed and merged into `EvalAltResult::ErrorMismatchDataType`: `ErrorCharMismatch`, `ErrorNumericIndexExpr`, `ErrorStringIndexExpr`, `ErrorImportExpr`, `ErrorLogicGuard`, `ErrorBooleanArgMismatch`

View File

@ -1,6 +1,6 @@
[package] [package]
name = "rhai_codegen" name = "rhai_codegen"
version = "0.1.0" version = "0.1.1"
edition = "2018" edition = "2018"
authors = ["jhwgh1968"] authors = ["jhwgh1968"]
description = "Procedural macro support package for Rhai, a scripting language for Rust" description = "Procedural macro support package for Rhai, a scripting language for Rust"

View File

@ -1,4 +1,5 @@
Procedural Macros for Plugins 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).

View File

@ -1,5 +1,5 @@
{ {
"version": "0.19.1", "version": "0.19.2",
"repoHome": "https://github.com/jonathandturner/rhai/blob/master", "repoHome": "https://github.com/jonathandturner/rhai/blob/master",
"repoTree": "https://github.com/jonathandturner/rhai/tree/master", "repoTree": "https://github.com/jonathandturner/rhai/tree/master",
"rootUrl": "", "rootUrl": "",

View File

@ -54,7 +54,6 @@ The following methods (mostly defined in the [`BasicArrayPackage`][packages] but
| `reduce` | 1) [function pointer] to accumulator function (usually a [closure]),<br/>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:<br/>1st parameter: accumulated value ([`()`] initially),<br/>2nd parameter: array item,<br/>3rd parameter: _(optional)_ offset index | | `reduce` | 1) [function pointer] to accumulator function (usually a [closure]),<br/>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:<br/>1st parameter: accumulated value ([`()`] initially),<br/>2nd parameter: array item,<br/>3rd parameter: _(optional)_ offset index |
| `reduce_rev` | 1) [function pointer] to accumulator function (usually a [closure]),<br/>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:<br/>1st parameter: accumulated value ([`()`] initially),<br/>2nd parameter: array item,<br/>3rd parameter: _(optional)_ offset index | | `reduce_rev` | 1) [function pointer] to accumulator function (usually a [closure]),<br/>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:<br/>1st parameter: accumulated value ([`()`] initially),<br/>2nd parameter: array item,<br/>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:<br/>1st parameter: array item,<br/>2nd 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:<br/>1st parameter: array item,<br/>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:<br/>1st parameter: array item,<br/>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:<br/>1st parameter: array item,<br/>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:<br/>1st parameter: array item,<br/>2nd parameter: _(optional)_ offset index |
| `sort` | [function pointer] to a comparison function (usually a [closure]) | sorts the array with a comparison function:<br/>1st parameter: first item,<br/>2nd parameter: second item,<br/>return value: `INT` < 0 if first < second, > 0 if first > second, 0 if first == second | | `sort` | [function pointer] to a comparison function (usually a [closure]) | sorts the array with a comparison function:<br/>1st parameter: first item,<br/>2nd parameter: second item,<br/>return value: `INT` < 0 if first < second, > 0 if first > second, 0 if first == second |

View File

@ -1652,9 +1652,7 @@ impl Engine {
ensure_no_data_race(name, args, false)?; ensure_no_data_race(name, args, false)?;
} }
self.call_script_fn( self.call_script_fn(scope, &mut mods, &mut state, lib, this_ptr, fn_def, args, 0)
scope, &mut mods, &mut state, lib, this_ptr, name, fn_def, args, 0,
)
} }
/// Optimize the `AST` with constants defined in an external Scope. /// Optimize the `AST` with constants defined in an external Scope.

View File

@ -347,7 +347,6 @@ impl Engine {
state: &mut State, state: &mut State,
lib: &Module, lib: &Module,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
fn_name: &str,
fn_def: &ScriptFnDef, fn_def: &ScriptFnDef,
args: &mut FnCallArgs, args: &mut FnCallArgs,
level: usize, level: usize,
@ -386,7 +385,7 @@ impl Engine {
let mut lib_merged; let mut lib_merged;
let unified_lib = if let Some(ref env_lib) = fn_def.lib { 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 // In the special case of the main script not defining any function
env_lib env_lib
} else { } else {
@ -408,13 +407,17 @@ impl Engine {
EvalAltResult::Return(x, _) => Ok(x), EvalAltResult::Return(x, _) => Ok(x),
EvalAltResult::ErrorInFunctionCall(name, err, _) => { EvalAltResult::ErrorInFunctionCall(name, err, _) => {
EvalAltResult::ErrorInFunctionCall( EvalAltResult::ErrorInFunctionCall(
format!("{} > {}", fn_name, name), format!("{} > {}", fn_def.name, name),
err, err,
Position::none(), Position::none(),
) )
.into() .into()
} }
_ => EvalAltResult::ErrorInFunctionCall(fn_name.to_string(), err, Position::none()) _ => EvalAltResult::ErrorInFunctionCall(
fn_def.name.to_string(),
err,
Position::none(),
)
.into(), .into(),
}); });
@ -594,7 +597,6 @@ impl Engine {
state, state,
lib, lib,
&mut Some(*first), &mut Some(*first),
fn_name,
func, func,
rest, rest,
_level, _level,
@ -605,9 +607,8 @@ impl Engine {
let mut backup: ArgBackup = Default::default(); let mut backup: ArgBackup = Default::default();
backup.change_first_arg_to_copy(is_ref, args); backup.change_first_arg_to_copy(is_ref, args);
let result = self.call_script_fn( let result = self
scope, mods, state, lib, &mut None, fn_name, func, args, _level, .call_script_fn(scope, mods, state, lib, &mut None, func, args, _level);
);
// Restore the original reference // Restore the original reference
backup.restore_first_arg(args); backup.restore_first_arg(args);
@ -1195,14 +1196,12 @@ impl Engine {
} }
let args = args.as_mut(); 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 new_scope = &mut Scope::new();
let mods = &mut Imports::new(); let mods = &mut Imports::new();
self.call_script_fn( self.call_script_fn(new_scope, mods, state, lib, &mut None, fn_def, args, level)
new_scope, mods, state, lib, &mut None, name, func, args, level,
)
} }
Some(f) if f.is_plugin_fn() => f.get_plugin_fn().call(args.as_mut()), Some(f) if f.is_plugin_fn() => f.get_plugin_fn().call(args.as_mut()),
Some(f) if f.is_native() => { Some(f) if f.is_native() => {

View File

@ -79,7 +79,6 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
lib.set_raw_fn("reduce_rev", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>(), TypeId::of::<FnPtr>()], reduce_rev_with_initial); lib.set_raw_fn("reduce_rev", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>(), TypeId::of::<FnPtr>()], reduce_rev_with_initial);
lib.set_raw_fn("some", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], some); lib.set_raw_fn("some", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], some);
lib.set_raw_fn("all", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], all); lib.set_raw_fn("all", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], all);
lib.set_raw_fn("none", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], none);
lib.set_raw_fn("sort", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], sort); lib.set_raw_fn("sort", &[TypeId::of::<Array>(), TypeId::of::<FnPtr>()], sort);
// Merge in the module at the end to override `+=` for arrays // Merge in the module at the end to override `+=` for arrays
@ -364,40 +363,6 @@ fn all(
Ok(true.into()) Ok(true.into())
} }
fn none(
engine: &Engine,
lib: &Module,
args: &mut [&mut Dynamic],
) -> Result<bool, Box<EvalAltResult>> {
let list = args[0].read_lock::<Array>().unwrap();
let filter = args[1].read_lock::<FnPtr>().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( fn reduce(
engine: &Engine, engine: &Engine,
lib: &Module, lib: &Module,

View File

@ -399,3 +399,36 @@ fn test_module_ast_namespace() -> Result<(), Box<EvalAltResult>> {
Ok(()) Ok(())
} }
#[cfg(not(feature = "no_function"))]
#[test]
fn test_module_ast_namespace2() -> Result<(), Box<EvalAltResult>> {
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(())
}