Move lib into global.
This commit is contained in:
@@ -5,9 +5,7 @@ use super::{Caches, GlobalRuntimeState, Target};
|
||||
use crate::ast::{ASTFlags, Expr, OpAssignment};
|
||||
use crate::types::dynamic::Union;
|
||||
use crate::types::RestoreOnDrop;
|
||||
use crate::{
|
||||
Dynamic, Engine, FnArgsVec, Position, RhaiResult, RhaiResultOf, Scope, SharedModule, ERR,
|
||||
};
|
||||
use crate::{Dynamic, Engine, FnArgsVec, Position, RhaiResult, RhaiResultOf, Scope, ERR};
|
||||
use std::hash::Hash;
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
@@ -43,7 +41,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
this_ptr: &mut Dynamic,
|
||||
target: &mut Target,
|
||||
root: (&str, Position),
|
||||
@@ -75,7 +72,7 @@ impl Engine {
|
||||
if !parent_options.contains(ASTFlags::BREAK) =>
|
||||
{
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, lib, scope, this_ptr, _parent)?;
|
||||
self.run_debugger(global, caches, scope, this_ptr, _parent)?;
|
||||
|
||||
let idx_val = &mut idx_values.pop().unwrap();
|
||||
let mut idx_val_for_setter = idx_val.clone();
|
||||
@@ -84,14 +81,14 @@ impl Engine {
|
||||
|
||||
let (try_setter, result) = {
|
||||
let mut obj = self.get_indexed_mut(
|
||||
global, caches, lib, target, idx_val, idx_pos, false, true,
|
||||
global, caches, target, idx_val, idx_pos, false, true,
|
||||
)?;
|
||||
let is_obj_temp_val = obj.is_temp_value();
|
||||
let obj_ptr = &mut obj;
|
||||
|
||||
match self.eval_dot_index_chain_helper(
|
||||
global, caches, lib, this_ptr, obj_ptr, root, rhs, *options,
|
||||
&x.rhs, idx_values, rhs_chain, new_val,
|
||||
global, caches, this_ptr, obj_ptr, root, rhs, *options, &x.rhs,
|
||||
idx_values, rhs_chain, new_val,
|
||||
) {
|
||||
Ok((result, true)) if is_obj_temp_val => {
|
||||
(Some(obj.take_or_clone()), (result, true))
|
||||
@@ -105,13 +102,11 @@ impl Engine {
|
||||
// Try to call index setter if value is changed
|
||||
let idx = &mut idx_val_for_setter;
|
||||
let new_val = &mut new_val;
|
||||
self.call_indexer_set(
|
||||
global, caches, lib, target, idx, new_val, is_ref_mut,
|
||||
)
|
||||
.or_else(|e| match *e {
|
||||
ERR::ErrorIndexingType(..) => Ok((Dynamic::UNIT, false)),
|
||||
_ => Err(e),
|
||||
})?;
|
||||
self.call_indexer_set(global, caches, target, idx, new_val, is_ref_mut)
|
||||
.or_else(|e| match *e {
|
||||
ERR::ErrorIndexingType(..) => Ok((Dynamic::UNIT, false)),
|
||||
_ => Err(e),
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
@@ -119,19 +114,19 @@ impl Engine {
|
||||
// xxx[rhs] op= new_val
|
||||
_ if new_val.is_some() => {
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, lib, scope, this_ptr, _parent)?;
|
||||
self.run_debugger(global, caches, scope, this_ptr, _parent)?;
|
||||
|
||||
let (new_val, op_info) = new_val.take().expect("`Some`");
|
||||
let idx_val = &mut idx_values.pop().unwrap();
|
||||
let idx = &mut idx_val.clone();
|
||||
|
||||
let try_setter = match self
|
||||
.get_indexed_mut(global, caches, lib, target, idx, pos, true, false)
|
||||
.get_indexed_mut(global, caches, target, idx, pos, true, false)
|
||||
{
|
||||
// Indexed value is not a temp value - update directly
|
||||
Ok(ref mut obj_ptr) => {
|
||||
self.eval_op_assignment(
|
||||
global, caches, lib, op_info, obj_ptr, root, new_val,
|
||||
global, caches, op_info, obj_ptr, root, new_val,
|
||||
)?;
|
||||
self.check_data_size(obj_ptr, op_info.pos)?;
|
||||
None
|
||||
@@ -149,13 +144,12 @@ impl Engine {
|
||||
let idx = &mut idx_val.clone();
|
||||
|
||||
// Call the index getter to get the current value
|
||||
if let Ok(val) =
|
||||
self.call_indexer_get(global, caches, lib, target, idx)
|
||||
if let Ok(val) = self.call_indexer_get(global, caches, target, idx)
|
||||
{
|
||||
let mut val = val.into();
|
||||
// Run the op-assignment
|
||||
self.eval_op_assignment(
|
||||
global, caches, lib, op_info, &mut val, root, new_val,
|
||||
global, caches, op_info, &mut val, root, new_val,
|
||||
)?;
|
||||
// Replace new value
|
||||
new_val = val.take_or_clone();
|
||||
@@ -167,7 +161,7 @@ impl Engine {
|
||||
let new_val = &mut new_val;
|
||||
|
||||
self.call_indexer_set(
|
||||
global, caches, lib, target, idx_val, new_val, is_ref_mut,
|
||||
global, caches, target, idx_val, new_val, is_ref_mut,
|
||||
)?;
|
||||
}
|
||||
|
||||
@@ -176,11 +170,11 @@ impl Engine {
|
||||
// xxx[rhs]
|
||||
_ => {
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, lib, scope, this_ptr, _parent)?;
|
||||
self.run_debugger(global, caches, scope, this_ptr, _parent)?;
|
||||
|
||||
let idx_val = &mut idx_values.pop().unwrap();
|
||||
|
||||
self.get_indexed_mut(global, caches, lib, target, idx_val, pos, false, true)
|
||||
self.get_indexed_mut(global, caches, target, idx_val, pos, false, true)
|
||||
.map(|v| (v.take_or_clone(), false))
|
||||
}
|
||||
}
|
||||
@@ -197,8 +191,8 @@ impl Engine {
|
||||
// xxx.fn_name(arg_expr_list)
|
||||
Expr::MethodCall(x, pos) if !x.is_qualified() && new_val.is_none() => {
|
||||
#[cfg(feature = "debugging")]
|
||||
let reset = self
|
||||
.run_debugger_with_reset(global, caches, lib, scope, this_ptr, rhs)?;
|
||||
let reset =
|
||||
self.run_debugger_with_reset(global, caches, scope, this_ptr, rhs)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| {
|
||||
g.debugger.reset_status(reset)
|
||||
@@ -217,7 +211,7 @@ impl Engine {
|
||||
let pos1 = args.get(0).map_or(Position::NONE, Expr::position);
|
||||
|
||||
self.make_method_call(
|
||||
global, caches, lib, name, *hashes, target, call_args, pos1, *pos,
|
||||
global, caches, name, *hashes, target, call_args, pos1, *pos,
|
||||
)
|
||||
}
|
||||
// xxx.fn_name(...) = ???
|
||||
@@ -231,16 +225,16 @@ impl Engine {
|
||||
// {xxx:map}.id op= ???
|
||||
Expr::Property(x, pos) if target.is_map() && new_val.is_some() => {
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, lib, scope, this_ptr, rhs)?;
|
||||
self.run_debugger(global, caches, scope, this_ptr, rhs)?;
|
||||
|
||||
let index = &mut x.2.clone().into();
|
||||
let (new_val, op_info) = new_val.take().expect("`Some`");
|
||||
{
|
||||
let val_target = &mut self.get_indexed_mut(
|
||||
global, caches, lib, target, index, *pos, true, false,
|
||||
global, caches, target, index, *pos, true, false,
|
||||
)?;
|
||||
self.eval_op_assignment(
|
||||
global, caches, lib, op_info, val_target, root, new_val,
|
||||
global, caches, op_info, val_target, root, new_val,
|
||||
)?;
|
||||
}
|
||||
self.check_data_size(target.source(), op_info.pos)?;
|
||||
@@ -249,18 +243,17 @@ impl Engine {
|
||||
// {xxx:map}.id
|
||||
Expr::Property(x, pos) if target.is_map() => {
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, lib, scope, this_ptr, rhs)?;
|
||||
self.run_debugger(global, caches, scope, this_ptr, rhs)?;
|
||||
|
||||
let index = &mut x.2.clone().into();
|
||||
let val = self.get_indexed_mut(
|
||||
global, caches, lib, target, index, *pos, false, false,
|
||||
)?;
|
||||
let val = self
|
||||
.get_indexed_mut(global, caches, target, index, *pos, false, false)?;
|
||||
Ok((val.take_or_clone(), false))
|
||||
}
|
||||
// xxx.id op= ???
|
||||
Expr::Property(x, pos) if new_val.is_some() => {
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, lib, scope, this_ptr, rhs)?;
|
||||
self.run_debugger(global, caches, scope, this_ptr, rhs)?;
|
||||
|
||||
let ((getter, hash_get), (setter, hash_set), name) = &**x;
|
||||
let (mut new_val, op_info) = new_val.take().expect("`Some`");
|
||||
@@ -269,23 +262,18 @@ impl Engine {
|
||||
let args = &mut [target.as_mut()];
|
||||
let (mut orig_val, ..) = self
|
||||
.exec_native_fn_call(
|
||||
global, caches, lib, getter, None, *hash_get, args, is_ref_mut,
|
||||
*pos,
|
||||
global, caches, getter, None, *hash_get, args, is_ref_mut, *pos,
|
||||
)
|
||||
.or_else(|err| match *err {
|
||||
// Try an indexer if property does not exist
|
||||
ERR::ErrorDotExpr(..) => {
|
||||
let mut prop = name.into();
|
||||
self.call_indexer_get(
|
||||
global, caches, lib, target, &mut prop,
|
||||
)
|
||||
.map(|r| (r, false))
|
||||
.map_err(|e| {
|
||||
match *e {
|
||||
self.call_indexer_get(global, caches, target, &mut prop)
|
||||
.map(|r| (r, false))
|
||||
.map_err(|e| match *e {
|
||||
ERR::ErrorIndexingType(..) => err,
|
||||
_ => e,
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
_ => Err(err),
|
||||
})?;
|
||||
@@ -294,7 +282,7 @@ impl Engine {
|
||||
let orig_val = &mut (&mut orig_val).into();
|
||||
|
||||
self.eval_op_assignment(
|
||||
global, caches, lib, op_info, orig_val, root, new_val,
|
||||
global, caches, op_info, orig_val, root, new_val,
|
||||
)?;
|
||||
}
|
||||
|
||||
@@ -303,7 +291,7 @@ impl Engine {
|
||||
|
||||
let args = &mut [target.as_mut(), &mut new_val];
|
||||
self.exec_native_fn_call(
|
||||
global, caches, lib, setter, None, *hash_set, args, is_ref_mut, *pos,
|
||||
global, caches, setter, None, *hash_set, args, is_ref_mut, *pos,
|
||||
)
|
||||
.or_else(|err| match *err {
|
||||
// Try an indexer if property does not exist
|
||||
@@ -311,7 +299,7 @@ impl Engine {
|
||||
let idx = &mut name.into();
|
||||
let new_val = &mut new_val;
|
||||
self.call_indexer_set(
|
||||
global, caches, lib, target, idx, new_val, is_ref_mut,
|
||||
global, caches, target, idx, new_val, is_ref_mut,
|
||||
)
|
||||
.map_err(|e| match *e {
|
||||
ERR::ErrorIndexingType(..) => err,
|
||||
@@ -324,19 +312,19 @@ impl Engine {
|
||||
// xxx.id
|
||||
Expr::Property(x, pos) => {
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, lib, scope, this_ptr, rhs)?;
|
||||
self.run_debugger(global, caches, scope, this_ptr, rhs)?;
|
||||
|
||||
let ((getter, hash_get), _, name) = &**x;
|
||||
let args = &mut [target.as_mut()];
|
||||
self.exec_native_fn_call(
|
||||
global, caches, lib, getter, None, *hash_get, args, is_ref_mut, *pos,
|
||||
global, caches, getter, None, *hash_get, args, is_ref_mut, *pos,
|
||||
)
|
||||
.map_or_else(
|
||||
|err| match *err {
|
||||
// Try an indexer if property does not exist
|
||||
ERR::ErrorDotExpr(..) => {
|
||||
let mut prop = name.into();
|
||||
self.call_indexer_get(global, caches, lib, target, &mut prop)
|
||||
self.call_indexer_get(global, caches, target, &mut prop)
|
||||
.map(|r| (r, false))
|
||||
.map_err(|e| match *e {
|
||||
ERR::ErrorIndexingType(..) => err,
|
||||
@@ -358,18 +346,18 @@ impl Engine {
|
||||
let val_target = &mut match x.lhs {
|
||||
Expr::Property(ref p, pos) => {
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, lib, scope, this_ptr, _node)?;
|
||||
self.run_debugger(global, caches, scope, this_ptr, _node)?;
|
||||
|
||||
let index = &mut p.2.clone().into();
|
||||
self.get_indexed_mut(
|
||||
global, caches, lib, target, index, pos, false, true,
|
||||
global, caches, target, index, pos, false, true,
|
||||
)?
|
||||
}
|
||||
// {xxx:map}.fn_name(arg_expr_list)[expr] | {xxx:map}.fn_name(arg_expr_list).expr
|
||||
Expr::MethodCall(ref x, pos) if !x.is_qualified() => {
|
||||
#[cfg(feature = "debugging")]
|
||||
let reset = self.run_debugger_with_reset(
|
||||
global, caches, lib, scope, this_ptr, _node,
|
||||
global, caches, scope, this_ptr, _node,
|
||||
)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| {
|
||||
@@ -390,8 +378,7 @@ impl Engine {
|
||||
let pos1 = args.get(0).map_or(Position::NONE, Expr::position);
|
||||
|
||||
self.make_method_call(
|
||||
global, caches, lib, name, *hashes, target, call_args, pos1,
|
||||
pos,
|
||||
global, caches, name, *hashes, target, call_args, pos1, pos,
|
||||
)?
|
||||
.0
|
||||
.into()
|
||||
@@ -406,7 +393,7 @@ impl Engine {
|
||||
let rhs_chain = rhs.into();
|
||||
|
||||
self.eval_dot_index_chain_helper(
|
||||
global, caches, lib, this_ptr, val_target, root, rhs, *options, &x.rhs,
|
||||
global, caches, this_ptr, val_target, root, rhs, *options, &x.rhs,
|
||||
idx_values, rhs_chain, new_val,
|
||||
)
|
||||
.map_err(|err| err.fill_position(*x_pos))
|
||||
@@ -419,7 +406,7 @@ impl Engine {
|
||||
// xxx.prop[expr] | xxx.prop.expr
|
||||
Expr::Property(ref p, pos) => {
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, lib, scope, this_ptr, _node)?;
|
||||
self.run_debugger(global, caches, scope, this_ptr, _node)?;
|
||||
|
||||
let ((getter, hash_get), (setter, hash_set), name) = &**p;
|
||||
let rhs_chain = rhs.into();
|
||||
@@ -429,23 +416,19 @@ impl Engine {
|
||||
// Assume getters are always pure
|
||||
let (mut val, ..) = self
|
||||
.exec_native_fn_call(
|
||||
global, caches, lib, getter, None, *hash_get, args,
|
||||
is_ref_mut, pos,
|
||||
global, caches, getter, None, *hash_get, args, is_ref_mut,
|
||||
pos,
|
||||
)
|
||||
.or_else(|err| match *err {
|
||||
// Try an indexer if property does not exist
|
||||
ERR::ErrorDotExpr(..) => {
|
||||
let mut prop = name.into();
|
||||
self.call_indexer_get(
|
||||
global, caches, lib, target, &mut prop,
|
||||
)
|
||||
.map(|r| (r, false))
|
||||
.map_err(
|
||||
|e| match *e {
|
||||
self.call_indexer_get(global, caches, target, &mut prop)
|
||||
.map(|r| (r, false))
|
||||
.map_err(|e| match *e {
|
||||
ERR::ErrorIndexingType(..) => err,
|
||||
_ => e,
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
_ => Err(err),
|
||||
})?;
|
||||
@@ -454,8 +437,8 @@ impl Engine {
|
||||
|
||||
let (result, may_be_changed) = self
|
||||
.eval_dot_index_chain_helper(
|
||||
global, caches, lib, this_ptr, val, root, rhs, *options,
|
||||
&x.rhs, idx_values, rhs_chain, new_val,
|
||||
global, caches, this_ptr, val, root, rhs, *options, &x.rhs,
|
||||
idx_values, rhs_chain, new_val,
|
||||
)
|
||||
.map_err(|err| err.fill_position(*x_pos))?;
|
||||
|
||||
@@ -465,8 +448,8 @@ impl Engine {
|
||||
let mut arg_values = [target.as_mut(), val.as_mut()];
|
||||
let args = &mut arg_values;
|
||||
self.exec_native_fn_call(
|
||||
global, caches, lib, setter, None, *hash_set, args,
|
||||
is_ref_mut, pos,
|
||||
global, caches, setter, None, *hash_set, args, is_ref_mut,
|
||||
pos,
|
||||
)
|
||||
.or_else(
|
||||
|err| match *err {
|
||||
@@ -475,7 +458,7 @@ impl Engine {
|
||||
let idx = &mut name.into();
|
||||
let new_val = val;
|
||||
self.call_indexer_set(
|
||||
global, caches, lib, target, idx, new_val,
|
||||
global, caches, target, idx, new_val,
|
||||
is_ref_mut,
|
||||
)
|
||||
.or_else(|e| match *e {
|
||||
@@ -499,7 +482,7 @@ impl Engine {
|
||||
let val = {
|
||||
#[cfg(feature = "debugging")]
|
||||
let reset = self.run_debugger_with_reset(
|
||||
global, caches, lib, scope, this_ptr, _node,
|
||||
global, caches, scope, this_ptr, _node,
|
||||
)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| {
|
||||
@@ -521,8 +504,7 @@ impl Engine {
|
||||
let pos1 = args.get(0).map_or(Position::NONE, Expr::position);
|
||||
|
||||
self.make_method_call(
|
||||
global, caches, lib, name, *hashes, target, call_args,
|
||||
pos1, pos,
|
||||
global, caches, name, *hashes, target, call_args, pos1, pos,
|
||||
)?
|
||||
.0
|
||||
};
|
||||
@@ -531,8 +513,8 @@ impl Engine {
|
||||
let rhs_chain = rhs.into();
|
||||
|
||||
self.eval_dot_index_chain_helper(
|
||||
global, caches, lib, this_ptr, val, root, rhs, *options,
|
||||
&x.rhs, idx_values, rhs_chain, new_val,
|
||||
global, caches, this_ptr, val, root, rhs, *options, &x.rhs,
|
||||
idx_values, rhs_chain, new_val,
|
||||
)
|
||||
.map_err(|err| err.fill_position(pos))
|
||||
}
|
||||
@@ -556,7 +538,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Dynamic,
|
||||
expr: &Expr,
|
||||
@@ -597,7 +578,7 @@ impl Engine {
|
||||
// All other patterns - evaluate the arguments chain
|
||||
_ => {
|
||||
self.eval_dot_index_chain_arguments(
|
||||
global, caches, lib, scope, this_ptr, rhs, options, chain_type, idx_values,
|
||||
global, caches, scope, this_ptr, rhs, options, chain_type, idx_values,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
@@ -606,18 +587,18 @@ impl Engine {
|
||||
// id.??? or id[???]
|
||||
Expr::Variable(x, .., var_pos) => {
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, lib, scope, this_ptr, lhs)?;
|
||||
self.run_debugger(global, caches, scope, this_ptr, lhs)?;
|
||||
self.track_operation(global, *var_pos)?;
|
||||
|
||||
let (mut target, ..) =
|
||||
self.search_namespace(global, caches, lib, scope, this_ptr, lhs)?;
|
||||
self.search_namespace(global, caches, scope, this_ptr, lhs)?;
|
||||
|
||||
let obj_ptr = &mut target;
|
||||
let root = (x.3.as_str(), *var_pos);
|
||||
let mut this = Dynamic::NULL;
|
||||
|
||||
self.eval_dot_index_chain_helper(
|
||||
global, caches, lib, &mut this, obj_ptr, root, expr, options, rhs, idx_values,
|
||||
global, caches, &mut this, obj_ptr, root, expr, options, rhs, idx_values,
|
||||
chain_type, new_val,
|
||||
)
|
||||
}
|
||||
@@ -626,13 +607,13 @@ impl Engine {
|
||||
// {expr}.??? or {expr}[???]
|
||||
expr => {
|
||||
let value = self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, expr)?
|
||||
.eval_expr(global, caches, scope, this_ptr, expr)?
|
||||
.flatten();
|
||||
let obj_ptr = &mut value.into();
|
||||
let root = ("", expr.start_position());
|
||||
|
||||
self.eval_dot_index_chain_helper(
|
||||
global, caches, lib, this_ptr, obj_ptr, root, expr, options, rhs, idx_values,
|
||||
global, caches, this_ptr, obj_ptr, root, expr, options, rhs, idx_values,
|
||||
chain_type, new_val,
|
||||
)
|
||||
}
|
||||
@@ -646,7 +627,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Dynamic,
|
||||
expr: &Expr,
|
||||
@@ -663,7 +643,7 @@ impl Engine {
|
||||
{
|
||||
for arg_expr in &x.args {
|
||||
idx_values.push(
|
||||
self.get_arg_value(global, caches, lib, scope, this_ptr, arg_expr)?
|
||||
self.get_arg_value(global, caches, scope, this_ptr, arg_expr)?
|
||||
.0
|
||||
.flatten(),
|
||||
);
|
||||
@@ -697,7 +677,7 @@ impl Engine {
|
||||
{
|
||||
for arg_expr in &x.args {
|
||||
_arg_values.push(
|
||||
self.get_arg_value(global, caches, lib, scope, this_ptr, arg_expr)?
|
||||
self.get_arg_value(global, caches, scope, this_ptr, arg_expr)?
|
||||
.0
|
||||
.flatten(),
|
||||
);
|
||||
@@ -714,7 +694,7 @@ impl Engine {
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
_ if parent_chain_type == ChainType::Indexing => {
|
||||
_arg_values.push(
|
||||
self.eval_expr(global, caches, lib, scope, this_ptr, lhs)?
|
||||
self.eval_expr(global, caches, scope, this_ptr, lhs)?
|
||||
.flatten(),
|
||||
);
|
||||
}
|
||||
@@ -725,7 +705,7 @@ impl Engine {
|
||||
let chain_type = expr.into();
|
||||
|
||||
self.eval_dot_index_chain_arguments(
|
||||
global, caches, lib, scope, this_ptr, rhs, *options, chain_type, idx_values,
|
||||
global, caches, scope, this_ptr, rhs, *options, chain_type, idx_values,
|
||||
)?;
|
||||
|
||||
if !_arg_values.is_empty() {
|
||||
@@ -739,7 +719,7 @@ impl Engine {
|
||||
}
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
_ if parent_chain_type == ChainType::Indexing => idx_values.push(
|
||||
self.eval_expr(global, caches, lib, scope, this_ptr, expr)?
|
||||
self.eval_expr(global, caches, scope, this_ptr, expr)?
|
||||
.flatten(),
|
||||
),
|
||||
_ => unreachable!("unknown chained expression: {:?}", expr),
|
||||
@@ -754,7 +734,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
target: &mut Dynamic,
|
||||
idx: &mut Dynamic,
|
||||
) -> RhaiResultOf<Dynamic> {
|
||||
@@ -766,7 +745,7 @@ impl Engine {
|
||||
global.level += 1;
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| g.level -= 1);
|
||||
|
||||
self.exec_native_fn_call(global, caches, lib, fn_name, None, hash, args, true, pos)
|
||||
self.exec_native_fn_call(global, caches, fn_name, None, hash, args, true, pos)
|
||||
.map(|(r, ..)| r)
|
||||
}
|
||||
|
||||
@@ -776,7 +755,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
target: &mut Dynamic,
|
||||
idx: &mut Dynamic,
|
||||
new_val: &mut Dynamic,
|
||||
@@ -790,9 +768,7 @@ impl Engine {
|
||||
global.level += 1;
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| g.level -= 1);
|
||||
|
||||
self.exec_native_fn_call(
|
||||
global, caches, lib, fn_name, None, hash, args, is_ref_mut, pos,
|
||||
)
|
||||
self.exec_native_fn_call(global, caches, fn_name, None, hash, args, is_ref_mut, pos)
|
||||
}
|
||||
|
||||
/// Get the value at the indexed position of a base type.
|
||||
@@ -801,7 +777,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
target: &'t mut Dynamic,
|
||||
idx: &mut Dynamic,
|
||||
idx_pos: Position,
|
||||
@@ -1010,7 +985,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
_ if use_indexers => self
|
||||
.call_indexer_get(global, caches, lib, target, idx)
|
||||
.call_indexer_get(global, caches, target, idx)
|
||||
.map(Into::into),
|
||||
|
||||
_ => Err(ERR::ErrorIndexingType(
|
||||
|
@@ -3,9 +3,7 @@
|
||||
|
||||
use super::{Caches, EvalContext, GlobalRuntimeState};
|
||||
use crate::ast::{ASTNode, Expr, Stmt};
|
||||
use crate::{
|
||||
Dynamic, Engine, EvalAltResult, ImmutableString, Position, RhaiResultOf, Scope, SharedModule,
|
||||
};
|
||||
use crate::{Dynamic, Engine, EvalAltResult, ImmutableString, Position, RhaiResultOf, Scope};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
use std::{fmt, iter::repeat, mem};
|
||||
@@ -413,14 +411,13 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Dynamic,
|
||||
node: impl Into<ASTNode<'a>>,
|
||||
) -> RhaiResultOf<()> {
|
||||
if self.debugger.is_some() {
|
||||
if let Some(cmd) =
|
||||
self.run_debugger_with_reset_raw(global, caches, lib, scope, this_ptr, node)?
|
||||
self.run_debugger_with_reset_raw(global, caches, scope, this_ptr, node)?
|
||||
{
|
||||
global.debugger.status = cmd;
|
||||
}
|
||||
@@ -439,13 +436,12 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Dynamic,
|
||||
node: impl Into<ASTNode<'a>>,
|
||||
) -> RhaiResultOf<Option<DebuggerStatus>> {
|
||||
if self.debugger.is_some() {
|
||||
self.run_debugger_with_reset_raw(global, caches, lib, scope, this_ptr, node)
|
||||
self.run_debugger_with_reset_raw(global, caches, scope, this_ptr, node)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
@@ -461,7 +457,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Dynamic,
|
||||
node: impl Into<ASTNode<'a>>,
|
||||
@@ -494,7 +489,7 @@ impl Engine {
|
||||
},
|
||||
};
|
||||
|
||||
self.run_debugger_raw(global, caches, lib, scope, this_ptr, node, event)
|
||||
self.run_debugger_raw(global, caches, scope, this_ptr, node, event)
|
||||
}
|
||||
/// Run the debugger callback unconditionally.
|
||||
///
|
||||
@@ -507,7 +502,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Dynamic,
|
||||
node: ASTNode<'a>,
|
||||
@@ -515,7 +509,7 @@ impl Engine {
|
||||
) -> Result<Option<DebuggerStatus>, Box<crate::EvalAltResult>> {
|
||||
let src = global.source_raw().cloned();
|
||||
let src = src.as_ref().map(|s| s.as_str());
|
||||
let context = crate::EvalContext::new(self, global, caches, lib, scope, this_ptr);
|
||||
let context = crate::EvalContext::new(self, global, caches, scope, this_ptr);
|
||||
|
||||
if let Some((.., ref on_debugger)) = self.debugger {
|
||||
let command = on_debugger(context, event, node, src, node.position())?;
|
||||
|
@@ -17,8 +17,6 @@ pub struct EvalContext<'a, 's, 'ps, 'g, 'c, 't> {
|
||||
global: &'g mut GlobalRuntimeState,
|
||||
/// The current [caches][Caches], if available.
|
||||
caches: &'c mut Caches,
|
||||
/// The current stack of imported [modules][Module].
|
||||
lib: &'a [SharedModule],
|
||||
/// The current bound `this` pointer, if any.
|
||||
this_ptr: &'t mut Dynamic,
|
||||
}
|
||||
@@ -31,7 +29,6 @@ impl<'a, 's, 'ps, 'g, 'c, 't> EvalContext<'a, 's, 'ps, 'g, 'c, 't> {
|
||||
engine: &'a Engine,
|
||||
global: &'g mut GlobalRuntimeState,
|
||||
caches: &'c mut Caches,
|
||||
lib: &'a [SharedModule],
|
||||
scope: &'s mut Scope<'ps>,
|
||||
this_ptr: &'t mut Dynamic,
|
||||
) -> Self {
|
||||
@@ -40,7 +37,6 @@ impl<'a, 's, 'ps, 'g, 'c, 't> EvalContext<'a, 's, 'ps, 'g, 'c, 't> {
|
||||
scope,
|
||||
global,
|
||||
caches,
|
||||
lib,
|
||||
this_ptr,
|
||||
}
|
||||
}
|
||||
@@ -106,15 +102,15 @@ impl<'a, 's, 'ps, 'g, 'c, 't> EvalContext<'a, 's, 'ps, 'g, 'c, 't> {
|
||||
/// Get an iterator over the namespaces containing definition of all script-defined functions.
|
||||
#[inline]
|
||||
pub fn iter_namespaces(&self) -> impl Iterator<Item = &Module> {
|
||||
self.lib.iter().map(|m| m.as_ref())
|
||||
self.global.lib.iter().map(|m| m.as_ref())
|
||||
}
|
||||
/// _(internals)_ The current set of namespaces containing definitions of all script-defined functions.
|
||||
/// Exported under the `internals` feature only.
|
||||
#[cfg(feature = "internals")]
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub const fn namespaces(&self) -> &[SharedModule] {
|
||||
self.lib
|
||||
pub fn namespaces(&self) -> &[SharedModule] {
|
||||
&self.global.lib
|
||||
}
|
||||
/// The current bound `this` pointer, if any.
|
||||
#[inline]
|
||||
@@ -181,20 +177,14 @@ impl<'a, 's, 'ps, 'g, 'c, 't> EvalContext<'a, 's, 'ps, 'g, 'c, 't> {
|
||||
crate::ast::Expr::Stmt(statements) => self.engine.eval_stmt_block(
|
||||
self.global,
|
||||
self.caches,
|
||||
self.lib,
|
||||
self.scope,
|
||||
self.this_ptr,
|
||||
statements,
|
||||
rewind_scope,
|
||||
),
|
||||
_ => self.engine.eval_expr(
|
||||
self.global,
|
||||
self.caches,
|
||||
self.lib,
|
||||
self.scope,
|
||||
self.this_ptr,
|
||||
expr,
|
||||
),
|
||||
_ => self
|
||||
.engine
|
||||
.eval_expr(self.global, self.caches, self.scope, self.this_ptr, expr),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -51,23 +51,22 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &'s mut Scope,
|
||||
this_ptr: &'s mut Dynamic,
|
||||
expr: &Expr,
|
||||
) -> RhaiResultOf<(Target<'s>, Position)> {
|
||||
match expr {
|
||||
Expr::Variable(_, Some(_), _) => {
|
||||
self.search_scope_only(global, caches, lib, scope, this_ptr, expr)
|
||||
self.search_scope_only(global, caches, scope, this_ptr, expr)
|
||||
}
|
||||
Expr::Variable(v, None, _var_pos) => match &**v {
|
||||
// Normal variable access
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
(_, ns, ..) if ns.is_empty() => {
|
||||
self.search_scope_only(global, caches, lib, scope, this_ptr, expr)
|
||||
self.search_scope_only(global, caches, scope, this_ptr, expr)
|
||||
}
|
||||
#[cfg(feature = "no_module")]
|
||||
(_, (), ..) => self.search_scope_only(global, caches, lib, scope, this_ptr, expr),
|
||||
(_, (), ..) => self.search_scope_only(global, caches, scope, this_ptr, expr),
|
||||
|
||||
// Qualified variable access
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
@@ -134,7 +133,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &'s mut Scope,
|
||||
this_ptr: &'s mut Dynamic,
|
||||
expr: &Expr,
|
||||
@@ -155,7 +153,8 @@ impl Engine {
|
||||
// Scripted function with the same name
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Expr::Variable(v, None, pos)
|
||||
if lib
|
||||
if global
|
||||
.lib
|
||||
.iter()
|
||||
.flat_map(|m| m.iter_script_fn())
|
||||
.any(|(_, _, f, ..)| f == v.3.as_str()) =>
|
||||
@@ -170,7 +169,7 @@ impl Engine {
|
||||
|
||||
// Check the variable resolver, if any
|
||||
if let Some(ref resolve_var) = self.resolve_var {
|
||||
let context = EvalContext::new(self, global, caches, lib, scope, this_ptr);
|
||||
let context = EvalContext::new(self, global, caches, scope, this_ptr);
|
||||
let var_name = expr.get_variable_name(true).expect("`Expr::Variable`");
|
||||
match resolve_var(var_name, index, context) {
|
||||
Ok(Some(mut result)) => {
|
||||
@@ -218,7 +217,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Dynamic,
|
||||
expr: &Expr,
|
||||
@@ -230,7 +228,7 @@ impl Engine {
|
||||
// binary operators are also function calls.
|
||||
if let Expr::FnCall(x, pos) = expr {
|
||||
#[cfg(feature = "debugging")]
|
||||
let reset = self.run_debugger_with_reset(global, caches, lib, scope, this_ptr, expr)?;
|
||||
let reset = self.run_debugger_with_reset(global, caches, scope, this_ptr, expr)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *crate::types::RestoreOnDrop::lock(global, move |g| {
|
||||
g.debugger.reset_status(reset)
|
||||
@@ -238,7 +236,7 @@ impl Engine {
|
||||
|
||||
self.track_operation(global, expr.position())?;
|
||||
|
||||
return self.eval_fn_call_expr(global, caches, lib, scope, this_ptr, x, *pos);
|
||||
return self.eval_fn_call_expr(global, caches, scope, this_ptr, x, *pos);
|
||||
}
|
||||
|
||||
// Then variable access.
|
||||
@@ -246,7 +244,7 @@ impl Engine {
|
||||
// will cost more than the mis-predicted `match` branch.
|
||||
if let Expr::Variable(x, index, var_pos) = expr {
|
||||
#[cfg(feature = "debugging")]
|
||||
self.run_debugger(global, caches, lib, scope, this_ptr, expr)?;
|
||||
self.run_debugger(global, caches, scope, this_ptr, expr)?;
|
||||
|
||||
self.track_operation(global, expr.position())?;
|
||||
|
||||
@@ -257,13 +255,13 @@ impl Engine {
|
||||
Ok(this_ptr.clone())
|
||||
}
|
||||
} else {
|
||||
self.search_namespace(global, caches, lib, scope, this_ptr, expr)
|
||||
self.search_namespace(global, caches, scope, this_ptr, expr)
|
||||
.map(|(val, ..)| val.take_or_clone())
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(feature = "debugging")]
|
||||
let reset = self.run_debugger_with_reset(global, caches, lib, scope, this_ptr, expr)?;
|
||||
let reset = self.run_debugger_with_reset(global, caches, scope, this_ptr, expr)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *crate::types::RestoreOnDrop::lock(global, move |g| {
|
||||
g.debugger.reset_status(reset)
|
||||
@@ -294,12 +292,12 @@ impl Engine {
|
||||
.iter()
|
||||
.try_for_each(|expr| {
|
||||
let item = self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, expr)?
|
||||
.eval_expr(global, caches, scope, this_ptr, expr)?
|
||||
.flatten();
|
||||
|
||||
op_info.pos = expr.start_position();
|
||||
|
||||
self.eval_op_assignment(global, caches, lib, &op_info, target, root, item)
|
||||
self.eval_op_assignment(global, caches, &op_info, target, root, item)
|
||||
})
|
||||
.map(|_| concat.take_or_clone());
|
||||
|
||||
@@ -316,7 +314,7 @@ impl Engine {
|
||||
crate::Array::with_capacity(x.len()),
|
||||
|mut array, item_expr| {
|
||||
let value = self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, item_expr)?
|
||||
.eval_expr(global, caches, scope, this_ptr, item_expr)?
|
||||
.flatten();
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
@@ -348,7 +346,7 @@ impl Engine {
|
||||
x.0.iter()
|
||||
.try_fold(x.1.clone(), |mut map, (key, value_expr)| {
|
||||
let value = self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, value_expr)?
|
||||
.eval_expr(global, caches, scope, this_ptr, value_expr)?
|
||||
.flatten();
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
@@ -371,30 +369,30 @@ impl Engine {
|
||||
}
|
||||
|
||||
Expr::And(x, ..) => Ok((self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, &x.lhs)?
|
||||
.eval_expr(global, caches, scope, this_ptr, &x.lhs)?
|
||||
.as_bool()
|
||||
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, x.lhs.position()))?
|
||||
&& self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, &x.rhs)?
|
||||
.eval_expr(global, caches, scope, this_ptr, &x.rhs)?
|
||||
.as_bool()
|
||||
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, x.rhs.position()))?)
|
||||
.into()),
|
||||
|
||||
Expr::Or(x, ..) => Ok((self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, &x.lhs)?
|
||||
.eval_expr(global, caches, scope, this_ptr, &x.lhs)?
|
||||
.as_bool()
|
||||
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, x.lhs.position()))?
|
||||
|| self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, &x.rhs)?
|
||||
.eval_expr(global, caches, scope, this_ptr, &x.rhs)?
|
||||
.as_bool()
|
||||
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, x.rhs.position()))?)
|
||||
.into()),
|
||||
|
||||
Expr::Coalesce(x, ..) => {
|
||||
let value = self.eval_expr(global, caches, lib, scope, this_ptr, &x.lhs)?;
|
||||
let value = self.eval_expr(global, caches, scope, this_ptr, &x.lhs)?;
|
||||
|
||||
if value.is_unit() {
|
||||
self.eval_expr(global, caches, lib, scope, this_ptr, &x.rhs)
|
||||
self.eval_expr(global, caches, scope, this_ptr, &x.rhs)
|
||||
} else {
|
||||
Ok(value)
|
||||
}
|
||||
@@ -414,7 +412,7 @@ impl Engine {
|
||||
*pos,
|
||||
))
|
||||
})?;
|
||||
let mut context = EvalContext::new(self, global, caches, lib, scope, this_ptr);
|
||||
let mut context = EvalContext::new(self, global, caches, scope, this_ptr);
|
||||
|
||||
let result = (custom_def.func)(&mut context, &expressions, &custom.state);
|
||||
|
||||
@@ -422,16 +420,16 @@ impl Engine {
|
||||
}
|
||||
|
||||
Expr::Stmt(x) if x.is_empty() => Ok(Dynamic::UNIT),
|
||||
Expr::Stmt(x) => self.eval_stmt_block(global, caches, lib, scope, this_ptr, x, true),
|
||||
Expr::Stmt(x) => self.eval_stmt_block(global, caches, scope, this_ptr, x, true),
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Expr::Index(..) => {
|
||||
self.eval_dot_index_chain(global, caches, lib, scope, this_ptr, expr, &mut None)
|
||||
self.eval_dot_index_chain(global, caches, scope, this_ptr, expr, &mut None)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
Expr::Dot(..) => {
|
||||
self.eval_dot_index_chain(global, caches, lib, scope, this_ptr, expr, &mut None)
|
||||
self.eval_dot_index_chain(global, caches, scope, this_ptr, expr, &mut None)
|
||||
}
|
||||
|
||||
_ => unreachable!("expression cannot be evaluated: {:?}", expr),
|
||||
|
@@ -1,6 +1,6 @@
|
||||
//! Global runtime state.
|
||||
|
||||
use crate::{Dynamic, Engine, ImmutableString};
|
||||
use crate::{Dynamic, Engine, ImmutableString, SharedModule, StaticVec};
|
||||
use std::fmt;
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
@@ -25,10 +25,12 @@ pub type GlobalConstants =
|
||||
pub struct GlobalRuntimeState {
|
||||
/// Names of imported [modules][crate::Module].
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
imports: crate::StaticVec<ImmutableString>,
|
||||
imports: StaticVec<ImmutableString>,
|
||||
/// Stack of imported [modules][crate::Module].
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
modules: crate::StaticVec<crate::SharedModule>,
|
||||
modules: StaticVec<SharedModule>,
|
||||
/// The current stack of loaded [modules][Module].
|
||||
pub lib: StaticVec<SharedModule>,
|
||||
/// Source of the current context.
|
||||
///
|
||||
/// No source if the string is empty.
|
||||
@@ -81,9 +83,10 @@ impl GlobalRuntimeState {
|
||||
pub fn new(engine: &Engine) -> Self {
|
||||
Self {
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
imports: crate::StaticVec::new_const(),
|
||||
imports: StaticVec::new_const(),
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
modules: crate::StaticVec::new_const(),
|
||||
modules: StaticVec::new_const(),
|
||||
lib: StaticVec::new_const(),
|
||||
source: None,
|
||||
num_operations: 0,
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
@@ -130,7 +133,7 @@ impl GlobalRuntimeState {
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub fn get_shared_import(&self, index: usize) -> Option<crate::SharedModule> {
|
||||
pub fn get_shared_import(&self, index: usize) -> Option<SharedModule> {
|
||||
self.modules.get(index).cloned()
|
||||
}
|
||||
/// Get a mutable reference to the globally-imported [module][crate::Module] at a
|
||||
@@ -141,10 +144,7 @@ impl GlobalRuntimeState {
|
||||
#[allow(dead_code)]
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub(crate) fn get_shared_import_mut(
|
||||
&mut self,
|
||||
index: usize,
|
||||
) -> Option<&mut crate::SharedModule> {
|
||||
pub(crate) fn get_shared_import_mut(&mut self, index: usize) -> Option<&mut SharedModule> {
|
||||
self.modules.get_mut(index)
|
||||
}
|
||||
/// Get the index of a globally-imported [module][crate::Module] by name.
|
||||
@@ -168,7 +168,7 @@ impl GlobalRuntimeState {
|
||||
pub fn push_import(
|
||||
&mut self,
|
||||
name: impl Into<ImmutableString>,
|
||||
module: impl Into<crate::SharedModule>,
|
||||
module: impl Into<SharedModule>,
|
||||
) {
|
||||
self.imports.push(name.into());
|
||||
self.modules.push(module.into());
|
||||
@@ -201,7 +201,7 @@ impl GlobalRuntimeState {
|
||||
#[inline]
|
||||
pub(crate) fn iter_imports_raw(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
|
||||
) -> impl Iterator<Item = (&ImmutableString, &SharedModule)> {
|
||||
self.imports.iter().zip(self.modules.iter()).rev()
|
||||
}
|
||||
/// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order.
|
||||
@@ -209,9 +209,7 @@ impl GlobalRuntimeState {
|
||||
/// Not available under `no_module`.
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
#[inline]
|
||||
pub fn scan_imports_raw(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (&ImmutableString, &crate::SharedModule)> {
|
||||
pub fn scan_imports_raw(&self) -> impl Iterator<Item = (&ImmutableString, &SharedModule)> {
|
||||
self.imports.iter().zip(self.modules.iter())
|
||||
}
|
||||
/// Can the particular function with [`Dynamic`] parameter(s) exist in the stack of
|
||||
@@ -318,37 +316,7 @@ impl GlobalRuntimeState {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
impl IntoIterator for GlobalRuntimeState {
|
||||
type Item = (ImmutableString, crate::SharedModule);
|
||||
type IntoIter = std::iter::Rev<
|
||||
std::iter::Zip<
|
||||
smallvec::IntoIter<[ImmutableString; crate::STATIC_VEC_INLINE_SIZE]>,
|
||||
smallvec::IntoIter<[crate::SharedModule; crate::STATIC_VEC_INLINE_SIZE]>,
|
||||
>,
|
||||
>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.imports.into_iter().zip(self.modules.into_iter()).rev()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
impl<'a> IntoIterator for &'a GlobalRuntimeState {
|
||||
type Item = (&'a ImmutableString, &'a crate::SharedModule);
|
||||
type IntoIter = std::iter::Rev<
|
||||
std::iter::Zip<
|
||||
std::slice::Iter<'a, ImmutableString>,
|
||||
std::slice::Iter<'a, crate::SharedModule>,
|
||||
>,
|
||||
>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.imports.iter().zip(self.modules.iter()).rev()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
impl<K: Into<ImmutableString>, M: Into<crate::SharedModule>> Extend<(K, M)> for GlobalRuntimeState {
|
||||
impl<K: Into<ImmutableString>, M: Into<SharedModule>> Extend<(K, M)> for GlobalRuntimeState {
|
||||
#[inline]
|
||||
fn extend<T: IntoIterator<Item = (K, M)>>(&mut self, iter: T) {
|
||||
for (k, m) in iter {
|
||||
|
130
src/eval/stmt.rs
130
src/eval/stmt.rs
@@ -9,8 +9,7 @@ use crate::func::{get_builtin_op_assignment_fn, get_hasher};
|
||||
use crate::types::dynamic::AccessMode;
|
||||
use crate::types::RestoreOnDrop;
|
||||
use crate::{
|
||||
Dynamic, Engine, ImmutableString, Position, RhaiResult, RhaiResultOf, Scope, SharedModule, ERR,
|
||||
INT,
|
||||
Dynamic, Engine, ImmutableString, Position, RhaiResult, RhaiResultOf, Scope, ERR, INT,
|
||||
};
|
||||
use std::hash::{Hash, Hasher};
|
||||
#[cfg(feature = "no_std")]
|
||||
@@ -29,7 +28,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Dynamic,
|
||||
statements: &[Stmt],
|
||||
@@ -76,15 +74,8 @@ impl Engine {
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
let imports_len = global.num_imports();
|
||||
|
||||
let result = self.eval_stmt(
|
||||
global,
|
||||
caches,
|
||||
lib,
|
||||
scope,
|
||||
this_ptr,
|
||||
stmt,
|
||||
restore_orig_state,
|
||||
)?;
|
||||
let result =
|
||||
self.eval_stmt(global, caches, scope, this_ptr, stmt, restore_orig_state)?;
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
if matches!(stmt, Stmt::Import(..)) {
|
||||
@@ -120,7 +111,6 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
op_info: &OpAssignment,
|
||||
target: &mut Target,
|
||||
root: (&str, Position),
|
||||
@@ -154,7 +144,7 @@ impl Engine {
|
||||
global.level += 1;
|
||||
let global = &*RestoreOnDrop::lock(global, move |g| g.level -= 1);
|
||||
|
||||
let context = (self, op, None, global, lib, *op_pos).into();
|
||||
let context = (self, op, None, global, *op_pos).into();
|
||||
return func(context, args).map(|_| ());
|
||||
}
|
||||
}
|
||||
@@ -163,9 +153,9 @@ impl Engine {
|
||||
let op = op_token.literal_syntax();
|
||||
let token = Some(op_assign_token);
|
||||
|
||||
match self.exec_native_fn_call(
|
||||
global, caches, lib, op_assign, token, hash, args, true, *op_pos,
|
||||
) {
|
||||
match self
|
||||
.exec_native_fn_call(global, caches, op_assign, token, hash, args, true, *op_pos)
|
||||
{
|
||||
Ok(_) => (),
|
||||
Err(err) if matches!(*err, ERR::ErrorFunctionNotFound(ref f, ..) if f.starts_with(op_assign)) =>
|
||||
{
|
||||
@@ -174,7 +164,7 @@ impl Engine {
|
||||
|
||||
*args[0] = self
|
||||
.exec_native_fn_call(
|
||||
global, caches, lib, op, token, *hash_op, args, true, *op_pos,
|
||||
global, caches, op, token, *hash_op, args, true, *op_pos,
|
||||
)
|
||||
.map_err(|err| err.fill_position(op_info.pos))?
|
||||
.0;
|
||||
@@ -210,14 +200,13 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &mut Scope,
|
||||
this_ptr: &mut Dynamic,
|
||||
stmt: &Stmt,
|
||||
rewind_scope: bool,
|
||||
) -> RhaiResult {
|
||||
#[cfg(feature = "debugging")]
|
||||
let reset = self.run_debugger_with_reset(global, caches, lib, scope, this_ptr, stmt)?;
|
||||
let reset = self.run_debugger_with_reset(global, caches, scope, this_ptr, stmt)?;
|
||||
#[cfg(feature = "debugging")]
|
||||
let global = &mut *RestoreOnDrop::lock(global, move |g| g.debugger.reset_status(reset));
|
||||
|
||||
@@ -228,7 +217,7 @@ impl Engine {
|
||||
if let Stmt::FnCall(x, pos) = stmt {
|
||||
self.track_operation(global, stmt.position())?;
|
||||
|
||||
return self.eval_fn_call_expr(global, caches, lib, scope, this_ptr, x, *pos);
|
||||
return self.eval_fn_call_expr(global, caches, scope, this_ptr, x, *pos);
|
||||
}
|
||||
|
||||
// Then assignments.
|
||||
@@ -241,11 +230,11 @@ impl Engine {
|
||||
|
||||
if let Expr::Variable(x, ..) = lhs {
|
||||
let rhs_val = self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, rhs)?
|
||||
.eval_expr(global, caches, scope, this_ptr, rhs)?
|
||||
.flatten();
|
||||
|
||||
let (mut lhs_ptr, pos) =
|
||||
self.search_namespace(global, caches, lib, scope, this_ptr, lhs)?;
|
||||
self.search_namespace(global, caches, scope, this_ptr, lhs)?;
|
||||
|
||||
let var_name = x.3.as_str();
|
||||
|
||||
@@ -266,7 +255,7 @@ impl Engine {
|
||||
let root = (var_name, pos);
|
||||
let lhs_ptr = &mut lhs_ptr;
|
||||
|
||||
self.eval_op_assignment(global, caches, lib, op_info, lhs_ptr, root, rhs_val)?;
|
||||
self.eval_op_assignment(global, caches, op_info, lhs_ptr, root, rhs_val)?;
|
||||
|
||||
return Ok(Dynamic::UNIT);
|
||||
}
|
||||
@@ -274,7 +263,7 @@ impl Engine {
|
||||
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||
{
|
||||
let mut rhs_val = self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, rhs)?
|
||||
.eval_expr(global, caches, scope, this_ptr, rhs)?
|
||||
.flatten();
|
||||
|
||||
// If value is a string, intern it
|
||||
@@ -293,12 +282,14 @@ impl Engine {
|
||||
}
|
||||
// idx_lhs[idx_expr] op= rhs
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Expr::Index(..) => self
|
||||
.eval_dot_index_chain(global, caches, lib, scope, this_ptr, lhs, _new_val),
|
||||
Expr::Index(..) => {
|
||||
self.eval_dot_index_chain(global, caches, scope, this_ptr, lhs, _new_val)
|
||||
}
|
||||
// dot_lhs.dot_rhs op= rhs
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
Expr::Dot(..) => self
|
||||
.eval_dot_index_chain(global, caches, lib, scope, this_ptr, lhs, _new_val),
|
||||
Expr::Dot(..) => {
|
||||
self.eval_dot_index_chain(global, caches, scope, this_ptr, lhs, _new_val)
|
||||
}
|
||||
_ => unreachable!("cannot assign to expression: {:?}", lhs),
|
||||
}?;
|
||||
|
||||
@@ -314,13 +305,13 @@ impl Engine {
|
||||
|
||||
// Expression as statement
|
||||
Stmt::Expr(expr) => self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, expr)
|
||||
.eval_expr(global, caches, scope, this_ptr, expr)
|
||||
.map(Dynamic::flatten),
|
||||
|
||||
// Block scope
|
||||
Stmt::Block(statements, ..) if statements.is_empty() => Ok(Dynamic::UNIT),
|
||||
Stmt::Block(statements, ..) => {
|
||||
self.eval_stmt_block(global, caches, lib, scope, this_ptr, statements, true)
|
||||
self.eval_stmt_block(global, caches, scope, this_ptr, statements, true)
|
||||
}
|
||||
|
||||
// If statement
|
||||
@@ -328,14 +319,14 @@ impl Engine {
|
||||
let (expr, if_block, else_block) = &**x;
|
||||
|
||||
let guard_val = self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, expr)?
|
||||
.eval_expr(global, caches, scope, this_ptr, expr)?
|
||||
.as_bool()
|
||||
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?;
|
||||
|
||||
if guard_val && !if_block.is_empty() {
|
||||
self.eval_stmt_block(global, caches, lib, scope, this_ptr, if_block, true)
|
||||
self.eval_stmt_block(global, caches, scope, this_ptr, if_block, true)
|
||||
} else if !guard_val && !else_block.is_empty() {
|
||||
self.eval_stmt_block(global, caches, lib, scope, this_ptr, else_block, true)
|
||||
self.eval_stmt_block(global, caches, scope, this_ptr, else_block, true)
|
||||
} else {
|
||||
Ok(Dynamic::UNIT)
|
||||
}
|
||||
@@ -355,7 +346,7 @@ impl Engine {
|
||||
|
||||
let mut result = None;
|
||||
|
||||
let value = self.eval_expr(global, caches, lib, scope, this_ptr, expr)?;
|
||||
let value = self.eval_expr(global, caches, scope, this_ptr, expr)?;
|
||||
|
||||
if value.is_hashable() {
|
||||
let hasher = &mut get_hasher();
|
||||
@@ -372,7 +363,7 @@ impl Engine {
|
||||
let cond_result = match block.condition {
|
||||
Expr::BoolConstant(b, ..) => b,
|
||||
ref c => self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, c)?
|
||||
.eval_expr(global, caches, scope, this_ptr, c)?
|
||||
.as_bool()
|
||||
.map_err(|typ| {
|
||||
self.make_type_mismatch_err::<bool>(typ, c.position())
|
||||
@@ -394,7 +385,7 @@ impl Engine {
|
||||
let cond_result = match block.condition {
|
||||
Expr::BoolConstant(b, ..) => b,
|
||||
ref c => self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, c)?
|
||||
.eval_expr(global, caches, scope, this_ptr, c)?
|
||||
.as_bool()
|
||||
.map_err(|typ| {
|
||||
self.make_type_mismatch_err::<bool>(typ, c.position())
|
||||
@@ -412,7 +403,7 @@ impl Engine {
|
||||
result
|
||||
.or_else(|| def_case.as_ref().map(|&index| &expressions[index].expr))
|
||||
.map_or(Ok(Dynamic::UNIT), |expr| {
|
||||
self.eval_expr(global, caches, lib, scope, this_ptr, expr)
|
||||
self.eval_expr(global, caches, scope, this_ptr, expr)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -428,7 +419,7 @@ impl Engine {
|
||||
|
||||
loop {
|
||||
if let Err(err) =
|
||||
self.eval_stmt_block(global, caches, lib, scope, this_ptr, body, true)
|
||||
self.eval_stmt_block(global, caches, scope, this_ptr, body, true)
|
||||
{
|
||||
match *err {
|
||||
ERR::LoopBreak(false, ..) => (),
|
||||
@@ -445,7 +436,7 @@ impl Engine {
|
||||
|
||||
loop {
|
||||
let condition = self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, expr)?
|
||||
.eval_expr(global, caches, scope, this_ptr, expr)?
|
||||
.as_bool()
|
||||
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?;
|
||||
|
||||
@@ -458,7 +449,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
if let Err(err) =
|
||||
self.eval_stmt_block(global, caches, lib, scope, this_ptr, body, true)
|
||||
self.eval_stmt_block(global, caches, scope, this_ptr, body, true)
|
||||
{
|
||||
match *err {
|
||||
ERR::LoopBreak(false, ..) => (),
|
||||
@@ -477,7 +468,7 @@ impl Engine {
|
||||
loop {
|
||||
if !body.is_empty() {
|
||||
if let Err(err) =
|
||||
self.eval_stmt_block(global, caches, lib, scope, this_ptr, body, true)
|
||||
self.eval_stmt_block(global, caches, scope, this_ptr, body, true)
|
||||
{
|
||||
match *err {
|
||||
ERR::LoopBreak(false, ..) => continue,
|
||||
@@ -488,7 +479,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
let condition = self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, expr)?
|
||||
.eval_expr(global, caches, scope, this_ptr, expr)?
|
||||
.as_bool()
|
||||
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?;
|
||||
|
||||
@@ -503,7 +494,7 @@ impl Engine {
|
||||
let (var_name, counter, expr, statements) = &**x;
|
||||
|
||||
let iter_obj = self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, expr)?
|
||||
.eval_expr(global, caches, scope, this_ptr, expr)?
|
||||
.flatten();
|
||||
|
||||
let iter_type = iter_obj.type_id();
|
||||
@@ -582,9 +573,7 @@ impl Engine {
|
||||
continue;
|
||||
}
|
||||
|
||||
match self
|
||||
.eval_stmt_block(global, caches, lib, scope, this_ptr, statements, true)
|
||||
{
|
||||
match self.eval_stmt_block(global, caches, scope, this_ptr, statements, true) {
|
||||
Ok(_) => (),
|
||||
Err(err) => match *err {
|
||||
ERR::LoopBreak(false, ..) => (),
|
||||
@@ -605,7 +594,7 @@ impl Engine {
|
||||
let is_break = options.contains(ASTFlags::BREAK);
|
||||
|
||||
let value = if let Some(ref expr) = expr {
|
||||
self.eval_expr(global, caches, lib, scope, this_ptr, expr)?
|
||||
self.eval_expr(global, caches, scope, this_ptr, expr)?
|
||||
} else {
|
||||
Dynamic::UNIT
|
||||
};
|
||||
@@ -624,7 +613,7 @@ impl Engine {
|
||||
catch_block,
|
||||
} = &**x;
|
||||
|
||||
match self.eval_stmt_block(global, caches, lib, scope, this_ptr, try_block, true) {
|
||||
match self.eval_stmt_block(global, caches, scope, this_ptr, try_block, true) {
|
||||
r @ Ok(_) => r,
|
||||
Err(err) if err.is_pseudo_error() => Err(err),
|
||||
Err(err) if !err.is_catchable() => Err(err),
|
||||
@@ -675,31 +664,23 @@ impl Engine {
|
||||
scope.push(catch_var.clone(), err_value);
|
||||
}
|
||||
|
||||
self.eval_stmt_block(
|
||||
global,
|
||||
caches,
|
||||
lib,
|
||||
scope,
|
||||
this_ptr,
|
||||
catch_block,
|
||||
true,
|
||||
)
|
||||
.map(|_| Dynamic::UNIT)
|
||||
.map_err(|result_err| match *result_err {
|
||||
// Re-throw exception
|
||||
ERR::ErrorRuntime(v, pos) if v.is_unit() => {
|
||||
err.set_position(pos);
|
||||
err
|
||||
}
|
||||
_ => result_err,
|
||||
})
|
||||
self.eval_stmt_block(global, caches, scope, this_ptr, catch_block, true)
|
||||
.map(|_| Dynamic::UNIT)
|
||||
.map_err(|result_err| match *result_err {
|
||||
// Re-throw exception
|
||||
ERR::ErrorRuntime(v, pos) if v.is_unit() => {
|
||||
err.set_position(pos);
|
||||
err
|
||||
}
|
||||
_ => result_err,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Throw value
|
||||
Stmt::Return(Some(expr), options, pos) if options.contains(ASTFlags::BREAK) => self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, expr)
|
||||
.eval_expr(global, caches, scope, this_ptr, expr)
|
||||
.and_then(|v| Err(ERR::ErrorRuntime(v.flatten(), *pos).into())),
|
||||
|
||||
// Empty throw
|
||||
@@ -709,7 +690,7 @@ impl Engine {
|
||||
|
||||
// Return value
|
||||
Stmt::Return(Some(expr), .., pos) => self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, expr)
|
||||
.eval_expr(global, caches, scope, this_ptr, expr)
|
||||
.and_then(|v| Err(ERR::Return(v.flatten(), *pos).into())),
|
||||
|
||||
// Empty return
|
||||
@@ -740,7 +721,7 @@ impl Engine {
|
||||
nesting_level: global.scope_level,
|
||||
will_shadow,
|
||||
};
|
||||
let context = EvalContext::new(self, global, caches, lib, scope, this_ptr);
|
||||
let context = EvalContext::new(self, global, caches, scope, this_ptr);
|
||||
|
||||
if !filter(true, info, context)? {
|
||||
return Err(ERR::ErrorForbiddenVariable(var_name.to_string(), *pos).into());
|
||||
@@ -749,7 +730,7 @@ impl Engine {
|
||||
|
||||
// Evaluate initial value
|
||||
let mut value = self
|
||||
.eval_expr(global, caches, lib, scope, this_ptr, expr)?
|
||||
.eval_expr(global, caches, scope, this_ptr, expr)?
|
||||
.flatten();
|
||||
|
||||
let _alias = if !rewind_scope {
|
||||
@@ -758,7 +739,7 @@ impl Engine {
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
if global.scope_level == 0
|
||||
&& access == AccessMode::ReadOnly
|
||||
&& lib.iter().any(|m| !m.is_empty())
|
||||
&& global.lib.iter().any(|m| !m.is_empty())
|
||||
{
|
||||
crate::func::locked_write(global.constants.get_or_insert_with(|| {
|
||||
crate::Shared::new(
|
||||
@@ -804,7 +785,7 @@ impl Engine {
|
||||
return Err(ERR::ErrorTooManyModules(*_pos).into());
|
||||
}
|
||||
|
||||
let v = self.eval_expr(global, caches, lib, scope, this_ptr, expr)?;
|
||||
let v = self.eval_expr(global, caches, scope, this_ptr, expr)?;
|
||||
let typ = v.type_name();
|
||||
let path = v.try_cast::<ImmutableString>().ok_or_else(|| {
|
||||
self.make_type_mismatch_err::<ImmutableString>(typ, expr.position())
|
||||
@@ -900,13 +881,12 @@ impl Engine {
|
||||
&self,
|
||||
global: &mut GlobalRuntimeState,
|
||||
caches: &mut Caches,
|
||||
lib: &[SharedModule],
|
||||
scope: &mut Scope,
|
||||
statements: &[Stmt],
|
||||
) -> RhaiResult {
|
||||
let mut this = Dynamic::NULL;
|
||||
|
||||
self.eval_stmt_block(global, caches, lib, scope, &mut this, statements, false)
|
||||
self.eval_stmt_block(global, caches, scope, &mut this, statements, false)
|
||||
.or_else(|err| match *err {
|
||||
ERR::Return(out, ..) => Ok(out),
|
||||
ERR::LoopBreak(..) => {
|
||||
|
Reference in New Issue
Block a user