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-11-16 16:10:14 +01:00
|
|
|
use crate::fn_native::{CallableFunction, IteratorFn};
|
2020-11-22 10:49:00 +01:00
|
|
|
use crate::stdlib::{any::TypeId, string::String};
|
2020-11-16 16:10:14 +01:00
|
|
|
use crate::{Module, Shared, StaticVec};
|
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;
|
|
|
|
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.
|
|
|
|
fn get(&self) -> PackageLibrary;
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 09:52:28 +01:00
|
|
|
/// A sharable [`Module`][crate::Module] to facilitate sharing library instances.
|
2020-05-22 15:50:24 +02:00
|
|
|
pub type PackageLibrary = Shared<Module>;
|
2020-05-07 09:25:50 +02:00
|
|
|
|
2020-11-20 09:52:28 +01:00
|
|
|
/// Type containing a collection of [`PackageLibrary`] instances.
|
2020-05-07 09:25:50 +02:00
|
|
|
/// All function and type iterator keys in the loaded packages are indexed for fast access.
|
2020-07-13 13:38:50 +02:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-11-19 03:24:43 +01:00
|
|
|
pub(crate) struct PackagesCollection(Option<StaticVec<PackageLibrary>>);
|
2020-05-07 09:25:50 +02:00
|
|
|
|
|
|
|
impl PackagesCollection {
|
2020-11-20 09:52:28 +01:00
|
|
|
/// Add a [`PackageLibrary`] into the [`PackagesCollection`].
|
2020-11-15 05:07:35 +01:00
|
|
|
///
|
|
|
|
/// Packages are searched in reverse order.
|
|
|
|
pub fn add(&mut self, package: PackageLibrary) {
|
2020-11-19 03:24:43 +01:00
|
|
|
if self.0.is_none() {
|
|
|
|
self.0 = Some(Default::default());
|
|
|
|
}
|
2020-05-08 07:27:51 +02:00
|
|
|
// Later packages override previous ones.
|
2020-11-19 03:24:43 +01:00
|
|
|
self.0.as_mut().unwrap().insert(0, package);
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
2020-11-20 09:52:28 +01:00
|
|
|
/// Does the specified function hash key exist in the [`PackagesCollection`]?
|
2020-07-27 12:10:45 +02:00
|
|
|
#[allow(dead_code)]
|
2020-11-16 07:07:48 +01:00
|
|
|
pub fn contains_fn(&self, hash: u64) -> bool {
|
2020-12-18 16:03:56 +01:00
|
|
|
if hash == 0 {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
self.0
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |x| x.iter().any(|p| p.contains_fn(hash, false)))
|
|
|
|
}
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
|
|
|
/// Get specified function via its hash key.
|
2020-11-16 07:07:48 +01:00
|
|
|
pub fn get_fn(&self, hash: u64) -> Option<&CallableFunction> {
|
2020-12-18 16:03:56 +01:00
|
|
|
if hash == 0 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.0
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|x| x.iter().find_map(|p| p.get_fn(hash, false)))
|
|
|
|
}
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
2020-11-20 09:52:28 +01:00
|
|
|
/// Does the specified [`TypeId`] iterator exist in the [`PackagesCollection`]?
|
2020-07-26 04:03:59 +02:00
|
|
|
#[allow(dead_code)]
|
2020-05-13 14:22:05 +02:00
|
|
|
pub fn contains_iter(&self, id: TypeId) -> bool {
|
2020-11-19 03:24:43 +01:00
|
|
|
self.0
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |x| x.iter().any(|p| p.contains_iter(id)))
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
2020-11-20 09:52:28 +01:00
|
|
|
/// Get the specified [`TypeId`] iterator.
|
2020-05-20 13:27:23 +02:00
|
|
|
pub fn get_iter(&self, id: TypeId) -> Option<IteratorFn> {
|
2020-11-19 03:24:43 +01:00
|
|
|
self.0
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|x| x.iter().find_map(|p| p.get_iter(id)))
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
2020-11-22 10:21:34 +01:00
|
|
|
/// Get an iterator over all the packages in the [`PackagesCollection`].
|
|
|
|
pub(crate) fn iter(&self) -> impl Iterator<Item = &PackageLibrary> {
|
|
|
|
self.0.iter().flat_map(|p| p.iter())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate signatures for all the functions in the [`PackagesCollection`].
|
|
|
|
pub fn gen_fn_signatures<'a>(&'a self) -> impl Iterator<Item = String> + 'a {
|
|
|
|
self.iter().flat_map(|m| m.gen_fn_signatures())
|
|
|
|
}
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
2020-05-13 13:21:42 +02:00
|
|
|
|
2020-05-15 15:40:54 +02:00
|
|
|
/// Macro that makes it easy to define a _package_ (which is basically a shared module)
|
2020-05-13 13:21:42 +02:00
|
|
|
/// and register functions into it.
|
|
|
|
///
|
|
|
|
/// Functions can be added to the package using the standard module methods such as
|
2020-11-20 09:52:28 +01:00
|
|
|
/// [`set_fn_2`][Module::set_fn_2], [`set_fn_3_mut`][Module::set_fn_3_mut], [`set_fn_0`][Module::set_fn_0] etc.
|
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
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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.
|
|
|
|
/// lib.set_fn_2("my_add", add);
|
|
|
|
/// });
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The above defines a package named 'MyPackage' with a single function named 'my_add'.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! def_package {
|
|
|
|
($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => {
|
|
|
|
#[doc=$comment]
|
|
|
|
pub struct $package($root::packages::PackageLibrary);
|
|
|
|
|
|
|
|
impl $root::packages::Package for $package {
|
|
|
|
fn get(&self) -> $root::packages::PackageLibrary {
|
|
|
|
self.0.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init($lib: &mut $root::Module) {
|
|
|
|
$block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $package {
|
|
|
|
pub fn new() -> Self {
|
2020-11-15 06:49:54 +01:00
|
|
|
let mut module = $root::Module::new_with_capacity(1024);
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|