diff --git a/CHANGELOG.md b/CHANGELOG.md index 74aba840..e94439f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,8 @@ Enhancements * A function `sleep` is added to block the current thread by a specified number of seconds. * `Scope::set_alias` is added to export a variable under a particular alias name. * `starts_with` and `ends_with` are added for strings. +* Variables in modules registered via `register_global_module` can now be accessed in the global namespace. +* `Dynamic::into_read_only` is added to convert a `Dynamic` value into constant. Version 1.5.0 diff --git a/src/eval/expr.rs b/src/eval/expr.rs index f824ac7b..c2d35b32 100644 --- a/src/eval/expr.rs +++ b/src/eval/expr.rs @@ -165,11 +165,8 @@ impl Engine { this_ptr, level, }; - match resolve_var( - expr.get_variable_name(true).expect("`Expr::Variable`"), - index, - &context, - ) { + let var_name = expr.get_variable_name(true).expect("`Expr::Variable`"); + match resolve_var(var_name, index, &context) { Ok(Some(mut result)) => { result.set_access_mode(AccessMode::ReadOnly); return Ok((result.into(), var_pos)); @@ -184,10 +181,18 @@ impl Engine { } else { // Find the variable in the scope let var_name = expr.get_variable_name(true).expect("`Expr::Variable`"); - scope - .get_index(var_name) - .ok_or_else(|| ERR::ErrorVariableNotFound(var_name.to_string(), var_pos))? - .0 + + match scope.get_index(var_name) { + Some((index, _)) => index, + None => { + return match self.global_modules.iter().find_map(|m| m.get_var(var_name)) { + Some(val) => Ok((val.into(), var_pos)), + None => { + Err(ERR::ErrorVariableNotFound(var_name.to_string(), var_pos).into()) + } + } + } + } }; let val = scope.get_mut_by_index(index); diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index afeb3d7c..e8a8a07a 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -1051,6 +1051,13 @@ impl Dynamic { } self } + /// Make this [`Dynamic`] read-only (i.e. a constant). + #[inline(always)] + pub fn into_read_only(self) -> Self { + let mut value = self; + value.set_access_mode(AccessMode::ReadOnly); + value + } /// Is this [`Dynamic`] read-only? /// /// Constant [`Dynamic`] values are read-only. diff --git a/tests/plugins.rs b/tests/plugins.rs index e0a0d648..5327595c 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -85,6 +85,8 @@ fn test_plugins_package() -> Result<(), Box> { reg_functions!(engine += greet::single(INT, bool, char)); + assert_eq!(engine.eval::("MYSTIC_NUMBER")?, 42); + #[cfg(not(feature = "no_object"))] { assert_eq!(engine.eval::("let a = [1, 2, 3]; a.foo")?, 1);