rhai/src/packages/string_basic.rs

163 lines
4.8 KiB
Rust
Raw Normal View History

2020-08-15 06:57:47 +02:00
#![allow(non_snake_case)]
2020-04-21 17:01:10 +02:00
use crate::def_package;
2020-06-25 05:07:46 +02:00
use crate::engine::{FN_TO_STRING, KEYWORD_DEBUG, KEYWORD_PRINT};
2020-06-25 12:07:57 +02:00
use crate::fn_native::FnPtr;
2020-05-25 07:44:28 +02:00
use crate::parser::{ImmutableString, INT};
2020-08-15 06:57:47 +02:00
use crate::plugin::*;
#[cfg(not(feature = "no_index"))]
use crate::engine::Array;
#[cfg(not(feature = "no_object"))]
use crate::engine::Map;
use crate::stdlib::{
fmt::{Debug, Display},
format,
2020-05-26 08:14:03 +02:00
string::ToString,
};
2020-08-15 06:57:47 +02:00
type Unit = ();
macro_rules! gen_functions {
($root:ident => $fn_name:ident ( $($arg_type:ident),+ )) => {
pub mod $root { $(pub mod $arg_type {
use super::super::*;
#[export_fn]
#[inline(always)]
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-15 06:57:47 +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),+) => { $(
set_exported_fn!($mod_name, FN_TO_STRING, $root::$arg_type::to_string_func);
set_exported_fn!($mod_name, KEYWORD_PRINT, $root::$arg_type::to_string_func);
)* }
}
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),+) => { $(
set_exported_fn!($mod_name, KEYWORD_DEBUG, $root::$arg_type::to_string_func);
)* }
}
2020-04-22 08:55:40 +02:00
def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, {
2020-08-15 06:57:47 +02:00
reg_print_functions!(lib += print_basic; INT, bool, char, FnPtr);
set_exported_fn!(lib, KEYWORD_PRINT, print_empty_string);
set_exported_fn!(lib, KEYWORD_PRINT, print_unit);
set_exported_fn!(lib, FN_TO_STRING, print_unit);
set_exported_fn!(lib, KEYWORD_PRINT, print_string);
set_exported_fn!(lib, FN_TO_STRING, print_string);
reg_debug_functions!(lib += debug_basic; INT, bool, Unit, char, ImmutableString);
set_exported_fn!(lib, KEYWORD_DEBUG, print_empty_string);
set_exported_fn!(lib, KEYWORD_DEBUG, debug_fn_ptr);
#[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-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-04-21 17:01:10 +02:00
#[cfg(not(feature = "no_index"))]
{
2020-08-15 06:57:47 +02:00
reg_print_functions!(lib += print_array; Array);
reg_debug_functions!(lib += print_array; Array);
2020-04-21 17:01:10 +02:00
}
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "no_object"))]
{
set_exported_fn!(lib, KEYWORD_PRINT, format_map::format_map);
set_exported_fn!(lib, FN_TO_STRING, format_map::format_map);
set_exported_fn!(lib, KEYWORD_DEBUG, format_map::format_map);
}
2020-04-21 17:01:10 +02:00
});
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));
#[cfg(not(feature = "no_index"))]
gen_functions!(print_array => to_debug(Array));
// Register print and debug
#[export_fn]
2020-08-20 16:11:41 +02:00
#[inline(always)]
2020-08-15 06:57:47 +02:00
fn print_empty_string() -> ImmutableString {
String::new().into()
2020-08-15 06:57:47 +02:00
}
#[export_fn]
2020-08-20 16:11:41 +02:00
#[inline(always)]
2020-08-15 06:57:47 +02:00
fn print_unit(_x: ()) -> ImmutableString {
String::new().into()
2020-08-15 06:57:47 +02:00
}
#[export_fn]
2020-08-20 16:11:41 +02:00
#[inline(always)]
2020-08-15 06:57:47 +02:00
fn print_string(s: ImmutableString) -> ImmutableString {
s
}
#[export_fn]
2020-08-20 16:11:41 +02:00
#[inline(always)]
2020-08-15 06:57:47 +02:00
fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString {
to_string(f)
2020-08-15 06:57:47 +02:00
}
2020-08-20 16:11:41 +02:00
#[inline(always)]
2020-08-15 06:57:47 +02:00
fn to_string<T: Display>(x: &mut T) -> ImmutableString {
x.to_string().into()
}
2020-08-20 16:11:41 +02:00
#[inline]
2020-08-15 06:57:47 +02:00
fn to_debug<T: Debug>(x: &mut T) -> ImmutableString {
format!("{:?}", x).into()
}
#[cfg(not(feature = "no_object"))]
mod format_map {
use super::*;
#[inline]
#[export_fn]
pub fn format_map(x: &mut Map) -> ImmutableString {
format!("#{:?}", x).into()
}
2020-08-15 06:57:47 +02:00
}