New syntax for def_package.

This commit is contained in:
Stephen Chung 2021-12-20 11:42:39 +08:00
parent 5729f0cdd4
commit bca9fe53b0
16 changed files with 452 additions and 370 deletions

View File

@ -16,11 +16,13 @@ Enhancements
------------ ------------
* Added `NativeCallContext::call_fn` to easily call a function. * Added `NativeCallContext::call_fn` to easily call a function.
* A new syntax is introduced for `def_package!` that will replace the old syntax in future versions.
Deprecated API's Deprecated API's
---------------- ----------------
* `Expression::get_variable_name` is deprecated in favor of the new `Expression::get_string_value`. * `Expression::get_variable_name` is deprecated in favor of the new `Expression::get_string_value`.
* The old syntax of `def_package!` is deprecated in favor of the new syntax.
Version 1.3.1 Version 1.3.1

View File

@ -179,36 +179,39 @@ macro_rules! reg_functions {
)* } )* }
} }
def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { def_package! {
lib.standard = true; /// Basic arithmetic package.
crate::ArithmeticPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "int", int_functions); combine_with_exported_module!(lib, "int", int_functions);
reg_functions!(lib += signed_basic; INT); reg_functions!(lib += signed_basic; INT);
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
{
reg_functions!(lib += arith_numbers; i8, u8, i16, u16, i32, u32, u64);
reg_functions!(lib += signed_numbers; i8, i16, i32);
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
{ {
reg_functions!(lib += arith_num_128; i128, u128); reg_functions!(lib += arith_numbers; i8, u8, i16, u16, i32, u32, u64);
reg_functions!(lib += signed_num_128; i128); reg_functions!(lib += signed_numbers; i8, i16, i32);
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
{
reg_functions!(lib += arith_num_128; i128, u128);
reg_functions!(lib += signed_num_128; i128);
}
} }
}
// Basic arithmetic for floating-point // Basic arithmetic for floating-point
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
{ {
combine_with_exported_module!(lib, "f32", f32_functions); combine_with_exported_module!(lib, "f32", f32_functions);
combine_with_exported_module!(lib, "f64", f64_functions); combine_with_exported_module!(lib, "f64", f64_functions);
} }
// Decimal functions // Decimal functions
#[cfg(feature = "decimal")] #[cfg(feature = "decimal")]
combine_with_exported_module!(lib, "decimal", decimal_functions); combine_with_exported_module!(lib, "decimal", decimal_functions);
}); }
}
#[export_module] #[export_module]
mod int_functions { mod int_functions {

View File

@ -11,14 +11,17 @@ use crate::{
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{any::TypeId, cmp::Ordering, mem}; use std::{any::TypeId, cmp::Ordering, mem};
def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { def_package! {
lib.standard = true; /// Package of basic array utilities.
crate::BasicArrayPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "array", array_functions); combine_with_exported_module!(lib, "array", array_functions);
// Register array iterator // Register array iterator
lib.set_iterable::<Array>(); lib.set_iterable::<Array>();
}); }
}
#[export_module] #[export_module]
mod array_functions { mod array_functions {

View File

@ -13,14 +13,17 @@ use std::{any::TypeId, mem};
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
use crate::FLOAT; use crate::FLOAT;
def_package!(crate:BasicBlobPackage:"Basic BLOB utilities.", lib, { def_package! {
lib.standard = true; /// Package of basic BLOB utilities.
crate::BasicBlobPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "blob", blob_functions); combine_with_exported_module!(lib, "blob", blob_functions);
// Register blob iterator // Register blob iterator
lib.set_iterable::<Blob>(); lib.set_iterable::<Blob>();
}); }
}
#[export_module] #[export_module]
mod blob_functions { mod blob_functions {

View File

@ -3,11 +3,14 @@ use crate::{def_package, FnPtr, ImmutableString, NativeCallContext};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, { def_package! {
lib.standard = true; /// Package of basic function poitner utilities.
crate::BasicFnPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "FnPtr", fn_ptr_functions); combine_with_exported_module!(lib, "FnPtr", fn_ptr_functions);
}); }
}
#[export_module] #[export_module]
mod fn_ptr_functions { mod fn_ptr_functions {

View File

@ -299,233 +299,236 @@ macro_rules! reg_range {
}; };
} }
def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { def_package! {
lib.standard = true; /// Package of basic range iterators
crate::BasicIteratorPackage => |lib| {
lib.standard = true;
reg_range!(lib | "range" => INT); reg_range!(lib | "range" => INT);
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
{ {
reg_range!(lib | "range" => i8, u8, i16, u16, i32, u32, i64, u64); reg_range!(lib | "range" => i8, u8, i16, u16, i32, u32, i64, u64);
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
reg_range!(lib | "range" => i128, u128); reg_range!(lib | "range" => i128, u128);
}
reg_range!(lib | step "range" => INT);
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
reg_range!(lib | step "range" => i8, u8, i16, u16, i32, u32, i64, u64);
#[cfg(not(target_arch = "wasm32"))]
reg_range!(lib | step "range" => i128, u128);
}
#[cfg(not(feature = "no_float"))]
{
use crate::FLOAT;
#[derive(Debug, Clone, Copy, PartialEq)]
struct StepFloatRange(FLOAT, FLOAT, FLOAT);
impl StepFloatRange {
pub fn new(from: FLOAT, to: FLOAT, step: FLOAT) -> Result<Self, Box<EvalAltResult>> {
#[cfg(not(feature = "unchecked"))]
if step == 0.0 {
return Err(EvalAltResult::ErrorInFunctionCall("range".to_string(), "".to_string(),
EvalAltResult::ErrorArithmetic("step value cannot be zero".to_string(), crate::Position::NONE).into(),
crate::Position::NONE,
).into());
}
Ok(Self(from, to, step))
}
} }
impl Iterator for StepFloatRange { reg_range!(lib | step "range" => INT);
type Item = FLOAT;
fn next(&mut self) -> Option<FLOAT> { #[cfg(not(feature = "only_i32"))]
if self.0 == self.1 { #[cfg(not(feature = "only_i64"))]
None {
} else if self.0 < self.1 { reg_range!(lib | step "range" => i8, u8, i16, u16, i32, u32, i64, u64);
#[cfg(not(feature = "unchecked"))]
if self.2 < 0.0 {
return None;
}
let v = self.0; #[cfg(not(target_arch = "wasm32"))]
let n = self.0 + self.2; reg_range!(lib | step "range" => i128, u128);
self.0 = if n >= self.1 { self.1 } else { n };
Some(v)
} else {
#[cfg(not(feature = "unchecked"))]
if self.2 > 0.0 {
return None;
}
let v = self.0;
let n = self.0 + self.2;
self.0 = if n <= self.1 { self.1 } else { n };
Some(v)
}
}
} }
impl FusedIterator for StepFloatRange {} #[cfg(not(feature = "no_float"))]
{
use crate::FLOAT;
lib.set_iterator::<StepFloatRange>(); #[derive(Debug, Clone, Copy, PartialEq)]
struct StepFloatRange(FLOAT, FLOAT, FLOAT);
let _hash = lib.set_native_fn("range", StepFloatRange::new); impl StepFloatRange {
#[cfg(feature = "metadata")] pub fn new(from: FLOAT, to: FLOAT, step: FLOAT) -> Result<Self, Box<EvalAltResult>> {
lib.update_fn_metadata(_hash, &["from: FLOAT", "to: FLOAT", "step: FLOAT", "Iterator<Item=FLOAT>"]);
}
#[cfg(feature = "decimal")]
{
use rust_decimal::Decimal;
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
struct StepDecimalRange(Decimal, Decimal, Decimal);
impl StepDecimalRange {
pub fn new(from: Decimal, to: Decimal, step: Decimal) -> Result<Self, Box<EvalAltResult>> {
#[cfg(not(feature = "unchecked"))]
if step.is_zero() {
return Err(EvalAltResult::ErrorInFunctionCall("range".to_string(), "".to_string(),
EvalAltResult::ErrorArithmetic("step value cannot be zero".to_string(), crate::Position::NONE).into(),
crate::Position::NONE,
).into());
}
Ok(Self(from, to, step))
}
}
impl Iterator for StepDecimalRange {
type Item = Decimal;
fn next(&mut self) -> Option<Decimal> {
if self.0 == self.1 {
None
} else if self.0 < self.1 {
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
if self.2.is_sign_negative() { if step == 0.0 {
return None; return Err(EvalAltResult::ErrorInFunctionCall("range".to_string(), "".to_string(),
EvalAltResult::ErrorArithmetic("step value cannot be zero".to_string(), crate::Position::NONE).into(),
crate::Position::NONE,
).into());
} }
let v = self.0; Ok(Self(from, to, step))
let n = self.0 + self.2;
self.0 = if n >= self.1 { self.1 } else { n };
Some(v)
} else {
#[cfg(not(feature = "unchecked"))]
if self.2.is_sign_positive() {
return None;
}
let v = self.0;
let n = self.0 + self.2;
self.0 = if n <= self.1 { self.1 } else { n };
Some(v)
} }
} }
impl Iterator for StepFloatRange {
type Item = FLOAT;
fn next(&mut self) -> Option<FLOAT> {
if self.0 == self.1 {
None
} else if self.0 < self.1 {
#[cfg(not(feature = "unchecked"))]
if self.2 < 0.0 {
return None;
}
let v = self.0;
let n = self.0 + self.2;
self.0 = if n >= self.1 { self.1 } else { n };
Some(v)
} else {
#[cfg(not(feature = "unchecked"))]
if self.2 > 0.0 {
return None;
}
let v = self.0;
let n = self.0 + self.2;
self.0 = if n <= self.1 { self.1 } else { n };
Some(v)
}
}
}
impl FusedIterator for StepFloatRange {}
lib.set_iterator::<StepFloatRange>();
let _hash = lib.set_native_fn("range", StepFloatRange::new);
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["from: FLOAT", "to: FLOAT", "step: FLOAT", "Iterator<Item=FLOAT>"]);
} }
impl FusedIterator for StepDecimalRange {} #[cfg(feature = "decimal")]
{
use rust_decimal::Decimal;
lib.set_iterator::<StepDecimalRange>(); #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
struct StepDecimalRange(Decimal, Decimal, Decimal);
let _hash = lib.set_native_fn("range", StepDecimalRange::new); impl StepDecimalRange {
pub fn new(from: Decimal, to: Decimal, step: Decimal) -> Result<Self, Box<EvalAltResult>> {
#[cfg(not(feature = "unchecked"))]
if step.is_zero() {
return Err(EvalAltResult::ErrorInFunctionCall("range".to_string(), "".to_string(),
EvalAltResult::ErrorArithmetic("step value cannot be zero".to_string(), crate::Position::NONE).into(),
crate::Position::NONE,
).into());
}
Ok(Self(from, to, step))
}
}
impl Iterator for StepDecimalRange {
type Item = Decimal;
fn next(&mut self) -> Option<Decimal> {
if self.0 == self.1 {
None
} else if self.0 < self.1 {
#[cfg(not(feature = "unchecked"))]
if self.2.is_sign_negative() {
return None;
}
let v = self.0;
let n = self.0 + self.2;
self.0 = if n >= self.1 { self.1 } else { n };
Some(v)
} else {
#[cfg(not(feature = "unchecked"))]
if self.2.is_sign_positive() {
return None;
}
let v = self.0;
let n = self.0 + self.2;
self.0 = if n <= self.1 { self.1 } else { n };
Some(v)
}
}
}
impl FusedIterator for StepDecimalRange {}
lib.set_iterator::<StepDecimalRange>();
let _hash = lib.set_native_fn("range", StepDecimalRange::new);
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["from: Decimal", "to: Decimal", "step: Decimal", "Iterator<Item=Decimal>"]);
}
// Register string iterator
lib.set_iterator::<CharsStream>();
let _hash = lib.set_native_fn("chars", |string, range: ExclusiveRange| {
let from = INT::max(range.start, 0);
let to = INT::max(range.end, from);
Ok(CharsStream::new(string, from, to - from))
});
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["from: Decimal", "to: Decimal", "step: Decimal", "Iterator<Item=Decimal>"]); lib.update_fn_metadata(_hash, &["string: &str", "range: Range", "Iterator<Item=char>"]);
}
// Register string iterator let _hash = lib.set_native_fn("chars", |string, range: InclusiveRange| {
lib.set_iterator::<CharsStream>(); let from = INT::max(*range.start(), 0);
let to = INT::max(*range.end(), from - 1);
let _hash = lib.set_native_fn("chars", |string, range: ExclusiveRange| { Ok(CharsStream::new(string, from, to-from + 1))
let from = INT::max(range.start, 0); });
let to = INT::max(range.end, from);
Ok(CharsStream::new(string, from, to - from))
});
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["string: &str", "range: Range", "Iterator<Item=char>"]);
let _hash = lib.set_native_fn("chars", |string, range: InclusiveRange| {
let from = INT::max(*range.start(), 0);
let to = INT::max(*range.end(), from - 1);
Ok(CharsStream::new(string, from, to-from + 1))
});
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["string: &str", "range: RangeInclusive", "Iterator<Item=char>"]);
let _hash = lib.set_native_fn("chars", |string, from, len| Ok(CharsStream::new(string, from, len)));
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["string: &str", "from: INT", "len: INT", "Iterator<Item=char>"]);
let _hash = lib.set_native_fn("chars", |string, from| Ok(CharsStream::new(string, from, INT::MAX)));
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["string: &str", "from: INT", "Iterator<Item=char>"]);
let _hash = lib.set_native_fn("chars", |string| Ok(CharsStream::new(string, 0, INT::MAX)));
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["string: &str", "Iterator<Item=char>"]);
#[cfg(not(feature = "no_object"))]
{
let _hash = lib.set_getter_fn("chars", |string: &mut ImmutableString| Ok(CharsStream::new(string, 0, INT::MAX)));
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["string: &mut ImmutableString", "Iterator<Item=char>"]); lib.update_fn_metadata(_hash, &["string: &str", "range: RangeInclusive", "Iterator<Item=char>"]);
}
// Register bit-field iterator let _hash = lib.set_native_fn("chars", |string, from, len| Ok(CharsStream::new(string, from, len)));
lib.set_iterator::<BitRange>();
let _hash = lib.set_native_fn("bits", |value, range: ExclusiveRange| {
let from = INT::max(range.start, 0);
let to = INT::max(range.end, from);
BitRange::new(value, from, to - from)
});
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "range: Range", "Iterator<Item=bool>"]);
let _hash = lib.set_native_fn("bits", |value, range: InclusiveRange| {
let from = INT::max(*range.start(), 0);
let to = INT::max(*range.end(), from - 1);
BitRange::new(value, from, to - from + 1)
});
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "range: RangeInclusive", "Iterator<Item=bool>"]);
let _hash = lib.set_native_fn("bits", BitRange::new);
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "from: INT", "len: INT", "Iterator<Item=bool>"]);
let _hash = lib.set_native_fn("bits", |value, from| BitRange::new(value, from, INT::MAX));
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "from: INT", "Iterator<Item=bool>"]);
let _hash = lib.set_native_fn("bits", |value| BitRange::new(value, 0, INT::MAX) );
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "Iterator<Item=bool>"]);
#[cfg(not(feature = "no_object"))]
{
let _hash = lib.set_getter_fn("bits", |value: &mut INT| BitRange::new(*value, 0, INT::MAX) );
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: &mut INT", "range: Range", "Iterator<Item=bool>"]); lib.update_fn_metadata(_hash, &["string: &str", "from: INT", "len: INT", "Iterator<Item=char>"]);
}
combine_with_exported_module!(lib, "range", range_functions); let _hash = lib.set_native_fn("chars", |string, from| Ok(CharsStream::new(string, from, INT::MAX)));
}); #[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["string: &str", "from: INT", "Iterator<Item=char>"]);
let _hash = lib.set_native_fn("chars", |string| Ok(CharsStream::new(string, 0, INT::MAX)));
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["string: &str", "Iterator<Item=char>"]);
#[cfg(not(feature = "no_object"))]
{
let _hash = lib.set_getter_fn("chars", |string: &mut ImmutableString| Ok(CharsStream::new(string, 0, INT::MAX)));
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["string: &mut ImmutableString", "Iterator<Item=char>"]);
}
// Register bit-field iterator
lib.set_iterator::<BitRange>();
let _hash = lib.set_native_fn("bits", |value, range: ExclusiveRange| {
let from = INT::max(range.start, 0);
let to = INT::max(range.end, from);
BitRange::new(value, from, to - from)
});
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "range: Range", "Iterator<Item=bool>"]);
let _hash = lib.set_native_fn("bits", |value, range: InclusiveRange| {
let from = INT::max(*range.start(), 0);
let to = INT::max(*range.end(), from - 1);
BitRange::new(value, from, to - from + 1)
});
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "range: RangeInclusive", "Iterator<Item=bool>"]);
let _hash = lib.set_native_fn("bits", BitRange::new);
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "from: INT", "len: INT", "Iterator<Item=bool>"]);
let _hash = lib.set_native_fn("bits", |value, from| BitRange::new(value, from, INT::MAX));
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "from: INT", "Iterator<Item=bool>"]);
let _hash = lib.set_native_fn("bits", |value| BitRange::new(value, 0, INT::MAX) );
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "Iterator<Item=bool>"]);
#[cfg(not(feature = "no_object"))]
{
let _hash = lib.set_getter_fn("bits", |value: &mut INT| BitRange::new(*value, 0, INT::MAX) );
#[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: &mut INT", "range: Range", "Iterator<Item=bool>"]);
}
combine_with_exported_module!(lib, "range", range_functions);
}
}
#[export_module] #[export_module]
mod range_functions { mod range_functions {

View File

@ -5,6 +5,15 @@ use crate::{Dynamic, EvalAltResult, INT};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
def_package! {
/// Package of core language features.
crate::LanguageCorePackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "language_core", core_functions);
}
}
#[export_module] #[export_module]
mod core_functions { mod core_functions {
#[rhai_fn(name = "tag", get = "tag", pure)] #[rhai_fn(name = "tag", get = "tag", pure)]
@ -41,9 +50,3 @@ mod core_functions {
} }
} }
} }
def_package!(crate:LanguageCorePackage:"Language core functions.", lib, {
lib.standard = true;
combine_with_exported_module!(lib, "language_core", core_functions);
});

View File

@ -37,33 +37,36 @@ macro_rules! reg_functions {
)* } )* }
} }
def_package!(crate:LogicPackage:"Logical operators.", lib, { def_package! {
lib.standard = true; /// Package of basic logic operators.
crate::LogicPackage => |lib| {
lib.standard = true;
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
{ {
reg_functions!(lib += numbers; i8, u8, i16, u16, i32, u32, u64); reg_functions!(lib += numbers; i8, u8, i16, u16, i32, u32, u64);
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
reg_functions!(lib += num_128; i128, u128); reg_functions!(lib += num_128; i128, u128);
}
#[cfg(not(feature = "no_float"))]
{
#[cfg(not(feature = "f32_float"))]
reg_functions!(lib += float; f32);
combine_with_exported_module!(lib, "f32", f32_functions);
#[cfg(feature = "f32_float")]
reg_functions!(lib += float; f64);
combine_with_exported_module!(lib, "f64", f64_functions);
}
set_exported_fn!(lib, "!", not);
combine_with_exported_module!(lib, "bit_field", bit_field_functions);
} }
}
#[cfg(not(feature = "no_float"))]
{
#[cfg(not(feature = "f32_float"))]
reg_functions!(lib += float; f32);
combine_with_exported_module!(lib, "f32", f32_functions);
#[cfg(feature = "f32_float")]
reg_functions!(lib += float; f64);
combine_with_exported_module!(lib, "f64", f64_functions);
}
set_exported_fn!(lib, "!", not);
combine_with_exported_module!(lib, "bit_field", bit_field_functions);
});
// Logic operators // Logic operators
#[export_fn] #[export_fn]

View File

@ -9,11 +9,14 @@ use std::prelude::v1::*;
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
use crate::Array; use crate::Array;
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, { def_package! {
lib.standard = true; /// Package of basic object map utilities.
crate::BasicMapPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "map", map_functions); combine_with_exported_module!(lib, "map", map_functions);
}); }
}
#[export_module] #[export_module]
mod map_functions { mod map_functions {

View File

@ -58,55 +58,58 @@ macro_rules! reg_functions {
)* } )* }
} }
def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { def_package! {
lib.standard = true; /// Basic mathematical package.
crate::BasicMathPackage => |lib| {
lib.standard = true;
// Integer functions // Integer functions
combine_with_exported_module!(lib, "int", int_functions); combine_with_exported_module!(lib, "int", int_functions);
reg_functions!(lib += basic_to_int::to_int(char)); reg_functions!(lib += basic_to_int::to_int(char));
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
reg_functions!(lib += numbers_to_int::to_int(i8, u8, i16, u16, i32, u32, i64, u64));
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
reg_functions!(lib += num_128_to_int::to_int(i128, u128));
}
#[cfg(not(feature = "no_float"))]
{
// Floating point functions
combine_with_exported_module!(lib, "float", float_functions);
// Trig functions
combine_with_exported_module!(lib, "trig", trig_functions);
reg_functions!(lib += basic_to_float::to_float(INT));
#[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i64"))]
{ {
reg_functions!(lib += numbers_to_float::to_float(i8, u8, i16, u16, i32, u32, i64, u32)); reg_functions!(lib += numbers_to_int::to_int(i8, u8, i16, u16, i32, u32, i64, u64));
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
reg_functions!(lib += num_128_to_float::to_float(i128, u128)); reg_functions!(lib += num_128_to_int::to_int(i128, u128));
}
#[cfg(not(feature = "no_float"))]
{
// Floating point functions
combine_with_exported_module!(lib, "float", float_functions);
// Trig functions
combine_with_exported_module!(lib, "trig", trig_functions);
reg_functions!(lib += basic_to_float::to_float(INT));
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
reg_functions!(lib += numbers_to_float::to_float(i8, u8, i16, u16, i32, u32, i64, u32));
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
reg_functions!(lib += num_128_to_float::to_float(i128, u128));
}
}
// Decimal functions
#[cfg(feature = "decimal")]
{
combine_with_exported_module!(lib, "decimal", decimal_functions);
reg_functions!(lib += basic_to_decimal::to_decimal(INT));
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
reg_functions!(lib += numbers_to_decimal::to_decimal(i8, u8, i16, u16, i32, u32, i64, u64));
} }
} }
}
// Decimal functions
#[cfg(feature = "decimal")]
{
combine_with_exported_module!(lib, "decimal", decimal_functions);
reg_functions!(lib += basic_to_decimal::to_decimal(INT));
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
reg_functions!(lib += numbers_to_decimal::to_decimal(i8, u8, i16, u16, i32, u32, i64, u64));
}
});
#[export_module] #[export_module]
mod int_functions { mod int_functions {

View File

@ -24,6 +24,7 @@ pub use array_basic::BasicArrayPackage;
pub use blob_basic::BasicBlobPackage; pub use blob_basic::BasicBlobPackage;
pub use fn_basic::BasicFnPackage; pub use fn_basic::BasicFnPackage;
pub use iter_basic::BasicIteratorPackage; pub use iter_basic::BasicIteratorPackage;
pub use lang_core::LanguageCorePackage;
pub use logic::LogicPackage; pub use logic::LogicPackage;
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
pub use map_basic::BasicMapPackage; pub use map_basic::BasicMapPackage;
@ -69,8 +70,8 @@ pub trait Package {
/// ``` /// ```
#[macro_export] #[macro_export]
macro_rules! def_package { macro_rules! def_package {
($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => { ($(#[$outer:meta])* $root:ident :: $package:ident => | $lib:ident | $block:stmt) => {
#[doc=$comment] $(#[$outer])*
pub struct $package($root::Shared<$root::Module>); pub struct $package($root::Shared<$root::Module>);
impl $root::packages::Package for $package { impl $root::packages::Package for $package {
@ -88,6 +89,42 @@ macro_rules! def_package {
} }
} }
impl $package {
pub fn new() -> Self {
let mut module = $root::Module::new();
<Self as $root::packages::Package>::init(&mut module);
module.build_index();
Self(module.into())
}
}
};
($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => {
#[deprecated(since = "1.4.0", note = "this is an old syntax of `def_package!` and is deprecated; use the new syntax of `def_package!` instead")]
#[doc=$comment]
///
/// # Deprecated
///
/// This old syntax of `def_package!` is deprecated. Use the new syntax instead.
///
/// This syntax will be removed in the next major version.
pub struct $package($root::Shared<$root::Module>);
impl $root::packages::Package for $package {
fn as_shared_module(&self) -> $root::Shared<$root::Module> {
#[allow(deprecated)]
self.0.clone()
}
fn init($lib: &mut $root::Module) {
$block
}
}
impl Default for $package {
fn default() -> Self {
Self::new()
}
}
impl $package { impl $package {
pub fn new() -> Self { pub fn new() -> Self {
let mut module = $root::Module::new(); let mut module = $root::Module::new();

View File

@ -1,21 +1,27 @@
use super::arithmetic::ArithmeticPackage;
use super::fn_basic::BasicFnPackage;
use super::iter_basic::BasicIteratorPackage;
use super::lang_core::LanguageCorePackage;
use super::logic::LogicPackage;
use super::string_basic::BasicStringPackage;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use crate::def_package; use crate::def_package;
def_package!(crate:CorePackage:"_Core_ package containing basic facilities.", lib, { def_package! {
lib.standard = true; /// Core package containing basic facilities.
///
/// # Contents
///
/// * [`LanguageCorePackage`][super::LanguageCorePackage]
/// * [`ArithmeticPackage`][super::ArithmeticPackage]
/// * [`LogicPackage`][super::LogicPackage]
/// * [`BasicStringPackage`][super::BasicStringPackage]
/// * [`BasicIteratorPackage`][super::BasicIteratorPackage]
/// * [`BasicFnPackage`][super::BasicFnPackage]
crate::CorePackage => |lib| {
lib.standard = true;
LanguageCorePackage::init(lib); super::LanguageCorePackage::init(lib);
ArithmeticPackage::init(lib); super::ArithmeticPackage::init(lib);
LogicPackage::init(lib); super::LogicPackage::init(lib);
BasicStringPackage::init(lib); super::BasicStringPackage::init(lib);
BasicIteratorPackage::init(lib); super::BasicIteratorPackage::init(lib);
BasicFnPackage::init(lib); super::BasicFnPackage::init(lib);
}); }
}

View File

@ -1,32 +1,33 @@
#[cfg(not(feature = "no_index"))]
use super::array_basic::BasicArrayPackage;
#[cfg(not(feature = "no_index"))]
use super::blob_basic::BasicBlobPackage;
#[cfg(not(feature = "no_object"))]
use super::map_basic::BasicMapPackage;
use super::math_basic::BasicMathPackage;
use super::pkg_core::CorePackage;
use super::string_more::MoreStringPackage;
#[cfg(not(feature = "no_std"))]
use super::time_basic::BasicTimePackage;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use crate::def_package; use crate::def_package;
def_package!(crate:StandardPackage:"_Standard_ package containing all built-in features.", lib, { def_package! {
lib.standard = true; /// Standard package containing all built-in features.
///
/// # Contents
///
/// * [`CorePackage`][super::CorePackage]
/// * [`BasicMathPackage`][super::BasicMathPackage]
/// * [`BasicArrayPackage`][super::BasicArrayPackage]
/// * [`BasicBlobPackage`][super::BasicBlobPackage]
/// * [`BasicMapPackage`][super::BasicMapPackage]
/// * [`BasicTimePackage`][super::BasicTimePackage]
/// * [`MoreStringPackage`][super::MoreStringPackage]
crate::StandardPackage => |lib| {
lib.standard = true;
CorePackage::init(lib); super::CorePackage::init(lib);
BasicMathPackage::init(lib); super::BasicMathPackage::init(lib);
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
{ super::BasicArrayPackage::init(lib);
BasicArrayPackage::init(lib); #[cfg(not(feature = "no_index"))]
BasicBlobPackage::init(lib); super::BasicBlobPackage::init(lib);
#[cfg(not(feature = "no_object"))]
super::BasicMapPackage::init(lib);
#[cfg(not(feature = "no_std"))]
super::BasicTimePackage::init(lib);
super::MoreStringPackage::init(lib);
} }
#[cfg(not(feature = "no_object"))] }
BasicMapPackage::init(lib);
#[cfg(not(feature = "no_std"))]
BasicTimePackage::init(lib);
MoreStringPackage::init(lib);
});

View File

@ -15,12 +15,15 @@ use crate::Map;
pub const FUNC_TO_STRING: &str = "to_string"; pub const FUNC_TO_STRING: &str = "to_string";
pub const FUNC_TO_DEBUG: &str = "to_debug"; pub const FUNC_TO_DEBUG: &str = "to_debug";
def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, { def_package! {
lib.standard = true; /// Package of basic string utilities (e.g. printing)
crate::BasicStringPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "print_debug", print_debug_functions); combine_with_exported_module!(lib, "print_debug", print_debug_functions);
combine_with_exported_module!(lib, "number_formatting", number_formatting); combine_with_exported_module!(lib, "number_formatting", number_formatting);
}); }
}
// Register print and debug // Register print and debug

View File

@ -8,11 +8,14 @@ use std::{any::TypeId, mem};
use super::string_basic::{print_with_func, FUNC_TO_STRING}; use super::string_basic::{print_with_func, FUNC_TO_STRING};
def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, { def_package! {
lib.standard = true; /// Package of additional string utilities over [`BasicStringPackage`][super::BasicStringPackage]
crate::MoreStringPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "string", string_functions); combine_with_exported_module!(lib, "string", string_functions);
}); }
}
#[export_module] #[export_module]
mod string_functions { mod string_functions {

View File

@ -13,12 +13,15 @@ use std::time::{Duration, Instant};
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] #[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))]
use instant::{Duration, Instant}; use instant::{Duration, Instant};
def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, { def_package! {
lib.standard = true; /// Package of basic timing utilities.
crate::BasicTimePackage => |lib| {
lib.standard = true;
// Register date/time functions // Register date/time functions
combine_with_exported_module!(lib, "time", time_functions); combine_with_exported_module!(lib, "time", time_functions);
}); }
}
#[export_module] #[export_module]
mod time_functions { mod time_functions {