Allow shadowing of global.

This commit is contained in:
Stephen Chung
2021-04-17 18:40:16 +08:00
parent 9a8da93145
commit eefdc09352
3 changed files with 72 additions and 19 deletions

View File

@@ -128,6 +128,8 @@ fn calc_native_fn_hash<'a>(
pub struct Module {
/// ID identifying the module.
id: Option<Identifier>,
/// Is this module internal?
internal: bool,
/// Sub-modules.
modules: BTreeMap<Identifier, Shared<Module>>,
/// [`Module`] variables.
@@ -156,6 +158,7 @@ impl Default for Module {
fn default() -> Self {
Self {
id: None,
internal: false,
modules: Default::default(),
variables: Default::default(),
all_variables: Default::default(),
@@ -301,8 +304,45 @@ impl Module {
/// assert_eq!(module.id(), Some("hello"));
/// ```
#[inline(always)]
pub fn set_id<S: Into<Identifier>>(&mut self, id: Option<S>) {
pub fn set_id<S: Into<Identifier>>(&mut self, id: Option<S>) -> &mut Self {
self.id = id.map(|s| s.into());
self
}
/// Is the [`Module`] internal?
///
/// # Example
///
/// ```
/// use rhai::Module;
///
/// let mut module = Module::new();
/// assert!(!module.is_internal());
/// module.set_internal(true);
/// assert!(module.is_internal());
/// ```
#[allow(dead_code)]
#[inline(always)]
pub(crate) fn is_internal(&self) -> bool {
self.internal
}
/// Set the interal status of the [`Module`].
///
/// # Example
///
/// ```
/// use rhai::Module;
///
/// let mut module = Module::new();
/// assert!(!module.is_internal());
/// module.set_internal(true);
/// assert!(module.is_internal());
/// ```
#[inline(always)]
pub(crate) fn set_internal(&mut self, value: bool) -> &mut Self {
self.internal = value;
self
}
/// Is the [`Module`] empty?