Fix no_module build.

This commit is contained in:
Stephen Chung 2020-10-18 22:10:08 +08:00
parent 46b92c9d1f
commit 6e5c903241
6 changed files with 24 additions and 13 deletions

View File

@ -10,7 +10,7 @@ Breaking changes
* `EvalAltResult::ErrorReadingScriptFile` is removed in favor of the new `EvalAltResult::ErrorSystem`. * `EvalAltResult::ErrorReadingScriptFile` is removed in favor of the new `EvalAltResult::ErrorSystem`.
* `EvalAltResult::ErrorLoopBreak` is renamed to `EvalAltResult::LoopBreak`. * `EvalAltResult::ErrorLoopBreak` is renamed to `EvalAltResult::LoopBreak`.
* `Engine::register_raw_fn` function signature has changed. * `Engine::register_raw_fn` and `FnPtr::call_dynamic` function signatures have changed.
New features New features
------------ ------------

View File

@ -425,7 +425,7 @@ pub struct Limits {
pub max_operations: u64, pub max_operations: u64,
/// Maximum number of modules allowed to load. /// Maximum number of modules allowed to load.
/// Not available under `no_module`. /// Not available under `no_module`.
#[cfg(not(feature = "no_modules"))] #[cfg(not(feature = "no_module"))]
pub max_modules: usize, pub max_modules: usize,
/// Maximum length of a string (0 = unlimited). /// Maximum length of a string (0 = unlimited).
pub max_string_size: usize, pub max_string_size: usize,
@ -459,7 +459,7 @@ impl<'e, 'a, 's, 'm, 't, 'd> EvalContext<'e, 'a, 's, 'm, 't, 'd> {
/// _[INTERNALS]_ The current set of modules imported via `import` statements. /// _[INTERNALS]_ The current set of modules imported via `import` statements.
/// Available under the `internals` feature only. /// Available under the `internals` feature only.
#[cfg(feature = "internals")] #[cfg(feature = "internals")]
#[cfg(not(feature = "no_modules"))] #[cfg(not(feature = "no_module"))]
#[inline(always)] #[inline(always)]
pub fn imports(&self) -> &'a Imports { pub fn imports(&self) -> &'a Imports {
self.mods self.mods

View File

@ -1586,6 +1586,7 @@ impl ModuleRef {
pub(crate) fn index(&self) -> Option<NonZeroUsize> { pub(crate) fn index(&self) -> Option<NonZeroUsize> {
self.1 self.1
} }
#[cfg(not(feature = "no_module"))]
pub(crate) fn set_index(&mut self, index: Option<NonZeroUsize>) { pub(crate) fn set_index(&mut self, index: Option<NonZeroUsize>) {
self.1 = index self.1 = index
} }

View File

@ -12,8 +12,12 @@ mod fn_ptr_functions {
pub fn name(f: &mut FnPtr) -> ImmutableString { pub fn name(f: &mut FnPtr) -> ImmutableString {
f.get_fn_name().clone() f.get_fn_name().clone()
} }
#[cfg(not(feature = "no_function"))]
pub mod anonymous {
#[rhai_fn(name = "is_anonymous", get = "is_anonymous")] #[rhai_fn(name = "is_anonymous", get = "is_anonymous")]
pub fn is_anonymous(f: &mut FnPtr) -> bool { pub fn is_anonymous(f: &mut FnPtr) -> bool {
f.is_anonymous() f.is_anonymous()
} }
}
} }

View File

@ -679,19 +679,15 @@ impl<'e> ParseState<'e> {
/// # Panics /// # Panics
/// ///
/// Panics when called under `no_module`. /// Panics when called under `no_module`.
#[cfg(not(feature = "no_module"))]
#[inline(always)] #[inline(always)]
pub fn find_module(&self, name: &str) -> Option<NonZeroUsize> { pub fn find_module(&self, name: &str) -> Option<NonZeroUsize> {
#[cfg(feature = "no_module")] self.modules
unreachable!();
#[cfg(not(feature = "no_module"))]
return self
.modules
.iter() .iter()
.rev() .rev()
.enumerate() .enumerate()
.find(|(_, n)| *n == name) .find(|(_, n)| *n == name)
.and_then(|(i, _)| NonZeroUsize::new(i + 1)); .and_then(|(i, _)| NonZeroUsize::new(i + 1))
} }
} }
@ -1422,6 +1418,7 @@ fn parse_fn_call(
eat_token(input, Token::RightParen); eat_token(input, Token::RightParen);
let hash_script = if let Some(modules) = modules.as_mut() { let hash_script = if let Some(modules) = modules.as_mut() {
#[cfg(not(feature = "no_module"))]
modules.set_index(state.find_module(&modules[0].0)); modules.set_index(state.find_module(&modules[0].0));
// Rust functions are indexed in two steps: // Rust functions are indexed in two steps:
@ -1464,6 +1461,7 @@ fn parse_fn_call(
eat_token(input, Token::RightParen); eat_token(input, Token::RightParen);
let hash_script = if let Some(modules) = modules.as_mut() { let hash_script = if let Some(modules) = modules.as_mut() {
#[cfg(not(feature = "no_module"))]
modules.set_index(state.find_module(&modules[0].0)); modules.set_index(state.find_module(&modules[0].0));
// Rust functions are indexed in two steps: // Rust functions are indexed in two steps:
@ -2049,6 +2047,8 @@ fn parse_primary(
// Qualifiers + variable name // Qualifiers + variable name
*hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, 0, empty()); *hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, 0, empty());
#[cfg(not(feature = "no_module"))]
modules.set_index(state.find_module(&modules[0].0)); modules.set_index(state.find_module(&modules[0].0));
} }
_ => (), _ => (),
@ -3092,6 +3092,8 @@ fn parse_block(
let mut statements = StaticVec::new(); let mut statements = StaticVec::new();
let prev_stack_len = state.stack.len(); let prev_stack_len = state.stack.len();
#[cfg(not(feature = "no_module"))]
let prev_mods_len = state.modules.len(); let prev_mods_len = state.modules.len();
while !match_token(input, Token::RightBrace)? { while !match_token(input, Token::RightBrace)? {
@ -3137,6 +3139,8 @@ fn parse_block(
} }
state.stack.truncate(prev_stack_len); state.stack.truncate(prev_stack_len);
#[cfg(not(feature = "no_module"))]
state.modules.truncate(prev_mods_len); state.modules.truncate(prev_mods_len);
Ok(Stmt::Block(Box::new((statements, settings.pos)))) Ok(Stmt::Block(Box::new((statements, settings.pos))))

View File

@ -86,6 +86,7 @@ impl Engine {
/// Set the maximum number of imported modules allowed for a script. /// Set the maximum number of imported modules allowed for a script.
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
#[cfg(not(feature = "no_module"))]
#[inline(always)] #[inline(always)]
pub fn set_max_modules(&mut self, modules: usize) -> &mut Self { pub fn set_max_modules(&mut self, modules: usize) -> &mut Self {
self.limits_set.max_modules = modules; self.limits_set.max_modules = modules;
@ -94,6 +95,7 @@ impl Engine {
/// The maximum number of imported modules allowed for a script. /// The maximum number of imported modules allowed for a script.
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
#[cfg(not(feature = "no_module"))]
#[inline(always)] #[inline(always)]
pub fn max_modules(&self) -> usize { pub fn max_modules(&self) -> usize {
self.limits_set.max_modules self.limits_set.max_modules