Add Dynamic::is_XXX API.

This commit is contained in:
Stephen Chung
2022-11-09 12:44:57 +08:00
parent ad018aaae3
commit ce046422f0
22 changed files with 327 additions and 91 deletions

View File

@@ -39,7 +39,7 @@ pub fn print_with_func(
value: &mut Dynamic,
) -> crate::ImmutableString {
match ctx.call_native_fn_raw(fn_name, true, &mut [value]) {
Ok(result) if result.is::<crate::ImmutableString>() => {
Ok(result) if result.is_string() => {
result.into_immutable_string().expect("`ImmutableString`")
}
Ok(result) => ctx.engine().map_type_name(result.type_name()).into(),
@@ -75,7 +75,7 @@ mod print_debug_functions {
/// Return the empty string.
#[rhai_fn(name = "print", name = "debug")]
pub fn print_empty_string(ctx: NativeCallContext) -> ImmutableString {
ctx.engine().get_interned_string("")
ctx.engine().const_empty_string()
}
/// Return the `string`.
@@ -121,7 +121,7 @@ mod print_debug_functions {
#[rhai_fn(name = "print", name = "to_string")]
pub fn print_unit(ctx: NativeCallContext, unit: ()) -> ImmutableString {
let _ = unit;
ctx.engine().get_interned_string("")
ctx.engine().const_empty_string()
}
/// Convert the unit into a string in debug format.
#[rhai_fn(name = "debug", name = "to_debug")]

View File

@@ -348,7 +348,7 @@ mod string_functions {
len: INT,
) -> ImmutableString {
if string.is_empty() || len <= 0 {
return ctx.engine().get_interned_string("");
return ctx.engine().const_empty_string();
}
let len = len.min(MAX_USIZE_INT) as usize;
@@ -896,18 +896,18 @@ mod string_functions {
len: INT,
) -> ImmutableString {
if string.is_empty() {
return ctx.engine().get_interned_string("");
return ctx.engine().const_empty_string();
}
let mut chars = StaticVec::with_capacity(string.len());
let offset = if string.is_empty() || len <= 0 {
return ctx.engine().get_interned_string("");
return ctx.engine().const_empty_string();
} else if start < 0 {
let abs_start = start.unsigned_abs();
if abs_start as u64 > MAX_USIZE_INT as u64 {
return ctx.engine().get_interned_string("");
return ctx.engine().const_empty_string();
}
let abs_start = abs_start as usize;
@@ -918,7 +918,7 @@ mod string_functions {
chars.len() - abs_start
}
} else if start > MAX_USIZE_INT || start as usize >= string.chars().count() {
return ctx.engine().get_interned_string("");
return ctx.engine().const_empty_string();
} else {
start as usize
};
@@ -964,7 +964,7 @@ mod string_functions {
start: INT,
) -> ImmutableString {
if string.is_empty() {
ctx.engine().get_interned_string("")
ctx.engine().const_empty_string()
} else {
let len = string.len() as INT;
sub_string(ctx, string, start, len)
@@ -1348,7 +1348,7 @@ mod string_functions {
if abs_index as u64 > MAX_USIZE_INT as u64 {
return vec![
ctx.engine().get_interned_string("").into(),
ctx.engine().const_empty_string().into(),
string.as_str().into(),
];
}
@@ -1357,7 +1357,7 @@ mod string_functions {
let num_chars = string.chars().count();
if abs_index > num_chars {
vec![
ctx.engine().get_interned_string("").into(),
ctx.engine().const_empty_string().into(),
string.as_str().into(),
]
} else {
@@ -1368,7 +1368,7 @@ mod string_functions {
} else if index > MAX_USIZE_INT {
vec![
string.as_str().into(),
ctx.engine().get_interned_string("").into(),
ctx.engine().const_empty_string().into(),
]
} else {
let prefix: String = string.chars().take(index as usize).collect();