Do not build index for multiple packages to avoid Engine creation regression.

This commit is contained in:
Stephen Chung 2020-05-08 13:27:51 +08:00
parent e6fabe58cc
commit e0745ef069
4 changed files with 52 additions and 45 deletions

View File

@ -176,9 +176,17 @@ impl<'a> State<'a> {
}
}
/// An external native Rust function.
#[cfg(not(feature = "sync"))]
pub type NativeFunction = Rc<Box<FnAny>>;
/// An external native Rust function.
#[cfg(feature = "sync")]
pub type NativeFunction = Arc<Box<FnAny>>;
/// A sharable script-defined function.
#[cfg(feature = "sync")]
pub type ScriptedFunction = Arc<FnDef>;
/// A sharable script-defined function.
#[cfg(not(feature = "sync"))]
pub type ScriptedFunction = Rc<FnDef>;
@ -512,6 +520,15 @@ impl Engine {
self.packages.push(package);
}
/// Load a new package into the `Engine`.
///
/// When searching for functions, packages loaded later are preferred.
/// In other words, loaded packages are searched in reverse order.
pub fn load_packages(&mut self, package: PackageLibrary) {
// Push the package to the top - packages are searched in reverse order
self.packages.push(package);
}
/// Control whether and how the `Engine` will optimize an AST after compilation.
///
/// Not available under the `no_optimize` feature.

View File

@ -3,7 +3,7 @@
use crate::any::{Dynamic, Variant};
use crate::calc_fn_hash;
use crate::engine::{Engine, FnAny, FnCallArgs, FunctionsLib, ScriptedFunction};
use crate::engine::{Engine, FnAny, FnCallArgs, FunctionsLib, NativeFunction, ScriptedFunction};
use crate::parser::{FnDef, AST};
use crate::result::EvalAltResult;
use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope};
@ -25,11 +25,6 @@ use crate::stdlib::{
vec::Vec,
};
#[cfg(not(feature = "sync"))]
type NativeFunction = Rc<Box<FnAny>>;
#[cfg(feature = "sync")]
type NativeFunction = Arc<Box<FnAny>>;
/// A trait that encapsulates a module resolution service.
pub trait ModuleResolver {
/// Resolve a module based on a path string.

View File

@ -34,11 +34,10 @@ pub use time_basic::BasicTimePackage;
pub use utils::*;
const NUM_NATIVE_FUNCTIONS: usize = 512;
/// Trait that all packages must implement.
pub trait Package {
/// Create a new instance of a package.
fn new() -> Self;
/// Register all the functions in a package into a store.
fn init(lib: &mut PackageStore);
@ -47,7 +46,6 @@ pub trait Package {
}
/// Type to store all functions in the package.
#[derive(Default)]
pub struct PackageStore {
/// All functions, keyed by a hash created from the function name and parameter types.
pub functions: HashMap<u64, Box<FnAny>>,
@ -61,14 +59,6 @@ impl PackageStore {
pub fn new() -> Self {
Default::default()
}
/// Get an iterator over the keys of the functions in the `PackageStore`.
pub fn function_keys(&self) -> impl Iterator<Item = &u64> {
self.functions.keys()
}
/// Get an iterator over the `TypeId` of the type iterators in the `PackageStore`.
pub fn type_iterator_keys(&self) -> impl Iterator<Item = &TypeId> {
self.type_iterators.keys()
}
/// Does the specified function hash key exist in the `PackageStore`?
pub fn contains_function(&self, hash: u64) -> bool {
self.functions.contains_key(&hash)
@ -87,6 +77,15 @@ impl PackageStore {
}
}
impl Default for PackageStore {
fn default() -> Self {
Self {
functions: HashMap::with_capacity(NUM_NATIVE_FUNCTIONS),
type_iterators: HashMap::with_capacity(4),
}
}
}
/// Type which `Rc`-wraps a `PackageStore` to facilitate sharing library instances.
#[cfg(not(feature = "sync"))]
pub type PackageLibrary = Rc<PackageStore>;
@ -95,48 +94,42 @@ pub type PackageLibrary = Rc<PackageStore>;
#[cfg(feature = "sync")]
pub type PackageLibrary = Arc<PackageStore>;
#[derive(Default)]
/// Type containing a collection of `PackageLibrary` instances.
/// All function and type iterator keys in the loaded packages are indexed for fast access.
#[derive(Clone, Default)]
pub(crate) struct PackagesCollection {
/// Collection of `PackageLibrary` instances.
packages: Vec<PackageLibrary>,
/// Index of all function keys, pointing to the offset in `packages`.
function_keys: HashMap<u64, usize>,
/// Index of all type iterator `TypeId`'s, pointing to the offset in `packages`.
iterator_types: HashMap<TypeId, usize>,
}
impl PackagesCollection {
/// Add a `PackageLibrary` into the `PackagesCollection`.
pub fn push(&mut self, package: PackageLibrary) {
let index = self.packages.len();
package.function_keys().for_each(|&hash| {
self.function_keys.insert(hash, index);
});
package.type_iterator_keys().for_each(|&id| {
self.iterator_types.insert(id, index);
});
self.packages.push(package);
// Later packages override previous ones.
self.packages.insert(0, package);
}
/// Does the specified function hash key exist in the `PackagesCollection`?
pub fn contains_function(&self, hash: u64) -> bool {
self.function_keys.contains_key(&hash)
self.packages.iter().any(|p| p.contains_function(hash))
}
/// Get specified function via its hash key.
pub fn get_function(&self, hash: u64) -> Option<&Box<FnAny>> {
self.function_keys
.get(&hash)
.and_then(|&index| self.packages[index].functions.get(&hash))
self.packages
.iter()
.map(|p| p.get_function(hash))
.find(|f| f.is_some())
.flatten()
}
/// Does the specified TypeId iterator exist in the `PackagesCollection`?
pub fn contains_iterator(&self, id: TypeId) -> bool {
self.iterator_types.contains_key(&id)
self.packages.iter().any(|p| p.contains_iterator(id))
}
/// Get the specified TypeId iterator.
pub fn get_iterator(&self, id: TypeId) -> Option<&Box<IteratorFn>> {
self.iterator_types
.get(&id)
.and_then(|&index| self.packages[index].type_iterators.get(&id))
self.packages
.iter()
.map(|p| p.get_iterator(id))
.find(|f| f.is_some())
.flatten()
}
}

View File

@ -44,12 +44,6 @@ macro_rules! def_package {
pub struct $package($root::packages::PackageLibrary);
impl $root::packages::Package for $package {
fn new() -> Self {
let mut pkg = $root::packages::PackageStore::new();
Self::init(&mut pkg);
Self(pkg.into())
}
fn get(&self) -> $root::packages::PackageLibrary {
self.0.clone()
}
@ -58,6 +52,14 @@ macro_rules! def_package {
$block
}
}
impl $package {
pub fn new() -> Self {
let mut pkg = $root::packages::PackageStore::new();
<Self as $root::packages::Package>::init(&mut pkg);
Self(pkg.into())
}
}
};
}