Refactor.

This commit is contained in:
Stephen Chung
2020-05-10 00:19:13 +08:00
parent a7bfac21bd
commit 80debbc432
6 changed files with 299 additions and 301 deletions

View File

@@ -250,7 +250,7 @@ impl Module {
/// use rhai::Module;
///
/// let mut module = Module::new();
/// let hash = module.set_fn_0("calc", || Ok(42_i64), false);
/// let hash = module.set_fn_0("calc", || Ok(42_i64));
/// assert!(module.contains_fn(hash));
/// ```
pub fn contains_fn(&self, hash: u64) -> bool {
@@ -289,7 +289,7 @@ impl Module {
/// use rhai::Module;
///
/// let mut module = Module::new();
/// let hash = module.set_fn_0("calc", || Ok(42_i64), false);
/// let hash = module.set_fn_0("calc", || Ok(42_i64));
/// assert!(module.get_fn(hash).is_some());
/// ```
pub fn set_fn_0<K: Into<String>, T: Into<Dynamic>>(
@@ -297,7 +297,6 @@ impl Module {
fn_name: K,
#[cfg(not(feature = "sync"))] func: impl Fn() -> FuncReturn<T> + 'static,
#[cfg(feature = "sync")] func: impl Fn() -> FuncReturn<T> + Send + Sync + 'static,
is_private: bool,
) -> u64 {
let f = move |_: &mut FnCallArgs, pos| {
func()
@@ -305,12 +304,7 @@ impl Module {
.map_err(|err| EvalAltResult::set_position(err, pos))
};
let arg_types = vec![];
let access = if is_private {
FnAccess::Private
} else {
FnAccess::Public
};
self.set_fn(fn_name.into(), access, arg_types, Box::new(f))
self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f))
}
/// Set a Rust function taking one parameter into the module, returning a hash key.
@@ -323,7 +317,7 @@ impl Module {
/// use rhai::Module;
///
/// let mut module = Module::new();
/// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1), false);
/// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1));
/// assert!(module.get_fn(hash).is_some());
/// ```
pub fn set_fn_1<K: Into<String>, A: Variant + Clone, T: Into<Dynamic>>(
@@ -331,7 +325,6 @@ impl Module {
fn_name: K,
#[cfg(not(feature = "sync"))] func: impl Fn(A) -> FuncReturn<T> + 'static,
#[cfg(feature = "sync")] func: impl Fn(A) -> FuncReturn<T> + Send + Sync + 'static,
is_private: bool,
) -> u64 {
let f = move |args: &mut FnCallArgs, pos| {
func(mem::take(args[0]).cast::<A>())
@@ -339,12 +332,7 @@ impl Module {
.map_err(|err| EvalAltResult::set_position(err, pos))
};
let arg_types = vec![TypeId::of::<A>()];
let access = if is_private {
FnAccess::Private
} else {
FnAccess::Public
};
self.set_fn(fn_name.into(), access, arg_types, Box::new(f))
self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f))
}
/// Set a Rust function taking one mutable parameter into the module, returning a hash key.
@@ -357,7 +345,7 @@ impl Module {
/// use rhai::Module;
///
/// let mut module = Module::new();
/// let hash = module.set_fn_1_mut("calc", |x: &mut i64| { *x += 1; Ok(*x) }, false);
/// let hash = module.set_fn_1_mut("calc", |x: &mut i64| { *x += 1; Ok(*x) });
/// assert!(module.get_fn(hash).is_some());
/// ```
pub fn set_fn_1_mut<K: Into<String>, A: Variant + Clone, T: Into<Dynamic>>(
@@ -365,7 +353,6 @@ impl Module {
fn_name: K,
#[cfg(not(feature = "sync"))] func: impl Fn(&mut A) -> FuncReturn<T> + 'static,
#[cfg(feature = "sync")] func: impl Fn(&mut A) -> FuncReturn<T> + Send + Sync + 'static,
is_private: bool,
) -> u64 {
let f = move |args: &mut FnCallArgs, pos| {
func(args[0].downcast_mut::<A>().unwrap())
@@ -373,12 +360,7 @@ impl Module {
.map_err(|err| EvalAltResult::set_position(err, pos))
};
let arg_types = vec![TypeId::of::<A>()];
let access = if is_private {
FnAccess::Private
} else {
FnAccess::Public
};
self.set_fn(fn_name.into(), access, arg_types, Box::new(f))
self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f))
}
/// Set a Rust function taking two parameters into the module, returning a hash key.
@@ -393,7 +375,7 @@ impl Module {
/// let mut module = Module::new();
/// let hash = module.set_fn_2("calc", |x: i64, y: String| {
/// Ok(x + y.len() as i64)
/// }, false);
/// });
/// assert!(module.get_fn(hash).is_some());
/// ```
pub fn set_fn_2<K: Into<String>, A: Variant + Clone, B: Variant + Clone, T: Into<Dynamic>>(
@@ -401,7 +383,6 @@ impl Module {
fn_name: K,
#[cfg(not(feature = "sync"))] func: impl Fn(A, B) -> FuncReturn<T> + 'static,
#[cfg(feature = "sync")] func: impl Fn(A, B) -> FuncReturn<T> + Send + Sync + 'static,
is_private: bool,
) -> u64 {
let f = move |args: &mut FnCallArgs, pos| {
let a = mem::take(args[0]).cast::<A>();
@@ -412,12 +393,7 @@ impl Module {
.map_err(|err| EvalAltResult::set_position(err, pos))
};
let arg_types = vec![TypeId::of::<A>(), TypeId::of::<B>()];
let access = if is_private {
FnAccess::Private
} else {
FnAccess::Public
};
self.set_fn(fn_name.into(), access, arg_types, Box::new(f))
self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f))
}
/// Set a Rust function taking two parameters (the first one mutable) into the module,
@@ -431,7 +407,7 @@ impl Module {
/// let mut module = Module::new();
/// let hash = module.set_fn_2_mut("calc", |x: &mut i64, y: String| {
/// *x += y.len() as i64; Ok(*x)
/// }, false);
/// });
/// assert!(module.get_fn(hash).is_some());
/// ```
pub fn set_fn_2_mut<
@@ -444,7 +420,6 @@ impl Module {
fn_name: K,
#[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn<T> + 'static,
#[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn<T> + Send + Sync + 'static,
is_private: bool,
) -> u64 {
let f = move |args: &mut FnCallArgs, pos| {
let b = mem::take(args[1]).cast::<B>();
@@ -455,12 +430,7 @@ impl Module {
.map_err(|err| EvalAltResult::set_position(err, pos))
};
let arg_types = vec![TypeId::of::<A>(), TypeId::of::<B>()];
let access = if is_private {
FnAccess::Private
} else {
FnAccess::Public
};
self.set_fn(fn_name.into(), access, arg_types, Box::new(f))
self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f))
}
/// Set a Rust function taking three parameters into the module, returning a hash key.
@@ -475,7 +445,7 @@ impl Module {
/// let mut module = Module::new();
/// let hash = module.set_fn_3("calc", |x: i64, y: String, z: i64| {
/// Ok(x + y.len() as i64 + z)
/// }, false);
/// });
/// assert!(module.get_fn(hash).is_some());
/// ```
pub fn set_fn_3<
@@ -489,7 +459,6 @@ impl Module {
fn_name: K,
#[cfg(not(feature = "sync"))] func: impl Fn(A, B, C) -> FuncReturn<T> + 'static,
#[cfg(feature = "sync")] func: impl Fn(A, B, C) -> FuncReturn<T> + Send + Sync + 'static,
is_private: bool,
) -> u64 {
let f = move |args: &mut FnCallArgs, pos| {
let a = mem::take(args[0]).cast::<A>();
@@ -501,12 +470,7 @@ impl Module {
.map_err(|err| EvalAltResult::set_position(err, pos))
};
let arg_types = vec![TypeId::of::<A>(), TypeId::of::<B>(), TypeId::of::<C>()];
let access = if is_private {
FnAccess::Private
} else {
FnAccess::Public
};
self.set_fn(fn_name.into(), access, arg_types, Box::new(f))
self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f))
}
/// Set a Rust function taking three parameters (the first one mutable) into the module,
@@ -522,7 +486,7 @@ impl Module {
/// let mut module = Module::new();
/// let hash = module.set_fn_3_mut("calc", |x: &mut i64, y: String, z: i64| {
/// *x += y.len() as i64 + z; Ok(*x)
/// }, false);
/// });
/// assert!(module.get_fn(hash).is_some());
/// ```
pub fn set_fn_3_mut<
@@ -536,7 +500,6 @@ impl Module {
fn_name: K,
#[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, C) -> FuncReturn<T> + 'static,
#[cfg(feature = "sync")] func: impl Fn(&mut A, B, C) -> FuncReturn<T> + Send + Sync + 'static,
is_private: bool,
) -> u64 {
let f = move |args: &mut FnCallArgs, pos| {
let b = mem::take(args[1]).cast::<B>();
@@ -548,12 +511,7 @@ impl Module {
.map_err(|err| EvalAltResult::set_position(err, pos))
};
let arg_types = vec![TypeId::of::<A>(), TypeId::of::<B>(), TypeId::of::<C>()];
let access = if is_private {
FnAccess::Private
} else {
FnAccess::Public
};
self.set_fn(fn_name.into(), access, arg_types, Box::new(f))
self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f))
}
/// Get a Rust function.
@@ -567,7 +525,7 @@ impl Module {
/// use rhai::Module;
///
/// let mut module = Module::new();
/// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1), false);
/// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1));
/// assert!(module.get_fn(hash).is_some());
/// ```
pub fn get_fn(&self, hash: u64) -> Option<&Box<FnAny>> {
@@ -694,16 +652,16 @@ impl Module {
// Rust functions are indexed in two steps:
// 1) Calculate a hash in a similar manner to script-defined functions,
// i.e. qualifiers + function name + dummy parameter types (one for each parameter).
let hash1 = calc_fn_hash(
let hash_fn_def = calc_fn_hash(
qualifiers.iter().map(|v| *v),
fn_name,
repeat(EMPTY_TYPE_ID()).take(params.len()),
);
// 2) Calculate a second hash with no qualifiers, empty function name, and
// the actual list of parameter `TypeId`'.s
let hash2 = calc_fn_hash(empty(), "", params.iter().cloned());
let hash_fn_args = calc_fn_hash(empty(), "", params.iter().cloned());
// 3) The final hash is the XOR of the two hashes.
let hash = hash1 ^ hash2;
let hash = hash_fn_def ^ hash_fn_args;
functions.push((hash, func.clone()));
}