rhai/src/packages/mod.rs

261 lines
7.9 KiB
Rust
Raw Normal View History

2020-05-15 15:40:54 +02:00
//! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages.
2022-08-18 11:22:56 +02:00
use crate::{Engine, Module, Shared};
2020-05-23 12:59:28 +02:00
pub(crate) mod arithmetic;
2021-12-22 12:59:48 +01:00
pub(crate) mod array_basic;
2022-07-20 15:17:21 +02:00
pub(crate) mod bit_field;
2021-12-22 12:59:48 +01:00
pub(crate) mod blob_basic;
2022-07-20 15:17:21 +02:00
pub(crate) mod debugging;
pub(crate) mod fn_basic;
2022-04-09 07:11:32 +02:00
pub(crate) mod iter_basic;
2022-07-20 15:17:21 +02:00
pub(crate) mod lang_core;
pub(crate) mod logic;
pub(crate) mod map_basic;
pub(crate) mod math_basic;
pub(crate) mod pkg_core;
pub(crate) mod pkg_std;
pub(crate) mod string_basic;
pub(crate) mod string_more;
pub(crate) mod time_basic;
pub use arithmetic::ArithmeticPackage;
#[cfg(not(feature = "no_index"))]
pub use array_basic::BasicArrayPackage;
2022-01-03 16:10:20 +01:00
pub use bit_field::BitFieldPackage;
2021-11-23 07:58:54 +01:00
#[cfg(not(feature = "no_index"))]
pub use blob_basic::BasicBlobPackage;
2022-01-25 07:32:42 +01:00
#[cfg(feature = "debugging")]
pub use debugging::DebuggingPackage;
2020-06-25 12:07:57 +02:00
pub use fn_basic::BasicFnPackage;
pub use iter_basic::BasicIteratorPackage;
2021-12-20 04:42:39 +01:00
pub use lang_core::LanguageCorePackage;
pub use logic::LogicPackage;
#[cfg(not(feature = "no_object"))]
pub use map_basic::BasicMapPackage;
pub use math_basic::BasicMathPackage;
pub use pkg_core::CorePackage;
pub use pkg_std::StandardPackage;
pub use string_basic::BasicStringPackage;
pub use string_more::MoreStringPackage;
2020-04-24 06:39:24 +02:00
#[cfg(not(feature = "no_std"))]
pub use time_basic::BasicTimePackage;
2020-04-21 17:01:10 +02:00
/// Trait that all packages must implement.
pub trait Package {
/// Initialize the package.
/// Functions should be registered into `module` here.
2022-09-28 06:06:22 +02:00
#[cold]
fn init(module: &mut Module);
2022-08-18 11:22:56 +02:00
/// Initialize the package with an [`Engine`].
///
/// Perform tasks such as registering custom operators/syntax.
2022-09-28 06:06:22 +02:00
#[cold]
#[inline]
2022-08-18 11:22:56 +02:00
#[allow(unused_variables)]
fn init_engine(engine: &mut Engine) {}
/// Register the package with an [`Engine`].
///
/// # Example
///
/// ```rust
/// # use rhai::Engine;
/// # use rhai::packages::{Package, CorePackage};
/// let mut engine = Engine::new_raw();
/// let package = CorePackage::new();
///
/// package.register_into_engine(&mut engine);
/// ```
2022-09-28 06:06:22 +02:00
#[cold]
#[inline]
2022-08-18 11:22:56 +02:00
fn register_into_engine(&self, engine: &mut Engine) -> &Self {
Self::init_engine(engine);
engine.register_global_module(self.as_shared_module());
self
}
/// Register the package with an [`Engine`] under a static namespace.
///
/// # Example
///
/// ```rust
/// # use rhai::Engine;
/// # use rhai::packages::{Package, CorePackage};
/// let mut engine = Engine::new_raw();
/// let package = CorePackage::new();
///
/// package.register_into_engine_as(&mut engine, "core");
/// ```
2022-08-18 15:16:42 +02:00
#[cfg(not(feature = "no_module"))]
2022-09-28 06:06:22 +02:00
#[cold]
#[inline]
2022-08-18 11:22:56 +02:00
fn register_into_engine_as(&self, engine: &mut Engine, name: &str) -> &Self {
Self::init_engine(engine);
engine.register_static_module(name, self.as_shared_module());
self
}
/// Get a reference to a shared module from this package.
2021-06-12 16:47:43 +02:00
#[must_use]
2020-12-22 16:45:14 +01:00
fn as_shared_module(&self) -> Shared<Module>;
}
2020-05-13 13:21:42 +02:00
2021-01-11 16:09:33 +01:00
/// Macro that makes it easy to define a _package_ (which is basically a shared [module][Module])
2020-05-13 13:21:42 +02:00
/// and register functions into it.
///
2021-03-15 05:39:06 +01:00
/// Functions can be added to the package using [`Module::set_native_fn`].
2020-05-13 13:21:42 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-05-13 13:21:42 +02:00
///
2021-01-11 16:09:33 +01:00
/// Define a package named `MyPackage` with a single function named `my_add`:
///
2020-05-13 13:21:42 +02:00
/// ```
/// use rhai::{Dynamic, EvalAltResult};
/// use rhai::def_package;
///
/// fn add(x: i64, y: i64) -> Result<i64, Box<EvalAltResult>> { Ok(x + y) }
///
2022-02-10 05:33:48 +01:00
/// def_package! {
/// /// My super-duper package.
2022-09-13 16:54:13 +02:00
/// pub MyPackage(module) {
2022-08-18 11:22:56 +02:00
/// // Load a native Rust function.
2022-02-10 05:33:48 +01:00
/// module.set_native_fn("my_add", add);
/// }
/// }
2020-05-13 13:21:42 +02:00
/// ```
#[macro_export]
macro_rules! def_package {
($($(#[$outer:meta])* $mod:vis $package:ident($lib:ident)
2022-08-18 11:22:56 +02:00
$( : $($(#[$base_meta:meta])* $base_pkg:ty),+ )?
$block:block
$( |> | $engine:ident | $init_engine:block )?
)+) => { $(
2022-02-10 05:33:48 +01:00
$(#[$outer])*
$mod struct $package($crate::Shared<$crate::Module>);
impl $crate::packages::Package for $package {
2022-08-18 11:22:56 +02:00
#[inline(always)]
2022-02-10 05:33:48 +01:00
fn as_shared_module(&self) -> $crate::Shared<$crate::Module> {
self.0.clone()
}
fn init($lib: &mut $crate::Module) {
2022-08-18 11:22:56 +02:00
$($(
$(#[$base_meta])* { <$base_pkg>::init($lib); }
2022-08-18 11:22:56 +02:00
)*)*
2022-02-10 05:33:48 +01:00
$block
}
2022-08-18 11:22:56 +02:00
fn init_engine(_engine: &mut $crate::Engine) {
$($(
$(#[$base_meta])* { <$base_pkg>::init_engine(_engine); }
2022-08-18 11:22:56 +02:00
)*)*
$(
let $engine = _engine;
$init_engine
)*
}
2022-02-10 05:33:48 +01:00
}
impl Default for $package {
2022-08-18 11:22:56 +02:00
#[inline(always)]
2022-09-28 06:06:22 +02:00
#[must_use]
2022-02-10 05:33:48 +01:00
fn default() -> Self {
Self::new()
}
}
impl $package {
2022-06-05 12:17:44 +02:00
#[doc=concat!("Create a new `", stringify!($package), "`")]
2022-08-18 11:22:56 +02:00
#[inline]
2022-07-27 12:04:59 +02:00
#[must_use]
2022-02-10 05:33:48 +01:00
pub fn new() -> Self {
let mut module = $crate::Module::new();
2022-02-10 05:33:48 +01:00
<Self as $crate::packages::Package>::init(&mut module);
module.build_index();
Self(module.into())
}
}
)* };
($($(#[$outer:meta])* $root:ident :: $package:ident => | $lib:ident | $block:block)+) => { $(
2021-12-20 04:42:39 +01:00
$(#[$outer])*
2022-02-10 05:33:48 +01:00
/// # Deprecated
///
/// This old syntax of `def_package!` is deprecated. Use the new syntax instead.
///
/// This syntax will be removed in the next major version.
#[deprecated(since = "1.5.0", note = "this is an old syntax of `def_package!` and is deprecated; use the new syntax of `def_package!` instead")]
2021-12-20 04:42:39 +01:00
pub struct $package($root::Shared<$root::Module>);
impl $root::packages::Package for $package {
fn as_shared_module(&self) -> $root::Shared<$root::Module> {
self.0.clone()
}
fn init($lib: &mut $root::Module) {
$block
}
}
impl Default for $package {
2022-09-28 06:06:22 +02:00
#[inline(always)]
#[must_use]
2021-12-20 04:42:39 +01:00
fn default() -> Self {
Self::new()
}
}
impl $package {
2022-09-28 06:06:22 +02:00
#[inline]
#[must_use]
2021-12-20 04:42:39 +01:00
pub fn new() -> Self {
let mut module = $root::Module::new();
<Self as $root::packages::Package>::init(&mut module);
module.build_index();
Self(module.into())
}
}
)* };
2020-05-13 13:21:42 +02:00
($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => {
#[doc=$comment]
2021-12-20 04:42:39 +01:00
///
/// # Deprecated
///
/// This old syntax of `def_package!` is deprecated. Use the new syntax instead.
///
/// This syntax will be removed in the next major version.
2022-02-10 05:33:48 +01:00
#[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")]
pub struct $package($root::Shared<$root::Module>);
2020-05-13 13:21:42 +02:00
impl $root::packages::Package for $package {
2020-12-22 16:45:14 +01:00
fn as_shared_module(&self) -> $root::Shared<$root::Module> {
2021-12-20 04:42:39 +01:00
#[allow(deprecated)]
2020-05-13 13:21:42 +02:00
self.0.clone()
}
fn init($lib: &mut $root::Module) {
$block
}
}
2021-07-24 08:11:16 +02:00
impl Default for $package {
2022-09-28 06:06:22 +02:00
#[inline(always)]
#[must_use]
2021-07-24 08:11:16 +02:00
fn default() -> Self {
Self::new()
}
}
2020-05-13 13:21:42 +02:00
impl $package {
2022-09-28 06:06:22 +02:00
#[inline]
#[must_use]
2020-05-13 13:21:42 +02:00
pub fn new() -> Self {
2021-03-23 05:13:53 +01:00
let mut module = $root::Module::new();
2020-05-13 13:21:42 +02:00
<Self as $root::packages::Package>::init(&mut module);
2020-11-21 15:18:32 +01:00
module.build_index();
2020-05-13 13:21:42 +02:00
Self(module.into())
}
}
};
}