2020-08-15 06:57:47 +02:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
2020-11-22 08:41:55 +01:00
|
|
|
use crate::engine::{KEYWORD_DEBUG, KEYWORD_PRINT};
|
2020-08-15 06:57:47 +02:00
|
|
|
use crate::plugin::*;
|
2020-11-16 16:10:14 +01:00
|
|
|
use crate::stdlib::{
|
|
|
|
fmt::{Debug, Display},
|
|
|
|
format,
|
|
|
|
string::ToString,
|
|
|
|
};
|
|
|
|
use crate::{def_package, FnPtr, ImmutableString, INT};
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-11-16 09:28:04 +01:00
|
|
|
use crate::Array;
|
2020-05-06 13:45:17 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-11-16 09:28:04 +01:00
|
|
|
use crate::Map;
|
2020-05-06 13:45:17 +02:00
|
|
|
|
2020-11-30 04:20:51 +01:00
|
|
|
const FUNC_TO_STRING: &'static str = "to_string";
|
|
|
|
const FUNC_TO_DEBUG: &'static str = "to_debug";
|
|
|
|
|
2020-08-15 06:57:47 +02:00
|
|
|
type Unit = ();
|
|
|
|
|
|
|
|
macro_rules! gen_functions {
|
|
|
|
($root:ident => $fn_name:ident ( $($arg_type:ident),+ )) => {
|
2020-08-28 17:13:38 +02:00
|
|
|
pub mod $root { $(pub mod $arg_type {
|
|
|
|
use super::super::*;
|
|
|
|
|
|
|
|
#[export_fn]
|
|
|
|
pub fn to_string_func(x: &mut $arg_type) -> ImmutableString {
|
|
|
|
super::super::$fn_name(x)
|
2020-08-15 06:57:47 +02:00
|
|
|
}
|
2020-08-28 17:13:38 +02:00
|
|
|
})* }
|
2020-08-15 06:57:47 +02:00
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-08-15 06:57:47 +02:00
|
|
|
|
|
|
|
macro_rules! reg_print_functions {
|
2020-08-24 16:37:44 +02:00
|
|
|
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
|
2020-11-30 04:20:51 +01:00
|
|
|
set_exported_fn!($mod_name, FUNC_TO_STRING, $root::$arg_type::to_string_func);
|
2020-08-24 16:37:44 +02:00
|
|
|
set_exported_fn!($mod_name, KEYWORD_PRINT, $root::$arg_type::to_string_func);
|
|
|
|
)* }
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-08-15 06:57:47 +02:00
|
|
|
macro_rules! reg_debug_functions {
|
2020-08-24 16:37:44 +02:00
|
|
|
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
|
2020-11-30 04:20:51 +01:00
|
|
|
set_exported_fn!($mod_name, FUNC_TO_DEBUG, $root::$arg_type::to_string_func);
|
2020-08-24 16:37:44 +02:00
|
|
|
set_exported_fn!($mod_name, KEYWORD_DEBUG, $root::$arg_type::to_string_func);
|
|
|
|
)* }
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 08:55:40 +02:00
|
|
|
def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, {
|
2020-10-19 17:49:01 +02:00
|
|
|
combine_with_exported_module!(lib, "print_debug", print_debug_functions);
|
2020-08-15 06:57:47 +02:00
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
reg_print_functions!(lib += print_basic; INT, bool, char, FnPtr);
|
2020-08-15 06:57:47 +02:00
|
|
|
reg_debug_functions!(lib += debug_basic; INT, bool, Unit, char, ImmutableString);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
{
|
|
|
|
reg_print_functions!(lib += print_numbers; i8, u8, i16, u16, i32, u32, i64, u64);
|
|
|
|
reg_debug_functions!(lib += debug_numbers; i8, u8, i16, u16, i32, u32, i64, u64);
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
{
|
|
|
|
reg_print_functions!(lib += print_num_128; i128, u128);
|
|
|
|
reg_debug_functions!(lib += debug_num_128; i128, u128);
|
2020-06-17 10:50:46 +02:00
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
{
|
2020-08-15 06:57:47 +02:00
|
|
|
reg_print_functions!(lib += print_float; f32, f64);
|
|
|
|
reg_debug_functions!(lib += debug_float; f32, f64);
|
2020-04-21 17:01:10 +02:00
|
|
|
}
|
|
|
|
});
|
2020-08-15 06:57:47 +02:00
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
fn to_string<T: Display>(x: &mut T) -> ImmutableString {
|
|
|
|
x.to_string().into()
|
|
|
|
}
|
|
|
|
fn to_debug<T: Debug>(x: &mut T) -> ImmutableString {
|
|
|
|
format!("{:?}", x).into()
|
|
|
|
}
|
|
|
|
|
2020-08-15 06:57:47 +02:00
|
|
|
gen_functions!(print_basic => to_string(INT, bool, char, FnPtr));
|
|
|
|
gen_functions!(debug_basic => to_debug(INT, bool, Unit, char, ImmutableString));
|
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
gen_functions!(print_numbers => to_string(i8, u8, i16, u16, i32, u32, i64, u64));
|
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
gen_functions!(debug_numbers => to_debug(i8, u8, i16, u16, i32, u32, i64, u64));
|
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
gen_functions!(print_num_128 => to_string(i128, u128));
|
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
gen_functions!(debug_num_128 => to_debug(i128, u128));
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
gen_functions!(print_float => to_string(f32, f64));
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
gen_functions!(debug_float => to_debug(f32, f64));
|
|
|
|
|
2020-11-30 04:20:51 +01:00
|
|
|
// Register print and debug
|
|
|
|
|
2020-11-30 14:16:59 +01:00
|
|
|
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
2020-11-30 04:20:51 +01:00
|
|
|
#[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(),
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 06:57:47 +02:00
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
#[export_module]
|
|
|
|
mod print_debug_functions {
|
|
|
|
#[rhai_fn(name = "print", name = "debug")]
|
|
|
|
pub fn print_empty_string() -> ImmutableString {
|
|
|
|
"".to_string().into()
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "print", name = "to_string")]
|
|
|
|
pub fn print_unit(_x: ()) -> ImmutableString {
|
|
|
|
"".to_string().into()
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "print", name = "to_string")]
|
|
|
|
pub fn print_string(s: ImmutableString) -> ImmutableString {
|
|
|
|
s
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "debug")]
|
|
|
|
pub fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString {
|
|
|
|
to_string(f)
|
|
|
|
}
|
2020-09-23 06:00:03 +02:00
|
|
|
|
2020-11-30 04:20:51 +01:00
|
|
|
#[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 {
|
2020-11-30 14:16:59 +01:00
|
|
|
let mut result = crate::stdlib::string::String::with_capacity(16);
|
2020-11-30 04:20:51 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 17:49:01 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
pub mod map_functions {
|
|
|
|
use super::*;
|
2020-10-08 16:25:50 +02:00
|
|
|
|
2020-11-30 04:20:51 +01:00
|
|
|
#[rhai_fn(name = "print", name = "to_string", name = "to_debug", name = "debug")]
|
|
|
|
pub fn format_map(ctx: NativeCallContext, map: &mut Map) -> ImmutableString {
|
2020-11-30 14:16:59 +01:00
|
|
|
let mut result = crate::stdlib::string::String::with_capacity(16);
|
2020-11-30 04:20:51 +01:00
|
|
|
result.push_str("#{");
|
|
|
|
|
|
|
|
let len = map.len();
|
|
|
|
|
|
|
|
map.iter_mut().enumerate().for_each(|(i, (k, v))| {
|
2020-12-07 15:21:02 +01:00
|
|
|
result.push_str(&format!(
|
|
|
|
"{:?}: {}{}",
|
|
|
|
k,
|
|
|
|
&print_with_func(FUNC_TO_DEBUG, &ctx, v),
|
|
|
|
if i < len - 1 { ", " } else { "" }
|
|
|
|
));
|
2020-11-30 04:20:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
result.push_str("}");
|
|
|
|
result.into()
|
2020-10-19 17:49:01 +02:00
|
|
|
}
|
2020-08-22 05:29:04 +02:00
|
|
|
}
|
2020-08-15 06:57:47 +02:00
|
|
|
}
|