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

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

View File

@@ -20,8 +20,9 @@ use crate::stdlib::ops::Deref;
#[cfg(not(feature = "no_function"))]
use crate::{
parser::ScriptFnDef, r#unsafe::unsafe_cast_var_name_to_lifetime,
scope::EntryType as ScopeEntryType,
parser::ScriptFnDef,
r#unsafe::unsafe_cast_var_name_to_lifetime,
scope::{Entry as ScopeEntry, EntryType as ScopeEntryType},
};
#[cfg(not(feature = "no_float"))]
@@ -36,11 +37,12 @@ use crate::engine::{Map, Target, FN_GET, FN_SET, KEYWORD_TAKE};
use crate::stdlib::{
any::{type_name, TypeId},
boxed::Box,
collections::HashSet,
convert::TryFrom,
format,
iter::{empty, once},
mem,
string::ToString,
string::{String, ToString},
vec::Vec,
};
@@ -66,48 +68,96 @@ fn extract_prop_from_setter(_fn_name: &str) -> Option<&str> {
None
}
/// This function replaces the first argument of a method call with a clone copy.
/// This is to prevent a pure function unintentionally consuming the first argument.
fn normalize_first_arg<'a>(
normalize: bool,
this_copy: &mut Dynamic,
old_this_ptr: &mut Option<&'a mut 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);
/// A type that temporarily stores a mutable reference to a `Dynamic`,
/// replacing it with a cloned copy.
#[derive(Debug, Default)]
struct ArgBackup<'a> {
orig_mut: Option<&'a mut Dynamic>,
value_copy: Dynamic,
}
/// This function restores the first argument that was replaced by `normalize_first_arg_of_method_call`.
fn restore_first_arg<'a>(old_this_ptr: Option<&'a mut Dynamic>, args: &mut FnCallArgs<'a>) {
if let Some(this_pointer) = old_this_ptr {
args[0] = this_pointer;
impl<'a> ArgBackup<'a> {
/// This function replaces the first argument of a method call with a clone copy.
/// This is to prevent a pure function unintentionally consuming the first argument.
///
/// `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 {
/// 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.
///
/// ## WARNING
@@ -115,100 +165,41 @@ impl Engine {
/// 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.
/// **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,
_scope: &mut Scope,
_mods: &mut Imports,
state: &mut State,
lib: &Module,
fn_name: &str,
(hash_fn, hash_script): (u64, u64),
hash_fn: u64,
args: &mut FnCallArgs,
is_ref: bool,
_is_method: bool,
pub_only: bool,
def_val: Option<bool>,
_level: usize,
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
self.inc_operations(state)?;
let native_only = hash_script == 0;
// 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)
// Search for the native function
// First search registered functions (can override packages)
// Then search packages
// NOTE: We skip script functions for global_module and packages, and native functions for lib
let func = if !native_only {
lib.get_fn(hash_script, pub_only) //.or_else(|| lib.get_fn(hash_fn, pub_only))
} else {
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));
let func = self
.global_module
.get_fn(hash_fn, pub_only)
.or_else(|| self.packages.get_fn(hash_fn, pub_only));
if let Some(func) = func {
#[cfg(not(feature = "no_function"))]
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();
assert!(func.is_native());
// Calling pure function but the first argument is a reference?
normalize_first_arg(need_normalize, &mut this_copy, &mut old_this_ptr, 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))
};
}
let mut backup: ArgBackup = Default::default();
backup.change_first_arg_to_copy(is_ref && func.is_pure(), args);
// 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_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)
return Ok(match fn_name {
@@ -340,6 +331,17 @@ impl Engine {
args: &mut FnCallArgs,
level: usize,
) -> 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;
state.scope_level += 1;
@@ -402,7 +404,7 @@ impl Engine {
|| 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.
///
/// ## WARNING
@@ -415,24 +417,23 @@ impl Engine {
state: &mut State,
lib: &Module,
fn_name: &str,
native_only: bool,
hash_script: u64,
args: &mut FnCallArgs,
is_ref: bool,
is_method: bool,
pub_only: bool,
capture: Option<Scope>,
def_val: Option<bool>,
level: usize,
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
// Qualifiers (none) + function name + number of arguments + argument `TypeId`'s.
let arg_types = args.iter().map(|a| a.type_id());
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 {
// 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((
self.map_type_name(args[0].type_name()).to_string().into(),
@@ -442,7 +443,7 @@ impl Engine {
// Fn
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(
"'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
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(
"'eval' should not be called in method style. Try eval(...);".into(),
@@ -460,15 +461,57 @@ impl Engine {
)))
}
// Normal function call
_ => {
let mut scope = Scope::new();
let mut mods = Imports::new();
self.call_fn_raw(
&mut scope, &mut mods, state, lib, fn_name, hashes, args, is_ref, is_method,
pub_only, def_val, level,
)
// Normal script function call
#[cfg(not(feature = "no_function"))]
_ if hash_script > 0 && lib.contains_fn(hash_script, pub_only) => {
// Get scripted function
let func = lib.get_fn(hash_script, pub_only).unwrap().get_fn_def();
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,
state: &mut State,
lib: &Module,
script: &Dynamic,
script_expr: &Dynamic,
_level: usize,
) -> 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(
self.map_type_name(type_name::<ImmutableString>()).into(),
typ.into(),
@@ -523,7 +578,7 @@ impl Engine {
state: &mut State,
lib: &Module,
name: &str,
hash: u64,
hash_script: u64,
target: &mut Target,
idx_val: Dynamic,
def_val: Option<bool>,
@@ -546,7 +601,11 @@ impl Engine {
// Redirect function name
let fn_name = fn_ptr.fn_name();
// 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
let mut arg_values = curry
.iter_mut()
@@ -556,7 +615,7 @@ impl Engine {
// Map it to name(args) in function-call style
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>() {
// FnPtr call on object
@@ -565,7 +624,11 @@ impl Engine {
// Redirect function name
let fn_name = fn_ptr.get_fn_name().clone();
// 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
let mut arg_values = once(obj)
.chain(curry.iter_mut())
@@ -575,7 +638,7 @@ impl Engine {
// Map it to name(args) in function-call style
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>() {
// Curry call
@@ -599,7 +662,7 @@ impl Engine {
} else {
#[cfg(not(feature = "no_object"))]
let redirected;
let mut _hash = hash;
let mut _hash = hash_script;
// Check if it is a map method call in OOP style
#[cfg(not(feature = "no_object"))]
@@ -615,12 +678,16 @@ impl Engine {
}
};
if native {
_hash = 0;
}
// Attached object pointer in front of the arguments
let mut arg_values = once(obj).chain(idx.iter_mut()).collect::<StaticVec<_>>();
let args = arg_values.as_mut();
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,
args_expr: &[Expr],
def_val: Option<bool>,
mut hash: u64,
mut hash_script: u64,
native: bool,
pub_only: bool,
capture: bool,
level: usize,
) -> Result<Dynamic, Box<EvalAltResult>> {
// Handle Fn()
if name == KEYWORD_FN_PTR && 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) {
if !self.has_override(lib, hash_fn, hash_script, pub_only) {
// Fn - only in function call style
let expr = args_expr.get(0).unwrap();
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))
.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());
}
// 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
let redirected;
let mut args_expr = args_expr.as_ref();
@@ -740,7 +786,7 @@ impl Engine {
if name == KEYWORD_FN_PTR_CALL
&& 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 fn_name = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
@@ -754,7 +800,7 @@ impl Engine {
// Skip the first argument
args_expr = &args_expr.as_ref()[1..];
// 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 {
return Err(Box::new(EvalAltResult::ErrorMismatchOutputType(
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 args: StaticVec<_>;
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() {
// No arguments
@@ -808,9 +882,11 @@ impl Engine {
}
}
let hash = if native { 0 } else { hash_script };
let args = args.as_mut();
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)
}
@@ -829,10 +905,18 @@ impl Engine {
args_expr: &[Expr],
def_val: Option<bool>,
hash_script: u64,
capture: bool,
level: usize,
) -> Result<Dynamic, Box<EvalAltResult>> {
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 args: StaticVec<_>;
@@ -898,12 +982,18 @@ impl Engine {
#[cfg(not(feature = "no_function"))]
Some(f) if f.is_script() => {
let args = args.as_mut();
let fn_def = f.get_fn_def();
let mut scope = Scope::new();
let mut mods = Imports::new();
self.call_script_fn(
&mut scope, &mut mods, state, lib, &mut None, name, fn_def, args, level,
)
let func = f.get_fn_def();
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);
}
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()),
None if def_val.is_some() => Ok(def_val.unwrap().into()),