Print arrays and maps with to_debug.

This commit is contained in:
Stephen Chung
2020-11-30 11:20:51 +08:00
parent 65a4ceb3be
commit 1004bca5b5
9 changed files with 113 additions and 19 deletions

View File

@@ -134,7 +134,7 @@ impl<'e, 'a, 'm, 'pm> NativeCallContext<'e, 'a, 'm, 'pm> {
/// If `is_method` is [`true`], the first argument is assumed to be passed
/// by reference and is not consumed.
pub fn call_fn_dynamic_raw(
&mut self,
&self,
fn_name: &str,
is_method: bool,
public_only: bool,
@@ -262,7 +262,7 @@ impl FnPtr {
/// clone them _before_ calling this function.
pub fn call_dynamic(
&self,
mut ctx: NativeCallContext,
ctx: NativeCallContext,
this_ptr: Option<&mut Dynamic>,
mut arg_values: impl AsMut<[Dynamic]>,
) -> Result<Dynamic, Box<EvalAltResult>> {

View File

@@ -650,7 +650,7 @@ mod array_functions {
}
#[rhai_fn(name = "==", return_raw)]
pub fn equals(
mut ctx: NativeCallContext,
ctx: NativeCallContext,
arr1: &mut Array,
mut arr2: Array,
) -> Result<Dynamic, Box<EvalAltResult>> {

View File

@@ -45,7 +45,7 @@ mod map_functions {
}
#[rhai_fn(name = "==", return_raw)]
pub fn equals(
mut ctx: NativeCallContext,
ctx: NativeCallContext,
map1: &mut Map,
mut map2: Map,
) -> Result<Dynamic, Box<EvalAltResult>> {

View File

@@ -15,6 +15,9 @@ use crate::Array;
#[cfg(not(feature = "no_object"))]
use crate::Map;
const FUNC_TO_STRING: &'static str = "to_string";
const FUNC_TO_DEBUG: &'static str = "to_debug";
type Unit = ();
macro_rules! gen_functions {
@@ -32,13 +35,14 @@ macro_rules! gen_functions {
macro_rules! reg_print_functions {
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
set_exported_fn!($mod_name, "to_string", $root::$arg_type::to_string_func);
set_exported_fn!($mod_name, FUNC_TO_STRING, $root::$arg_type::to_string_func);
set_exported_fn!($mod_name, KEYWORD_PRINT, $root::$arg_type::to_string_func);
)* }
}
macro_rules! reg_debug_functions {
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
set_exported_fn!($mod_name, FUNC_TO_DEBUG, $root::$arg_type::to_string_func);
set_exported_fn!($mod_name, KEYWORD_DEBUG, $root::$arg_type::to_string_func);
)* }
}
@@ -67,12 +71,6 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin
reg_print_functions!(lib += print_float; f32, f64);
reg_debug_functions!(lib += debug_float; f32, f64);
}
#[cfg(not(feature = "no_index"))]
{
reg_print_functions!(lib += print_array; Array);
reg_debug_functions!(lib += print_array; Array);
}
});
fn to_string<T: Display>(x: &mut T) -> ImmutableString {
@@ -109,10 +107,18 @@ gen_functions!(print_float => to_string(f32, f64));
#[cfg(not(feature = "no_float"))]
gen_functions!(debug_float => to_debug(f32, f64));
#[cfg(not(feature = "no_index"))]
gen_functions!(print_array => to_debug(Array));
// Register print and debug
#[cfg(not(feature = "no_index"))]
#[inline(always)]
fn print_with_func(fn_name: &str, ctx: &NativeCallContext, value: &mut Dynamic) -> ImmutableString {
match ctx.call_fn_dynamic_raw(fn_name, true, false, &mut [value], None) {
Ok(result) if result.is::<ImmutableString>() => result.take_immutable_string().unwrap(),
Ok(result) => ctx.engine().map_type_name(result.type_name()).into(),
Err(_) => ctx.engine().map_type_name(value.type_name()).into(),
}
}
#[export_module]
mod print_debug_functions {
#[rhai_fn(name = "print", name = "debug")]
@@ -132,13 +138,50 @@ mod print_debug_functions {
to_string(f)
}
#[cfg(not(feature = "no_index"))]
pub mod array_functions {
use super::*;
#[rhai_fn(name = "print", name = "to_string", name = "to_debug", name = "debug")]
pub fn format_array(ctx: NativeCallContext, arr: &mut Array) -> ImmutableString {
let mut result = String::with_capacity(16);
result.push_str("[");
let len = arr.len();
arr.iter_mut().enumerate().for_each(|(i, x)| {
result.push_str(&print_with_func(FUNC_TO_DEBUG, &ctx, x));
if i < len - 1 {
result.push_str(", ");
}
});
result.push_str("]");
result.into()
}
}
#[cfg(not(feature = "no_object"))]
pub mod map_functions {
use super::*;
#[rhai_fn(name = "print", name = "debug", name = "to_string")]
pub fn format_map(x: &mut Map) -> ImmutableString {
format!("#{:?}", x).into()
#[rhai_fn(name = "print", name = "to_string", name = "to_debug", name = "debug")]
pub fn format_map(ctx: NativeCallContext, map: &mut Map) -> ImmutableString {
let mut result = String::with_capacity(16);
result.push_str("#{");
let len = map.len();
map.iter_mut().enumerate().for_each(|(i, (k, v))| {
result.push_str(k);
result.push_str(": ");
result.push_str(&print_with_func(FUNC_TO_DEBUG, &ctx, v));
if i < len - 1 {
result.push_str(", ");
}
});
result.push_str("}");
result.into()
}
}
}