Merge remote-tracking branch 'schungx/closures' into closures

This commit is contained in:
Ilya Lakhin 2020-07-31 13:05:16 +07:00
commit 89f75bbf0c
22 changed files with 665 additions and 322 deletions

View File

@ -9,6 +9,8 @@ This version adds:
* Binding the `this` pointer in a function pointer `call`. * Binding the `this` pointer in a function pointer `call`.
* Anonymous functions (in Rust closure syntax). Simplifies creation of single-use ad-hoc functions. * Anonymous functions (in Rust closure syntax). Simplifies creation of single-use ad-hoc functions.
* Currying of function pointers. * Currying of function pointers.
* Auto-currying of anonymous functions.
* Capturing call scope via `func!(...)` syntax.
New features New features
------------ ------------
@ -19,6 +21,8 @@ New features
* Anonymous functions are supported in the syntax of a Rust closure, e.g. `|x, y, z| x + y - z`. * Anonymous functions are supported in the syntax of a Rust closure, e.g. `|x, y, z| x + y - z`.
* Custom syntax now works even without the `internals` feature. * Custom syntax now works even without the `internals` feature.
* Currying of function pointers is supported via the new `curry` keyword. * Currying of function pointers is supported via the new `curry` keyword.
* Automatic currying of anonymous functions to capture environment variables.
* Capturing of the calling scope for function call via the `func!(...)` syntax.
* `Module::set_indexer_get_set_fn` is added as a shorthand of both `Module::set_indexer_get_fn` and `Module::set_indexer_set_fn`. * `Module::set_indexer_get_set_fn` is added as a shorthand of both `Module::set_indexer_get_fn` and `Module::set_indexer_set_fn`.
* New `unicode-xid-ident` feature to allow [Unicode Standard Annex #31](http://www.unicode.org/reports/tr31/) for identifiers. * New `unicode-xid-ident` feature to allow [Unicode Standard Annex #31](http://www.unicode.org/reports/tr31/) for identifiers.

View File

@ -100,21 +100,22 @@ The Rhai Scripting Language
8. [Maximum Call Stack Depth](safety/max-call-stack.md) 8. [Maximum Call Stack Depth](safety/max-call-stack.md)
9. [Maximum Statement Depth](safety/max-stmt-depth.md) 9. [Maximum Statement Depth](safety/max-stmt-depth.md)
7. [Advanced Topics](advanced.md) 7. [Advanced Topics](advanced.md)
1. [Object-Oriented Programming (OOP)](language/oop.md) 1. [Capture Scope for Function Call](language/fn-capture.md)
2. [Serialization/Deserialization of `Dynamic` with `serde`](rust/serde.md) 2. [Object-Oriented Programming (OOP)](language/oop.md)
3. [Script Optimization](engine/optimize/index.md) 3. [Serialization/Deserialization of `Dynamic` with `serde`](rust/serde.md)
4. [Script Optimization](engine/optimize/index.md)
1. [Optimization Levels](engine/optimize/optimize-levels.md) 1. [Optimization Levels](engine/optimize/optimize-levels.md)
2. [Re-Optimize an AST](engine/optimize/reoptimize.md) 2. [Re-Optimize an AST](engine/optimize/reoptimize.md)
3. [Eager Function Evaluation](engine/optimize/eager.md) 3. [Eager Function Evaluation](engine/optimize/eager.md)
4. [Side-Effect Considerations](engine/optimize/side-effects.md) 4. [Side-Effect Considerations](engine/optimize/side-effects.md)
5. [Volatility Considerations](engine/optimize/volatility.md) 5. [Volatility Considerations](engine/optimize/volatility.md)
6. [Subtle Semantic Changes](engine/optimize/semantics.md) 6. [Subtle Semantic Changes](engine/optimize/semantics.md)
4. [Low-Level API](rust/register-raw.md) 5. [Low-Level API](rust/register-raw.md)
5. [Use as DSL](engine/dsl.md) 6. [Use as DSL](engine/dsl.md)
1. [Disable Keywords and/or Operators](engine/disable.md) 1. [Disable Keywords and/or Operators](engine/disable.md)
2. [Custom Operators](engine/custom-op.md) 2. [Custom Operators](engine/custom-op.md)
3. [Extending with Custom Syntax](engine/custom-syntax.md) 3. [Extending with Custom Syntax](engine/custom-syntax.md)
6. [Eval Statement](language/eval.md) 7. [Eval Statement](language/eval.md)
8. [Appendix](appendix/index.md) 8. [Appendix](appendix/index.md)
1. [Keywords](appendix/keywords.md) 1. [Keywords](appendix/keywords.md)
2. [Operators and Symbols](appendix/operators.md) 2. [Operators and Symbols](appendix/operators.md)

View File

@ -5,6 +5,8 @@ Advanced Topics
This section covers advanced features such as: This section covers advanced features such as:
* [Capture the calling scope]({{rootUrl}}/language/fn-capture.md) in a function call.
* Simulated [Object Oriented Programming (OOP)][OOP]. * Simulated [Object Oriented Programming (OOP)][OOP].
* [`serde`] integration. * [`serde`] integration.

View File

@ -54,7 +54,7 @@ WARNING - NOT Closures
---------------------- ----------------------
Remember: anonymous functions, though having the same syntax as Rust _closures_, are themselves Remember: anonymous functions, though having the same syntax as Rust _closures_, are themselves
**not** closures. In particular, they do not capture their running environment. They are more like **not** closures. In particular, they do not capture their execution environment. They are more like
Rust's function pointers. Rust's function pointers.
They do, however, _capture_ variable _values_ from their execution environment, unless the [`no_capture`] They do, however, _capture_ variable _values_ from their execution environment, unless the [`no_capture`]

View File

@ -0,0 +1,62 @@
Capture The Calling Scope for Function Call
==========================================
{{#include ../links.md}}
Peeking Out of The Pure Box
---------------------------
Rhai functions are _pure_, meaning that they depend on on their arguments and have no
access to the calling environment.
When a function accesses a variable that is not defined within that function's scope,
it raises an evaluation error.
It is possible, through a special syntax, to capture the calling scope - i.e. the scope
that makes the function call - and access variables defined there.
```rust
fn foo(y) { // function accesses 'x' and 'y', but 'x' is not defined
x += y; // 'x' is modified in this function
x
}
let x = 1;
foo(41); // error: variable 'x' not found
// Calling a function with a '!' causes it to capture the calling scope
foo!(41) == 42; // the function can access the value of 'x', but cannot change it
x == 1; // 'x' is still the original value
x.method!(); // <- syntax error: capturing is not allowed in method-call style
// Capturing also works for function pointers
let f = Fn("foo");
call!(f, 41) == 42; // must use function-call style
f.call!(41); // <- syntax error: capturing is not allowed in method-call style
```
No Mutations
------------
Variables in the calling scope are accessed as copies.
Changes to them do not reflect back to the calling scope.
Rhai functions remain _pure_ in the sense that they can never mutate their environment.
Caveat Emptor
-------------
Functions relying on the calling scope is a _Very Bad Idea™_ because it makes code almost impossible
to reason and maintain, as their behaviors are volatile and unpredictable.
This usage should be at the last resort.

View File

@ -1,6 +1,8 @@
Capture External Variables via Automatic Currying Capture External Variables via Automatic Currying
================================================ ================================================
{{#include ../links.md}}
Poor Man's Closures Poor Man's Closures
------------------- -------------------

View File

@ -33,7 +33,7 @@ curried.call(2) == 42; // <- de-sugars to 'func.call(21, 2)'
Automatic Currying Automatic Currying
------------------ ------------------
[Anonymous functions] defined via a closure syntax _capture_ external variables that are not shadowed inside [Anonymous functions] defined via a closure syntax _capture_ the _values_ of external variables
the function's scope. that are not shadowed inside the function's scope.
This is accomplished via [automatic currying]. This is accomplished via [automatic currying].

View File

@ -127,5 +127,5 @@ x.change(); // call 'change' in method-call style, 'this' binds to 'x'
x == 42; // 'x' is changed! x == 42; // 'x' is changed!
change(); // <- error: `this` is unbounded change(); // <- error: `this` is unbound
``` ```

View File

@ -23,7 +23,7 @@ more control over what a script can (or cannot) do.
| `no_object` | Disable support for [custom types] and [object maps]. | | `no_object` | Disable support for [custom types] and [object maps]. |
| `no_function` | Disable script-defined [functions]. | | `no_function` | Disable script-defined [functions]. |
| `no_module` | Disable loading external [modules]. | | `no_module` | Disable loading external [modules]. |
| `no_capture` | Disable capturing external variables in [anonymous functions]. | | `no_capture` | Disable [capturing][capture] external variables in [anonymous functions] and [capturing the calling scope]({{rootUrl}}/language/fn-capture.md) in function calls. |
| `no_std` | Build for `no-std`. Notice that additional dependencies will be pulled in to replace `std` features. | | `no_std` | Build for `no-std`. Notice that additional dependencies will be pulled in to replace `std` features. |
| `serde` | Enable serialization/deserialization via `serde`. Notice that the [`serde`](https://crates.io/crates/serde) crate will be pulled in together with its dependencies. | | `serde` | Enable serialization/deserialization via `serde`. Notice that the [`serde`](https://crates.io/crates/serde) crate will be pulled in together with its dependencies. |
| `internals` | Expose internal data structures (e.g. [`AST`] nodes). Beware that Rhai internals are volatile and may change from version to version. | | `internals` | Expose internal data structures (e.g. [`AST`] nodes). Beware that Rhai internals are volatile and may change from version to version. |

View File

@ -576,7 +576,7 @@ pub fn search_scope_only<'s, 'a>(
if let Some(val) = this_ptr { if let Some(val) = this_ptr {
return Ok(((*val).into(), KEYWORD_THIS, ScopeEntryType::Normal, *pos)); return Ok(((*val).into(), KEYWORD_THIS, ScopeEntryType::Normal, *pos));
} else { } else {
return Err(Box::new(EvalAltResult::ErrorUnboundedThis(*pos))); return Err(Box::new(EvalAltResult::ErrorUnboundThis(*pos)));
} }
} }
@ -737,16 +737,16 @@ impl Engine {
let args = &mut [target.as_mut(), &mut idx_val2, _new_val]; let args = &mut [target.as_mut(), &mut idx_val2, _new_val];
self.exec_fn_call( self.exec_fn_call(
state, lib, FN_IDX_SET, true, 0, args, is_ref, true, false, state, lib, FN_IDX_SET, 0, args, is_ref, true, false, None,
None, level, None, level,
) )
.or_else(|err| match *err { .or_else(|err| match *err {
// If there is no index setter, no need to set it back because the indexer is read-only // If there is no index setter, no need to set it back because the indexer is read-only
EvalAltResult::ErrorFunctionNotFound(_, _) => { EvalAltResult::ErrorFunctionNotFound(_, _) => {
Ok(Default::default()) Ok(Default::default())
} }
_ => Err(err), _ => Err(err),
})?; })?;
} }
// next step is custom index setter call in case of error // next step is custom index setter call in case of error
@ -754,7 +754,7 @@ impl Engine {
let args = &mut [target.as_mut(), &mut idx_val2, _new_val]; let args = &mut [target.as_mut(), &mut idx_val2, _new_val];
self.exec_fn_call( self.exec_fn_call(
state, lib, FN_IDX_SET, true, 0, args, is_ref, true, false, state, lib, FN_IDX_SET, 0, args, is_ref, true, false, None,
None, level, None, level,
)?; )?;
} }
@ -776,7 +776,7 @@ impl Engine {
match rhs { match rhs {
// xxx.fn_name(arg_expr_list) // xxx.fn_name(arg_expr_list)
Expr::FnCall(x) if x.1.is_none() => { Expr::FnCall(x) if x.1.is_none() => {
let ((name, native, pos), _, hash, _, def_val) = x.as_ref(); let ((name, native, _, pos), _, hash, _, def_val) = x.as_ref();
self.make_method_call( self.make_method_call(
state, lib, name, *hash, target, idx_val, *def_val, *native, false, state, lib, name, *hash, target, idx_val, *def_val, *native, false,
level, level,
@ -810,7 +810,7 @@ impl Engine {
let ((_, _, setter), pos) = x.as_ref(); let ((_, _, setter), pos) = x.as_ref();
let mut args = [target.as_mut(), _new_val.as_mut().unwrap()]; let mut args = [target.as_mut(), _new_val.as_mut().unwrap()];
self.exec_fn_call( self.exec_fn_call(
state, lib, setter, true, 0, &mut args, is_ref, true, false, None, state, lib, setter, 0, &mut args, is_ref, true, false, None, None,
level, level,
) )
.map(|(v, _)| (v, true)) .map(|(v, _)| (v, true))
@ -821,7 +821,7 @@ impl Engine {
let ((_, getter, _), pos) = x.as_ref(); let ((_, getter, _), pos) = x.as_ref();
let mut args = [target.as_mut()]; let mut args = [target.as_mut()];
self.exec_fn_call( self.exec_fn_call(
state, lib, getter, true, 0, &mut args, is_ref, true, false, None, state, lib, getter, 0, &mut args, is_ref, true, false, None, None,
level, level,
) )
.map(|(v, _)| (v, false)) .map(|(v, _)| (v, false))
@ -839,7 +839,7 @@ impl Engine {
} }
// {xxx:map}.fn_name(arg_expr_list)[expr] | {xxx:map}.fn_name(arg_expr_list).expr // {xxx:map}.fn_name(arg_expr_list)[expr] | {xxx:map}.fn_name(arg_expr_list).expr
Expr::FnCall(x) if x.1.is_none() => { Expr::FnCall(x) if x.1.is_none() => {
let ((name, native, pos), _, hash, _, def_val) = x.as_ref(); let ((name, native, _, pos), _, hash, _, def_val) = x.as_ref();
let (val, _) = self let (val, _) = self
.make_method_call( .make_method_call(
state, lib, name, *hash, target, idx_val, *def_val, state, lib, name, *hash, target, idx_val, *def_val,
@ -872,7 +872,7 @@ impl Engine {
let args = &mut arg_values[..1]; let args = &mut arg_values[..1];
let (mut val, updated) = self let (mut val, updated) = self
.exec_fn_call( .exec_fn_call(
state, lib, getter, true, 0, args, is_ref, true, false, state, lib, getter, 0, args, is_ref, true, false, None,
None, level, None, level,
) )
.map_err(|err| err.new_position(*pos))?; .map_err(|err| err.new_position(*pos))?;
@ -891,8 +891,8 @@ impl Engine {
// Re-use args because the first &mut parameter will not be consumed // Re-use args because the first &mut parameter will not be consumed
arg_values[1] = val; arg_values[1] = val;
self.exec_fn_call( self.exec_fn_call(
state, lib, setter, true, 0, arg_values, is_ref, true, state, lib, setter, 0, arg_values, is_ref, true, false,
false, None, level, None, None, level,
) )
.or_else( .or_else(
|err| match *err { |err| match *err {
@ -909,7 +909,7 @@ impl Engine {
} }
// xxx.fn_name(arg_expr_list)[expr] | xxx.fn_name(arg_expr_list).expr // xxx.fn_name(arg_expr_list)[expr] | xxx.fn_name(arg_expr_list).expr
Expr::FnCall(x) if x.1.is_none() => { Expr::FnCall(x) if x.1.is_none() => {
let ((name, native, pos), _, hash, _, def_val) = x.as_ref(); let ((name, native, _, pos), _, hash, _, def_val) = x.as_ref();
let (mut val, _) = self let (mut val, _) = self
.make_method_call( .make_method_call(
state, lib, name, *hash, target, idx_val, *def_val, state, lib, name, *hash, target, idx_val, *def_val,
@ -1148,7 +1148,7 @@ impl Engine {
.read_lock::<ImmutableString>() .read_lock::<ImmutableString>()
.ok_or_else(|| EvalAltResult::ErrorStringIndexExpr(idx_pos))?; .ok_or_else(|| EvalAltResult::ErrorStringIndexExpr(idx_pos))?;
map.get_mut(index.as_str()) map.get_mut(&*index)
.map(Target::from) .map(Target::from)
.unwrap_or_else(|| Target::from(())) .unwrap_or_else(|| Target::from(()))
}) })
@ -1181,7 +1181,7 @@ impl Engine {
let type_name = val.type_name(); let type_name = val.type_name();
let args = &mut [val, &mut _idx]; let args = &mut [val, &mut _idx];
self.exec_fn_call( self.exec_fn_call(
state, _lib, FN_IDX_GET, true, 0, args, is_ref, true, false, None, _level, state, _lib, FN_IDX_GET, 0, args, is_ref, true, false, None, None, _level,
) )
.map(|(v, _)| v.into()) .map(|(v, _)| v.into())
.map_err(|err| match *err { .map_err(|err| match *err {
@ -1222,26 +1222,23 @@ impl Engine {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Dynamic(Union::Array(mut rhs_value)) => { Dynamic(Union::Array(mut rhs_value)) => {
let op = "=="; let op = "==";
let mut scope = Scope::new();
// Call the `==` operator to compare each value // Call the `==` operator to compare each value
for value in rhs_value.iter_mut() { for value in rhs_value.iter_mut() {
let def_value = Some(false); let def_value = Some(false);
let args = &mut [&mut lhs_value.clone(), value]; let args = &mut [&mut lhs_value.clone(), value];
let hashes = ( // Qualifiers (none) + function name + number of arguments + argument `TypeId`'s.
// Qualifiers (none) + function name + number of arguments + argument `TypeId`'s. let hash =
calc_fn_hash(empty(), op, args.len(), args.iter().map(|a| a.type_id())), calc_fn_hash(empty(), op, args.len(), args.iter().map(|a| a.type_id()));
0,
);
let (r, _) = self if self
.call_fn_raw( .call_native_fn(state, lib, op, hash, args, false, false, def_value)
&mut scope, mods, state, lib, op, hashes, args, false, false, false, .map_err(|err| err.new_position(rhs.position()))?
def_value, level, .0
) .as_bool()
.map_err(|err| err.new_position(rhs.position()))?; .unwrap_or(false)
if r.as_bool().unwrap_or(false) { {
return Ok(true.into()); return Ok(true.into());
} }
} }
@ -1251,10 +1248,8 @@ impl Engine {
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Dynamic(Union::Map(rhs_value)) => match lhs_value { Dynamic(Union::Map(rhs_value)) => match lhs_value {
// Only allows String or char // Only allows String or char
Dynamic(Union::Str(s)) => Ok(rhs_value.contains_key(s.as_str()).into()), Dynamic(Union::Str(s)) => Ok(rhs_value.contains_key(&s).into()),
Dynamic(Union::Char(c)) => { Dynamic(Union::Char(c)) => Ok(rhs_value.contains_key(&c.to_string()).into()),
Ok(rhs_value.contains_key(c.to_string().as_str()).into())
}
_ => Err(Box::new(EvalAltResult::ErrorInExpr(lhs.position()))), _ => Err(Box::new(EvalAltResult::ErrorInExpr(lhs.position()))),
}, },
Dynamic(Union::Str(rhs_value)) => match lhs_value { Dynamic(Union::Str(rhs_value)) => match lhs_value {
@ -1294,7 +1289,7 @@ impl Engine {
if let Some(val) = this_ptr { if let Some(val) = this_ptr {
Ok(val.clone()) Ok(val.clone())
} else { } else {
Err(Box::new(EvalAltResult::ErrorUnboundedThis((x.0).1))) Err(Box::new(EvalAltResult::ErrorUnboundThis((x.0).1)))
} }
} }
Expr::Variable(_) => { Expr::Variable(_) => {
@ -1355,14 +1350,14 @@ impl Engine {
} else if run_builtin_op_assignment(op, lhs_ptr, &rhs_val)?.is_none() { } else if run_builtin_op_assignment(op, lhs_ptr, &rhs_val)?.is_none() {
// Not built in, map to `var = var op rhs` // Not built in, map to `var = var op rhs`
let op = &op[..op.len() - 1]; // extract operator without = let op = &op[..op.len() - 1]; // extract operator without =
let hash = calc_fn_hash(empty(), op, 2, empty());
// Clone the LHS value // Clone the LHS value
let args = &mut [&mut lhs_ptr.clone(), &mut rhs_val]; let args = &mut [&mut lhs_ptr.clone(), &mut rhs_val];
// Run function // Run function
let (value, _) = self let (value, _) = self
.exec_fn_call( .exec_fn_call(
state, lib, op, true, hash, args, false, false, false, None, state, lib, op, 0, args, false, false, false, None, None, level,
level,
) )
.map_err(|err| err.new_position(*op_pos))?; .map_err(|err| err.new_position(*op_pos))?;
if lhs_ptr.is_shared() { if lhs_ptr.is_shared() {
@ -1390,13 +1385,12 @@ impl Engine {
} else { } else {
// Op-assignment - always map to `lhs = lhs op rhs` // Op-assignment - always map to `lhs = lhs op rhs`
let op = &op[..op.len() - 1]; // extract operator without = let op = &op[..op.len() - 1]; // extract operator without =
let hash = calc_fn_hash(empty(), op, 2, empty());
let args = &mut [ let args = &mut [
&mut self.eval_expr(scope, mods, state, lib, this_ptr, lhs_expr, level)?, &mut self.eval_expr(scope, mods, state, lib, this_ptr, lhs_expr, level)?,
&mut rhs_val, &mut rhs_val,
]; ];
self.exec_fn_call( self.exec_fn_call(
state, lib, op, true, hash, args, false, false, false, None, level, state, lib, op, 0, args, false, false, false, None, None, level,
) )
.map(|(v, _)| v) .map(|(v, _)| v)
.map_err(|err| err.new_position(*op_pos))? .map_err(|err| err.new_position(*op_pos))?
@ -1466,20 +1460,20 @@ impl Engine {
// Normal function call // Normal function call
Expr::FnCall(x) if x.1.is_none() => { Expr::FnCall(x) if x.1.is_none() => {
let ((name, native, pos), _, hash, args_expr, def_val) = x.as_ref(); let ((name, native, capture, pos), _, hash, args_expr, def_val) = x.as_ref();
self.make_function_call( self.make_function_call(
scope, mods, state, lib, this_ptr, name, args_expr, *def_val, *hash, *native, scope, mods, state, lib, this_ptr, name, args_expr, *def_val, *hash, *native,
false, level, false, *capture, level,
) )
.map_err(|err| err.new_position(*pos)) .map_err(|err| err.new_position(*pos))
} }
// Module-qualified function call // Module-qualified function call
Expr::FnCall(x) if x.1.is_some() => { Expr::FnCall(x) if x.1.is_some() => {
let ((name, _, pos), modules, hash, args_expr, def_val) = x.as_ref(); let ((name, _, capture, pos), modules, hash, args_expr, def_val) = x.as_ref();
self.make_qualified_function_call( self.make_qualified_function_call(
scope, mods, state, lib, this_ptr, modules, name, args_expr, *def_val, *hash, scope, mods, state, lib, this_ptr, modules, name, args_expr, *def_val, *hash,
level, *capture, level,
) )
.map_err(|err| err.new_position(*pos)) .map_err(|err| err.new_position(*pos))
} }

View File

@ -91,6 +91,10 @@ pub enum ParseErrorType {
/// ///
/// Never appears under the `no_object` and `no_index` features combination. /// Never appears under the `no_object` and `no_index` features combination.
MalformedInExpr(String), MalformedInExpr(String),
/// A capturing has syntax error. Wrapped value is the error description (if any).
///
/// Never appears under the `no_capture` feature.
MalformedCapture(String),
/// A map definition has duplicated property names. Wrapped value is the property name. /// A map definition has duplicated property names. Wrapped value is the property name.
/// ///
/// Never appears under the `no_object` feature. /// Never appears under the `no_object` feature.
@ -166,6 +170,7 @@ impl ParseErrorType {
Self::MalformedCallExpr(_) => "Invalid expression in function call arguments", Self::MalformedCallExpr(_) => "Invalid expression in function call arguments",
Self::MalformedIndexExpr(_) => "Invalid index in indexing expression", Self::MalformedIndexExpr(_) => "Invalid index in indexing expression",
Self::MalformedInExpr(_) => "Invalid 'in' expression", Self::MalformedInExpr(_) => "Invalid 'in' expression",
Self::MalformedCapture(_) => "Invalid capturing",
Self::DuplicatedProperty(_) => "Duplicated property in object map literal", Self::DuplicatedProperty(_) => "Duplicated property in object map literal",
Self::ForbiddenConstantExpr(_) => "Expecting a constant", Self::ForbiddenConstantExpr(_) => "Expecting a constant",
Self::PropertyExpected => "Expecting name of a property", Self::PropertyExpected => "Expecting name of a property",
@ -199,9 +204,9 @@ impl fmt::Display for ParseErrorType {
} }
Self::UnknownOperator(s) => write!(f, "{}: '{}'", self.desc(), s), Self::UnknownOperator(s) => write!(f, "{}: '{}'", self.desc(), s),
Self::MalformedIndexExpr(s) => f.write_str(if s.is_empty() { self.desc() } else { s }), Self::MalformedIndexExpr(s) | Self::MalformedInExpr(s) | Self::MalformedCapture(s) => {
f.write_str(if s.is_empty() { self.desc() } else { s })
Self::MalformedInExpr(s) => f.write_str(if s.is_empty() { self.desc() } else { s }), }
Self::DuplicatedProperty(s) => { Self::DuplicatedProperty(s) => {
write!(f, "Duplicated property '{}' for object map literal", s) write!(f, "Duplicated property '{}' for object map literal", s)

View File

@ -20,8 +20,9 @@ use crate::stdlib::ops::Deref;
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
use crate::{ use crate::{
parser::ScriptFnDef, r#unsafe::unsafe_cast_var_name_to_lifetime, parser::ScriptFnDef,
scope::EntryType as ScopeEntryType, r#unsafe::unsafe_cast_var_name_to_lifetime,
scope::{Entry as ScopeEntry, EntryType as ScopeEntryType},
}; };
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
@ -36,11 +37,12 @@ use crate::engine::{Map, Target, FN_GET, FN_SET, KEYWORD_TAKE};
use crate::stdlib::{ use crate::stdlib::{
any::{type_name, TypeId}, any::{type_name, TypeId},
boxed::Box, boxed::Box,
collections::HashSet,
convert::TryFrom, convert::TryFrom,
format, format,
iter::{empty, once}, iter::{empty, once},
mem, mem,
string::ToString, string::{String, ToString},
vec::Vec, vec::Vec,
}; };
@ -66,48 +68,96 @@ fn extract_prop_from_setter(_fn_name: &str) -> Option<&str> {
None None
} }
/// This function replaces the first argument of a method call with a clone copy. /// A type that temporarily stores a mutable reference to a `Dynamic`,
/// This is to prevent a pure function unintentionally consuming the first argument. /// replacing it with a cloned copy.
fn normalize_first_arg<'a>( #[derive(Debug, Default)]
normalize: bool, struct ArgBackup<'a> {
this_copy: &mut Dynamic, orig_mut: Option<&'a mut Dynamic>,
old_this_ptr: &mut Option<&'a mut Dynamic>, value_copy: Dynamic,
args: &mut FnCallArgs<'a>,
) {
// Only do it for method calls with arguments.
if !normalize || args.is_empty() {
return;
}
// Clone the original value.
*this_copy = args[0].clone();
// Replace the first reference with a reference to the clone, force-casting the lifetime.
// Keep the original reference. Must remember to restore it later with `restore_first_arg_of_method_call`.
//
// # Safety
//
// Blindly casting a a reference to another lifetime saves on allocations and string cloning,
// but must be used with the utmost care.
//
// We can do this here because, at the end of this scope, we'd restore the original reference
// with `restore_first_arg_of_method_call`. Therefore this shorter lifetime does not get "out".
let this_pointer = mem::replace(args.get_mut(0).unwrap(), unsafe {
mem::transmute(this_copy)
});
*old_this_ptr = Some(this_pointer);
} }
/// This function restores the first argument that was replaced by `normalize_first_arg_of_method_call`. impl<'a> ArgBackup<'a> {
fn restore_first_arg<'a>(old_this_ptr: Option<&'a mut Dynamic>, args: &mut FnCallArgs<'a>) { /// This function replaces the first argument of a method call with a clone copy.
if let Some(this_pointer) = old_this_ptr { /// This is to prevent a pure function unintentionally consuming the first argument.
args[0] = this_pointer; ///
/// `restore_first_arg` must be called before the end of the scope to prevent the shorter lifetime from leaking.
///
/// # Safety
///
/// This method blindly casts a reference to another lifetime, which saves allocation and string cloning.
///
/// If `restore_first_arg` is called before the end of the scope, the shorter lifetime will not leak.
fn change_first_arg_to_copy(&mut self, normalize: bool, args: &mut FnCallArgs<'a>) {
// Only do it for method calls with arguments.
if !normalize || args.is_empty() {
return;
}
// Clone the original value.
self.value_copy = args[0].clone();
// Replace the first reference with a reference to the clone, force-casting the lifetime.
// Must remember to restore it later with `restore_first_arg`.
//
// # Safety
//
// Blindly casting a reference to another lifetime saves allocation and string cloning,
// but must be used with the utmost care.
//
// We can do this here because, before the end of this scope, we'd restore the original reference
// via `restore_first_arg`. Therefore this shorter lifetime does not leak.
self.orig_mut = Some(mem::replace(args.get_mut(0).unwrap(), unsafe {
mem::transmute(&mut self.value_copy)
}));
} }
/// This function restores the first argument that was replaced by `change_first_arg_to_copy`.
///
/// # Safety
///
/// If `change_first_arg_to_copy` has been called, this function **MUST** be called _BEFORE_ exiting
/// the current scope. Otherwise it is undefined behavior as the shorter lifetime will leak.
fn restore_first_arg(&mut self, args: &mut FnCallArgs<'a>) {
if let Some(this_pointer) = self.orig_mut.take() {
args[0] = this_pointer;
}
}
}
impl Drop for ArgBackup<'_> {
fn drop(&mut self) {
// Panic if the shorter lifetime leaks.
assert!(
self.orig_mut.is_none(),
"MutBackup::restore has not been called prior to existing this scope"
);
}
}
// Add captured variables into scope
#[cfg(not(feature = "no_capture"))]
fn add_captured_variables_into_scope<'s>(
externals: &HashSet<String>,
captured: Scope<'s>,
scope: &mut Scope<'s>,
) {
captured
.into_iter()
.filter(|ScopeEntry { name, .. }| externals.contains(name.as_ref()))
.for_each(
|ScopeEntry {
name, typ, value, ..
}| {
match typ {
ScopeEntryType::Normal => scope.push(name, value),
ScopeEntryType::Constant => scope.push_constant(name, value),
};
},
);
} }
impl Engine { impl Engine {
/// Universal method for calling functions either registered with the `Engine` or written in Rhai. /// Call a native Rust function registered with the `Engine`.
/// Position in `EvalAltResult` is `None` and must be set afterwards. /// Position in `EvalAltResult` is `None` and must be set afterwards.
/// ///
/// ## WARNING /// ## WARNING
@ -115,100 +165,41 @@ impl Engine {
/// Function call arguments be _consumed_ when the function requires them to be passed by value. /// Function call arguments be _consumed_ when the function requires them to be passed by value.
/// All function arguments not in the first position are always passed by value and thus consumed. /// All function arguments not in the first position are always passed by value and thus consumed.
/// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`! /// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`!
pub(crate) fn call_fn_raw( pub(crate) fn call_native_fn(
&self, &self,
_scope: &mut Scope,
_mods: &mut Imports,
state: &mut State, state: &mut State,
lib: &Module, lib: &Module,
fn_name: &str, fn_name: &str,
(hash_fn, hash_script): (u64, u64), hash_fn: u64,
args: &mut FnCallArgs, args: &mut FnCallArgs,
is_ref: bool, is_ref: bool,
_is_method: bool,
pub_only: bool, pub_only: bool,
def_val: Option<bool>, def_val: Option<bool>,
_level: usize,
) -> Result<(Dynamic, bool), Box<EvalAltResult>> { ) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
self.inc_operations(state)?; self.inc_operations(state)?;
let native_only = hash_script == 0; // Search for the native function
// First search registered functions (can override packages)
// Check for stack overflow
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "unchecked"))]
if _level > self.limits.max_call_stack_depth {
return Err(Box::new(
EvalAltResult::ErrorStackOverflow(Position::none()),
));
}
let mut this_copy: Dynamic = Default::default();
let mut old_this_ptr: Option<&mut Dynamic> = None;
// Search for the function
// First search in script-defined functions (can override built-in)
// Then search registered native functions (can override packages)
// Then search packages // Then search packages
// NOTE: We skip script functions for global_module and packages, and native functions for lib let func = self
let func = if !native_only { .global_module
lib.get_fn(hash_script, pub_only) //.or_else(|| lib.get_fn(hash_fn, pub_only)) .get_fn(hash_fn, pub_only)
} else { .or_else(|| self.packages.get_fn(hash_fn, pub_only));
None
}
//.or_else(|| self.global_module.get_fn(hash_script, pub_only))
.or_else(|| self.global_module.get_fn(hash_fn, pub_only))
//.or_else(|| self.packages.get_fn(hash_script, pub_only))
.or_else(|| self.packages.get_fn(hash_fn, pub_only));
if let Some(func) = func { if let Some(func) = func {
#[cfg(not(feature = "no_function"))] assert!(func.is_native());
let need_normalize = is_ref && (func.is_pure() || (func.is_script() && !_is_method));
#[cfg(feature = "no_function")]
let need_normalize = is_ref && func.is_pure();
// Calling pure function but the first argument is a reference? // Calling pure function but the first argument is a reference?
normalize_first_arg(need_normalize, &mut this_copy, &mut old_this_ptr, args); let mut backup: ArgBackup = Default::default();
backup.change_first_arg_to_copy(is_ref && func.is_pure(), args);
#[cfg(not(feature = "no_function"))]
if func.is_script() {
// Run scripted function
let fn_def = func.get_fn_def();
// Method call of script function - map first argument to `this`
return if _is_method {
let (first, rest) = args.split_at_mut(1);
Ok((
self.call_script_fn(
_scope,
_mods,
state,
lib,
&mut Some(first[0]),
fn_name,
fn_def,
rest,
_level,
)?,
false,
))
} else {
let result = self.call_script_fn(
_scope, _mods, state, lib, &mut None, fn_name, fn_def, args, _level,
)?;
// Restore the original reference
restore_first_arg(old_this_ptr, args);
Ok((result, false))
};
}
// Run external function // Run external function
let result = func.get_native_fn()(self, lib, args)?; let result = func.get_native_fn()(self, lib, args);
// Restore the original reference // Restore the original reference
restore_first_arg(old_this_ptr, args); backup.restore_first_arg(args);
let result = result?;
// See if the function match print/debug (which requires special processing) // See if the function match print/debug (which requires special processing)
return Ok(match fn_name { return Ok(match fn_name {
@ -340,6 +331,17 @@ impl Engine {
args: &mut FnCallArgs, args: &mut FnCallArgs,
level: usize, level: usize,
) -> Result<Dynamic, Box<EvalAltResult>> { ) -> Result<Dynamic, Box<EvalAltResult>> {
self.inc_operations(state)?;
// Check for stack overflow
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "unchecked"))]
if level > self.limits.max_call_stack_depth {
return Err(Box::new(
EvalAltResult::ErrorStackOverflow(Position::none()),
));
}
let orig_scope_level = state.scope_level; let orig_scope_level = state.scope_level;
state.scope_level += 1; state.scope_level += 1;
@ -402,7 +404,7 @@ impl Engine {
|| self.packages.contains_fn(hash_fn, pub_only) || self.packages.contains_fn(hash_fn, pub_only)
} }
/// Perform an actual function call, taking care of special functions /// Perform an actual function call, native Rust or scripted, taking care of special functions.
/// Position in `EvalAltResult` is `None` and must be set afterwards. /// Position in `EvalAltResult` is `None` and must be set afterwards.
/// ///
/// ## WARNING /// ## WARNING
@ -415,24 +417,23 @@ impl Engine {
state: &mut State, state: &mut State,
lib: &Module, lib: &Module,
fn_name: &str, fn_name: &str,
native_only: bool,
hash_script: u64, hash_script: u64,
args: &mut FnCallArgs, args: &mut FnCallArgs,
is_ref: bool, is_ref: bool,
is_method: bool, is_method: bool,
pub_only: bool, pub_only: bool,
capture: Option<Scope>,
def_val: Option<bool>, def_val: Option<bool>,
level: usize, level: usize,
) -> Result<(Dynamic, bool), Box<EvalAltResult>> { ) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
// Qualifiers (none) + function name + number of arguments + argument `TypeId`'s. // Qualifiers (none) + function name + number of arguments + argument `TypeId`'s.
let arg_types = args.iter().map(|a| a.type_id()); let arg_types = args.iter().map(|a| a.type_id());
let hash_fn = calc_fn_hash(empty(), fn_name, args.len(), arg_types); let hash_fn = calc_fn_hash(empty(), fn_name, args.len(), arg_types);
let hashes = (hash_fn, if native_only { 0 } else { hash_script });
match fn_name { match fn_name {
// type_of // type_of
KEYWORD_TYPE_OF KEYWORD_TYPE_OF
if args.len() == 1 && !self.has_override(lib, hashes.0, hashes.1, pub_only) => if args.len() == 1 && !self.has_override(lib, hash_fn, hash_script, pub_only) =>
{ {
Ok(( Ok((
self.map_type_name(args[0].type_name()).to_string().into(), self.map_type_name(args[0].type_name()).to_string().into(),
@ -442,7 +443,7 @@ impl Engine {
// Fn // Fn
KEYWORD_FN_PTR KEYWORD_FN_PTR
if args.len() == 1 && !self.has_override(lib, hashes.0, hashes.1, pub_only) => if args.len() == 1 && !self.has_override(lib, hash_fn, hash_script, pub_only) =>
{ {
Err(Box::new(EvalAltResult::ErrorRuntime( Err(Box::new(EvalAltResult::ErrorRuntime(
"'Fn' should not be called in method style. Try Fn(...);".into(), "'Fn' should not be called in method style. Try Fn(...);".into(),
@ -452,7 +453,7 @@ impl Engine {
// eval - reaching this point it must be a method-style call // eval - reaching this point it must be a method-style call
KEYWORD_EVAL KEYWORD_EVAL
if args.len() == 1 && !self.has_override(lib, hashes.0, hashes.1, pub_only) => if args.len() == 1 && !self.has_override(lib, hash_fn, hash_script, pub_only) =>
{ {
Err(Box::new(EvalAltResult::ErrorRuntime( Err(Box::new(EvalAltResult::ErrorRuntime(
"'eval' should not be called in method style. Try eval(...);".into(), "'eval' should not be called in method style. Try eval(...);".into(),
@ -460,15 +461,57 @@ impl Engine {
))) )))
} }
// Normal function call // Normal script function call
_ => { #[cfg(not(feature = "no_function"))]
let mut scope = Scope::new(); _ if hash_script > 0 && lib.contains_fn(hash_script, pub_only) => {
let mut mods = Imports::new(); // Get scripted function
self.call_fn_raw( let func = lib.get_fn(hash_script, pub_only).unwrap().get_fn_def();
&mut scope, &mut mods, state, lib, fn_name, hashes, args, is_ref, is_method,
pub_only, def_val, level, let scope = &mut Scope::new();
) let mods = &mut Imports::new();
// Add captured variables into scope
#[cfg(not(feature = "no_capture"))]
if let Some(captured) = capture {
add_captured_variables_into_scope(&func.externals, captured, scope);
}
let result = if is_method {
// Method call of script function - map first argument to `this`
let (first, rest) = args.split_at_mut(1);
self.call_script_fn(
scope,
mods,
state,
lib,
&mut Some(first[0]),
fn_name,
func,
rest,
level,
)?
} else {
// Normal call of script function - map first argument to `this`
// The first argument is a reference?
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,
);
// Restore the original reference
backup.restore_first_arg(args);
result?
};
Ok((result, false))
} }
// Normal native function call
_ => self.call_native_fn(
state, lib, fn_name, hash_fn, args, is_ref, pub_only, def_val,
),
} }
} }
@ -480,9 +523,21 @@ impl Engine {
mods: &mut Imports, mods: &mut Imports,
state: &mut State, state: &mut State,
lib: &Module, lib: &Module,
script: &Dynamic, script_expr: &Dynamic,
_level: usize,
) -> Result<Dynamic, Box<EvalAltResult>> { ) -> Result<Dynamic, Box<EvalAltResult>> {
let script = script.as_str().map_err(|typ| { self.inc_operations(state)?;
// Check for stack overflow
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "unchecked"))]
if _level > self.limits.max_call_stack_depth {
return Err(Box::new(
EvalAltResult::ErrorStackOverflow(Position::none()),
));
}
let script = script_expr.as_str().map_err(|typ| {
EvalAltResult::ErrorMismatchOutputType( EvalAltResult::ErrorMismatchOutputType(
self.map_type_name(type_name::<ImmutableString>()).into(), self.map_type_name(type_name::<ImmutableString>()).into(),
typ.into(), typ.into(),
@ -523,7 +578,7 @@ impl Engine {
state: &mut State, state: &mut State,
lib: &Module, lib: &Module,
name: &str, name: &str,
hash: u64, hash_script: u64,
target: &mut Target, target: &mut Target,
idx_val: Dynamic, idx_val: Dynamic,
def_val: Option<bool>, def_val: Option<bool>,
@ -546,7 +601,11 @@ impl Engine {
// Redirect function name // Redirect function name
let fn_name = fn_ptr.fn_name(); let fn_name = fn_ptr.fn_name();
// Recalculate hash // Recalculate hash
let hash = calc_fn_hash(empty(), fn_name, curry.len() + idx.len(), empty()); let hash = if native {
0
} else {
calc_fn_hash(empty(), fn_name, curry.len() + idx.len(), empty())
};
// Arguments are passed as-is, adding the curried arguments // Arguments are passed as-is, adding the curried arguments
let mut arg_values = curry let mut arg_values = curry
.iter_mut() .iter_mut()
@ -556,7 +615,7 @@ impl Engine {
// Map it to name(args) in function-call style // Map it to name(args) in function-call style
self.exec_fn_call( self.exec_fn_call(
state, lib, fn_name, native, hash, args, false, false, pub_only, def_val, level, state, lib, fn_name, hash, args, false, false, pub_only, None, def_val, level,
) )
} else if _fn_name == KEYWORD_FN_PTR_CALL && idx.len() > 0 && idx[0].is::<FnPtr>() { } else if _fn_name == KEYWORD_FN_PTR_CALL && idx.len() > 0 && idx[0].is::<FnPtr>() {
// FnPtr call on object // FnPtr call on object
@ -565,7 +624,11 @@ impl Engine {
// Redirect function name // Redirect function name
let fn_name = fn_ptr.get_fn_name().clone(); let fn_name = fn_ptr.get_fn_name().clone();
// Recalculate hash // Recalculate hash
let hash = calc_fn_hash(empty(), &fn_name, curry.len() + idx.len(), empty()); let hash = if native {
0
} else {
calc_fn_hash(empty(), &fn_name, curry.len() + idx.len(), empty())
};
// Replace the first argument with the object pointer, adding the curried arguments // Replace the first argument with the object pointer, adding the curried arguments
let mut arg_values = once(obj) let mut arg_values = once(obj)
.chain(curry.iter_mut()) .chain(curry.iter_mut())
@ -575,7 +638,7 @@ impl Engine {
// Map it to name(args) in function-call style // Map it to name(args) in function-call style
self.exec_fn_call( self.exec_fn_call(
state, lib, &fn_name, native, hash, args, is_ref, true, pub_only, def_val, level, state, lib, &fn_name, hash, args, is_ref, true, pub_only, None, def_val, level,
) )
} else if _fn_name == KEYWORD_FN_PTR_CURRY && obj.is::<FnPtr>() { } else if _fn_name == KEYWORD_FN_PTR_CURRY && obj.is::<FnPtr>() {
// Curry call // Curry call
@ -599,7 +662,7 @@ impl Engine {
} else { } else {
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
let redirected; let redirected;
let mut _hash = hash; let mut _hash = hash_script;
// Check if it is a map method call in OOP style // Check if it is a map method call in OOP style
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
@ -615,12 +678,16 @@ impl Engine {
} }
}; };
if native {
_hash = 0;
}
// Attached object pointer in front of the arguments // Attached object pointer in front of the arguments
let mut arg_values = once(obj).chain(idx.iter_mut()).collect::<StaticVec<_>>(); let mut arg_values = once(obj).chain(idx.iter_mut()).collect::<StaticVec<_>>();
let args = arg_values.as_mut(); let args = arg_values.as_mut();
self.exec_fn_call( self.exec_fn_call(
state, lib, _fn_name, native, _hash, args, is_ref, true, pub_only, def_val, level, state, lib, _fn_name, _hash, args, is_ref, true, pub_only, None, def_val, level,
) )
}?; }?;
@ -645,16 +712,17 @@ impl Engine {
name: &str, name: &str,
args_expr: &[Expr], args_expr: &[Expr],
def_val: Option<bool>, def_val: Option<bool>,
mut hash: u64, mut hash_script: u64,
native: bool, native: bool,
pub_only: bool, pub_only: bool,
capture: bool,
level: usize, level: usize,
) -> Result<Dynamic, Box<EvalAltResult>> { ) -> Result<Dynamic, Box<EvalAltResult>> {
// Handle Fn() // Handle Fn()
if name == KEYWORD_FN_PTR && args_expr.len() == 1 { if name == KEYWORD_FN_PTR && args_expr.len() == 1 {
let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::<ImmutableString>())); let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::<ImmutableString>()));
if !self.has_override(lib, hash_fn, hash, pub_only) { if !self.has_override(lib, hash_fn, hash_script, pub_only) {
// Fn - only in function call style // Fn - only in function call style
let expr = args_expr.get(0).unwrap(); let expr = args_expr.get(0).unwrap();
let arg_value = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?; let arg_value = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
@ -669,7 +737,8 @@ impl Engine {
)) ))
}) })
.and_then(|s| FnPtr::try_from(s)) .and_then(|s| FnPtr::try_from(s))
.map(Into::<Dynamic>::into); .map(Into::<Dynamic>::into)
.map_err(|err| err.new_position(expr.position()));
} }
} }
@ -709,29 +778,6 @@ impl Engine {
return Ok(value.into_shared()); return Ok(value.into_shared());
} }
// Handle eval()
if name == KEYWORD_EVAL && args_expr.len() == 1 {
let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::<ImmutableString>()));
if !self.has_override(lib, hash_fn, hash, pub_only) {
// eval - only in function call style
let prev_len = scope.len();
let expr = args_expr.get(0).unwrap();
let script = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
let result = self
.eval_script_expr(scope, mods, state, lib, &script)
.map_err(|err| err.new_position(expr.position()));
if scope.len() != prev_len {
// IMPORTANT! If the eval defines new variables in the current scope,
// all variable offsets from this point on will be mis-aligned.
state.always_search = true;
}
return result;
}
}
// Handle call() - Redirect function call // Handle call() - Redirect function call
let redirected; let redirected;
let mut args_expr = args_expr.as_ref(); let mut args_expr = args_expr.as_ref();
@ -740,7 +786,7 @@ impl Engine {
if name == KEYWORD_FN_PTR_CALL if name == KEYWORD_FN_PTR_CALL
&& args_expr.len() >= 1 && args_expr.len() >= 1
&& !self.has_override(lib, 0, hash, pub_only) && !self.has_override(lib, 0, hash_script, pub_only)
{ {
let expr = args_expr.get(0).unwrap(); let expr = args_expr.get(0).unwrap();
let fn_name = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?; let fn_name = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
@ -754,7 +800,7 @@ impl Engine {
// Skip the first argument // Skip the first argument
args_expr = &args_expr.as_ref()[1..]; args_expr = &args_expr.as_ref()[1..];
// Recalculate hash // Recalculate hash
hash = calc_fn_hash(empty(), name, curry.len() + args_expr.len(), empty()); hash_script = calc_fn_hash(empty(), name, curry.len() + args_expr.len(), empty());
} else { } else {
return Err(Box::new(EvalAltResult::ErrorMismatchOutputType( return Err(Box::new(EvalAltResult::ErrorMismatchOutputType(
self.map_type_name(type_name::<FnPtr>()).into(), self.map_type_name(type_name::<FnPtr>()).into(),
@ -764,10 +810,38 @@ impl Engine {
} }
} }
// Normal function call - except for Fn and eval (handled above) // Handle eval()
if name == KEYWORD_EVAL && args_expr.len() == 1 {
let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::<ImmutableString>()));
if !self.has_override(lib, hash_fn, hash_script, pub_only) {
// eval - only in function call style
let prev_len = scope.len();
let expr = args_expr.get(0).unwrap();
let script = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
let result = self
.eval_script_expr(scope, mods, state, lib, &script, level + 1)
.map_err(|err| err.new_position(expr.position()));
// IMPORTANT! If the eval defines new variables in the current scope,
// all variable offsets from this point on will be mis-aligned.
if scope.len() != prev_len {
state.always_search = true;
}
return result;
}
}
// Normal function call - except for Fn, curry, call and eval (handled above)
let mut arg_values: StaticVec<_>; let mut arg_values: StaticVec<_>;
let mut args: StaticVec<_>; let mut args: StaticVec<_>;
let mut is_ref = false; let mut is_ref = false;
let capture = if capture && !scope.is_empty() {
Some(scope.flatten_clone())
} else {
None
};
if args_expr.is_empty() && curry.is_empty() { if args_expr.is_empty() && curry.is_empty() {
// No arguments // No arguments
@ -808,9 +882,11 @@ impl Engine {
} }
} }
let hash = if native { 0 } else { hash_script };
let args = args.as_mut(); let args = args.as_mut();
self.exec_fn_call( self.exec_fn_call(
state, lib, name, native, hash, args, is_ref, false, pub_only, def_val, level, state, lib, name, hash, args, is_ref, false, pub_only, capture, def_val, level,
) )
.map(|(v, _)| v) .map(|(v, _)| v)
} }
@ -829,10 +905,18 @@ impl Engine {
args_expr: &[Expr], args_expr: &[Expr],
def_val: Option<bool>, def_val: Option<bool>,
hash_script: u64, hash_script: u64,
capture: bool,
level: usize, level: usize,
) -> Result<Dynamic, Box<EvalAltResult>> { ) -> Result<Dynamic, Box<EvalAltResult>> {
let modules = modules.as_ref().unwrap(); let modules = modules.as_ref().unwrap();
#[cfg(not(feature = "no_capture"))]
let capture = if capture && !scope.is_empty() {
Some(scope.flatten_clone())
} else {
None
};
let mut arg_values: StaticVec<_>; let mut arg_values: StaticVec<_>;
let mut args: StaticVec<_>; let mut args: StaticVec<_>;
@ -898,12 +982,18 @@ impl Engine {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
Some(f) if f.is_script() => { Some(f) if f.is_script() => {
let args = args.as_mut(); let args = args.as_mut();
let fn_def = f.get_fn_def(); let func = f.get_fn_def();
let mut scope = Scope::new();
let mut mods = Imports::new(); let scope = &mut Scope::new();
self.call_script_fn( let mods = &mut Imports::new();
&mut scope, &mut mods, state, lib, &mut None, name, fn_def, args, level,
) // Add captured variables into scope
#[cfg(not(feature = "no_capture"))]
if let Some(captured) = capture {
add_captured_variables_into_scope(&func.externals, captured, scope);
}
self.call_script_fn(scope, mods, state, lib, &mut None, name, func, args, level)
} }
Some(f) => f.get_native_fn()(self, lib, args.as_mut()), Some(f) => f.get_native_fn()(self, lib, args.as_mut()),
None if def_val.is_some() => Ok(def_val.unwrap().into()), None if def_val.is_some() => Ok(def_val.unwrap().into()),

View File

@ -137,13 +137,13 @@ impl FnPtr {
&mut Default::default(), &mut Default::default(),
lib.as_ref(), lib.as_ref(),
fn_name, fn_name,
false,
hash_script, hash_script,
args.as_mut(), args.as_mut(),
has_this, has_this,
has_this, has_this,
true, true,
None, None,
None,
0, 0,
) )
.map(|(v, _)| v) .map(|(v, _)| v)
@ -287,6 +287,16 @@ impl CallableFunction {
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => false, Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => false,
} }
} }
/// Is this a native Rust function?
pub fn is_native(&self) -> bool {
match self {
Self::Pure(_) | Self::Method(_) => true,
Self::Iterator(_) => true,
#[cfg(not(feature = "no_function"))]
Self::Script(_) => false,
}
}
/// Get the access mode. /// Get the access mode.
pub fn access(&self) -> FnAccess { pub fn access(&self) -> FnAccess {
match self { match self {

View File

@ -3,12 +3,12 @@
use crate::any::Dynamic; use crate::any::Dynamic;
use crate::calc_fn_hash; use crate::calc_fn_hash;
use crate::engine::{ use crate::engine::{
Engine, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_PRINT, KEYWORD_TYPE_OF, Engine, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_PRINT, KEYWORD_TYPE_OF,
}; };
use crate::fn_native::FnPtr;
use crate::module::Module; use crate::module::Module;
use crate::parser::{map_dynamic_to_expr, Expr, ScriptFnDef, Stmt, AST}; use crate::parser::{map_dynamic_to_expr, Expr, ScriptFnDef, Stmt, AST};
use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope};
use crate::token::is_valid_identifier;
use crate::utils::StaticVec; use crate::utils::StaticVec;
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
@ -19,6 +19,7 @@ use crate::parser::CustomExpr;
use crate::stdlib::{ use crate::stdlib::{
boxed::Box, boxed::Box,
convert::TryFrom,
iter::empty, iter::empty,
string::{String, ToString}, string::{String, ToString},
vec, vec,
@ -131,22 +132,18 @@ fn call_fn_with_constant_arguments(
state state
.engine .engine
.call_fn_raw( .call_native_fn(
&mut Scope::new(),
&mut Imports::new(),
&mut Default::default(), &mut Default::default(),
state.lib, state.lib,
fn_name, fn_name,
(hash_fn, 0), hash_fn,
arg_values.iter_mut().collect::<StaticVec<_>>().as_mut(), arg_values.iter_mut().collect::<StaticVec<_>>().as_mut(),
false, false,
false,
true, true,
None, None,
0,
) )
.map(|(v, _)| Some(v)) .ok()
.unwrap_or_else(|_| None) .map(|(v, _)| v)
} }
/// Optimize a statement. /// Optimize a statement.
@ -418,7 +415,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
// All other items can be thrown away. // All other items can be thrown away.
state.set_dirty(); state.set_dirty();
let pos = m.1; let pos = m.1;
m.0.into_iter().find(|((name, _), _)| name.as_str() == prop.as_str()) m.0.into_iter().find(|((name, _), _)| name == prop)
.map(|(_, mut expr)| { expr.set_position(pos); expr }) .map(|(_, mut expr)| { expr.set_position(pos); expr })
.unwrap_or_else(|| Expr::Unit(pos)) .unwrap_or_else(|| Expr::Unit(pos))
} }
@ -495,7 +492,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
state.set_dirty(); state.set_dirty();
let ch = a.0.to_string(); let ch = a.0.to_string();
if b.0.iter().find(|((name, _), _)| name.as_str() == ch.as_str()).is_some() { if b.0.iter().find(|((name, _), _)| name == &ch).is_some() {
Expr::True(a.1) Expr::True(a.1)
} else { } else {
Expr::False(a.1) Expr::False(a.1)
@ -553,14 +550,19 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
// Fn("...") // Fn("...")
Expr::FnCall(x) Expr::FnCall(x)
if x.1.is_none() if x.1.is_none()
&& (x.0).0 == KEYWORD_FN_PTR && (x.0).0 == KEYWORD_FN_PTR
&& x.3.len() == 1 && x.3.len() == 1
&& matches!(x.3[0], Expr::StringConstant(_)) && matches!(x.3[0], Expr::StringConstant(_))
=> { => {
match &x.3[0] { if let Expr::StringConstant(s) = &x.3[0] {
Expr::StringConstant(s) if is_valid_identifier(s.0.chars()) => Expr::FnPointer(s.clone()), if let Ok(fn_ptr) = FnPtr::try_from(s.0.as_str()) {
_ => Expr::FnCall(x) Expr::FnPointer(Box::new((fn_ptr.take_data().0, s.1)))
} else {
Expr::FnCall(x)
}
} else {
unreachable!()
} }
} }
@ -570,7 +572,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
&& state.optimization_level == OptimizationLevel::Full // full optimizations && state.optimization_level == OptimizationLevel::Full // full optimizations
&& x.3.iter().all(|expr| expr.is_constant()) // all arguments are constants && x.3.iter().all(|expr| expr.is_constant()) // all arguments are constants
=> { => {
let ((name, _, pos), _, _, args, def_value) = x.as_mut(); let ((name, _, _, pos), _, _, args, def_value) = x.as_mut();
// First search in functions lib (can override built-in) // First search in functions lib (can override built-in)
// Cater for both normal function call style and method call style (one additional arguments) // Cater for both normal function call style and method call style (one additional arguments)
@ -578,7 +580,7 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
let _has_script_fn = state.lib.iter_fn().find(|(_, _, _, f)| { let _has_script_fn = state.lib.iter_fn().find(|(_, _, _, f)| {
if !f.is_script() { return false; } if !f.is_script() { return false; }
let fn_def = f.get_fn_def(); let fn_def = f.get_fn_def();
fn_def.name.as_str() == name && (args.len()..=args.len() + 1).contains(&fn_def.params.len()) fn_def.name == name && (args.len()..=args.len() + 1).contains(&fn_def.params.len())
}).is_some(); }).is_some();
#[cfg(feature = "no_function")] #[cfg(feature = "no_function")]
@ -765,6 +767,8 @@ pub fn optimize_into_ast(
access: fn_def.access, access: fn_def.access,
body: Default::default(), body: Default::default(),
params: fn_def.params.clone(), params: fn_def.params.clone(),
#[cfg(not(feature = "no_capture"))]
externals: fn_def.externals.clone(),
pos: fn_def.pos, pos: fn_def.pos,
} }
.into() .into()

View File

@ -23,7 +23,7 @@ fn map_get_values(map: &mut Map) -> FuncReturn<Vec<Dynamic>> {
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, { def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
lib.set_fn_2_mut( lib.set_fn_2_mut(
"has", "has",
|map: &mut Map, prop: ImmutableString| Ok(map.contains_key(prop.as_str())), |map: &mut Map, prop: ImmutableString| Ok(map.contains_key(&prop)),
); );
lib.set_fn_1_mut("len", |map: &mut Map| Ok(map.len() as INT)); lib.set_fn_1_mut("len", |map: &mut Map| Ok(map.len() as INT));
lib.set_fn_1_mut("clear", |map: &mut Map| { lib.set_fn_1_mut("clear", |map: &mut Map| {
@ -32,7 +32,7 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
}); });
lib.set_fn_2_mut( lib.set_fn_2_mut(
"remove", "remove",
|x: &mut Map, name: ImmutableString| Ok(x.remove(name.as_str()).unwrap_or_else(|| ().into())), |x: &mut Map, name: ImmutableString| Ok(x.remove(&name).unwrap_or_else(|| ().into())),
); );
lib.set_fn_2_mut( lib.set_fn_2_mut(
"mixin", "mixin",
@ -47,7 +47,7 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
"fill_with", "fill_with",
|map1: &mut Map, map2: Map| { |map1: &mut Map, map2: Map| {
map2.into_iter().for_each(|(key, value)| { map2.into_iter().for_each(|(key, value)| {
if !map1.contains_key(key.as_str()) { if !map1.contains_key(&key) {
map1.insert(key, value); map1.insert(key, value);
} }
}); });

View File

@ -25,7 +25,7 @@ use crate::stdlib::{
borrow::Cow, borrow::Cow,
boxed::Box, boxed::Box,
char, char,
collections::HashMap, collections::{HashMap, HashSet},
fmt, format, fmt, format,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
iter::empty, iter::empty,
@ -355,7 +355,7 @@ impl fmt::Display for FnAccess {
/// ## WARNING /// ## WARNING
/// ///
/// This type is volatile and may change. /// This type is volatile and may change.
#[derive(Debug, Clone, Hash)] #[derive(Debug, Clone)]
pub struct ScriptFnDef { pub struct ScriptFnDef {
/// Function name. /// Function name.
pub name: ImmutableString, pub name: ImmutableString,
@ -363,6 +363,9 @@ pub struct ScriptFnDef {
pub access: FnAccess, pub access: FnAccess,
/// Names of function parameters. /// Names of function parameters.
pub params: StaticVec<String>, pub params: StaticVec<String>,
/// Access to external variables.
#[cfg(not(feature = "no_capture"))]
pub externals: HashSet<String>,
/// Function body. /// Function body.
pub body: Stmt, pub body: Stmt,
/// Position of the function definition. /// Position of the function definition.
@ -455,7 +458,7 @@ impl<'e> ParseState<'e> {
/// The return value is the offset to be deducted from `Stack::len`, /// The return value is the offset to be deducted from `Stack::len`,
/// i.e. the top element of the `ParseState` is offset 1. /// i.e. the top element of the `ParseState` is offset 1.
/// Return `None` when the variable name is not found in the `stack`. /// Return `None` when the variable name is not found in the `stack`.
fn access_var(&mut self, name: &str, pos: Position) -> Option<NonZeroUsize> { fn access_var(&mut self, name: &str, _pos: Position) -> Option<NonZeroUsize> {
let index = self let index = self
.stack .stack
.iter() .iter()
@ -467,7 +470,7 @@ impl<'e> ParseState<'e> {
#[cfg(not(feature = "no_capture"))] #[cfg(not(feature = "no_capture"))]
if self.capture { if self.capture {
if index.is_none() && !self.externals.contains_key(name) { if index.is_none() && !self.externals.contains_key(name) {
self.externals.insert(name.to_string(), pos); self.externals.insert(name.to_string(), _pos);
} }
} else { } else {
self.capture = true self.capture = true
@ -756,12 +759,12 @@ pub enum Expr {
Stmt(Box<(Stmt, Position)>), Stmt(Box<(Stmt, Position)>),
/// Wrapped expression - should not be optimized away. /// Wrapped expression - should not be optimized away.
Expr(Box<Expr>), Expr(Box<Expr>),
/// func(expr, ... ) - ((function name, native_only, position), optional modules, hash, arguments, optional default value) /// func(expr, ... ) - ((function name, native_only, capture, position), optional modules, hash, arguments, optional default value)
/// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls /// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls
/// and the function names are predictable, so no need to allocate a new `String`. /// and the function names are predictable, so no need to allocate a new `String`.
FnCall( FnCall(
Box<( Box<(
(Cow<'static, str>, bool, Position), (Cow<'static, str>, bool, bool, Position),
Option<Box<ModuleRef>>, Option<Box<ModuleRef>>,
u64, u64,
StaticVec<Expr>, StaticVec<Expr>,
@ -879,7 +882,7 @@ impl Expr {
Self::Property(x) => x.1, Self::Property(x) => x.1,
Self::Stmt(x) => x.1, Self::Stmt(x) => x.1,
Self::Variable(x) => (x.0).1, Self::Variable(x) => (x.0).1,
Self::FnCall(x) => (x.0).2, Self::FnCall(x) => (x.0).3,
Self::Assignment(x) => x.0.position(), Self::Assignment(x) => x.0.position(),
Self::And(x) | Self::Or(x) | Self::In(x) => x.2, Self::And(x) | Self::Or(x) | Self::In(x) => x.2,
@ -911,7 +914,7 @@ impl Expr {
Self::Variable(x) => (x.0).1 = new_pos, Self::Variable(x) => (x.0).1 = new_pos,
Self::Property(x) => x.1 = new_pos, Self::Property(x) => x.1 = new_pos,
Self::Stmt(x) => x.1 = new_pos, Self::Stmt(x) => x.1 = new_pos,
Self::FnCall(x) => (x.0).2 = new_pos, Self::FnCall(x) => (x.0).3 = new_pos,
Self::And(x) => x.2 = new_pos, Self::And(x) => x.2 = new_pos,
Self::Or(x) => x.2 = new_pos, Self::Or(x) => x.2 = new_pos,
Self::In(x) => x.2 = new_pos, Self::In(x) => x.2 = new_pos,
@ -1017,6 +1020,7 @@ impl Expr {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Token::LeftBracket => true, Token::LeftBracket => true,
Token::LeftParen => true, Token::LeftParen => true,
Token::Bang => true,
Token::DoubleColon => true, Token::DoubleColon => true,
_ => false, _ => false,
}, },
@ -1109,6 +1113,7 @@ fn parse_fn_call(
state: &mut ParseState, state: &mut ParseState,
lib: &mut FunctionsLib, lib: &mut FunctionsLib,
id: String, id: String,
capture: bool,
mut modules: Option<Box<ModuleRef>>, mut modules: Option<Box<ModuleRef>>,
settings: ParseSettings, settings: ParseSettings,
) -> Result<Expr, ParseError> { ) -> Result<Expr, ParseError> {
@ -1151,7 +1156,7 @@ fn parse_fn_call(
}; };
return Ok(Expr::FnCall(Box::new(( return Ok(Expr::FnCall(Box::new((
(id.into(), false, settings.pos), (id.into(), false, capture, settings.pos),
modules, modules,
hash_script, hash_script,
args, args,
@ -1193,7 +1198,7 @@ fn parse_fn_call(
}; };
return Ok(Expr::FnCall(Box::new(( return Ok(Expr::FnCall(Box::new((
(id.into(), false, settings.pos), (id.into(), false, capture, settings.pos),
modules, modules,
hash_script, hash_script,
args, args,
@ -1602,6 +1607,8 @@ fn parse_primary(
_ => input.next().unwrap(), _ => input.next().unwrap(),
}; };
let (next_token, _) = input.peek().unwrap();
let mut root_expr = match token { let mut root_expr = match token {
Token::IntegerConstant(x) => Expr::IntegerConstant(Box::new((x, settings.pos))), Token::IntegerConstant(x) => Expr::IntegerConstant(Box::new((x, settings.pos))),
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
@ -1613,7 +1620,7 @@ fn parse_primary(
Expr::Variable(Box::new(((s, settings.pos), None, 0, index))) Expr::Variable(Box::new(((s, settings.pos), None, 0, index)))
} }
// Function call is allowed to have reserved keyword // Function call is allowed to have reserved keyword
Token::Reserved(s) if input.peek().unwrap().0 == Token::LeftParen => { Token::Reserved(s) if *next_token == Token::LeftParen || *next_token == Token::Bang => {
if is_keyword_function(&s) { if is_keyword_function(&s) {
Expr::Variable(Box::new(((s, settings.pos), None, 0, None))) Expr::Variable(Box::new(((s, settings.pos), None, 0, None)))
} else { } else {
@ -1621,7 +1628,7 @@ fn parse_primary(
} }
} }
// Access to `this` as a variable is OK // Access to `this` as a variable is OK
Token::Reserved(s) if s == KEYWORD_THIS && input.peek().unwrap().0 != Token::LeftParen => { Token::Reserved(s) if s == KEYWORD_THIS && *next_token != Token::LeftParen => {
if !settings.is_function_scope { if !settings.is_function_scope {
return Err( return Err(
PERR::BadInput(format!("'{}' can only be used in functions", s)) PERR::BadInput(format!("'{}' can only be used in functions", s))
@ -1661,11 +1668,26 @@ fn parse_primary(
settings.pos = token_pos; settings.pos = token_pos;
root_expr = match (root_expr, token) { root_expr = match (root_expr, token) {
// Function call
#[cfg(not(feature = "no_capture"))]
(Expr::Variable(x), Token::Bang) => {
if !match_token(input, Token::LeftParen)? {
return Err(PERR::MissingToken(
Token::LeftParen.syntax().into(),
"to start arguments list of function call".into(),
)
.into_err(input.peek().unwrap().1));
}
let ((name, pos), modules, _, _) = *x;
settings.pos = pos;
parse_fn_call(input, state, lib, name, true, modules, settings.level_up())?
}
// Function call // Function call
(Expr::Variable(x), Token::LeftParen) => { (Expr::Variable(x), Token::LeftParen) => {
let ((name, pos), modules, _, _) = *x; let ((name, pos), modules, _, _) = *x;
settings.pos = pos; settings.pos = pos;
parse_fn_call(input, state, lib, name, modules, settings.level_up())? parse_fn_call(input, state, lib, name, false, modules, settings.level_up())?
} }
(Expr::Property(_), _) => unreachable!(), (Expr::Property(_), _) => unreachable!(),
// module access // module access
@ -1775,7 +1797,7 @@ fn parse_unary(
args.push(expr); args.push(expr);
Ok(Expr::FnCall(Box::new(( Ok(Expr::FnCall(Box::new((
(op.into(), true, pos), (op.into(), true, false, pos),
None, None,
hash, hash,
args, args,
@ -1800,7 +1822,7 @@ fn parse_unary(
let hash = calc_fn_hash(empty(), op, 2, empty()); let hash = calc_fn_hash(empty(), op, 2, empty());
Ok(Expr::FnCall(Box::new(( Ok(Expr::FnCall(Box::new((
(op.into(), true, pos), (op.into(), true, false, pos),
None, None,
hash, hash,
args, args,
@ -1995,7 +2017,14 @@ fn make_dot_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result<Expr, ParseEr
op_pos, op_pos,
))) )))
} }
// lhs.func() // lhs.func!(...)
(_, Expr::FnCall(x)) if (x.0).2 => {
return Err(PERR::MalformedCapture(
"method-call style does not support capturing".into(),
)
.into_err((x.0).3))
}
// lhs.func(...)
(lhs, func @ Expr::FnCall(_)) => Expr::Dot(Box::new((lhs, func, op_pos))), (lhs, func @ Expr::FnCall(_)) => Expr::Dot(Box::new((lhs, func, op_pos))),
// lhs.rhs // lhs.rhs
(_, rhs) => return Err(PERR::PropertyExpected.into_err(rhs.position())), (_, rhs) => return Err(PERR::PropertyExpected.into_err(rhs.position())),
@ -2212,7 +2241,7 @@ fn parse_binary_op(
let cmp_def = Some(false); let cmp_def = Some(false);
let op = op_token.syntax(); let op = op_token.syntax();
let hash = calc_fn_hash(empty(), &op, 2, empty()); let hash = calc_fn_hash(empty(), &op, 2, empty());
let op = (op, true, pos); let op = (op, true, false, pos);
let mut args = StaticVec::new(); let mut args = StaticVec::new();
args.push(root); args.push(root);
@ -2273,7 +2302,7 @@ fn parse_binary_op(
.unwrap_or(false) => .unwrap_or(false) =>
{ {
// Accept non-native functions for custom operators // Accept non-native functions for custom operators
let op = (op.0, false, op.2); let op = (op.0, false, op.2, op.3);
Expr::FnCall(Box::new((op, None, hash, args, None))) Expr::FnCall(Box::new((op, None, hash, args, None)))
} }
@ -2867,7 +2896,7 @@ fn parse_stmt(
match input.next().unwrap() { match input.next().unwrap() {
(Token::Fn, pos) => { (Token::Fn, pos) => {
let mut state = ParseState::new( let mut new_state = ParseState::new(
state.engine, state.engine,
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
state.max_function_expr_depth, state.max_function_expr_depth,
@ -2886,7 +2915,7 @@ fn parse_stmt(
pos: pos, pos: pos,
}; };
let func = parse_fn(input, &mut state, lib, access, settings)?; let func = parse_fn(input, &mut new_state, lib, access, settings)?;
// Qualifiers (none) + function name + number of arguments. // Qualifiers (none) + function name + number of arguments.
let hash = calc_fn_hash(empty(), &func.name, func.params.len(), empty()); let hash = calc_fn_hash(empty(), &func.name, func.params.len(), empty());
@ -2991,10 +3020,12 @@ fn parse_fn(
let (token, pos) = input.next().unwrap(); let (token, pos) = input.next().unwrap();
let name = token.into_function_name().map_err(|t| match t { let name = token
Token::Reserved(s) => PERR::Reserved(s).into_err(pos), .into_function_name_for_override()
_ => PERR::FnMissingName.into_err(pos), .map_err(|t| match t {
})?; Token::Reserved(s) => PERR::Reserved(s).into_err(pos),
_ => PERR::FnMissingName.into_err(pos),
})?;
match input.peek().unwrap() { match input.peek().unwrap() {
(Token::LeftParen, _) => eat_token(input, Token::LeftParen), (Token::LeftParen, _) => eat_token(input, Token::LeftParen),
@ -3058,12 +3089,23 @@ fn parse_fn(
(_, pos) => return Err(PERR::FnMissingBody(name).into_err(*pos)), (_, pos) => return Err(PERR::FnMissingBody(name).into_err(*pos)),
}; };
let params = params.into_iter().map(|(p, _)| p).collect(); let params: StaticVec<_> = params.into_iter().map(|(p, _)| p).collect();
#[cfg(not(feature = "no_capture"))]
let externals = state
.externals
.iter()
.map(|(name, _)| name)
.filter(|name| !params.contains(name))
.cloned()
.collect();
Ok(ScriptFnDef { Ok(ScriptFnDef {
name: name.into(), name: name.into(),
access, access,
params, params,
#[cfg(not(feature = "no_capture"))]
externals,
body, body,
pos: settings.pos, pos: settings.pos,
}) })
@ -3073,40 +3115,31 @@ fn parse_fn(
#[cfg(not(feature = "no_capture"))] #[cfg(not(feature = "no_capture"))]
fn make_curry_from_externals( fn make_curry_from_externals(
fn_expr: Expr, fn_expr: Expr,
state: &mut ParseState, externals: StaticVec<(String, Position)>,
settings: &ParseSettings, pos: Position,
) -> Expr { ) -> Expr {
if state.externals.is_empty() { if externals.is_empty() {
return fn_expr; return fn_expr;
} }
let num_externals = externals.len();
let mut args: StaticVec<_> = Default::default(); let mut args: StaticVec<_> = Default::default();
state.externals.iter().for_each(|(var_name, pos)| { externals.into_iter().for_each(|(var_name, pos)| {
args.push(Expr::Variable(Box::new(( args.push(Expr::Variable(Box::new(((var_name, pos), None, 0, None))));
(var_name.clone(), *pos),
None,
0,
None,
))));
}); });
let hash = calc_fn_hash( let hash = calc_fn_hash(empty(), KEYWORD_FN_PTR_CURRY, num_externals, empty());
empty(),
KEYWORD_FN_PTR_CURRY,
state.externals.len(),
empty(),
);
let fn_call = Expr::FnCall(Box::new(( let fn_call = Expr::FnCall(Box::new((
(KEYWORD_FN_PTR_CURRY.into(), false, settings.pos), (KEYWORD_FN_PTR_CURRY.into(), false, false, pos),
None, None,
hash, hash,
args, args,
None, None,
))); )));
Expr::Dot(Box::new((fn_expr, fn_call, settings.pos))) Expr::Dot(Box::new((fn_expr, fn_call, pos)))
} }
/// Parse an anonymous function definition. /// Parse an anonymous function definition.
@ -3179,11 +3212,20 @@ fn parse_anon_fn(
#[cfg(feature = "no_capture")] #[cfg(feature = "no_capture")]
let params: StaticVec<_> = params.into_iter().map(|(v, _)| v).collect(); let params: StaticVec<_> = params.into_iter().map(|(v, _)| v).collect();
// External variables may need to be processed in a consistent order,
// so extract them into a list.
#[cfg(not(feature = "no_capture"))]
let externals: StaticVec<_> = state
.externals
.iter()
.map(|(k, &v)| (k.clone(), v))
.collect();
// Add parameters that are auto-curried // Add parameters that are auto-curried
#[cfg(not(feature = "no_capture"))] #[cfg(not(feature = "no_capture"))]
let params: StaticVec<_> = state let params: StaticVec<_> = externals
.externals .iter()
.keys() .map(|(k, _)| k)
.cloned() .cloned()
.chain(params.into_iter().map(|(v, _)| v)) .chain(params.into_iter().map(|(v, _)| v))
.collect(); .collect();
@ -3202,10 +3244,13 @@ fn parse_anon_fn(
// Create unique function name // Create unique function name
let fn_name: ImmutableString = format!("{}{:16x}", FN_ANONYMOUS, hash).into(); let fn_name: ImmutableString = format!("{}{:16x}", FN_ANONYMOUS, hash).into();
// Define the function
let script = ScriptFnDef { let script = ScriptFnDef {
name: fn_name.clone(), name: fn_name.clone(),
access: FnAccess::Public, access: FnAccess::Public,
params, params,
#[cfg(not(feature = "no_capture"))]
externals: Default::default(),
body, body,
pos: settings.pos, pos: settings.pos,
}; };
@ -3213,7 +3258,7 @@ fn parse_anon_fn(
let expr = Expr::FnPointer(Box::new((fn_name, settings.pos))); let expr = Expr::FnPointer(Box::new((fn_name, settings.pos)));
#[cfg(not(feature = "no_capture"))] #[cfg(not(feature = "no_capture"))]
let expr = make_curry_from_externals(expr, state, &settings); let expr = make_curry_from_externals(expr, externals, settings.pos);
Ok((expr, script)) Ok((expr, script))
} }

View File

@ -39,8 +39,8 @@ pub enum EvalAltResult {
/// An error has occurred inside a called function. /// An error has occurred inside a called function.
/// Wrapped values are the name of the function and the interior error. /// Wrapped values are the name of the function and the interior error.
ErrorInFunctionCall(String, Box<EvalAltResult>, Position), ErrorInFunctionCall(String, Box<EvalAltResult>, Position),
/// Access to `this` that is not bounded. /// Access to `this` that is not bound.
ErrorUnboundedThis(Position), ErrorUnboundThis(Position),
/// Non-boolean operand encountered for boolean operator. Wrapped value is the operator. /// Non-boolean operand encountered for boolean operator. Wrapped value is the operator.
ErrorBooleanArgMismatch(String, Position), ErrorBooleanArgMismatch(String, Position),
/// Non-character value encountered where a character is required. /// Non-character value encountered where a character is required.
@ -112,7 +112,7 @@ impl EvalAltResult {
Self::ErrorParsing(p, _) => p.desc(), Self::ErrorParsing(p, _) => p.desc(),
Self::ErrorInFunctionCall(_, _, _) => "Error in called function", Self::ErrorInFunctionCall(_, _, _) => "Error in called function",
Self::ErrorFunctionNotFound(_, _) => "Function not found", Self::ErrorFunctionNotFound(_, _) => "Function not found",
Self::ErrorUnboundedThis(_) => "'this' is not bounded", Self::ErrorUnboundThis(_) => "'this' is not bound",
Self::ErrorBooleanArgMismatch(_, _) => "Boolean operator expects boolean operands", Self::ErrorBooleanArgMismatch(_, _) => "Boolean operator expects boolean operands",
Self::ErrorCharMismatch(_) => "Character expected", Self::ErrorCharMismatch(_) => "Character expected",
Self::ErrorNumericIndexExpr(_) => { Self::ErrorNumericIndexExpr(_) => {
@ -187,7 +187,7 @@ impl fmt::Display for EvalAltResult {
Self::ErrorIndexingType(_, _) Self::ErrorIndexingType(_, _)
| Self::ErrorNumericIndexExpr(_) | Self::ErrorNumericIndexExpr(_)
| Self::ErrorStringIndexExpr(_) | Self::ErrorStringIndexExpr(_)
| Self::ErrorUnboundedThis(_) | Self::ErrorUnboundThis(_)
| Self::ErrorImportExpr(_) | Self::ErrorImportExpr(_)
| Self::ErrorLogicGuard(_) | Self::ErrorLogicGuard(_)
| Self::ErrorFor(_) | Self::ErrorFor(_)
@ -276,7 +276,7 @@ impl EvalAltResult {
Self::ErrorParsing(_, pos) Self::ErrorParsing(_, pos)
| Self::ErrorFunctionNotFound(_, pos) | Self::ErrorFunctionNotFound(_, pos)
| Self::ErrorInFunctionCall(_, _, pos) | Self::ErrorInFunctionCall(_, _, pos)
| Self::ErrorUnboundedThis(pos) | Self::ErrorUnboundThis(pos)
| Self::ErrorBooleanArgMismatch(_, pos) | Self::ErrorBooleanArgMismatch(_, pos)
| Self::ErrorCharMismatch(pos) | Self::ErrorCharMismatch(pos)
| Self::ErrorArrayBounds(_, _, pos) | Self::ErrorArrayBounds(_, _, pos)
@ -316,7 +316,7 @@ impl EvalAltResult {
Self::ErrorParsing(_, pos) Self::ErrorParsing(_, pos)
| Self::ErrorFunctionNotFound(_, pos) | Self::ErrorFunctionNotFound(_, pos)
| Self::ErrorInFunctionCall(_, _, pos) | Self::ErrorInFunctionCall(_, _, pos)
| Self::ErrorUnboundedThis(pos) | Self::ErrorUnboundThis(pos)
| Self::ErrorBooleanArgMismatch(_, pos) | Self::ErrorBooleanArgMismatch(_, pos)
| Self::ErrorCharMismatch(pos) | Self::ErrorCharMismatch(pos)
| Self::ErrorArrayBounds(_, _, pos) | Self::ErrorArrayBounds(_, _, pos)

View File

@ -4,7 +4,9 @@ use crate::any::{Dynamic, Variant};
use crate::parser::{map_dynamic_to_expr, Expr}; use crate::parser::{map_dynamic_to_expr, Expr};
use crate::token::Position; use crate::token::Position;
use crate::stdlib::{borrow::Cow, boxed::Box, iter, string::String, vec::Vec}; use crate::stdlib::{
borrow::Cow, boxed::Box, collections::HashMap, iter, string::String, vec::Vec,
};
/// Type of an entry in the Scope. /// Type of an entry in the Scope.
#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)] #[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)]
@ -316,6 +318,14 @@ impl<'a> Scope<'a> {
}) })
} }
/// Get an entry in the Scope, starting from the last.
pub(crate) fn get_entry(&self, name: &str) -> Option<&Entry> {
self.0
.iter()
.rev()
.find(|Entry { name: key, .. }| name == key)
}
/// Get the value of an entry in the Scope, starting from the last. /// Get the value of an entry in the Scope, starting from the last.
/// ///
/// # Examples /// # Examples
@ -329,10 +339,7 @@ impl<'a> Scope<'a> {
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42); /// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
/// ``` /// ```
pub fn get_value<T: Variant + Clone>(&self, name: &str) -> Option<T> { pub fn get_value<T: Variant + Clone>(&self, name: &str) -> Option<T> {
self.0 self.get_entry(name)
.iter()
.rev()
.find(|Entry { name: key, .. }| name == key)
.and_then(|Entry { value, .. }| value.read::<T>()) .and_then(|Entry { value, .. }| value.read::<T>())
} }
@ -384,13 +391,27 @@ impl<'a> Scope<'a> {
self self
} }
/// Clone the Scope, keeping only the last instances of each variable name.
/// Shadowed variables are omitted in the copy.
#[cfg(not(feature = "no_capture"))]
pub(crate) fn flatten_clone(&self) -> Self {
let mut entries: HashMap<&str, Entry> = Default::default();
self.0.iter().rev().for_each(|entry| {
entries
.entry(entry.name.as_ref())
.or_insert_with(|| entry.clone());
});
Self(entries.into_iter().map(|(_, v)| v).collect())
}
/// Get an iterator to entries in the Scope. /// Get an iterator to entries in the Scope.
#[cfg(not(feature = "no_module"))]
pub(crate) fn into_iter(self) -> impl Iterator<Item = Entry<'a>> { pub(crate) fn into_iter(self) -> impl Iterator<Item = Entry<'a>> {
self.0.into_iter() self.0.into_iter()
} }
/// Get an iterator to entries in the Scope. /// Get an iterator to entries in the Scope in reverse order.
pub(crate) fn to_iter(&self) -> impl Iterator<Item = &Entry> { pub(crate) fn to_iter(&self) -> impl Iterator<Item = &Entry> {
self.0.iter().rev() // Always search a Scope in reverse order self.0.iter().rev() // Always search a Scope in reverse order
} }

View File

@ -679,9 +679,9 @@ impl Token {
} }
/// Convert a token into a function name, if possible. /// Convert a token into a function name, if possible.
pub(crate) fn into_function_name(self) -> Result<String, Self> { pub(crate) fn into_function_name_for_override(self) -> Result<String, Self> {
match self { match self {
Self::Reserved(s) if is_keyword_function(&s) => Ok(s), Self::Reserved(s) if can_override_keyword(&s) => Ok(s),
Self::Custom(s) | Self::Identifier(s) if is_valid_identifier(s.chars()) => Ok(s), Self::Custom(s) | Self::Identifier(s) if is_valid_identifier(s.chars()) => Ok(s),
_ => Err(self), _ => Err(self),
} }
@ -1447,6 +1447,16 @@ pub fn is_keyword_function(name: &str) -> bool {
result result
} }
/// Can this keyword be overridden as a function?
#[inline(always)]
pub fn can_override_keyword(name: &str) -> bool {
name == KEYWORD_PRINT
|| name == KEYWORD_DEBUG
|| name == KEYWORD_TYPE_OF
|| name == KEYWORD_EVAL
|| name == KEYWORD_FN_PTR
}
pub fn is_valid_identifier(name: impl Iterator<Item = char>) -> bool { pub fn is_valid_identifier(name: impl Iterator<Item = char>) -> bool {
let mut first_alphabetic = false; let mut first_alphabetic = false;

View File

@ -6,6 +6,7 @@ use crate::stdlib::{
any::TypeId, any::TypeId,
borrow::Borrow, borrow::Borrow,
boxed::Box, boxed::Box,
cmp::Ordering,
fmt, fmt,
hash::{BuildHasher, Hash, Hasher}, hash::{BuildHasher, Hash, Hasher},
iter::FromIterator, iter::FromIterator,
@ -141,6 +142,12 @@ impl AsRef<String> for ImmutableString {
} }
} }
impl Borrow<String> for ImmutableString {
fn borrow(&self) -> &String {
&self.0
}
}
impl Borrow<str> for ImmutableString { impl Borrow<str> for ImmutableString {
fn borrow(&self) -> &str { fn borrow(&self) -> &str {
self.0.as_str() self.0.as_str()
@ -348,6 +355,42 @@ impl AddAssign<char> for ImmutableString {
} }
} }
impl<S: AsRef<str>> PartialEq<S> for ImmutableString {
fn eq(&self, other: &S) -> bool {
self.as_str().eq(other.as_ref())
}
}
impl PartialEq<ImmutableString> for str {
fn eq(&self, other: &ImmutableString) -> bool {
self.eq(other.as_str())
}
}
impl PartialEq<ImmutableString> for String {
fn eq(&self, other: &ImmutableString) -> bool {
self.eq(other.as_str())
}
}
impl<S: AsRef<str>> PartialOrd<S> for ImmutableString {
fn partial_cmp(&self, other: &S) -> Option<Ordering> {
self.as_str().partial_cmp(other.as_ref())
}
}
impl PartialOrd<ImmutableString> for str {
fn partial_cmp(&self, other: &ImmutableString) -> Option<Ordering> {
self.partial_cmp(other.as_str())
}
}
impl PartialOrd<ImmutableString> for String {
fn partial_cmp(&self, other: &ImmutableString) -> Option<Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}
impl ImmutableString { impl ImmutableString {
/// Consume the `ImmutableString` and convert it into a `String`. /// Consume the `ImmutableString` and convert it into a `String`.
/// If there are other references to the same string, a cloned copy is returned. /// If there are other references to the same string, a cloned copy is returned.

View File

@ -73,7 +73,7 @@ fn test_fn_ptr() -> Result<(), Box<EvalAltResult>> {
"# "#
) )
.expect_err("should error"), .expect_err("should error"),
EvalAltResult::ErrorInFunctionCall(fn_name, err, _) if fn_name == "foo" && matches!(*err, EvalAltResult::ErrorUnboundedThis(_)) EvalAltResult::ErrorInFunctionCall(fn_name, err, _) if fn_name == "foo" && matches!(*err, EvalAltResult::ErrorUnboundThis(_))
)); ));
Ok(()) Ok(())

View File

@ -1,5 +1,5 @@
#![cfg(not(feature = "no_function"))] #![cfg(not(feature = "no_function"))]
use rhai::{Engine, EvalAltResult, INT}; use rhai::{Engine, EvalAltResult, ParseError, ParseErrorType, INT};
#[test] #[test]
fn test_functions() -> Result<(), Box<EvalAltResult>> { fn test_functions() -> Result<(), Box<EvalAltResult>> {
@ -120,3 +120,53 @@ fn test_function_pointers() -> Result<(), Box<EvalAltResult>> {
Ok(()) Ok(())
} }
#[test]
#[cfg(not(feature = "no_capture"))]
fn test_function_captures() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
engine.eval::<INT>(
r#"
fn foo(y) { x += y; x }
let x = 41;
let y = 999;
foo!(1) + x
"#
)?,
83
);
assert!(engine
.eval::<INT>(
r#"
fn foo(y) { x += y; x }
let x = 41;
let y = 999;
foo(1) + x
"#
)
.is_err());
#[cfg(not(feature = "no_object"))]
assert!(matches!(
engine.compile(
r#"
fn foo() { this += x; }
let x = 41;
let y = 999;
y.foo!();
"#
).expect_err("should error"),
ParseError(err, _) if matches!(*err, ParseErrorType::MalformedCapture(_))
));
Ok(())
}