Minor code and docs refactor.

This commit is contained in:
Stephen Chung
2021-11-28 22:57:28 +08:00
parent dca47d5233
commit 9fa4d60336
13 changed files with 175 additions and 127 deletions

View File

@@ -156,7 +156,7 @@ impl Engine {
if eval_ast && !statements.is_empty() {
// Make sure new variables introduced at global level do not _spill_ into the function call
self.eval_global_statements(scope, mods, state, statements, &[ast.lib()], 0)?;
self.eval_global_statements(scope, mods, state, statements, &[ast.as_ref()], 0)?;
if rewind_scope {
scope.rewind(orig_scope_len);
@@ -169,7 +169,7 @@ impl Engine {
let mut args: StaticVec<_> = arg_values.as_mut().iter_mut().collect();
let fn_def = ast
.lib()
.shared_lib()
.get_script_fn(name, args.len())
.ok_or_else(|| EvalAltResult::ErrorFunctionNotFound(name.into(), Position::NONE))?;
@@ -181,7 +181,7 @@ impl Engine {
scope,
mods,
state,
&[ast.lib()],
&[ast.as_ref()],
&mut this_ptr,
fn_def,
&mut args,

View File

@@ -230,6 +230,12 @@ impl NativeCallContext<'_> {
///
/// If `is_method` is [`true`], the first argument is assumed to be passed
/// by reference and is not consumed.
///
/// # Deprecated
///
/// This method is deprecated. Use [`call_fn_raw`][NativeCallContext::call_fn_raw] instead.
///
/// This method will be removed in the next major version.
#[deprecated(since = "1.2.0", note = "use `call_fn_raw` instead")]
#[inline(always)]
pub fn call_fn_dynamic_raw(
@@ -250,3 +256,23 @@ impl<T> From<EvalAltResult> for Result<T, Box<EvalAltResult>> {
Err(err.into())
}
}
impl AST {
/// _(internals)_ Get the internal [`Module`] containing all script-defined functions.
/// Exported under the `internals` feature only.
///
/// Not available under `no_function`.
///
/// # Deprecated
///
/// This method is deprecated. Use [`shared_lib`][AST::shared_lib] instead.
///
/// This method will be removed in the next major version.
#[deprecated(since = "1.3.0", note = "use `shared_lib` instead")]
#[cfg(feature = "internals")]
#[inline(always)]
#[must_use]
pub fn lib(&self) -> &Module {
&self.functions
}
}

View File

@@ -226,7 +226,7 @@ impl Engine {
return Ok(Dynamic::UNIT);
}
let lib = &[ast.lib()];
let lib = &[ast.as_ref()];
self.eval_global_statements(scope, mods, &mut state, statements, lib, level)
}
}

View File

@@ -7,6 +7,80 @@ use std::prelude::v1::*;
use std::num::{NonZeroU64, NonZeroUsize};
/// _(internals)_ A type containing all the limits imposed by the [`Engine`].
/// Exported under the `internals` feature only.
///
/// Not available under `unchecked`.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Limits {
/// Maximum levels of call-stack to prevent infinite recursion.
///
/// Set to zero to effectively disable function calls.
///
/// Not available under `no_function`.
#[cfg(not(feature = "no_function"))]
pub max_call_stack_depth: usize,
/// Maximum depth of statements/expressions at global level.
pub max_expr_depth: Option<NonZeroUsize>,
/// Maximum depth of statements/expressions in functions.
///
/// Not available under `no_function`.
#[cfg(not(feature = "no_function"))]
pub max_function_expr_depth: Option<NonZeroUsize>,
/// Maximum number of operations allowed to run.
pub max_operations: Option<std::num::NonZeroU64>,
/// Maximum number of [modules][Module] allowed to load.
///
/// Set to zero to effectively disable loading any [module][Module].
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))]
pub max_modules: usize,
/// Maximum length of a [string][ImmutableString].
pub max_string_size: Option<NonZeroUsize>,
/// Maximum length of an [array][Array].
///
/// Not available under `no_index`.
#[cfg(not(feature = "no_index"))]
pub max_array_size: Option<NonZeroUsize>,
/// Maximum number of properties in an [object map][Map].
///
/// Not available under `no_object`.
#[cfg(not(feature = "no_object"))]
pub max_map_size: Option<NonZeroUsize>,
}
impl Limits {
/// Create a new [`Limits`] with default values.
///
/// Not available under `unchecked`.
#[inline]
pub const fn new() -> Self {
Self {
#[cfg(not(feature = "no_function"))]
max_call_stack_depth: crate::engine::MAX_CALL_STACK_DEPTH,
max_expr_depth: NonZeroUsize::new(crate::engine::MAX_EXPR_DEPTH),
#[cfg(not(feature = "no_function"))]
max_function_expr_depth: NonZeroUsize::new(crate::engine::MAX_FUNCTION_EXPR_DEPTH),
max_operations: None,
#[cfg(not(feature = "no_module"))]
max_modules: usize::MAX,
max_string_size: None,
#[cfg(not(feature = "no_index"))]
max_array_size: None,
#[cfg(not(feature = "no_object"))]
max_map_size: None,
}
}
}
impl Default for Limits {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}
impl Engine {
/// Set the maximum levels of function calls allowed for a script in order to avoid
/// infinite recursion and stack overflows.

View File

@@ -75,7 +75,7 @@ impl Engine {
#[cfg(not(feature = "no_function"))]
let lib = ast
.lib()
.shared_lib()
.iter_fn()
.filter(|f| f.func.is_script())
.map(|f| {

View File

@@ -64,7 +64,7 @@ impl Engine {
let statements = ast.statements();
if !statements.is_empty() {
let lib = &[ast.lib()];
let lib = &[ast.as_ref()];
self.eval_global_statements(scope, mods, &mut state, statements, lib, 0)?;
}
Ok(())