2020-04-21 17:01:10 +02:00
|
|
|
//! This module contains all built-in _packages_ available to Rhai, plus facilities to define custom packages.
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
use crate::engine::{FnAny, IteratorFn};
|
|
|
|
|
2020-05-07 09:25:50 +02:00
|
|
|
use crate::stdlib::{any::TypeId, boxed::Box, collections::HashMap, rc::Rc, sync::Arc, vec::Vec};
|
2020-04-20 18:11:25 +02:00
|
|
|
|
|
|
|
mod arithmetic;
|
|
|
|
mod array_basic;
|
|
|
|
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;
|
2020-04-21 17:01:10 +02:00
|
|
|
mod utils;
|
2020-04-20 18:11:25 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
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
|
|
|
pub use utils::*;
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-05-08 07:27:51 +02:00
|
|
|
const NUM_NATIVE_FUNCTIONS: usize = 512;
|
|
|
|
|
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.
|
|
|
|
fn init(lib: &mut PackageStore);
|
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-04-21 17:01:10 +02:00
|
|
|
/// Type to store all functions in the package.
|
|
|
|
pub struct PackageStore {
|
|
|
|
/// All functions, keyed by a hash created from the function name and parameter types.
|
|
|
|
pub functions: HashMap<u64, Box<FnAny>>,
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
/// All iterator functions, keyed by the type producing the iterator.
|
|
|
|
pub type_iterators: HashMap<TypeId, Box<IteratorFn>>,
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
impl PackageStore {
|
|
|
|
/// Create a new `PackageStore`.
|
|
|
|
pub fn new() -> Self {
|
2020-05-05 09:00:10 +02:00
|
|
|
Default::default()
|
2020-04-21 17:01:10 +02:00
|
|
|
}
|
2020-05-07 09:25:50 +02:00
|
|
|
/// Does the specified function hash key exist in the `PackageStore`?
|
|
|
|
pub fn contains_function(&self, hash: u64) -> bool {
|
|
|
|
self.functions.contains_key(&hash)
|
|
|
|
}
|
|
|
|
/// Get specified function via its hash key.
|
|
|
|
pub fn get_function(&self, hash: u64) -> Option<&Box<FnAny>> {
|
|
|
|
self.functions.get(&hash)
|
|
|
|
}
|
|
|
|
/// Does the specified TypeId iterator exist in the `PackageStore`?
|
|
|
|
pub fn contains_iterator(&self, id: TypeId) -> bool {
|
|
|
|
self.type_iterators.contains_key(&id)
|
|
|
|
}
|
|
|
|
/// Get the specified TypeId iterator.
|
|
|
|
pub fn get_iterator(&self, id: TypeId) -> Option<&Box<IteratorFn>> {
|
|
|
|
self.type_iterators.get(&id)
|
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 07:27:51 +02:00
|
|
|
impl Default for PackageStore {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
functions: HashMap::with_capacity(NUM_NATIVE_FUNCTIONS),
|
|
|
|
type_iterators: HashMap::with_capacity(4),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
/// Type which `Rc`-wraps a `PackageStore` to facilitate sharing library instances.
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
pub type PackageLibrary = Rc<PackageStore>;
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-04-21 17:01:10 +02:00
|
|
|
/// Type which `Arc`-wraps a `PackageStore` to facilitate sharing library instances.
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
pub type PackageLibrary = Arc<PackageStore>;
|
2020-05-07 09:25:50 +02:00
|
|
|
|
|
|
|
/// Type containing a collection of `PackageLibrary` instances.
|
|
|
|
/// All function and type iterator keys in the loaded packages are indexed for fast access.
|
2020-05-08 07:27:51 +02:00
|
|
|
#[derive(Clone, Default)]
|
2020-05-07 09:25:50 +02:00
|
|
|
pub(crate) struct PackagesCollection {
|
|
|
|
/// Collection of `PackageLibrary` instances.
|
|
|
|
packages: Vec<PackageLibrary>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PackagesCollection {
|
|
|
|
/// Add a `PackageLibrary` into the `PackagesCollection`.
|
|
|
|
pub fn push(&mut self, package: PackageLibrary) {
|
2020-05-08 07:27:51 +02:00
|
|
|
// Later packages override previous ones.
|
|
|
|
self.packages.insert(0, package);
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
|
|
|
/// Does the specified function hash key exist in the `PackagesCollection`?
|
|
|
|
pub fn contains_function(&self, hash: u64) -> bool {
|
2020-05-08 07:27:51 +02:00
|
|
|
self.packages.iter().any(|p| p.contains_function(hash))
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
|
|
|
/// Get specified function via its hash key.
|
|
|
|
pub fn get_function(&self, hash: u64) -> Option<&Box<FnAny>> {
|
2020-05-08 07:27:51 +02:00
|
|
|
self.packages
|
|
|
|
.iter()
|
|
|
|
.map(|p| p.get_function(hash))
|
|
|
|
.find(|f| f.is_some())
|
|
|
|
.flatten()
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
|
|
|
/// Does the specified TypeId iterator exist in the `PackagesCollection`?
|
|
|
|
pub fn contains_iterator(&self, id: TypeId) -> bool {
|
2020-05-08 07:27:51 +02:00
|
|
|
self.packages.iter().any(|p| p.contains_iterator(id))
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
|
|
|
/// Get the specified TypeId iterator.
|
|
|
|
pub fn get_iterator(&self, id: TypeId) -> Option<&Box<IteratorFn>> {
|
2020-05-08 07:27:51 +02:00
|
|
|
self.packages
|
|
|
|
.iter()
|
|
|
|
.map(|p| p.get_iterator(id))
|
|
|
|
.find(|f| f.is_some())
|
|
|
|
.flatten()
|
2020-05-07 09:25:50 +02:00
|
|
|
}
|
|
|
|
}
|