diff --git a/RELEASES.md b/RELEASES.md
index 1e200e97..48ef1e69 100644
--- a/RELEASES.md
+++ b/RELEASES.md
@@ -209,7 +209,7 @@ Bug fixes
---------
* Fixes bug that prevents calling functions in closures.
-* Fixes bug that erroneously consumes the first argument to a module-qualified function call.
+* Fixes bug that erroneously consumes the first argument to a namespace-qualified function call.
New features
------------
@@ -299,7 +299,7 @@ New features
* The boolean `^` (XOR) operator is added.
* `FnPtr` is exposed as the function pointer type.
* `rhai::module_resolvers::ModuleResolversCollection` added to try a list of module resolvers.
-* It is now possible to mutate the first argument of a module-qualified function call when the argument is a simple variable (but not a module constant).
+* It is now possible to mutate the first argument of a namespace-qualified function call when the argument is a simple variable (but not a module constant).
* Many configuration/setting API's now returns `&mut Self` so that the calls can be chained.
* `String` parameters in functions are supported (but inefficiently).
diff --git a/doc/src/language/fn-namespaces.md b/doc/src/language/fn-namespaces.md
index 0d798ae4..ba551367 100644
--- a/doc/src/language/fn-namespaces.md
+++ b/doc/src/language/fn-namespaces.md
@@ -15,10 +15,10 @@ allow combining all functions in one [`AST`] into another, forming a new, unifie
In general, there are two types of _namespaces_ where functions are looked up:
-| Namespace | Source | Lookup method | Sub-modules? | Variables? |
-| --------- | ------------------------------------------------------------------------------------- | ------------------------------ | :----------: | :--------: |
-| Global | 1) `Engine::register_XXX` API
2) [`AST`] being evaluated
3) [packages] loaded | simple function name | ignored | ignored |
-| Module | [`Module`] | module-qualified function name | yes | yes |
+| Namespace | Source | Lookup method | Sub-modules? | Variables? |
+| --------- | ------------------------------------------------------------------------------------- | --------------------------------- | :----------: | :--------: |
+| Global | 1) `Engine::register_XXX` API
2) [`AST`] being evaluated
3) [packages] loaded | simple function name | ignored | ignored |
+| Module | [`Module`] | namespace-qualified function name | yes | yes |
Global Namespace
diff --git a/doc/src/rust/modules/create.md b/doc/src/rust/modules/create.md
index 51462b22..069e5896 100644
--- a/doc/src/rust/modules/create.md
+++ b/doc/src/rust/modules/create.md
@@ -69,9 +69,9 @@ resolver.insert("question", module);
let mut engine = Engine::new();
engine.set_module_resolver(Some(resolver));
-// Use module-qualified variables
+// Use namespace-qualified variables
engine.eval::(r#"import "question" as q; q::answer + 1"#)? == 42;
-// Call module-qualified functions
+// Call namespace-qualified functions
engine.eval::(r#"import "question" as q; q::inc(q::answer)"#)? == 42;
```
diff --git a/src/ast.rs b/src/ast.rs
index e5409b24..d99e6a74 100644
--- a/src/ast.rs
+++ b/src/ast.rs
@@ -2,7 +2,7 @@
use crate::dynamic::{Dynamic, Union};
use crate::fn_native::{FnPtr, Shared};
-use crate::module::{Module, ModuleRef};
+use crate::module::{Module, NamespaceRef};
use crate::syntax::FnCustomSyntaxEval;
use crate::token::{Position, Token, NO_POS};
use crate::utils::ImmutableString;
@@ -778,7 +778,7 @@ impl Stmt {
}
}
-/// _[INTERNALS]_ A type wrapping a custom syntax definition.
+/// _[INTERNALS]_ A custom syntax definition.
/// Exported under the `internals` feature only.
///
/// ## WARNING
@@ -823,7 +823,7 @@ impl CustomExpr {
/// Exported under the `internals` feature only.
///
/// This type is mainly used to provide a standard `Hash` implementation
-/// to floating-point numbers, allowing `Expr` to derive `Hash` automatically.
+/// for floating-point numbers, allowing `Expr` to derive `Hash`.
///
/// ## WARNING
///
@@ -856,7 +856,7 @@ impl From for FloatWrapper {
}
}
-/// A binary expression structure.
+/// _[INTERNALS]_ A binary expression.
/// Exported under the `internals` feature only.
///
/// ## WARNING
@@ -877,7 +877,7 @@ pub struct BinaryExpr {
///
/// This type is volatile and may change.
#[derive(Debug, Clone, Hash, Default)]
-pub struct FnCallInfo {
+pub struct FnCallExpr {
/// Pre-calculated hash for a script-defined function of the same name and number of parameters.
pub hash: u64,
/// Call native functions only? Set to `true` to skip searching for script-defined function overrides
@@ -889,7 +889,7 @@ pub struct FnCallInfo {
/// Type is `bool` in order for `FnCallInfo` to be `Hash`
pub def_value: Option,
/// Namespace of the function, if any. Boxed because it occurs rarely.
- pub namespace: Option>,
+ pub namespace: Option>,
/// Function name.
/// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls
/// and the function names are predictable, so no need to allocate a new `String`.
@@ -918,7 +918,7 @@ pub enum Expr {
/// FnPtr constant.
FnPointer(Box),
/// Variable access - (optional index, optional modules, hash, variable name)
- Variable(Box<(Option, Option>, u64, Ident)>),
+ Variable(Box<(Option, Option>, u64, Ident)>),
/// Property access - (getter, setter), prop
Property(Box<((String, String), IdentX)>),
/// { stmt }
@@ -926,7 +926,7 @@ pub enum Expr {
/// Wrapped expression - should not be optimized away.
Expr(Box),
/// func(expr, ... )
- FnCall(Box, Position),
+ FnCall(Box, Position),
/// lhs.rhs
Dot(Box, Position),
/// expr[expr]
diff --git a/src/engine.rs b/src/engine.rs
index 57ced10c..b2b4480f 100644
--- a/src/engine.rs
+++ b/src/engine.rs
@@ -1,10 +1,10 @@
//! Main module defining the script evaluation `Engine`.
-use crate::ast::{BinaryExpr, Expr, FnCallInfo, Ident, IdentX, ReturnType, Stmt};
+use crate::ast::{BinaryExpr, Expr, FnCallExpr, Ident, IdentX, ReturnType, Stmt};
use crate::dynamic::{map_std_type_name, Dynamic, Union, Variant};
use crate::fn_call::run_builtin_op_assignment;
use crate::fn_native::{Callback, FnPtr, OnVarCallback, Shared};
-use crate::module::{Module, ModuleRef};
+use crate::module::{Module, NamespaceRef};
use crate::optimize::OptimizationLevel;
use crate::packages::{Package, PackagesCollection, StandardPackage};
use crate::r#unsafe::unsafe_cast_var_name_to_lifetime;
@@ -593,7 +593,7 @@ pub struct Engine {
/// Max limits.
#[cfg(not(feature = "unchecked"))]
- pub(crate) limits_set: Limits,
+ pub(crate) limits: Limits,
}
impl fmt::Debug for Engine {
@@ -636,6 +636,7 @@ pub fn is_anonymous_fn(fn_name: &str) -> bool {
}
/// Print/debug to stdout
+#[inline(always)]
fn default_print(_s: &str) {
#[cfg(not(feature = "no_std"))]
#[cfg(not(target_arch = "wasm32"))]
@@ -647,15 +648,15 @@ fn default_print(_s: &str) {
pub fn search_imports(
mods: &Imports,
state: &mut State,
- modules: &ModuleRef,
+ namespace: &NamespaceRef,
) -> Result, Box> {
- let Ident { name: root, pos } = &modules[0];
+ let Ident { name: root, pos } = &namespace[0];
// Qualified - check if the root module is directly indexed
let index = if state.always_search {
0
} else {
- modules.index().map_or(0, NonZeroUsize::get)
+ namespace.index().map_or(0, NonZeroUsize::get)
};
Ok(if index > 0 {
@@ -670,7 +671,7 @@ pub fn search_imports(
impl Engine {
/// Create a new `Engine`
- #[inline(always)]
+ #[inline]
pub fn new() -> Self {
// Create the new scripting Engine
let mut engine = Self {
@@ -710,7 +711,7 @@ impl Engine {
},
#[cfg(not(feature = "unchecked"))]
- limits_set: Limits {
+ limits: Limits {
max_call_stack_depth: MAX_CALL_STACK_DEPTH,
max_expr_depth: MAX_EXPR_DEPTH,
#[cfg(not(feature = "no_function"))]
@@ -733,7 +734,7 @@ impl Engine {
/// Create a new `Engine` with minimal built-in functions.
/// Use the `load_package` method to load additional packages of functions.
- #[inline(always)]
+ #[inline]
pub fn new_raw() -> Self {
Self {
id: Default::default(),
@@ -762,7 +763,7 @@ impl Engine {
},
#[cfg(not(feature = "unchecked"))]
- limits_set: Limits {
+ limits: Limits {
max_call_stack_depth: MAX_CALL_STACK_DEPTH,
max_expr_depth: MAX_EXPR_DEPTH,
#[cfg(not(feature = "no_function"))]
@@ -780,7 +781,7 @@ impl Engine {
}
/// Search for a variable within the scope or within imports,
- /// depending on whether the variable name is qualified.
+ /// depending on whether the variable name is namespace-qualified.
pub(crate) fn search_namespace<'s, 'a>(
&self,
scope: &'s mut Scope,
@@ -1000,7 +1001,7 @@ impl Engine {
match rhs {
// xxx.fn_name(arg_expr_list)
Expr::FnCall(x, pos) if x.namespace.is_none() => {
- let FnCallInfo {
+ let FnCallExpr {
name,
native_only: native,
hash,
@@ -1073,7 +1074,7 @@ impl Engine {
}
// {xxx:map}.fn_name(arg_expr_list)[expr] | {xxx:map}.fn_name(arg_expr_list).expr
Expr::FnCall(x, pos) if x.namespace.is_none() => {
- let FnCallInfo {
+ let FnCallExpr {
name,
native_only: native,
hash,
@@ -1157,7 +1158,7 @@ impl Engine {
}
// xxx.fn_name(arg_expr_list)[expr] | xxx.fn_name(arg_expr_list).expr
Expr::FnCall(f, pos) if f.namespace.is_none() => {
- let FnCallInfo {
+ let FnCallExpr {
name,
native_only: native,
hash,
@@ -1209,14 +1210,7 @@ impl Engine {
level: usize,
new_val: Option<(Dynamic, Position)>,
) -> Result> {
- let (
- BinaryExpr {
- lhs: dot_lhs,
- rhs: dot_rhs,
- },
- chain_type,
- op_pos,
- ) = match expr {
+ let (BinaryExpr { lhs, rhs }, chain_type, op_pos) = match expr {
Expr::Index(x, pos) => (x.as_ref(), ChainType::Index, *pos),
Expr::Dot(x, pos) => (x.as_ref(), ChainType::Dot, *pos),
_ => unreachable!(),
@@ -1230,14 +1224,14 @@ impl Engine {
state,
lib,
this_ptr,
- dot_rhs,
+ rhs,
chain_type,
&mut idx_values,
0,
level,
)?;
- match dot_lhs {
+ match lhs {
// id.??? or id[???]
Expr::Variable(x) => {
let Ident {
@@ -1249,7 +1243,7 @@ impl Engine {
.map_err(|err| err.fill_position(*var_pos))?;
let (target, _, typ, pos) =
- self.search_namespace(scope, mods, state, lib, this_ptr, dot_lhs)?;
+ self.search_namespace(scope, mods, state, lib, this_ptr, lhs)?;
// Constants cannot be modified
match typ {
@@ -1262,7 +1256,7 @@ impl Engine {
let obj_ptr = &mut target.into();
self.eval_dot_index_chain_helper(
- mods, state, lib, &mut None, obj_ptr, dot_rhs, idx_values, chain_type, level,
+ mods, state, lib, &mut None, obj_ptr, rhs, idx_values, chain_type, level,
new_val,
)
.map(|(v, _)| v)
@@ -1275,7 +1269,7 @@ impl Engine {
let val = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
let obj_ptr = &mut val.into();
self.eval_dot_index_chain_helper(
- mods, state, lib, this_ptr, obj_ptr, dot_rhs, idx_values, chain_type, level,
+ mods, state, lib, this_ptr, obj_ptr, rhs, idx_values, chain_type, level,
new_val,
)
.map(|(v, _)| v)
@@ -1605,7 +1599,7 @@ impl Engine {
// Normal function call
Expr::FnCall(x, pos) if x.namespace.is_none() => {
- let FnCallInfo {
+ let FnCallExpr {
name,
native_only: native,
capture: cap_scope,
@@ -1622,9 +1616,9 @@ impl Engine {
.map_err(|err| err.fill_position(*pos))
}
- // Module-qualified function call
+ // Namespace-qualified function call
Expr::FnCall(x, pos) if x.namespace.is_some() => {
- let FnCallInfo {
+ let FnCallExpr {
name,
namespace,
hash,
@@ -1632,9 +1626,9 @@ impl Engine {
def_value,
..
} = x.as_ref();
- let modules = namespace.as_ref().map(|v| v.as_ref());
+ let namespace = namespace.as_ref().map(|v| v.as_ref());
self.make_qualified_function_call(
- scope, mods, state, lib, this_ptr, modules, name, args, *def_value, *hash,
+ scope, mods, state, lib, this_ptr, namespace, name, args, *def_value, *hash,
level,
)
.map_err(|err| err.fill_position(*pos))
diff --git a/src/engine_api.rs b/src/engine_api.rs
index 084c2040..3207fc94 100644
--- a/src/engine_api.rs
+++ b/src/engine_api.rs
@@ -1156,10 +1156,9 @@ impl Engine {
) -> Result {
let scripts = [script];
let stream = self.lex(&scripts, None);
- {
- let mut peekable = stream.peekable();
- self.parse_global_expr(&mut peekable, scope, self.optimization_level)
- }
+
+ let mut peekable = stream.peekable();
+ self.parse_global_expr(&mut peekable, scope, self.optimization_level)
}
/// Evaluate a script file.
diff --git a/src/engine_settings.rs b/src/engine_settings.rs
index 987187f1..b6d7cfd9 100644
--- a/src/engine_settings.rs
+++ b/src/engine_settings.rs
@@ -53,7 +53,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn set_max_call_levels(&mut self, levels: usize) -> &mut Self {
- self.limits_set.max_call_stack_depth = levels;
+ self.limits.max_call_stack_depth = levels;
self
}
@@ -61,7 +61,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn max_call_levels(&self) -> usize {
- self.limits_set.max_call_stack_depth
+ self.limits.max_call_stack_depth
}
/// Set the maximum number of operations allowed for a script to run to avoid
@@ -69,7 +69,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn set_max_operations(&mut self, operations: u64) -> &mut Self {
- self.limits_set.max_operations = if operations == u64::MAX {
+ self.limits.max_operations = if operations == u64::MAX {
0
} else {
operations
@@ -81,7 +81,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn max_operations(&self) -> u64 {
- self.limits_set.max_operations
+ self.limits.max_operations
}
/// Set the maximum number of imported modules allowed for a script.
@@ -89,7 +89,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
#[inline(always)]
pub fn set_max_modules(&mut self, modules: usize) -> &mut Self {
- self.limits_set.max_modules = modules;
+ self.limits.max_modules = modules;
self
}
@@ -98,7 +98,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))]
#[inline(always)]
pub fn max_modules(&self) -> usize {
- self.limits_set.max_modules
+ self.limits.max_modules
}
/// Set the depth limits for expressions (0 for unlimited).
@@ -109,14 +109,14 @@ impl Engine {
max_expr_depth: usize,
#[cfg(not(feature = "no_function"))] max_function_expr_depth: usize,
) -> &mut Self {
- self.limits_set.max_expr_depth = if max_expr_depth == usize::MAX {
+ self.limits.max_expr_depth = if max_expr_depth == usize::MAX {
0
} else {
max_expr_depth
};
#[cfg(not(feature = "no_function"))]
{
- self.limits_set.max_function_expr_depth = if max_function_expr_depth == usize::MAX {
+ self.limits.max_function_expr_depth = if max_function_expr_depth == usize::MAX {
0
} else {
max_function_expr_depth
@@ -129,7 +129,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn max_expr_depth(&self) -> usize {
- self.limits_set.max_expr_depth
+ self.limits.max_expr_depth
}
/// The depth limit for expressions in functions (0 for unlimited).
@@ -137,14 +137,14 @@ impl Engine {
#[cfg(not(feature = "no_function"))]
#[inline(always)]
pub fn max_function_expr_depth(&self) -> usize {
- self.limits_set.max_function_expr_depth
+ self.limits.max_function_expr_depth
}
/// Set the maximum length of strings (0 for unlimited).
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn set_max_string_size(&mut self, max_size: usize) -> &mut Self {
- self.limits_set.max_string_size = if max_size == usize::MAX { 0 } else { max_size };
+ self.limits.max_string_size = if max_size == usize::MAX { 0 } else { max_size };
self
}
@@ -152,7 +152,7 @@ impl Engine {
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
pub fn max_string_size(&self) -> usize {
- self.limits_set.max_string_size
+ self.limits.max_string_size
}
/// Set the maximum length of arrays (0 for unlimited).
@@ -160,7 +160,7 @@ impl Engine {
#[cfg(not(feature = "no_index"))]
#[inline(always)]
pub fn set_max_array_size(&mut self, max_size: usize) -> &mut Self {
- self.limits_set.max_array_size = if max_size == usize::MAX { 0 } else { max_size };
+ self.limits.max_array_size = if max_size == usize::MAX { 0 } else { max_size };
self
}
@@ -169,7 +169,7 @@ impl Engine {
#[cfg(not(feature = "no_index"))]
#[inline(always)]
pub fn max_array_size(&self) -> usize {
- self.limits_set.max_array_size
+ self.limits.max_array_size
}
/// Set the maximum length of object maps (0 for unlimited).
@@ -177,7 +177,7 @@ impl Engine {
#[cfg(not(feature = "no_object"))]
#[inline(always)]
pub fn set_max_map_size(&mut self, max_size: usize) -> &mut Self {
- self.limits_set.max_map_size = if max_size == usize::MAX { 0 } else { max_size };
+ self.limits.max_map_size = if max_size == usize::MAX { 0 } else { max_size };
self
}
@@ -186,7 +186,7 @@ impl Engine {
#[cfg(not(feature = "no_object"))]
#[inline(always)]
pub fn max_map_size(&self) -> usize {
- self.limits_set.max_map_size
+ self.limits.max_map_size
}
/// Set the module resolution service used by the `Engine`.
diff --git a/src/fn_call.rs b/src/fn_call.rs
index 3c258f93..cc740594 100644
--- a/src/fn_call.rs
+++ b/src/fn_call.rs
@@ -8,7 +8,7 @@ use crate::engine::{
KEYWORD_PRINT, KEYWORD_TYPE_OF,
};
use crate::fn_native::{FnCallArgs, FnPtr};
-use crate::module::{Module, ModuleRef};
+use crate::module::{Module, NamespaceRef};
use crate::optimize::OptimizationLevel;
use crate::parse_error::ParseErrorType;
use crate::result::EvalAltResult;
@@ -432,13 +432,13 @@ impl Engine {
pub(crate) fn has_override_by_name_and_arguments(
&self,
lib: &[&Module],
- name: &str,
+ fn_name: &str,
arg_types: impl AsRef<[TypeId]>,
pub_only: bool,
) -> bool {
let arg_types = arg_types.as_ref();
- let hash_fn = calc_native_fn_hash(empty(), name, arg_types.iter().cloned());
- let hash_script = calc_script_fn_hash(empty(), name, arg_types.len());
+ let hash_fn = calc_native_fn_hash(empty(), fn_name, arg_types.iter().cloned());
+ let hash_script = calc_script_fn_hash(empty(), fn_name, arg_types.len());
self.has_override(lib, hash_fn, hash_script, pub_only)
}
@@ -694,7 +694,7 @@ impl Engine {
mods: &mut Imports,
state: &mut State,
lib: &[&Module],
- name: &str,
+ fn_name: &str,
hash_script: u64,
target: &mut Target,
mut call_args: StaticVec,
@@ -707,9 +707,9 @@ impl Engine {
// Get a reference to the mutation target Dynamic
let obj = target.as_mut();
- let mut _fn_name = name;
+ let mut fn_name = fn_name;
- let (result, updated) = if _fn_name == KEYWORD_FN_PTR_CALL && obj.is::() {
+ let (result, updated) = if fn_name == KEYWORD_FN_PTR_CALL && obj.is::() {
// FnPtr call
let fn_ptr = obj.read_lock::().unwrap();
// Redirect function name
@@ -733,7 +733,7 @@ impl Engine {
self.exec_fn_call(
mods, state, lib, fn_name, hash, args, false, false, pub_only, None, def_val, level,
)
- } else if _fn_name == KEYWORD_FN_PTR_CALL
+ } else if fn_name == KEYWORD_FN_PTR_CALL
&& call_args.len() > 0
&& call_args[0].is::()
{
@@ -760,7 +760,7 @@ impl Engine {
self.exec_fn_call(
mods, state, lib, fn_name, hash, args, is_ref, true, pub_only, None, def_val, level,
)
- } else if _fn_name == KEYWORD_FN_PTR_CURRY && obj.is::() {
+ } else if fn_name == KEYWORD_FN_PTR_CURRY && obj.is::() {
// Curry call
let fn_ptr = obj.read_lock::().unwrap();
Ok((
@@ -779,7 +779,7 @@ impl Engine {
} else if {
#[cfg(not(feature = "no_closure"))]
{
- _fn_name == KEYWORD_IS_SHARED && call_args.is_empty()
+ fn_name == KEYWORD_IS_SHARED && call_args.is_empty()
}
#[cfg(feature = "no_closure")]
false
@@ -793,11 +793,11 @@ impl Engine {
// Check if it is a map method call in OOP style
#[cfg(not(feature = "no_object"))]
if let Some(map) = obj.read_lock::