Implement string functions with to_string/to_debug.

This commit is contained in:
Stephen Chung 2021-04-02 19:26:55 +08:00
parent 1866331e7b
commit a738f750f9
10 changed files with 97 additions and 44 deletions

View File

@ -141,9 +141,12 @@ fn main() {
engine.set_optimization_level(rhai::OptimizationLevel::None);
// Set a file module resolver without caching
#[cfg(not(feature = "no_module"))]
{
let mut resolver = FileModuleResolver::new();
resolver.enable_cache(false);
engine.set_module_resolver(resolver);
}
// Make Engine immutable
let engine = engine;

View File

@ -712,12 +712,12 @@ pub struct Engine {
pub(crate) module_resolver: Box<dyn crate::ModuleResolver>,
/// A map mapping type names to pretty-print names.
pub(crate) type_names: BTreeMap<String, String>,
pub(crate) type_names: BTreeMap<Identifier, Identifier>,
/// A set of symbols to disable.
pub(crate) disabled_symbols: BTreeSet<String>,
pub(crate) disabled_symbols: BTreeSet<Identifier>,
/// A map containing custom keywords and precedence to recognize.
pub(crate) custom_keywords: BTreeMap<String, Option<Precedence>>,
pub(crate) custom_keywords: BTreeMap<Identifier, Option<Precedence>>,
/// Custom syntax.
pub(crate) custom_syntax: BTreeMap<Identifier, CustomSyntax>,
/// Callback closure for resolving variable access.
@ -2653,7 +2653,7 @@ impl Engine {
pub fn map_type_name<'a>(&'a self, name: &'a str) -> &'a str {
self.type_names
.get(name)
.map(String::as_str)
.map(|s| s.as_str())
.unwrap_or_else(|| map_std_type_name(name))
}

View File

@ -1,9 +1,9 @@
//! Configuration settings for [`Engine`].
use crate::engine::Precedence;
use crate::stdlib::{format, string::String};
use crate::token::Token;
use crate::Engine;
use crate::{engine::Precedence, Identifier};
#[cfg(not(feature = "unchecked"))]
use crate::stdlib::num::{NonZeroU64, NonZeroUsize};
@ -236,7 +236,7 @@ impl Engine {
/// # }
/// ```
#[inline(always)]
pub fn disable_symbol(&mut self, symbol: impl Into<String>) -> &mut Self {
pub fn disable_symbol(&mut self, symbol: impl Into<Identifier>) -> &mut Self {
self.disabled_symbols.insert(symbol.into());
self
}
@ -270,7 +270,7 @@ impl Engine {
/// ```
pub fn register_custom_operator(
&mut self,
keyword: impl AsRef<str> + Into<String>,
keyword: impl AsRef<str> + Into<Identifier>,
precedence: u8,
) -> Result<&mut Self, String> {
let precedence = Precedence::new(precedence);

View File

@ -3,9 +3,8 @@ use crate::stdlib::{
collections::BTreeMap,
io::Error as IoError,
path::{Path, PathBuf},
string::String,
};
use crate::{Engine, EvalAltResult, Module, ModuleResolver, Position, Shared};
use crate::{Engine, EvalAltResult, Identifier, Module, ModuleResolver, Position, Shared};
pub const RHAI_SCRIPT_EXTENSION: &str = "rhai";
@ -42,7 +41,7 @@ pub const RHAI_SCRIPT_EXTENSION: &str = "rhai";
#[derive(Debug)]
pub struct FileModuleResolver {
base_path: Option<PathBuf>,
extension: String,
extension: Identifier,
cache_enabled: bool,
#[cfg(not(feature = "sync"))]
@ -118,7 +117,7 @@ impl FileModuleResolver {
/// engine.set_module_resolver(resolver);
/// ```
#[inline(always)]
pub fn new_with_extension(extension: impl Into<String>) -> Self {
pub fn new_with_extension(extension: impl Into<Identifier>) -> Self {
Self {
base_path: None,
extension: extension.into(),
@ -145,7 +144,7 @@ impl FileModuleResolver {
#[inline(always)]
pub fn new_with_path_and_extension(
path: impl Into<PathBuf>,
extension: impl Into<String>,
extension: impl Into<Identifier>,
) -> Self {
Self {
base_path: Some(path.into()),
@ -175,7 +174,7 @@ impl FileModuleResolver {
/// Set the script file extension.
#[inline(always)]
pub fn set_extension(&mut self, extension: impl Into<String>) -> &mut Self {
pub fn set_extension(&mut self, extension: impl Into<Identifier>) -> &mut Self {
self.extension = extension.into();
self
}
@ -256,7 +255,7 @@ impl FileModuleResolver {
file_path = path.into();
}
file_path.set_extension(&self.extension); // Force extension
file_path.set_extension(self.extension.as_str()); // Force extension
file_path
}
}

View File

@ -1,5 +1,5 @@
use crate::stdlib::{boxed::Box, collections::BTreeMap, ops::AddAssign, string::String};
use crate::{Engine, EvalAltResult, Module, ModuleResolver, Position, Shared};
use crate::{Engine, EvalAltResult, Identifier, Module, ModuleResolver, Position, Shared};
/// A static [module][Module] resolution service that serves [modules][Module] added into it.
///
@ -19,7 +19,7 @@ use crate::{Engine, EvalAltResult, Module, ModuleResolver, Position, Shared};
/// engine.set_module_resolver(resolver);
/// ```
#[derive(Debug, Clone, Default)]
pub struct StaticModuleResolver(BTreeMap<String, Shared<Module>>);
pub struct StaticModuleResolver(BTreeMap<Identifier, Shared<Module>>);
impl StaticModuleResolver {
/// Create a new [`StaticModuleResolver`].
@ -44,7 +44,7 @@ impl StaticModuleResolver {
}
/// Add a [module][Module] keyed by its path.
#[inline(always)]
pub fn insert(&mut self, path: impl Into<String>, mut module: Module) {
pub fn insert(&mut self, path: impl Into<Identifier>, mut module: Module) {
module.build_index();
self.0.insert(path.into(), module.into());
}
@ -70,13 +70,13 @@ impl StaticModuleResolver {
}
/// Get a mutable iterator of all the modules.
#[inline(always)]
pub fn into_iter(self) -> impl Iterator<Item = (String, Shared<Module>)> {
pub fn into_iter(self) -> impl Iterator<Item = (Identifier, Shared<Module>)> {
self.0.into_iter()
}
/// Get an iterator of all the [module][Module] paths.
#[inline(always)]
pub fn paths(&self) -> impl Iterator<Item = &str> {
self.0.keys().map(String::as_str)
self.0.keys().map(|s| s.as_str())
}
/// Get an iterator of all the [modules][Module].
#[inline(always)]

View File

@ -10,8 +10,8 @@ use crate::Array;
#[cfg(not(feature = "no_object"))]
use crate::Map;
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
const FUNC_TO_DEBUG: &'static str = "to_debug";
pub const FUNC_TO_STRING: &'static str = "to_string";
pub const FUNC_TO_DEBUG: &'static str = "to_debug";
def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, {
combine_with_exported_module!(lib, "print_debug", print_debug_functions);
@ -19,9 +19,8 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin
// Register print and debug
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline(always)]
fn print_with_func(
pub fn print_with_func(
fn_name: &str,
ctx: &NativeCallContext,
value: &mut Dynamic,
@ -39,17 +38,25 @@ fn print_with_func(
mod print_debug_functions {
use crate::ImmutableString;
#[rhai_fn(name = "print", name = "to_string", pure)]
pub fn print_generic(item: &mut Dynamic) -> ImmutableString {
item.to_string().into()
#[rhai_fn(name = "print", pure)]
pub fn print_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString {
print_with_func(FUNC_TO_STRING, &ctx, item)
}
#[rhai_fn(name = "debug", name = "to_debug", pure)]
pub fn debug_generic(item: &mut Dynamic) -> ImmutableString {
format!("{:?}", item).into()
#[rhai_fn(name = "to_string", pure)]
pub fn to_string_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString {
ctx.engine().map_type_name(&item.to_string()).into()
}
#[rhai_fn(name = "debug", pure)]
pub fn debug_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString {
print_with_func(FUNC_TO_DEBUG, &ctx, item)
}
#[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()
}
#[rhai_fn(name = "print", name = "debug")]
pub fn print_empty_string() -> ImmutableString {
"".to_string().into()
Default::default()
}
#[rhai_fn(name = "print", name = "to_string")]
pub fn print_string(s: ImmutableString) -> ImmutableString {

View File

@ -6,6 +6,8 @@ use crate::stdlib::{
};
use crate::{def_package, Dynamic, ImmutableString, StaticVec, INT};
use super::string_basic::{print_with_func, FUNC_TO_STRING};
def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, {
combine_with_exported_module!(lib, "string", string_functions);
@ -21,22 +23,30 @@ mod string_functions {
use crate::ImmutableString;
#[rhai_fn(name = "+", name = "append")]
pub fn add_append(string: &str, item: Dynamic) -> ImmutableString {
format!("{}{}", string, item).into()
pub fn add_append(ctx: NativeCallContext, string: &str, mut item: Dynamic) -> ImmutableString {
let s = print_with_func(FUNC_TO_STRING, &ctx, &mut item);
format!("{}{}", string, s).into()
}
#[rhai_fn(name = "+", pure)]
pub fn add_prepend(item: &mut Dynamic, string: &str) -> ImmutableString {
format!("{}{}", item, string).into()
pub fn add_prepend(
ctx: NativeCallContext,
item: &mut Dynamic,
string: &str,
) -> ImmutableString {
let s = print_with_func(FUNC_TO_STRING, &ctx, item);
format!("{}{}", s, string).into()
}
#[rhai_fn(name = "+")]
pub fn add_append_unit(string: ImmutableString, _x: ()) -> ImmutableString {
pub fn add_append_unit(string: ImmutableString, _item: ()) -> ImmutableString {
string
}
#[rhai_fn(name = "+")]
pub fn add_prepend_unit(_x: (), string: ImmutableString) -> ImmutableString {
pub fn add_prepend_unit(_item: (), string: ImmutableString) -> ImmutableString {
string
}
#[rhai_fn(name = "+=")]
pub fn add_append_assign_unit(_string: &mut ImmutableString, _item: ()) {}
#[rhai_fn(name = "len", get = "len")]
pub fn len(string: &str) -> INT {

View File

@ -1621,7 +1621,7 @@ fn parse_binary_op(
Token::Custom(c) => state
.engine
.custom_keywords
.get(c)
.get(c.as_str())
.cloned()
.ok_or_else(|| PERR::Reserved(c.clone()).into_err(*current_pos))?,
Token::Reserved(c) if !is_valid_identifier(c.chars()) => {
@ -1646,7 +1646,7 @@ fn parse_binary_op(
Token::Custom(c) => state
.engine
.custom_keywords
.get(c)
.get(c.as_str())
.cloned()
.ok_or_else(|| PERR::Reserved(c.clone()).into_err(*next_pos))?,
Token::Reserved(c) if !is_valid_identifier(c.chars()) => {
@ -1753,7 +1753,7 @@ fn parse_binary_op(
if state
.engine
.custom_keywords
.get(&s)
.get(s.as_str())
.map_or(false, Option::is_some) =>
{
let hash = calc_fn_hash(empty(), &s, 2);

View File

@ -1828,7 +1828,7 @@ impl<'a> Iterator for TokenIterator<'a> {
None => return None,
// Reserved keyword/symbol
Some((Token::Reserved(s), pos)) => (match
(s.as_str(), self.engine.custom_keywords.contains_key(&s))
(s.as_str(), self.engine.custom_keywords.contains_key(s.as_str()))
{
("===", false) => Token::LexError(LERR::ImproperSymbol(s,
"'===' is not a valid operator. This is not JavaScript! Should it be '=='?".to_string(),
@ -1869,7 +1869,7 @@ impl<'a> Iterator for TokenIterator<'a> {
(_, false) => Token::Reserved(s),
}, pos),
// Custom keyword
Some((Token::Identifier(s), pos)) if self.engine.custom_keywords.contains_key(&s) => {
Some((Token::Identifier(s), pos)) if self.engine.custom_keywords.contains_key(s.as_str()) => {
(Token::Custom(s), pos)
}
// Custom standard keyword/symbol - must be disabled

View File

@ -222,6 +222,40 @@ fn test_string_substring() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_string_format() -> Result<(), Box<EvalAltResult>> {
#[derive(Debug, Clone)]
struct TestStruct {
field: i64,
}
let mut engine = Engine::new();
engine
.register_type_with_name::<TestStruct>("TestStruct")
.register_fn("new_ts", || TestStruct { field: 42 })
.register_fn("to_string", |ts: TestStruct| format!("TS={}", ts.field))
.register_fn("to_debug", |ts: TestStruct| {
format!("!!!TS={}!!!", ts.field)
});
assert_eq!(
engine.eval::<String>(r#"let x = new_ts(); "foo" + x"#)?,
"fooTS=42"
);
assert_eq!(
engine.eval::<String>(r#"let x = new_ts(); x + "foo""#)?,
"TS=42foo"
);
#[cfg(not(feature = "no_index"))]
assert_eq!(
engine.eval::<String>(r#"let x = [new_ts()]; "foo" + x"#)?,
"foo[!!!TS=42!!!]"
);
Ok(())
}
#[test]
fn test_string_fn() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();