Inline scope entries.
This commit is contained in:
parent
c2a34bd518
commit
4e5d009386
60
Cargo.toml
60
Cargo.toml
@ -1,8 +1,5 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [".", "codegen"]
|
||||||
".",
|
|
||||||
"codegen"
|
|
||||||
]
|
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "rhai"
|
name = "rhai"
|
||||||
@ -14,14 +11,9 @@ homepage = "https://rhai.rs"
|
|||||||
repository = "https://github.com/rhaiscript"
|
repository = "https://github.com/rhaiscript"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
include = [
|
include = ["**/*.rs", "scripts/*.rhai", "**/*.md", "Cargo.toml"]
|
||||||
"**/*.rs",
|
keywords = ["scripting", "scripting-engine", "scripting-language", "embedded"]
|
||||||
"scripts/*.rhai",
|
categories = ["no-std", "embedded", "wasm", "parser-implementations"]
|
||||||
"**/*.md",
|
|
||||||
"Cargo.toml"
|
|
||||||
]
|
|
||||||
keywords = [ "scripting", "scripting-engine", "scripting-language", "embedded" ]
|
|
||||||
categories = [ "no-std", "embedded", "wasm", "parser-implementations" ]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
smallvec = { version = "1.6", default-features = false, features = ["union"] }
|
smallvec = { version = "1.6", default-features = false, features = ["union"] }
|
||||||
@ -31,29 +23,29 @@ rhai_codegen = { version = "0.3.3", path = "codegen" }
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
unchecked = [] # unchecked arithmetic
|
unchecked = [] # unchecked arithmetic
|
||||||
sync = [] # restrict to only types that implement Send + Sync
|
sync = [] # restrict to only types that implement Send + Sync
|
||||||
no_optimize = [] # no script optimizer
|
no_optimize = [] # no script optimizer
|
||||||
no_float = [] # no floating-point
|
no_float = [] # no floating-point
|
||||||
f32_float = [] # set FLOAT=f32
|
f32_float = [] # set FLOAT=f32
|
||||||
only_i32 = [] # set INT=i32 (useful for 32-bit systems)
|
only_i32 = [] # set INT=i32 (useful for 32-bit systems)
|
||||||
only_i64 = [] # set INT=i64 (default) and disable support for all other integer types
|
only_i64 = [] # set INT=i64 (default) and disable support for all other integer types
|
||||||
decimal = [ "rust_decimal" ] # add the Decimal number type
|
decimal = ["rust_decimal"] # add the Decimal number type
|
||||||
no_index = [] # no arrays and indexing
|
no_index = [] # no arrays and indexing
|
||||||
no_object = [] # no custom objects
|
no_object = [] # no custom objects
|
||||||
no_function = [ "no_closure" ] # no script-defined functions (meaning no closures)
|
no_function = ["no_closure"] # no script-defined functions (meaning no closures)
|
||||||
no_closure = [] # no automatic sharing and capture of anonymous functions to external variables
|
no_closure = [] # no automatic sharing and capture of anonymous functions to external variables
|
||||||
no_module = [] # no modules
|
no_module = [] # no modules
|
||||||
internals = [] # expose internal data structures
|
internals = [] # expose internal data structures
|
||||||
unicode-xid-ident = [ "unicode-xid" ] # allow Unicode Standard Annex #31 for identifiers.
|
unicode-xid-ident = ["unicode-xid"] # allow Unicode Standard Annex #31 for identifiers.
|
||||||
metadata = [ "serde", "serde_json" ] # enables exporting functions metadata to JSON
|
metadata = ["serde", "serde_json"] # enables exporting functions metadata to JSON
|
||||||
|
|
||||||
# compiling for no-std
|
# compiling for no-std
|
||||||
no_std = [ "smallvec/union", "num-traits/libm", "hashbrown", "core-error", "libm", "ahash/compile-time-rng" ]
|
no_std = ["smallvec/union", "num-traits/libm", "hashbrown", "core-error", "libm", "ahash/compile-time-rng"]
|
||||||
|
|
||||||
# compiling for WASM
|
# compiling for WASM
|
||||||
wasm-bindgen = [ "instant/wasm-bindgen" ]
|
wasm-bindgen = ["instant/wasm-bindgen"]
|
||||||
stdweb = [ "instant/stdweb" ]
|
stdweb = ["instant/stdweb"]
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = "fat"
|
lto = "fat"
|
||||||
@ -101,10 +93,10 @@ default_features = false
|
|||||||
optional = true
|
optional = true
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
instant= { version = "0.1" } # WASM implementation of std::time::Instant
|
instant = { version = "0.1" } # WASM implementation of std::time::Instant
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm64")'.dependencies]
|
[target.'cfg(target_arch = "wasm64")'.dependencies]
|
||||||
instant= { version = "0.1" } # WASM implementation of std::time::Instant
|
instant = { version = "0.1" } # WASM implementation of std::time::Instant
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
features = [ "metadata", "internals", "decimal" ]
|
features = ["metadata", "internals", "decimal"]
|
||||||
|
26
src/scope.rs
26
src/scope.rs
@ -4,6 +4,9 @@ use crate::dynamic::{AccessMode, Variant};
|
|||||||
use crate::stdlib::{borrow::Cow, boxed::Box, iter, vec::Vec};
|
use crate::stdlib::{borrow::Cow, boxed::Box, iter, vec::Vec};
|
||||||
use crate::{Dynamic, ImmutableString, StaticVec};
|
use crate::{Dynamic, ImmutableString, StaticVec};
|
||||||
|
|
||||||
|
/// Keep a number of entries inline (since [`Dynamic`] is usually small enough).
|
||||||
|
const SCOPE_SIZE: usize = 16;
|
||||||
|
|
||||||
/// Type containing information about the current scope.
|
/// Type containing information about the current scope.
|
||||||
/// Useful for keeping state between [`Engine`][crate::Engine] evaluation runs.
|
/// Useful for keeping state between [`Engine`][crate::Engine] evaluation runs.
|
||||||
///
|
///
|
||||||
@ -49,17 +52,17 @@ use crate::{Dynamic, ImmutableString, StaticVec};
|
|||||||
#[derive(Debug, Clone, Hash)]
|
#[derive(Debug, Clone, Hash)]
|
||||||
pub struct Scope<'a> {
|
pub struct Scope<'a> {
|
||||||
/// Current value of the entry.
|
/// Current value of the entry.
|
||||||
values: Vec<Dynamic>,
|
values: smallvec::SmallVec<[Dynamic; SCOPE_SIZE]>,
|
||||||
/// (Name, aliases) of the entry.
|
/// (Name, aliases) of the entry.
|
||||||
names: Vec<(Cow<'a, str>, Box<StaticVec<ImmutableString>>)>,
|
names: Vec<(Cow<'a, str>, Option<Box<StaticVec<ImmutableString>>>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Scope<'_> {
|
impl Default for Scope<'_> {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
values: Vec::with_capacity(16),
|
values: Default::default(),
|
||||||
names: Vec::with_capacity(16),
|
names: Vec::with_capacity(SCOPE_SIZE),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -244,7 +247,7 @@ impl<'a> Scope<'a> {
|
|||||||
access: AccessMode,
|
access: AccessMode,
|
||||||
mut value: Dynamic,
|
mut value: Dynamic,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
self.names.push((name.into(), Box::new(Default::default())));
|
self.names.push((name.into(), None));
|
||||||
value.set_access_mode(access);
|
value.set_access_mode(access);
|
||||||
self.values.push(value.into());
|
self.values.push(value.into());
|
||||||
self
|
self
|
||||||
@ -413,8 +416,11 @@ impl<'a> Scope<'a> {
|
|||||||
alias: impl Into<ImmutableString> + PartialEq<ImmutableString>,
|
alias: impl Into<ImmutableString> + PartialEq<ImmutableString>,
|
||||||
) -> &mut Self {
|
) -> &mut Self {
|
||||||
let entry = self.names.get_mut(index).expect("invalid index in Scope");
|
let entry = self.names.get_mut(index).expect("invalid index in Scope");
|
||||||
if !entry.1.iter().any(|a| &alias == a) {
|
if entry.1.is_none() {
|
||||||
entry.1.push(alias.into());
|
entry.1 = Some(Default::default());
|
||||||
|
}
|
||||||
|
if !entry.1.as_ref().unwrap().iter().any(|a| &alias == a) {
|
||||||
|
entry.1.as_mut().unwrap().push(alias.into());
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -446,7 +452,9 @@ impl<'a> Scope<'a> {
|
|||||||
self.names
|
self.names
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.zip(self.values.into_iter())
|
.zip(self.values.into_iter())
|
||||||
.map(|((name, alias), value)| (name, value, alias.to_vec()))
|
.map(|((name, alias), value)| {
|
||||||
|
(name, value, alias.map(|a| a.to_vec()).unwrap_or_default())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
/// Get an iterator to entries in the [`Scope`].
|
/// Get an iterator to entries in the [`Scope`].
|
||||||
/// Shared values are flatten-cloned.
|
/// Shared values are flatten-cloned.
|
||||||
@ -493,7 +501,7 @@ impl<'a, K: Into<Cow<'a, str>>> iter::Extend<(K, Dynamic)> for Scope<'a> {
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn extend<T: IntoIterator<Item = (K, Dynamic)>>(&mut self, iter: T) {
|
fn extend<T: IntoIterator<Item = (K, Dynamic)>>(&mut self, iter: T) {
|
||||||
iter.into_iter().for_each(|(name, value)| {
|
iter.into_iter().for_each(|(name, value)| {
|
||||||
self.names.push((name.into(), Box::new(Default::default())));
|
self.names.push((name.into(), None));
|
||||||
self.values.push(value);
|
self.values.push(value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user