Fix test.

This commit is contained in:
Stephen Chung 2021-11-28 10:49:48 +08:00
parent 9c4ed44ab8
commit f49ff28b82
3 changed files with 11 additions and 5 deletions

View File

@ -1,13 +1,12 @@
//! Module implementing the [`AST`] optimizer. //! Module implementing the [`AST`] optimizer.
#![cfg(not(feature = "no_optimize"))] #![cfg(not(feature = "no_optimize"))]
use crate::ast::{Expr, OpAssignment, ScriptFnDef, Stmt, StmtBlock, AST_OPTION_FLAGS::*}; use crate::ast::{Expr, OpAssignment, ScriptFnDef, Stmt, AST_OPTION_FLAGS::*};
use crate::engine::{ use crate::engine::{
EvalState, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_PRINT, KEYWORD_TYPE_OF, EvalState, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_PRINT, KEYWORD_TYPE_OF,
}; };
use crate::func::builtin::get_builtin_binary_op_fn; use crate::func::builtin::get_builtin_binary_op_fn;
use crate::func::hashing::get_hasher; use crate::func::hashing::get_hasher;
use crate::func::native::shared_take_or_clone;
use crate::tokenizer::Token; use crate::tokenizer::Token;
use crate::types::dynamic::AccessMode; use crate::types::dynamic::AccessMode;
use crate::{ use crate::{
@ -1149,7 +1148,7 @@ pub fn optimize_into_ast(
.map(|fn_def| ScriptFnDef { .map(|fn_def| ScriptFnDef {
name: fn_def.name.clone(), name: fn_def.name.clone(),
access: fn_def.access, access: fn_def.access,
body: StmtBlock::NONE, body: crate::ast::StmtBlock::NONE,
params: fn_def.params.clone(), params: fn_def.params.clone(),
lib: None, lib: None,
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
@ -1167,7 +1166,7 @@ pub fn optimize_into_ast(
_functions _functions
.into_iter() .into_iter()
.map(|fn_def| { .map(|fn_def| {
let mut fn_def = shared_take_or_clone(fn_def); let mut fn_def = crate::func::native::shared_take_or_clone(fn_def);
// Optimize the function body // Optimize the function body
let body = mem::take(fn_def.body.deref_mut()); let body = mem::take(fn_def.body.deref_mut());

View File

@ -223,10 +223,12 @@ struct ParseSettings {
/// Is the construct being parsed located at global level? /// Is the construct being parsed located at global level?
is_global: bool, is_global: bool,
/// Is the construct being parsed located at function definition level? /// Is the construct being parsed located at function definition level?
#[cfg(not(feature = "no_function"))]
is_function_scope: bool, is_function_scope: bool,
/// Is the current position inside a loop? /// Is the current position inside a loop?
is_breakable: bool, is_breakable: bool,
/// Is anonymous function allowed? /// Is anonymous function allowed?
#[cfg(not(feature = "no_function"))]
allow_anonymous_fn: bool, allow_anonymous_fn: bool,
/// Is if-expression allowed? /// Is if-expression allowed?
allow_if_expr: bool, allow_if_expr: bool,
@ -1315,6 +1317,7 @@ fn parse_primary(
(None, None, state.get_identifier(s)).into(), (None, None, state.get_identifier(s)).into(),
), ),
// Access to `this` as a variable is OK within a function scope // Access to `this` as a variable is OK within a function scope
#[cfg(not(feature = "no_function"))]
_ if &*s == KEYWORD_THIS && settings.is_function_scope => Expr::Variable( _ if &*s == KEYWORD_THIS && settings.is_function_scope => Expr::Variable(
None, None,
settings.pos, settings.pos,
@ -3232,8 +3235,10 @@ impl Engine {
allow_if_expr: false, allow_if_expr: false,
allow_switch_expr: false, allow_switch_expr: false,
allow_stmt_expr: false, allow_stmt_expr: false,
#[cfg(not(feature = "no_function"))]
allow_anonymous_fn: false, allow_anonymous_fn: false,
is_global: true, is_global: true,
#[cfg(not(feature = "no_function"))]
is_function_scope: false, is_function_scope: false,
is_breakable: false, is_breakable: false,
level: 0, level: 0,
@ -3281,8 +3286,10 @@ impl Engine {
allow_if_expr: true, allow_if_expr: true,
allow_switch_expr: true, allow_switch_expr: true,
allow_stmt_expr: true, allow_stmt_expr: true,
#[cfg(not(feature = "no_function"))]
allow_anonymous_fn: true, allow_anonymous_fn: true,
is_global: true, is_global: true,
#[cfg(not(feature = "no_function"))]
is_function_scope: false, is_function_scope: false,
is_breakable: false, is_breakable: false,
level: 0, level: 0,

View File

@ -129,7 +129,7 @@ fn test_fn_ptr_call() -> Result<(), Box<EvalAltResult>> {
} }
#[test] #[test]
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_closure"))]
fn test_fn_ptr_make_closure() -> Result<(), Box<EvalAltResult>> { fn test_fn_ptr_make_closure() -> Result<(), Box<EvalAltResult>> {
let f = { let f = {
let engine = Engine::new(); let engine = Engine::new();