Move level into GlobalRuntimeState.

This commit is contained in:
Stephen Chung
2022-11-08 21:28:20 +08:00
parent 5bae4d8a19
commit e93923b3b6
21 changed files with 409 additions and 493 deletions

View File

@@ -326,7 +326,6 @@ impl Engine {
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[SharedModule],
level: usize,
name: &str,
op_token: Option<&Token>,
hash: u64,
@@ -385,7 +384,7 @@ impl Engine {
// Run external function
let src = source.as_ref().map(|s| s.as_str());
let context = (self, name, src, &*global, lib, pos, level).into();
let context = (self, name, src, &*global, lib, pos).into();
let mut _result = if func.is_plugin_fn() {
let f = func.get_plugin_fn().unwrap();
@@ -401,7 +400,7 @@ impl Engine {
#[cfg(feature = "debugging")]
{
let trigger = match global.debugger.status {
crate::eval::DebuggerStatus::FunctionExit(n) => n >= level,
crate::eval::DebuggerStatus::FunctionExit(n) => n >= global.level,
crate::eval::DebuggerStatus::Next(.., true) => true,
_ => false,
};
@@ -415,8 +414,8 @@ impl Engine {
Err(ref err) => crate::eval::DebuggerEvent::FunctionExitWithError(err),
};
if let Err(err) = self
.run_debugger_raw(global, caches, lib, level, scope, &mut this, node, event)
if let Err(err) =
self.run_debugger_raw(global, caches, lib, scope, &mut this, node, event)
{
_result = Err(err);
}
@@ -539,7 +538,6 @@ impl Engine {
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[SharedModule],
level: usize,
_scope: Option<&mut Scope>,
fn_name: &str,
op_token: Option<&Token>,
@@ -561,7 +559,8 @@ impl Engine {
#[cfg(not(feature = "no_closure"))]
ensure_no_data_race(fn_name, args, is_ref_mut)?;
let level = level + 1;
global.level += 1;
let global = &mut *RestoreOnDrop::lock(global, move |g| g.level -= 1);
// These may be redirected from method style calls.
if hashes.is_native_only() {
@@ -647,7 +646,7 @@ impl Engine {
let (first_arg, rest_args) = args.split_first_mut().unwrap();
self.call_script_fn(
global, caches, lib, level, scope, first_arg, func, rest_args, true, pos,
global, caches, lib, scope, first_arg, func, rest_args, true, pos,
)
} else {
// Normal call of script function
@@ -667,7 +666,7 @@ impl Engine {
let mut this = Dynamic::NULL;
self.call_script_fn(
global, caches, lib, level, scope, &mut this, func, args, true, pos,
global, caches, lib, scope, &mut this, func, args, true, pos,
)
}
.map(|r| (r, false));
@@ -678,7 +677,7 @@ impl Engine {
let hash = hashes.native();
self.exec_native_fn_call(
global, caches, lib, level, fn_name, op_token, hash, args, is_ref_mut, pos,
global, caches, lib, fn_name, op_token, hash, args, is_ref_mut, pos,
)
}
@@ -689,7 +688,6 @@ impl Engine {
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[SharedModule],
level: usize,
scope: &mut Scope,
this_ptr: &mut Dynamic,
arg_expr: &Expr,
@@ -699,7 +697,7 @@ impl Engine {
self.track_operation(global, arg_expr.start_position())?;
#[cfg(feature = "debugging")]
self.run_debugger(global, caches, lib, level, scope, this_ptr, arg_expr)?;
self.run_debugger(global, caches, lib, scope, this_ptr, arg_expr)?;
return Ok((value, arg_expr.start_position()));
}
@@ -712,7 +710,7 @@ impl Engine {
#[cfg(feature = "debugging")]
let global = &mut *RestoreOnDrop::lock(global, move |g| g.debugger.reset_status(reset));
self.eval_expr(global, caches, lib, level, scope, this_ptr, arg_expr)
self.eval_expr(global, caches, lib, scope, this_ptr, arg_expr)
.map(|r| (r, arg_expr.start_position()))
}
@@ -723,7 +721,6 @@ impl Engine {
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[SharedModule],
level: usize,
fn_name: &str,
mut hash: FnCallHashes,
target: &mut crate::eval::Target,
@@ -764,7 +761,6 @@ impl Engine {
global,
caches,
lib,
level,
None,
fn_name,
None,
@@ -820,7 +816,6 @@ impl Engine {
global,
caches,
lib,
level,
None,
&fn_name,
None,
@@ -921,7 +916,6 @@ impl Engine {
global,
caches,
lib,
level,
None,
fn_name,
None,
@@ -948,7 +942,6 @@ impl Engine {
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[SharedModule],
level: usize,
scope: &mut Scope,
this_ptr: &mut Dynamic,
fn_name: &str,
@@ -974,7 +967,7 @@ impl Engine {
KEYWORD_FN_PTR_CALL if total_args >= 1 => {
let arg = first_arg.unwrap();
let (arg_value, arg_pos) =
self.get_arg_value(global, caches, lib, level, scope, this_ptr, arg)?;
self.get_arg_value(global, caches, lib, scope, this_ptr, arg)?;
if !arg_value.is::<FnPtr>() {
let typ = self.map_type_name(arg_value.type_name());
@@ -1015,7 +1008,7 @@ impl Engine {
KEYWORD_FN_PTR if total_args == 1 => {
let arg = first_arg.unwrap();
let (arg_value, arg_pos) =
self.get_arg_value(global, caches, lib, level, scope, this_ptr, arg)?;
self.get_arg_value(global, caches, lib, scope, this_ptr, arg)?;
// Fn - only in function call style
return arg_value
@@ -1030,7 +1023,7 @@ impl Engine {
KEYWORD_FN_PTR_CURRY if total_args > 1 => {
let first = first_arg.unwrap();
let (arg_value, arg_pos) =
self.get_arg_value(global, caches, lib, level, scope, this_ptr, first)?;
self.get_arg_value(global, caches, lib, scope, this_ptr, first)?;
if !arg_value.is::<FnPtr>() {
let typ = self.map_type_name(arg_value.type_name());
@@ -1042,7 +1035,7 @@ impl Engine {
// Append the new curried arguments to the existing list.
let fn_curry = a_expr.iter().try_fold(fn_curry, |mut curried, expr| {
let (value, ..) =
self.get_arg_value(global, caches, lib, level, scope, this_ptr, expr)?;
self.get_arg_value(global, caches, lib, scope, this_ptr, expr)?;
curried.push(value);
Ok::<_, RhaiError>(curried)
})?;
@@ -1055,7 +1048,7 @@ impl Engine {
crate::engine::KEYWORD_IS_SHARED if total_args == 1 => {
let arg = first_arg.unwrap();
let (arg_value, ..) =
self.get_arg_value(global, caches, lib, level, scope, this_ptr, arg)?;
self.get_arg_value(global, caches, lib, scope, this_ptr, arg)?;
return Ok(arg_value.is_shared().into());
}
@@ -1064,14 +1057,14 @@ impl Engine {
crate::engine::KEYWORD_IS_DEF_FN if total_args == 2 => {
let first = first_arg.unwrap();
let (arg_value, arg_pos) =
self.get_arg_value(global, caches, lib, level, scope, this_ptr, first)?;
self.get_arg_value(global, caches, lib, scope, this_ptr, first)?;
let fn_name = arg_value
.into_immutable_string()
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, arg_pos))?;
let (arg_value, arg_pos) =
self.get_arg_value(global, caches, lib, level, scope, this_ptr, &a_expr[0])?;
self.get_arg_value(global, caches, lib, scope, this_ptr, &a_expr[0])?;
let num_params = arg_value
.as_int()
@@ -1090,7 +1083,7 @@ impl Engine {
KEYWORD_IS_DEF_VAR if total_args == 1 => {
let arg = first_arg.unwrap();
let (arg_value, arg_pos) =
self.get_arg_value(global, caches, lib, level, scope, this_ptr, arg)?;
self.get_arg_value(global, caches, lib, scope, this_ptr, arg)?;
let var_name = arg_value
.into_immutable_string()
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, arg_pos))?;
@@ -1105,12 +1098,15 @@ impl Engine {
let orig_imports_len = global.num_imports();
let arg = first_arg.unwrap();
let (arg_value, pos) =
self.get_arg_value(global, caches, lib, level, scope, this_ptr, arg)?;
self.get_arg_value(global, caches, lib, scope, this_ptr, arg)?;
let s = &arg_value
.into_immutable_string()
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, pos))?;
let result =
self.eval_script_expr_in_place(global, caches, lib, level + 1, scope, s, pos);
global.level += 1;
let global = &mut *RestoreOnDrop::lock(global, move |g| g.level -= 1);
let result = self.eval_script_expr_in_place(global, caches, lib, scope, s, pos);
// IMPORTANT! If the eval defines new variables in the current scope,
// all variable offsets from this point on will be mis-aligned.
@@ -1152,7 +1148,7 @@ impl Engine {
.copied()
.chain(a_expr.iter())
.try_for_each(|expr| {
self.get_arg_value(global, caches, lib, level, scope, this_ptr, expr)
self.get_arg_value(global, caches, lib, scope, this_ptr, expr)
.map(|(value, ..)| arg_values.push(value.flatten()))
})?;
args.extend(curry.iter_mut());
@@ -1163,8 +1159,8 @@ impl Engine {
return self
.exec_fn_call(
global, caches, lib, level, scope, name, op_token, hashes, &mut args,
is_ref_mut, false, pos,
global, caches, lib, scope, name, op_token, hashes, &mut args, is_ref_mut,
false, pos,
)
.map(|(v, ..)| v);
}
@@ -1180,16 +1176,16 @@ impl Engine {
let first_expr = first_arg.unwrap();
#[cfg(feature = "debugging")]
self.run_debugger(global, caches, lib, level, scope, this_ptr, first_expr)?;
self.run_debugger(global, caches, lib, scope, this_ptr, first_expr)?;
// func(x, ...) -> x.func(...)
a_expr.iter().try_for_each(|expr| {
self.get_arg_value(global, caches, lib, level, scope, this_ptr, expr)
self.get_arg_value(global, caches, lib, scope, this_ptr, expr)
.map(|(value, ..)| arg_values.push(value.flatten()))
})?;
let (mut target, _pos) =
self.search_namespace(global, caches, lib, level, scope, this_ptr, first_expr)?;
self.search_namespace(global, caches, lib, scope, this_ptr, first_expr)?;
if target.is_read_only() {
target = target.into_owned();
@@ -1216,7 +1212,7 @@ impl Engine {
.into_iter()
.chain(a_expr.iter())
.try_for_each(|expr| {
self.get_arg_value(global, caches, lib, level, scope, this_ptr, expr)
self.get_arg_value(global, caches, lib, scope, this_ptr, expr)
.map(|(value, ..)| arg_values.push(value.flatten()))
})?;
args.extend(curry.iter_mut());
@@ -1226,8 +1222,7 @@ impl Engine {
}
self.exec_fn_call(
global, caches, lib, level, None, name, op_token, hashes, &mut args, is_ref_mut, false,
pos,
global, caches, lib, None, name, op_token, hashes, &mut args, is_ref_mut, false, pos,
)
.map(|(v, ..)| v)
}
@@ -1239,7 +1234,6 @@ impl Engine {
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[SharedModule],
level: usize,
scope: &mut Scope,
this_ptr: &mut Dynamic,
namespace: &crate::ast::Namespace,
@@ -1260,20 +1254,20 @@ impl Engine {
// and avoid cloning the value
if !args_expr.is_empty() && args_expr[0].is_variable_access(true) {
#[cfg(feature = "debugging")]
self.run_debugger(global, caches, lib, level, scope, this_ptr, &args_expr[0])?;
self.run_debugger(global, caches, lib, scope, this_ptr, &args_expr[0])?;
// func(x, ...) -> x.func(...)
arg_values.push(Dynamic::UNIT);
args_expr.iter().skip(1).try_for_each(|expr| {
self.get_arg_value(global, caches, lib, level, scope, this_ptr, expr)
self.get_arg_value(global, caches, lib, scope, this_ptr, expr)
.map(|(value, ..)| arg_values.push(value.flatten()))
})?;
// Get target reference to first argument
let first_arg = &args_expr[0];
let (target, _pos) =
self.search_scope_only(global, caches, lib, level, scope, this_ptr, first_arg)?;
self.search_scope_only(global, caches, lib, scope, this_ptr, first_arg)?;
self.track_operation(global, _pos)?;
@@ -1296,7 +1290,7 @@ impl Engine {
} else {
// func(..., ...) or func(mod::x, ...)
args_expr.iter().try_for_each(|expr| {
self.get_arg_value(global, caches, lib, level, scope, this_ptr, expr)
self.get_arg_value(global, caches, lib, scope, this_ptr, expr)
.map(|(value, ..)| arg_values.push(value.flatten()))
})?;
args.extend(arg_values.iter_mut());
@@ -1363,7 +1357,8 @@ impl Engine {
}
}
let level = level + 1;
global.level += 1;
let global = &mut *RestoreOnDrop::lock(global, move |g| g.level -= 1);
match func {
#[cfg(not(feature = "no_function"))]
@@ -1376,12 +1371,12 @@ impl Engine {
let global = &mut *RestoreOnDrop::lock(global, move |g| g.source = orig_source);
self.call_script_fn(
global, caches, lib, level, new_scope, &mut this, fn_def, &mut args, true, pos,
global, caches, lib, new_scope, &mut this, fn_def, &mut args, true, pos,
)
}
Some(f) if f.is_plugin_fn() => {
let context = (self, fn_name, module.id(), &*global, lib, pos, level).into();
let context = (self, fn_name, module.id(), &*global, lib, pos).into();
let f = f.get_plugin_fn().expect("plugin function");
let result = if !f.is_pure() && !args.is_empty() && args[0].is_read_only() {
Err(ERR::ErrorNonPureMethodCallOnConstant(fn_name.to_string(), pos).into())
@@ -1393,7 +1388,7 @@ impl Engine {
Some(f) if f.is_native() => {
let func = f.get_native_fn().expect("native function");
let context = (self, fn_name, module.id(), &*global, lib, pos, level).into();
let context = (self, fn_name, module.id(), &*global, lib, pos).into();
let result = func(context, &mut args);
self.check_return_value(result, pos)
}
@@ -1422,7 +1417,6 @@ impl Engine {
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[SharedModule],
level: usize,
scope: &mut Scope,
script: &str,
_pos: Position,
@@ -1458,7 +1452,7 @@ impl Engine {
}
// Evaluate the AST
self.eval_global_statements(global, caches, lib, level, scope, statements)
self.eval_global_statements(global, caches, lib, scope, statements)
}
/// Evaluate a function call expression.
@@ -1467,7 +1461,6 @@ impl Engine {
global: &mut GlobalRuntimeState,
caches: &mut Caches,
lib: &[SharedModule],
level: usize,
scope: &mut Scope,
this_ptr: &mut Dynamic,
expr: &FnCallExpr,
@@ -1489,12 +1482,12 @@ impl Engine {
// Short-circuit native binary operator call if under Fast Operators mode
if op_token.is_some() && self.fast_operators() && args.len() == 2 {
let mut lhs = self
.get_arg_value(global, caches, lib, level, scope, this_ptr, &args[0])?
.get_arg_value(global, caches, lib, scope, this_ptr, &args[0])?
.0
.flatten();
let mut rhs = self
.get_arg_value(global, caches, lib, level, scope, this_ptr, &args[1])?
.get_arg_value(global, caches, lib, scope, this_ptr, &args[1])?
.0
.flatten();
@@ -1504,14 +1497,16 @@ impl Engine {
get_builtin_binary_op_fn(op_token.as_ref().unwrap(), operands[0], operands[1])
{
// Built-in found
let context = (self, name.as_str(), None, &*global, lib, pos, level + 1).into();
global.level += 1;
let global = &*RestoreOnDrop::lock(global, move |g| g.level -= 1);
let context = (self, name.as_str(), None, global, lib, pos).into();
return func(context, operands);
}
return self
.exec_fn_call(
global, caches, lib, level, None, name, op_token, *hashes, operands, false,
false, pos,
global, caches, lib, None, name, op_token, *hashes, operands, false, false, pos,
)
.map(|(v, ..)| v);
}
@@ -1522,7 +1517,7 @@ impl Engine {
let hash = hashes.native();
return self.make_qualified_function_call(
global, caches, lib, level, scope, this_ptr, namespace, name, args, hash, pos,
global, caches, lib, scope, this_ptr, namespace, name, args, hash, pos,
);
}
@@ -1533,7 +1528,7 @@ impl Engine {
);
self.make_function_call(
global, caches, lib, level, scope, this_ptr, name, op_token, first_arg, args, *hashes,
global, caches, lib, scope, this_ptr, name, op_token, first_arg, args, *hashes,
*capture, pos,
)
}