Merge branch 'main' of github.com:Mathieu-Lala/rhai into fix/lint

This commit is contained in:
Mathieu Lala 2023-02-05 17:18:45 +01:00
commit 097fa1f4e3
No known key found for this signature in database
GPG Key ID: C5D308ABEB84866F
23 changed files with 281 additions and 118 deletions

25
.github/workflows/ci.yaml vendored Normal file
View File

@ -0,0 +1,25 @@
name: Manual CI
on:
workflow_dispatch:
inputs:
depth:
description: "Specify a max number of simultaneous feature flags"
required: true
type: string
default: "2"
jobs:
feature_powerset:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@v1
with:
toolchain: stable
- uses: taiki-e/install-action@v2
with:
tool: cargo-hack@0.5.25
- run: cargo hack check --feature-powerset --depth ${{ inputs.depth }} --no-dev-deps --exclude-features "$FEATURE_EXCLUDE"
env:
FEATURE_EXCLUDE: "no_std stdweb wasm-bindgen f32_float only_i32 unicode-xid-ident bin-features"

View File

@ -1,6 +1,17 @@
Rhai Release Notes Rhai Release Notes
================== ==================
Version 1.13.0
==============
Bug fixes
---------
* Complex indexing/dotting chains now parse correctly, for example: `a[b][c[d]].e`
* `map` and `filter` for arrays are marked `pure`. Warnings are added to the documentation of pure array methods that take `this` closures.
* Syntax such as `foo.bar::baz` no longer panics, but returns a proper parse error.
Version 1.12.0 Version 1.12.0
============== ==============

View File

@ -3,7 +3,7 @@ members = [".", "codegen"]
[package] [package]
name = "rhai" name = "rhai"
version = "1.12.0" version = "1.13.0"
rust-version = "1.61.0" rust-version = "1.61.0"
edition = "2018" edition = "2018"
resolver = "2" resolver = "2"

View File

@ -4,14 +4,14 @@ error: duplicated attribute 'rhai_fn'
6 | #[rhai_fn(pure)] 6 | #[rhai_fn(pure)]
| ^ | ^
error[E0433]: failed to resolve: use of undeclared crate or module `test_module`
--> ui_tests/rhai_fn_duplicate_attr.rs:13:8
|
13 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared crate or module `test_module`
error[E0425]: cannot find value `n` in this scope error[E0425]: cannot find value `n` in this scope
--> ui_tests/rhai_fn_duplicate_attr.rs:13:29 --> ui_tests/rhai_fn_duplicate_attr.rs:13:29
| |
13 | if test_module::test_fn(n) { 13 | if test_module::test_fn(n) {
| ^ not found in this scope | ^ not found in this scope
error[E0433]: failed to resolve: use of undeclared crate or module `test_module`
--> ui_tests/rhai_fn_duplicate_attr.rs:13:8
|
13 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared crate or module `test_module`

View File

@ -202,7 +202,11 @@ impl Engine {
scope: &Scope, scope: &Scope,
scripts: impl AsRef<[S]>, scripts: impl AsRef<[S]>,
) -> ParseResult<AST> { ) -> ParseResult<AST> {
self.compile_with_scope_and_optimization_level(scope, scripts, self.optimization_level) self.compile_with_scope_and_optimization_level(
Some(scope),
scripts,
self.optimization_level,
)
} }
/// Join a list of strings and compile into an [`AST`] using own scope at a specific optimization level. /// Join a list of strings and compile into an [`AST`] using own scope at a specific optimization level.
/// ///
@ -214,7 +218,7 @@ impl Engine {
#[inline] #[inline]
pub(crate) fn compile_with_scope_and_optimization_level<S: AsRef<str>>( pub(crate) fn compile_with_scope_and_optimization_level<S: AsRef<str>>(
&self, &self,
scope: &Scope, scope: Option<&Scope>,
scripts: impl AsRef<[S]>, scripts: impl AsRef<[S]>,
optimization_level: OptimizationLevel, optimization_level: OptimizationLevel,
) -> ParseResult<AST> { ) -> ParseResult<AST> {
@ -291,7 +295,7 @@ impl Engine {
let scripts = [script]; let scripts = [script];
let (stream, t) = self.lex_raw(&scripts, self.token_mapper.as_deref()); let (stream, t) = self.lex_raw(&scripts, self.token_mapper.as_deref());
let interned_strings = &mut *locked_write(&self.interned_strings); let interned_strings = &mut *locked_write(&self.interned_strings);
let state = &mut ParseState::new(scope, interned_strings, t); let state = &mut ParseState::new(Some(scope), interned_strings, t);
self.parse_global_expr(stream.peekable(), state, |_| {}, self.optimization_level) self.parse_global_expr(stream.peekable(), state, |_| {}, self.optimization_level)
} }
} }

View File

@ -69,7 +69,7 @@ impl Engine {
script: &str, script: &str,
) -> RhaiResultOf<T> { ) -> RhaiResultOf<T> {
let ast = self.compile_with_scope_and_optimization_level( let ast = self.compile_with_scope_and_optimization_level(
scope, Some(scope),
[script], [script],
self.optimization_level, self.optimization_level,
)?; )?;
@ -123,7 +123,7 @@ impl Engine {
let (stream, tc) = self.lex_raw(&scripts, self.token_mapper.as_deref()); let (stream, tc) = self.lex_raw(&scripts, self.token_mapper.as_deref());
let state = &mut ParseState::new(scope, interned_strings, tc); let state = &mut ParseState::new(Some(scope), interned_strings, tc);
// No need to optimize a lone expression // No need to optimize a lone expression
self.parse_global_expr( self.parse_global_expr(

View File

@ -4,7 +4,7 @@ use crate::parser::{ParseResult, ParseState};
use crate::types::StringsInterner; use crate::types::StringsInterner;
use crate::{ use crate::{
Engine, ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, OptimizationLevel, Position, Engine, ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, OptimizationLevel, Position,
RhaiError, Scope, SmartString, ERR, RhaiError, SmartString, ERR,
}; };
use std::any::type_name; use std::any::type_name;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
@ -282,9 +282,8 @@ impl Engine {
let (mut stream, tc) = self.lex_raw(&scripts, self.token_mapper.as_deref()); let (mut stream, tc) = self.lex_raw(&scripts, self.token_mapper.as_deref());
tc.borrow_mut().compressed = Some(String::new()); tc.borrow_mut().compressed = Some(String::new());
stream.state.last_token = Some(SmartString::new_const()); stream.state.last_token = Some(SmartString::new_const());
let scope = Scope::new();
let mut interner = StringsInterner::new(); let mut interner = StringsInterner::new();
let mut state = ParseState::new(&scope, &mut interner, tc); let mut state = ParseState::new(None, &mut interner, tc);
let mut _ast = self.parse( let mut _ast = self.parse(
stream.peekable(), stream.peekable(),
&mut state, &mut state,

View File

@ -4,7 +4,7 @@
use crate::func::native::locked_write; use crate::func::native::locked_write;
use crate::parser::{ParseSettingFlags, ParseState}; use crate::parser::{ParseSettingFlags, ParseState};
use crate::tokenizer::Token; use crate::tokenizer::Token;
use crate::{Engine, LexError, Map, OptimizationLevel, RhaiResultOf, Scope}; use crate::{Engine, LexError, Map, OptimizationLevel, RhaiResultOf};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -115,9 +115,8 @@ impl Engine {
); );
let ast = { let ast = {
let scope = Scope::new();
let interned_strings = &mut *locked_write(&self.interned_strings); let interned_strings = &mut *locked_write(&self.interned_strings);
let state = &mut ParseState::new(&scope, interned_strings, tokenizer_control); let state = &mut ParseState::new(None, interned_strings, tokenizer_control);
self.parse_global_expr( self.parse_global_expr(
stream.peekable(), stream.peekable(),

View File

@ -52,7 +52,7 @@ impl Engine {
let mut ast = ast; let mut ast = ast;
let mut _new_ast = self.optimize_into_ast( let mut _new_ast = self.optimize_into_ast(
scope, Some(scope),
ast.take_statements(), ast.take_statements(),
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
ast.shared_lib() ast.shared_lib()

View File

@ -60,7 +60,7 @@ impl Engine {
let ast = { let ast = {
let (stream, tc) = self.lex_raw(&scripts, self.token_mapper.as_deref()); let (stream, tc) = self.lex_raw(&scripts, self.token_mapper.as_deref());
let interned_strings = &mut *locked_write(&self.interned_strings); let interned_strings = &mut *locked_write(&self.interned_strings);
let state = &mut ParseState::new(scope, interned_strings, tc); let state = &mut ParseState::new(Some(scope), interned_strings, tc);
self.parse(stream.peekable(), state, self.optimization_level)? self.parse(stream.peekable(), state, self.optimization_level)?
}; };
self.run_ast_with_scope(scope, &ast) self.run_ast_with_scope(scope, &ast)

View File

@ -389,6 +389,11 @@ impl Engine {
)?, )?,
} }
#[cfg(feature = "debugging")]
let scope2 = &mut Scope::new();
#[cfg(not(feature = "debugging"))]
let scope2 = ();
match lhs { match lhs {
// id.??? or id[???] // id.??? or id[???]
Expr::Variable(.., var_pos) => { Expr::Variable(.., var_pos) => {
@ -399,7 +404,7 @@ impl Engine {
let target = &mut self.search_namespace(global, caches, scope, this_ptr, lhs)?; let target = &mut self.search_namespace(global, caches, scope, this_ptr, lhs)?;
self.eval_dot_index_chain_raw( self.eval_dot_index_chain_raw(
global, caches, None, lhs, expr, target, rhs, idx_values, new_val, global, caches, scope2, None, lhs, expr, target, rhs, idx_values, new_val,
) )
} }
// {expr}.??? = ??? or {expr}[???] = ??? // {expr}.??? = ??? or {expr}[???] = ???
@ -412,7 +417,8 @@ impl Engine {
let obj_ptr = &mut value.into(); let obj_ptr = &mut value.into();
self.eval_dot_index_chain_raw( self.eval_dot_index_chain_raw(
global, caches, this_ptr, lhs_expr, expr, obj_ptr, rhs, idx_values, new_val, global, caches, scope2, this_ptr, lhs_expr, expr, obj_ptr, rhs, idx_values,
new_val,
) )
} }
} }
@ -523,6 +529,8 @@ impl Engine {
&self, &self,
global: &mut GlobalRuntimeState, global: &mut GlobalRuntimeState,
caches: &mut Caches, caches: &mut Caches,
#[cfg(feature = "debugging")] scope: &mut Scope,
#[cfg(not(feature = "debugging"))] scope: (),
this_ptr: Option<&mut Dynamic>, this_ptr: Option<&mut Dynamic>,
root: &Expr, root: &Expr,
parent: &Expr, parent: &Expr,
@ -537,9 +545,6 @@ impl Engine {
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
let mut this_ptr = this_ptr; let mut this_ptr = this_ptr;
#[cfg(feature = "debugging")]
let scope = &mut Scope::new();
match ChainType::from(parent) { match ChainType::from(parent) {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
ChainType::Indexing => { ChainType::Indexing => {
@ -570,8 +575,8 @@ impl Engine {
let obj_ptr = &mut obj; let obj_ptr = &mut obj;
match self.eval_dot_index_chain_raw( match self.eval_dot_index_chain_raw(
global, caches, this_ptr, root, rhs, obj_ptr, &x.rhs, idx_values, global, caches, scope, this_ptr, root, rhs, obj_ptr, &x.rhs,
new_val, idx_values, new_val,
) { ) {
Ok((result, true)) if is_obj_temp_val => { Ok((result, true)) if is_obj_temp_val => {
(Some(obj.take_or_clone()), (result, true)) (Some(obj.take_or_clone()), (result, true))
@ -880,8 +885,8 @@ impl Engine {
}; };
self.eval_dot_index_chain_raw( self.eval_dot_index_chain_raw(
global, caches, _this_ptr, root, rhs, val_target, &x.rhs, idx_values, global, caches, scope, _this_ptr, root, rhs, val_target, &x.rhs,
new_val, idx_values, new_val,
) )
} }
// xxx.sub_lhs[expr] | xxx.sub_lhs.expr // xxx.sub_lhs[expr] | xxx.sub_lhs.expr
@ -926,8 +931,8 @@ impl Engine {
let val = &mut (&mut val).into(); let val = &mut (&mut val).into();
let (result, may_be_changed) = self.eval_dot_index_chain_raw( let (result, may_be_changed) = self.eval_dot_index_chain_raw(
global, caches, _this_ptr, root, rhs, val, &x.rhs, idx_values, global, caches, scope, _this_ptr, root, rhs, val, &x.rhs,
new_val, idx_values, new_val,
)?; )?;
// Feed the value back via a setter just in case it has been updated // Feed the value back via a setter just in case it has been updated
@ -994,8 +999,8 @@ impl Engine {
let val = &mut val.into(); let val = &mut val.into();
self.eval_dot_index_chain_raw( self.eval_dot_index_chain_raw(
global, caches, _this_ptr, root, rhs, val, &x.rhs, idx_values, global, caches, scope, _this_ptr, root, rhs, val, &x.rhs,
new_val, idx_values, new_val,
) )
} }
// xxx.module::fn_name(...) - syntax error // xxx.module::fn_name(...) - syntax error

View File

@ -505,14 +505,21 @@ impl Engine {
event: DebuggerEvent, event: DebuggerEvent,
) -> Result<Option<DebuggerStatus>, Box<crate::EvalAltResult>> { ) -> Result<Option<DebuggerStatus>, Box<crate::EvalAltResult>> {
if let Some(ref x) = self.debugger_interface { if let Some(ref x) = self.debugger_interface {
let orig_scope_len = scope.len();
let src = global.source_raw().cloned(); let src = global.source_raw().cloned();
let src = src.as_ref().map(|s| s.as_str()); let src = src.as_ref().map(|s| s.as_str());
let context = EvalContext::new(self, global, caches, scope, this_ptr); let context = EvalContext::new(self, global, caches, scope, this_ptr);
let (.., ref on_debugger) = **x; let (.., ref on_debugger) = **x;
let command = on_debugger(context, event, node, src, node.position())?; let command = on_debugger(context, event, node, src, node.position());
match command { if orig_scope_len != scope.len() {
// The scope is changed, always search from now on
global.always_search_scope = true;
}
match command? {
DebuggerCommand::Continue => { DebuggerCommand::Continue => {
global.debugger_mut().status = DebuggerStatus::CONTINUE; global.debugger_mut().status = DebuggerStatus::CONTINUE;
Ok(None) Ok(None)

View File

@ -174,9 +174,18 @@ impl Engine {
// Check the variable resolver, if any // Check the variable resolver, if any
if let Some(ref resolve_var) = self.resolve_var { if let Some(ref resolve_var) = self.resolve_var {
let orig_scope_len = scope.len();
let context = EvalContext::new(self, global, caches, scope, this_ptr); let context = EvalContext::new(self, global, caches, scope, this_ptr);
let var_name = expr.get_variable_name(true).expect("`Expr::Variable`"); let var_name = expr.get_variable_name(true).expect("`Expr::Variable`");
match resolve_var(var_name, index, context) { let resolved_var = resolve_var(var_name, index, context);
if orig_scope_len != scope.len() {
// The scope is changed, always search from now on
global.always_search_scope = true;
}
match resolved_var {
Ok(Some(mut result)) => { Ok(Some(mut result)) => {
result.set_access_mode(AccessMode::ReadOnly); result.set_access_mode(AccessMode::ReadOnly);
return Ok(result.into()); return Ok(result.into());
@ -298,7 +307,7 @@ impl Engine {
let source = global.source(); let source = global.source();
let context = &(self, FUNC_TO_STRING, source, &*global, pos).into(); let context = &(self, FUNC_TO_STRING, source, &*global, pos).into();
let display = print_with_func(FUNC_TO_STRING, context, item); let display = print_with_func(FUNC_TO_STRING, context, item);
write!(concat, "{}", display).unwrap(); write!(concat, "{display}").unwrap();
} }
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]

View File

@ -728,10 +728,17 @@ impl Engine {
nesting_level: global.scope_level, nesting_level: global.scope_level,
will_shadow, will_shadow,
}; };
let orig_scope_len = scope.len();
let context = let context =
EvalContext::new(self, global, caches, scope, this_ptr.as_deref_mut()); EvalContext::new(self, global, caches, scope, this_ptr.as_deref_mut());
let filter_result = filter(true, info, context);
if !filter(true, info, context)? { if orig_scope_len != scope.len() {
// The scope is changed, always search from now on
global.always_search_scope = true;
}
if !filter_result? {
return Err(ERR::ErrorForbiddenVariable(var_name.to_string(), *pos).into()); return Err(ERR::ErrorForbiddenVariable(var_name.to_string(), *pos).into());
} }
} }

View File

@ -11,7 +11,7 @@ use crate::eval::{Caches, FnResolutionCacheEntry, GlobalRuntimeState};
use crate::tokenizer::{is_valid_function_name, Token}; use crate::tokenizer::{is_valid_function_name, Token};
use crate::{ use crate::{
calc_fn_hash, calc_fn_hash_full, Dynamic, Engine, FnArgsVec, FnPtr, ImmutableString, calc_fn_hash, calc_fn_hash_full, Dynamic, Engine, FnArgsVec, FnPtr, ImmutableString,
OptimizationLevel, Position, RhaiError, RhaiResult, RhaiResultOf, Scope, Shared, ERR, OptimizationLevel, Position, RhaiResult, RhaiResultOf, Scope, Shared, ERR,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use hashbrown::hash_map::Entry; use hashbrown::hash_map::Entry;
@ -1053,7 +1053,7 @@ impl Engine {
let mut arg_values = curry let mut arg_values = curry
.into_iter() .into_iter()
.map(Ok) .map(Ok)
.chain(a_expr.iter().map(|expr| -> Result<_, RhaiError> { .chain(a_expr.iter().map(|expr| -> Result<_, crate::RhaiError> {
let this_ptr = this_ptr.as_deref_mut(); let this_ptr = this_ptr.as_deref_mut();
self.get_arg_value(global, caches, scope, this_ptr, expr) self.get_arg_value(global, caches, scope, this_ptr, expr)
.map(|(v, ..)| v) .map(|(v, ..)| v)
@ -1518,7 +1518,7 @@ impl Engine {
// Compile the script text // Compile the script text
// No optimizations because we only run it once // No optimizations because we only run it once
let ast = self.compile_with_scope_and_optimization_level( let ast = self.compile_with_scope_and_optimization_level(
&Scope::new(), None,
[script], [script],
#[cfg(not(feature = "no_optimize"))] #[cfg(not(feature = "no_optimize"))]
OptimizationLevel::None, OptimizationLevel::None,

View File

@ -100,8 +100,6 @@ use std::prelude::v1::*;
mod reify; mod reify;
#[macro_use] #[macro_use]
mod restore; mod restore;
#[macro_use]
mod types;
mod api; mod api;
mod ast; mod ast;
@ -117,6 +115,7 @@ mod parser;
pub mod serde; pub mod serde;
mod tests; mod tests;
mod tokenizer; mod tokenizer;
mod types;
/// Error encountered when parsing a script. /// Error encountered when parsing a script.
type PERR = ParseErrorType; type PERR = ParseErrorType;

View File

@ -1288,7 +1288,7 @@ impl Engine {
fn optimize_top_level( fn optimize_top_level(
&self, &self,
statements: StmtBlockContainer, statements: StmtBlockContainer,
scope: &Scope, scope: Option<&Scope>,
lib: &[crate::SharedModule], lib: &[crate::SharedModule],
optimization_level: OptimizationLevel, optimization_level: OptimizationLevel,
) -> StmtBlockContainer { ) -> StmtBlockContainer {
@ -1309,11 +1309,13 @@ impl Engine {
} }
// Add constants and variables from the scope // Add constants and variables from the scope
for (name, constant, value) in scope.iter() { if let Some(scope) = scope {
if constant { for (name, constant, value) in scope.iter() {
state.push_var(name, AccessMode::ReadOnly, Some(value)); if constant {
} else { state.push_var(name, AccessMode::ReadOnly, Some(value));
state.push_var(name, AccessMode::ReadWrite, None); } else {
state.push_var(name, AccessMode::ReadWrite, None);
}
} }
} }
@ -1323,7 +1325,7 @@ impl Engine {
/// Optimize a collection of statements and functions into an [`AST`]. /// Optimize a collection of statements and functions into an [`AST`].
pub(crate) fn optimize_into_ast( pub(crate) fn optimize_into_ast(
&self, &self,
scope: &Scope, scope: Option<&Scope>,
statements: StmtBlockContainer, statements: StmtBlockContainer,
#[cfg(not(feature = "no_function"))] functions: StaticVec< #[cfg(not(feature = "no_function"))] functions: StaticVec<
crate::Shared<crate::ast::ScriptFnDef>, crate::Shared<crate::ast::ScriptFnDef>,

View File

@ -651,6 +651,8 @@ pub mod array_functions {
/// ///
/// Array element (mutable) is bound to `this`. /// Array element (mutable) is bound to `this`.
/// ///
/// This method is marked _pure_; the `mapper` function should not mutate array elements.
///
/// # Function Parameters /// # Function Parameters
/// ///
/// * `element`: copy of array element /// * `element`: copy of array element
@ -669,7 +671,7 @@ pub mod array_functions {
/// ///
/// print(y); // prints "[0, 2, 6, 12, 20]" /// print(y); // prints "[0, 2, 6, 12, 20]"
/// ``` /// ```
#[rhai_fn(return_raw)] #[rhai_fn(return_raw, pure)]
pub fn map(ctx: NativeCallContext, array: &mut Array, map: FnPtr) -> RhaiResultOf<Array> { pub fn map(ctx: NativeCallContext, array: &mut Array, map: FnPtr) -> RhaiResultOf<Array> {
if array.is_empty() { if array.is_empty() {
return Ok(Array::new()); return Ok(Array::new());
@ -692,6 +694,8 @@ pub mod array_functions {
/// ///
/// Array element (mutable) is bound to `this`. /// Array element (mutable) is bound to `this`.
/// ///
/// This method is marked _pure_; the `filter` function should not mutate array elements.
///
/// # Function Parameters /// # Function Parameters
/// ///
/// * `element`: copy of array element /// * `element`: copy of array element
@ -710,7 +714,7 @@ pub mod array_functions {
/// ///
/// print(y); // prints "[12, 20]" /// print(y); // prints "[12, 20]"
/// ``` /// ```
#[rhai_fn(return_raw)] #[rhai_fn(return_raw, pure)]
pub fn filter(ctx: NativeCallContext, array: &mut Array, filter: FnPtr) -> RhaiResultOf<Array> { pub fn filter(ctx: NativeCallContext, array: &mut Array, filter: FnPtr) -> RhaiResultOf<Array> {
if array.is_empty() { if array.is_empty() {
return Ok(Array::new()); return Ok(Array::new());
@ -882,6 +886,8 @@ pub mod array_functions {
/// ///
/// Array element (mutable) is bound to `this`. /// Array element (mutable) is bound to `this`.
/// ///
/// This method is marked _pure_; the `filter` function should not mutate array elements.
///
/// # Function Parameters /// # Function Parameters
/// ///
/// * `element`: copy of array element /// * `element`: copy of array element
@ -922,6 +928,8 @@ pub mod array_functions {
/// ///
/// Array element (mutable) is bound to `this`. /// Array element (mutable) is bound to `this`.
/// ///
/// This method is marked _pure_; the `filter` function should not mutate array elements.
///
/// # Function Parameters /// # Function Parameters
/// ///
/// * `element`: copy of array element /// * `element`: copy of array element
@ -1011,6 +1019,8 @@ pub mod array_functions {
/// ///
/// Array element (mutable) is bound to `this`. /// Array element (mutable) is bound to `this`.
/// ///
/// This method is marked _pure_; the `filter` function should not mutate array elements.
///
/// # Function Parameters /// # Function Parameters
/// ///
/// * `element`: copy of array element /// * `element`: copy of array element
@ -1055,6 +1065,8 @@ pub mod array_functions {
/// ///
/// Array element (mutable) is bound to `this`. /// Array element (mutable) is bound to `this`.
/// ///
/// This method is marked _pure_; the `mapper` function should not mutate array elements.
///
/// # Function Parameters /// # Function Parameters
/// ///
/// * `element`: copy of array element /// * `element`: copy of array element
@ -1087,6 +1099,8 @@ pub mod array_functions {
/// ///
/// Array element (mutable) is bound to `this`. /// Array element (mutable) is bound to `this`.
/// ///
/// This method is marked _pure_; the `mapper` function should not mutate array elements.
///
/// # Function Parameters /// # Function Parameters
/// ///
/// * `element`: copy of array element /// * `element`: copy of array element
@ -1143,6 +1157,8 @@ pub mod array_functions {
/// ///
/// Array element (mutable) is bound to `this`. /// Array element (mutable) is bound to `this`.
/// ///
/// This method is marked _pure_; the `filter` function should not mutate array elements.
///
/// # Function Parameters /// # Function Parameters
/// ///
/// * `element`: copy of array element /// * `element`: copy of array element
@ -1185,6 +1201,8 @@ pub mod array_functions {
/// ///
/// Array element (mutable) is bound to `this`. /// Array element (mutable) is bound to `this`.
/// ///
/// This method is marked _pure_; the `filter` function should not mutate array elements.
///
/// # Function Parameters /// # Function Parameters
/// ///
/// * `element`: copy of array element /// * `element`: copy of array element
@ -1281,9 +1299,11 @@ pub mod array_functions {
/// # Function Parameters /// # Function Parameters
/// ///
/// * `result`: accumulated result, initially `()` /// * `result`: accumulated result, initially `()`
/// * `element`: copy of array element /// * `element`: copy of array element, or bound to `this` if omitted
/// * `index` _(optional)_: current index in the array /// * `index` _(optional)_: current index in the array
/// ///
/// This method is marked _pure_; the `reducer` function should not mutate array elements.
///
/// # Example /// # Example
/// ///
/// ```rhai /// ```rhai
@ -1306,9 +1326,11 @@ pub mod array_functions {
/// # Function Parameters /// # Function Parameters
/// ///
/// * `result`: accumulated result, starting with the value of `initial` /// * `result`: accumulated result, starting with the value of `initial`
/// * `element`: copy of array element /// * `element`: copy of array element, or bound to `this` if omitted
/// * `index` _(optional)_: current index in the array /// * `index` _(optional)_: current index in the array
/// ///
/// This method is marked _pure_; the `reducer` function should not mutate array elements.
///
/// # Example /// # Example
/// ///
/// ```rhai /// ```rhai
@ -1347,9 +1369,11 @@ pub mod array_functions {
/// # Function Parameters /// # Function Parameters
/// ///
/// * `result`: accumulated result, initially `()` /// * `result`: accumulated result, initially `()`
/// * `element`: copy of array element /// * `element`: copy of array element, or bound to `this` if omitted
/// * `index` _(optional)_: current index in the array /// * `index` _(optional)_: current index in the array
/// ///
/// This method is marked _pure_; the `reducer` function should not mutate array elements.
///
/// # Example /// # Example
/// ///
/// ```rhai /// ```rhai
@ -1373,9 +1397,11 @@ pub mod array_functions {
/// # Function Parameters /// # Function Parameters
/// ///
/// * `result`: accumulated result, starting with the value of `initial` /// * `result`: accumulated result, starting with the value of `initial`
/// * `element`: copy of array element /// * `element`: copy of array element, or bound to `this` if omitted
/// * `index` _(optional)_: current index in the array /// * `index` _(optional)_: current index in the array
/// ///
/// This method is marked _pure_; the `reducer` function should not mutate array elements.
///
/// # Example /// # Example
/// ///
/// ```rhai /// ```rhai

View File

@ -55,7 +55,7 @@ pub struct ParseState<'e, 's> {
/// Strings interner. /// Strings interner.
pub interned_strings: &'s mut StringsInterner, pub interned_strings: &'s mut StringsInterner,
/// External [scope][Scope] with constants. /// External [scope][Scope] with constants.
pub scope: &'e Scope<'e>, pub external_constants: Option<&'e Scope<'e>>,
/// Global runtime state. /// Global runtime state.
pub global: Option<Box<GlobalRuntimeState>>, pub global: Option<Box<GlobalRuntimeState>>,
/// Encapsulates a local stack with variable names to simulate an actual runtime scope. /// Encapsulates a local stack with variable names to simulate an actual runtime scope.
@ -65,11 +65,13 @@ pub struct ParseState<'e, 's> {
/// Tracks a list of external variables (variables that are not explicitly declared in the scope). /// Tracks a list of external variables (variables that are not explicitly declared in the scope).
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
pub external_vars: Option<Box<crate::FnArgsVec<Ident>>>, pub external_vars: Option<Box<crate::FnArgsVec<Ident>>>,
/// An indicator that disables variable capturing into externals one single time /// An indicator that, when set to `false`, disables variable capturing into externals one
/// up until the nearest consumed Identifier token. /// single time up until the nearest consumed Identifier token.
/// If set to false the next call to [`access_var`][ParseState::access_var] will not capture the variable. ///
/// If set to `false` the next call to [`access_var`][ParseState::access_var] will not capture
/// the variable.
///
/// All consequent calls to [`access_var`][ParseState::access_var] will not be affected. /// All consequent calls to [`access_var`][ParseState::access_var] will not be affected.
#[cfg(not(feature = "no_closure"))]
pub allow_capture: bool, pub allow_capture: bool,
/// Encapsulates a local stack with imported [module][crate::Module] names. /// Encapsulates a local stack with imported [module][crate::Module] names.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
@ -87,7 +89,7 @@ impl fmt::Debug for ParseState<'_, '_> {
f.field("tokenizer_control", &self.tokenizer_control) f.field("tokenizer_control", &self.tokenizer_control)
.field("interned_strings", &self.interned_strings) .field("interned_strings", &self.interned_strings)
.field("scope", &self.scope) .field("external_constants_scope", &self.external_constants)
.field("global", &self.global) .field("global", &self.global)
.field("stack", &self.stack) .field("stack", &self.stack)
.field("block_stack_len", &self.block_stack_len); .field("block_stack_len", &self.block_stack_len);
@ -109,7 +111,7 @@ impl<'e, 's> ParseState<'e, 's> {
#[inline] #[inline]
#[must_use] #[must_use]
pub fn new( pub fn new(
scope: &'e Scope, external_constants: Option<&'e Scope>,
interned_strings: &'s mut StringsInterner, interned_strings: &'s mut StringsInterner,
tokenizer_control: TokenizerControl, tokenizer_control: TokenizerControl,
) -> Self { ) -> Self {
@ -118,10 +120,9 @@ impl<'e, 's> ParseState<'e, 's> {
expr_filter: |_| true, expr_filter: |_| true,
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
external_vars: None, external_vars: None,
#[cfg(not(feature = "no_closure"))]
allow_capture: true, allow_capture: true,
interned_strings, interned_strings,
scope, external_constants,
global: None, global: None,
stack: None, stack: None,
block_stack_len: 0, block_stack_len: 0,
@ -296,11 +297,8 @@ bitflags! {
/// Is the construct being parsed located at global level? /// Is the construct being parsed located at global level?
const GLOBAL_LEVEL = 0b0000_0001; const GLOBAL_LEVEL = 0b0000_0001;
/// Is the construct being parsed located inside a function definition? /// Is the construct being parsed located inside a function definition?
#[cfg(not(feature = "no_function"))]
const FN_SCOPE = 0b0000_0010; const FN_SCOPE = 0b0000_0010;
/// Is the construct being parsed located inside a closure definition? /// Is the construct being parsed located inside a closure definition?
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_closure"))]
const CLOSURE_SCOPE = 0b0000_0100; const CLOSURE_SCOPE = 0b0000_0100;
/// Is the construct being parsed located inside a breakable loop? /// Is the construct being parsed located inside a breakable loop?
const BREAKABLE = 0b0000_1000; const BREAKABLE = 0b0000_1000;
@ -312,6 +310,16 @@ bitflags! {
} }
} }
bitflags! {
/// Bit-flags containing all status for parsing property/indexing/namespace chains.
struct ChainingFlags: u8 {
/// Is the construct being parsed a property?
const PROPERTY = 0b0000_0001;
/// Disallow namespaces?
const DISALLOW_NAMESPACES = 0b0000_0010;
}
}
/// A type that encapsulates all the settings for a particular parsing function. /// A type that encapsulates all the settings for a particular parsing function.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ParseSettings { pub struct ParseSettings {
@ -456,8 +464,8 @@ fn ensure_not_statement_expr(
/// Make sure that the next expression is not a mis-typed assignment (i.e. `a = b` instead of `a == b`). /// Make sure that the next expression is not a mis-typed assignment (i.e. `a = b` instead of `a == b`).
fn ensure_not_assignment(input: &mut TokenStream) -> ParseResult<()> { fn ensure_not_assignment(input: &mut TokenStream) -> ParseResult<()> {
match input.peek().expect(NEVER_ENDS) { match input.peek().expect(NEVER_ENDS) {
(Token::Equals, pos) => Err(LexError::ImproperSymbol( (token @ Token::Equals, pos) => Err(LexError::ImproperSymbol(
"=".into(), token.literal_syntax().into(),
"Possibly a typo of '=='?".into(), "Possibly a typo of '=='?".into(),
) )
.into_err(*pos)), .into_err(*pos)),
@ -1308,7 +1316,7 @@ impl Engine {
state: &mut ParseState, state: &mut ParseState,
lib: &mut FnLib, lib: &mut FnLib,
settings: ParseSettings, settings: ParseSettings,
is_property: bool, options: ChainingFlags,
) -> ParseResult<Expr> { ) -> ParseResult<Expr> {
let (token, token_pos) = input.peek().expect(NEVER_ENDS); let (token, token_pos) = input.peek().expect(NEVER_ENDS);
@ -1416,7 +1424,7 @@ impl Engine {
// Build new parse state // Build new parse state
let new_interner = &mut StringsInterner::new(); let new_interner = &mut StringsInterner::new();
let new_state = &mut ParseState::new( let new_state = &mut ParseState::new(
state.scope, state.external_constants,
new_interner, new_interner,
state.tokenizer_control.clone(), state.tokenizer_control.clone(),
); );
@ -1444,15 +1452,13 @@ impl Engine {
#[cfg(feature = "no_closure")] #[cfg(feature = "no_closure")]
let options = self.options | (settings.options & LangOptions::STRICT_VAR); let options = self.options | (settings.options & LangOptions::STRICT_VAR);
// Brand new flags, turn on function scope // Brand new flags, turn on function scope and closure scope
let flags = ParseSettingFlags::FN_SCOPE let flags = ParseSettingFlags::FN_SCOPE
| ParseSettingFlags::CLOSURE_SCOPE
| (settings.flags | (settings.flags
& (ParseSettingFlags::DISALLOW_UNQUOTED_MAP_PROPERTIES & (ParseSettingFlags::DISALLOW_UNQUOTED_MAP_PROPERTIES
| ParseSettingFlags::DISALLOW_STATEMENTS_IN_BLOCKS)); | ParseSettingFlags::DISALLOW_STATEMENTS_IN_BLOCKS));
#[cfg(not(feature = "no_closure"))]
let flags = flags | ParseSettingFlags::CLOSURE_SCOPE; // turn on closure scope
let new_settings = ParseSettings { let new_settings = ParseSettings {
flags, flags,
options, options,
@ -1476,7 +1482,9 @@ impl Engine {
&& index.is_none() && index.is_none()
&& !settings.has_flag(ParseSettingFlags::CLOSURE_SCOPE) && !settings.has_flag(ParseSettingFlags::CLOSURE_SCOPE)
&& settings.has_option(LangOptions::STRICT_VAR) && settings.has_option(LangOptions::STRICT_VAR)
&& !state.scope.contains(name) && !state
.external_constants
.map_or(false, |scope| scope.contains(name))
{ {
// If the parent scope is not inside another capturing closure // If the parent scope is not inside another capturing closure
// then we can conclude that the captured variable doesn't exist. // then we can conclude that the captured variable doesn't exist.
@ -1591,14 +1599,12 @@ impl Engine {
token => unreachable!("Token::Identifier expected but gets {:?}", token), token => unreachable!("Token::Identifier expected but gets {:?}", token),
}; };
match input.peek().expect(NEVER_ENDS).0 { match input.peek().expect(NEVER_ENDS) {
// Function call // Function call
Token::LeftParen | Token::Bang | Token::Unit => { (Token::LeftParen | Token::Bang | Token::Unit, _) => {
#[cfg(not(feature = "no_closure"))] // Once the identifier consumed we must enable next variables capturing
{ state.allow_capture = true;
// Once the identifier consumed we must enable next variables capturing
state.allow_capture = true;
}
Expr::Variable( Expr::Variable(
(None, ns, 0, state.get_interned_string(*s)).into(), (None, ns, 0, state.get_interned_string(*s)).into(),
None, None,
@ -1607,12 +1613,18 @@ impl Engine {
} }
// Namespace qualification // Namespace qualification
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
Token::DoubleColon => { (token @ Token::DoubleColon, pos) => {
#[cfg(not(feature = "no_closure"))] if options.contains(ChainingFlags::DISALLOW_NAMESPACES) {
{ return Err(LexError::ImproperSymbol(
// Once the identifier consumed we must enable next variables capturing token.literal_syntax().into(),
state.allow_capture = true; String::new(),
)
.into_err(*pos));
} }
// Once the identifier consumed we must enable next variables capturing
state.allow_capture = true;
let name = state.get_interned_string(*s); let name = state.get_interned_string(*s);
Expr::Variable((None, ns, 0, name).into(), None, settings.pos) Expr::Variable((None, ns, 0, name).into(), None, settings.pos)
} }
@ -1620,11 +1632,13 @@ impl Engine {
_ => { _ => {
let (index, is_func) = state.access_var(&s, lib, settings.pos); let (index, is_func) = state.access_var(&s, lib, settings.pos);
if !is_property if !options.contains(ChainingFlags::PROPERTY)
&& !is_func && !is_func
&& index.is_none() && index.is_none()
&& settings.has_option(LangOptions::STRICT_VAR) && settings.has_option(LangOptions::STRICT_VAR)
&& !state.scope.contains(&s) && !state
.external_constants
.map_or(false, |scope| scope.contains(&s))
{ {
return Err( return Err(
PERR::VariableUndefined(s.to_string()).into_err(settings.pos) PERR::VariableUndefined(s.to_string()).into_err(settings.pos)
@ -1690,7 +1704,14 @@ impl Engine {
return Ok(root_expr); return Ok(root_expr);
} }
self.parse_postfix(input, state, lib, settings, root_expr) self.parse_postfix(
input,
state,
lib,
settings,
root_expr,
ChainingFlags::empty(),
)
} }
/// Tail processing of all possible postfix operators of a primary expression. /// Tail processing of all possible postfix operators of a primary expression.
@ -1701,6 +1722,7 @@ impl Engine {
lib: &mut FnLib, lib: &mut FnLib,
settings: ParseSettings, settings: ParseSettings,
mut lhs: Expr, mut lhs: Expr,
options: ChainingFlags,
) -> ParseResult<Expr> { ) -> ParseResult<Expr> {
let mut settings = settings; let mut settings = settings;
@ -1747,6 +1769,7 @@ impl Engine {
let (.., ns, _, name) = *x; let (.., ns, _, name) = *x;
settings.pos = pos; settings.pos = pos;
self.parse_fn_call(input, state, lib, settings, name, no_args, true, ns)? self.parse_fn_call(input, state, lib, settings, name, no_args, true, ns)?
} }
// Function call // Function call
@ -1754,8 +1777,20 @@ impl Engine {
let (.., ns, _, name) = *x; let (.., ns, _, name) = *x;
let no_args = t == Token::Unit; let no_args = t == Token::Unit;
settings.pos = pos; settings.pos = pos;
self.parse_fn_call(input, state, lib, settings, name, no_args, false, ns)? self.parse_fn_call(input, state, lib, settings, name, no_args, false, ns)?
} }
// Disallowed module separator
#[cfg(not(feature = "no_module"))]
(_, token @ Token::DoubleColon)
if options.contains(ChainingFlags::DISALLOW_NAMESPACES) =>
{
return Err(LexError::ImproperSymbol(
token.literal_syntax().into(),
String::new(),
)
.into_err(tail_pos))
}
// module access // module access
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
(Expr::Variable(x, .., pos), Token::DoubleColon) => { (Expr::Variable(x, .., pos), Token::DoubleColon) => {
@ -1765,11 +1800,9 @@ impl Engine {
namespace.push(var_name_def); namespace.push(var_name_def);
Expr::Variable( let var_name = state.get_interned_string(id2);
(None, namespace, 0, state.get_interned_string(id2)).into(),
None, Expr::Variable((None, namespace, 0, var_name).into(), None, pos2)
pos2,
)
} }
// Indexing // Indexing
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
@ -1788,11 +1821,8 @@ impl Engine {
// Expression after dot must start with an identifier // Expression after dot must start with an identifier
match input.peek().expect(NEVER_ENDS) { match input.peek().expect(NEVER_ENDS) {
(Token::Identifier(..), ..) => { (Token::Identifier(..), ..) => {
#[cfg(not(feature = "no_closure"))] // Prevents capturing of the object properties as vars: xxx.<var>
{ state.allow_capture = false;
// Prevents capturing of the object properties as vars: xxx.<var>
state.allow_capture = false;
}
} }
(Token::Reserved(s), ..) if is_keyword_function(s).1 => (), (Token::Reserved(s), ..) if is_keyword_function(s).1 => (),
(Token::Reserved(s), pos) => { (Token::Reserved(s), pos) => {
@ -1801,12 +1831,15 @@ impl Engine {
(.., pos) => return Err(PERR::PropertyExpected.into_err(*pos)), (.., pos) => return Err(PERR::PropertyExpected.into_err(*pos)),
} }
let rhs = self.parse_primary(input, state, lib, settings.level_up()?, true)?;
let op_flags = match op { let op_flags = match op {
Token::Period => ASTFlags::NONE, Token::Period => ASTFlags::NONE,
Token::Elvis => ASTFlags::NEGATED, Token::Elvis => ASTFlags::NEGATED,
_ => unreachable!("`.` or `?.`"), _ => unreachable!("`.` or `?.`"),
}; };
let options = ChainingFlags::PROPERTY | ChainingFlags::DISALLOW_NAMESPACES;
let rhs =
self.parse_primary(input, state, lib, settings.level_up()?, options)?;
Self::make_dot_expr(state, expr, rhs, ASTFlags::NONE, op_flags, tail_pos)? Self::make_dot_expr(state, expr, rhs, ASTFlags::NONE, op_flags, tail_pos)?
} }
// Unknown postfix operator // Unknown postfix operator
@ -1978,7 +2011,7 @@ impl Engine {
// <EOF> // <EOF>
Token::EOF => Err(PERR::UnexpectedEOF.into_err(settings.pos)), Token::EOF => Err(PERR::UnexpectedEOF.into_err(settings.pos)),
// All other tokens // All other tokens
_ => self.parse_primary(input, state, lib, settings, false), _ => self.parse_primary(input, state, lib, settings, ChainingFlags::empty()),
} }
} }
@ -2077,11 +2110,13 @@ impl Engine {
} }
} }
// ??? && ??? = rhs, ??? || ??? = rhs, xxx ?? xxx = rhs // ??? && ??? = rhs, ??? || ??? = rhs, xxx ?? xxx = rhs
Expr::And(..) | Expr::Or(..) | Expr::Coalesce(..) => Err(LexError::ImproperSymbol( Expr::And(..) | Expr::Or(..) | Expr::Coalesce(..) if !op_info.is_op_assignment() => {
"=".into(), Err(LexError::ImproperSymbol(
"Possibly a typo of '=='?".into(), Token::Equals.literal_syntax().into(),
) "Possibly a typo of '=='?".into(),
.into_err(op_pos)), )
.into_err(op_pos))
}
// expr = rhs // expr = rhs
_ => Err(PERR::AssignmentToInvalidLHS(String::new()).into_err(lhs.position())), _ => Err(PERR::AssignmentToInvalidLHS(String::new()).into_err(lhs.position())),
} }
@ -2098,8 +2133,10 @@ impl Engine {
op_pos: Position, op_pos: Position,
) -> ParseResult<Expr> { ) -> ParseResult<Expr> {
match (lhs, rhs) { match (lhs, rhs) {
// lhs[idx_expr].rhs // lhs[...][...].rhs
(Expr::Index(mut x, options, pos), rhs) => { (Expr::Index(mut x, options, pos), rhs)
if !parent_options.contains(ASTFlags::BREAK) =>
{
let options = options | parent_options; let options = options | parent_options;
x.rhs = Self::make_dot_expr(state, x.rhs, rhs, options, op_flags, op_pos)?; x.rhs = Self::make_dot_expr(state, x.rhs, rhs, options, op_flags, op_pos)?;
Ok(Expr::Index(x, ASTFlags::NONE, pos)) Ok(Expr::Index(x, ASTFlags::NONE, pos))
@ -3298,7 +3335,7 @@ impl Engine {
(Token::Fn, pos) => { (Token::Fn, pos) => {
// Build new parse state // Build new parse state
let new_state = &mut ParseState::new( let new_state = &mut ParseState::new(
state.scope, state.external_constants,
state.interned_strings, state.interned_strings,
state.tokenizer_control.clone(), state.tokenizer_control.clone(),
); );
@ -3848,7 +3885,7 @@ impl Engine {
#[cfg(not(feature = "no_optimize"))] #[cfg(not(feature = "no_optimize"))]
return Ok(self.optimize_into_ast( return Ok(self.optimize_into_ast(
state.scope, state.external_constants,
statements, statements,
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
functions.into_iter().map(|(.., v)| v).collect(), functions.into_iter().map(|(.., v)| v).collect(),
@ -3934,7 +3971,7 @@ impl Engine {
#[cfg(not(feature = "no_optimize"))] #[cfg(not(feature = "no_optimize"))]
return Ok(self.optimize_into_ast( return Ok(self.optimize_into_ast(
state.scope, state.external_constants,
statements, statements,
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
_lib, _lib,

View File

@ -276,8 +276,6 @@ pub enum Token {
/// End of the input stream. /// End of the input stream.
/// Used as a placeholder for the end of input. /// Used as a placeholder for the end of input.
EOF, EOF,
/// Placeholder to indicate the lack of a token.
NONE,
} }
impl fmt::Display for Token { impl fmt::Display for Token {
@ -303,7 +301,6 @@ impl fmt::Display for Token {
Comment(s) => f.write_str(s), Comment(s) => f.write_str(s),
EOF => f.write_str("{EOF}"), EOF => f.write_str("{EOF}"),
NONE => f.write_str("{NONE}"),
token => f.write_str(token.literal_syntax()), token => f.write_str(token.literal_syntax()),
} }
@ -332,7 +329,7 @@ impl Token {
Custom(..) => false, Custom(..) => false,
LexError(..) | Comment(..) => false, LexError(..) | Comment(..) => false,
EOF | NONE => false, EOF => false,
_ => true, _ => true,
} }

View File

@ -133,6 +133,20 @@ fn test_arrays() -> Result<(), Box<EvalAltResult>> {
); );
} }
#[cfg(not(feature = "no_object"))]
assert_eq!(
engine.eval::<INT>(
"
let x = #{ foo: 42 };
let n = 0;
let a = [[x]];
let i = [n];
a[n][i[n]].foo
"
)?,
42
);
assert_eq!( assert_eq!(
engine engine
.eval::<Dynamic>( .eval::<Dynamic>(

View File

@ -144,6 +144,21 @@ fn test_functions_global_module() -> Result<(), Box<EvalAltResult>> {
123 123
); );
// Other globals
let mut module = Module::new();
module.set_var("ANSWER", 123 as INT);
engine.register_global_module(module.into());
assert_eq!(
engine.eval::<INT>(
"
fn foo() { global::ANSWER }
foo()
"
)?,
123
);
Ok(()) Ok(())
} }

View File

@ -17,6 +17,13 @@ fn test_module() {
assert_eq!(module.get_var_value::<INT>("answer").unwrap(), 42); assert_eq!(module.get_var_value::<INT>("answer").unwrap(), 42);
} }
#[test]
fn test_module_syntax() {
let engine = Engine::new();
assert!(engine.compile("abc.def::xyz").is_err());
assert!(engine.compile("abc.def::xyz()").is_err());
}
#[test] #[test]
fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> { fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> {
let mut module = Module::new(); let mut module = Module::new();