Minor refactors.
This commit is contained in:
parent
82317f0dbf
commit
c727b529f5
@ -695,13 +695,13 @@ impl Engine {
|
||||
|
||||
if root.is_empty() || !root.contains_key(sub_module) {
|
||||
let mut m = Module::new();
|
||||
register_static_module_raw(m.get_sub_modules(), remainder, module);
|
||||
register_static_module_raw(m.get_sub_modules_mut(), remainder, module);
|
||||
m.build_index();
|
||||
root.insert(sub_module.into(), m.into());
|
||||
} else {
|
||||
let m = root.remove(sub_module).expect("contains sub-module");
|
||||
let mut m = crate::func::shared_take_or_clone(m);
|
||||
register_static_module_raw(m.get_sub_modules(), remainder, module);
|
||||
register_static_module_raw(m.get_sub_modules_mut(), remainder, module);
|
||||
m.build_index();
|
||||
root.insert(sub_module.into(), m.into());
|
||||
}
|
||||
|
@ -25,32 +25,42 @@ const BUILTIN: &str = "data type was checked";
|
||||
#[inline]
|
||||
#[must_use]
|
||||
fn is_numeric(type_id: TypeId) -> bool {
|
||||
let result = false;
|
||||
if type_id == TypeId::of::<INT>() {
|
||||
return true;
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "only_i64"))]
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
let result = result
|
||||
|| type_id == TypeId::of::<u8>()
|
||||
if type_id == TypeId::of::<u8>()
|
||||
|| type_id == TypeId::of::<u16>()
|
||||
|| type_id == TypeId::of::<u32>()
|
||||
|| type_id == TypeId::of::<u64>()
|
||||
|| type_id == TypeId::of::<i8>()
|
||||
|| type_id == TypeId::of::<i16>()
|
||||
|| type_id == TypeId::of::<i32>()
|
||||
|| type_id == TypeId::of::<i64>();
|
||||
|| type_id == TypeId::of::<i64>()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "only_i64"))]
|
||||
#[cfg(not(feature = "only_i32"))]
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
let result = result || type_id == TypeId::of::<u128>() || type_id == TypeId::of::<i128>();
|
||||
if type_id == TypeId::of::<u128>() || type_id == TypeId::of::<i128>() {
|
||||
return true;
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
let result = result || type_id == TypeId::of::<f32>() || type_id == TypeId::of::<f64>();
|
||||
if type_id == TypeId::of::<f32>() || type_id == TypeId::of::<f64>() {
|
||||
return true;
|
||||
}
|
||||
|
||||
#[cfg(feature = "decimal")]
|
||||
let result = result || type_id == TypeId::of::<rust_decimal::Decimal>();
|
||||
if type_id == TypeId::of::<rust_decimal::Decimal>() {
|
||||
return true;
|
||||
}
|
||||
|
||||
result
|
||||
false
|
||||
}
|
||||
|
||||
/// Build in common binary operator implementations to avoid the cost of calling a registered function.
|
||||
@ -61,8 +71,6 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<Fn
|
||||
let type1 = x.type_id();
|
||||
let type2 = y.type_id();
|
||||
|
||||
let types_pair = (type1, type2);
|
||||
|
||||
macro_rules! impl_op {
|
||||
($xx:ident $op:tt $yy:ident) => { |_, args| {
|
||||
let x = &*args[0].read_lock::<$xx>().expect(BUILTIN);
|
||||
@ -255,7 +263,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<Fn
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
macro_rules! impl_float {
|
||||
($x:ty, $xx:ident, $y:ty, $yy:ident) => {
|
||||
if types_pair == (TypeId::of::<$x>(), TypeId::of::<$y>()) {
|
||||
if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) {
|
||||
return match op {
|
||||
"+" => Some(impl_op!(FLOAT => $xx + $yy)),
|
||||
"-" => Some(impl_op!(FLOAT => $xx - $yy)),
|
||||
@ -285,7 +293,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<Fn
|
||||
#[cfg(feature = "decimal")]
|
||||
macro_rules! impl_decimal {
|
||||
($x:ty, $xx:ident, $y:ty, $yy:ident) => {
|
||||
if types_pair == (TypeId::of::<$x>(), TypeId::of::<$y>()) {
|
||||
if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) {
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
use crate::packages::arithmetic::decimal_functions::*;
|
||||
|
||||
@ -335,7 +343,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<Fn
|
||||
}
|
||||
|
||||
// char op string
|
||||
if types_pair == (TypeId::of::<char>(), TypeId::of::<ImmutableString>()) {
|
||||
if (type1, type2) == (TypeId::of::<char>(), TypeId::of::<ImmutableString>()) {
|
||||
fn get_s1s2(args: &FnCallArgs) -> ([char; 2], [char; 2]) {
|
||||
let x = args[0].as_char().expect(BUILTIN);
|
||||
let y = &*args[1].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
@ -361,7 +369,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<Fn
|
||||
};
|
||||
}
|
||||
// string op char
|
||||
if types_pair == (TypeId::of::<ImmutableString>(), TypeId::of::<char>()) {
|
||||
if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<char>()) {
|
||||
fn get_s1s2(args: &FnCallArgs) -> ([char; 2], [char; 2]) {
|
||||
let x = &*args[0].read_lock::<ImmutableString>().expect(BUILTIN);
|
||||
let y = args[1].as_char().expect(BUILTIN);
|
||||
@ -397,7 +405,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<Fn
|
||||
};
|
||||
}
|
||||
// () op string
|
||||
if types_pair == (TypeId::of::<()>(), TypeId::of::<ImmutableString>()) {
|
||||
if (type1, type2) == (TypeId::of::<()>(), TypeId::of::<ImmutableString>()) {
|
||||
return match op {
|
||||
"+" => Some(|_, args| Ok(args[1].clone())),
|
||||
"==" | ">" | ">=" | "<" | "<=" => Some(|_, _| Ok(Dynamic::FALSE)),
|
||||
@ -406,7 +414,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<Fn
|
||||
};
|
||||
}
|
||||
// string op ()
|
||||
if types_pair == (TypeId::of::<ImmutableString>(), TypeId::of::<()>()) {
|
||||
if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<()>()) {
|
||||
return match op {
|
||||
"+" => Some(|_, args| Ok(args[0].clone())),
|
||||
"==" | ">" | ">=" | "<" | "<=" => Some(|_, _| Ok(Dynamic::FALSE)),
|
||||
@ -446,7 +454,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<Fn
|
||||
|
||||
// map op string
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
if types_pair == (TypeId::of::<crate::Map>(), TypeId::of::<ImmutableString>()) {
|
||||
if (type1, type2) == (TypeId::of::<crate::Map>(), TypeId::of::<ImmutableString>()) {
|
||||
use crate::Map;
|
||||
|
||||
return match op {
|
||||
@ -456,12 +464,12 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<Fn
|
||||
}
|
||||
|
||||
// Non-compatible ranges
|
||||
if types_pair
|
||||
if (type1, type2)
|
||||
== (
|
||||
TypeId::of::<ExclusiveRange>(),
|
||||
TypeId::of::<InclusiveRange>(),
|
||||
)
|
||||
|| types_pair
|
||||
|| (type1, type2)
|
||||
== (
|
||||
TypeId::of::<InclusiveRange>(),
|
||||
TypeId::of::<ExclusiveRange>(),
|
||||
@ -554,8 +562,6 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio
|
||||
let type1 = x.type_id();
|
||||
let type2 = y.type_id();
|
||||
|
||||
let types_pair = (type1, type2);
|
||||
|
||||
macro_rules! impl_op {
|
||||
($x:ty = x $op:tt $yy:ident) => { |_, args| {
|
||||
let x = args[0].$yy().expect(BUILTIN);
|
||||
@ -691,7 +697,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
macro_rules! impl_float {
|
||||
($x:ident, $xx:ident, $y:ty, $yy:ident) => {
|
||||
if types_pair == (TypeId::of::<$x>(), TypeId::of::<$y>()) {
|
||||
if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) {
|
||||
return match op {
|
||||
"+=" => Some(impl_op!($x += $yy)),
|
||||
"-=" => Some(impl_op!($x -= $yy)),
|
||||
@ -714,7 +720,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio
|
||||
#[cfg(feature = "decimal")]
|
||||
macro_rules! impl_decimal {
|
||||
($x:ident, $xx:ident, $y:ty, $yy:ident) => {
|
||||
if types_pair == (TypeId::of::<$x>(), TypeId::of::<$y>()) {
|
||||
if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) {
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
use crate::packages::arithmetic::decimal_functions::*;
|
||||
|
||||
@ -753,7 +759,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio
|
||||
}
|
||||
|
||||
// string op= char
|
||||
if types_pair == (TypeId::of::<ImmutableString>(), TypeId::of::<char>()) {
|
||||
if (type1, type2) == (TypeId::of::<ImmutableString>(), TypeId::of::<char>()) {
|
||||
return match op {
|
||||
"+=" => Some(impl_op!(ImmutableString += as_char as char)),
|
||||
"-=" => Some(impl_op!(ImmutableString -= as_char as char)),
|
||||
@ -761,7 +767,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio
|
||||
};
|
||||
}
|
||||
// char op= string
|
||||
if types_pair == (TypeId::of::<char>(), TypeId::of::<ImmutableString>()) {
|
||||
if (type1, type2) == (TypeId::of::<char>(), TypeId::of::<ImmutableString>()) {
|
||||
return match op {
|
||||
"+=" => Some(|_, args| {
|
||||
let mut ch = args[0].as_char().expect(BUILTIN).to_string();
|
||||
@ -810,7 +816,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio
|
||||
use crate::Blob;
|
||||
|
||||
// blob op= int
|
||||
if types_pair == (TypeId::of::<Blob>(), TypeId::of::<INT>()) {
|
||||
if (type1, type2) == (TypeId::of::<Blob>(), TypeId::of::<INT>()) {
|
||||
return match op {
|
||||
"+=" => Some(|_, args| {
|
||||
let x = args[1].as_int().expect("`INT`");
|
||||
@ -822,7 +828,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio
|
||||
}
|
||||
|
||||
// blob op= char
|
||||
if types_pair == (TypeId::of::<Blob>(), TypeId::of::<char>()) {
|
||||
if (type1, type2) == (TypeId::of::<Blob>(), TypeId::of::<char>()) {
|
||||
return match op {
|
||||
"+=" => Some(|_, args| {
|
||||
let x = args[1].as_char().expect("`char`");
|
||||
@ -834,7 +840,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio
|
||||
}
|
||||
|
||||
// blob op= string
|
||||
if types_pair == (TypeId::of::<Blob>(), TypeId::of::<ImmutableString>()) {
|
||||
if (type1, type2) == (TypeId::of::<Blob>(), TypeId::of::<ImmutableString>()) {
|
||||
return match op {
|
||||
"+=" => Some(|_, args| {
|
||||
let s = std::mem::take(args[1]).cast::<ImmutableString>();
|
||||
|
@ -362,12 +362,12 @@ impl Engine {
|
||||
let parent_source = global.source.clone();
|
||||
|
||||
// Check if function access already in the cache
|
||||
let mut local_entry = None;
|
||||
let local_entry = &mut None;
|
||||
|
||||
let func = self.resolve_fn(
|
||||
global,
|
||||
caches,
|
||||
&mut local_entry,
|
||||
local_entry,
|
||||
lib,
|
||||
name,
|
||||
hash,
|
||||
@ -640,14 +640,14 @@ impl Engine {
|
||||
|
||||
// Script-defined function call?
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
let mut local_entry = None;
|
||||
let local_entry = &mut None;
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
if let Some(FnResolutionCacheEntry { func, ref source }) = self
|
||||
.resolve_fn(
|
||||
global,
|
||||
caches,
|
||||
&mut local_entry,
|
||||
local_entry,
|
||||
lib,
|
||||
fn_name,
|
||||
hashes.script,
|
||||
|
@ -64,7 +64,7 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
|
||||
///
|
||||
/// # Type Parameters
|
||||
///
|
||||
/// * `ARGS` - a tuple containing parameter types, with `&mut T` represented by [`Mut<T>`].
|
||||
/// * `ARGS` - a tuple containing parameter types, with `&mut T` represented by `Mut<T>`.
|
||||
/// * `RET` - return type of the function; if the function returns `Result`, it is the unwrapped inner value type.
|
||||
pub trait RegisterNativeFunction<ARGS, RET, RESULT> {
|
||||
/// Convert this function into a [`CallableFunction`].
|
||||
|
@ -16,7 +16,7 @@ use crate::{
|
||||
use std::prelude::v1::*;
|
||||
use std::{
|
||||
any::TypeId,
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
collections::BTreeMap,
|
||||
fmt,
|
||||
ops::{Add, AddAssign},
|
||||
};
|
||||
@ -213,7 +213,7 @@ impl fmt::Debug for Module {
|
||||
.iter()
|
||||
.flat_map(|m| m.keys())
|
||||
.map(SmartString::as_str)
|
||||
.collect::<BTreeSet<_>>(),
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.field("vars", &self.variables)
|
||||
.field(
|
||||
@ -221,7 +221,7 @@ impl fmt::Debug for Module {
|
||||
&self
|
||||
.iter_fn()
|
||||
.map(|f| f.func.to_string())
|
||||
.collect::<BTreeSet<_>>(),
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
#[cfg(feature = "metadata")]
|
||||
@ -710,8 +710,7 @@ impl Module {
|
||||
#[cfg(feature = "metadata")]
|
||||
comments: Box::default(),
|
||||
func: fn_def.into(),
|
||||
}
|
||||
.into(),
|
||||
},
|
||||
);
|
||||
self.indexed = false;
|
||||
self.contains_indexed_global_functions = false;
|
||||
@ -749,7 +748,7 @@ impl Module {
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub(crate) fn get_sub_modules(&mut self) -> &mut BTreeMap<Identifier, Shared<Module>> {
|
||||
pub(crate) fn get_sub_modules_mut(&mut self) -> &mut BTreeMap<Identifier, Shared<Module>> {
|
||||
// We must assume that the user has changed the sub-modules
|
||||
// (otherwise why take a mutable reference?)
|
||||
self.all_functions = None;
|
||||
@ -1044,8 +1043,7 @@ impl Module {
|
||||
return_type: return_type_name,
|
||||
#[cfg(feature = "metadata")]
|
||||
comments: Box::default(),
|
||||
}
|
||||
.into(),
|
||||
},
|
||||
);
|
||||
|
||||
self.indexed = false;
|
||||
|
@ -4,7 +4,7 @@
|
||||
use crate::eval::GlobalRuntimeState;
|
||||
use crate::func::{locked_read, locked_write};
|
||||
use crate::{
|
||||
Engine, Identifier, Module, ModuleResolver, Position, RhaiResultOf, Scope, Shared, ERR,
|
||||
Engine, Identifier, Locked, Module, ModuleResolver, Position, RhaiResultOf, Scope, Shared, ERR,
|
||||
};
|
||||
|
||||
use std::{
|
||||
@ -51,11 +51,7 @@ pub struct FileModuleResolver {
|
||||
extension: Identifier,
|
||||
cache_enabled: bool,
|
||||
scope: Scope<'static>,
|
||||
|
||||
#[cfg(not(feature = "sync"))]
|
||||
cache: std::cell::RefCell<BTreeMap<PathBuf, Shared<Module>>>,
|
||||
#[cfg(feature = "sync")]
|
||||
cache: std::sync::RwLock<BTreeMap<PathBuf, Shared<Module>>>,
|
||||
cache: Locked<BTreeMap<PathBuf, Shared<Module>>>,
|
||||
}
|
||||
|
||||
impl Default for FileModuleResolver {
|
||||
|
@ -1115,12 +1115,12 @@ impl Dynamic {
|
||||
/// Beware that you need to pass in an [`Array`][crate::Array] type for it to be recognized as
|
||||
/// an [`Array`][crate::Array]. A [`Vec<T>`][Vec] does not get automatically converted to an
|
||||
/// [`Array`][crate::Array], but will be a custom type instead (stored as a trait object). Use
|
||||
/// `Into<Dynamic>` to convert a [`Vec<T>`][Vec] into a [`Dynamic`] as an
|
||||
/// [`Dynamic::from_array`] to convert a [`Vec<T>`][Vec] into a [`Dynamic`] as an
|
||||
/// [`Array`][crate::Array] value.
|
||||
///
|
||||
/// Similarly, passing in a [`HashMap<String, T>`][std::collections::HashMap] or
|
||||
/// [`BTreeMap<String, T>`][std::collections::BTreeMap] will not get a [`Map`][crate::Map] but a
|
||||
/// custom type. Again, use `Into<Dynamic>` to get a [`Dynamic`] with a [`Map`][crate::Map]
|
||||
/// custom type. Again, use [`Dynamic::from_map`] to get a [`Dynamic`] with a [`Map`][crate::Map]
|
||||
/// value.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -23,7 +23,6 @@ pub const MAX_STRING_LEN: usize = 24;
|
||||
/// Exported under the `internals` feature only.
|
||||
///
|
||||
/// Normal identifiers, property getters and setters are interned separately.
|
||||
#[derive(Clone)]
|
||||
pub struct StringsInterner<'a> {
|
||||
/// Maximum number of strings interned.
|
||||
pub capacity: usize,
|
||||
|
Loading…
Reference in New Issue
Block a user