Move lib into global.

This commit is contained in:
Stephen Chung
2022-11-10 11:49:10 +08:00
parent 3800a6d9f5
commit 3a028f2642
19 changed files with 353 additions and 500 deletions

View File

@@ -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(