2020-05-15 15:40:54 +02:00
|
|
|
//! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages.
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-12-22 16:45:14 +01:00
|
|
|
use crate::{Module, Shared};
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-05-23 12:59:28 +02:00
|
|
|
pub(crate) mod arithmetic;
|
2020-04-20 18:11:25 +02:00
|
|
|
mod array_basic;
|
2020-06-25 12:07:57 +02:00
|
|
|
mod fn_basic;
|
2020-04-20 18:11:25 +02:00
|
|
|
mod iter_basic;
|
2021-05-02 17:57:35 +02:00
|
|
|
mod lang_core;
|
2020-04-20 18:11:25 +02:00
|
|
|
mod logic;
|
|
|
|
mod map_basic;
|
|
|
|
mod math_basic;
|
|
|
|
mod pkg_core;
|
|
|
|
mod pkg_std;
|
|
|
|
mod string_basic;
|
|
|
|
mod string_more;
|
|
|
|
mod time_basic;
|
|
|
|
|
|
|
|
pub use arithmetic::ArithmeticPackage;
|
2020-04-21 17:25:12 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-04-20 18:11:25 +02:00
|
|
|
pub use array_basic::BasicArrayPackage;
|
2020-06-25 12:07:57 +02:00
|
|
|
pub use fn_basic::BasicFnPackage;
|
2020-04-20 18:11:25 +02:00
|
|
|
pub use iter_basic::BasicIteratorPackage;
|
|
|
|
pub use logic::LogicPackage;
|
2020-04-21 17:25:12 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-04-20 18:11:25 +02:00
|
|
|
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"))]
|
2020-04-20 18:11:25 +02:00
|
|
|
pub use time_basic::BasicTimePackage;
|
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
/// Trait that all packages must implement.
|
|
|
|
pub trait Package {
|
|
|
|
/// Register all the functions in a package into a store.
|
2020-05-13 13:21:42 +02:00
|
|
|
fn init(lib: &mut Module);
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
/// Retrieve the generic package library from this package.
|
2020-12-22 16:45:14 +01:00
|
|
|
fn as_shared_module(&self) -> Shared<Module>;
|
2020-12-23 08:30:35 +01:00
|
|
|
|
|
|
|
/// Retrieve the generic package library from this package.
|
2021-03-28 11:04:16 +02:00
|
|
|
/// This method is deprecated and will be removed in the future.
|
2021-01-02 16:30:10 +01:00
|
|
|
/// Use [`as_shared_module`][Package::as_shared_module] instead.
|
2021-03-28 11:04:16 +02:00
|
|
|
#[deprecated(since = "0.19.9", note = "use `as_shared_module` instead")]
|
2020-12-23 08:30:35 +01:00
|
|
|
fn get(&self) -> Shared<Module> {
|
|
|
|
self.as_shared_module()
|
|
|
|
}
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
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) }
|
|
|
|
///
|
|
|
|
/// def_package!(rhai:MyPackage:"My super-duper package", lib,
|
|
|
|
/// {
|
|
|
|
/// // Load a binary function with all value parameters.
|
2021-03-15 05:39:06 +01:00
|
|
|
/// lib.set_native_fn("my_add", add);
|
2020-05-13 13:21:42 +02:00
|
|
|
/// });
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! def_package {
|
|
|
|
($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => {
|
|
|
|
#[doc=$comment]
|
2020-12-22 15:36:30 +01:00
|
|
|
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> {
|
2020-05-13 13:21:42 +02:00
|
|
|
self.0.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init($lib: &mut $root::Module) {
|
|
|
|
$block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $package {
|
2021-05-02 17:57:35 +02:00
|
|
|
#[allow(dead_code)]
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|