Make sure the global namespace is always searched first.
This commit is contained in:
parent
46e99df9cd
commit
eeca8c90aa
@ -8,6 +8,7 @@ Bug fixes
|
||||
---------
|
||||
|
||||
* Reverses a regression on string `+` operations.
|
||||
* The global namespace is now searched before packages, which is the correct behavior.
|
||||
|
||||
|
||||
Version 1.1.2
|
||||
|
@ -2970,6 +2970,7 @@ impl Engine {
|
||||
// Create automatic global module
|
||||
let mut global = Module::new();
|
||||
global.internal = true;
|
||||
global.standard = false;
|
||||
mods.push(KEYWORD_GLOBAL, global);
|
||||
Some(mods.get_mut(mods.len() - 1).expect("global module exists"))
|
||||
};
|
||||
|
@ -27,16 +27,16 @@ impl Engine {
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn global_namespace(&self) -> &Module {
|
||||
self.global_modules
|
||||
.last()
|
||||
.first()
|
||||
.expect("global_modules contains at least one module")
|
||||
}
|
||||
/// Get a mutable reference to the global namespace module
|
||||
/// (which is the last module in `global_modules`).
|
||||
/// (which is the first module in `global_modules`).
|
||||
#[inline(always)]
|
||||
pub(crate) fn global_namespace_mut(&mut self) -> &mut Module {
|
||||
Shared::get_mut(
|
||||
self.global_modules
|
||||
.last_mut()
|
||||
.first_mut()
|
||||
.expect("global_modules contains at least one module"),
|
||||
)
|
||||
.expect("global namespace module is never shared")
|
||||
@ -895,8 +895,9 @@ impl Engine {
|
||||
/// modules are searched in reverse order.
|
||||
#[inline(always)]
|
||||
pub fn register_global_module(&mut self, module: Shared<Module>) -> &mut Self {
|
||||
// Insert the module into the front
|
||||
self.global_modules.insert(0, module);
|
||||
// Insert the module into the front.
|
||||
// The first module is always the global namespace.
|
||||
self.global_modules.insert(1, module);
|
||||
self
|
||||
}
|
||||
/// Register a shared [`Module`] as a static module namespace with the [`Engine`].
|
||||
|
@ -613,7 +613,7 @@ impl Engine {
|
||||
|
||||
// First check script-defined functions
|
||||
let result = lib.iter().any(|&m| m.contains_fn(hash_script))
|
||||
// Then check packages
|
||||
// Then check the global namespace and packages
|
||||
|| self.global_modules.iter().any(|m| m.contains_fn(hash_script))
|
||||
// Then check imported modules
|
||||
|| mods.map_or(false, |m| m.contains_fn(hash_script))
|
||||
|
@ -131,6 +131,8 @@ pub struct Module {
|
||||
id: Option<Identifier>,
|
||||
/// Is this module internal?
|
||||
pub(crate) internal: bool,
|
||||
/// Is this module part of a standard library?
|
||||
pub(crate) standard: bool,
|
||||
/// Sub-modules.
|
||||
modules: BTreeMap<Identifier, Shared<Module>>,
|
||||
/// [`Module`] variables.
|
||||
@ -246,6 +248,7 @@ impl Module {
|
||||
Self {
|
||||
id: None,
|
||||
internal: false,
|
||||
standard: false,
|
||||
modules: Default::default(),
|
||||
variables: Default::default(),
|
||||
all_variables: Default::default(),
|
||||
|
@ -145,12 +145,13 @@ impl<'a> OptimizerState<'a> {
|
||||
.map(|(v, _)| v)
|
||||
}
|
||||
// Has a system function a Rust-native override?
|
||||
pub fn has_native_fn(&self, hash_script: u64, arg_types: &[TypeId]) -> bool {
|
||||
pub fn has_native_fn_override(&self, hash_script: u64, arg_types: &[TypeId]) -> bool {
|
||||
let hash_params = calc_fn_params_hash(arg_types.iter().cloned());
|
||||
let hash = combine_hashes(hash_script, hash_params);
|
||||
|
||||
// First check packages
|
||||
self.engine.global_modules.iter().any(|m| m.contains_fn(hash))
|
||||
// First check the global namespace and packages, but skip modules that are standard because
|
||||
// they should never conflict with system functions.
|
||||
self.engine.global_modules.iter().filter(|m| !m.standard).any(|m| m.contains_fn(hash))
|
||||
// Then check sub-modules
|
||||
|| self.engine.global_sub_modules.values().any(|m| m.contains_qualified_fn(hash))
|
||||
}
|
||||
@ -997,7 +998,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
|
||||
return;
|
||||
}
|
||||
// Overloaded operators can override built-in.
|
||||
_ if x.args.len() == 2 && !state.has_native_fn(x.hashes.native, arg_types.as_ref()) => {
|
||||
_ if x.args.len() == 2 && !state.has_native_fn_override(x.hashes.native, arg_types.as_ref()) => {
|
||||
if let Some(result) = get_builtin_binary_op_fn(x.name.as_ref(), &arg_values[0], &arg_values[1])
|
||||
.and_then(|f| {
|
||||
let ctx = (state.engine, x.name.as_ref(), state.lib).into();
|
||||
|
@ -177,6 +177,8 @@ macro_rules! reg_functions {
|
||||
}
|
||||
|
||||
def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
combine_with_exported_module!(lib, "int", int_functions);
|
||||
reg_functions!(lib += signed_basic; INT);
|
||||
|
||||
|
@ -9,6 +9,8 @@ use std::prelude::v1::*;
|
||||
use std::{any::TypeId, cmp::Ordering, mem};
|
||||
|
||||
def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
combine_with_exported_module!(lib, "array", array_functions);
|
||||
|
||||
// Register array iterator
|
||||
|
@ -4,6 +4,8 @@ use crate::{def_package, FnPtr, ImmutableString, NativeCallContext};
|
||||
use std::prelude::v1::*;
|
||||
|
||||
def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
combine_with_exported_module!(lib, "FnPtr", fn_ptr_functions);
|
||||
});
|
||||
|
||||
|
@ -292,6 +292,8 @@ macro_rules! reg_range {
|
||||
}
|
||||
|
||||
def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
reg_range!(lib | "range" => INT);
|
||||
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
|
@ -43,5 +43,7 @@ mod core_functions {
|
||||
}
|
||||
|
||||
def_package!(crate:LanguageCorePackage:"Language core functions.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
combine_with_exported_module!(lib, "language_core", core_functions);
|
||||
});
|
||||
|
@ -38,6 +38,8 @@ macro_rules! reg_functions {
|
||||
}
|
||||
|
||||
def_package!(crate:LogicPackage:"Logical operators.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
#[cfg(not(feature = "only_i64"))]
|
||||
{
|
||||
|
@ -10,6 +10,8 @@ use std::prelude::v1::*;
|
||||
use crate::Array;
|
||||
|
||||
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
combine_with_exported_module!(lib, "map", map_functions);
|
||||
});
|
||||
|
||||
|
@ -62,6 +62,8 @@ macro_rules! reg_functions {
|
||||
}
|
||||
|
||||
def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
// Integer functions
|
||||
combine_with_exported_module!(lib, "int", int_functions);
|
||||
|
||||
|
@ -10,6 +10,8 @@ use std::prelude::v1::*;
|
||||
use crate::def_package;
|
||||
|
||||
def_package!(crate:CorePackage:"_Core_ package containing basic facilities.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
LanguageCorePackage::init(lib);
|
||||
ArithmeticPackage::init(lib);
|
||||
LogicPackage::init(lib);
|
||||
|
@ -13,6 +13,8 @@ use std::prelude::v1::*;
|
||||
use crate::def_package;
|
||||
|
||||
def_package!(crate:StandardPackage:"_Standard_ package containing all built-in features.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
CorePackage::init(lib);
|
||||
BasicMathPackage::init(lib);
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
|
@ -16,6 +16,8 @@ pub const FUNC_TO_STRING: &str = "to_string";
|
||||
pub const FUNC_TO_DEBUG: &str = "to_debug";
|
||||
|
||||
def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
combine_with_exported_module!(lib, "print_debug", print_debug_functions);
|
||||
combine_with_exported_module!(lib, "number_formatting", number_formatting);
|
||||
});
|
||||
|
@ -9,6 +9,8 @@ use std::{any::TypeId, mem};
|
||||
use super::string_basic::{print_with_func, FUNC_TO_STRING};
|
||||
|
||||
def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
combine_with_exported_module!(lib, "string", string_functions);
|
||||
});
|
||||
|
||||
|
@ -16,6 +16,8 @@ use std::time::{Duration, Instant};
|
||||
use instant::{Duration, Instant};
|
||||
|
||||
def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, {
|
||||
lib.standard = true;
|
||||
|
||||
// Register date/time functions
|
||||
combine_with_exported_module!(lib, "time", time_functions);
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
use rhai::{Dynamic, Engine, EvalAltResult, NativeCallContext, INT};
|
||||
use rhai::{Dynamic, Engine, EvalAltResult, ImmutableString, NativeCallContext, INT};
|
||||
use std::any::TypeId;
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
@ -49,3 +49,41 @@ fn test_native_context_fn_name() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_native_overload() -> Result<(), Box<EvalAltResult>> {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<String>(r#"let x = "hello, "; let y = "world"; x + y"#)?,
|
||||
"hello, world"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>(r#"let x = "hello"; let y = (); x + y"#)?,
|
||||
"hello"
|
||||
);
|
||||
|
||||
// Overload the `+` operator for strings
|
||||
|
||||
engine
|
||||
.register_fn(
|
||||
"+",
|
||||
|s1: ImmutableString, s2: ImmutableString| -> ImmutableString {
|
||||
format!("{}***{}", s1, s2).into()
|
||||
},
|
||||
)
|
||||
.register_fn("+", |s1: ImmutableString, _: ()| -> ImmutableString {
|
||||
format!("{} Foo!", s1).into()
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<String>(r#"let x = "hello"; let y = "world"; x + y"#)?,
|
||||
"hello***world"
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<String>(r#"let x = "hello"; let y = (); x + y"#)?,
|
||||
"hello Foo!"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user