Trap &mut String parameters.

This commit is contained in:
Stephen Chung
2021-03-06 14:41:35 +08:00
parent fe633ea7d3
commit e14bef4b10
3 changed files with 27 additions and 7 deletions

View File

@@ -161,15 +161,15 @@ pub fn map_result(data: RhaiResult) -> RhaiResult {
/// Remap `&str` | `String` to `ImmutableString`.
#[inline(always)]
fn map_type_id<T: 'static>() -> TypeId {
let id = TypeId::of::<T>();
fn map_type_id<R: 'static, T: 'static>() -> TypeId {
let ref_id = TypeId::of::<R>();
if id == TypeId::of::<&str>() {
if ref_id == TypeId::of::<&str>() {
TypeId::of::<ImmutableString>()
} else if id == TypeId::of::<String>() {
} else if ref_id == TypeId::of::<String>() {
TypeId::of::<ImmutableString>()
} else {
id
TypeId::of::<T>()
}
}
@@ -193,7 +193,7 @@ macro_rules! def_register {
#[inline(always)]
fn register_fn(&mut self, name: &str, f: FN) -> &mut Self {
self.global_namespace.set_fn(name, FnNamespace::Global, FnAccess::Public, None,
&[$(map_type_id::<$par>()),*],
&[$(map_type_id::<$param, $par>()),*],
CallableFunction::$abi(make_func!(f : map_dynamic ; $($par => $let => $clone => $arg),*))
);
self
@@ -208,7 +208,7 @@ macro_rules! def_register {
#[inline(always)]
fn register_result_fn(&mut self, name: &str, f: FN) -> &mut Self {
self.global_namespace.set_fn(name, FnNamespace::Global, FnAccess::Public, None,
&[$(map_type_id::<$par>()),*],
&[$(map_type_id::<$param, $par>()),*],
CallableFunction::$abi(make_func!(f : map_result ; $($par => $let => $clone => $arg),*))
);
self