Optimize hot path of operators calling.
This commit is contained in:
parent
65ee262f1b
commit
0374311cf6
23
src/api.rs
23
src/api.rs
@ -909,12 +909,12 @@ impl Engine {
|
|||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
ast: &AST,
|
ast: &AST,
|
||||||
) -> Result<(Dynamic, u64), Box<EvalAltResult>> {
|
) -> Result<(Dynamic, u64), Box<EvalAltResult>> {
|
||||||
let mut state = State::new(ast.fn_lib());
|
let mut state = State::new();
|
||||||
|
|
||||||
ast.statements()
|
ast.statements()
|
||||||
.iter()
|
.iter()
|
||||||
.try_fold(().into(), |_, stmt| {
|
.try_fold(().into(), |_, stmt| {
|
||||||
self.eval_stmt(scope, &mut state, stmt, 0)
|
self.eval_stmt(scope, &mut state, ast.lib(), stmt, 0)
|
||||||
})
|
})
|
||||||
.or_else(|err| match *err {
|
.or_else(|err| match *err {
|
||||||
EvalAltResult::Return(out, _) => Ok(out),
|
EvalAltResult::Return(out, _) => Ok(out),
|
||||||
@ -980,12 +980,12 @@ impl Engine {
|
|||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
ast: &AST,
|
ast: &AST,
|
||||||
) -> Result<(), Box<EvalAltResult>> {
|
) -> Result<(), Box<EvalAltResult>> {
|
||||||
let mut state = State::new(ast.fn_lib());
|
let mut state = State::new();
|
||||||
|
|
||||||
ast.statements()
|
ast.statements()
|
||||||
.iter()
|
.iter()
|
||||||
.try_fold(().into(), |_, stmt| {
|
.try_fold(().into(), |_, stmt| {
|
||||||
self.eval_stmt(scope, &mut state, stmt, 0)
|
self.eval_stmt(scope, &mut state, ast.lib(), stmt, 0)
|
||||||
})
|
})
|
||||||
.map_or_else(
|
.map_or_else(
|
||||||
|err| match *err {
|
|err| match *err {
|
||||||
@ -1041,17 +1041,18 @@ impl Engine {
|
|||||||
) -> Result<T, Box<EvalAltResult>> {
|
) -> Result<T, Box<EvalAltResult>> {
|
||||||
let mut arg_values = args.into_vec();
|
let mut arg_values = args.into_vec();
|
||||||
let mut args: StaticVec<_> = arg_values.iter_mut().collect();
|
let mut args: StaticVec<_> = arg_values.iter_mut().collect();
|
||||||
let fn_lib = ast.fn_lib();
|
let lib = ast.lib();
|
||||||
let pos = Position::none();
|
let pos = Position::none();
|
||||||
|
|
||||||
let fn_def = fn_lib
|
let fn_def = lib
|
||||||
.get_function_by_signature(name, args.len(), true)
|
.get_function_by_signature(name, args.len(), true)
|
||||||
.ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.into(), pos)))?;
|
.ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.into(), pos)))?;
|
||||||
|
|
||||||
let state = State::new(fn_lib);
|
let mut state = State::new();
|
||||||
let args = args.as_mut();
|
let args = args.as_mut();
|
||||||
|
|
||||||
let (result, _) = self.call_script_fn(Some(scope), state, name, fn_def, args, pos, 0)?;
|
let result =
|
||||||
|
self.call_script_fn(Some(scope), &mut state, &lib, name, fn_def, args, pos, 0)?;
|
||||||
|
|
||||||
let return_type = self.map_type_name(result.type_name());
|
let return_type = self.map_type_name(result.type_name());
|
||||||
|
|
||||||
@ -1081,14 +1082,14 @@ impl Engine {
|
|||||||
mut ast: AST,
|
mut ast: AST,
|
||||||
optimization_level: OptimizationLevel,
|
optimization_level: OptimizationLevel,
|
||||||
) -> AST {
|
) -> AST {
|
||||||
let fn_lib = ast
|
let lib = ast
|
||||||
.fn_lib()
|
.lib()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_, fn_def)| fn_def.as_ref().clone())
|
.map(|(_, fn_def)| fn_def.as_ref().clone())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let stmt = mem::take(ast.statements_mut());
|
let stmt = mem::take(ast.statements_mut());
|
||||||
optimize_into_ast(self, scope, stmt, fn_lib, optimization_level)
|
optimize_into_ast(self, scope, stmt, lib, optimization_level)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register a callback for script evaluation progress.
|
/// Register a callback for script evaluation progress.
|
||||||
|
411
src/engine.rs
411
src/engine.rs
@ -32,9 +32,7 @@ use crate::stdlib::{
|
|||||||
mem,
|
mem,
|
||||||
num::NonZeroUsize,
|
num::NonZeroUsize,
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
rc::Rc,
|
|
||||||
string::{String, ToString},
|
string::{String, ToString},
|
||||||
sync::Arc,
|
|
||||||
vec::Vec,
|
vec::Vec,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -171,11 +169,8 @@ impl<T: Into<Dynamic>> From<T> for Target<'_> {
|
|||||||
///
|
///
|
||||||
/// This type uses some unsafe code, mainly for avoiding cloning of local variable names via
|
/// This type uses some unsafe code, mainly for avoiding cloning of local variable names via
|
||||||
/// direct lifetime casting.
|
/// direct lifetime casting.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct State<'a> {
|
pub struct State {
|
||||||
/// Global script-defined functions.
|
|
||||||
pub fn_lib: &'a FunctionsLib,
|
|
||||||
|
|
||||||
/// Normally, access to variables are parsed with a relative offset into the scope to avoid a lookup.
|
/// Normally, access to variables are parsed with a relative offset into the scope to avoid a lookup.
|
||||||
/// In some situation, e.g. after running an `eval` statement, subsequent offsets may become mis-aligned.
|
/// In some situation, e.g. after running an `eval` statement, subsequent offsets may become mis-aligned.
|
||||||
/// When that happens, this flag is turned on to force a scope lookup by name.
|
/// When that happens, this flag is turned on to force a scope lookup by name.
|
||||||
@ -190,26 +185,39 @@ pub struct State<'a> {
|
|||||||
|
|
||||||
/// Number of modules loaded.
|
/// Number of modules loaded.
|
||||||
pub modules: u64,
|
pub modules: u64,
|
||||||
|
|
||||||
|
/// Times built-in function used.
|
||||||
|
pub use_builtin_counter: usize,
|
||||||
|
|
||||||
|
/// Number of modules loaded.
|
||||||
|
pub use_builtin_binary_op: HashMap<String, TypeId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> State<'a> {
|
impl State {
|
||||||
/// Create a new `State`.
|
/// Create a new `State`.
|
||||||
pub fn new(fn_lib: &'a FunctionsLib) -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Default::default()
|
||||||
fn_lib,
|
}
|
||||||
always_search: false,
|
/// Cache a built-in binary op function.
|
||||||
scope_level: 0,
|
pub fn set_builtin_binary_op(&mut self, fn_name: impl Into<String>, typ: TypeId) {
|
||||||
operations: 0,
|
// Only cache when exceeding a certain number of calls
|
||||||
modules: 0,
|
if self.use_builtin_counter < 10 {
|
||||||
|
self.use_builtin_counter += 1;
|
||||||
|
} else {
|
||||||
|
self.use_builtin_binary_op.insert(fn_name.into(), typ);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Does a certain script-defined function exist in the `State`?
|
/// Is a binary op known to be built-in?
|
||||||
pub fn has_function(&self, hash: u64) -> bool {
|
pub fn use_builtin_binary_op(&self, fn_name: impl AsRef<str>, args: &[&mut Dynamic]) -> bool {
|
||||||
self.fn_lib.contains_key(&hash)
|
if self.use_builtin_counter < 10 {
|
||||||
}
|
false
|
||||||
/// Get a script-defined function definition from the `State`.
|
} else if args.len() != 2 {
|
||||||
pub fn get_function(&self, hash: u64) -> Option<&FnDef> {
|
false
|
||||||
self.fn_lib.get(&hash).map(|fn_def| fn_def.as_ref())
|
} else if args[0].type_id() != args[1].type_id() {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
self.use_builtin_binary_op.get(fn_name.as_ref()) == Some(&args[0].type_id())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -378,7 +386,7 @@ impl Default for Engine {
|
|||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
module_resolver: None,
|
module_resolver: None,
|
||||||
|
|
||||||
type_names: Default::default(),
|
type_names: HashMap::new(),
|
||||||
|
|
||||||
// default print/debug implementations
|
// default print/debug implementations
|
||||||
print: Box::new(default_print),
|
print: Box::new(default_print),
|
||||||
@ -516,7 +524,7 @@ impl Engine {
|
|||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
module_resolver: None,
|
module_resolver: None,
|
||||||
|
|
||||||
type_names: Default::default(),
|
type_names: HashMap::new(),
|
||||||
print: Box::new(|_| {}),
|
print: Box::new(|_| {}),
|
||||||
debug: Box::new(|_| {}),
|
debug: Box::new(|_| {}),
|
||||||
progress: None,
|
progress: None,
|
||||||
@ -611,6 +619,7 @@ impl Engine {
|
|||||||
&self,
|
&self,
|
||||||
scope: Option<&mut Scope>,
|
scope: Option<&mut Scope>,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
|
lib: &FunctionsLib,
|
||||||
fn_name: &str,
|
fn_name: &str,
|
||||||
hashes: (u64, u64),
|
hashes: (u64, u64),
|
||||||
args: &mut FnCallArgs,
|
args: &mut FnCallArgs,
|
||||||
@ -630,77 +639,94 @@ impl Engine {
|
|||||||
|
|
||||||
// First search in script-defined functions (can override built-in)
|
// First search in script-defined functions (can override built-in)
|
||||||
if !native_only {
|
if !native_only {
|
||||||
if let Some(fn_def) = state.get_function(hashes.1) {
|
if let Some(fn_def) = lib.get(&hashes.1) {
|
||||||
let (result, state2) =
|
let result =
|
||||||
self.call_script_fn(scope, *state, fn_name, fn_def, args, pos, level)?;
|
self.call_script_fn(scope, state, lib, fn_name, fn_def, args, pos, level)?;
|
||||||
*state = state2;
|
|
||||||
return Ok((result, false));
|
return Ok((result, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search built-in's and external functions
|
let builtin = state.use_builtin_binary_op(fn_name, args);
|
||||||
if let Some(func) = self
|
|
||||||
.global_module
|
|
||||||
.get_fn(hashes.0)
|
|
||||||
.or_else(|| self.packages.get_fn(hashes.0))
|
|
||||||
{
|
|
||||||
// Calling pure function in method-call?
|
|
||||||
let mut this_copy: Option<Dynamic>;
|
|
||||||
let mut this_pointer: Option<&mut Dynamic> = None;
|
|
||||||
|
|
||||||
if func.is_pure() && is_ref && args.len() > 0 {
|
if is_ref || !native_only || !builtin {
|
||||||
// Clone the original value. It'll be consumed because the function
|
// Search built-in's and external functions
|
||||||
// is pure and doesn't know that the first value is a reference (i.e. `is_ref`)
|
if let Some(func) = self
|
||||||
this_copy = Some(args[0].clone());
|
.global_module
|
||||||
|
.get_fn(hashes.0)
|
||||||
|
.or_else(|| self.packages.get_fn(hashes.0))
|
||||||
|
{
|
||||||
|
// Calling pure function in method-call?
|
||||||
|
let mut this_copy: Option<Dynamic>;
|
||||||
|
let mut this_pointer: Option<&mut Dynamic> = None;
|
||||||
|
|
||||||
// Replace the first reference with a reference to the clone, force-casting the lifetime.
|
if func.is_pure() && is_ref && args.len() > 0 {
|
||||||
// Keep the original reference. Must remember to restore it before existing this function.
|
// Clone the original value. It'll be consumed because the function
|
||||||
this_pointer = Some(mem::replace(
|
// is pure and doesn't know that the first value is a reference (i.e. `is_ref`)
|
||||||
args.get_mut(0).unwrap(),
|
this_copy = Some(args[0].clone());
|
||||||
unsafe_mut_cast_to_lifetime(this_copy.as_mut().unwrap()),
|
|
||||||
));
|
// Replace the first reference with a reference to the clone, force-casting the lifetime.
|
||||||
|
// Keep the original reference. Must remember to restore it before existing this function.
|
||||||
|
this_pointer = Some(mem::replace(
|
||||||
|
args.get_mut(0).unwrap(),
|
||||||
|
unsafe_mut_cast_to_lifetime(this_copy.as_mut().unwrap()),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run external function
|
||||||
|
let result = func.get_native_fn()(args);
|
||||||
|
|
||||||
|
// Restore the original reference
|
||||||
|
if let Some(this_pointer) = this_pointer {
|
||||||
|
mem::replace(args.get_mut(0).unwrap(), this_pointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = result.map_err(|err| err.new_position(pos))?;
|
||||||
|
|
||||||
|
// See if the function match print/debug (which requires special processing)
|
||||||
|
return Ok(match fn_name {
|
||||||
|
KEYWORD_PRINT => (
|
||||||
|
(self.print)(result.as_str().map_err(|type_name| {
|
||||||
|
Box::new(EvalAltResult::ErrorMismatchOutputType(
|
||||||
|
type_name.into(),
|
||||||
|
pos,
|
||||||
|
))
|
||||||
|
})?)
|
||||||
|
.into(),
|
||||||
|
false,
|
||||||
|
),
|
||||||
|
KEYWORD_DEBUG => (
|
||||||
|
(self.debug)(result.as_str().map_err(|type_name| {
|
||||||
|
Box::new(EvalAltResult::ErrorMismatchOutputType(
|
||||||
|
type_name.into(),
|
||||||
|
pos,
|
||||||
|
))
|
||||||
|
})?)
|
||||||
|
.into(),
|
||||||
|
false,
|
||||||
|
),
|
||||||
|
_ => (result, func.is_method()),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run external function
|
|
||||||
let result = func.get_native_fn()(args);
|
|
||||||
|
|
||||||
// Restore the original reference
|
|
||||||
if let Some(this_pointer) = this_pointer {
|
|
||||||
mem::replace(args.get_mut(0).unwrap(), this_pointer);
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = result.map_err(|err| err.new_position(pos))?;
|
|
||||||
|
|
||||||
// See if the function match print/debug (which requires special processing)
|
|
||||||
return Ok(match fn_name {
|
|
||||||
KEYWORD_PRINT => (
|
|
||||||
(self.print)(result.as_str().map_err(|type_name| {
|
|
||||||
Box::new(EvalAltResult::ErrorMismatchOutputType(
|
|
||||||
type_name.into(),
|
|
||||||
pos,
|
|
||||||
))
|
|
||||||
})?)
|
|
||||||
.into(),
|
|
||||||
false,
|
|
||||||
),
|
|
||||||
KEYWORD_DEBUG => (
|
|
||||||
(self.debug)(result.as_str().map_err(|type_name| {
|
|
||||||
Box::new(EvalAltResult::ErrorMismatchOutputType(
|
|
||||||
type_name.into(),
|
|
||||||
pos,
|
|
||||||
))
|
|
||||||
})?)
|
|
||||||
.into(),
|
|
||||||
false,
|
|
||||||
),
|
|
||||||
_ => (result, func.is_method()),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it is a 2-operand operator, see if it is built in
|
// If it is a 2-operand operator, see if it is built in.
|
||||||
if !is_ref && native_only && args.len() == 2 && args[0].type_id() == args[1].type_id() {
|
// Only consider situations where:
|
||||||
|
// 1) It is not a method call,
|
||||||
|
// 2) the call explicitly specifies `native_only` because, remember, an `eval` may create a new function, so you
|
||||||
|
// can never predict what functions lie in `lib`!
|
||||||
|
// 3) It is already a built-in run once before, or both parameter types are the same
|
||||||
|
if !is_ref
|
||||||
|
&& native_only
|
||||||
|
&& (builtin || (args.len() == 2 && args[0].type_id() == args[1].type_id()))
|
||||||
|
{
|
||||||
match run_builtin_binary_op(fn_name, args[0], args[1])? {
|
match run_builtin_binary_op(fn_name, args[0], args[1])? {
|
||||||
Some(v) => return Ok((v, false)),
|
Some(v) => {
|
||||||
|
// Mark the function call as built-in
|
||||||
|
if !builtin {
|
||||||
|
state.set_builtin_binary_op(fn_name, args[0].type_id());
|
||||||
|
}
|
||||||
|
return Ok((v, false));
|
||||||
|
}
|
||||||
None => (),
|
None => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -753,16 +779,17 @@ impl Engine {
|
|||||||
/// Function call arguments may be _consumed_ when the function requires them to be passed by value.
|
/// Function call arguments may 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_script_fn<'s>(
|
pub(crate) fn call_script_fn(
|
||||||
&self,
|
&self,
|
||||||
scope: Option<&mut Scope>,
|
scope: Option<&mut Scope>,
|
||||||
mut state: State<'s>,
|
state: &mut State,
|
||||||
|
lib: &FunctionsLib,
|
||||||
fn_name: &str,
|
fn_name: &str,
|
||||||
fn_def: &FnDef,
|
fn_def: &FnDef,
|
||||||
args: &mut FnCallArgs,
|
args: &mut FnCallArgs,
|
||||||
pos: Position,
|
pos: Position,
|
||||||
level: usize,
|
level: usize,
|
||||||
) -> Result<(Dynamic, State<'s>), Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
let orig_scope_level = state.scope_level;
|
let orig_scope_level = state.scope_level;
|
||||||
state.scope_level += 1;
|
state.scope_level += 1;
|
||||||
|
|
||||||
@ -779,15 +806,14 @@ impl Engine {
|
|||||||
.iter()
|
.iter()
|
||||||
.zip(args.iter_mut().map(|v| mem::take(*v)))
|
.zip(args.iter_mut().map(|v| mem::take(*v)))
|
||||||
.map(|(name, value)| {
|
.map(|(name, value)| {
|
||||||
let var_name =
|
let var_name = unsafe_cast_var_name_to_lifetime(name.as_str(), state);
|
||||||
unsafe_cast_var_name_to_lifetime(name.as_str(), &mut state);
|
|
||||||
(var_name, ScopeEntryType::Normal, value)
|
(var_name, ScopeEntryType::Normal, value)
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Evaluate the function at one higher level of call depth
|
// Evaluate the function at one higher level of call depth
|
||||||
let result = self
|
let result = self
|
||||||
.eval_stmt(scope, &mut state, &fn_def.body, level + 1)
|
.eval_stmt(scope, state, lib, &fn_def.body, level + 1)
|
||||||
.or_else(|err| match *err {
|
.or_else(|err| match *err {
|
||||||
// Convert return statement to return value
|
// Convert return statement to return value
|
||||||
EvalAltResult::Return(x, _) => Ok(x),
|
EvalAltResult::Return(x, _) => Ok(x),
|
||||||
@ -809,7 +835,7 @@ impl Engine {
|
|||||||
scope.rewind(scope_len);
|
scope.rewind(scope_len);
|
||||||
state.scope_level = orig_scope_level;
|
state.scope_level = orig_scope_level;
|
||||||
|
|
||||||
return result.map(|v| (v, state));
|
return result;
|
||||||
}
|
}
|
||||||
// No new scope - create internal scope
|
// No new scope - create internal scope
|
||||||
_ => {
|
_ => {
|
||||||
@ -827,7 +853,7 @@ impl Engine {
|
|||||||
|
|
||||||
// Evaluate the function at one higher level of call depth
|
// Evaluate the function at one higher level of call depth
|
||||||
let result = self
|
let result = self
|
||||||
.eval_stmt(&mut scope, &mut state, &fn_def.body, level + 1)
|
.eval_stmt(&mut scope, state, lib, &fn_def.body, level + 1)
|
||||||
.or_else(|err| match *err {
|
.or_else(|err| match *err {
|
||||||
// Convert return statement to return value
|
// Convert return statement to return value
|
||||||
EvalAltResult::Return(x, _) => Ok(x),
|
EvalAltResult::Return(x, _) => Ok(x),
|
||||||
@ -846,19 +872,19 @@ impl Engine {
|
|||||||
});
|
});
|
||||||
|
|
||||||
state.scope_level = orig_scope_level;
|
state.scope_level = orig_scope_level;
|
||||||
return result.map(|v| (v, state));
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Has a system function an override?
|
// Has a system function an override?
|
||||||
fn has_override(&self, state: &State, hashes: (u64, u64)) -> bool {
|
fn has_override(&self, lib: &FunctionsLib, hashes: (u64, u64)) -> bool {
|
||||||
// First check registered functions
|
// First check registered functions
|
||||||
self.global_module.contains_fn(hashes.0)
|
self.global_module.contains_fn(hashes.0)
|
||||||
// Then check packages
|
// Then check packages
|
||||||
|| self.packages.contains_fn(hashes.0)
|
|| self.packages.contains_fn(hashes.0)
|
||||||
// Then check script-defined functions
|
// Then check script-defined functions
|
||||||
|| state.has_function(hashes.1)
|
|| lib.contains_key(&hashes.1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform an actual function call, taking care of special functions
|
// Perform an actual function call, taking care of special functions
|
||||||
@ -871,6 +897,7 @@ impl Engine {
|
|||||||
fn exec_fn_call(
|
fn exec_fn_call(
|
||||||
&self,
|
&self,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
|
lib: &FunctionsLib,
|
||||||
fn_name: &str,
|
fn_name: &str,
|
||||||
native_only: bool,
|
native_only: bool,
|
||||||
hash_fn_def: u64,
|
hash_fn_def: u64,
|
||||||
@ -891,13 +918,13 @@ impl Engine {
|
|||||||
|
|
||||||
match fn_name {
|
match fn_name {
|
||||||
// type_of
|
// type_of
|
||||||
KEYWORD_TYPE_OF if args.len() == 1 && !self.has_override(state, hashes) => Ok((
|
KEYWORD_TYPE_OF if args.len() == 1 && !self.has_override(lib, hashes) => Ok((
|
||||||
self.map_type_name(args[0].type_name()).to_string().into(),
|
self.map_type_name(args[0].type_name()).to_string().into(),
|
||||||
false,
|
false,
|
||||||
)),
|
)),
|
||||||
|
|
||||||
// eval - reaching this point it must be a method-style call
|
// eval - reaching this point it must be a method-style call
|
||||||
KEYWORD_EVAL if args.len() == 1 && !self.has_override(state, hashes) => {
|
KEYWORD_EVAL if args.len() == 1 && !self.has_override(lib, hashes) => {
|
||||||
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(),
|
||||||
pos,
|
pos,
|
||||||
@ -906,7 +933,7 @@ impl Engine {
|
|||||||
|
|
||||||
// Normal function call
|
// Normal function call
|
||||||
_ => self.call_fn_raw(
|
_ => self.call_fn_raw(
|
||||||
None, state, fn_name, hashes, args, is_ref, def_val, pos, level,
|
None, state, lib, fn_name, hashes, args, is_ref, def_val, pos, level,
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -916,6 +943,7 @@ impl Engine {
|
|||||||
&self,
|
&self,
|
||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
|
lib: &FunctionsLib,
|
||||||
script: &Dynamic,
|
script: &Dynamic,
|
||||||
pos: Position,
|
pos: Position,
|
||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
@ -932,14 +960,14 @@ impl Engine {
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
// If new functions are defined within the eval string, it is an error
|
// If new functions are defined within the eval string, it is an error
|
||||||
if ast.fn_lib().len() > 0 {
|
if ast.lib().len() > 0 {
|
||||||
return Err(Box::new(EvalAltResult::ErrorParsing(
|
return Err(Box::new(EvalAltResult::ErrorParsing(
|
||||||
ParseErrorType::WrongFnDefinition.into_err(pos),
|
ParseErrorType::WrongFnDefinition.into_err(pos),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let statements = mem::take(ast.statements_mut());
|
let statements = mem::take(ast.statements_mut());
|
||||||
let ast = AST::new(statements, state.fn_lib.clone());
|
let ast = AST::new(statements, lib.clone());
|
||||||
|
|
||||||
// Evaluate the AST
|
// Evaluate the AST
|
||||||
let (result, operations) = self
|
let (result, operations) = self
|
||||||
@ -956,6 +984,7 @@ impl Engine {
|
|||||||
fn eval_dot_index_chain_helper(
|
fn eval_dot_index_chain_helper(
|
||||||
&self,
|
&self,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
|
lib: &FunctionsLib,
|
||||||
target: &mut Target,
|
target: &mut Target,
|
||||||
rhs: &Expr,
|
rhs: &Expr,
|
||||||
idx_values: &mut StaticVec<Dynamic>,
|
idx_values: &mut StaticVec<Dynamic>,
|
||||||
@ -979,24 +1008,33 @@ impl Engine {
|
|||||||
let is_idx = matches!(rhs, Expr::Index(_));
|
let is_idx = matches!(rhs, Expr::Index(_));
|
||||||
let pos = x.0.position();
|
let pos = x.0.position();
|
||||||
let this_ptr = &mut self
|
let this_ptr = &mut self
|
||||||
.get_indexed_mut(state, obj, is_ref, idx_val, pos, op_pos, false)?;
|
.get_indexed_mut(state, lib, obj, is_ref, idx_val, pos, op_pos, false)?;
|
||||||
|
|
||||||
self.eval_dot_index_chain_helper(
|
self.eval_dot_index_chain_helper(
|
||||||
state, this_ptr, &x.1, idx_values, is_idx, x.2, level, new_val,
|
state, lib, this_ptr, &x.1, idx_values, is_idx, x.2, level, new_val,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// xxx[rhs] = new_val
|
// xxx[rhs] = new_val
|
||||||
_ if new_val.is_some() => {
|
_ if new_val.is_some() => {
|
||||||
let pos = rhs.position();
|
let pos = rhs.position();
|
||||||
let this_ptr = &mut self
|
let this_ptr = &mut self
|
||||||
.get_indexed_mut(state, obj, is_ref, idx_val, pos, op_pos, true)?;
|
.get_indexed_mut(state, lib, obj, is_ref, idx_val, pos, op_pos, true)?;
|
||||||
|
|
||||||
this_ptr.set_value(new_val.unwrap(), rhs.position())?;
|
this_ptr.set_value(new_val.unwrap(), rhs.position())?;
|
||||||
Ok((Default::default(), true))
|
Ok((Default::default(), true))
|
||||||
}
|
}
|
||||||
// xxx[rhs]
|
// xxx[rhs]
|
||||||
_ => self
|
_ => self
|
||||||
.get_indexed_mut(state, obj, is_ref, idx_val, rhs.position(), op_pos, false)
|
.get_indexed_mut(
|
||||||
|
state,
|
||||||
|
lib,
|
||||||
|
obj,
|
||||||
|
is_ref,
|
||||||
|
idx_val,
|
||||||
|
rhs.position(),
|
||||||
|
op_pos,
|
||||||
|
false,
|
||||||
|
)
|
||||||
.map(|v| (v.clone_into_dynamic(), false)),
|
.map(|v| (v.clone_into_dynamic(), false)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -1016,7 +1054,9 @@ impl Engine {
|
|||||||
.collect();
|
.collect();
|
||||||
let args = arg_values.as_mut();
|
let args = arg_values.as_mut();
|
||||||
|
|
||||||
self.exec_fn_call(state, name, *native, *hash, args, is_ref, def_val, *pos, 0)
|
self.exec_fn_call(
|
||||||
|
state, lib, name, *native, *hash, args, is_ref, def_val, *pos, 0,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
// xxx.module::fn_name(...) - syntax error
|
// xxx.module::fn_name(...) - syntax error
|
||||||
Expr::FnCall(_) => unreachable!(),
|
Expr::FnCall(_) => unreachable!(),
|
||||||
@ -1026,7 +1066,7 @@ impl Engine {
|
|||||||
let ((prop, _, _), pos) = x.as_ref();
|
let ((prop, _, _), pos) = x.as_ref();
|
||||||
let index = prop.clone().into();
|
let index = prop.clone().into();
|
||||||
let mut val =
|
let mut val =
|
||||||
self.get_indexed_mut(state, obj, is_ref, index, *pos, op_pos, true)?;
|
self.get_indexed_mut(state, lib, obj, is_ref, index, *pos, op_pos, true)?;
|
||||||
|
|
||||||
val.set_value(new_val.unwrap(), rhs.position())?;
|
val.set_value(new_val.unwrap(), rhs.position())?;
|
||||||
Ok((Default::default(), true))
|
Ok((Default::default(), true))
|
||||||
@ -1037,7 +1077,7 @@ impl Engine {
|
|||||||
let ((prop, _, _), pos) = x.as_ref();
|
let ((prop, _, _), pos) = x.as_ref();
|
||||||
let index = prop.clone().into();
|
let index = prop.clone().into();
|
||||||
let val =
|
let val =
|
||||||
self.get_indexed_mut(state, obj, is_ref, index, *pos, op_pos, false)?;
|
self.get_indexed_mut(state, lib, obj, is_ref, index, *pos, op_pos, false)?;
|
||||||
|
|
||||||
Ok((val.clone_into_dynamic(), false))
|
Ok((val.clone_into_dynamic(), false))
|
||||||
}
|
}
|
||||||
@ -1045,15 +1085,19 @@ impl Engine {
|
|||||||
Expr::Property(x) if new_val.is_some() => {
|
Expr::Property(x) if new_val.is_some() => {
|
||||||
let ((_, _, setter), pos) = x.as_ref();
|
let ((_, _, setter), pos) = x.as_ref();
|
||||||
let mut args = [obj, new_val.as_mut().unwrap()];
|
let mut args = [obj, new_val.as_mut().unwrap()];
|
||||||
self.exec_fn_call(state, setter, true, 0, &mut args, is_ref, None, *pos, 0)
|
self.exec_fn_call(
|
||||||
.map(|(v, _)| (v, true))
|
state, lib, setter, true, 0, &mut args, is_ref, None, *pos, 0,
|
||||||
|
)
|
||||||
|
.map(|(v, _)| (v, true))
|
||||||
}
|
}
|
||||||
// xxx.id
|
// xxx.id
|
||||||
Expr::Property(x) => {
|
Expr::Property(x) => {
|
||||||
let ((_, getter, _), pos) = x.as_ref();
|
let ((_, getter, _), pos) = x.as_ref();
|
||||||
let mut args = [obj];
|
let mut args = [obj];
|
||||||
self.exec_fn_call(state, getter, true, 0, &mut args, is_ref, None, *pos, 0)
|
self.exec_fn_call(
|
||||||
.map(|(v, _)| (v, false))
|
state, lib, getter, true, 0, &mut args, is_ref, None, *pos, 0,
|
||||||
|
)
|
||||||
|
.map(|(v, _)| (v, false))
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
// {xxx:map}.idx_lhs[idx_expr] | {xxx:map}.dot_lhs.rhs
|
// {xxx:map}.idx_lhs[idx_expr] | {xxx:map}.dot_lhs.rhs
|
||||||
@ -1063,7 +1107,7 @@ impl Engine {
|
|||||||
let mut val = if let Expr::Property(p) = &x.0 {
|
let mut val = if let Expr::Property(p) = &x.0 {
|
||||||
let ((prop, _, _), _) = p.as_ref();
|
let ((prop, _, _), _) = p.as_ref();
|
||||||
let index = prop.clone().into();
|
let index = prop.clone().into();
|
||||||
self.get_indexed_mut(state, obj, is_ref, index, x.2, op_pos, false)?
|
self.get_indexed_mut(state, lib, obj, is_ref, index, x.2, op_pos, false)?
|
||||||
} else {
|
} else {
|
||||||
// Syntax error
|
// Syntax error
|
||||||
return Err(Box::new(EvalAltResult::ErrorDotExpr(
|
return Err(Box::new(EvalAltResult::ErrorDotExpr(
|
||||||
@ -1073,7 +1117,7 @@ impl Engine {
|
|||||||
};
|
};
|
||||||
|
|
||||||
self.eval_dot_index_chain_helper(
|
self.eval_dot_index_chain_helper(
|
||||||
state, &mut val, &x.1, idx_values, is_idx, x.2, level, new_val,
|
state, lib, &mut val, &x.1, idx_values, is_idx, x.2, level, new_val,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// xxx.idx_lhs[idx_expr] | xxx.dot_lhs.rhs
|
// xxx.idx_lhs[idx_expr] | xxx.dot_lhs.rhs
|
||||||
@ -1084,7 +1128,7 @@ impl Engine {
|
|||||||
let (mut val, updated) = if let Expr::Property(p) = &x.0 {
|
let (mut val, updated) = if let Expr::Property(p) = &x.0 {
|
||||||
let ((_, getter, _), _) = p.as_ref();
|
let ((_, getter, _), _) = p.as_ref();
|
||||||
let args = &mut args[..1];
|
let args = &mut args[..1];
|
||||||
self.exec_fn_call(state, getter, true, 0, args, is_ref, None, x.2, 0)?
|
self.exec_fn_call(state, lib, getter, true, 0, args, is_ref, None, x.2, 0)?
|
||||||
} else {
|
} else {
|
||||||
// Syntax error
|
// Syntax error
|
||||||
return Err(Box::new(EvalAltResult::ErrorDotExpr(
|
return Err(Box::new(EvalAltResult::ErrorDotExpr(
|
||||||
@ -1096,7 +1140,7 @@ impl Engine {
|
|||||||
let target = &mut val.into();
|
let target = &mut val.into();
|
||||||
|
|
||||||
let (result, may_be_changed) = self.eval_dot_index_chain_helper(
|
let (result, may_be_changed) = self.eval_dot_index_chain_helper(
|
||||||
state, target, &x.1, idx_values, is_idx, x.2, level, new_val,
|
state, lib, target, &x.1, idx_values, is_idx, x.2, level, new_val,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Feed the value back via a setter just in case it has been updated
|
// Feed the value back via a setter just in case it has been updated
|
||||||
@ -1105,12 +1149,14 @@ impl Engine {
|
|||||||
let ((_, _, setter), _) = p.as_ref();
|
let ((_, _, setter), _) = p.as_ref();
|
||||||
// Re-use args because the first &mut parameter will not be consumed
|
// Re-use args because the first &mut parameter will not be consumed
|
||||||
args[1] = val;
|
args[1] = val;
|
||||||
self.exec_fn_call(state, setter, true, 0, args, is_ref, None, x.2, 0)
|
self.exec_fn_call(
|
||||||
.or_else(|err| match *err {
|
state, lib, setter, true, 0, args, is_ref, None, x.2, 0,
|
||||||
// If there is no setter, no need to feed it back because the property is read-only
|
)
|
||||||
EvalAltResult::ErrorDotExpr(_, _) => Ok(Default::default()),
|
.or_else(|err| match *err {
|
||||||
err => Err(Box::new(err)),
|
// If there is no setter, no need to feed it back because the property is read-only
|
||||||
})?;
|
EvalAltResult::ErrorDotExpr(_, _) => Ok(Default::default()),
|
||||||
|
err => Err(Box::new(err)),
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1130,6 +1176,7 @@ impl Engine {
|
|||||||
&self,
|
&self,
|
||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
|
lib: &FunctionsLib,
|
||||||
dot_lhs: &Expr,
|
dot_lhs: &Expr,
|
||||||
dot_rhs: &Expr,
|
dot_rhs: &Expr,
|
||||||
is_index: bool,
|
is_index: bool,
|
||||||
@ -1139,7 +1186,7 @@ impl Engine {
|
|||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
let idx_values = &mut StaticVec::new();
|
let idx_values = &mut StaticVec::new();
|
||||||
|
|
||||||
self.eval_indexed_chain(scope, state, dot_rhs, idx_values, 0, level)?;
|
self.eval_indexed_chain(scope, state, lib, dot_rhs, idx_values, 0, level)?;
|
||||||
|
|
||||||
match dot_lhs {
|
match dot_lhs {
|
||||||
// id.??? or id[???]
|
// id.??? or id[???]
|
||||||
@ -1164,7 +1211,7 @@ impl Engine {
|
|||||||
|
|
||||||
let this_ptr = &mut target.into();
|
let this_ptr = &mut target.into();
|
||||||
self.eval_dot_index_chain_helper(
|
self.eval_dot_index_chain_helper(
|
||||||
state, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val,
|
state, lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val,
|
||||||
)
|
)
|
||||||
.map(|(v, _)| v)
|
.map(|(v, _)| v)
|
||||||
}
|
}
|
||||||
@ -1176,10 +1223,10 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
// {expr}.??? or {expr}[???]
|
// {expr}.??? or {expr}[???]
|
||||||
expr => {
|
expr => {
|
||||||
let val = self.eval_expr(scope, state, expr, level)?;
|
let val = self.eval_expr(scope, state, lib, expr, level)?;
|
||||||
let this_ptr = &mut val.into();
|
let this_ptr = &mut val.into();
|
||||||
self.eval_dot_index_chain_helper(
|
self.eval_dot_index_chain_helper(
|
||||||
state, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val,
|
state, lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val,
|
||||||
)
|
)
|
||||||
.map(|(v, _)| v)
|
.map(|(v, _)| v)
|
||||||
}
|
}
|
||||||
@ -1195,6 +1242,7 @@ impl Engine {
|
|||||||
&self,
|
&self,
|
||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
|
lib: &FunctionsLib,
|
||||||
expr: &Expr,
|
expr: &Expr,
|
||||||
idx_values: &mut StaticVec<Dynamic>,
|
idx_values: &mut StaticVec<Dynamic>,
|
||||||
size: usize,
|
size: usize,
|
||||||
@ -1206,7 +1254,7 @@ impl Engine {
|
|||||||
Expr::FnCall(x) if x.1.is_none() => {
|
Expr::FnCall(x) if x.1.is_none() => {
|
||||||
let arg_values =
|
let arg_values =
|
||||||
x.3.iter()
|
x.3.iter()
|
||||||
.map(|arg_expr| self.eval_expr(scope, state, arg_expr, level))
|
.map(|arg_expr| self.eval_expr(scope, state, lib, arg_expr, level))
|
||||||
.collect::<Result<StaticVec<Dynamic>, _>>()?;
|
.collect::<Result<StaticVec<Dynamic>, _>>()?;
|
||||||
|
|
||||||
idx_values.push(Dynamic::from(arg_values));
|
idx_values.push(Dynamic::from(arg_values));
|
||||||
@ -1217,15 +1265,15 @@ impl Engine {
|
|||||||
// Evaluate in left-to-right order
|
// Evaluate in left-to-right order
|
||||||
let lhs_val = match x.0 {
|
let lhs_val = match x.0 {
|
||||||
Expr::Property(_) => Default::default(), // Store a placeholder in case of a property
|
Expr::Property(_) => Default::default(), // Store a placeholder in case of a property
|
||||||
_ => self.eval_expr(scope, state, &x.0, level)?,
|
_ => self.eval_expr(scope, state, lib, &x.0, level)?,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Push in reverse order
|
// Push in reverse order
|
||||||
self.eval_indexed_chain(scope, state, &x.1, idx_values, size, level)?;
|
self.eval_indexed_chain(scope, state, lib, &x.1, idx_values, size, level)?;
|
||||||
|
|
||||||
idx_values.push(lhs_val);
|
idx_values.push(lhs_val);
|
||||||
}
|
}
|
||||||
_ => idx_values.push(self.eval_expr(scope, state, expr, level)?),
|
_ => idx_values.push(self.eval_expr(scope, state, lib, expr, level)?),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -1235,6 +1283,7 @@ impl Engine {
|
|||||||
fn get_indexed_mut<'a>(
|
fn get_indexed_mut<'a>(
|
||||||
&self,
|
&self,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
|
lib: &FunctionsLib,
|
||||||
val: &'a mut Dynamic,
|
val: &'a mut Dynamic,
|
||||||
is_ref: bool,
|
is_ref: bool,
|
||||||
mut idx: Dynamic,
|
mut idx: Dynamic,
|
||||||
@ -1305,9 +1354,10 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
|
let fn_name = FUNC_INDEXER;
|
||||||
let type_name = self.map_type_name(val.type_name());
|
let type_name = self.map_type_name(val.type_name());
|
||||||
let args = &mut [val, &mut idx];
|
let args = &mut [val, &mut idx];
|
||||||
self.exec_fn_call(state, FUNC_INDEXER, true, 0, args, is_ref, None, op_pos, 0)
|
self.exec_fn_call(state, lib, fn_name, true, 0, args, is_ref, None, op_pos, 0)
|
||||||
.map(|(v, _)| v.into())
|
.map(|(v, _)| v.into())
|
||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
Box::new(EvalAltResult::ErrorIndexingType(type_name.into(), op_pos))
|
Box::new(EvalAltResult::ErrorIndexingType(type_name.into(), op_pos))
|
||||||
@ -1321,14 +1371,15 @@ impl Engine {
|
|||||||
&self,
|
&self,
|
||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
|
lib: &FunctionsLib,
|
||||||
lhs: &Expr,
|
lhs: &Expr,
|
||||||
rhs: &Expr,
|
rhs: &Expr,
|
||||||
level: usize,
|
level: usize,
|
||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
self.inc_operations(state, rhs.position())?;
|
self.inc_operations(state, rhs.position())?;
|
||||||
|
|
||||||
let lhs_value = self.eval_expr(scope, state, lhs, level)?;
|
let lhs_value = self.eval_expr(scope, state, lib, lhs, level)?;
|
||||||
let rhs_value = self.eval_expr(scope, state, rhs, level)?;
|
let rhs_value = self.eval_expr(scope, state, lib, rhs, level)?;
|
||||||
|
|
||||||
match rhs_value {
|
match rhs_value {
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
@ -1347,8 +1398,9 @@ impl Engine {
|
|||||||
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()));
|
||||||
let hashes = (hash_fn, 0);
|
let hashes = (hash_fn, 0);
|
||||||
|
|
||||||
let (r, _) = self
|
let (r, _) = self.call_fn_raw(
|
||||||
.call_fn_raw(None, state, op, hashes, args, false, def_value, pos, level)?;
|
None, state, lib, op, hashes, args, false, def_value, pos, level,
|
||||||
|
)?;
|
||||||
if r.as_bool().unwrap_or(false) {
|
if r.as_bool().unwrap_or(false) {
|
||||||
return Ok(true.into());
|
return Ok(true.into());
|
||||||
}
|
}
|
||||||
@ -1378,6 +1430,7 @@ impl Engine {
|
|||||||
&self,
|
&self,
|
||||||
scope: &mut Scope,
|
scope: &mut Scope,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
|
lib: &FunctionsLib,
|
||||||
expr: &Expr,
|
expr: &Expr,
|
||||||
level: usize,
|
level: usize,
|
||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
@ -1399,12 +1452,12 @@ impl Engine {
|
|||||||
Expr::Property(_) => unreachable!(),
|
Expr::Property(_) => unreachable!(),
|
||||||
|
|
||||||
// Statement block
|
// Statement block
|
||||||
Expr::Stmt(stmt) => self.eval_stmt(scope, state, &stmt.0, level),
|
Expr::Stmt(stmt) => self.eval_stmt(scope, state, lib, &stmt.0, level),
|
||||||
|
|
||||||
// lhs = rhs
|
// lhs = rhs
|
||||||
Expr::Assignment(x) => {
|
Expr::Assignment(x) => {
|
||||||
let op_pos = x.2;
|
let op_pos = x.2;
|
||||||
let rhs_val = self.eval_expr(scope, state, &x.1, level)?;
|
let rhs_val = self.eval_expr(scope, state, lib, &x.1, level)?;
|
||||||
|
|
||||||
match &x.0 {
|
match &x.0 {
|
||||||
// name = rhs
|
// name = rhs
|
||||||
@ -1432,7 +1485,7 @@ impl Engine {
|
|||||||
Expr::Index(x) => {
|
Expr::Index(x) => {
|
||||||
let new_val = Some(rhs_val);
|
let new_val = Some(rhs_val);
|
||||||
self.eval_dot_index_chain(
|
self.eval_dot_index_chain(
|
||||||
scope, state, &x.0, &x.1, true, x.2, level, new_val,
|
scope, state, lib, &x.0, &x.1, true, x.2, level, new_val,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// dot_lhs.dot_rhs = rhs
|
// dot_lhs.dot_rhs = rhs
|
||||||
@ -1440,7 +1493,7 @@ impl Engine {
|
|||||||
Expr::Dot(x) => {
|
Expr::Dot(x) => {
|
||||||
let new_val = Some(rhs_val);
|
let new_val = Some(rhs_val);
|
||||||
self.eval_dot_index_chain(
|
self.eval_dot_index_chain(
|
||||||
scope, state, &x.0, &x.1, false, op_pos, level, new_val,
|
scope, state, lib, &x.0, &x.1, false, op_pos, level, new_val,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// Error assignment to constant
|
// Error assignment to constant
|
||||||
@ -1460,19 +1513,19 @@ impl Engine {
|
|||||||
// lhs[idx_expr]
|
// lhs[idx_expr]
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Expr::Index(x) => {
|
Expr::Index(x) => {
|
||||||
self.eval_dot_index_chain(scope, state, &x.0, &x.1, true, x.2, level, None)
|
self.eval_dot_index_chain(scope, state, lib, &x.0, &x.1, true, x.2, level, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
// lhs.dot_rhs
|
// lhs.dot_rhs
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
Expr::Dot(x) => {
|
Expr::Dot(x) => {
|
||||||
self.eval_dot_index_chain(scope, state, &x.0, &x.1, false, x.2, level, None)
|
self.eval_dot_index_chain(scope, state, lib, &x.0, &x.1, false, x.2, level, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Expr::Array(x) => Ok(Dynamic(Union::Array(Box::new(
|
Expr::Array(x) => Ok(Dynamic(Union::Array(Box::new(
|
||||||
x.0.iter()
|
x.0.iter()
|
||||||
.map(|item| self.eval_expr(scope, state, item, level))
|
.map(|item| self.eval_expr(scope, state, lib, item, level))
|
||||||
.collect::<Result<Vec<_>, _>>()?,
|
.collect::<Result<Vec<_>, _>>()?,
|
||||||
)))),
|
)))),
|
||||||
|
|
||||||
@ -1480,7 +1533,7 @@ impl Engine {
|
|||||||
Expr::Map(x) => Ok(Dynamic(Union::Map(Box::new(
|
Expr::Map(x) => Ok(Dynamic(Union::Map(Box::new(
|
||||||
x.0.iter()
|
x.0.iter()
|
||||||
.map(|((key, _), expr)| {
|
.map(|((key, _), expr)| {
|
||||||
self.eval_expr(scope, state, expr, level)
|
self.eval_expr(scope, state, lib, expr, level)
|
||||||
.map(|val| (key.clone(), val))
|
.map(|val| (key.clone(), val))
|
||||||
})
|
})
|
||||||
.collect::<Result<HashMap<_, _>, _>>()?,
|
.collect::<Result<HashMap<_, _>, _>>()?,
|
||||||
@ -1493,7 +1546,7 @@ impl Engine {
|
|||||||
|
|
||||||
let mut arg_values = args_expr
|
let mut arg_values = args_expr
|
||||||
.iter()
|
.iter()
|
||||||
.map(|expr| self.eval_expr(scope, state, expr, level))
|
.map(|expr| self.eval_expr(scope, state, lib, expr, level))
|
||||||
.collect::<Result<StaticVec<_>, _>>()?;
|
.collect::<Result<StaticVec<_>, _>>()?;
|
||||||
|
|
||||||
let mut args: StaticVec<_> = arg_values.iter_mut().collect();
|
let mut args: StaticVec<_> = arg_values.iter_mut().collect();
|
||||||
@ -1501,13 +1554,13 @@ impl Engine {
|
|||||||
if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::<String>() {
|
if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::<String>() {
|
||||||
let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::<String>()));
|
let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::<String>()));
|
||||||
|
|
||||||
if !self.has_override(state, (hash_fn, *hash)) {
|
if !self.has_override(lib, (hash_fn, *hash)) {
|
||||||
// eval - only in function call style
|
// eval - only in function call style
|
||||||
let prev_len = scope.len();
|
let prev_len = scope.len();
|
||||||
let pos = args_expr.get(0).position();
|
let pos = args_expr.get(0).position();
|
||||||
|
|
||||||
// Evaluate the text string as a script
|
// Evaluate the text string as a script
|
||||||
let result = self.eval_script_expr(scope, state, args.pop(), pos);
|
let result = self.eval_script_expr(scope, state, lib, args.pop(), pos);
|
||||||
|
|
||||||
if scope.len() != prev_len {
|
if scope.len() != prev_len {
|
||||||
// IMPORTANT! If the eval defines new variables in the current scope,
|
// IMPORTANT! If the eval defines new variables in the current scope,
|
||||||
@ -1522,7 +1575,7 @@ impl Engine {
|
|||||||
// Normal function call - except for eval (handled above)
|
// Normal function call - except for eval (handled above)
|
||||||
let args = args.as_mut();
|
let args = args.as_mut();
|
||||||
self.exec_fn_call(
|
self.exec_fn_call(
|
||||||
state, name, *native, *hash, args, false, def_val, *pos, level,
|
state, lib, name, *native, *hash, args, false, def_val, *pos, level,
|
||||||
)
|
)
|
||||||
.map(|(v, _)| v)
|
.map(|(v, _)| v)
|
||||||
}
|
}
|
||||||
@ -1535,7 +1588,7 @@ impl Engine {
|
|||||||
|
|
||||||
let mut arg_values = args_expr
|
let mut arg_values = args_expr
|
||||||
.iter()
|
.iter()
|
||||||
.map(|expr| self.eval_expr(scope, state, expr, level))
|
.map(|expr| self.eval_expr(scope, state, lib, expr, level))
|
||||||
.collect::<Result<StaticVec<_>, _>>()?;
|
.collect::<Result<StaticVec<_>, _>>()?;
|
||||||
|
|
||||||
let mut args: StaticVec<_> = arg_values.iter_mut().collect();
|
let mut args: StaticVec<_> = arg_values.iter_mut().collect();
|
||||||
@ -1579,9 +1632,8 @@ impl Engine {
|
|||||||
Ok(x) if x.is_script() => {
|
Ok(x) if x.is_script() => {
|
||||||
let args = args.as_mut();
|
let args = args.as_mut();
|
||||||
let fn_def = x.get_fn_def();
|
let fn_def = x.get_fn_def();
|
||||||
let (result, state2) =
|
let result =
|
||||||
self.call_script_fn(None, *state, name, fn_def, args, *pos, level)?;
|
self.call_script_fn(None, state, lib, name, fn_def, args, *pos, level)?;
|
||||||
*state = state2;
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
Ok(x) => x.get_native_fn()(args.as_mut()).map_err(|err| err.new_position(*pos)),
|
Ok(x) => x.get_native_fn()(args.as_mut()).map_err(|err| err.new_position(*pos)),
|
||||||
@ -1595,19 +1647,19 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Expr::In(x) => self.eval_in_expr(scope, state, &x.0, &x.1, level),
|
Expr::In(x) => self.eval_in_expr(scope, state, lib, &x.0, &x.1, level),
|
||||||
|
|
||||||
Expr::And(x) => {
|
Expr::And(x) => {
|
||||||
let (lhs, rhs, _) = x.as_ref();
|
let (lhs, rhs, _) = x.as_ref();
|
||||||
Ok((self
|
Ok((self
|
||||||
.eval_expr(scope, state, lhs, level)?
|
.eval_expr(scope, state, lib, lhs, level)?
|
||||||
.as_bool()
|
.as_bool()
|
||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
EvalAltResult::ErrorBooleanArgMismatch("AND".into(), lhs.position())
|
EvalAltResult::ErrorBooleanArgMismatch("AND".into(), lhs.position())
|
||||||
})?
|
})?
|
||||||
&& // Short-circuit using &&
|
&& // Short-circuit using &&
|
||||||
self
|
self
|
||||||
.eval_expr(scope, state, rhs, level)?
|
.eval_expr(scope, state, lib, rhs, level)?
|
||||||
.as_bool()
|
.as_bool()
|
||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
EvalAltResult::ErrorBooleanArgMismatch("AND".into(), rhs.position())
|
EvalAltResult::ErrorBooleanArgMismatch("AND".into(), rhs.position())
|
||||||
@ -1618,14 +1670,14 @@ impl Engine {
|
|||||||
Expr::Or(x) => {
|
Expr::Or(x) => {
|
||||||
let (lhs, rhs, _) = x.as_ref();
|
let (lhs, rhs, _) = x.as_ref();
|
||||||
Ok((self
|
Ok((self
|
||||||
.eval_expr(scope, state, lhs, level)?
|
.eval_expr(scope, state, lib, lhs, level)?
|
||||||
.as_bool()
|
.as_bool()
|
||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
EvalAltResult::ErrorBooleanArgMismatch("OR".into(), lhs.position())
|
EvalAltResult::ErrorBooleanArgMismatch("OR".into(), lhs.position())
|
||||||
})?
|
})?
|
||||||
|| // Short-circuit using ||
|
|| // Short-circuit using ||
|
||||||
self
|
self
|
||||||
.eval_expr(scope, state, rhs, level)?
|
.eval_expr(scope, state, lib, rhs, level)?
|
||||||
.as_bool()
|
.as_bool()
|
||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
EvalAltResult::ErrorBooleanArgMismatch("OR".into(), rhs.position())
|
EvalAltResult::ErrorBooleanArgMismatch("OR".into(), rhs.position())
|
||||||
@ -1646,6 +1698,7 @@ impl Engine {
|
|||||||
&self,
|
&self,
|
||||||
scope: &mut Scope<'s>,
|
scope: &mut Scope<'s>,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
|
lib: &FunctionsLib,
|
||||||
stmt: &Stmt,
|
stmt: &Stmt,
|
||||||
level: usize,
|
level: usize,
|
||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
@ -1657,7 +1710,7 @@ impl Engine {
|
|||||||
|
|
||||||
// Expression as statement
|
// Expression as statement
|
||||||
Stmt::Expr(expr) => {
|
Stmt::Expr(expr) => {
|
||||||
let result = self.eval_expr(scope, state, expr, level)?;
|
let result = self.eval_expr(scope, state, lib, expr, level)?;
|
||||||
|
|
||||||
Ok(match expr.as_ref() {
|
Ok(match expr.as_ref() {
|
||||||
// If it is an assignment, erase the result at the root
|
// If it is an assignment, erase the result at the root
|
||||||
@ -1672,7 +1725,7 @@ impl Engine {
|
|||||||
state.scope_level += 1;
|
state.scope_level += 1;
|
||||||
|
|
||||||
let result = x.0.iter().try_fold(Default::default(), |_, stmt| {
|
let result = x.0.iter().try_fold(Default::default(), |_, stmt| {
|
||||||
self.eval_stmt(scope, state, stmt, level)
|
self.eval_stmt(scope, state, lib, stmt, level)
|
||||||
});
|
});
|
||||||
|
|
||||||
scope.rewind(prev_len);
|
scope.rewind(prev_len);
|
||||||
@ -1689,14 +1742,14 @@ impl Engine {
|
|||||||
Stmt::IfThenElse(x) => {
|
Stmt::IfThenElse(x) => {
|
||||||
let (expr, if_block, else_block) = x.as_ref();
|
let (expr, if_block, else_block) = x.as_ref();
|
||||||
|
|
||||||
self.eval_expr(scope, state, expr, level)?
|
self.eval_expr(scope, state, lib, expr, level)?
|
||||||
.as_bool()
|
.as_bool()
|
||||||
.map_err(|_| Box::new(EvalAltResult::ErrorLogicGuard(expr.position())))
|
.map_err(|_| Box::new(EvalAltResult::ErrorLogicGuard(expr.position())))
|
||||||
.and_then(|guard_val| {
|
.and_then(|guard_val| {
|
||||||
if guard_val {
|
if guard_val {
|
||||||
self.eval_stmt(scope, state, if_block, level)
|
self.eval_stmt(scope, state, lib, if_block, level)
|
||||||
} else if let Some(stmt) = else_block {
|
} else if let Some(stmt) = else_block {
|
||||||
self.eval_stmt(scope, state, stmt, level)
|
self.eval_stmt(scope, state, lib, stmt, level)
|
||||||
} else {
|
} else {
|
||||||
Ok(Default::default())
|
Ok(Default::default())
|
||||||
}
|
}
|
||||||
@ -1707,8 +1760,8 @@ impl Engine {
|
|||||||
Stmt::While(x) => loop {
|
Stmt::While(x) => loop {
|
||||||
let (expr, body) = x.as_ref();
|
let (expr, body) = x.as_ref();
|
||||||
|
|
||||||
match self.eval_expr(scope, state, expr, level)?.as_bool() {
|
match self.eval_expr(scope, state, lib, expr, level)?.as_bool() {
|
||||||
Ok(true) => match self.eval_stmt(scope, state, body, level) {
|
Ok(true) => match self.eval_stmt(scope, state, lib, body, level) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => match *err {
|
Err(err) => match *err {
|
||||||
EvalAltResult::ErrorLoopBreak(false, _) => (),
|
EvalAltResult::ErrorLoopBreak(false, _) => (),
|
||||||
@ -1725,7 +1778,7 @@ impl Engine {
|
|||||||
|
|
||||||
// Loop statement
|
// Loop statement
|
||||||
Stmt::Loop(body) => loop {
|
Stmt::Loop(body) => loop {
|
||||||
match self.eval_stmt(scope, state, body, level) {
|
match self.eval_stmt(scope, state, lib, body, level) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => match *err {
|
Err(err) => match *err {
|
||||||
EvalAltResult::ErrorLoopBreak(false, _) => (),
|
EvalAltResult::ErrorLoopBreak(false, _) => (),
|
||||||
@ -1738,7 +1791,7 @@ impl Engine {
|
|||||||
// For loop
|
// For loop
|
||||||
Stmt::For(x) => {
|
Stmt::For(x) => {
|
||||||
let (name, expr, stmt) = x.as_ref();
|
let (name, expr, stmt) = x.as_ref();
|
||||||
let iter_type = self.eval_expr(scope, state, expr, level)?;
|
let iter_type = self.eval_expr(scope, state, lib, expr, level)?;
|
||||||
let tid = iter_type.type_id();
|
let tid = iter_type.type_id();
|
||||||
|
|
||||||
if let Some(func) = self
|
if let Some(func) = self
|
||||||
@ -1756,7 +1809,7 @@ impl Engine {
|
|||||||
*scope.get_mut(index).0 = loop_var;
|
*scope.get_mut(index).0 = loop_var;
|
||||||
self.inc_operations(state, stmt.position())?;
|
self.inc_operations(state, stmt.position())?;
|
||||||
|
|
||||||
match self.eval_stmt(scope, state, stmt, level) {
|
match self.eval_stmt(scope, state, lib, stmt, level) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => match *err {
|
Err(err) => match *err {
|
||||||
EvalAltResult::ErrorLoopBreak(false, _) => (),
|
EvalAltResult::ErrorLoopBreak(false, _) => (),
|
||||||
@ -1783,7 +1836,7 @@ impl Engine {
|
|||||||
// Return value
|
// Return value
|
||||||
Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Return => {
|
Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Return => {
|
||||||
Err(Box::new(EvalAltResult::Return(
|
Err(Box::new(EvalAltResult::Return(
|
||||||
self.eval_expr(scope, state, x.1.as_ref().unwrap(), level)?,
|
self.eval_expr(scope, state, lib, x.1.as_ref().unwrap(), level)?,
|
||||||
(x.0).1,
|
(x.0).1,
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
@ -1795,7 +1848,7 @@ impl Engine {
|
|||||||
|
|
||||||
// Throw value
|
// Throw value
|
||||||
Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Exception => {
|
Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Exception => {
|
||||||
let val = self.eval_expr(scope, state, x.1.as_ref().unwrap(), level)?;
|
let val = self.eval_expr(scope, state, lib, x.1.as_ref().unwrap(), level)?;
|
||||||
Err(Box::new(EvalAltResult::ErrorRuntime(
|
Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||||
val.take_string().unwrap_or_else(|_| "".into()),
|
val.take_string().unwrap_or_else(|_| "".into()),
|
||||||
(x.0).1,
|
(x.0).1,
|
||||||
@ -1812,7 +1865,7 @@ impl Engine {
|
|||||||
// Let statement
|
// Let statement
|
||||||
Stmt::Let(x) if x.1.is_some() => {
|
Stmt::Let(x) if x.1.is_some() => {
|
||||||
let ((var_name, _), expr) = x.as_ref();
|
let ((var_name, _), expr) = x.as_ref();
|
||||||
let val = self.eval_expr(scope, state, expr.as_ref().unwrap(), level)?;
|
let val = self.eval_expr(scope, state, lib, expr.as_ref().unwrap(), level)?;
|
||||||
let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state);
|
let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state);
|
||||||
scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false);
|
scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false);
|
||||||
Ok(Default::default())
|
Ok(Default::default())
|
||||||
@ -1828,7 +1881,7 @@ impl Engine {
|
|||||||
// Const statement
|
// Const statement
|
||||||
Stmt::Const(x) if x.1.is_constant() => {
|
Stmt::Const(x) if x.1.is_constant() => {
|
||||||
let ((var_name, _), expr) = x.as_ref();
|
let ((var_name, _), expr) = x.as_ref();
|
||||||
let val = self.eval_expr(scope, state, &expr, level)?;
|
let val = self.eval_expr(scope, state, lib, &expr, level)?;
|
||||||
let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state);
|
let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state);
|
||||||
scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true);
|
scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true);
|
||||||
Ok(Default::default())
|
Ok(Default::default())
|
||||||
@ -1852,7 +1905,7 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(path) = self
|
if let Some(path) = self
|
||||||
.eval_expr(scope, state, &expr, level)?
|
.eval_expr(scope, state, lib, &expr, level)?
|
||||||
.try_cast::<String>()
|
.try_cast::<String>()
|
||||||
{
|
{
|
||||||
if let Some(resolver) = &self.module_resolver {
|
if let Some(resolver) = &self.module_resolver {
|
||||||
@ -1945,7 +1998,11 @@ fn run_builtin_binary_op(
|
|||||||
) -> Result<Option<Dynamic>, Box<EvalAltResult>> {
|
) -> Result<Option<Dynamic>, Box<EvalAltResult>> {
|
||||||
use crate::packages::arithmetic::*;
|
use crate::packages::arithmetic::*;
|
||||||
|
|
||||||
if x.type_id() == TypeId::of::<INT>() {
|
let args_type = x.type_id();
|
||||||
|
|
||||||
|
assert_eq!(y.type_id(), args_type);
|
||||||
|
|
||||||
|
if args_type == TypeId::of::<INT>() {
|
||||||
let x = x.downcast_ref::<INT>().unwrap().clone();
|
let x = x.downcast_ref::<INT>().unwrap().clone();
|
||||||
let y = y.downcast_ref::<INT>().unwrap().clone();
|
let y = y.downcast_ref::<INT>().unwrap().clone();
|
||||||
|
|
||||||
@ -1987,7 +2044,7 @@ fn run_builtin_binary_op(
|
|||||||
"^" => return Ok(Some((x ^ y).into())),
|
"^" => return Ok(Some((x ^ y).into())),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
} else if x.type_id() == TypeId::of::<bool>() {
|
} else if args_type == TypeId::of::<bool>() {
|
||||||
let x = x.downcast_ref::<bool>().unwrap().clone();
|
let x = x.downcast_ref::<bool>().unwrap().clone();
|
||||||
let y = y.downcast_ref::<bool>().unwrap().clone();
|
let y = y.downcast_ref::<bool>().unwrap().clone();
|
||||||
|
|
||||||
@ -1998,7 +2055,7 @@ fn run_builtin_binary_op(
|
|||||||
"!=" => return Ok(Some((x != y).into())),
|
"!=" => return Ok(Some((x != y).into())),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
} else if x.type_id() == TypeId::of::<String>() {
|
} else if args_type == TypeId::of::<String>() {
|
||||||
let x = x.downcast_ref::<String>().unwrap();
|
let x = x.downcast_ref::<String>().unwrap();
|
||||||
let y = y.downcast_ref::<String>().unwrap();
|
let y = y.downcast_ref::<String>().unwrap();
|
||||||
|
|
||||||
@ -2011,7 +2068,7 @@ fn run_builtin_binary_op(
|
|||||||
"<=" => return Ok(Some((x <= y).into())),
|
"<=" => return Ok(Some((x <= y).into())),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
} else if x.type_id() == TypeId::of::<char>() {
|
} else if args_type == TypeId::of::<char>() {
|
||||||
let x = x.downcast_ref::<char>().unwrap().clone();
|
let x = x.downcast_ref::<char>().unwrap().clone();
|
||||||
let y = y.downcast_ref::<char>().unwrap().clone();
|
let y = y.downcast_ref::<char>().unwrap().clone();
|
||||||
|
|
||||||
@ -2024,7 +2081,7 @@ fn run_builtin_binary_op(
|
|||||||
"<=" => return Ok(Some((x <= y).into())),
|
"<=" => return Ok(Some((x <= y).into())),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
} else if x.type_id() == TypeId::of::<()>() {
|
} else if args_type == TypeId::of::<()>() {
|
||||||
match op {
|
match op {
|
||||||
"==" => return Ok(Some(true.into())),
|
"==" => return Ok(Some(true.into())),
|
||||||
"!=" | ">" | ">=" | "<" | "<=" => return Ok(Some(false.into())),
|
"!=" | ">" | ">=" | "<" | "<=" => return Ok(Some(false.into())),
|
||||||
@ -2034,7 +2091,7 @@ fn run_builtin_binary_op(
|
|||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
{
|
{
|
||||||
if x.type_id() == TypeId::of::<FLOAT>() {
|
if args_type == TypeId::of::<FLOAT>() {
|
||||||
let x = x.downcast_ref::<FLOAT>().unwrap().clone();
|
let x = x.downcast_ref::<FLOAT>().unwrap().clone();
|
||||||
let y = y.downcast_ref::<FLOAT>().unwrap().clone();
|
let y = y.downcast_ref::<FLOAT>().unwrap().clone();
|
||||||
|
|
||||||
|
@ -138,12 +138,6 @@ pub fn map_dynamic<T: Variant + Clone>(data: T) -> Result<Dynamic, Box<EvalAltRe
|
|||||||
Ok(data.into_dynamic())
|
Ok(data.into_dynamic())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// To Dynamic mapping function.
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn map_identity(data: Dynamic) -> Result<Dynamic, Box<EvalAltResult>> {
|
|
||||||
Ok(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// To Dynamic mapping function.
|
/// To Dynamic mapping function.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn map_result(
|
pub fn map_result(
|
||||||
|
@ -50,7 +50,7 @@ pub struct Module {
|
|||||||
functions: HashMap<u64, (String, FnAccess, StaticVec<TypeId>, CallableFunction)>,
|
functions: HashMap<u64, (String, FnAccess, StaticVec<TypeId>, CallableFunction)>,
|
||||||
|
|
||||||
/// Script-defined functions.
|
/// Script-defined functions.
|
||||||
fn_lib: FunctionsLib,
|
lib: FunctionsLib,
|
||||||
|
|
||||||
/// Iterator functions, keyed by the type producing the iterator.
|
/// Iterator functions, keyed by the type producing the iterator.
|
||||||
type_iterators: HashMap<TypeId, IteratorFn>,
|
type_iterators: HashMap<TypeId, IteratorFn>,
|
||||||
@ -67,7 +67,7 @@ impl fmt::Debug for Module {
|
|||||||
"<module {:?}, functions={}, lib={}>",
|
"<module {:?}, functions={}, lib={}>",
|
||||||
self.variables,
|
self.variables,
|
||||||
self.functions.len(),
|
self.functions.len(),
|
||||||
self.fn_lib.len()
|
self.lib.len()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -614,7 +614,7 @@ impl Module {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
module.fn_lib = module.fn_lib.merge(ast.fn_lib());
|
module.lib = module.lib.merge(ast.lib());
|
||||||
|
|
||||||
Ok(module)
|
Ok(module)
|
||||||
}
|
}
|
||||||
@ -663,7 +663,7 @@ impl Module {
|
|||||||
functions.push((hash_fn_native, func.clone()));
|
functions.push((hash_fn_native, func.clone()));
|
||||||
}
|
}
|
||||||
// Index all script-defined functions
|
// Index all script-defined functions
|
||||||
for fn_def in module.fn_lib.values() {
|
for fn_def in module.lib.values() {
|
||||||
match fn_def.access {
|
match fn_def.access {
|
||||||
// Private functions are not exported
|
// Private functions are not exported
|
||||||
Private => continue,
|
Private => continue,
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
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, FunctionsLib, State as EngineState, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_PRINT,
|
Engine, FunctionsLib, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_PRINT,
|
||||||
KEYWORD_TYPE_OF,
|
KEYWORD_TYPE_OF,
|
||||||
};
|
};
|
||||||
use crate::fn_native::FnCallArgs;
|
|
||||||
use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST};
|
use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST};
|
||||||
use crate::result::EvalAltResult;
|
use crate::result::EvalAltResult;
|
||||||
use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope};
|
use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope};
|
||||||
@ -53,23 +52,19 @@ struct State<'a> {
|
|||||||
/// An `Engine` instance for eager function evaluation.
|
/// An `Engine` instance for eager function evaluation.
|
||||||
engine: &'a Engine,
|
engine: &'a Engine,
|
||||||
/// Library of script-defined functions.
|
/// Library of script-defined functions.
|
||||||
fn_lib: &'a [(&'a str, usize)],
|
lib: &'a FunctionsLib,
|
||||||
/// Optimization level.
|
/// Optimization level.
|
||||||
optimization_level: OptimizationLevel,
|
optimization_level: OptimizationLevel,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> State<'a> {
|
impl<'a> State<'a> {
|
||||||
/// Create a new State.
|
/// Create a new State.
|
||||||
pub fn new(
|
pub fn new(engine: &'a Engine, lib: &'a FunctionsLib, level: OptimizationLevel) -> Self {
|
||||||
engine: &'a Engine,
|
|
||||||
fn_lib: &'a [(&'a str, usize)],
|
|
||||||
level: OptimizationLevel,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
changed: false,
|
changed: false,
|
||||||
constants: vec![],
|
constants: vec![],
|
||||||
engine,
|
engine,
|
||||||
fn_lib,
|
lib,
|
||||||
optimization_level: level,
|
optimization_level: level,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -128,7 +123,8 @@ fn call_fn_with_constant_arguments(
|
|||||||
.engine
|
.engine
|
||||||
.call_fn_raw(
|
.call_fn_raw(
|
||||||
None,
|
None,
|
||||||
&mut EngineState::new(&Default::default()),
|
&mut Default::default(),
|
||||||
|
state.lib,
|
||||||
fn_name,
|
fn_name,
|
||||||
(hash_fn, 0),
|
(hash_fn, 0),
|
||||||
arg_values.iter_mut().collect::<StaticVec<_>>().as_mut(),
|
arg_values.iter_mut().collect::<StaticVec<_>>().as_mut(),
|
||||||
@ -557,7 +553,10 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr {
|
|||||||
|
|
||||||
// First search in script-defined functions (can override built-in)
|
// First search in script-defined functions (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)
|
||||||
if !*native_only && state.fn_lib.iter().find(|(id, len)| *id == name && (*len == args.len() || *len == args.len() + 1)).is_some() {
|
if !*native_only && state.lib.values().find(|f|
|
||||||
|
&f.name == name
|
||||||
|
&& (args.len()..=args.len() + 1).contains(&f.params.len())
|
||||||
|
).is_some() {
|
||||||
// A script-defined function overrides the built-in function - do not make the call
|
// A script-defined function overrides the built-in function - do not make the call
|
||||||
x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect();
|
x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect();
|
||||||
return Expr::FnCall(x);
|
return Expr::FnCall(x);
|
||||||
@ -615,11 +614,11 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn optimize<'a>(
|
fn optimize(
|
||||||
statements: Vec<Stmt>,
|
statements: Vec<Stmt>,
|
||||||
engine: &Engine,
|
engine: &Engine,
|
||||||
scope: &Scope,
|
scope: &Scope,
|
||||||
fn_lib: &'a [(&'a str, usize)],
|
lib: &FunctionsLib,
|
||||||
level: OptimizationLevel,
|
level: OptimizationLevel,
|
||||||
) -> Vec<Stmt> {
|
) -> Vec<Stmt> {
|
||||||
// If optimization level is None then skip optimizing
|
// If optimization level is None then skip optimizing
|
||||||
@ -628,7 +627,7 @@ fn optimize<'a>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set up the state
|
// Set up the state
|
||||||
let mut state = State::new(engine, fn_lib, level);
|
let mut state = State::new(engine, lib, level);
|
||||||
|
|
||||||
// Add constants from the scope into the state
|
// Add constants from the scope into the state
|
||||||
scope
|
scope
|
||||||
@ -713,41 +712,35 @@ pub fn optimize_into_ast(
|
|||||||
const level: OptimizationLevel = OptimizationLevel::None;
|
const level: OptimizationLevel = OptimizationLevel::None;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
let fn_lib_values: StaticVec<_> = functions
|
let lib = {
|
||||||
.iter()
|
|
||||||
.map(|fn_def| (fn_def.name.as_str(), fn_def.params.len()))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
let fn_lib = fn_lib_values.as_ref();
|
|
||||||
|
|
||||||
#[cfg(feature = "no_function")]
|
|
||||||
const fn_lib: &[(&str, usize)] = &[];
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
let lib = FunctionsLib::from_iter(functions.iter().cloned().map(|mut fn_def| {
|
|
||||||
if !level.is_none() {
|
if !level.is_none() {
|
||||||
let pos = fn_def.body.position();
|
let lib = FunctionsLib::from_iter(functions.iter().cloned());
|
||||||
|
|
||||||
// Optimize the function body
|
FunctionsLib::from_iter(functions.into_iter().map(|mut fn_def| {
|
||||||
let mut body = optimize(vec![fn_def.body], engine, &Scope::new(), fn_lib, level);
|
let pos = fn_def.body.position();
|
||||||
|
|
||||||
// {} -> Noop
|
// Optimize the function body
|
||||||
fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) {
|
let mut body = optimize(vec![fn_def.body], engine, &Scope::new(), &lib, level);
|
||||||
// { return val; } -> val
|
|
||||||
Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Return => {
|
// {} -> Noop
|
||||||
Stmt::Expr(Box::new(x.1.unwrap()))
|
fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) {
|
||||||
}
|
// { return val; } -> val
|
||||||
// { return; } -> ()
|
Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Return => {
|
||||||
Stmt::ReturnWithVal(x) if x.1.is_none() && (x.0).0 == ReturnType::Return => {
|
Stmt::Expr(Box::new(x.1.unwrap()))
|
||||||
Stmt::Expr(Box::new(Expr::Unit((x.0).1)))
|
}
|
||||||
}
|
// { return; } -> ()
|
||||||
// All others
|
Stmt::ReturnWithVal(x) if x.1.is_none() && (x.0).0 == ReturnType::Return => {
|
||||||
stmt => stmt,
|
Stmt::Expr(Box::new(Expr::Unit((x.0).1)))
|
||||||
};
|
}
|
||||||
|
// All others
|
||||||
|
stmt => stmt,
|
||||||
|
};
|
||||||
|
fn_def
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
FunctionsLib::from_iter(functions.into_iter())
|
||||||
}
|
}
|
||||||
fn_def
|
};
|
||||||
}));
|
|
||||||
|
|
||||||
#[cfg(feature = "no_function")]
|
#[cfg(feature = "no_function")]
|
||||||
let lib: FunctionsLib = Default::default();
|
let lib: FunctionsLib = Default::default();
|
||||||
@ -756,7 +749,7 @@ pub fn optimize_into_ast(
|
|||||||
match level {
|
match level {
|
||||||
OptimizationLevel::None => statements,
|
OptimizationLevel::None => statements,
|
||||||
OptimizationLevel::Simple | OptimizationLevel::Full => {
|
OptimizationLevel::Simple | OptimizationLevel::Full => {
|
||||||
optimize(statements, engine, &scope, fn_lib, level)
|
optimize(statements, engine, &scope, &lib, level)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
lib,
|
lib,
|
||||||
|
@ -63,8 +63,8 @@ pub struct AST(
|
|||||||
|
|
||||||
impl AST {
|
impl AST {
|
||||||
/// Create a new `AST`.
|
/// Create a new `AST`.
|
||||||
pub fn new(statements: Vec<Stmt>, fn_lib: FunctionsLib) -> Self {
|
pub fn new(statements: Vec<Stmt>, lib: FunctionsLib) -> Self {
|
||||||
Self(statements, fn_lib)
|
Self(statements, lib)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the statements.
|
/// Get the statements.
|
||||||
@ -78,7 +78,7 @@ impl AST {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get the script-defined functions.
|
/// Get the script-defined functions.
|
||||||
pub(crate) fn fn_lib(&self) -> &FunctionsLib {
|
pub(crate) fn lib(&self) -> &FunctionsLib {
|
||||||
&self.1
|
&self.1
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2585,10 +2585,10 @@ pub fn parse<'a>(
|
|||||||
) -> Result<AST, ParseError> {
|
) -> Result<AST, ParseError> {
|
||||||
let (statements, functions) = parse_global_level(input, max_expr_depth)?;
|
let (statements, functions) = parse_global_level(input, max_expr_depth)?;
|
||||||
|
|
||||||
let fn_lib = functions.into_iter().map(|(_, v)| v).collect();
|
let lib = functions.into_iter().map(|(_, v)| v).collect();
|
||||||
Ok(
|
Ok(
|
||||||
// Optimize AST
|
// Optimize AST
|
||||||
optimize_into_ast(engine, scope, statements, fn_lib, optimization_level),
|
optimize_into_ast(engine, scope, statements, lib, optimization_level),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user