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.
* 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

View File

@ -179,7 +179,9 @@ macro_rules! reg_functions {
)* }
}
def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
def_package! {
/// Basic arithmetic package.
crate::ArithmeticPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "int", int_functions);
@ -208,7 +210,8 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
// Decimal functions
#[cfg(feature = "decimal")]
combine_with_exported_module!(lib, "decimal", decimal_functions);
});
}
}
#[export_module]
mod int_functions {

View File

@ -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, {
def_package! {
/// Package of basic array utilities.
crate::BasicArrayPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "array", array_functions);
// Register array iterator
lib.set_iterable::<Array>();
});
}
}
#[export_module]
mod array_functions {

View File

@ -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, {
def_package! {
/// Package of basic BLOB utilities.
crate::BasicBlobPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "blob", blob_functions);
// Register blob iterator
lib.set_iterable::<Blob>();
});
}
}
#[export_module]
mod blob_functions {

View File

@ -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, {
def_package! {
/// Package of basic function poitner utilities.
crate::BasicFnPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "FnPtr", fn_ptr_functions);
});
}
}
#[export_module]
mod fn_ptr_functions {

View File

@ -299,7 +299,9 @@ macro_rules! reg_range {
};
}
def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
def_package! {
/// Package of basic range iterators
crate::BasicIteratorPackage => |lib| {
lib.standard = true;
reg_range!(lib | "range" => INT);
@ -525,7 +527,8 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
}
combine_with_exported_module!(lib, "range", range_functions);
});
}
}
#[export_module]
mod range_functions {

View File

@ -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);
});

View File

@ -37,7 +37,9 @@ macro_rules! reg_functions {
)* }
}
def_package!(crate:LogicPackage:"Logical operators.", lib, {
def_package! {
/// Package of basic logic operators.
crate::LogicPackage => |lib| {
lib.standard = true;
#[cfg(not(feature = "only_i32"))]
@ -63,7 +65,8 @@ def_package!(crate:LogicPackage:"Logical operators.", lib, {
set_exported_fn!(lib, "!", not);
combine_with_exported_module!(lib, "bit_field", bit_field_functions);
});
}
}
// Logic operators
#[export_fn]

View File

@ -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, {
def_package! {
/// Package of basic object map utilities.
crate::BasicMapPackage => |lib| {
lib.standard = true;
combine_with_exported_module!(lib, "map", map_functions);
});
}
}
#[export_module]
mod map_functions {

View File

@ -58,7 +58,9 @@ macro_rules! reg_functions {
)* }
}
def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
def_package! {
/// Basic mathematical package.
crate::BasicMathPackage => |lib| {
lib.standard = true;
// Integer functions
@ -106,7 +108,8 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
#[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 {

View File

@ -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();
<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 {
pub fn new() -> Self {
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")]
use std::prelude::v1::*;
use crate::def_package;
def_package!(crate:CorePackage:"_Core_ package containing basic facilities.", lib, {
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);
}
}

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")]
use std::prelude::v1::*;
use crate::def_package;
def_package!(crate:StandardPackage:"_Standard_ package containing all built-in features.", lib, {
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);
super::CorePackage::init(lib);
super::BasicMathPackage::init(lib);
#[cfg(not(feature = "no_index"))]
{
BasicArrayPackage::init(lib);
BasicBlobPackage::init(lib);
}
super::BasicArrayPackage::init(lib);
#[cfg(not(feature = "no_index"))]
super::BasicBlobPackage::init(lib);
#[cfg(not(feature = "no_object"))]
BasicMapPackage::init(lib);
super::BasicMapPackage::init(lib);
#[cfg(not(feature = "no_std"))]
BasicTimePackage::init(lib);
MoreStringPackage::init(lib);
});
super::BasicTimePackage::init(lib);
super::MoreStringPackage::init(lib);
}
}

View File

@ -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, {
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);
});
}
}
// 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};
def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, {
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);
});
}
}
#[export_module]
mod string_functions {

View File

@ -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, {
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);
});
}
}
#[export_module]
mod time_functions {