Add support for String in function parameters.

This commit is contained in:
Stephen Chung 2020-07-13 13:40:51 +08:00
parent 8449f8c55e
commit e8d78bdfde
4 changed files with 16 additions and 11 deletions

View File

@ -855,7 +855,7 @@ impl Engine {
fn_name, fn_name,
args.iter() args.iter()
.map(|name| if name.is::<ImmutableString>() { .map(|name| if name.is::<ImmutableString>() {
"&str | ImmutableString" "&str | ImmutableString | String"
} else { } else {
self.map_type_name((*name).type_name()) self.map_type_name((*name).type_name())
}) })

View File

@ -6,6 +6,7 @@ use crate::engine::Engine;
use crate::fn_native::{CallableFunction, FnAny, FnCallArgs, SendSync}; use crate::fn_native::{CallableFunction, FnAny, FnCallArgs, SendSync};
use crate::module::Module; use crate::module::Module;
use crate::parser::FnAccess; use crate::parser::FnAccess;
use crate::r#unsafe::unsafe_cast_box;
use crate::result::EvalAltResult; use crate::result::EvalAltResult;
use crate::utils::ImmutableString; use crate::utils::ImmutableString;
@ -105,6 +106,9 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
let ref_str = data.as_str().unwrap(); let ref_str = data.as_str().unwrap();
let ref_T = unsafe { mem::transmute::<_, &T>(&ref_str) }; let ref_T = unsafe { mem::transmute::<_, &T>(&ref_str) };
ref_T.clone() ref_T.clone()
} else if TypeId::of::<T>() == TypeId::of::<String>() {
// If T is String, data must be ImmutableString, so map directly to it
*unsafe_cast_box(Box::new(data.as_str().unwrap().to_string())).unwrap()
} else { } else {
// We consume the argument and then replace it with () - the argument is not supposed to be used again. // We consume the argument and then replace it with () - the argument is not supposed to be used again.
// This way, we avoid having to clone the argument again, because it is already a clone when passed here. // This way, we avoid having to clone the argument again, because it is already a clone when passed here.
@ -154,13 +158,15 @@ pub fn map_result(
data data
} }
/// Remap `&str` to `ImmutableString`. /// Remap `&str` | `String` to `ImmutableString`.
#[inline(always)] #[inline(always)]
fn map_type_id<T: 'static>() -> TypeId { fn map_type_id<T: 'static>() -> TypeId {
let id = TypeId::of::<T>(); let id = TypeId::of::<T>();
if id == TypeId::of::<&str>() { if id == TypeId::of::<&str>() {
TypeId::of::<ImmutableString>() TypeId::of::<ImmutableString>()
} else if id == TypeId::of::<String>() {
TypeId::of::<ImmutableString>()
} else { } else {
id id
} }

View File

@ -8,7 +8,7 @@ fn test_decrement() -> Result<(), Box<EvalAltResult>> {
assert!(matches!( assert!(matches!(
*engine.eval::<String>(r#"let s = "test"; s -= "ing"; s"#).expect_err("expects error"), *engine.eval::<String>(r#"let s = "test"; s -= "ing"; s"#).expect_err("expects error"),
EvalAltResult::ErrorFunctionNotFound(err, _) if err == "- (&str | ImmutableString, &str | ImmutableString)" EvalAltResult::ErrorFunctionNotFound(err, _) if err == "- (&str | ImmutableString | String, &str | ImmutableString | String)"
)); ));
Ok(()) Ok(())

View File

@ -173,17 +173,16 @@ fn test_string_fn() -> Result<(), Box<EvalAltResult>> {
"foo" "foo"
); );
engine.register_fn("foo1", |s: &str| s.len() as INT); engine
engine.register_fn("foo2", |s: ImmutableString| s.len() as INT); .register_fn("foo1", |s: &str| s.len() as INT)
engine.register_fn("foo3", |s: String| s.len() as INT); .register_fn("foo2", |s: ImmutableString| s.len() as INT)
.register_fn("foo3", |s: String| s.len() as INT)
.register_fn("foo4", |s: &mut ImmutableString| s.len() as INT);
assert_eq!(engine.eval::<INT>(r#"foo1("hello")"#)?, 5); assert_eq!(engine.eval::<INT>(r#"foo1("hello")"#)?, 5);
assert_eq!(engine.eval::<INT>(r#"foo2("hello")"#)?, 5); assert_eq!(engine.eval::<INT>(r#"foo2("hello")"#)?, 5);
assert_eq!(engine.eval::<INT>(r#"foo3("hello")"#)?, 5);
assert!(matches!( assert_eq!(engine.eval::<INT>(r#"foo4("hello")"#)?, 5);
*engine.eval::<INT>(r#"foo3("hello")"#).expect_err("should error"),
EvalAltResult::ErrorFunctionNotFound(err, _) if err == "foo3 (&str | ImmutableString)"
));
Ok(()) Ok(())
} }