2022-01-07 11:43:47 +08:00
|
|
|
//! Module defining functions for evaluating a statement.
|
|
|
|
|
2022-02-04 22:59:41 +08:00
|
|
|
use super::{EvalContext, EvalState, GlobalRuntimeState, Target};
|
2022-02-15 10:56:05 +08:00
|
|
|
use crate::api::events::VarDefInfo;
|
2022-01-28 10:11:40 +08:00
|
|
|
use crate::ast::{
|
2022-02-25 11:42:59 +08:00
|
|
|
ASTFlags, BinaryExpr, Expr, Ident, OpAssignment, Stmt, SwitchCases, TryCatchBlock,
|
2022-01-28 10:11:40 +08:00
|
|
|
};
|
2022-01-07 11:43:47 +08:00
|
|
|
use crate::func::get_hasher;
|
|
|
|
use crate::types::dynamic::{AccessMode, Union};
|
2022-01-07 12:19:01 +08:00
|
|
|
use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR, INT};
|
2022-01-15 11:26:43 +08:00
|
|
|
use std::hash::{Hash, Hasher};
|
2022-01-07 11:43:47 +08:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
|
|
|
|
|
|
|
impl Engine {
|
|
|
|
/// Evaluate a statements block.
|
2022-01-24 17:04:40 +08:00
|
|
|
//
|
|
|
|
// # Implementation Notes
|
|
|
|
//
|
|
|
|
// Do not use the `?` operator within the main body as it makes this function return early,
|
|
|
|
// possibly by-passing important cleanup tasks at the end.
|
|
|
|
//
|
|
|
|
// Errors that are not recoverable, such as system errors or safety errors, can use `?`.
|
2022-01-07 11:43:47 +08:00
|
|
|
pub(crate) fn eval_stmt_block(
|
|
|
|
&self,
|
|
|
|
scope: &mut Scope,
|
|
|
|
global: &mut GlobalRuntimeState,
|
|
|
|
state: &mut EvalState,
|
|
|
|
lib: &[&Module],
|
|
|
|
this_ptr: &mut Option<&mut Dynamic>,
|
|
|
|
statements: &[Stmt],
|
|
|
|
restore_orig_state: bool,
|
|
|
|
level: usize,
|
|
|
|
) -> RhaiResult {
|
|
|
|
if statements.is_empty() {
|
|
|
|
return Ok(Dynamic::UNIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
let orig_always_search_scope = state.always_search_scope;
|
|
|
|
let orig_scope_len = scope.len();
|
2022-01-29 11:09:43 +08:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-01-24 17:04:40 +08:00
|
|
|
let orig_imports_len = global.num_imports();
|
2022-01-07 11:43:47 +08:00
|
|
|
let orig_fn_resolution_caches_len = state.fn_resolution_caches_len();
|
|
|
|
|
|
|
|
if restore_orig_state {
|
|
|
|
state.scope_level += 1;
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:34:21 +08:00
|
|
|
let mut result = Ok(Dynamic::UNIT);
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
for stmt in statements {
|
2022-01-24 17:04:40 +08:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
let imports_len = global.num_imports();
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
result = self.eval_stmt(
|
|
|
|
scope,
|
|
|
|
global,
|
|
|
|
state,
|
|
|
|
lib,
|
|
|
|
this_ptr,
|
|
|
|
stmt,
|
|
|
|
restore_orig_state,
|
|
|
|
level,
|
2022-01-24 08:34:21 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if result.is_err() {
|
|
|
|
break;
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
if matches!(stmt, Stmt::Import(..)) {
|
2022-01-07 11:43:47 +08:00
|
|
|
// Get the extra modules - see if any functions are marked global.
|
|
|
|
// Without global functions, the extra modules never affect function resolution.
|
|
|
|
if global
|
2022-01-20 08:17:34 +08:00
|
|
|
.scan_imports_raw()
|
2022-01-24 17:04:40 +08:00
|
|
|
.skip(imports_len)
|
2022-02-08 09:02:15 +08:00
|
|
|
.any(|(.., m)| m.contains_indexed_global_functions())
|
2022-01-07 11:43:47 +08:00
|
|
|
{
|
|
|
|
if state.fn_resolution_caches_len() > orig_fn_resolution_caches_len {
|
|
|
|
// When new module is imported with global functions and there is already
|
|
|
|
// a new cache, clear it - notice that this is expensive as all function
|
|
|
|
// resolutions must start again
|
|
|
|
state.fn_resolution_cache_mut().clear();
|
|
|
|
} else if restore_orig_state {
|
|
|
|
// When new module is imported with global functions, push a new cache
|
|
|
|
state.push_fn_resolution_cache();
|
|
|
|
} else {
|
|
|
|
// When the block is to be evaluated in-place, just clear the current cache
|
|
|
|
state.fn_resolution_cache_mut().clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If imports list is modified, pop the functions lookup cache
|
|
|
|
state.rewind_fn_resolution_caches(orig_fn_resolution_caches_len);
|
|
|
|
|
|
|
|
if restore_orig_state {
|
|
|
|
scope.rewind(orig_scope_len);
|
|
|
|
state.scope_level -= 1;
|
2022-01-29 11:09:43 +08:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-01-24 17:04:40 +08:00
|
|
|
global.truncate_imports(orig_imports_len);
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
// The impact of new local variables goes away at the end of a block
|
|
|
|
// because any new variables introduced will go out of scope
|
|
|
|
state.always_search_scope = orig_always_search_scope;
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:34:21 +08:00
|
|
|
result
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate an op-assignment statement.
|
|
|
|
/// [`Position`] in [`EvalAltResult`] is [`NONE`][Position::NONE] and should be set afterwards.
|
|
|
|
pub(crate) fn eval_op_assignment(
|
|
|
|
&self,
|
|
|
|
global: &mut GlobalRuntimeState,
|
|
|
|
state: &mut EvalState,
|
|
|
|
lib: &[&Module],
|
|
|
|
op_info: Option<OpAssignment>,
|
|
|
|
op_pos: Position,
|
|
|
|
target: &mut Target,
|
|
|
|
root: (&str, Position),
|
|
|
|
new_val: Dynamic,
|
2022-02-02 14:47:35 +08:00
|
|
|
level: usize,
|
2022-01-07 11:43:47 +08:00
|
|
|
) -> RhaiResultOf<()> {
|
|
|
|
if target.is_read_only() {
|
|
|
|
// Assignment to constant variable
|
|
|
|
return Err(ERR::ErrorAssignmentToConstant(root.0.to_string(), root.1).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut new_val = new_val;
|
|
|
|
|
|
|
|
if let Some(OpAssignment {
|
|
|
|
hash_op_assign,
|
|
|
|
hash_op,
|
2022-01-28 08:28:31 +08:00
|
|
|
op_assign,
|
2022-01-07 11:43:47 +08:00
|
|
|
op,
|
|
|
|
}) = op_info
|
|
|
|
{
|
|
|
|
let mut lock_guard;
|
|
|
|
let lhs_ptr_inner;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
let target_is_shared = target.is_shared();
|
|
|
|
#[cfg(feature = "no_closure")]
|
|
|
|
let target_is_shared = false;
|
|
|
|
|
|
|
|
if target_is_shared {
|
|
|
|
lock_guard = target.write_lock::<Dynamic>().expect("`Dynamic`");
|
|
|
|
lhs_ptr_inner = &mut *lock_guard;
|
|
|
|
} else {
|
|
|
|
lhs_ptr_inner = &mut *target;
|
|
|
|
}
|
|
|
|
|
|
|
|
let hash = hash_op_assign;
|
|
|
|
let args = &mut [lhs_ptr_inner, &mut new_val];
|
2022-02-02 22:42:33 +08:00
|
|
|
let level = level + 1;
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-28 08:28:31 +08:00
|
|
|
match self.call_native_fn(
|
2022-02-02 14:47:35 +08:00
|
|
|
global, state, lib, op_assign, hash, args, true, true, op_pos, level,
|
2022-01-28 08:28:31 +08:00
|
|
|
) {
|
2022-01-09 12:44:24 +08:00
|
|
|
Ok(_) => {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
2022-01-28 08:28:31 +08:00
|
|
|
self.check_data_size(&args[0], root.1)?;
|
2022-01-09 12:44:24 +08:00
|
|
|
}
|
2022-02-08 09:02:15 +08:00
|
|
|
Err(err) if matches!(*err, ERR::ErrorFunctionNotFound(ref f, ..) if f.starts_with(op_assign)) =>
|
2022-01-07 11:43:47 +08:00
|
|
|
{
|
|
|
|
// Expand to `var = var op rhs`
|
2022-02-08 09:02:15 +08:00
|
|
|
let (value, ..) = self.call_native_fn(
|
2022-02-02 14:47:35 +08:00
|
|
|
global, state, lib, op, hash_op, args, true, false, op_pos, level,
|
2022-01-07 11:43:47 +08:00
|
|
|
)?;
|
|
|
|
|
2022-01-28 08:28:31 +08:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
self.check_data_size(&value, root.1)?;
|
|
|
|
|
2022-01-07 11:43:47 +08:00
|
|
|
*args[0] = value.flatten();
|
|
|
|
}
|
2022-01-08 23:23:43 +08:00
|
|
|
Err(err) => return Err(err),
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Normal assignment
|
|
|
|
*target.as_mut() = new_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
target.propagate_changed_value()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate a statement.
|
2022-01-24 17:04:40 +08:00
|
|
|
//
|
|
|
|
// # Implementation Notes
|
|
|
|
//
|
|
|
|
// Do not use the `?` operator within the main body as it makes this function return early,
|
|
|
|
// possibly by-passing important cleanup tasks at the end.
|
|
|
|
//
|
|
|
|
// Errors that are not recoverable, such as system errors or safety errors, can use `?`.
|
2022-01-07 11:43:47 +08:00
|
|
|
pub(crate) fn eval_stmt(
|
|
|
|
&self,
|
|
|
|
scope: &mut Scope,
|
|
|
|
global: &mut GlobalRuntimeState,
|
|
|
|
state: &mut EvalState,
|
|
|
|
lib: &[&Module],
|
|
|
|
this_ptr: &mut Option<&mut Dynamic>,
|
|
|
|
stmt: &Stmt,
|
|
|
|
rewind_scope: bool,
|
|
|
|
level: usize,
|
|
|
|
) -> RhaiResult {
|
2022-01-24 17:04:40 +08:00
|
|
|
#[cfg(feature = "debugging")]
|
2022-02-03 11:56:08 +08:00
|
|
|
let reset_debugger =
|
|
|
|
self.run_debugger_with_reset(scope, global, state, lib, this_ptr, stmt, level)?;
|
2022-01-24 17:04:40 +08:00
|
|
|
|
2022-01-07 11:43:47 +08:00
|
|
|
// Coded this way for better branch prediction.
|
|
|
|
// Popular branches are lifted out of the `match` statement into their own branches.
|
|
|
|
|
|
|
|
// Function calls should account for a relatively larger portion of statements.
|
2022-02-08 09:02:15 +08:00
|
|
|
if let Stmt::FnCall(x, ..) = stmt {
|
2022-01-07 11:43:47 +08:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
self.inc_operations(&mut global.num_operations, stmt.position())?;
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
let result =
|
2022-02-07 16:02:49 +08:00
|
|
|
self.eval_fn_call_expr(scope, global, state, lib, this_ptr, x, x.pos, level);
|
2022-01-24 17:04:40 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "debugging")]
|
2022-01-25 12:24:30 +08:00
|
|
|
global.debugger.reset_status(reset_debugger);
|
2022-01-24 17:04:40 +08:00
|
|
|
|
|
|
|
return result;
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then assignments.
|
|
|
|
// We shouldn't do this for too many variants because, soon or later, the added comparisons
|
|
|
|
// will cost more than the mis-predicted `match` branch.
|
|
|
|
if let Stmt::Assignment(x, op_pos) = stmt {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
self.inc_operations(&mut global.num_operations, stmt.position())?;
|
|
|
|
|
2022-01-28 10:11:40 +08:00
|
|
|
let result = if x.1.lhs.is_variable_access(false) {
|
|
|
|
let (op_info, BinaryExpr { lhs, rhs }) = x.as_ref();
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
let rhs_result = self
|
2022-01-28 10:11:40 +08:00
|
|
|
.eval_expr(scope, global, state, lib, this_ptr, rhs, level)
|
2022-01-24 17:04:40 +08:00
|
|
|
.map(Dynamic::flatten);
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
if let Ok(rhs_val) = rhs_result {
|
|
|
|
let search_result =
|
2022-02-04 22:59:41 +08:00
|
|
|
self.search_namespace(scope, global, state, lib, this_ptr, lhs, level);
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
if let Ok(search_val) = search_result {
|
|
|
|
let (mut lhs_ptr, pos) = search_val;
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-28 10:11:40 +08:00
|
|
|
let var_name = lhs.get_variable_name(false).expect("`Expr::Variable`");
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
if !lhs_ptr.is_ref() {
|
|
|
|
return Err(
|
|
|
|
ERR::ErrorAssignmentToConstant(var_name.to_string(), pos).into()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
self.inc_operations(&mut global.num_operations, pos)?;
|
|
|
|
|
2022-02-24 10:36:20 +08:00
|
|
|
let root = (var_name, pos);
|
|
|
|
let lhs_ptr = &mut lhs_ptr;
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
self.eval_op_assignment(
|
2022-02-24 10:36:20 +08:00
|
|
|
global, state, lib, *op_info, *op_pos, lhs_ptr, root, rhs_val, level,
|
2022-01-24 17:04:40 +08:00
|
|
|
)
|
2022-02-04 12:04:33 +08:00
|
|
|
.map_err(|err| err.fill_position(rhs.start_position()))
|
2022-01-24 17:04:40 +08:00
|
|
|
.map(|_| Dynamic::UNIT)
|
|
|
|
} else {
|
|
|
|
search_result.map(|_| Dynamic::UNIT)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rhs_result
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
} else {
|
2022-01-28 10:11:40 +08:00
|
|
|
let (op_info, BinaryExpr { lhs, rhs }) = x.as_ref();
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
let rhs_result = self
|
2022-01-28 10:11:40 +08:00
|
|
|
.eval_expr(scope, global, state, lib, this_ptr, rhs, level)
|
2022-01-24 17:04:40 +08:00
|
|
|
.map(Dynamic::flatten);
|
|
|
|
|
|
|
|
if let Ok(rhs_val) = rhs_result {
|
2022-02-04 12:04:33 +08:00
|
|
|
let _new_val = Some(((rhs_val, rhs.start_position()), (*op_info, *op_pos)));
|
2022-01-24 17:04:40 +08:00
|
|
|
|
|
|
|
// Must be either `var[index] op= val` or `var.prop op= val`
|
2022-01-28 10:11:40 +08:00
|
|
|
match lhs {
|
2022-01-24 17:04:40 +08:00
|
|
|
// name op= rhs (handled above)
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Variable(..) => {
|
2022-01-24 17:04:40 +08:00
|
|
|
unreachable!("Expr::Variable case is already handled")
|
|
|
|
}
|
|
|
|
// idx_lhs[idx_expr] op= rhs
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Index(..) => self
|
2022-01-24 17:04:40 +08:00
|
|
|
.eval_dot_index_chain(
|
2022-01-28 10:11:40 +08:00
|
|
|
scope, global, state, lib, this_ptr, lhs, level, _new_val,
|
2022-01-24 17:04:40 +08:00
|
|
|
)
|
|
|
|
.map(|_| Dynamic::UNIT),
|
|
|
|
// dot_lhs.dot_rhs op= rhs
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Expr::Dot(..) => self
|
2022-01-24 17:04:40 +08:00
|
|
|
.eval_dot_index_chain(
|
2022-01-28 10:11:40 +08:00
|
|
|
scope, global, state, lib, this_ptr, lhs, level, _new_val,
|
2022-01-24 17:04:40 +08:00
|
|
|
)
|
|
|
|
.map(|_| Dynamic::UNIT),
|
2022-01-28 10:11:40 +08:00
|
|
|
_ => unreachable!("cannot assign to expression: {:?}", lhs),
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
2022-01-24 17:04:40 +08:00
|
|
|
} else {
|
|
|
|
rhs_result
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
};
|
2022-01-24 17:04:40 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "debugging")]
|
2022-01-25 12:24:30 +08:00
|
|
|
global.debugger.reset_status(reset_debugger);
|
2022-01-24 17:04:40 +08:00
|
|
|
|
|
|
|
return result;
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
self.inc_operations(&mut global.num_operations, stmt.position())?;
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
let result = match stmt {
|
2022-01-07 11:43:47 +08:00
|
|
|
// No-op
|
2022-02-08 09:46:14 +08:00
|
|
|
Stmt::Noop(..) => Ok(Dynamic::UNIT),
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
// Expression as statement
|
2022-01-24 17:04:40 +08:00
|
|
|
Stmt::Expr(expr) => self
|
|
|
|
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
|
|
|
|
.map(Dynamic::flatten),
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
// Block scope
|
2022-02-08 09:02:15 +08:00
|
|
|
Stmt::Block(statements, ..) if statements.is_empty() => Ok(Dynamic::UNIT),
|
|
|
|
Stmt::Block(statements, ..) => {
|
2022-01-07 11:43:47 +08:00
|
|
|
self.eval_stmt_block(scope, global, state, lib, this_ptr, statements, true, level)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If statement
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::If(x, ..) => {
|
|
|
|
let (expr, if_block, else_block) = x.as_ref();
|
|
|
|
|
2022-01-07 11:43:47 +08:00
|
|
|
let guard_val = self
|
2022-01-24 17:04:40 +08:00
|
|
|
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
|
|
|
|
.and_then(|v| {
|
|
|
|
v.as_bool().map_err(|typ| {
|
|
|
|
self.make_type_mismatch_err::<bool>(typ, expr.position())
|
|
|
|
})
|
|
|
|
});
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
match guard_val {
|
|
|
|
Ok(true) => {
|
2022-02-16 17:51:14 +08:00
|
|
|
if !if_block.is_empty() {
|
2022-01-24 17:04:40 +08:00
|
|
|
self.eval_stmt_block(
|
2022-02-16 17:51:14 +08:00
|
|
|
scope, global, state, lib, this_ptr, if_block, true, level,
|
2022-01-24 17:04:40 +08:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Ok(Dynamic::UNIT)
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
2022-01-24 17:04:40 +08:00
|
|
|
Ok(false) => {
|
2022-02-16 17:51:14 +08:00
|
|
|
if !else_block.is_empty() {
|
2022-01-24 17:04:40 +08:00
|
|
|
self.eval_stmt_block(
|
2022-02-16 17:51:14 +08:00
|
|
|
scope, global, state, lib, this_ptr, else_block, true, level,
|
2022-01-24 17:04:40 +08:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Ok(Dynamic::UNIT)
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
2022-01-24 17:04:40 +08:00
|
|
|
err => err.map(Into::into),
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch statement
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Switch(x, ..) => {
|
|
|
|
let (
|
|
|
|
expr,
|
|
|
|
SwitchCases {
|
|
|
|
cases,
|
|
|
|
def_case,
|
|
|
|
ranges,
|
|
|
|
},
|
|
|
|
) = x.as_ref();
|
|
|
|
|
|
|
|
let value_result = self.eval_expr(scope, global, state, lib, this_ptr, expr, level);
|
2022-01-24 17:04:40 +08:00
|
|
|
|
|
|
|
if let Ok(value) = value_result {
|
|
|
|
let stmt_block_result = if value.is_hashable() {
|
|
|
|
let hasher = &mut get_hasher();
|
|
|
|
value.hash(hasher);
|
|
|
|
let hash = hasher.finish();
|
|
|
|
|
|
|
|
// First check hashes
|
2022-02-24 10:36:20 +08:00
|
|
|
if let Some(case_block) = cases.get(&hash) {
|
|
|
|
let cond_result = case_block
|
2022-01-28 10:11:40 +08:00
|
|
|
.condition
|
2022-01-24 17:04:40 +08:00
|
|
|
.as_ref()
|
|
|
|
.map(|cond| {
|
|
|
|
self.eval_expr(scope, global, state, lib, this_ptr, cond, level)
|
|
|
|
.and_then(|v| {
|
|
|
|
v.as_bool().map_err(|typ| {
|
|
|
|
self.make_type_mismatch_err::<bool>(
|
|
|
|
typ,
|
|
|
|
cond.position(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.unwrap_or(Ok(true));
|
|
|
|
|
|
|
|
match cond_result {
|
2022-02-24 10:36:20 +08:00
|
|
|
Ok(true) => Ok(Some(&case_block.statements)),
|
2022-01-24 17:04:40 +08:00
|
|
|
Ok(false) => Ok(None),
|
|
|
|
_ => cond_result.map(|_| None),
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
2022-01-24 17:04:40 +08:00
|
|
|
} else if value.is::<INT>() && !ranges.is_empty() {
|
|
|
|
// Then check integer ranges
|
|
|
|
let value = value.as_int().expect("`INT`");
|
|
|
|
let mut result = Ok(None);
|
|
|
|
|
2022-02-08 09:02:15 +08:00
|
|
|
for (.., block) in
|
|
|
|
ranges.iter().filter(|&&(start, end, inclusive, ..)| {
|
2022-01-24 17:04:40 +08:00
|
|
|
(!inclusive && (start..end).contains(&value))
|
|
|
|
|| (inclusive && (start..=end).contains(&value))
|
|
|
|
})
|
|
|
|
{
|
2022-01-28 10:11:40 +08:00
|
|
|
let cond_result = block
|
|
|
|
.condition
|
2022-01-24 17:04:40 +08:00
|
|
|
.as_ref()
|
|
|
|
.map(|cond| {
|
|
|
|
self.eval_expr(
|
|
|
|
scope, global, state, lib, this_ptr, cond, level,
|
|
|
|
)
|
|
|
|
.and_then(|v| {
|
|
|
|
v.as_bool().map_err(|typ| {
|
|
|
|
self.make_type_mismatch_err::<bool>(
|
|
|
|
typ,
|
|
|
|
cond.position(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.unwrap_or(Ok(true));
|
|
|
|
|
|
|
|
match cond_result {
|
2022-01-28 10:11:40 +08:00
|
|
|
Ok(true) => result = Ok(Some(&block.statements)),
|
2022-01-24 17:04:40 +08:00
|
|
|
Ok(false) => continue,
|
|
|
|
_ => result = cond_result.map(|_| None),
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
2022-01-24 17:04:40 +08:00
|
|
|
|
|
|
|
break;
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
result
|
|
|
|
} else {
|
|
|
|
// Nothing matches
|
|
|
|
Ok(None)
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-24 17:04:40 +08:00
|
|
|
// Non-hashable
|
|
|
|
Ok(None)
|
|
|
|
};
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
if let Ok(Some(statements)) = stmt_block_result {
|
|
|
|
if !statements.is_empty() {
|
|
|
|
self.eval_stmt_block(
|
|
|
|
scope, global, state, lib, this_ptr, statements, true, level,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Ok(Dynamic::UNIT)
|
|
|
|
}
|
|
|
|
} else if let Ok(None) = stmt_block_result {
|
|
|
|
// Default match clause
|
2022-01-28 10:11:40 +08:00
|
|
|
if !def_case.is_empty() {
|
2022-01-24 17:04:40 +08:00
|
|
|
self.eval_stmt_block(
|
2022-01-28 10:11:40 +08:00
|
|
|
scope, global, state, lib, this_ptr, def_case, true, level,
|
2022-01-24 17:04:40 +08:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Ok(Dynamic::UNIT)
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
} else {
|
2022-01-24 17:04:40 +08:00
|
|
|
stmt_block_result.map(|_| Dynamic::UNIT)
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-24 17:04:40 +08:00
|
|
|
value_result
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::While(x, ..) if matches!(x.0, Expr::Unit(..)) => loop {
|
|
|
|
let (.., body) = x.as_ref();
|
|
|
|
|
2022-01-07 11:43:47 +08:00
|
|
|
if !body.is_empty() {
|
|
|
|
match self
|
|
|
|
.eval_stmt_block(scope, global, state, lib, this_ptr, body, true, level)
|
|
|
|
{
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => match *err {
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::LoopBreak(false, ..) => (),
|
|
|
|
ERR::LoopBreak(true, ..) => break Ok(Dynamic::UNIT),
|
2022-01-24 17:04:40 +08:00
|
|
|
_ => break Err(err),
|
2022-01-07 11:43:47 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
self.inc_operations(&mut global.num_operations, body.position())?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// While loop
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::While(x, ..) => loop {
|
|
|
|
let (expr, body) = x.as_ref();
|
|
|
|
|
2022-01-07 11:43:47 +08:00
|
|
|
let condition = self
|
2022-01-24 17:04:40 +08:00
|
|
|
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
|
|
|
|
.and_then(|v| {
|
|
|
|
v.as_bool().map_err(|typ| {
|
|
|
|
self.make_type_mismatch_err::<bool>(typ, expr.position())
|
|
|
|
})
|
|
|
|
});
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
match condition {
|
|
|
|
Ok(false) => break Ok(Dynamic::UNIT),
|
|
|
|
Ok(true) if body.is_empty() => (),
|
|
|
|
Ok(true) => {
|
|
|
|
match self
|
|
|
|
.eval_stmt_block(scope, global, state, lib, this_ptr, body, true, level)
|
|
|
|
{
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => match *err {
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::LoopBreak(false, ..) => (),
|
|
|
|
ERR::LoopBreak(true, ..) => break Ok(Dynamic::UNIT),
|
2022-01-24 17:04:40 +08:00
|
|
|
_ => break Err(err),
|
|
|
|
},
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
2022-01-24 17:04:40 +08:00
|
|
|
err => break err.map(|_| Dynamic::UNIT),
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Do loop
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Do(x, options, ..) => loop {
|
|
|
|
let (expr, body) = x.as_ref();
|
2022-02-25 11:42:59 +08:00
|
|
|
let is_while = !options.contains(ASTFlags::NEGATED);
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
if !body.is_empty() {
|
|
|
|
match self
|
|
|
|
.eval_stmt_block(scope, global, state, lib, this_ptr, body, true, level)
|
|
|
|
{
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => match *err {
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::LoopBreak(false, ..) => continue,
|
|
|
|
ERR::LoopBreak(true, ..) => break Ok(Dynamic::UNIT),
|
2022-01-24 17:04:40 +08:00
|
|
|
_ => break Err(err),
|
2022-01-07 11:43:47 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let condition = self
|
2022-02-16 17:51:14 +08:00
|
|
|
.eval_expr(scope, global, state, lib, this_ptr, &expr, level)
|
2022-01-24 17:04:40 +08:00
|
|
|
.and_then(|v| {
|
|
|
|
v.as_bool().map_err(|typ| {
|
|
|
|
self.make_type_mismatch_err::<bool>(typ, expr.position())
|
|
|
|
})
|
|
|
|
});
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
match condition {
|
|
|
|
Ok(condition) if condition ^ is_while => break Ok(Dynamic::UNIT),
|
|
|
|
Ok(_) => (),
|
|
|
|
err => break err.map(|_| Dynamic::UNIT),
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// For loop
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::For(x, ..) => {
|
2022-03-05 17:57:23 +08:00
|
|
|
let (var_name, counter, expr, statements) = x.as_ref();
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
let iter_result = self
|
|
|
|
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
|
|
|
|
.map(Dynamic::flatten);
|
|
|
|
|
|
|
|
if let Ok(iter_obj) = iter_result {
|
|
|
|
let iter_type = iter_obj.type_id();
|
|
|
|
|
|
|
|
// lib should only contain scripts, so technically they cannot have iterators
|
|
|
|
|
|
|
|
// Search order:
|
|
|
|
// 1) Global namespace - functions registered via Engine::register_XXX
|
|
|
|
// 2) Global modules - packages
|
|
|
|
// 3) Imported modules - functions marked with global namespace
|
|
|
|
// 4) Global sub-modules - functions marked with global namespace
|
|
|
|
let func = self
|
|
|
|
.global_modules
|
|
|
|
.iter()
|
2022-01-29 13:37:58 +08:00
|
|
|
.find_map(|m| m.get_iter(iter_type));
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
let func = func.or_else(|| global.get_iter(iter_type)).or_else(|| {
|
|
|
|
self.global_sub_modules
|
|
|
|
.values()
|
|
|
|
.find_map(|m| m.get_qualified_iter(iter_type))
|
|
|
|
});
|
2022-01-15 10:18:16 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
if let Some(func) = func {
|
|
|
|
// Add the loop variables
|
|
|
|
let orig_scope_len = scope.len();
|
2022-03-05 17:57:23 +08:00
|
|
|
let counter_index = if !counter.is_empty() {
|
2022-01-24 17:04:40 +08:00
|
|
|
scope.push(counter.name.clone(), 0 as INT);
|
|
|
|
scope.len() - 1
|
|
|
|
} else {
|
|
|
|
usize::MAX
|
|
|
|
};
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-03-05 17:57:23 +08:00
|
|
|
scope.push(var_name.name.clone(), ());
|
2022-01-24 17:04:40 +08:00
|
|
|
let index = scope.len() - 1;
|
|
|
|
|
|
|
|
let mut loop_result = Ok(Dynamic::UNIT);
|
|
|
|
|
|
|
|
for (x, iter_value) in func(iter_obj).enumerate() {
|
|
|
|
// Increment counter
|
|
|
|
if counter_index < usize::MAX {
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if x > INT::MAX as usize {
|
|
|
|
loop_result = Err(ERR::ErrorArithmetic(
|
|
|
|
format!("for-loop counter overflow: {}", x),
|
2022-03-05 17:57:23 +08:00
|
|
|
counter.pos,
|
2022-01-24 17:04:40 +08:00
|
|
|
)
|
|
|
|
.into());
|
|
|
|
break;
|
|
|
|
}
|
2022-01-15 10:18:16 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
let index_value = (x as INT).into();
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
{
|
|
|
|
let index_var = scope.get_mut_by_index(counter_index);
|
|
|
|
if index_var.is_shared() {
|
|
|
|
*index_var.write_lock().expect("`Dynamic`") = index_value;
|
|
|
|
} else {
|
|
|
|
*index_var = index_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "no_closure")]
|
|
|
|
{
|
|
|
|
*scope.get_mut_by_index(counter_index) = index_value;
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
let value = iter_value.flatten();
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
{
|
2022-01-24 17:04:40 +08:00
|
|
|
let loop_var = scope.get_mut_by_index(index);
|
|
|
|
if loop_var.is_shared() {
|
|
|
|
*loop_var.write_lock().expect("`Dynamic`") = value;
|
2022-01-07 11:43:47 +08:00
|
|
|
} else {
|
2022-01-24 17:04:40 +08:00
|
|
|
*loop_var = value;
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "no_closure")]
|
|
|
|
{
|
2022-01-24 17:04:40 +08:00
|
|
|
*scope.get_mut_by_index(index) = value;
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if let Err(err) = self
|
|
|
|
.inc_operations(&mut global.num_operations, statements.position())
|
|
|
|
{
|
|
|
|
loop_result = Err(err);
|
|
|
|
break;
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
if statements.is_empty() {
|
|
|
|
continue;
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
let result = self.eval_stmt_block(
|
|
|
|
scope, global, state, lib, this_ptr, statements, true, level,
|
|
|
|
);
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
match result {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => match *err {
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::LoopBreak(false, ..) => (),
|
|
|
|
ERR::LoopBreak(true, ..) => break,
|
2022-01-24 17:04:40 +08:00
|
|
|
_ => {
|
|
|
|
loop_result = Err(err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
scope.rewind(orig_scope_len);
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
loop_result
|
|
|
|
} else {
|
2022-02-04 12:04:33 +08:00
|
|
|
Err(ERR::ErrorFor(expr.start_position()).into())
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-24 17:04:40 +08:00
|
|
|
iter_result
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue/Break statement
|
|
|
|
Stmt::BreakLoop(options, pos) => {
|
2022-02-25 11:42:59 +08:00
|
|
|
Err(ERR::LoopBreak(options.contains(ASTFlags::BREAK), *pos).into())
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try/Catch statement
|
2022-02-08 09:02:15 +08:00
|
|
|
Stmt::TryCatch(x, ..) => {
|
2022-01-28 10:11:40 +08:00
|
|
|
let TryCatchBlock {
|
|
|
|
try_block,
|
2022-03-05 17:57:23 +08:00
|
|
|
catch_var:
|
|
|
|
Ident {
|
|
|
|
name: catch_var, ..
|
|
|
|
},
|
2022-01-28 10:11:40 +08:00
|
|
|
catch_block,
|
|
|
|
} = x.as_ref();
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
let result = self
|
2022-01-28 10:11:40 +08:00
|
|
|
.eval_stmt_block(scope, global, state, lib, this_ptr, try_block, true, level)
|
2022-01-07 11:43:47 +08:00
|
|
|
.map(|_| Dynamic::UNIT);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => result,
|
|
|
|
Err(err) if err.is_pseudo_error() => Err(err),
|
|
|
|
Err(err) if !err.is_catchable() => Err(err),
|
|
|
|
Err(mut err) => {
|
2022-02-08 19:02:40 +08:00
|
|
|
let err_value = match err.unwrap_inner() {
|
|
|
|
ERR::ErrorRuntime(x, ..) => x.clone(),
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "no_object")]
|
|
|
|
_ => {
|
|
|
|
err.take_position();
|
|
|
|
err.to_string().into()
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
_ => {
|
|
|
|
let mut err_map = crate::Map::new();
|
|
|
|
let err_pos = err.take_position();
|
|
|
|
|
|
|
|
err_map.insert("message".into(), err.to_string().into());
|
|
|
|
|
|
|
|
if !global.source.is_empty() {
|
|
|
|
err_map.insert("source".into(), global.source.clone().into());
|
|
|
|
}
|
|
|
|
|
2022-01-25 14:32:07 +08:00
|
|
|
if !err_pos.is_none() {
|
|
|
|
err_map.insert(
|
|
|
|
"line".into(),
|
|
|
|
(err_pos.line().unwrap() as INT).into(),
|
|
|
|
);
|
|
|
|
err_map.insert(
|
|
|
|
"position".into(),
|
|
|
|
(err_pos.position().unwrap_or(0) as INT).into(),
|
|
|
|
);
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err.dump_fields(&mut err_map);
|
|
|
|
err_map.into()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let orig_scope_len = scope.len();
|
|
|
|
|
2022-03-05 17:57:23 +08:00
|
|
|
if !catch_var.is_empty() {
|
|
|
|
scope.push(catch_var.clone(), err_value);
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
let result = self.eval_stmt_block(
|
2022-01-28 10:11:40 +08:00
|
|
|
scope,
|
|
|
|
global,
|
|
|
|
state,
|
|
|
|
lib,
|
|
|
|
this_ptr,
|
|
|
|
catch_block,
|
|
|
|
true,
|
|
|
|
level,
|
2022-01-07 11:43:47 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
scope.rewind(orig_scope_len);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => Ok(Dynamic::UNIT),
|
|
|
|
Err(result_err) => match *result_err {
|
|
|
|
// Re-throw exception
|
2022-02-08 09:02:15 +08:00
|
|
|
ERR::ErrorRuntime(Dynamic(Union::Unit(..)), pos) => {
|
2022-01-07 11:43:47 +08:00
|
|
|
err.set_position(pos);
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
_ => Err(result_err),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throw value
|
2022-02-25 11:42:59 +08:00
|
|
|
Stmt::Return(Some(expr), options, pos) if options.contains(ASTFlags::BREAK) => self
|
2022-01-28 08:28:17 +08:00
|
|
|
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
|
|
|
|
.and_then(|v| Err(ERR::ErrorRuntime(v.flatten(), *pos).into())),
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
// Empty throw
|
2022-02-25 11:42:59 +08:00
|
|
|
Stmt::Return(None, options, pos) if options.contains(ASTFlags::BREAK) => {
|
2022-01-07 11:43:47 +08:00
|
|
|
Err(ERR::ErrorRuntime(Dynamic::UNIT, *pos).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return value
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Return(Some(expr), .., pos) => self
|
2022-01-24 17:04:40 +08:00
|
|
|
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
|
|
|
|
.and_then(|v| Err(ERR::Return(v.flatten(), *pos).into())),
|
2022-01-07 11:43:47 +08:00
|
|
|
|
|
|
|
// Empty return
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Return(None, .., pos) => Err(ERR::Return(Dynamic::UNIT, *pos).into()),
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-02-04 13:20:47 +08:00
|
|
|
// Let/const statement - shadowing disallowed
|
2022-03-05 17:57:23 +08:00
|
|
|
Stmt::Var(x, .., pos) if !self.allow_shadowing() && scope.contains(&x.0) => {
|
|
|
|
Err(ERR::ErrorVariableExists(x.0.to_string(), *pos).into())
|
2022-02-04 13:20:47 +08:00
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
// Let/const statement
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Var(x, options, pos) => {
|
2022-03-05 17:57:23 +08:00
|
|
|
let (var_name, expr, index) = x.as_ref();
|
2022-02-04 13:20:47 +08:00
|
|
|
|
2022-02-25 11:42:59 +08:00
|
|
|
let access = if options.contains(ASTFlags::CONSTANT) {
|
2022-01-07 11:43:47 +08:00
|
|
|
AccessMode::ReadOnly
|
|
|
|
} else {
|
|
|
|
AccessMode::ReadWrite
|
|
|
|
};
|
2022-02-25 11:42:59 +08:00
|
|
|
let export = options.contains(ASTFlags::EXPORTED);
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-02-18 15:04:46 +08:00
|
|
|
// Check variable definition filter
|
2022-02-04 22:59:41 +08:00
|
|
|
let result = if let Some(ref filter) = self.def_var_filter {
|
2022-02-15 10:56:05 +08:00
|
|
|
let will_shadow = scope.contains(var_name);
|
|
|
|
let nesting_level = state.scope_level;
|
2022-02-18 15:04:46 +08:00
|
|
|
let is_const = access == AccessMode::ReadOnly;
|
2022-02-15 10:56:05 +08:00
|
|
|
let info = VarDefInfo {
|
|
|
|
name: var_name,
|
|
|
|
is_const,
|
|
|
|
nesting_level,
|
|
|
|
will_shadow,
|
|
|
|
};
|
2022-02-04 22:59:41 +08:00
|
|
|
let context = EvalContext {
|
|
|
|
engine: self,
|
|
|
|
scope,
|
|
|
|
global,
|
|
|
|
state,
|
|
|
|
lib,
|
|
|
|
this_ptr,
|
2022-02-15 10:56:05 +08:00
|
|
|
level,
|
2022-02-04 22:59:41 +08:00
|
|
|
};
|
2022-01-24 17:04:40 +08:00
|
|
|
|
2022-02-15 10:56:05 +08:00
|
|
|
match filter(true, info, &context) {
|
2022-02-04 22:59:41 +08:00
|
|
|
Ok(true) => None,
|
2022-02-13 18:46:25 +08:00
|
|
|
Ok(false) => {
|
|
|
|
Some(Err(
|
|
|
|
ERR::ErrorForbiddenVariable(var_name.to_string(), *pos).into()
|
|
|
|
))
|
|
|
|
}
|
2022-02-04 22:59:41 +08:00
|
|
|
err @ Err(_) => Some(err),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(result) = result {
|
|
|
|
result.map(|_| Dynamic::UNIT)
|
|
|
|
} else {
|
2022-02-18 15:04:46 +08:00
|
|
|
// Evaluate initial value
|
2022-02-04 22:59:41 +08:00
|
|
|
let value_result = self
|
|
|
|
.eval_expr(scope, global, state, lib, this_ptr, expr, level)
|
|
|
|
.map(Dynamic::flatten);
|
|
|
|
|
2022-02-18 15:04:46 +08:00
|
|
|
if let Ok(mut value) = value_result {
|
2022-02-04 22:59:41 +08:00
|
|
|
let _alias = if !rewind_scope {
|
2022-02-18 15:04:46 +08:00
|
|
|
// Put global constants into global module
|
2022-02-04 22:59:41 +08:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
if state.scope_level == 0
|
2022-02-18 15:04:46 +08:00
|
|
|
&& access == AccessMode::ReadOnly
|
2022-02-04 22:59:41 +08:00
|
|
|
&& lib.iter().any(|&m| !m.is_empty())
|
|
|
|
{
|
|
|
|
if global.constants.is_none() {
|
|
|
|
global.constants = Some(crate::Shared::new(
|
|
|
|
crate::Locked::new(std::collections::BTreeMap::new()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
crate::func::locked_write(global.constants.as_ref().unwrap())
|
2022-03-05 17:57:23 +08:00
|
|
|
.insert(var_name.name.clone(), value.clone());
|
2022-01-28 22:07:49 +08:00
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-02-04 22:59:41 +08:00
|
|
|
if export {
|
|
|
|
Some(var_name)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else if export {
|
|
|
|
unreachable!("exported variable not on global level");
|
2022-01-24 17:04:40 +08:00
|
|
|
} else {
|
|
|
|
None
|
2022-02-04 22:59:41 +08:00
|
|
|
};
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-02-18 15:04:46 +08:00
|
|
|
if let Some(index) = index {
|
|
|
|
value.set_access_mode(access);
|
|
|
|
*scope.get_mut_by_index(scope.len() - index.get()) = value;
|
|
|
|
} else {
|
2022-03-05 17:57:23 +08:00
|
|
|
scope.push_entry(var_name.name.clone(), access, value);
|
2022-02-18 15:04:46 +08:00
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-02-04 22:59:41 +08:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
if let Some(alias) = _alias {
|
2022-03-09 09:25:32 +08:00
|
|
|
scope.add_alias_by_index(scope.len() - 1, alias.name.clone());
|
2022-02-04 22:59:41 +08:00
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-02-04 22:59:41 +08:00
|
|
|
Ok(Dynamic::UNIT)
|
|
|
|
} else {
|
|
|
|
value_result
|
|
|
|
}
|
2022-01-24 17:04:40 +08:00
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Import statement
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-02-16 17:51:14 +08:00
|
|
|
Stmt::Import(x, _pos) => {
|
|
|
|
let (expr, export) = x.as_ref();
|
|
|
|
|
2022-01-07 11:43:47 +08:00
|
|
|
// Guard against too many modules
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if global.num_modules_loaded >= self.max_modules() {
|
|
|
|
return Err(ERR::ErrorTooManyModules(*_pos).into());
|
|
|
|
}
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
let path_result = self
|
|
|
|
.eval_expr(scope, global, state, lib, this_ptr, &expr, level)
|
|
|
|
.and_then(|v| {
|
2022-02-04 12:04:33 +08:00
|
|
|
let typ = v.type_name();
|
2022-01-24 17:04:40 +08:00
|
|
|
v.try_cast::<crate::ImmutableString>().ok_or_else(|| {
|
|
|
|
self.make_type_mismatch_err::<crate::ImmutableString>(
|
2022-02-04 12:04:33 +08:00
|
|
|
typ,
|
2022-01-24 17:04:40 +08:00
|
|
|
expr.position(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Ok(path) = path_result {
|
2022-01-07 11:43:47 +08:00
|
|
|
use crate::ModuleResolver;
|
|
|
|
|
2022-02-04 12:04:33 +08:00
|
|
|
let path_pos = expr.start_position();
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-28 18:59:18 +08:00
|
|
|
let resolver = global.embedded_module_resolver.clone();
|
|
|
|
|
|
|
|
let module_result = resolver
|
2022-01-07 11:43:47 +08:00
|
|
|
.as_ref()
|
2022-01-28 18:59:18 +08:00
|
|
|
.and_then(|r| match r.resolve_raw(self, global, &path, path_pos) {
|
2022-02-08 09:02:15 +08:00
|
|
|
Err(err) if matches!(*err, ERR::ErrorModuleNotFound(..)) => None,
|
2022-01-07 11:43:47 +08:00
|
|
|
result => Some(result),
|
|
|
|
})
|
|
|
|
.or_else(|| {
|
|
|
|
self.module_resolver
|
|
|
|
.as_ref()
|
2022-01-28 18:59:18 +08:00
|
|
|
.map(|r| r.resolve_raw(self, global, &path, path_pos))
|
2022-01-07 11:43:47 +08:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
Err(ERR::ErrorModuleNotFound(path.to_string(), path_pos).into())
|
2022-01-24 17:04:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if let Ok(module) = module_result {
|
2022-03-05 17:57:23 +08:00
|
|
|
if !export.is_empty() {
|
2022-01-24 17:04:40 +08:00
|
|
|
if !module.is_indexed() {
|
|
|
|
// Index the module (making a clone copy if necessary) if it is not indexed
|
2022-03-05 17:57:23 +08:00
|
|
|
let mut m = crate::func::native::shared_take_or_clone(module);
|
|
|
|
m.build_index();
|
|
|
|
global.push_import(export.name.clone(), m);
|
2022-01-24 17:04:40 +08:00
|
|
|
} else {
|
2022-03-05 17:57:23 +08:00
|
|
|
global.push_import(export.name.clone(), module);
|
2022-01-24 17:04:40 +08:00
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
global.num_modules_loaded += 1;
|
2022-01-07 11:43:47 +08:00
|
|
|
|
2022-01-24 17:04:40 +08:00
|
|
|
Ok(Dynamic::UNIT)
|
|
|
|
} else {
|
|
|
|
module_result.map(|_| Dynamic::UNIT)
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
} else {
|
2022-01-24 17:04:40 +08:00
|
|
|
path_result.map(|_| Dynamic::UNIT)
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Export statement
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-02-08 09:02:15 +08:00
|
|
|
Stmt::Export(x, ..) => {
|
2022-03-05 17:57:23 +08:00
|
|
|
let (Ident { name, pos, .. }, alias) = x.as_ref();
|
2022-01-28 10:11:40 +08:00
|
|
|
// Mark scope variables as public
|
2022-02-08 09:02:15 +08:00
|
|
|
if let Some((index, ..)) = scope.get_index(name) {
|
2022-03-09 09:25:32 +08:00
|
|
|
let alias = if alias.is_empty() { name } else { alias }.clone();
|
|
|
|
scope.add_alias_by_index(index, alias);
|
2022-01-28 10:11:40 +08:00
|
|
|
Ok(Dynamic::UNIT)
|
|
|
|
} else {
|
|
|
|
Err(ERR::ErrorVariableNotFound(name.to_string(), *pos).into())
|
|
|
|
}
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Share statement
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2022-02-09 13:40:51 +08:00
|
|
|
Stmt::Share(name, ..) => {
|
2022-02-08 09:02:15 +08:00
|
|
|
if let Some((index, ..)) = scope.get_index(name) {
|
2022-01-07 11:43:47 +08:00
|
|
|
let val = scope.get_mut_by_index(index);
|
|
|
|
|
|
|
|
if !val.is_shared() {
|
|
|
|
// Replace the variable with a shared value.
|
|
|
|
*val = std::mem::take(val).into_shared();
|
|
|
|
}
|
2022-02-09 13:40:51 +08:00
|
|
|
} else {
|
|
|
|
unreachable!("variable {} not found for sharing", name);
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
Ok(Dynamic::UNIT)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => unreachable!("statement cannot be evaluated: {:?}", stmt),
|
2022-01-24 17:04:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature = "debugging")]
|
2022-01-25 12:24:30 +08:00
|
|
|
global.debugger.reset_status(reset_debugger);
|
2022-01-24 17:04:40 +08:00
|
|
|
|
|
|
|
return result;
|
2022-01-07 11:43:47 +08:00
|
|
|
}
|
|
|
|
}
|