Use SmartString for buffers and literal_syntax can panic.

This commit is contained in:
Stephen Chung
2022-11-22 23:30:43 +08:00
parent 05c7d00a8e
commit d911327242
16 changed files with 225 additions and 174 deletions

View File

@@ -247,8 +247,8 @@ pub mod array_functions {
let mut arr_len = array.len();
let mut arr = Dynamic::from_array(mem::take(array));
let (mut a1, mut m1, mut s1) = crate::Engine::calc_data_sizes(&arr, true);
let (a2, m2, s2) = crate::Engine::calc_data_sizes(&item, true);
let (mut a1, mut m1, mut s1) = arr.calc_data_sizes(true);
let (a2, m2, s2) = item.calc_data_sizes(true);
{
let mut guard = arr.write_lock::<Array>().unwrap();

View File

@@ -269,9 +269,13 @@ fn collect_fn_metadata(
.iter_script_fn()
.filter(|(s, a, n, p, f)| filter(*s, *a, n, *p, f))
.for_each(|(.., f)| list.push(make_metadata(dict, namespace.into(), f).into()));
for (ns, m) in module.iter_sub_modules() {
let ns = format!(
"{namespace}{}{ns}",
for (name, m) in module.iter_sub_modules() {
use std::fmt::Write;
let mut ns = crate::SmartString::new_const();
write!(
&mut ns,
"{namespace}{}{name}",
crate::tokenizer::Token::DoubleColon.literal_syntax()
);
scan_module(dict, list, &ns, &**m, filter);

View File

@@ -1,7 +1,7 @@
use crate::plugin::*;
use crate::{def_package, FnPtr, INT};
use crate::{def_package, FnPtr, SmartString, INT};
use std::any::TypeId;
use std::fmt::{Binary, LowerHex, Octal};
use std::fmt::{Binary, LowerHex, Octal, Write};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -69,7 +69,9 @@ mod print_debug_functions {
/// Convert the value of the `item` into a string in debug format.
#[rhai_fn(name = "to_debug", pure)]
pub fn to_debug_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString {
ctx.engine().map_type_name(&format!("{item:?}")).into()
let mut buf = SmartString::new_const();
write!(&mut buf, "{item:?}").unwrap();
ctx.engine().map_type_name(&buf).into()
}
/// Return the empty string.
@@ -86,7 +88,9 @@ mod print_debug_functions {
/// Convert the string into debug format.
#[rhai_fn(name = "debug", name = "to_debug", pure)]
pub fn debug_string(string: &mut ImmutableString) -> ImmutableString {
format!("{string:?}").into()
let mut buf = SmartString::new_const();
write!(&mut buf, "{string:?}").unwrap();
buf.into()
}
/// Return the character into a string.
@@ -97,7 +101,9 @@ mod print_debug_functions {
/// Convert the string into debug format.
#[rhai_fn(name = "debug", name = "to_debug")]
pub fn debug_char(character: char) -> ImmutableString {
format!("{character:?}").into()
let mut buf = SmartString::new_const();
buf.push(character);
buf.into()
}
/// Convert the function pointer into a string in debug format.
@@ -114,7 +120,9 @@ mod print_debug_functions {
/// Convert the boolean value into a string in debug format.
#[rhai_fn(name = "debug", name = "to_debug")]
pub fn debug_bool(value: bool) -> ImmutableString {
format!("{value:?}").into()
let mut buf = SmartString::new_const();
write!(&mut buf, "{value:?}").unwrap();
buf.into()
}
/// Return the empty string.
@@ -147,14 +155,18 @@ mod print_debug_functions {
#[rhai_fn(name = "debug", name = "to_debug")]
pub fn debug_f64(number: f64) -> ImmutableString {
let number = crate::types::FloatWrapper::new(number);
format!("{number:?}").into()
let mut buf = SmartString::new_const();
write!(&mut buf, "{number:?}").unwrap();
buf.into()
}
/// Convert the value of `number` into a string.
#[cfg(not(feature = "no_float"))]
#[rhai_fn(name = "debug", name = "to_debug")]
pub fn debug_f32(number: f32) -> ImmutableString {
let number = crate::types::FloatWrapper::new(number);
format!("{number:?}").into()
let mut buf = SmartString::new_const();
write!(&mut buf, "{number:?}").unwrap();
buf.into()
}
/// Convert the array into a string.
@@ -217,13 +229,19 @@ mod print_debug_functions {
#[export_module]
mod number_formatting {
fn to_hex<T: LowerHex>(value: T) -> ImmutableString {
format!("{value:x}").into()
let mut buf = SmartString::new_const();
write!(&mut buf, "{value:x}").unwrap();
buf.into()
}
fn to_octal<T: Octal>(value: T) -> ImmutableString {
format!("{value:o}").into()
let mut buf = SmartString::new_const();
write!(&mut buf, "{value:o}").unwrap();
buf.into()
}
fn to_binary<T: Binary>(value: T) -> ImmutableString {
format!("{value:b}").into()
let mut buf = SmartString::new_const();
write!(&mut buf, "{value:b}").unwrap();
buf.into()
}
/// Convert the `value` into a string in hex format.

View File

@@ -33,7 +33,9 @@ mod string_functions {
if s.is_empty() {
string.clone()
} else {
format!("{string}{s}").into()
let mut buf = SmartString::from(string.as_str());
buf.push_str(&s);
buf.into()
}
}
#[rhai_fn(name = "+=", name = "append")]
@@ -41,7 +43,9 @@ mod string_functions {
let s = print_with_func(FUNC_TO_STRING, &ctx, &mut item);
if !s.is_empty() {
*string = format!("{string}{s}").into();
let mut buf = SmartString::from(string.as_str());
buf.push_str(&s);
*string = buf.into();
}
}
#[rhai_fn(name = "+", pure)]
@@ -74,7 +78,10 @@ mod string_functions {
}
#[rhai_fn(name = "+")]
pub fn add_prepend_char(character: char, string: &str) -> ImmutableString {
format!("{character}{string}").into()
let mut buf = SmartString::new_const();
buf.push(character);
buf.push_str(string);
buf.into()
}
#[rhai_fn(name = "+")]