Remove no_module gates to prepare for plugins.

This commit is contained in:
Stephen Chung
2020-06-01 10:58:14 +08:00
parent c6e5f672c9
commit cec6748ac6
8 changed files with 99 additions and 142 deletions

View File

@@ -4,18 +4,12 @@ use crate::any::{Dynamic, Union};
use crate::calc_fn_hash;
use crate::engine::{make_getter, make_setter, Engine, FunctionsLib};
use crate::error::{LexError, ParseError, ParseErrorType};
use crate::module::ModuleRef;
use crate::optimize::{optimize_into_ast, OptimizationLevel};
use crate::scope::{EntryType as ScopeEntryType, Scope};
use crate::token::{Position, Token, TokenIterator};
use crate::utils::{StaticVec, StraightHasherBuilder};
#[cfg(not(feature = "no_module"))]
use crate::module::ModuleRef;
#[cfg(feature = "no_module")]
#[derive(Debug, Eq, PartialEq, Clone, Hash, Copy, Default)]
pub struct ModuleRef;
use crate::stdlib::{
borrow::Cow,
boxed::Box,
@@ -644,7 +638,6 @@ impl Expr {
Self::Variable(_) => match token {
Token::LeftBracket | Token::LeftParen => true,
#[cfg(not(feature = "no_module"))]
Token::DoubleColon => true,
_ => false,
},
@@ -761,27 +754,21 @@ fn parse_call_expr<'a>(
Token::RightParen => {
eat_token(input, Token::RightParen);
#[cfg(not(feature = "no_module"))]
let hash_fn_def = {
if let Some(modules) = modules.as_mut() {
modules.set_index(state.find_module(&modules.get(0).0));
let hash_fn_def = if let Some(modules) = modules.as_mut() {
modules.set_index(state.find_module(&modules.get(0).0));
// Rust functions are indexed in two steps:
// 1) Calculate a hash in a similar manner to script-defined functions,
// i.e. qualifiers + function name + number of arguments.
// 2) Calculate a second hash with no qualifiers, empty function name,
// zero number of arguments, and the actual list of argument `TypeId`'s.
// 3) The final hash is the XOR of the two hashes.
let qualifiers = modules.iter().map(|(m, _)| m.as_str());
calc_fn_hash(qualifiers, &id, 0, empty())
} else {
// Qualifiers (none) + function name + no parameters.
calc_fn_hash(empty(), &id, 0, empty())
}
// Rust functions are indexed in two steps:
// 1) Calculate a hash in a similar manner to script-defined functions,
// i.e. qualifiers + function name + number of arguments.
// 2) Calculate a second hash with no qualifiers, empty function name,
// zero number of arguments, and the actual list of argument `TypeId`'s.
// 3) The final hash is the XOR of the two hashes.
let qualifiers = modules.iter().map(|(m, _)| m.as_str());
calc_fn_hash(qualifiers, &id, 0, empty())
} else {
// Qualifiers (none) + function name + no parameters.
calc_fn_hash(empty(), &id, 0, empty())
};
// Qualifiers (none) + function name + no parameters.
#[cfg(feature = "no_module")]
let hash_fn_def = calc_fn_hash(empty(), &id, 0, empty());
return Ok(Expr::FnCall(Box::new((
(id.into(), false, begin),
@@ -803,27 +790,21 @@ fn parse_call_expr<'a>(
(Token::RightParen, _) => {
eat_token(input, Token::RightParen);
#[cfg(not(feature = "no_module"))]
let hash_fn_def = {
if let Some(modules) = modules.as_mut() {
modules.set_index(state.find_module(&modules.get(0).0));
let hash_fn_def = if let Some(modules) = modules.as_mut() {
modules.set_index(state.find_module(&modules.get(0).0));
// Rust functions are indexed in two steps:
// 1) Calculate a hash in a similar manner to script-defined functions,
// i.e. qualifiers + function name + number of arguments.
// 2) Calculate a second hash with no qualifiers, empty function name,
// zero number of arguments, and the actual list of argument `TypeId`'s.
// 3) The final hash is the XOR of the two hashes.
let qualifiers = modules.iter().map(|(m, _)| m.as_str());
calc_fn_hash(qualifiers, &id, args.len(), empty())
} else {
// Qualifiers (none) + function name + number of arguments.
calc_fn_hash(empty(), &id, args.len(), empty())
}
// Rust functions are indexed in two steps:
// 1) Calculate a hash in a similar manner to script-defined functions,
// i.e. qualifiers + function name + number of arguments.
// 2) Calculate a second hash with no qualifiers, empty function name,
// zero number of arguments, and the actual list of argument `TypeId`'s.
// 3) The final hash is the XOR of the two hashes.
let qualifiers = modules.iter().map(|(m, _)| m.as_str());
calc_fn_hash(qualifiers, &id, args.len(), empty())
} else {
// Qualifiers (none) + function name + number of arguments.
calc_fn_hash(empty(), &id, args.len(), empty())
};
// Qualifiers (none) + function name + number of arguments.
#[cfg(feature = "no_module")]
let hash_fn_def = calc_fn_hash(empty(), &id, args.len(), empty());
return Ok(Expr::FnCall(Box::new((
(id.into(), false, begin),
@@ -1265,7 +1246,6 @@ fn parse_primary<'a>(
}
(Expr::Property(_), _) => unreachable!(),
// module access
#[cfg(not(feature = "no_module"))]
(Expr::Variable(x), Token::DoubleColon) => match input.next().unwrap() {
(Token::Identifier(id2), pos2) => {
let ((name, pos), mut modules, _, index) = *x;
@@ -1293,7 +1273,6 @@ fn parse_primary<'a>(
match &mut root_expr {
// Cache the hash key for module-qualified variables
#[cfg(not(feature = "no_module"))]
Expr::Variable(x) if x.1.is_some() => {
let ((name, _), modules, hash, _) = x.as_mut();
let modules = modules.as_mut().unwrap();
@@ -1520,9 +1499,6 @@ fn make_dot_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result<Expr, ParseEr
}
// lhs.module::id - syntax error
(_, Expr::Variable(x)) if x.1.is_some() => {
#[cfg(feature = "no_module")]
unreachable!();
#[cfg(not(feature = "no_module"))]
return Err(PERR::PropertyExpected.into_err(x.1.unwrap().get(0).1));
}
// lhs.prop
@@ -2113,6 +2089,7 @@ fn parse_import<'a>(
}
/// Parse an export statement.
#[cfg(not(feature = "no_module"))]
fn parse_export<'a>(
input: &mut Peekable<TokenIterator<'a>>,
state: &mut ParseState,
@@ -2294,7 +2271,9 @@ fn parse_stmt<'a>(
Token::LeftBrace => parse_block(input, state, breakable, level + 1, allow_stmt_expr),
// fn ...
#[cfg(not(feature = "no_function"))]
Token::Fn if !is_global => Err(PERR::WrongFnDefinition.into_err(*pos)),
#[cfg(not(feature = "no_function"))]
Token::Fn => unreachable!(),
Token::If => parse_if(input, state, breakable, level + 1, allow_stmt_expr),
@@ -2343,8 +2322,6 @@ fn parse_stmt<'a>(
Token::Let => parse_let(input, state, Normal, level + 1, allow_stmt_expr),
Token::Const => parse_let(input, state, Constant, level + 1, allow_stmt_expr),
#[cfg(not(feature = "no_module"))]
Token::Import => parse_import(input, state, level + 1, allow_stmt_expr),
#[cfg(not(feature = "no_module"))]
@@ -2358,6 +2335,7 @@ fn parse_stmt<'a>(
}
/// Parse a function definition.
#[cfg(not(feature = "no_function"))]
fn parse_fn<'a>(
input: &mut Peekable<TokenIterator<'a>>,
state: &mut ParseState,
@@ -2499,6 +2477,7 @@ fn parse_global_level<'a>(
};
match input.peek().unwrap() {
#[cfg(not(feature = "no_function"))]
(Token::Fn, _) => {
let mut state = ParseState::new(max_expr_depth.1);
let func = parse_fn(input, &mut state, access, 0, true)?;