From bca9fe53b0d6194181f23316fe21f40c7aae45f3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 20 Dec 2021 11:42:39 +0800 Subject: [PATCH] New syntax for def_package. --- CHANGELOG.md | 2 + src/packages/arithmetic.rs | 51 ++--- src/packages/array_basic.rs | 15 +- src/packages/blob_basic.rs | 15 +- src/packages/fn_basic.rs | 11 +- src/packages/iter_basic.rs | 405 ++++++++++++++++++----------------- src/packages/lang_core.rs | 15 +- src/packages/logic.rs | 51 ++--- src/packages/map_basic.rs | 11 +- src/packages/math_basic.rs | 81 +++---- src/packages/mod.rs | 41 +++- src/packages/pkg_core.rs | 36 ++-- src/packages/pkg_std.rs | 51 ++--- src/packages/string_basic.rs | 13 +- src/packages/string_more.rs | 11 +- src/packages/time_basic.rs | 13 +- 16 files changed, 452 insertions(+), 370 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fc1fe03..46cf10f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,11 +16,13 @@ Enhancements ------------ * 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 ---------------- * `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 diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index 6de5634b..452d0c22 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -179,36 +179,39 @@ macro_rules! reg_functions { )* } } -def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { - lib.standard = true; +def_package! { + /// Basic arithmetic package. + crate::ArithmeticPackage => |lib| { + lib.standard = true; - combine_with_exported_module!(lib, "int", int_functions); - reg_functions!(lib += signed_basic; INT); + combine_with_exported_module!(lib, "int", int_functions); + reg_functions!(lib += signed_basic; INT); - #[cfg(not(feature = "only_i32"))] - #[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")))] + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] { - reg_functions!(lib += arith_num_128; i128, u128); - reg_functions!(lib += signed_num_128; i128); + 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 += signed_num_128; i128); + } } - } - // Basic arithmetic for floating-point - #[cfg(not(feature = "no_float"))] - { - combine_with_exported_module!(lib, "f32", f32_functions); - combine_with_exported_module!(lib, "f64", f64_functions); - } + // Basic arithmetic for floating-point + #[cfg(not(feature = "no_float"))] + { + combine_with_exported_module!(lib, "f32", f32_functions); + combine_with_exported_module!(lib, "f64", f64_functions); + } - // Decimal functions - #[cfg(feature = "decimal")] - combine_with_exported_module!(lib, "decimal", decimal_functions); -}); + // Decimal functions + #[cfg(feature = "decimal")] + combine_with_exported_module!(lib, "decimal", decimal_functions); + } +} #[export_module] mod int_functions { diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index cc1f68db..8b9476e8 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -11,14 +11,17 @@ use crate::{ use std::prelude::v1::*; use std::{any::TypeId, cmp::Ordering, mem}; -def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { - lib.standard = true; +def_package! { + /// 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 - lib.set_iterable::(); -}); + // Register array iterator + lib.set_iterable::(); + } +} #[export_module] mod array_functions { diff --git a/src/packages/blob_basic.rs b/src/packages/blob_basic.rs index 8e70d94f..bc4e06cd 100644 --- a/src/packages/blob_basic.rs +++ b/src/packages/blob_basic.rs @@ -13,14 +13,17 @@ use std::{any::TypeId, mem}; #[cfg(not(feature = "no_float"))] use crate::FLOAT; -def_package!(crate:BasicBlobPackage:"Basic BLOB utilities.", lib, { - lib.standard = true; +def_package! { + /// 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 - lib.set_iterable::(); -}); + // Register blob iterator + lib.set_iterable::(); + } +} #[export_module] mod blob_functions { diff --git a/src/packages/fn_basic.rs b/src/packages/fn_basic.rs index 37bb6eea..a727ede7 100644 --- a/src/packages/fn_basic.rs +++ b/src/packages/fn_basic.rs @@ -3,11 +3,14 @@ use crate::{def_package, FnPtr, ImmutableString, NativeCallContext}; #[cfg(feature = "no_std")] use std::prelude::v1::*; -def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, { - lib.standard = true; +def_package! { + /// 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] mod fn_ptr_functions { diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 86952e05..bdbcfaf9 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -299,233 +299,236 @@ macro_rules! reg_range { }; } -def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { - lib.standard = true; +def_package! { + /// 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_i64"))] - { - reg_range!(lib | "range" => i8, u8, i16, u16, i32, u32, i64, u64); + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_range!(lib | "range" => i8, u8, i16, u16, i32, u32, i64, u64); - #[cfg(not(target_arch = "wasm32"))] - 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> { - #[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)) - } + #[cfg(not(target_arch = "wasm32"))] + reg_range!(lib | "range" => i128, u128); } - impl Iterator for StepFloatRange { - type Item = FLOAT; + reg_range!(lib | step "range" => INT); - fn next(&mut self) -> Option { - if self.0 == self.1 { - None - } else if self.0 < self.1 { - #[cfg(not(feature = "unchecked"))] - if self.2 < 0.0 { - return None; - } + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_range!(lib | step "range" => i8, u8, i16, u16, i32, u32, i64, u64); - 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) - } - } + #[cfg(not(target_arch = "wasm32"))] + reg_range!(lib | step "range" => i128, u128); } - impl FusedIterator for StepFloatRange {} + #[cfg(not(feature = "no_float"))] + { + use crate::FLOAT; - lib.set_iterator::(); + #[derive(Debug, Clone, Copy, PartialEq)] + struct StepFloatRange(FLOAT, FLOAT, FLOAT); - let _hash = lib.set_native_fn("range", StepFloatRange::new); - #[cfg(feature = "metadata")] - lib.update_fn_metadata(_hash, &["from: FLOAT", "to: FLOAT", "step: FLOAT", "Iterator"]); - } - - #[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> { - #[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 { - if self.0 == self.1 { - None - } else if self.0 < self.1 { + impl StepFloatRange { + pub fn new(from: FLOAT, to: FLOAT, step: FLOAT) -> Result> { #[cfg(not(feature = "unchecked"))] - if self.2.is_sign_negative() { - return None; + 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()); } - 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) + Ok(Self(from, to, step)) } } + + impl Iterator for StepFloatRange { + type Item = FLOAT; + + fn next(&mut self) -> Option { + 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::(); + + let _hash = lib.set_native_fn("range", StepFloatRange::new); + #[cfg(feature = "metadata")] + lib.update_fn_metadata(_hash, &["from: FLOAT", "to: FLOAT", "step: FLOAT", "Iterator"]); } - impl FusedIterator for StepDecimalRange {} + #[cfg(feature = "decimal")] + { + use rust_decimal::Decimal; - lib.set_iterator::(); + #[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> { + #[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 { + 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::(); + + let _hash = lib.set_native_fn("range", StepDecimalRange::new); + #[cfg(feature = "metadata")] + lib.update_fn_metadata(_hash, &["from: Decimal", "to: Decimal", "step: Decimal", "Iterator"]); + } + + // Register string iterator + lib.set_iterator::(); + + 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")] - lib.update_fn_metadata(_hash, &["from: Decimal", "to: Decimal", "step: Decimal", "Iterator"]); - } + lib.update_fn_metadata(_hash, &["string: &str", "range: Range", "Iterator"]); - // Register string iterator - lib.set_iterator::(); - - 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")] - lib.update_fn_metadata(_hash, &["string: &str", "range: Range", "Iterator"]); - - 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"]); - - 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"]); - - 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"]); - - 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"]); - - #[cfg(not(feature = "no_object"))] - { - let _hash = lib.set_getter_fn("chars", |string: &mut ImmutableString| Ok(CharsStream::new(string, 0, INT::MAX))); + 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: &mut ImmutableString", "Iterator"]); - } + lib.update_fn_metadata(_hash, &["string: &str", "range: RangeInclusive", "Iterator"]); - // Register bit-field iterator - lib.set_iterator::(); - - 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"]); - - 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"]); - - let _hash = lib.set_native_fn("bits", BitRange::new); - #[cfg(feature = "metadata")] - lib.update_fn_metadata(_hash, &["value: INT", "from: INT", "len: INT", "Iterator"]); - - 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"]); - - 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"]); - - #[cfg(not(feature = "no_object"))] - { - let _hash = lib.set_getter_fn("bits", |value: &mut INT| BitRange::new(*value, 0, INT::MAX) ); + let _hash = lib.set_native_fn("chars", |string, from, len| Ok(CharsStream::new(string, from, len))); #[cfg(feature = "metadata")] - lib.update_fn_metadata(_hash, &["value: &mut INT", "range: Range", "Iterator"]); - } + lib.update_fn_metadata(_hash, &["string: &str", "from: INT", "len: INT", "Iterator"]); - 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"]); + + 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"]); + + #[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"]); + } + + // Register bit-field iterator + lib.set_iterator::(); + + 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"]); + + 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"]); + + let _hash = lib.set_native_fn("bits", BitRange::new); + #[cfg(feature = "metadata")] + lib.update_fn_metadata(_hash, &["value: INT", "from: INT", "len: INT", "Iterator"]); + + 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"]); + + 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"]); + + #[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"]); + } + + combine_with_exported_module!(lib, "range", range_functions); + } +} #[export_module] mod range_functions { diff --git a/src/packages/lang_core.rs b/src/packages/lang_core.rs index cab9ebc3..75ac1d8e 100644 --- a/src/packages/lang_core.rs +++ b/src/packages/lang_core.rs @@ -5,6 +5,15 @@ use crate::{Dynamic, EvalAltResult, INT}; #[cfg(feature = "no_std")] 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] mod core_functions { #[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); -}); diff --git a/src/packages/logic.rs b/src/packages/logic.rs index d6c5523b..a800425c 100644 --- a/src/packages/logic.rs +++ b/src/packages/logic.rs @@ -37,33 +37,36 @@ macro_rules! reg_functions { )* } } -def_package!(crate:LogicPackage:"Logical operators.", lib, { - lib.standard = true; +def_package! { + /// Package of basic logic operators. + crate::LogicPackage => |lib| { + lib.standard = true; - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { - reg_functions!(lib += numbers; i8, u8, i16, u16, i32, u32, u64); + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_functions!(lib += numbers; i8, u8, i16, u16, i32, u32, u64); - #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] - reg_functions!(lib += num_128; i128, u128); + #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] + 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 #[export_fn] diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index eec7f678..7f6c8e2f 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -9,11 +9,14 @@ use std::prelude::v1::*; #[cfg(not(feature = "no_index"))] use crate::Array; -def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, { - lib.standard = true; +def_package! { + /// 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] mod map_functions { diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 2e9eec92..c596cc62 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -58,55 +58,58 @@ macro_rules! reg_functions { )* } } -def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { - lib.standard = true; +def_package! { + /// Basic mathematical package. + crate::BasicMathPackage => |lib| { + lib.standard = true; - // Integer functions - combine_with_exported_module!(lib, "int", int_functions); + // Integer functions + combine_with_exported_module!(lib, "int", int_functions); - 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)); + reg_functions!(lib += basic_to_int::to_int(char)); #[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)); + 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_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] mod int_functions { diff --git a/src/packages/mod.rs b/src/packages/mod.rs index a18a11cf..dffdb85b 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -24,6 +24,7 @@ pub use array_basic::BasicArrayPackage; pub use blob_basic::BasicBlobPackage; pub use fn_basic::BasicFnPackage; pub use iter_basic::BasicIteratorPackage; +pub use lang_core::LanguageCorePackage; pub use logic::LogicPackage; #[cfg(not(feature = "no_object"))] pub use map_basic::BasicMapPackage; @@ -69,8 +70,8 @@ pub trait Package { /// ``` #[macro_export] macro_rules! def_package { - ($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => { - #[doc=$comment] + ($(#[$outer:meta])* $root:ident :: $package:ident => | $lib:ident | $block:stmt) => { + $(#[$outer])* pub struct $package($root::Shared<$root::Module>); 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(); + ::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 { pub fn new() -> Self { let mut module = $root::Module::new(); diff --git a/src/packages/pkg_core.rs b/src/packages/pkg_core.rs index e82e7fc2..dc106dee 100644 --- a/src/packages/pkg_core.rs +++ b/src/packages/pkg_core.rs @@ -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")] use std::prelude::v1::*; use crate::def_package; -def_package!(crate:CorePackage:"_Core_ package containing basic facilities.", lib, { - lib.standard = true; +def_package! { + /// 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); - ArithmeticPackage::init(lib); - LogicPackage::init(lib); - BasicStringPackage::init(lib); - BasicIteratorPackage::init(lib); - BasicFnPackage::init(lib); -}); + super::LanguageCorePackage::init(lib); + super::ArithmeticPackage::init(lib); + super::LogicPackage::init(lib); + super::BasicStringPackage::init(lib); + super::BasicIteratorPackage::init(lib); + super::BasicFnPackage::init(lib); + } +} diff --git a/src/packages/pkg_std.rs b/src/packages/pkg_std.rs index 3c881232..012aa7d8 100644 --- a/src/packages/pkg_std.rs +++ b/src/packages/pkg_std.rs @@ -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")] use std::prelude::v1::*; use crate::def_package; -def_package!(crate:StandardPackage:"_Standard_ package containing all built-in features.", lib, { - lib.standard = true; +def_package! { + /// 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); - BasicMathPackage::init(lib); - #[cfg(not(feature = "no_index"))] - { - BasicArrayPackage::init(lib); - BasicBlobPackage::init(lib); + super::CorePackage::init(lib); + super::BasicMathPackage::init(lib); + #[cfg(not(feature = "no_index"))] + super::BasicArrayPackage::init(lib); + #[cfg(not(feature = "no_index"))] + 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); -}); +} diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index facc8360..33048147 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -15,12 +15,15 @@ use crate::Map; pub const FUNC_TO_STRING: &str = "to_string"; pub const FUNC_TO_DEBUG: &str = "to_debug"; -def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, { - lib.standard = true; +def_package! { + /// 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, "number_formatting", number_formatting); -}); + combine_with_exported_module!(lib, "print_debug", print_debug_functions); + combine_with_exported_module!(lib, "number_formatting", number_formatting); + } +} // Register print and debug diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index e95c15e3..c29b4af0 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -8,11 +8,14 @@ use std::{any::TypeId, mem}; use super::string_basic::{print_with_func, FUNC_TO_STRING}; -def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, { - lib.standard = true; +def_package! { + /// 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] mod string_functions { diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index c51e340b..bf6a0d50 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -13,12 +13,15 @@ use std::time::{Duration, Instant}; #[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] use instant::{Duration, Instant}; -def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, { - lib.standard = true; +def_package! { + /// Package of basic timing utilities. + crate::BasicTimePackage => |lib| { + lib.standard = true; - // Register date/time functions - combine_with_exported_module!(lib, "time", time_functions); -}); + // Register date/time functions + combine_with_exported_module!(lib, "time", time_functions); + } +} #[export_module] mod time_functions {