From 1824dced69a9b15e6f94bd14d00b9ff157be1c0d Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 18 May 2020 19:32:22 +0800 Subject: [PATCH 01/53] Limit expression/statement nesting depths. --- README.md | 82 +++++++-- RELEASES.md | 9 + src/api.rs | 37 +++- src/engine.rs | 37 +++- src/error.rs | 7 +- src/parser.rs | 479 +++++++++++++++++++++++++++++++------------------ tests/stack.rs | 60 ++++++- 7 files changed, 513 insertions(+), 198 deletions(-) diff --git a/README.md b/README.md index 831f3765..6edb1ca7 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Rhai's current features set: one single source file, all with names starting with `"unsafe_"`). * Re-entrant scripting [`Engine`] can be made `Send + Sync` (via the [`sync`] feature). * Sand-boxed - the scripting [`Engine`], if declared immutable, cannot mutate the containing environment without explicit permission. -* Rugged (protection against [stack-overflow](#maximum-stack-depth) and [runaway scripts](#maximum-number-of-operations) etc.). +* Rugged (protection against [stack-overflow](#maximum-call-stack-depth) and [runaway scripts](#maximum-number-of-operations) etc.). * Track script evaluation [progress](#tracking-progress) and manually terminate a script run. * [`no-std`](#optional-features) support. * [Function overloading](#function-overloading). @@ -1066,12 +1066,13 @@ fn main() -> Result<(), Box> Engine configuration options --------------------------- -| Method | Description | -| ------------------------ | ---------------------------------------------------------------------------------------- | -| `set_optimization_level` | Set the amount of script _optimizations_ performed. See [`script optimization`]. | -| `set_max_call_levels` | Set the maximum number of function call levels (default 50) to avoid infinite recursion. | - -[`script optimization`]: #script-optimization +| Method | Description | +| ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| `set_optimization_level` | Set the amount of script _optimizations_ performed. See [script optimization]. | +| `set_max_expr_depths` | Set the maximum nesting levels of an expression/statement. See [maximum statement depth](#maximum-statement-depth). | +| `set_max_call_levels` | Set the maximum number of function call levels (default 50) to avoid infinite recursion. See [maximum call stack depth](#maximum-call-stack-depth). | +| `set_max_operations` | Set the maximum number of _operations_ that a script is allowed to consume. See [maximum number of operations](#maximum-number-of-operations). | +| `set_max_modules` | Set the maximum number of [modules] that a script is allowed to load. See [maximum number of modules](#maximum-number-of-modules). | ------- @@ -2267,9 +2268,12 @@ so that it does not consume more resources that it is allowed to. The most important resources to watch out for are: * **Memory**: A malignant script may continuously grow an [array] or [object map] until all memory is consumed. + It may also create a large [array] or [objecct map] literal that exhausts all memory during parsing. * **CPU**: A malignant script may run an infinite tight loop that consumes all CPU cycles. * **Time**: A malignant script may run indefinitely, thereby blocking the calling system which is waiting for a result. * **Stack**: A malignant script may attempt an infinite recursive call that exhausts the call stack. + Alternatively, it may create a degenerated deep expression with so many levels that the parser exhausts the call stack + when parsing the expression; or even deeply-nested statement blocks, if nested deep enough. * **Overflows**: A malignant script may deliberately cause numeric over-flows and/or under-flows, divide by zero, and/or create bad floating-point representations, in order to crash the system. * **Files**: A malignant script may continuously [`import`] an external module within an infinite loop, @@ -2341,10 +2345,15 @@ engine.set_max_modules(5); // allow loading only up to 5 module engine.set_max_modules(0); // allow unlimited modules ``` -### Maximum stack depth +### Maximum call stack depth -Rhai by default limits function calls to a maximum depth of 256 levels (28 levels in debug build). +Rhai by default limits function calls to a maximum depth of 128 levels (8 levels in debug build). This limit may be changed via the `Engine::set_max_call_levels` method. + +When setting this limit, care must be also taken to the evaluation depth of each _statement_ +within the function. It is entirely possible for a malignant script to embed an recursive call deep +inside a nested expression or statement block (see [maximum statement depth](#maximum-statement-depth)). + The limit can be disabled via the [`unchecked`] feature for higher performance (but higher risks as well). @@ -2358,12 +2367,57 @@ engine.set_max_call_levels(0); // allow no function calls at all (m A script exceeding the maximum call stack depth will terminate with an error result. +### Maximum statement depth + +Rhai by default limits statements and expressions nesting to a maximum depth of 128 +(which should be plenty) when they are at _global_ level, but only a depth of 32 +when they are within function bodies. For debug builds, these limits are set further +downwards to 32 and 16 respectively. + +That is because it is possible to overflow the [`Engine`]'s stack when it tries to +recursively parse an extremely deeply-nested code stream. + +```rust +// The following, if long enough, can easily cause stack overflow during parsing. +let a = (1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(...)+1))))))))))); +``` + +This limit may be changed via the `Engine::set_max_expr_depths` method. There are two limits to set, +one for the maximum depth at global level, and the other for function bodies. + +```rust +let mut engine = Engine::new(); + +engine.set_max_expr_depths(50, 5); // allow nesting up to 50 layers of expressions/statements + // at global level, but only 5 inside functions +``` + +Beware that there may be multiple layers for a simple language construct, even though it may correspond +to only one AST node. That is because the Rhai _parser_ internally runs a recursive chain of function calls +and it is important that a malignant script does not panic the parser in the first place. + +Functions are placed under stricter limits because of the multiplicative effect of recursion. +A script can effectively call itself while deep inside an expression chain within the function body, +thereby overflowing the stack even when the level of recursion is within limit. + +Make sure that `C x ( 5 + F ) + S` layered calls do not cause a stack overflow, where: + +* `C` = maximum call stack depth, +* `F` = maximum statement depth for functions, +* `S` = maximum statement depth at global level. + +A script exceeding the maximum nesting depths will terminate with a parsing error. +The malignant `AST` will not be able to get past parsing in the first place. + +The limits can be disabled via the [`unchecked`] feature for higher performance +(but higher risks as well). + ### Checked arithmetic -All arithmetic calculations in Rhai are _checked_, meaning that the script terminates with an error whenever -it detects a numeric over-flow/under-flow condition or an invalid floating-point operation, instead of -crashing the entire system. This checking can be turned off via the [`unchecked`] feature for higher performance -(but higher risks as well). +By default, all arithmetic calculations in Rhai are _checked_, meaning that the script terminates +with an error whenever it detects a numeric over-flow/under-flow condition or an invalid +floating-point operation, instead of crashing the entire system. This checking can be turned off +via the [`unchecked`] feature for higher performance (but higher risks as well). ### Blocking access to external data @@ -2383,6 +2437,8 @@ let engine = engine; // shadow the variable so that 'engi Script optimization =================== +[script optimization]: #script-optimization + Rhai includes an _optimizer_ that tries to optimize a script after parsing. This can reduce resource utilization and increase execution speed. Script optimization can be turned off via the [`no_optimize`] feature. diff --git a/RELEASES.md b/RELEASES.md index cee016d7..825105a8 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,6 +1,15 @@ Rhai Release Notes ================== +Version 0.15.0 +============== + +New features +------------ + +* Set limits on maximum level of nesting expressions and statements to avoid panics during parsing. + + Version 0.14.1 ============== diff --git a/src/api.rs b/src/api.rs index 26a825ab..f0e249e9 100644 --- a/src/api.rs +++ b/src/api.rs @@ -444,7 +444,14 @@ impl Engine { optimization_level: OptimizationLevel, ) -> Result> { let stream = lex(scripts); - parse(&mut stream.peekable(), self, scope, optimization_level) + + parse( + &mut stream.peekable(), + self, + scope, + optimization_level, + (self.max_expr_depth, self.max_function_expr_depth), + ) } /// Read the contents of a file into a string. @@ -571,6 +578,7 @@ impl Engine { self, &scope, OptimizationLevel::None, + self.max_expr_depth, )?; // Handle null - map to () @@ -654,7 +662,13 @@ impl Engine { { let mut peekable = stream.peekable(); - parse_global_expr(&mut peekable, self, scope, self.optimization_level) + parse_global_expr( + &mut peekable, + self, + scope, + self.optimization_level, + self.max_expr_depth, + ) } } @@ -805,8 +819,14 @@ impl Engine { ) -> Result> { let scripts = [script]; let stream = lex(&scripts); - // Since the AST will be thrown away afterwards, don't bother to optimize it - let ast = parse_global_expr(&mut stream.peekable(), self, scope, OptimizationLevel::None)?; + + let ast = parse_global_expr( + &mut stream.peekable(), + self, + scope, + self.optimization_level, + self.max_expr_depth, + )?; self.eval_ast_with_scope(scope, &ast) } @@ -931,8 +951,13 @@ impl Engine { let scripts = [script]; let stream = lex(&scripts); - // Since the AST will be thrown away afterwards, don't bother to optimize it - let ast = parse(&mut stream.peekable(), self, scope, OptimizationLevel::None)?; + let ast = parse( + &mut stream.peekable(), + self, + scope, + self.optimization_level, + (self.max_expr_depth, self.max_function_expr_depth), + )?; self.consume_ast_with_scope(scope, &ast) } diff --git a/src/engine.rs b/src/engine.rs index 3febbc6f..6bd2bafa 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -49,14 +49,30 @@ pub type Map = HashMap; #[cfg(not(feature = "unchecked"))] #[cfg(debug_assertions)] -pub const MAX_CALL_STACK_DEPTH: usize = 28; +pub const MAX_CALL_STACK_DEPTH: usize = 8; +#[cfg(not(feature = "unchecked"))] +#[cfg(debug_assertions)] +pub const MAX_EXPR_DEPTH: usize = 32; +#[cfg(not(feature = "unchecked"))] +#[cfg(debug_assertions)] +pub const MAX_FUNCTION_EXPR_DEPTH: usize = 16; #[cfg(not(feature = "unchecked"))] #[cfg(not(debug_assertions))] -pub const MAX_CALL_STACK_DEPTH: usize = 256; +pub const MAX_CALL_STACK_DEPTH: usize = 128; +#[cfg(not(feature = "unchecked"))] +#[cfg(not(debug_assertions))] +pub const MAX_EXPR_DEPTH: usize = 128; +#[cfg(not(feature = "unchecked"))] +#[cfg(not(debug_assertions))] +pub const MAX_FUNCTION_EXPR_DEPTH: usize = 32; #[cfg(feature = "unchecked")] pub const MAX_CALL_STACK_DEPTH: usize = usize::MAX; +#[cfg(feature = "unchecked")] +pub const MAX_EXPR_DEPTH: usize = usize::MAX; +#[cfg(feature = "unchecked")] +pub const MAX_FUNCTION_EXPR_DEPTH: usize = usize::MAX; pub const KEYWORD_PRINT: &str = "print"; pub const KEYWORD_DEBUG: &str = "debug"; @@ -338,8 +354,12 @@ pub struct Engine { pub(crate) optimization_level: OptimizationLevel, /// Maximum levels of call-stack to prevent infinite recursion. /// - /// Defaults to 28 for debug builds and 256 for non-debug builds. + /// Defaults to 8 for debug builds and 128 for non-debug builds. pub(crate) max_call_stack_depth: usize, + /// Maximum depth of statements/expressions at global level. + pub(crate) max_expr_depth: usize, + /// Maximum depth of statements/expressions in functions. + pub(crate) max_function_expr_depth: usize, /// Maximum number of operations allowed to run. pub(crate) max_operations: Option, /// Maximum number of modules allowed to load. @@ -382,6 +402,8 @@ impl Default for Engine { optimization_level: OptimizationLevel::Full, max_call_stack_depth: MAX_CALL_STACK_DEPTH, + max_expr_depth: MAX_EXPR_DEPTH, + max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, max_operations: None, max_modules: None, }; @@ -523,6 +545,8 @@ impl Engine { optimization_level: OptimizationLevel::Full, max_call_stack_depth: MAX_CALL_STACK_DEPTH, + max_expr_depth: MAX_EXPR_DEPTH, + max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, max_operations: None, max_modules: None, } @@ -574,6 +598,13 @@ impl Engine { self.max_modules = NonZeroU64::new(modules); } + /// Set the depth limits for expressions/statements. + #[cfg(not(feature = "unchecked"))] + pub fn set_max_expr_depths(&mut self, max_expr_depth: usize, max_function_expr_depth: usize) { + self.max_expr_depth = max_expr_depth; + self.max_function_expr_depth = max_function_expr_depth; + } + /// Set the module resolution service used by the `Engine`. /// /// Not available under the `no_module` feature. diff --git a/src/error.rs b/src/error.rs index 11b7c9e2..8e180c25 100644 --- a/src/error.rs +++ b/src/error.rs @@ -110,6 +110,10 @@ pub enum ParseErrorType { AssignmentToCopy, /// Assignment to an a constant variable. AssignmentToConstant(String), + /// Expression exceeding the maximum levels of complexity. + /// + /// Never appears under the `unchecked` feature. + ExprTooDeep, /// Break statement not inside a loop. LoopBreak, } @@ -158,7 +162,8 @@ impl ParseError { ParseErrorType::DuplicatedExport(_) => "Duplicated variable/function in export statement", ParseErrorType::WrongExport => "Export statement can only appear at global level", ParseErrorType::AssignmentToCopy => "Only a copy of the value is change with this assignment", - ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant value.", + ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant value", + ParseErrorType::ExprTooDeep => "Expression exceeds maximum complexity", ParseErrorType::LoopBreak => "Break statement should only be used inside a loop" } } diff --git a/src/parser.rs b/src/parser.rs index 29c955e2..39fa6678 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -218,21 +218,27 @@ pub enum ReturnType { Exception, } -/// A type that encapsulates a local stack with variable names to simulate an actual runtime scope. #[derive(Debug, Clone, Default)] -struct Stack(Vec<(String, ScopeEntryType)>); +struct ParseState { + /// Encapsulates a local stack with variable names to simulate an actual runtime scope. + stack: Vec<(String, ScopeEntryType)>, + max_expr_depth: usize, +} -impl Stack { - /// Create a new `Stack`. - pub fn new() -> Self { - Default::default() +impl ParseState { + /// Create a new `ParseState`. + pub fn new(max_expr_depth: usize) -> Self { + Self { + max_expr_depth, + ..Default::default() + } } - /// Find a variable by name in the `Stack`, searching in reverse. + /// Find a variable by name in the `ParseState`, searching in reverse. /// The return value is the offset to be deducted from `Stack::len`, - /// i.e. the top element of the `Stack` is offset 1. - /// Return zero when the variable name is not found in the `Stack`. + /// i.e. the top element of the `ParseState` is offset 1. + /// Return zero when the variable name is not found in the `ParseState`. pub fn find(&self, name: &str) -> Option { - self.0 + self.stack .iter() .rev() .enumerate() @@ -242,12 +248,12 @@ impl Stack { }) .and_then(|(i, _)| NonZeroUsize::new(i + 1)) } - /// Find a module by name in the `Stack`, searching in reverse. + /// Find a module by name in the `ParseState`, searching in reverse. /// The return value is the offset to be deducted from `Stack::len`, - /// i.e. the top element of the `Stack` is offset 1. - /// Return zero when the variable name is not found in the `Stack`. + /// i.e. the top element of the `ParseState` is offset 1. + /// Return zero when the variable name is not found in the `ParseState`. pub fn find_module(&self, name: &str) -> Option { - self.0 + self.stack .iter() .rev() .enumerate() @@ -259,17 +265,17 @@ impl Stack { } } -impl Deref for Stack { +impl Deref for ParseState { type Target = Vec<(String, ScopeEntryType)>; fn deref(&self) -> &Self::Target { - &self.0 + &self.stack } } -impl DerefMut for Stack { +impl DerefMut for ParseState { fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 + &mut self.stack } } @@ -691,15 +697,20 @@ fn match_token(input: &mut Peekable, token: Token) -> Result( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, pos: Position, + level: usize, allow_stmt_expr: bool, ) -> Result> { + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + if match_token(input, Token::RightParen)? { return Ok(Expr::Unit(pos)); } - let expr = parse_expr(input, stack, allow_stmt_expr)?; + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; match input.next().unwrap() { // ( xxx ) @@ -718,18 +729,25 @@ fn parse_paren_expr<'a>( /// Parse a function call. fn parse_call_expr<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, id: String, #[cfg(not(feature = "no_module"))] mut modules: Option>, #[cfg(feature = "no_module")] modules: Option, begin: Position, + level: usize, allow_stmt_expr: bool, ) -> Result> { + let (token, pos) = input.peek().unwrap(); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(*pos)); + } + let mut args = StaticVec::new(); - match input.peek().unwrap() { + match token { // id - (Token::EOF, pos) => { + Token::EOF => { return Err(PERR::MissingToken( Token::RightParen.into(), format!("to close the arguments list of this function call '{}'", id), @@ -737,15 +755,15 @@ fn parse_call_expr<'a>( .into_err(*pos)) } // id - (Token::LexError(err), pos) => return Err(PERR::BadInput(err.to_string()).into_err(*pos)), + Token::LexError(err) => return Err(PERR::BadInput(err.to_string()).into_err(*pos)), // id() - (Token::RightParen, _) => { + 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(stack.find_module(&modules.get(0).0)); + 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, @@ -775,7 +793,7 @@ fn parse_call_expr<'a>( } loop { - args.push(parse_expr(input, stack, allow_stmt_expr)?); + args.push(parse_expr(input, state, level + 1, allow_stmt_expr)?); match input.peek().unwrap() { // id(...args) @@ -786,7 +804,7 @@ fn parse_call_expr<'a>( #[cfg(not(feature = "no_module"))] let hash_fn_def = { if let Some(modules) = modules.as_mut() { - modules.set_index(stack.find_module(&modules.get(0).0)); + 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, @@ -843,12 +861,17 @@ fn parse_call_expr<'a>( /// Indexing binds to the right, so this call parses all possible levels of indexing following in the input. fn parse_index_chain<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, lhs: Expr, pos: Position, + level: usize, allow_stmt_expr: bool, ) -> Result> { - let idx_expr = parse_expr(input, stack, allow_stmt_expr)?; + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + + let idx_expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; // Check type of indexing - must be integer or string match &idx_expr { @@ -1001,7 +1024,14 @@ fn parse_index_chain<'a>( (Token::LeftBracket, _) => { let idx_pos = eat_token(input, Token::LeftBracket); // Recursively parse the indexing chain, right-binding each - let idx = parse_index_chain(input, stack, idx_expr, idx_pos, allow_stmt_expr)?; + let idx = parse_index_chain( + input, + state, + idx_expr, + idx_pos, + level + 1, + allow_stmt_expr, + )?; // Indexing binds to right Ok(Expr::Index(Box::new((lhs, idx, pos)))) } @@ -1021,15 +1051,20 @@ fn parse_index_chain<'a>( /// Parse an array literal. fn parse_array_literal<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, pos: Position, + level: usize, allow_stmt_expr: bool, ) -> Result> { + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + let mut arr = StaticVec::new(); if !match_token(input, Token::RightBracket)? { while !input.peek().unwrap().0.is_eof() { - arr.push(parse_expr(input, stack, allow_stmt_expr)?); + arr.push(parse_expr(input, state, level + 1, allow_stmt_expr)?); match input.peek().unwrap() { (Token::Comma, _) => eat_token(input, Token::Comma), @@ -1064,10 +1099,15 @@ fn parse_array_literal<'a>( /// Parse a map literal. fn parse_map_literal<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, pos: Position, + level: usize, allow_stmt_expr: bool, ) -> Result> { + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + let mut map = StaticVec::new(); if !match_token(input, Token::RightBrace)? { @@ -1112,7 +1152,7 @@ fn parse_map_literal<'a>( } }; - let expr = parse_expr(input, stack, allow_stmt_expr)?; + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; map.push(((name, pos), expr)); @@ -1161,17 +1201,24 @@ fn parse_map_literal<'a>( /// Parse a primary expression. fn parse_primary<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { - let (token, pos) = match input.peek().unwrap() { + let (token, pos) = input.peek().unwrap(); + let pos = *pos; + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + + let (token, _) = match token { // { - block statement as expression - (Token::LeftBrace, pos) if allow_stmt_expr => { - let pos = *pos; - return parse_block(input, stack, false, allow_stmt_expr) + Token::LeftBrace if allow_stmt_expr => { + return parse_block(input, state, false, level + 1, allow_stmt_expr) .map(|block| Expr::Stmt(Box::new((block, pos)))); } - (Token::EOF, pos) => return Err(PERR::UnexpectedEOF.into_err(*pos)), + Token::EOF => return Err(PERR::UnexpectedEOF.into_err(pos)), _ => input.next().unwrap(), }; @@ -1182,14 +1229,14 @@ fn parse_primary<'a>( Token::CharConstant(c) => Expr::CharConstant(Box::new((c, pos))), Token::StringConst(s) => Expr::StringConstant(Box::new((s, pos))), Token::Identifier(s) => { - let index = stack.find(&s); + let index = state.find(&s); Expr::Variable(Box::new(((s, pos), None, 0, index))) } - Token::LeftParen => parse_paren_expr(input, stack, pos, allow_stmt_expr)?, + Token::LeftParen => parse_paren_expr(input, state, pos, level + 1, allow_stmt_expr)?, #[cfg(not(feature = "no_index"))] - Token::LeftBracket => parse_array_literal(input, stack, pos, allow_stmt_expr)?, + Token::LeftBracket => parse_array_literal(input, state, pos, level + 1, allow_stmt_expr)?, #[cfg(not(feature = "no_object"))] - Token::MapStart => parse_map_literal(input, stack, pos, allow_stmt_expr)?, + Token::MapStart => parse_map_literal(input, state, pos, level + 1, allow_stmt_expr)?, Token::True => Expr::True(pos), Token::False => Expr::False(pos), Token::LexError(err) => return Err(PERR::BadInput(err.to_string()).into_err(pos)), @@ -1212,7 +1259,7 @@ fn parse_primary<'a>( // Function call (Expr::Variable(x), Token::LeftParen) => { let ((name, pos), modules, _, _) = *x; - parse_call_expr(input, stack, name, modules, pos, allow_stmt_expr)? + parse_call_expr(input, state, name, modules, pos, level + 1, allow_stmt_expr)? } (Expr::Property(_), _) => unreachable!(), // module access @@ -1235,7 +1282,7 @@ fn parse_primary<'a>( // Indexing #[cfg(not(feature = "no_index"))] (expr, Token::LeftBracket) => { - parse_index_chain(input, stack, expr, token_pos, allow_stmt_expr)? + parse_index_chain(input, state, expr, token_pos, level + 1, allow_stmt_expr)? } // Unknown postfix operator (expr, token) => panic!("unknown postfix operator {:?} for {:?}", token, expr), @@ -1251,7 +1298,7 @@ fn parse_primary<'a>( // Qualifiers + variable name *hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, empty()); - modules.set_index(stack.find_module(&modules.get(0).0)); + modules.set_index(state.find_module(&modules.get(0).0)); } _ => (), } @@ -1262,23 +1309,28 @@ fn parse_primary<'a>( /// Parse a potential unary operator. fn parse_unary<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { - match input.peek().unwrap() { + let (token, pos) = input.peek().unwrap(); + let pos = *pos; + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + + match token { // If statement is allowed to act as expressions - (Token::If, pos) => { - let pos = *pos; - Ok(Expr::Stmt(Box::new(( - parse_if(input, stack, false, allow_stmt_expr)?, - pos, - )))) - } + Token::If => Ok(Expr::Stmt(Box::new(( + parse_if(input, state, false, level + 1, allow_stmt_expr)?, + pos, + )))), // -expr - (Token::UnaryMinus, _) => { + Token::UnaryMinus => { let pos = eat_token(input, Token::UnaryMinus); - match parse_unary(input, stack, allow_stmt_expr)? { + match parse_unary(input, state, level + 1, allow_stmt_expr)? { // Negative integer Expr::IntegerConstant(x) => { let (num, pos) = *x; @@ -1325,15 +1377,15 @@ fn parse_unary<'a>( } } // +expr - (Token::UnaryPlus, _) => { + Token::UnaryPlus => { eat_token(input, Token::UnaryPlus); - parse_unary(input, stack, allow_stmt_expr) + parse_unary(input, state, level + 1, allow_stmt_expr) } // !expr - (Token::Bang, _) => { + Token::Bang => { let pos = eat_token(input, Token::Bang); let mut args = StaticVec::new(); - args.push(parse_primary(input, stack, allow_stmt_expr)?); + args.push(parse_primary(input, state, level + 1, allow_stmt_expr)?); let op = "!"; let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); @@ -1347,14 +1399,14 @@ fn parse_unary<'a>( )))) } // - (Token::EOF, pos) => Err(PERR::UnexpectedEOF.into_err(*pos)), + Token::EOF => Err(PERR::UnexpectedEOF.into_err(pos)), // All other tokens - _ => parse_primary(input, stack, allow_stmt_expr), + _ => parse_primary(input, state, level + 1, allow_stmt_expr), } } fn make_assignment_stmt<'a>( - stack: &mut Stack, + state: &mut ParseState, lhs: Expr, rhs: Expr, pos: Position, @@ -1363,7 +1415,7 @@ fn make_assignment_stmt<'a>( Expr::Variable(x) if x.3.is_none() => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), Expr::Variable(x) => { let ((name, name_pos), _, _, index) = x.as_ref(); - match stack[(stack.len() - index.unwrap().get())].1 { + match state.stack[(state.len() - index.unwrap().get())].1 { ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), // Constant values cannot be assigned to ScopeEntryType::Constant => { @@ -1376,7 +1428,7 @@ fn make_assignment_stmt<'a>( Expr::Variable(x) if x.3.is_none() => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), Expr::Variable(x) => { let ((name, name_pos), _, _, index) = x.as_ref(); - match stack[(stack.len() - index.unwrap().get())].1 { + match state.stack[(state.len() - index.unwrap().get())].1 { ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), // Constant values cannot be assigned to ScopeEntryType::Constant => { @@ -1397,44 +1449,53 @@ fn make_assignment_stmt<'a>( /// Parse an operator-assignment expression. fn parse_op_assignment_stmt<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, lhs: Expr, + level: usize, allow_stmt_expr: bool, ) -> Result> { - let (op, pos) = match *input.peek().unwrap() { - (Token::Equals, _) => { + let (token, pos) = input.peek().unwrap(); + let pos = *pos; + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + + let op = match token { + Token::Equals => { let pos = eat_token(input, Token::Equals); - let rhs = parse_expr(input, stack, allow_stmt_expr)?; - return make_assignment_stmt(stack, lhs, rhs, pos); + let rhs = parse_expr(input, state, level + 1, allow_stmt_expr)?; + return make_assignment_stmt(state, lhs, rhs, pos); } - (Token::PlusAssign, pos) => ("+", pos), - (Token::MinusAssign, pos) => ("-", pos), - (Token::MultiplyAssign, pos) => ("*", pos), - (Token::DivideAssign, pos) => ("/", pos), - (Token::LeftShiftAssign, pos) => ("<<", pos), - (Token::RightShiftAssign, pos) => (">>", pos), - (Token::ModuloAssign, pos) => ("%", pos), - (Token::PowerOfAssign, pos) => ("~", pos), - (Token::AndAssign, pos) => ("&", pos), - (Token::OrAssign, pos) => ("|", pos), - (Token::XOrAssign, pos) => ("^", pos), - (_, _) => return Ok(lhs), + Token::PlusAssign => Token::Plus.syntax(), + Token::MinusAssign => Token::Minus.syntax(), + Token::MultiplyAssign => Token::Multiply.syntax(), + Token::DivideAssign => Token::Divide.syntax(), + Token::LeftShiftAssign => Token::LeftShift.syntax(), + Token::RightShiftAssign => Token::RightShift.syntax(), + Token::ModuloAssign => Token::Modulo.syntax(), + Token::PowerOfAssign => Token::PowerOf.syntax(), + Token::AndAssign => Token::Ampersand.syntax(), + Token::OrAssign => Token::Pipe.syntax(), + Token::XOrAssign => Token::XOr.syntax(), + + _ => return Ok(lhs), }; input.next(); let lhs_copy = lhs.clone(); - let rhs = parse_expr(input, stack, allow_stmt_expr)?; + let rhs = parse_expr(input, state, level + 1, allow_stmt_expr)?; // lhs op= rhs -> lhs = op(lhs, rhs) let mut args = StaticVec::new(); args.push(lhs_copy); args.push(rhs); - let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(args.len())); - let rhs_expr = Expr::FnCall(Box::new(((op.into(), pos), None, hash, args, None))); + let hash = calc_fn_hash(empty(), &op, repeat(EMPTY_TYPE_ID()).take(args.len())); + let rhs_expr = Expr::FnCall(Box::new(((op, pos), None, hash, args, None))); - make_assignment_stmt(stack, lhs, rhs_expr, pos) + make_assignment_stmt(state, lhs, rhs_expr, pos) } /// Make a dot expression. @@ -1673,53 +1734,59 @@ fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, parent_precedence: u8, lhs: Expr, + mut level: usize, allow_stmt_expr: bool, ) -> Result> { - let mut current_lhs = lhs; + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(lhs.position())); + } + + let mut root = lhs; loop { - let (current_precedence, bind_right) = input.peek().map_or_else( - || (0, false), - |(current_op, _)| (current_op.precedence(), current_op.is_bind_right()), - ); + let (current_op, _) = input.peek().unwrap(); + let precedence = current_op.precedence(); + let bind_right = current_op.is_bind_right(); // Bind left to the parent lhs expression if precedence is higher // If same precedence, then check if the operator binds right - if current_precedence < parent_precedence - || (current_precedence == parent_precedence && !bind_right) - { - return Ok(current_lhs); + if precedence < parent_precedence || (precedence == parent_precedence && !bind_right) { + return Ok(root); } let (op_token, pos) = input.next().unwrap(); - let rhs = parse_unary(input, stack, allow_stmt_expr)?; + let rhs = parse_unary(input, state, level, allow_stmt_expr)?; let next_precedence = input.peek().unwrap().0.precedence(); // Bind to right if the next operator has higher precedence // If same precedence, then check if the operator binds right - let rhs = if (current_precedence == next_precedence && bind_right) - || current_precedence < next_precedence - { - parse_binary_op(input, stack, current_precedence, rhs, allow_stmt_expr)? + let rhs = if (precedence == next_precedence && bind_right) || precedence < next_precedence { + parse_binary_op(input, state, precedence, rhs, level, allow_stmt_expr)? } else { // Otherwise bind to left (even if next operator has the same precedence) rhs }; + level += 1; + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + let cmp_def = Some(false.into()); let op = op_token.syntax(); let hash = calc_fn_hash(empty(), &op, repeat(EMPTY_TYPE_ID()).take(2)); let mut args = StaticVec::new(); - args.push(current_lhs); + args.push(root); args.push(rhs); - current_lhs = match op_token { + root = match op_token { Token::Plus => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), Token::Minus => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), Token::Multiply => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), @@ -1789,11 +1856,18 @@ fn parse_binary_op<'a>( /// Parse an expression. fn parse_expr<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { - let lhs = parse_unary(input, stack, allow_stmt_expr)?; - parse_binary_op(input, stack, 1, lhs, allow_stmt_expr) + let (_, pos) = input.peek().unwrap(); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(*pos)); + } + + let lhs = parse_unary(input, state, level + 1, allow_stmt_expr)?; + parse_binary_op(input, state, 1, lhs, level + 1, allow_stmt_expr) } /// Make sure that the expression is not a statement expression (i.e. wrapped in `{}`). @@ -1843,27 +1917,32 @@ fn ensure_not_assignment<'a>( /// Parse an if statement. fn parse_if<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, breakable: bool, + level: usize, allow_stmt_expr: bool, ) -> Result> { // if ... - eat_token(input, Token::If); + let pos = eat_token(input, Token::If); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } // if guard { if_body } ensure_not_statement_expr(input, "a boolean")?; - let guard = parse_expr(input, stack, allow_stmt_expr)?; + let guard = parse_expr(input, state, level + 1, allow_stmt_expr)?; ensure_not_assignment(input)?; - let if_body = parse_block(input, stack, breakable, allow_stmt_expr)?; + let if_body = parse_block(input, state, breakable, level + 1, allow_stmt_expr)?; // if guard { if_body } else ... let else_body = if match_token(input, Token::Else).unwrap_or(false) { Some(if let (Token::If, _) = input.peek().unwrap() { // if guard { if_body } else if ... - parse_if(input, stack, breakable, allow_stmt_expr)? + parse_if(input, state, breakable, level + 1, allow_stmt_expr)? } else { // if guard { if_body } else { else-body } - parse_block(input, stack, breakable, allow_stmt_expr)? + parse_block(input, state, breakable, level + 1, allow_stmt_expr)? }) } else { None @@ -1875,17 +1954,22 @@ fn parse_if<'a>( /// Parse a while loop. fn parse_while<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { // while ... - eat_token(input, Token::While); + let pos = eat_token(input, Token::While); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } // while guard { body } ensure_not_statement_expr(input, "a boolean")?; - let guard = parse_expr(input, stack, allow_stmt_expr)?; + let guard = parse_expr(input, state, level + 1, allow_stmt_expr)?; ensure_not_assignment(input)?; - let body = parse_block(input, stack, true, allow_stmt_expr)?; + let body = parse_block(input, state, true, level + 1, allow_stmt_expr)?; Ok(Stmt::While(Box::new((guard, body)))) } @@ -1893,14 +1977,19 @@ fn parse_while<'a>( /// Parse a loop statement. fn parse_loop<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { // loop ... - eat_token(input, Token::Loop); + let pos = eat_token(input, Token::Loop); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } // loop { body } - let body = parse_block(input, stack, true, allow_stmt_expr)?; + let body = parse_block(input, state, true, level + 1, allow_stmt_expr)?; Ok(Stmt::Loop(Box::new(body))) } @@ -1908,11 +1997,16 @@ fn parse_loop<'a>( /// Parse a for loop. fn parse_for<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { // for ... - eat_token(input, Token::For); + let pos = eat_token(input, Token::For); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } // for name ... let name = match input.next().unwrap() { @@ -1940,14 +2034,14 @@ fn parse_for<'a>( // for name in expr { body } ensure_not_statement_expr(input, "a boolean")?; - let expr = parse_expr(input, stack, allow_stmt_expr)?; + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; - let prev_len = stack.len(); - stack.push((name.clone(), ScopeEntryType::Normal)); + let prev_len = state.len(); + state.push((name.clone(), ScopeEntryType::Normal)); - let body = parse_block(input, stack, true, allow_stmt_expr)?; + let body = parse_block(input, state, true, level + 1, allow_stmt_expr)?; - stack.truncate(prev_len); + state.truncate(prev_len); Ok(Stmt::For(Box::new((name, expr, body)))) } @@ -1955,12 +2049,17 @@ fn parse_for<'a>( /// Parse a variable definition statement. fn parse_let<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, var_type: ScopeEntryType, + level: usize, allow_stmt_expr: bool, ) -> Result> { // let/const... (specified in `var_type`) - input.next(); + let (_, pos) = input.next().unwrap(); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } // let name ... let (name, pos) = match input.next().unwrap() { @@ -1972,17 +2071,17 @@ fn parse_let<'a>( // let name = ... if match_token(input, Token::Equals)? { // let name = expr - let init_value = parse_expr(input, stack, allow_stmt_expr)?; + let init_value = parse_expr(input, state, level + 1, allow_stmt_expr)?; match var_type { // let name = expr ScopeEntryType::Normal => { - stack.push((name.clone(), ScopeEntryType::Normal)); + state.push((name.clone(), ScopeEntryType::Normal)); Ok(Stmt::Let(Box::new(((name, pos), Some(init_value))))) } // const name = { expr:constant } ScopeEntryType::Constant if init_value.is_constant() => { - stack.push((name.clone(), ScopeEntryType::Constant)); + state.push((name.clone(), ScopeEntryType::Constant)); Ok(Stmt::Const(Box::new(((name, pos), init_value)))) } // const name = expr - error @@ -1996,11 +2095,11 @@ fn parse_let<'a>( // let name match var_type { ScopeEntryType::Normal => { - stack.push((name.clone(), ScopeEntryType::Normal)); + state.push((name.clone(), ScopeEntryType::Normal)); Ok(Stmt::Let(Box::new(((name, pos), None)))) } ScopeEntryType::Constant => { - stack.push((name.clone(), ScopeEntryType::Constant)); + state.push((name.clone(), ScopeEntryType::Constant)); Ok(Stmt::Const(Box::new(((name, pos), Expr::Unit(pos))))) } // Variable cannot be a module @@ -2012,14 +2111,19 @@ fn parse_let<'a>( /// Parse an import statement. fn parse_import<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { // import ... let pos = eat_token(input, Token::Import); + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + // import expr ... - let expr = parse_expr(input, stack, allow_stmt_expr)?; + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; // import expr as ... match input.next().unwrap() { @@ -2039,13 +2143,21 @@ fn parse_import<'a>( (_, pos) => return Err(PERR::VariableExpected.into_err(pos)), }; - stack.push((name.clone(), ScopeEntryType::Module)); + state.push((name.clone(), ScopeEntryType::Module)); Ok(Stmt::Import(Box::new((expr, (name, pos))))) } /// Parse an export statement. -fn parse_export<'a>(input: &mut Peekable>) -> Result> { - eat_token(input, Token::Export); +fn parse_export<'a>( + input: &mut Peekable>, + state: &mut ParseState, + level: usize, +) -> Result> { + let pos = eat_token(input, Token::Export); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } let mut exports = StaticVec::new(); @@ -2103,8 +2215,9 @@ fn parse_export<'a>(input: &mut Peekable>) -> Result( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, breakable: bool, + level: usize, allow_stmt_expr: bool, ) -> Result> { // Must start with { @@ -2120,12 +2233,16 @@ fn parse_block<'a>( } }; + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + let mut statements = StaticVec::new(); - let prev_len = stack.len(); + let prev_len = state.len(); while !match_token(input, Token::RightBrace)? { // Parse statements inside the block - let stmt = parse_stmt(input, stack, breakable, false, allow_stmt_expr)?; + let stmt = parse_stmt(input, state, breakable, false, level + 1, allow_stmt_expr)?; // See if it needs a terminating semicolon let need_semicolon = !stmt.is_self_terminated(); @@ -2162,7 +2279,7 @@ fn parse_block<'a>( } } - stack.truncate(prev_len); + state.truncate(prev_len); Ok(Stmt::Block(Box::new((statements, pos)))) } @@ -2170,41 +2287,55 @@ fn parse_block<'a>( /// Parse an expression as a statement. fn parse_expr_stmt<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { - let expr = parse_expr(input, stack, allow_stmt_expr)?; - let expr = parse_op_assignment_stmt(input, stack, expr, allow_stmt_expr)?; + let (_, pos) = input.peek().unwrap(); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(*pos)); + } + + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; + let expr = parse_op_assignment_stmt(input, state, expr, level + 1, allow_stmt_expr)?; Ok(Stmt::Expr(Box::new(expr))) } /// Parse a single statement. fn parse_stmt<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, breakable: bool, is_global: bool, + level: usize, allow_stmt_expr: bool, ) -> Result> { + use ScopeEntryType::{Constant, Normal}; + let (token, pos) = match input.peek().unwrap() { (Token::EOF, pos) => return Ok(Stmt::Noop(*pos)), x => x, }; + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(*pos)); + } + match token { // Semicolon - empty statement Token::SemiColon => Ok(Stmt::Noop(*pos)), - Token::LeftBrace => parse_block(input, stack, breakable, allow_stmt_expr), + Token::LeftBrace => parse_block(input, state, breakable, level + 1, allow_stmt_expr), // fn ... Token::Fn if !is_global => Err(PERR::WrongFnDefinition.into_err(*pos)), Token::Fn => unreachable!(), - Token::If => parse_if(input, stack, breakable, allow_stmt_expr), - Token::While => parse_while(input, stack, allow_stmt_expr), - Token::Loop => parse_loop(input, stack, allow_stmt_expr), - Token::For => parse_for(input, stack, allow_stmt_expr), + Token::If => parse_if(input, state, breakable, level + 1, allow_stmt_expr), + Token::While => parse_while(input, state, level + 1, allow_stmt_expr), + Token::Loop => parse_loop(input, state, level + 1, allow_stmt_expr), + Token::For => parse_for(input, state, level + 1, allow_stmt_expr), Token::Continue if breakable => { let pos = eat_token(input, Token::Continue); @@ -2234,7 +2365,7 @@ fn parse_stmt<'a>( } // `return` or `throw` with expression (_, _) => { - let expr = parse_expr(input, stack, allow_stmt_expr)?; + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; let pos = expr.position(); Ok(Stmt::ReturnWithVal(Box::new(( @@ -2245,31 +2376,36 @@ fn parse_stmt<'a>( } } - Token::Let => parse_let(input, stack, ScopeEntryType::Normal, allow_stmt_expr), - Token::Const => parse_let(input, stack, ScopeEntryType::Constant, allow_stmt_expr), + 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, stack, allow_stmt_expr), + Token::Import => parse_import(input, state, level + 1, allow_stmt_expr), #[cfg(not(feature = "no_module"))] Token::Export if !is_global => Err(PERR::WrongExport.into_err(*pos)), #[cfg(not(feature = "no_module"))] - Token::Export => parse_export(input), + Token::Export => parse_export(input, state, level + 1), - _ => parse_expr_stmt(input, stack, allow_stmt_expr), + _ => parse_expr_stmt(input, state, level + 1, allow_stmt_expr), } } /// Parse a function definition. fn parse_fn<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, access: FnAccess, + level: usize, allow_stmt_expr: bool, ) -> Result> { let pos = eat_token(input, Token::Fn); + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + let name = match input.next().unwrap() { (Token::Identifier(s), _) => s, (_, pos) => return Err(PERR::FnMissingName.into_err(pos)), @@ -2289,7 +2425,7 @@ fn parse_fn<'a>( loop { match input.next().unwrap() { (Token::Identifier(s), pos) => { - stack.push((s.clone(), ScopeEntryType::Normal)); + state.push((s.clone(), ScopeEntryType::Normal)); params.push((s, pos)) } (Token::LexError(err), pos) => { @@ -2333,7 +2469,7 @@ fn parse_fn<'a>( // Parse function body let body = match input.peek().unwrap() { - (Token::LeftBrace, _) => parse_block(input, stack, false, allow_stmt_expr)?, + (Token::LeftBrace, _) => parse_block(input, state, false, level + 1, allow_stmt_expr)?, (_, pos) => return Err(PERR::FnMissingBody(name).into_err(*pos)), }; @@ -2353,9 +2489,10 @@ pub fn parse_global_expr<'a>( engine: &Engine, scope: &Scope, optimization_level: OptimizationLevel, + max_expr_depth: usize, ) -> Result> { - let mut stack = Stack::new(); - let expr = parse_expr(input, &mut stack, false)?; + let mut state = ParseState::new(max_expr_depth); + let expr = parse_expr(input, &mut state, 0, false)?; match input.peek().unwrap() { (Token::EOF, _) => (), @@ -2380,10 +2517,11 @@ pub fn parse_global_expr<'a>( /// Parse the global level statements. fn parse_global_level<'a>( input: &mut Peekable>, + max_expr_depth: (usize, usize), ) -> Result<(Vec, HashMap), Box> { let mut statements = Vec::::new(); let mut functions = HashMap::::new(); - let mut stack = Stack::new(); + let mut state = ParseState::new(max_expr_depth.0); while !input.peek().unwrap().0.is_eof() { // Collect all the function definitions @@ -2399,8 +2537,8 @@ fn parse_global_level<'a>( match input.peek().unwrap() { (Token::Fn, _) => { - let mut stack = Stack::new(); - let func = parse_fn(input, &mut stack, access, true)?; + let mut state = ParseState::new(max_expr_depth.1); + let func = parse_fn(input, &mut state, access, 0, true)?; // Qualifiers (none) + function name + argument `TypeId`'s let hash = calc_fn_hash( @@ -2423,7 +2561,7 @@ fn parse_global_level<'a>( } } // Actual statement - let stmt = parse_stmt(input, &mut stack, false, true, true)?; + let stmt = parse_stmt(input, &mut state, false, true, 0, true)?; let need_semicolon = !stmt.is_self_terminated(); @@ -2465,8 +2603,9 @@ pub fn parse<'a>( engine: &Engine, scope: &Scope, optimization_level: OptimizationLevel, + max_expr_depth: (usize, usize), ) -> Result> { - let (statements, functions) = parse_global_level(input)?; + let (statements, functions) = parse_global_level(input, max_expr_depth)?; let fn_lib = functions.into_iter().map(|(_, v)| v).collect(); Ok( diff --git a/tests/stack.rs b/tests/stack.rs index 5ebb550b..3087e558 100644 --- a/tests/stack.rs +++ b/tests/stack.rs @@ -1,18 +1,18 @@ #![cfg(not(feature = "no_function"))] -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, ParseErrorType}; #[test] -fn test_stack_overflow() -> Result<(), Box> { +fn test_stack_overflow_fn_calls() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( engine.eval::( r" - fn foo(n) { if n == 0 { 0 } else { n + foo(n-1) } } - foo(25) + fn foo(n) { if n <= 1 { 0 } else { n + foo(n-1) } } + foo(8) ", )?, - 325 + 35 ); #[cfg(not(feature = "unchecked"))] @@ -32,3 +32,53 @@ fn test_stack_overflow() -> Result<(), Box> { Ok(()) } + +#[test] +fn test_stack_overflow_parsing() -> Result<(), Box> { + let mut engine = Engine::new(); + + assert!(matches!( + *engine.compile(r" + let a = (1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+1)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) + ").expect_err("should error"), + err if err.error_type() == &ParseErrorType::ExprTooDeep + )); + + engine.set_max_expr_depths(100, 6); + + engine.compile("1 + 2")?; + engine.compile( + r" + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 0 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + ", + )?; + + assert!(matches!( + *engine.compile(r" + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + ").expect_err("should error"), + err if err.error_type() == &ParseErrorType::ExprTooDeep + )); + + engine.compile("fn abc(x) { x + 1 }")?; + + Ok(()) +} From 6b8c6bda424a46cd32bf9c4fea27a03298a44861 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 May 2020 10:08:27 +0800 Subject: [PATCH 02/53] Use u64 for operations counter. --- README.md | 2 +- src/engine.rs | 38 +++++++++++++++++++------------------- tests/modules.rs | 12 ++++++------ tests/stack.rs | 15 +++++---------- 4 files changed, 31 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 6edb1ca7..f6db9187 100644 --- a/README.md +++ b/README.md @@ -2347,7 +2347,7 @@ engine.set_max_modules(0); // allow unlimited modules ### Maximum call stack depth -Rhai by default limits function calls to a maximum depth of 128 levels (8 levels in debug build). +Rhai by default limits function calls to a maximum depth of 128 levels (16 levels in debug build). This limit may be changed via the `Engine::set_max_call_levels` method. When setting this limit, care must be also taken to the evaluation depth of each _statement_ diff --git a/src/engine.rs b/src/engine.rs index 6bd2bafa..bd28ccf1 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -27,7 +27,7 @@ use crate::stdlib::{ format, iter::{empty, once, repeat}, mem, - num::{NonZeroU64, NonZeroUsize}, + num::NonZeroUsize, ops::{Deref, DerefMut}, rc::Rc, string::{String, ToString}, @@ -49,7 +49,7 @@ pub type Map = HashMap; #[cfg(not(feature = "unchecked"))] #[cfg(debug_assertions)] -pub const MAX_CALL_STACK_DEPTH: usize = 8; +pub const MAX_CALL_STACK_DEPTH: usize = 16; #[cfg(not(feature = "unchecked"))] #[cfg(debug_assertions)] pub const MAX_EXPR_DEPTH: usize = 32; @@ -354,16 +354,16 @@ pub struct Engine { pub(crate) optimization_level: OptimizationLevel, /// Maximum levels of call-stack to prevent infinite recursion. /// - /// Defaults to 8 for debug builds and 128 for non-debug builds. + /// Defaults to 16 for debug builds and 128 for non-debug builds. pub(crate) max_call_stack_depth: usize, /// Maximum depth of statements/expressions at global level. pub(crate) max_expr_depth: usize, /// Maximum depth of statements/expressions in functions. pub(crate) max_function_expr_depth: usize, /// Maximum number of operations allowed to run. - pub(crate) max_operations: Option, + pub(crate) max_operations: u64, /// Maximum number of modules allowed to load. - pub(crate) max_modules: Option, + pub(crate) max_modules: u64, } impl Default for Engine { @@ -404,8 +404,8 @@ impl Default for Engine { max_call_stack_depth: MAX_CALL_STACK_DEPTH, max_expr_depth: MAX_EXPR_DEPTH, max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, - max_operations: None, - max_modules: None, + max_operations: u64::MAX, + max_modules: u64::MAX, }; #[cfg(feature = "no_stdlib")] @@ -547,8 +547,8 @@ impl Engine { max_call_stack_depth: MAX_CALL_STACK_DEPTH, max_expr_depth: MAX_EXPR_DEPTH, max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, - max_operations: None, - max_modules: None, + max_operations: u64::MAX, + max_modules: u64::MAX, } } @@ -589,13 +589,17 @@ impl Engine { /// consuming too much resources (0 for unlimited). #[cfg(not(feature = "unchecked"))] pub fn set_max_operations(&mut self, operations: u64) { - self.max_operations = NonZeroU64::new(operations); + self.max_operations = if operations == 0 { + u64::MAX + } else { + operations + }; } /// Set the maximum number of imported modules allowed for a script (0 for unlimited). #[cfg(not(feature = "unchecked"))] pub fn set_max_modules(&mut self, modules: u64) { - self.max_modules = NonZeroU64::new(modules); + self.max_modules = if modules == 0 { u64::MAX } else { modules }; } /// Set the depth limits for expressions/statements. @@ -1835,10 +1839,8 @@ impl Engine { let (expr, (name, pos)) = x.as_ref(); // Guard against too many modules - if let Some(max) = self.max_modules { - if state.modules >= max.get() { - return Err(Box::new(EvalAltResult::ErrorTooManyModules(*pos))); - } + if state.modules >= self.max_modules { + return Err(Box::new(EvalAltResult::ErrorTooManyModules(*pos))); } if let Some(path) = self @@ -1902,10 +1904,8 @@ impl Engine { #[cfg(not(feature = "unchecked"))] { // Guard against too many operations - if let Some(max) = self.max_operations { - if state.operations > max.get() { - return Err(Box::new(EvalAltResult::ErrorTooManyOperations(pos))); - } + if state.operations > self.max_operations { + return Err(Box::new(EvalAltResult::ErrorTooManyOperations(pos))); } } diff --git a/tests/modules.rs b/tests/modules.rs index 9c6c8ec4..4e475d1c 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -86,14 +86,14 @@ fn test_module_resolver() -> Result<(), Box> { *engine .eval::( r#" - let x = 0; + let sum = 0; for x in range(0, 10) { import "hello" as h; - x += h::answer; + sum += h::answer; } - x + sum "# ) .expect_err("should error"), @@ -105,18 +105,18 @@ fn test_module_resolver() -> Result<(), Box> { *engine .eval::( r#" - let x = 0; + let sum = 0; fn foo() { import "hello" as h; - x += h::answer; + sum += h::answer; } for x in range(0, 10) { foo(); } - x + sum "# ) .expect_err("should error"), diff --git a/tests/stack.rs b/tests/stack.rs index 3087e558..ee7eedc4 100644 --- a/tests/stack.rs +++ b/tests/stack.rs @@ -16,19 +16,14 @@ fn test_stack_overflow_fn_calls() -> Result<(), Box> { ); #[cfg(not(feature = "unchecked"))] - match engine.eval::<()>( + assert!(matches!( + *engine.eval::<()>( r" fn foo(n) { if n == 0 { 0 } else { n + foo(n-1) } } foo(1000) - ", - ) { - Ok(_) => panic!("should be stack overflow"), - Err(err) => match *err { - EvalAltResult::ErrorInFunctionCall(name, _, _) - if name.starts_with("foo > foo > foo") => {} - _ => panic!("should be stack overflow"), - }, - } + ").expect_err("should error"), + EvalAltResult::ErrorInFunctionCall(name, _, _) if name.starts_with("foo > foo > foo") + )); Ok(()) } From a22f338b03857742f6a650b77885a4a5fb6ac94a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 May 2020 10:13:37 +0800 Subject: [PATCH 03/53] Back out NativeCallable trait. --- src/fn_native.rs | 12 +++++++----- src/lib.rs | 2 +- src/module.rs | 10 +++++----- src/optimize.rs | 1 - src/packages/mod.rs | 4 ++-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/fn_native.rs b/src/fn_native.rs index 7a1dd0e6..ccc458d3 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -82,6 +82,7 @@ pub enum NativeFunctionABI { Method, } +/* /// Trait implemented by all native Rust functions that are callable by Rhai. #[cfg(not(feature = "sync"))] pub trait NativeCallable { @@ -99,15 +100,16 @@ pub trait NativeCallable: Send + Sync { /// Call a native Rust function. fn call(&self, args: &mut FnCallArgs) -> Result>; } +*/ /// A type encapsulating a native Rust function callable by Rhai. pub struct NativeFunction(Box, NativeFunctionABI); -impl NativeCallable for NativeFunction { - fn abi(&self) -> NativeFunctionABI { +impl NativeFunction { + pub fn abi(&self) -> NativeFunctionABI { self.1 } - fn call(&self, args: &mut FnCallArgs) -> Result> { + pub fn call(&self, args: &mut FnCallArgs) -> Result> { (self.0)(args) } } @@ -126,10 +128,10 @@ impl NativeFunction { /// An external native Rust function. #[cfg(not(feature = "sync"))] -pub type SharedNativeFunction = Rc>; +pub type SharedNativeFunction = Rc; /// An external native Rust function. #[cfg(feature = "sync")] -pub type SharedNativeFunction = Arc>; +pub type SharedNativeFunction = Arc; /// A type iterator function. #[cfg(not(feature = "sync"))] diff --git a/src/lib.rs b/src/lib.rs index 5c7a10ae..2494d71f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,7 +91,7 @@ mod utils; pub use any::Dynamic; pub use engine::Engine; pub use error::{ParseError, ParseErrorType}; -pub use fn_native::NativeCallable; +//pub use fn_native::NativeCallable; pub use fn_register::{RegisterDynamicFn, RegisterFn, RegisterResultFn}; pub use module::Module; pub use parser::{AST, INT}; diff --git a/src/module.rs b/src/module.rs index 377a4357..f28c87fe 100644 --- a/src/module.rs +++ b/src/module.rs @@ -4,8 +4,8 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; use crate::fn_native::{ - FnAny, FnCallArgs, IteratorFn, NativeCallable, NativeFunction, NativeFunctionABI, - NativeFunctionABI::*, SharedIteratorFunction, SharedNativeFunction, + FnAny, FnCallArgs, IteratorFn, NativeFunction, NativeFunctionABI, NativeFunctionABI::*, + SharedIteratorFunction, SharedNativeFunction, }; use crate::parser::{FnAccess, FnDef, SharedFnDef, AST}; use crate::result::EvalAltResult; @@ -285,7 +285,7 @@ impl Module { ) -> u64 { let hash_fn = calc_fn_hash(empty(), &name, params.iter().cloned()); - let f = Box::new(NativeFunction::from((func, abi))) as Box; + let f = NativeFunction::from((func, abi)); #[cfg(not(feature = "sync"))] let func = Rc::new(f); @@ -531,7 +531,7 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn get_fn(&self, hash_fn: u64) -> Option<&Box> { + pub fn get_fn(&self, hash_fn: u64) -> Option<&NativeFunction> { self.functions.get(&hash_fn).map(|(_, _, _, v)| v.as_ref()) } @@ -543,7 +543,7 @@ impl Module { &mut self, name: &str, hash_fn_native: u64, - ) -> Result<&Box, Box> { + ) -> Result<&NativeFunction, Box> { self.all_functions .get(&hash_fn_native) .map(|f| f.as_ref()) diff --git a/src/optimize.rs b/src/optimize.rs index 671415e3..5fc6d608 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -15,7 +15,6 @@ use crate::utils::StaticVec; use crate::stdlib::{ boxed::Box, iter::empty, - mem, string::{String, ToString}, vec, vec::Vec, diff --git a/src/packages/mod.rs b/src/packages/mod.rs index e56843f0..ad61ae19 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -1,6 +1,6 @@ //! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages. -use crate::fn_native::{NativeCallable, SharedIteratorFunction}; +use crate::fn_native::{NativeFunction, SharedIteratorFunction}; use crate::module::Module; use crate::utils::StaticVec; @@ -69,7 +69,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_fn(hash)) } /// Get specified function via its hash key. - pub fn get_fn(&self, hash: u64) -> Option<&Box> { + pub fn get_fn(&self, hash: u64) -> Option<&NativeFunction> { self.packages .iter() .map(|p| p.get_fn(hash)) From 3295060dba35440239732ef87b8aa05df9bce63c Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 May 2020 19:03:06 +0800 Subject: [PATCH 04/53] Unify all functions under CallableFunction type. --- src/engine.rs | 113 +++++++++++++++----------------- src/fn_native.rs | 155 ++++++++++++++++++++++++++------------------ src/fn_register.rs | 26 ++++---- src/lib.rs | 1 - src/module.rs | 86 +++++++++--------------- src/optimize.rs | 8 +-- src/packages/mod.rs | 6 +- src/result.rs | 24 ------- 8 files changed, 199 insertions(+), 220 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index bd28ccf1..85c0c9f4 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::fn_native::{FnCallArgs, NativeFunctionABI, PrintCallback, ProgressCallback}; +use crate::fn_native::{FnCallArgs, PrintCallback, ProgressCallback}; use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; @@ -229,15 +229,7 @@ impl FunctionsLib { // Qualifiers (none) + function name + placeholders (one for each parameter). let args_iter = repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()); let hash = calc_fn_hash(empty(), &fn_def.name, args_iter); - - #[cfg(feature = "sync")] - { - (hash, Arc::new(fn_def)) - } - #[cfg(not(feature = "sync"))] - { - (hash, Rc::new(fn_def)) - } + (hash, fn_def.into()) }) .collect(), ) @@ -268,8 +260,7 @@ impl FunctionsLib { match fn_def.as_ref().map(|f| f.access) { None => None, Some(FnAccess::Private) if public_only => None, - Some(FnAccess::Private) => fn_def, - Some(FnAccess::Public) => fn_def, + Some(FnAccess::Private) | Some(FnAccess::Public) => fn_def, } } /// Merge another `FunctionsLib` into this `FunctionsLib`. @@ -659,25 +650,20 @@ impl Engine { .get_fn(hashes.0) .or_else(|| self.packages.get_fn(hashes.0)) { - let mut backup: Dynamic = Default::default(); - - let (updated, restore) = match func.abi() { - // Calling pure function in method-call - NativeFunctionABI::Pure if is_ref && args.len() > 0 => { - // Backup the original value. It'll be consumed because the function - // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) - backup = args[0].clone(); - (false, true) - } - NativeFunctionABI::Pure => (false, false), - NativeFunctionABI::Method => (true, false), + // Calling pure function in method-call? + let backup: Option = if func.is_pure() && is_ref && args.len() > 0 { + // Backup the original value. It'll be consumed because the function + // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) + Some(args[0].clone()) + } else { + None }; // Run external function - let result = match func.call(args) { + let result = match func.get_native_fn()(args) { Ok(r) => { // Restore the backup value for the first argument since it has been consumed! - if restore { + if let Some(backup) = backup { *args[0] = backup; } r @@ -709,7 +695,7 @@ impl Engine { .into(), false, ), - _ => (result, updated), + _ => (result, func.is_method()), }); } @@ -780,14 +766,12 @@ impl Engine { let scope_len = scope.len(); // Put arguments into scope as variables + // Actually consume the arguments instead of cloning them scope.extend( fn_def .params .iter() - .zip( - // Actually consume the arguments instead of cloning them - args.into_iter().map(|v| mem::take(*v)), - ) + .zip(args.iter_mut().map(|v| mem::take(*v))) .map(|(name, value)| { let var_name = unsafe_cast_var_name_to_lifetime(name.as_str(), &mut state); @@ -826,14 +810,12 @@ impl Engine { let mut scope = Scope::new(); // Put arguments into scope as variables + // Actually consume the arguments instead of cloning them scope.extend( fn_def .params .iter() - .zip( - // Actually consume the arguments instead of cloning them - args.into_iter().map(|v| mem::take(*v)), - ) + .zip(args.iter_mut().map(|v| mem::take(*v))) .map(|(name, value)| (name, ScopeEntryType::Normal, value)), ); @@ -1558,32 +1540,43 @@ impl Engine { }; // First search in script-defined functions (can override built-in) - if let Some(fn_def) = module.get_qualified_scripted_fn(*hash_fn_def) { - let args = args.as_mut(); - let (result, state2) = - self.call_script_fn(None, *state, name, fn_def, args, *pos, level)?; - *state = state2; - Ok(result) - } else { - // Then search in Rust functions - self.inc_operations(state, *pos)?; + let func = match module.get_qualified_fn(name, *hash_fn_def) { + Err(err) if matches!(*err, EvalAltResult::ErrorFunctionNotFound(_, _)) => { + // Then search in Rust functions + self.inc_operations(state, *pos)?; - // Rust functions are indexed in two steps: - // 1) Calculate a hash in a similar manner to script-defined functions, - // i.e. qualifiers + function name + dummy parameter types (one for each parameter). - // 2) Calculate a second hash with no qualifiers, empty function name, and - // the actual list of parameter `TypeId`'.s - let hash_fn_args = calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); - // 3) The final hash is the XOR of the two hashes. - let hash_fn_native = *hash_fn_def ^ hash_fn_args; + // Rust functions are indexed in two steps: + // 1) Calculate a hash in a similar manner to script-defined functions, + // i.e. qualifiers + function name + dummy parameter types (one for each parameter). + // 2) Calculate a second hash with no qualifiers, empty function name, and + // the actual list of parameter `TypeId`'.s + let hash_fn_args = + calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); + // 3) The final hash is the XOR of the two hashes. + let hash_fn_native = *hash_fn_def ^ hash_fn_args; - match module.get_qualified_fn(name, hash_fn_native) { - Ok(func) => func - .call(args.as_mut()) - .map_err(|err| err.new_position(*pos)), - Err(_) if def_val.is_some() => Ok(def_val.clone().unwrap()), - Err(err) => Err(err), + module.get_qualified_fn(name, hash_fn_native) } + r => r, + }; + + match func { + Ok(x) if x.is_script() => { + let args = args.as_mut(); + let fn_def = x.get_fn_def(); + let (result, state2) = + self.call_script_fn(None, *state, name, fn_def, args, *pos, level)?; + *state = state2; + Ok(result) + } + Ok(x) => x.get_native_fn()(args.as_mut()).map_err(|err| err.new_position(*pos)), + Err(err) + if def_val.is_some() + && matches!(*err, EvalAltResult::ErrorFunctionNotFound(_, _)) => + { + Ok(def_val.clone().unwrap()) + } + Err(err) => Err(err), } } @@ -1733,7 +1726,7 @@ impl Engine { let iter_type = self.eval_expr(scope, state, expr, level)?; let tid = iter_type.type_id(); - if let Some(iter_fn) = self + if let Some(func) = self .global_module .get_iter(tid) .or_else(|| self.packages.get_iter(tid)) @@ -1744,7 +1737,7 @@ impl Engine { let index = scope.len() - 1; state.scope_level += 1; - for loop_var in iter_fn(iter_type) { + for loop_var in func.get_iter_fn()(iter_type) { *scope.get_mut(index).0 = loop_var; self.inc_operations(state, stmt.position())?; diff --git a/src/fn_native.rs b/src/fn_native.rs index ccc458d3..328372b9 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -1,4 +1,5 @@ use crate::any::Dynamic; +use crate::parser::{FnDef, SharedFnDef}; use crate::result::EvalAltResult; use crate::stdlib::{boxed::Box, rc::Rc, sync::Arc}; @@ -72,70 +73,98 @@ pub trait IteratorCallback: Fn(Dynamic) -> Box> + ' #[cfg(not(feature = "sync"))] impl Box> + 'static> IteratorCallback for F {} -/// A type representing the type of ABI of a native Rust function. -#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)] -pub enum NativeFunctionABI { - /// A pure function where all arguments are passed by value. - Pure, - /// An object method where the first argument is the object passed by mutable reference. - /// All other arguments are passed by value. - Method, +/// A type encapsulating a function callable by Rhai. +pub enum CallableFunction { + /// A pure native Rust function with all arguments passed by value. + Pure(Box), + /// A native Rust object method with the first argument passed by reference, + /// and the rest passed by value. + Method(Box), + /// An iterator function. + Iterator(Box), + /// A script-defined function. + Script(SharedFnDef), } -/* -/// Trait implemented by all native Rust functions that are callable by Rhai. +impl CallableFunction { + /// Is this a pure native Rust function? + pub fn is_pure(&self) -> bool { + match self { + CallableFunction::Pure(_) => true, + CallableFunction::Method(_) + | CallableFunction::Iterator(_) + | CallableFunction::Script(_) => false, + } + } + /// Is this a pure native Rust method-call? + pub fn is_method(&self) -> bool { + match self { + CallableFunction::Method(_) => true, + CallableFunction::Pure(_) + | CallableFunction::Iterator(_) + | CallableFunction::Script(_) => false, + } + } + /// Is this an iterator function? + pub fn is_iter(&self) -> bool { + match self { + CallableFunction::Iterator(_) => true, + CallableFunction::Pure(_) + | CallableFunction::Method(_) + | CallableFunction::Script(_) => false, + } + } + /// Is this a Rhai-scripted function? + pub fn is_script(&self) -> bool { + match self { + CallableFunction::Script(_) => true, + CallableFunction::Pure(_) + | CallableFunction::Method(_) + | CallableFunction::Iterator(_) => false, + } + } + /// Get a reference to a native Rust function. + /// + /// # Panics + /// + /// Panics if the `CallableFunction` is not `Pure` or `Method`. + pub fn get_native_fn(&self) -> &Box { + match self { + CallableFunction::Pure(f) | CallableFunction::Method(f) => f, + CallableFunction::Iterator(_) | CallableFunction::Script(_) => panic!(), + } + } + /// Get a reference to a script-defined function definition. + /// + /// # Panics + /// + /// Panics if the `CallableFunction` is not `Script`. + pub fn get_fn_def(&self) -> &FnDef { + match self { + CallableFunction::Pure(_) + | CallableFunction::Method(_) + | CallableFunction::Iterator(_) => panic!(), + CallableFunction::Script(f) => f, + } + } + /// Get a reference to an iterator function. + /// + /// # Panics + /// + /// Panics if the `CallableFunction` is not `Iterator`. + pub fn get_iter_fn(&self) -> &Box { + match self { + CallableFunction::Pure(_) + | CallableFunction::Method(_) + | CallableFunction::Script(_) => panic!(), + CallableFunction::Iterator(f) => f, + } + } +} + +/// A callable function. #[cfg(not(feature = "sync"))] -pub trait NativeCallable { - /// Get the ABI type of a native Rust function. - fn abi(&self) -> NativeFunctionABI; - /// Call a native Rust function. - fn call(&self, args: &mut FnCallArgs) -> Result>; -} - -/// Trait implemented by all native Rust functions that are callable by Rhai. +pub type SharedFunction = Rc; +/// A callable function. #[cfg(feature = "sync")] -pub trait NativeCallable: Send + Sync { - /// Get the ABI type of a native Rust function. - fn abi(&self) -> NativeFunctionABI; - /// Call a native Rust function. - fn call(&self, args: &mut FnCallArgs) -> Result>; -} -*/ - -/// A type encapsulating a native Rust function callable by Rhai. -pub struct NativeFunction(Box, NativeFunctionABI); - -impl NativeFunction { - pub fn abi(&self) -> NativeFunctionABI { - self.1 - } - pub fn call(&self, args: &mut FnCallArgs) -> Result> { - (self.0)(args) - } -} - -impl From<(Box, NativeFunctionABI)> for NativeFunction { - fn from(func: (Box, NativeFunctionABI)) -> Self { - Self::new(func.0, func.1) - } -} -impl NativeFunction { - /// Create a new `NativeFunction`. - pub fn new(func: Box, abi: NativeFunctionABI) -> Self { - Self(func, abi) - } -} - -/// An external native Rust function. -#[cfg(not(feature = "sync"))] -pub type SharedNativeFunction = Rc; -/// An external native Rust function. -#[cfg(feature = "sync")] -pub type SharedNativeFunction = Arc; - -/// A type iterator function. -#[cfg(not(feature = "sync"))] -pub type SharedIteratorFunction = Rc>; -/// An external native Rust function. -#[cfg(feature = "sync")] -pub type SharedIteratorFunction = Arc>; +pub type SharedFunction = Arc; diff --git a/src/fn_register.rs b/src/fn_register.rs index 42e81c9b..9636e557 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -4,7 +4,10 @@ use crate::any::{Dynamic, Variant}; use crate::engine::Engine; -use crate::fn_native::{FnCallArgs, NativeFunctionABI::*}; +use crate::fn_native::{ + CallableFunction::{Method, Pure}, + FnCallArgs, +}; use crate::parser::FnAccess; use crate::result::EvalAltResult; @@ -181,9 +184,9 @@ pub fn map_result( macro_rules! def_register { () => { - def_register!(imp Pure;); + def_register!(imp Pure :); }; - (imp $abi:expr ; $($par:ident => $mark:ty => $param:ty => $clone:expr),*) => { + (imp $abi:ident : $($par:ident => $mark:ty => $param:ty => $clone:expr),*) => { // ^ function ABI type // ^ function parameter generic type name (A, B, C etc.) // ^ function parameter marker type (T, Ref or Mut) @@ -202,9 +205,9 @@ macro_rules! def_register { > RegisterFn for Engine { fn register_fn(&mut self, name: &str, f: FN) { - self.global_module.set_fn(name.to_string(), $abi, FnAccess::Public, + self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - make_func!(f : map_dynamic ; $($par => $clone),*) + $abi(make_func!(f : map_dynamic ; $($par => $clone),*)) ); } } @@ -220,9 +223,9 @@ macro_rules! def_register { > RegisterDynamicFn for Engine { fn register_dynamic_fn(&mut self, name: &str, f: FN) { - self.global_module.set_fn(name.to_string(), $abi, FnAccess::Public, + self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - make_func!(f : map_identity ; $($par => $clone),*) + $abi(make_func!(f : map_identity ; $($par => $clone),*)) ); } } @@ -239,9 +242,9 @@ macro_rules! def_register { > RegisterResultFn for Engine { fn register_result_fn(&mut self, name: &str, f: FN) { - self.global_module.set_fn(name.to_string(), $abi, FnAccess::Public, + self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - make_func!(f : map_result ; $($par => $clone),*) + $abi(make_func!(f : map_result ; $($par => $clone),*)) ); } } @@ -249,8 +252,9 @@ macro_rules! def_register { //def_register!(imp_pop $($par => $mark => $param),*); }; ($p0:ident $(, $p:ident)*) => { - def_register!(imp Pure ; $p0 => $p0 => $p0 => by_value $(, $p => $p => $p => by_value)*); - def_register!(imp Method ; $p0 => Mut<$p0> => &mut $p0 => by_ref $(, $p => $p => $p => by_value)*); + def_register!(imp Pure : $p0 => $p0 => $p0 => by_value $(, $p => $p => $p => by_value)*); + def_register!(imp Method : $p0 => Mut<$p0> => &mut $p0 => by_ref $(, $p => $p => $p => by_value)*); + // ^ CallableFunction // handle the first parameter ^ first parameter passed through // ^ others passed by value (by_value) diff --git a/src/lib.rs b/src/lib.rs index 2494d71f..b4e71a83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,7 +91,6 @@ mod utils; pub use any::Dynamic; pub use engine::Engine; pub use error::{ParseError, ParseErrorType}; -//pub use fn_native::NativeCallable; pub use fn_register::{RegisterDynamicFn, RegisterFn, RegisterResultFn}; pub use module::Module; pub use parser::{AST, INT}; diff --git a/src/module.rs b/src/module.rs index f28c87fe..06d0cbbd 100644 --- a/src/module.rs +++ b/src/module.rs @@ -4,10 +4,11 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; use crate::fn_native::{ - FnAny, FnCallArgs, IteratorFn, NativeFunction, NativeFunctionABI, NativeFunctionABI::*, - SharedIteratorFunction, SharedNativeFunction, + CallableFunction, + CallableFunction::{Method, Pure}, + FnCallArgs, IteratorFn, SharedFunction, }; -use crate::parser::{FnAccess, FnDef, SharedFnDef, AST}; +use crate::parser::{FnAccess, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token}; @@ -51,19 +52,17 @@ pub struct Module { all_variables: HashMap, /// External Rust functions. - functions: HashMap, SharedNativeFunction)>, - - /// Flattened collection of all external Rust functions, including those in sub-modules. - all_functions: HashMap, + functions: HashMap, SharedFunction)>, /// Script-defined functions. fn_lib: FunctionsLib, - /// Flattened collection of all script-defined functions, including those in sub-modules. - all_fn_lib: FunctionsLib, - /// Iterator functions, keyed by the type producing the iterator. - type_iterators: HashMap, + type_iterators: HashMap, + + /// Flattened collection of all external Rust functions, native or scripted, + /// including those in sub-modules. + all_functions: HashMap, } impl fmt::Debug for Module { @@ -278,23 +277,16 @@ impl Module { pub fn set_fn( &mut self, name: String, - abi: NativeFunctionABI, access: FnAccess, params: &[TypeId], - func: Box, + func: CallableFunction, ) -> u64 { let hash_fn = calc_fn_hash(empty(), &name, params.iter().cloned()); - let f = NativeFunction::from((func, abi)); - - #[cfg(not(feature = "sync"))] - let func = Rc::new(f); - #[cfg(feature = "sync")] - let func = Arc::new(f); - let params = params.into_iter().cloned().collect(); - self.functions.insert(hash_fn, (name, access, params, func)); + self.functions + .insert(hash_fn, (name, access, params, func.into())); hash_fn } @@ -320,7 +312,7 @@ impl Module { ) -> u64 { let f = move |_: &mut FnCallArgs| func().map(Dynamic::from); let arg_types = []; - self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -345,7 +337,7 @@ impl Module { let f = move |args: &mut FnCallArgs| func(mem::take(args[0]).cast::()).map(Dynamic::from); let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -371,7 +363,7 @@ impl Module { func(args[0].downcast_mut::().unwrap()).map(Dynamic::from) }; let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -402,7 +394,7 @@ impl Module { func(a, b).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -437,7 +429,7 @@ impl Module { func(a, b).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -475,7 +467,7 @@ impl Module { func(a, b, c).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -514,7 +506,7 @@ impl Module { func(a, b, c).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) } /// Get a Rust function. @@ -531,7 +523,7 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn get_fn(&self, hash_fn: u64) -> Option<&NativeFunction> { + pub fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> { self.functions.get(&hash_fn).map(|(_, _, _, v)| v.as_ref()) } @@ -543,7 +535,7 @@ impl Module { &mut self, name: &str, hash_fn_native: u64, - ) -> Result<&NativeFunction, Box> { + ) -> Result<&CallableFunction, Box> { self.all_functions .get(&hash_fn_native) .map(|f| f.as_ref()) @@ -555,13 +547,6 @@ impl Module { }) } - /// Get a modules-qualified script-defined functions. - /// - /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. - pub(crate) fn get_qualified_scripted_fn(&mut self, hash_fn_def: u64) -> Option<&FnDef> { - self.all_fn_lib.get_function(hash_fn_def) - } - /// Create a new `Module` by evaluating an `AST`. /// /// # Examples @@ -620,13 +605,12 @@ impl Module { module: &'a Module, qualifiers: &mut Vec<&'a str>, variables: &mut Vec<(u64, Dynamic)>, - functions: &mut Vec<(u64, SharedNativeFunction)>, - fn_lib: &mut Vec<(u64, SharedFnDef)>, + functions: &mut Vec<(u64, SharedFunction)>, ) { for (name, m) in &module.modules { // Index all the sub-modules first. qualifiers.push(name); - index_module(m, qualifiers, variables, functions, fn_lib); + index_module(m, qualifiers, variables, functions); qualifiers.pop(); } @@ -672,25 +656,17 @@ impl Module { &fn_def.name, repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()), ); - fn_lib.push((hash_fn_def, fn_def.clone())); + functions.push((hash_fn_def, CallableFunction::Script(fn_def.clone()).into())); } } let mut variables = Vec::new(); let mut functions = Vec::new(); - let mut fn_lib = Vec::new(); - index_module( - self, - &mut vec!["root"], - &mut variables, - &mut functions, - &mut fn_lib, - ); + index_module(self, &mut vec!["root"], &mut variables, &mut functions); self.all_variables = variables.into_iter().collect(); self.all_functions = functions.into_iter().collect(); - self.all_fn_lib = fn_lib.into(); } /// Does a type iterator exist in the module? @@ -701,14 +677,16 @@ impl Module { /// Set a type iterator into the module. pub fn set_iter(&mut self, typ: TypeId, func: Box) { #[cfg(not(feature = "sync"))] - self.type_iterators.insert(typ, Rc::new(func)); + self.type_iterators + .insert(typ, Rc::new(CallableFunction::Iterator(func))); #[cfg(feature = "sync")] - self.type_iterators.insert(typ, Arc::new(func)); + self.type_iterators + .insert(typ, Arc::new(CallableFunction::Iterator(func))); } /// Get the specified type iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&SharedIteratorFunction> { - self.type_iterators.get(&id) + pub fn get_iter(&self, id: TypeId) -> Option<&CallableFunction> { + self.type_iterators.get(&id).map(|v| v.as_ref()) } } diff --git a/src/optimize.rs b/src/optimize.rs index 5fc6d608..d320d4bf 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -119,12 +119,12 @@ fn call_fn( pos: Position, ) -> Result, Box> { // Search built-in's and external functions - let hash = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); + let hash_fn = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); global_module - .get_fn(hash) - .or_else(|| packages.get_fn(hash)) - .map(|func| func.call(args)) + .get_fn(hash_fn) + .or_else(|| packages.get_fn(hash_fn)) + .map(|func| func.get_native_fn()(args)) .transpose() .map_err(|err| err.new_position(pos)) } diff --git a/src/packages/mod.rs b/src/packages/mod.rs index ad61ae19..39ce601c 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -1,6 +1,6 @@ //! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages. -use crate::fn_native::{NativeFunction, SharedIteratorFunction}; +use crate::fn_native::CallableFunction; use crate::module::Module; use crate::utils::StaticVec; @@ -69,7 +69,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_fn(hash)) } /// Get specified function via its hash key. - pub fn get_fn(&self, hash: u64) -> Option<&NativeFunction> { + pub fn get_fn(&self, hash: u64) -> Option<&CallableFunction> { self.packages .iter() .map(|p| p.get_fn(hash)) @@ -81,7 +81,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_iter(id)) } /// Get the specified TypeId iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&SharedIteratorFunction> { + pub fn get_iter(&self, id: TypeId) -> Option<&CallableFunction> { self.packages .iter() .map(|p| p.get_iter(id)) diff --git a/src/result.rs b/src/result.rs index 4ae0d9e5..af55535a 100644 --- a/src/result.rs +++ b/src/result.rs @@ -36,10 +36,6 @@ pub enum EvalAltResult { /// An error has occurred inside a called function. /// Wrapped values re the name of the function and the interior error. ErrorInFunctionCall(String, Box, Position), - /// Function call has incorrect number of arguments. - /// Wrapped values are the name of the function, the number of parameters required - /// and the actual number of arguments passed. - ErrorFunctionArgsMismatch(String, usize, usize, Position), /// Non-boolean operand encountered for boolean operator. Wrapped value is the operator. ErrorBooleanArgMismatch(String, Position), /// Non-character value encountered where a character is required. @@ -108,9 +104,6 @@ impl EvalAltResult { Self::ErrorParsing(p) => p.desc(), Self::ErrorInFunctionCall(_, _, _) => "Error in called function", Self::ErrorFunctionNotFound(_, _) => "Function not found", - Self::ErrorFunctionArgsMismatch(_, _, _, _) => { - "Function call with wrong number of arguments" - } Self::ErrorBooleanArgMismatch(_, _) => "Boolean operator expects boolean operands", Self::ErrorCharMismatch(_) => "Character expected", Self::ErrorNumericIndexExpr(_) => { @@ -208,21 +201,6 @@ impl fmt::Display for EvalAltResult { Self::ErrorLoopBreak(_, pos) => write!(f, "{} ({})", desc, pos), Self::Return(_, pos) => write!(f, "{} ({})", desc, pos), - Self::ErrorFunctionArgsMismatch(fn_name, 0, n, pos) => write!( - f, - "Function '{}' expects no argument but {} found ({})", - fn_name, n, pos - ), - Self::ErrorFunctionArgsMismatch(fn_name, 1, n, pos) => write!( - f, - "Function '{}' expects one argument but {} found ({})", - fn_name, n, pos - ), - Self::ErrorFunctionArgsMismatch(fn_name, need, n, pos) => write!( - f, - "Function '{}' expects {} argument(s) but {} found ({})", - fn_name, need, n, pos - ), Self::ErrorBooleanArgMismatch(op, pos) => { write!(f, "{} operator expects boolean operands ({})", op, pos) } @@ -292,7 +270,6 @@ impl EvalAltResult { Self::ErrorFunctionNotFound(_, pos) | Self::ErrorInFunctionCall(_, _, pos) - | Self::ErrorFunctionArgsMismatch(_, _, _, pos) | Self::ErrorBooleanArgMismatch(_, pos) | Self::ErrorCharMismatch(pos) | Self::ErrorArrayBounds(_, _, pos) @@ -331,7 +308,6 @@ impl EvalAltResult { Self::ErrorFunctionNotFound(_, pos) | Self::ErrorInFunctionCall(_, _, pos) - | Self::ErrorFunctionArgsMismatch(_, _, _, pos) | Self::ErrorBooleanArgMismatch(_, pos) | Self::ErrorCharMismatch(pos) | Self::ErrorArrayBounds(_, _, pos) From ab76a69b1242bda3dc3e3cfe851a3855bcb1c995 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 May 2020 20:07:51 +0800 Subject: [PATCH 05/53] Avoid repeating empty TypeId's when calculating hash. --- src/engine.rs | 40 ++++++++++++++++++--------------- src/module.rs | 59 ++++++++++++++++++++++++------------------------- src/optimize.rs | 7 +++++- src/parser.rs | 48 ++++++++++++++++++---------------------- src/utils.rs | 7 ++---- 5 files changed, 81 insertions(+), 80 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 85c0c9f4..d45d1099 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -12,7 +12,7 @@ use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::Position; -use crate::utils::{StaticVec, EMPTY_TYPE_ID}; +use crate::utils::StaticVec; #[cfg(not(feature = "no_module"))] use crate::module::{resolvers, ModuleRef, ModuleResolver}; @@ -25,7 +25,7 @@ use crate::stdlib::{ boxed::Box, collections::HashMap, format, - iter::{empty, once, repeat}, + iter::{empty, once}, mem, num::NonZeroUsize, ops::{Deref, DerefMut}, @@ -215,8 +215,7 @@ impl<'a> State<'a> { /// Since script-defined functions have `Dynamic` parameters, functions with the same name /// and number of parameters are considered equivalent. /// -/// The key of the `HashMap` is a `u64` hash calculated by the function `calc_fn_hash` -/// with dummy parameter types `EMPTY_TYPE_ID()` repeated the correct number of times. +/// The key of the `HashMap` is a `u64` hash calculated by the function `calc_fn_hash`. #[derive(Debug, Clone, Default)] pub struct FunctionsLib(HashMap); @@ -226,9 +225,8 @@ impl FunctionsLib { FunctionsLib( vec.into_iter() .map(|fn_def| { - // Qualifiers (none) + function name + placeholders (one for each parameter). - let args_iter = repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()); - let hash = calc_fn_hash(empty(), &fn_def.name, args_iter); + // Qualifiers (none) + function name + number of arguments. + let hash = calc_fn_hash(empty(), &fn_def.name, fn_def.params.len(), empty()); (hash, fn_def.into()) }) .collect(), @@ -253,8 +251,8 @@ impl FunctionsLib { params: usize, public_only: bool, ) -> Option<&FnDef> { - // Qualifiers (none) + function name + placeholders (one for each parameter). - let hash_fn_def = calc_fn_hash(empty(), name, repeat(EMPTY_TYPE_ID()).take(params)); + // Qualifiers (none) + function name + number of arguments. + let hash_fn_def = calc_fn_hash(empty(), name, params, empty()); let fn_def = self.get_function(hash_fn_def); match fn_def.as_ref().map(|f| f.access) { @@ -873,8 +871,13 @@ impl Engine { pos: Position, level: usize, ) -> Result<(Dynamic, bool), Box> { - // Qualifiers (none) + function name + argument `TypeId`'s. - let hash_fn = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); + // Qualifiers (none) + function name + number of arguments + argument `TypeId`'s. + let hash_fn = calc_fn_hash( + empty(), + fn_name, + args.len(), + args.iter().map(|a| a.type_id()), + ); let hashes = (hash_fn, hash_fn_def); match fn_name { @@ -1322,7 +1325,7 @@ impl Engine { Dynamic(Union::Array(mut rhs_value)) => { let op = "=="; let def_value = false.into(); - let hash_fn_def = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); + let hash_fn_def = calc_fn_hash(empty(), op, 2, empty()); // Call the `==` operator to compare each value for value in rhs_value.iter_mut() { @@ -1331,7 +1334,8 @@ impl Engine { let pos = rhs.position(); // Qualifiers (none) + function name + argument `TypeId`'s. - let hash_fn = calc_fn_hash(empty(), op, args.iter().map(|a| a.type_id())); + let hash_fn = + calc_fn_hash(empty(), op, args.len(), args.iter().map(|a| a.type_id())); let hashes = (hash_fn, hash_fn_def); let (r, _) = self @@ -1486,7 +1490,7 @@ impl Engine { let mut args: StaticVec<_> = arg_values.iter_mut().collect(); if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::() { - let hash_fn = calc_fn_hash(empty(), name, once(TypeId::of::())); + let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::())); if !self.has_override(state, (hash_fn, *hash_fn_def)) { // eval - only in function call style @@ -1547,11 +1551,11 @@ impl Engine { // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, - // i.e. qualifiers + function name + dummy parameter types (one for each parameter). - // 2) Calculate a second hash with no qualifiers, empty function name, and - // the actual list of parameter `TypeId`'.s + // 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 let hash_fn_args = - calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); + calc_fn_hash(empty(), "", 0, args.iter().map(|a| a.type_id())); // 3) The final hash is the XOR of the two hashes. let hash_fn_native = *hash_fn_def ^ hash_fn_args; diff --git a/src/module.rs b/src/module.rs index 06d0cbbd..59a0d0f1 100644 --- a/src/module.rs +++ b/src/module.rs @@ -8,18 +8,22 @@ use crate::fn_native::{ CallableFunction::{Method, Pure}, FnCallArgs, IteratorFn, SharedFunction, }; -use crate::parser::{FnAccess, AST}; +use crate::parser::{ + FnAccess, + FnAccess::{Private, Public}, + AST, +}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token}; -use crate::utils::{StaticVec, EMPTY_TYPE_ID}; +use crate::utils::StaticVec; use crate::stdlib::{ any::TypeId, boxed::Box, collections::HashMap, fmt, - iter::{empty, repeat}, + iter::empty, mem, num::NonZeroUsize, ops::{Deref, DerefMut}, @@ -30,9 +34,6 @@ use crate::stdlib::{ vec::Vec, }; -/// Default function access mode. -const DEF_ACCESS: FnAccess = FnAccess::Public; - /// Return type of module-level Rust function. pub type FuncReturn = Result>; @@ -281,7 +282,7 @@ impl Module { params: &[TypeId], func: CallableFunction, ) -> u64 { - let hash_fn = calc_fn_hash(empty(), &name, params.iter().cloned()); + let hash_fn = calc_fn_hash(empty(), &name, params.len(), params.iter().cloned()); let params = params.into_iter().cloned().collect(); @@ -312,7 +313,7 @@ impl Module { ) -> u64 { let f = move |_: &mut FnCallArgs| func().map(Dynamic::from); let arg_types = []; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -337,7 +338,7 @@ impl Module { let f = move |args: &mut FnCallArgs| func(mem::take(args[0]).cast::()).map(Dynamic::from); let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -363,7 +364,7 @@ impl Module { func(args[0].downcast_mut::().unwrap()).map(Dynamic::from) }; let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -394,7 +395,7 @@ impl Module { func(a, b).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -429,7 +430,7 @@ impl Module { func(a, b).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -467,7 +468,7 @@ impl Module { func(a, b, c).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -506,7 +507,7 @@ impl Module { func(a, b, c).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) } /// Get a Rust function. @@ -617,27 +618,24 @@ impl Module { // Index all variables for (var_name, value) in &module.variables { // Qualifiers + variable name - let hash_var = calc_fn_hash(qualifiers.iter().map(|&v| v), var_name, empty()); + let hash_var = calc_fn_hash(qualifiers.iter().map(|&v| v), var_name, 0, empty()); variables.push((hash_var, value.clone())); } // Index all Rust functions for (name, access, params, func) in module.functions.values() { match access { // Private functions are not exported - FnAccess::Private => continue, - FnAccess::Public => (), + Private => continue, + Public => (), } // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, - // i.e. qualifiers + function name + dummy parameter types (one for each parameter). - let hash_fn_def = calc_fn_hash( - qualifiers.iter().map(|&v| v), - name, - repeat(EMPTY_TYPE_ID()).take(params.len()), - ); - // 2) Calculate a second hash with no qualifiers, empty function name, and - // the actual list of parameter `TypeId`'.s - let hash_fn_args = calc_fn_hash(empty(), "", params.iter().cloned()); + // i.e. qualifiers + function name + number of arguments. + let hash_fn_def = + calc_fn_hash(qualifiers.iter().map(|&v| v), name, params.len(), empty()); + // 2) Calculate a second hash with no qualifiers, empty function name, + // zero number of arguments, and the actual list of argument `TypeId`'.s + let hash_fn_args = calc_fn_hash(empty(), "", 0, params.iter().cloned()); // 3) The final hash is the XOR of the two hashes. let hash_fn_native = hash_fn_def ^ hash_fn_args; @@ -647,14 +645,15 @@ impl Module { for fn_def in module.fn_lib.values() { match fn_def.access { // Private functions are not exported - FnAccess::Private => continue, - DEF_ACCESS => (), + Private => continue, + Public => (), } - // Qualifiers + function name + placeholders (one for each parameter) + // Qualifiers + function name + number of arguments. let hash_fn_def = calc_fn_hash( qualifiers.iter().map(|&v| v), &fn_def.name, - repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()), + fn_def.params.len(), + empty(), ); functions.push((hash_fn_def, CallableFunction::Script(fn_def.clone()).into())); } diff --git a/src/optimize.rs b/src/optimize.rs index d320d4bf..1bba1196 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -119,7 +119,12 @@ fn call_fn( pos: Position, ) -> Result, Box> { // Search built-in's and external functions - let hash_fn = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); + let hash_fn = calc_fn_hash( + empty(), + fn_name, + args.len(), + args.iter().map(|a| a.type_id()), + ); global_module .get_fn(hash_fn) diff --git a/src/parser.rs b/src/parser.rs index 39fa6678..1c013666 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -7,7 +7,7 @@ use crate::error::{LexError, ParseError, ParseErrorType}; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token, TokenIterator}; -use crate::utils::{StaticVec, EMPTY_TYPE_ID}; +use crate::utils::StaticVec; #[cfg(not(feature = "no_module"))] use crate::module::ModuleRef; @@ -22,7 +22,7 @@ use crate::stdlib::{ char, collections::HashMap, format, - iter::{empty, repeat, Peekable}, + iter::{empty, Peekable}, num::NonZeroUsize, ops::{Add, Deref, DerefMut}, rc::Rc, @@ -767,13 +767,14 @@ fn parse_call_expr<'a>( // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, - // i.e. qualifiers + function name + no parameters. - // 2) Calculate a second hash with no qualifiers, empty function name, and - // the actual list of parameter `TypeId`'.s + // 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. - calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()) + let qualifiers = modules.iter().map(|(m, _)| m.as_str()); + calc_fn_hash(qualifiers, &id, 0, empty()) } else { - calc_fn_hash(empty(), &id, empty()) + calc_fn_hash(empty(), &id, 0, empty()) } }; // Qualifiers (none) + function name + no parameters. @@ -799,7 +800,6 @@ fn parse_call_expr<'a>( // id(...args) (Token::RightParen, _) => { eat_token(input, Token::RightParen); - let args_iter = repeat(EMPTY_TYPE_ID()).take(args.len()); #[cfg(not(feature = "no_module"))] let hash_fn_def = { @@ -808,13 +808,14 @@ fn parse_call_expr<'a>( // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, - // i.e. qualifiers + function name + dummy parameter types (one for each parameter). - // 2) Calculate a second hash with no qualifiers, empty function name, and - // the actual list of parameter `TypeId`'.s + // 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. - calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, args_iter) + let qualifiers = modules.iter().map(|(m, _)| m.as_str()); + calc_fn_hash(qualifiers, &id, args.len(), empty()) } else { - calc_fn_hash(empty(), &id, args_iter) + calc_fn_hash(empty(), &id, args.len(), empty()) } }; // Qualifiers (none) + function name + dummy parameter types (one for each parameter). @@ -1297,7 +1298,7 @@ fn parse_primary<'a>( let modules = modules.as_mut().unwrap(); // Qualifiers + variable name - *hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, empty()); + *hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, 0, empty()); modules.set_index(state.find_module(&modules.get(0).0)); } _ => (), @@ -1362,7 +1363,7 @@ fn parse_unary<'a>( // Call negative function expr => { let op = "-"; - let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); + let hash = calc_fn_hash(empty(), op, 2, empty()); let mut args = StaticVec::new(); args.push(expr); @@ -1388,7 +1389,7 @@ fn parse_unary<'a>( args.push(parse_primary(input, state, level + 1, allow_stmt_expr)?); let op = "!"; - let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); + let hash = calc_fn_hash(empty(), op, 2, empty()); Ok(Expr::FnCall(Box::new(( (op.into(), pos), @@ -1492,7 +1493,7 @@ fn parse_op_assignment_stmt<'a>( args.push(lhs_copy); args.push(rhs); - let hash = calc_fn_hash(empty(), &op, repeat(EMPTY_TYPE_ID()).take(args.len())); + let hash = calc_fn_hash(empty(), &op, args.len(), empty()); let rhs_expr = Expr::FnCall(Box::new(((op, pos), None, hash, args, None))); make_assignment_stmt(state, lhs, rhs_expr, pos) @@ -1780,7 +1781,7 @@ fn parse_binary_op<'a>( let cmp_def = Some(false.into()); let op = op_token.syntax(); - let hash = calc_fn_hash(empty(), &op, repeat(EMPTY_TYPE_ID()).take(2)); + let hash = calc_fn_hash(empty(), &op, 2, empty()); let mut args = StaticVec::new(); args.push(root); @@ -1839,8 +1840,7 @@ fn parse_binary_op<'a>( Expr::FnCall(x) => { let ((id, _), _, hash, args, _) = x.as_mut(); // Recalculate function call hash because there is an additional argument - let args_iter = repeat(EMPTY_TYPE_ID()).take(args.len() + 1); - *hash = calc_fn_hash(empty(), id, args_iter); + *hash = calc_fn_hash(empty(), id, args.len() + 1, empty()); } _ => (), } @@ -2540,12 +2540,8 @@ fn parse_global_level<'a>( let mut state = ParseState::new(max_expr_depth.1); let func = parse_fn(input, &mut state, access, 0, true)?; - // Qualifiers (none) + function name + argument `TypeId`'s - let hash = calc_fn_hash( - empty(), - &func.name, - repeat(EMPTY_TYPE_ID()).take(func.params.len()), - ); + // Qualifiers (none) + function name + number of arguments. + let hash = calc_fn_hash(empty(), &func.name, func.params.len(), empty()); functions.insert(hash, func); continue; diff --git a/src/utils.rs b/src/utils.rs index a8f73390..126f6809 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -21,11 +21,6 @@ use crate::stdlib::collections::hash_map::DefaultHasher; #[cfg(feature = "no_std")] use ahash::AHasher; -#[inline(always)] -pub fn EMPTY_TYPE_ID() -> TypeId { - TypeId::of::<()>() -} - /// Calculate a `u64` hash key from a module-qualified function name and parameter types. /// /// Module names are passed in via `&str` references from an iterator. @@ -37,6 +32,7 @@ pub fn EMPTY_TYPE_ID() -> TypeId { pub fn calc_fn_spec<'a>( modules: impl Iterator, fn_name: &str, + num: usize, params: impl Iterator, ) -> u64 { #[cfg(feature = "no_std")] @@ -47,6 +43,7 @@ pub fn calc_fn_spec<'a>( // We always skip the first module modules.skip(1).for_each(|m| m.hash(&mut s)); s.write(fn_name.as_bytes()); + s.write_usize(num); params.for_each(|t| t.hash(&mut s)); s.finish() } From 4a1fd66b9f1d5d1e2f357aba56748b10095a9cbf Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 May 2020 22:25:57 +0800 Subject: [PATCH 06/53] Reduce Rc/Arc wrapping for functions. --- src/engine.rs | 6 ++-- src/fn_native.rs | 83 +++++++++++++++++++++++--------------------- src/fn_register.rs | 23 ++++++------- src/module.rs | 84 +++++++++++++++++---------------------------- src/packages/mod.rs | 4 +-- src/parser.rs | 35 ++++--------------- 6 files changed, 97 insertions(+), 138 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index d45d1099..cb3b0e8c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,11 +3,11 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::fn_native::{FnCallArgs, PrintCallback, ProgressCallback}; +use crate::fn_native::{FnCallArgs, PrintCallback, ProgressCallback, SharedFnDef}; use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; -use crate::parser::{Expr, FnAccess, FnDef, ReturnType, SharedFnDef, Stmt, AST}; +use crate::parser::{Expr, FnAccess, FnDef, ReturnType, Stmt, AST}; use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; @@ -1741,7 +1741,7 @@ impl Engine { let index = scope.len() - 1; state.scope_level += 1; - for loop_var in func.get_iter_fn()(iter_type) { + for loop_var in func(iter_type) { *scope.get_mut(index).0 = loop_var; self.inc_operations(state, stmt.position())?; diff --git a/src/fn_native.rs b/src/fn_native.rs index 328372b9..619fb102 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -1,5 +1,5 @@ use crate::any::Dynamic; -use crate::parser::{FnDef, SharedFnDef}; +use crate::parser::FnDef; use crate::result::EvalAltResult; use crate::stdlib::{boxed::Box, rc::Rc, sync::Arc}; @@ -73,15 +73,31 @@ pub trait IteratorCallback: Fn(Dynamic) -> Box> + ' #[cfg(not(feature = "sync"))] impl Box> + 'static> IteratorCallback for F {} +#[cfg(not(feature = "sync"))] +pub type SharedNativeFunction = Rc; +#[cfg(feature = "sync")] +pub type SharedNativeFunction = Arc; + +#[cfg(not(feature = "sync"))] +pub type SharedIteratorFn = Rc; +#[cfg(feature = "sync")] +pub type SharedIteratorFn = Arc; + +#[cfg(feature = "sync")] +pub type SharedFnDef = Arc; +#[cfg(not(feature = "sync"))] +pub type SharedFnDef = Rc; + /// A type encapsulating a function callable by Rhai. +#[derive(Clone)] pub enum CallableFunction { /// A pure native Rust function with all arguments passed by value. - Pure(Box), + Pure(SharedNativeFunction), /// A native Rust object method with the first argument passed by reference, /// and the rest passed by value. - Method(Box), + Method(SharedNativeFunction), /// An iterator function. - Iterator(Box), + Iterator(SharedIteratorFn), /// A script-defined function. Script(SharedFnDef), } @@ -90,37 +106,29 @@ impl CallableFunction { /// Is this a pure native Rust function? pub fn is_pure(&self) -> bool { match self { - CallableFunction::Pure(_) => true, - CallableFunction::Method(_) - | CallableFunction::Iterator(_) - | CallableFunction::Script(_) => false, + Self::Pure(_) => true, + Self::Method(_) | Self::Iterator(_) | Self::Script(_) => false, } } /// Is this a pure native Rust method-call? pub fn is_method(&self) -> bool { match self { - CallableFunction::Method(_) => true, - CallableFunction::Pure(_) - | CallableFunction::Iterator(_) - | CallableFunction::Script(_) => false, + Self::Method(_) => true, + Self::Pure(_) | Self::Iterator(_) | Self::Script(_) => false, } } /// Is this an iterator function? pub fn is_iter(&self) -> bool { match self { - CallableFunction::Iterator(_) => true, - CallableFunction::Pure(_) - | CallableFunction::Method(_) - | CallableFunction::Script(_) => false, + Self::Iterator(_) => true, + Self::Pure(_) | Self::Method(_) | Self::Script(_) => false, } } /// Is this a Rhai-scripted function? pub fn is_script(&self) -> bool { match self { - CallableFunction::Script(_) => true, - CallableFunction::Pure(_) - | CallableFunction::Method(_) - | CallableFunction::Iterator(_) => false, + Self::Script(_) => true, + Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => false, } } /// Get a reference to a native Rust function. @@ -128,10 +136,10 @@ impl CallableFunction { /// # Panics /// /// Panics if the `CallableFunction` is not `Pure` or `Method`. - pub fn get_native_fn(&self) -> &Box { + pub fn get_native_fn(&self) -> &FnAny { match self { - CallableFunction::Pure(f) | CallableFunction::Method(f) => f, - CallableFunction::Iterator(_) | CallableFunction::Script(_) => panic!(), + Self::Pure(f) | Self::Method(f) => f.as_ref(), + Self::Iterator(_) | Self::Script(_) => panic!(), } } /// Get a reference to a script-defined function definition. @@ -141,10 +149,8 @@ impl CallableFunction { /// Panics if the `CallableFunction` is not `Script`. pub fn get_fn_def(&self) -> &FnDef { match self { - CallableFunction::Pure(_) - | CallableFunction::Method(_) - | CallableFunction::Iterator(_) => panic!(), - CallableFunction::Script(f) => f, + Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => panic!(), + Self::Script(f) => f, } } /// Get a reference to an iterator function. @@ -152,19 +158,18 @@ impl CallableFunction { /// # Panics /// /// Panics if the `CallableFunction` is not `Iterator`. - pub fn get_iter_fn(&self) -> &Box { + pub fn get_iter_fn(&self) -> &IteratorFn { match self { - CallableFunction::Pure(_) - | CallableFunction::Method(_) - | CallableFunction::Script(_) => panic!(), - CallableFunction::Iterator(f) => f, + Self::Iterator(f) => f.as_ref(), + Self::Pure(_) | Self::Method(_) | Self::Script(_) => panic!(), } } + /// Create a new `CallableFunction::Pure`. + pub fn from_pure(func: Box) -> Self { + Self::Pure(func.into()) + } + /// Create a new `CallableFunction::Method`. + pub fn from_method(func: Box) -> Self { + Self::Method(func.into()) + } } - -/// A callable function. -#[cfg(not(feature = "sync"))] -pub type SharedFunction = Rc; -/// A callable function. -#[cfg(feature = "sync")] -pub type SharedFunction = Arc; diff --git a/src/fn_register.rs b/src/fn_register.rs index 9636e557..46dbd166 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -4,10 +4,7 @@ use crate::any::{Dynamic, Variant}; use crate::engine::Engine; -use crate::fn_native::{ - CallableFunction::{Method, Pure}, - FnCallArgs, -}; +use crate::fn_native::{CallableFunction, FnAny, FnCallArgs}; use crate::parser::FnAccess; use crate::result::EvalAltResult; @@ -158,7 +155,7 @@ macro_rules! make_func { // Map the result $map(r) - }) + }) as Box }; } @@ -184,7 +181,7 @@ pub fn map_result( macro_rules! def_register { () => { - def_register!(imp Pure :); + def_register!(imp from_pure :); }; (imp $abi:ident : $($par:ident => $mark:ty => $param:ty => $clone:expr),*) => { // ^ function ABI type @@ -207,7 +204,7 @@ macro_rules! def_register { fn register_fn(&mut self, name: &str, f: FN) { self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - $abi(make_func!(f : map_dynamic ; $($par => $clone),*)) + CallableFunction::$abi(make_func!(f : map_dynamic ; $($par => $clone),*)) ); } } @@ -225,7 +222,7 @@ macro_rules! def_register { fn register_dynamic_fn(&mut self, name: &str, f: FN) { self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - $abi(make_func!(f : map_identity ; $($par => $clone),*)) + CallableFunction::$abi(make_func!(f : map_identity ; $($par => $clone),*)) ); } } @@ -244,7 +241,7 @@ macro_rules! def_register { fn register_result_fn(&mut self, name: &str, f: FN) { self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - $abi(make_func!(f : map_result ; $($par => $clone),*)) + CallableFunction::$abi(make_func!(f : map_result ; $($par => $clone),*)) ); } } @@ -252,11 +249,11 @@ macro_rules! def_register { //def_register!(imp_pop $($par => $mark => $param),*); }; ($p0:ident $(, $p:ident)*) => { - def_register!(imp Pure : $p0 => $p0 => $p0 => by_value $(, $p => $p => $p => by_value)*); - def_register!(imp Method : $p0 => Mut<$p0> => &mut $p0 => by_ref $(, $p => $p => $p => by_value)*); + def_register!(imp from_pure : $p0 => $p0 => $p0 => by_value $(, $p => $p => $p => by_value)*); + def_register!(imp from_method : $p0 => Mut<$p0> => &mut $p0 => by_ref $(, $p => $p => $p => by_value)*); // ^ CallableFunction - // handle the first parameter ^ first parameter passed through - // ^ others passed by value (by_value) + // handle the first parameter ^ first parameter passed through + // ^ others passed by value (by_value) // Currently does not support first argument which is a reference, as there will be // conflicting implementations since &T: Any and T: Any cannot be distinguished diff --git a/src/module.rs b/src/module.rs index 59a0d0f1..5e751df8 100644 --- a/src/module.rs +++ b/src/module.rs @@ -3,11 +3,7 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; -use crate::fn_native::{ - CallableFunction, - CallableFunction::{Method, Pure}, - FnCallArgs, IteratorFn, SharedFunction, -}; +use crate::fn_native::{CallableFunction as CF, FnCallArgs, IteratorFn, SharedIteratorFn}; use crate::parser::{ FnAccess, FnAccess::{Private, Public}, @@ -27,9 +23,7 @@ use crate::stdlib::{ mem, num::NonZeroUsize, ops::{Deref, DerefMut}, - rc::Rc, string::{String, ToString}, - sync::Arc, vec, vec::Vec, }; @@ -53,17 +47,17 @@ pub struct Module { all_variables: HashMap, /// External Rust functions. - functions: HashMap, SharedFunction)>, + functions: HashMap, CF)>, /// Script-defined functions. fn_lib: FunctionsLib, /// Iterator functions, keyed by the type producing the iterator. - type_iterators: HashMap, + type_iterators: HashMap, /// Flattened collection of all external Rust functions, native or scripted, /// including those in sub-modules. - all_functions: HashMap, + all_functions: HashMap, } impl fmt::Debug for Module { @@ -275,13 +269,7 @@ impl Module { /// Set a Rust function into the module, returning a hash key. /// /// If there is an existing Rust function of the same hash, it is replaced. - pub fn set_fn( - &mut self, - name: String, - access: FnAccess, - params: &[TypeId], - func: CallableFunction, - ) -> u64 { + pub fn set_fn(&mut self, name: String, access: FnAccess, params: &[TypeId], func: CF) -> u64 { let hash_fn = calc_fn_hash(empty(), &name, params.len(), params.iter().cloned()); let params = params.into_iter().cloned().collect(); @@ -312,8 +300,8 @@ impl Module { #[cfg(feature = "sync")] func: impl Fn() -> FuncReturn + Send + Sync + 'static, ) -> u64 { let f = move |_: &mut FnCallArgs| func().map(Dynamic::from); - let arg_types = []; - self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) + let args = []; + self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -337,8 +325,8 @@ impl Module { ) -> u64 { let f = move |args: &mut FnCallArgs| func(mem::take(args[0]).cast::()).map(Dynamic::from); - let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) + let args = [TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -363,8 +351,8 @@ impl Module { let f = move |args: &mut FnCallArgs| { func(args[0].downcast_mut::().unwrap()).map(Dynamic::from) }; - let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) + let args = [TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_method(Box::new(f))) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -394,8 +382,8 @@ impl Module { func(a, b).map(Dynamic::from) }; - let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) + let args = [TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -429,8 +417,8 @@ impl Module { func(a, b).map(Dynamic::from) }; - let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) + let args = [TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_method(Box::new(f))) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -467,8 +455,8 @@ impl Module { func(a, b, c).map(Dynamic::from) }; - let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) + let args = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -506,8 +494,8 @@ impl Module { func(a, b, c).map(Dynamic::from) }; - let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) + let args = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_method(Box::new(f))) } /// Get a Rust function. @@ -524,8 +512,8 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> { - self.functions.get(&hash_fn).map(|(_, _, _, v)| v.as_ref()) + pub fn get_fn(&self, hash_fn: u64) -> Option<&CF> { + self.functions.get(&hash_fn).map(|(_, _, _, v)| v) } /// Get a modules-qualified function. @@ -536,16 +524,13 @@ impl Module { &mut self, name: &str, hash_fn_native: u64, - ) -> Result<&CallableFunction, Box> { - self.all_functions - .get(&hash_fn_native) - .map(|f| f.as_ref()) - .ok_or_else(|| { - Box::new(EvalAltResult::ErrorFunctionNotFound( - name.to_string(), - Position::none(), - )) - }) + ) -> Result<&CF, Box> { + self.all_functions.get(&hash_fn_native).ok_or_else(|| { + Box::new(EvalAltResult::ErrorFunctionNotFound( + name.to_string(), + Position::none(), + )) + }) } /// Create a new `Module` by evaluating an `AST`. @@ -606,7 +591,7 @@ impl Module { module: &'a Module, qualifiers: &mut Vec<&'a str>, variables: &mut Vec<(u64, Dynamic)>, - functions: &mut Vec<(u64, SharedFunction)>, + functions: &mut Vec<(u64, CF)>, ) { for (name, m) in &module.modules { // Index all the sub-modules first. @@ -655,7 +640,7 @@ impl Module { fn_def.params.len(), empty(), ); - functions.push((hash_fn_def, CallableFunction::Script(fn_def.clone()).into())); + functions.push((hash_fn_def, CF::Script(fn_def.clone()).into())); } } @@ -675,16 +660,11 @@ impl Module { /// Set a type iterator into the module. pub fn set_iter(&mut self, typ: TypeId, func: Box) { - #[cfg(not(feature = "sync"))] - self.type_iterators - .insert(typ, Rc::new(CallableFunction::Iterator(func))); - #[cfg(feature = "sync")] - self.type_iterators - .insert(typ, Arc::new(CallableFunction::Iterator(func))); + self.type_iterators.insert(typ, func.into()); } /// Get the specified type iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&CallableFunction> { + pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { self.type_iterators.get(&id).map(|v| v.as_ref()) } } diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 39ce601c..3e8a4f04 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -1,6 +1,6 @@ //! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages. -use crate::fn_native::CallableFunction; +use crate::fn_native::{CallableFunction, IteratorFn}; use crate::module::Module; use crate::utils::StaticVec; @@ -81,7 +81,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_iter(id)) } /// Get the specified TypeId iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&CallableFunction> { + pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { self.packages .iter() .map(|p| p.get_iter(id)) diff --git a/src/parser.rs b/src/parser.rs index 1c013666..e03f3686 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -25,9 +25,7 @@ use crate::stdlib::{ iter::{empty, Peekable}, num::NonZeroUsize, ops::{Add, Deref, DerefMut}, - rc::Rc, string::{String, ToString}, - sync::Arc, vec, vec::Vec, }; @@ -59,21 +57,14 @@ type PERR = ParseErrorType; pub struct AST( /// Global statements. Vec, - /// Script-defined functions, wrapped in an `Arc` for shared access. - #[cfg(feature = "sync")] - Arc, - /// Script-defined functions, wrapped in an `Rc` for shared access. - #[cfg(not(feature = "sync"))] - Rc, + /// Script-defined functions. + FunctionsLib, ); impl AST { /// Create a new `AST`. pub fn new(statements: Vec, fn_lib: FunctionsLib) -> Self { - #[cfg(feature = "sync")] - return Self(statements, Arc::new(fn_lib)); - #[cfg(not(feature = "sync"))] - return Self(statements, Rc::new(fn_lib)); + Self(statements, fn_lib) } /// Get the statements. @@ -88,7 +79,7 @@ impl AST { /// Get the script-defined functions. pub(crate) fn fn_lib(&self) -> &FunctionsLib { - self.1.as_ref() + &self.1 } /// Merge two `AST` into one. Both `AST`'s are untouched and a new, merged, version @@ -147,20 +138,13 @@ impl AST { (true, true) => vec![], }; - Self::new(ast, functions.merge(other.1.as_ref())) + Self::new(ast, functions.merge(&other.1)) } /// Clear all function definitions in the `AST`. #[cfg(not(feature = "no_function"))] pub fn clear_functions(&mut self) { - #[cfg(feature = "sync")] - { - self.1 = Arc::new(Default::default()); - } - #[cfg(not(feature = "sync"))] - { - self.1 = Rc::new(Default::default()); - } + self.1 = Default::default(); } /// Clear all statements in the `AST`, leaving only function definitions. @@ -202,13 +186,6 @@ pub struct FnDef { pub pos: Position, } -/// A sharable script-defined function. -#[cfg(feature = "sync")] -pub type SharedFnDef = Arc; -/// A sharable script-defined function. -#[cfg(not(feature = "sync"))] -pub type SharedFnDef = Rc; - /// `return`/`throw` statement. #[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] pub enum ReturnType { From fad60c0a7d1a7abd974d5e1f046345af30cf49ae Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 May 2020 00:06:07 +0800 Subject: [PATCH 07/53] Bump version. --- Cargo.toml | 2 +- README.md | 4 ++-- RELEASES.md | 7 ++++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 68837a2d..981f1241 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rhai" -version = "0.14.1" +version = "0.14.2" edition = "2018" authors = ["Jonathan Turner", "Lukáš Hozda", "Stephen Chung"] description = "Embedded scripting for Rust" diff --git a/README.md b/README.md index f6db9187..b824d178 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Rhai's current features set: to do checked arithmetic operations); for [`no-std`](#optional-features) builds, a number of additional dependencies are pulled in to provide for functionalities that used to be in `std`. -**Note:** Currently, the version is 0.14.1, so the language and API's may change before they stabilize. +**Note:** Currently, the version is 0.14.2, so the language and API's may change before they stabilize. Installation ------------ @@ -45,7 +45,7 @@ Install the Rhai crate by adding this line to `dependencies`: ```toml [dependencies] -rhai = "0.14.1" +rhai = "0.14.2" ``` Use the latest released crate version on [`crates.io`](https::/crates.io/crates/rhai/): diff --git a/RELEASES.md b/RELEASES.md index 825105a8..8d01f9f5 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,9 +1,14 @@ Rhai Release Notes ================== -Version 0.15.0 +Version 0.14.2 ============== +Regression +---------- + +* Do not optimize script with `eval_expression` - it is assumed to be one-off and short. + New features ------------ From 5db1fd37122ee587da732300ed48f79535d95e1a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 May 2020 00:06:19 +0800 Subject: [PATCH 08/53] Do not optimize eval_expression scripts. --- src/api.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api.rs b/src/api.rs index f0e249e9..874aaa4e 100644 --- a/src/api.rs +++ b/src/api.rs @@ -824,9 +824,10 @@ impl Engine { &mut stream.peekable(), self, scope, - self.optimization_level, + OptimizationLevel::None, // No need to optimize a lone expression self.max_expr_depth, )?; + self.eval_ast_with_scope(scope, &ast) } From c98633dd2bf3761a29719ede3b03efe85305dedd Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 May 2020 11:12:22 +0800 Subject: [PATCH 09/53] Add EvalPackage. --- README.md | 10 +++++++++- src/packages/eval.rs | 12 ++++++++++++ src/packages/mod.rs | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/packages/eval.rs diff --git a/README.md b/README.md index b824d178..8673aba5 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ Disable script-defined functions (`no_function`) only when the feature is not ne [`Engine::new_raw`](#raw-engine) creates a _raw_ engine which does not register _any_ utility functions. This makes the scripting language quite useless as even basic arithmetic operators are not supported. -Selectively include the necessary functionalities by loading specific [packages](#packages) to minimize the footprint. +Selectively include the necessary functionalities by loading specific [packages] to minimize the footprint. Packages are sharable (even across threads via the [`sync`] feature), so they only have to be created once. Related @@ -380,6 +380,9 @@ Use `Engine::new_raw` to create a _raw_ `Engine`, in which _nothing_ is added, n ### Packages +[package]: #packages +[packages]: #packages + Rhai functional features are provided in different _packages_ that can be loaded via a call to `Engine::load_package`. Packages reside under `rhai::packages::*` and the trait `rhai::packages::Package` must be loaded in order for packages to be used. @@ -408,6 +411,7 @@ The follow packages are available: | `BasicMathPackage` | Basic math functions (e.g. `sin`, `sqrt`) | No | Yes | | `BasicArrayPackage` | Basic [array] functions | No | Yes | | `BasicMapPackage` | Basic [object map] functions | No | Yes | +| `EvalPackage` | Disable [`eval`] | No | No | | `CorePackage` | Basic essentials | | | | `StandardPackage` | Standard library | | | @@ -2669,6 +2673,8 @@ engine.set_optimization_level(rhai::OptimizationLevel::None); `eval` - or "How to Shoot Yourself in the Foot even Easier" --------------------------------------------------------- +[`eval`]: #eval---or-how-to-shoot-yourself-in-the-foot-even-easier + Saving the best for last: in addition to script optimizations, there is the ever-dreaded... `eval` function! ```rust @@ -2730,3 +2736,5 @@ fn alt_eval(script: String) -> Result<(), Box> { engine.register_result_fn("eval", alt_eval); ``` + +There is even a [package] named `EvalPackage` which implements the disabling override. diff --git a/src/packages/eval.rs b/src/packages/eval.rs new file mode 100644 index 00000000..a6756d96 --- /dev/null +++ b/src/packages/eval.rs @@ -0,0 +1,12 @@ +use crate::def_package; +use crate::module::FuncReturn; +use crate::stdlib::string::String; + +def_package!(crate:EvalPackage:"Disable 'eval'.", lib, { + lib.set_fn_1_mut( + "eval", + |_: &mut String| -> FuncReturn<()> { + Err("eval is evil!".into()) + }, + ); +}); diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 3e8a4f04..62df57a6 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -8,6 +8,7 @@ use crate::stdlib::{any::TypeId, boxed::Box, collections::HashMap, rc::Rc, sync: mod arithmetic; mod array_basic; +mod eval; mod iter_basic; mod logic; mod map_basic; @@ -21,6 +22,7 @@ mod time_basic; pub use arithmetic::ArithmeticPackage; #[cfg(not(feature = "no_index"))] pub use array_basic::BasicArrayPackage; +pub use eval::EvalPackage; pub use iter_basic::BasicIteratorPackage; pub use logic::LogicPackage; #[cfg(not(feature = "no_object"))] From 55ee4d6a199bc8f19b6808cf476c7b93fee72224 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 May 2020 11:12:35 +0800 Subject: [PATCH 10/53] More benchmarks. --- benches/eval_expression.rs | 64 ++++++++++++++++++++++++++++++++++++++ benches/parsing.rs | 32 +++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/benches/eval_expression.rs b/benches/eval_expression.rs index 131d3bb5..c647c1b5 100644 --- a/benches/eval_expression.rs +++ b/benches/eval_expression.rs @@ -41,3 +41,67 @@ fn bench_eval_expression_number_operators(bench: &mut Bencher) { bench.iter(|| engine.consume_ast(&ast).unwrap()); } + +#[bench] +fn bench_eval_expression_optimized_simple(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::Simple); + let ast = engine.compile_expression(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_expression_optimized_full(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::Full); + let ast = engine.compile_expression(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_call_expression(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + + bench.iter(|| engine.eval_expression::(script).unwrap()); +} + +#[bench] +fn bench_eval_call(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + + bench.iter(|| engine.eval::(script).unwrap()); +} diff --git a/benches/parsing.rs b/benches/parsing.rs index c4486bd5..01011877 100644 --- a/benches/parsing.rs +++ b/benches/parsing.rs @@ -102,3 +102,35 @@ fn bench_parse_primes(bench: &mut Bencher) { bench.iter(|| engine.compile(script).unwrap()); } + +#[bench] +fn bench_parse_optimize_simple(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::Simple); + + bench.iter(|| engine.compile_expression(script).unwrap()); +} + +#[bench] +fn bench_parse_optimize_full(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::Full); + + bench.iter(|| engine.compile_expression(script).unwrap()); +} From 80fcc40710fb9607b949a31a8b24eb48ee05bae9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 May 2020 19:27:23 +0800 Subject: [PATCH 11/53] Use function pointers for iterators. --- src/api.rs | 8 +++----- src/fn_native.rs | 32 ++++---------------------------- src/module.rs | 12 ++++++------ src/packages/array_basic.rs | 4 +--- src/packages/iter_basic.rs | 22 ++++++++-------------- src/packages/mod.rs | 2 +- 6 files changed, 23 insertions(+), 57 deletions(-) diff --git a/src/api.rs b/src/api.rs index 874aaa4e..48052e85 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,9 +4,7 @@ use crate::any::{Dynamic, Variant}; use crate::engine::{make_getter, make_setter, Engine, State, FUNC_INDEXER}; use crate::error::ParseError; use crate::fn_call::FuncArgs; -use crate::fn_native::{ - IteratorCallback, ObjectGetCallback, ObjectIndexerCallback, ObjectSetCallback, -}; +use crate::fn_native::{IteratorFn, ObjectGetCallback, ObjectIndexerCallback, ObjectSetCallback}; use crate::fn_register::RegisterFn; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::parser::{parse, parse_global_expr, AST}; @@ -123,8 +121,8 @@ impl Engine { /// Register an iterator adapter for a type with the `Engine`. /// This is an advanced feature. - pub fn register_iterator(&mut self, f: F) { - self.global_module.set_iter(TypeId::of::(), Box::new(f)); + pub fn register_iterator(&mut self, f: IteratorFn) { + self.global_module.set_iter(TypeId::of::(), f); } /// Register a getter function for a member of a registered type with the `Engine`. diff --git a/src/fn_native.rs b/src/fn_native.rs index 619fb102..262bad7c 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -11,10 +11,7 @@ pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result> #[cfg(not(feature = "sync"))] pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result>; -#[cfg(feature = "sync")] -pub type IteratorFn = dyn Fn(Dynamic) -> Box> + Send + Sync; -#[cfg(not(feature = "sync"))] -pub type IteratorFn = dyn Fn(Dynamic) -> Box>; +pub type IteratorFn = fn(Dynamic) -> Box>; #[cfg(feature = "sync")] pub type PrintCallback = dyn Fn(&str) + Send + Sync + 'static; @@ -57,32 +54,11 @@ pub trait ObjectIndexerCallback: Fn(&mut T, X) -> U + 'static {} #[cfg(not(feature = "sync"))] impl U + 'static, T, X, U> ObjectIndexerCallback for F {} -#[cfg(feature = "sync")] -pub trait IteratorCallback: - Fn(Dynamic) -> Box> + Send + Sync + 'static -{ -} -#[cfg(feature = "sync")] -impl Box> + Send + Sync + 'static> IteratorCallback - for F -{ -} - -#[cfg(not(feature = "sync"))] -pub trait IteratorCallback: Fn(Dynamic) -> Box> + 'static {} -#[cfg(not(feature = "sync"))] -impl Box> + 'static> IteratorCallback for F {} - #[cfg(not(feature = "sync"))] pub type SharedNativeFunction = Rc; #[cfg(feature = "sync")] pub type SharedNativeFunction = Arc; -#[cfg(not(feature = "sync"))] -pub type SharedIteratorFn = Rc; -#[cfg(feature = "sync")] -pub type SharedIteratorFn = Arc; - #[cfg(feature = "sync")] pub type SharedFnDef = Arc; #[cfg(not(feature = "sync"))] @@ -97,7 +73,7 @@ pub enum CallableFunction { /// and the rest passed by value. Method(SharedNativeFunction), /// An iterator function. - Iterator(SharedIteratorFn), + Iterator(IteratorFn), /// A script-defined function. Script(SharedFnDef), } @@ -158,9 +134,9 @@ impl CallableFunction { /// # Panics /// /// Panics if the `CallableFunction` is not `Iterator`. - pub fn get_iter_fn(&self) -> &IteratorFn { + pub fn get_iter_fn(&self) -> IteratorFn { match self { - Self::Iterator(f) => f.as_ref(), + Self::Iterator(f) => *f, Self::Pure(_) | Self::Method(_) | Self::Script(_) => panic!(), } } diff --git a/src/module.rs b/src/module.rs index 5e751df8..48e6e936 100644 --- a/src/module.rs +++ b/src/module.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; -use crate::fn_native::{CallableFunction as CF, FnCallArgs, IteratorFn, SharedIteratorFn}; +use crate::fn_native::{CallableFunction as CF, FnCallArgs, IteratorFn}; use crate::parser::{ FnAccess, FnAccess::{Private, Public}, @@ -53,7 +53,7 @@ pub struct Module { fn_lib: FunctionsLib, /// Iterator functions, keyed by the type producing the iterator. - type_iterators: HashMap, + type_iterators: HashMap, /// Flattened collection of all external Rust functions, native or scripted, /// including those in sub-modules. @@ -659,13 +659,13 @@ impl Module { } /// Set a type iterator into the module. - pub fn set_iter(&mut self, typ: TypeId, func: Box) { - self.type_iterators.insert(typ, func.into()); + pub fn set_iter(&mut self, typ: TypeId, func: IteratorFn) { + self.type_iterators.insert(typ, func); } /// Get the specified type iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { - self.type_iterators.get(&id).map(|v| v.as_ref()) + pub fn get_iter(&self, id: TypeId) -> Option { + self.type_iterators.get(&id).cloned() } } diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index bdfd03aa..eb56bbf4 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -120,8 +120,6 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { // Register array iterator lib.set_iter( TypeId::of::(), - Box::new(|arr| Box::new( - arr.cast::().into_iter()) as Box> - ), + |arr| Box::new(arr.cast::().into_iter()) as Box>, ); }); diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 553873e7..ebf55992 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -14,13 +14,10 @@ fn reg_range(lib: &mut Module) where Range: Iterator, { - lib.set_iter( - TypeId::of::>(), - Box::new(|source| { - Box::new(source.cast::>().map(|x| x.into_dynamic())) - as Box> - }), - ); + lib.set_iter(TypeId::of::>(), |source| { + Box::new(source.cast::>().map(|x| x.into_dynamic())) + as Box> + }); } fn get_range(from: T, to: T) -> FuncReturn> { @@ -58,13 +55,10 @@ where T: Variant + Clone + PartialOrd, StepRange: Iterator, { - lib.set_iter( - TypeId::of::>(), - Box::new(|source| { - Box::new(source.cast::>().map(|x| x.into_dynamic())) - as Box> - }), - ); + lib.set_iter(TypeId::of::>(), |source| { + Box::new(source.cast::>().map(|x| x.into_dynamic())) + as Box> + }); } fn get_step_range(from: T, to: T, step: T) -> FuncReturn> diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 62df57a6..54b6ecea 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -83,7 +83,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_iter(id)) } /// Get the specified TypeId iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { + pub fn get_iter(&self, id: TypeId) -> Option { self.packages .iter() .map(|p| p.get_iter(id)) From 800a7bf2831d91038660ab74f52090b772ade162 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 21 May 2020 17:11:01 +0800 Subject: [PATCH 12/53] Remove unnecessary traits and types. --- RELEASES.md | 2 + src/api.rs | 139 ++++++++--------------------------------------- src/engine.rs | 40 ++++++++------ src/fn_native.rs | 76 +++++++------------------- 4 files changed, 69 insertions(+), 188 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 8d01f9f5..e8fe628d 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -13,6 +13,8 @@ New features ------------ * Set limits on maximum level of nesting expressions and statements to avoid panics during parsing. +* New `EvalPackage` to disable `eval`. +* More benchmarks. Version 0.14.1 diff --git a/src/api.rs b/src/api.rs index 48052e85..555141cf 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,7 +4,7 @@ use crate::any::{Dynamic, Variant}; use crate::engine::{make_getter, make_setter, Engine, State, FUNC_INDEXER}; use crate::error::ParseError; use crate::fn_call::FuncArgs; -use crate::fn_native::{IteratorFn, ObjectGetCallback, ObjectIndexerCallback, ObjectSetCallback}; +use crate::fn_native::{IteratorFn, SendSync}; use crate::fn_register::RegisterFn; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::parser::{parse, parse_global_expr, AST}; @@ -162,11 +162,13 @@ impl Engine { /// # } /// ``` #[cfg(not(feature = "no_object"))] - pub fn register_get(&mut self, name: &str, callback: F) - where + pub fn register_get( + &mut self, + name: &str, + callback: impl Fn(&mut T) -> U + SendSync + 'static, + ) where T: Variant + Clone, U: Variant + Clone, - F: ObjectGetCallback, { self.register_fn(&make_getter(name), callback); } @@ -208,11 +210,13 @@ impl Engine { /// # } /// ``` #[cfg(not(feature = "no_object"))] - pub fn register_set(&mut self, name: &str, callback: F) - where + pub fn register_set( + &mut self, + name: &str, + callback: impl Fn(&mut T, U) + SendSync + 'static, + ) where T: Variant + Clone, U: Variant + Clone, - F: ObjectSetCallback, { self.register_fn(&make_setter(name), callback); } @@ -256,12 +260,14 @@ impl Engine { /// # } /// ``` #[cfg(not(feature = "no_object"))] - pub fn register_get_set(&mut self, name: &str, get_fn: G, set_fn: S) - where + pub fn register_get_set( + &mut self, + name: &str, + get_fn: impl Fn(&mut T) -> U + SendSync + 'static, + set_fn: impl Fn(&mut T, U) + SendSync + 'static, + ) where T: Variant + Clone, U: Variant + Clone, - G: ObjectGetCallback, - S: ObjectSetCallback, { self.register_get(name, get_fn); self.register_set(name, set_fn); @@ -305,12 +311,13 @@ impl Engine { /// ``` #[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_index"))] - pub fn register_indexer(&mut self, callback: F) - where + pub fn register_indexer( + &mut self, + callback: impl Fn(&mut T, X) -> U + SendSync + 'static, + ) where T: Variant + Clone, U: Variant + Clone, X: Variant + Clone, - F: ObjectIndexerCallback, { self.register_fn(FUNC_INDEXER, callback); } @@ -1118,47 +1125,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - #[cfg(feature = "sync")] - pub fn on_progress(&mut self, callback: impl Fn(u64) -> bool + Send + Sync + 'static) { - self.progress = Some(Box::new(callback)); - } - - /// Register a callback for script evaluation progress. - /// - /// # Example - /// - /// ``` - /// # fn main() -> Result<(), Box> { - /// # use std::cell::Cell; - /// # use std::rc::Rc; - /// use rhai::Engine; - /// - /// let result = Rc::new(Cell::new(0_u64)); - /// let logger = result.clone(); - /// - /// let mut engine = Engine::new(); - /// - /// engine.on_progress(move |ops| { - /// if ops > 10000 { - /// false - /// } else if ops % 800 == 0 { - /// logger.set(ops); - /// true - /// } else { - /// true - /// } - /// }); - /// - /// engine.consume("for x in range(0, 50000) {}") - /// .expect_err("should error"); - /// - /// assert_eq!(result.get(), 9600); - /// - /// # Ok(()) - /// # } - /// ``` - #[cfg(not(feature = "sync"))] - pub fn on_progress(&mut self, callback: impl Fn(u64) -> bool + 'static) { + pub fn on_progress(&mut self, callback: impl Fn(u64) -> bool + SendSync + 'static) { self.progress = Some(Box::new(callback)); } @@ -1186,36 +1153,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - #[cfg(feature = "sync")] - pub fn on_print(&mut self, callback: impl Fn(&str) + Send + Sync + 'static) { - self.print = Box::new(callback); - } - /// Override default action of `print` (print to stdout using `println!`) - /// - /// # Example - /// - /// ``` - /// # fn main() -> Result<(), Box> { - /// # use std::cell::RefCell; - /// # use std::rc::Rc; - /// use rhai::Engine; - /// - /// let result = Rc::new(RefCell::new(String::from(""))); - /// - /// let mut engine = Engine::new(); - /// - /// // Override action of 'print' function - /// let logger = result.clone(); - /// engine.on_print(move |s| logger.borrow_mut().push_str(s)); - /// - /// engine.consume("print(40 + 2);")?; - /// - /// assert_eq!(*result.borrow(), "42"); - /// # Ok(()) - /// # } - /// ``` - #[cfg(not(feature = "sync"))] - pub fn on_print(&mut self, callback: impl Fn(&str) + 'static) { + pub fn on_print(&mut self, callback: impl Fn(&str) + SendSync + 'static) { self.print = Box::new(callback); } @@ -1243,36 +1181,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - #[cfg(feature = "sync")] - pub fn on_debug(&mut self, callback: impl Fn(&str) + Send + Sync + 'static) { - self.debug = Box::new(callback); - } - /// Override default action of `debug` (print to stdout using `println!`) - /// - /// # Example - /// - /// ``` - /// # fn main() -> Result<(), Box> { - /// # use std::cell::RefCell; - /// # use std::rc::Rc; - /// use rhai::Engine; - /// - /// let result = Rc::new(RefCell::new(String::from(""))); - /// - /// let mut engine = Engine::new(); - /// - /// // Override action of 'print' function - /// let logger = result.clone(); - /// engine.on_debug(move |s| logger.borrow_mut().push_str(s)); - /// - /// engine.consume(r#"debug("hello");"#)?; - /// - /// assert_eq!(*result.borrow(), r#""hello""#); - /// # Ok(()) - /// # } - /// ``` - #[cfg(not(feature = "sync"))] - pub fn on_debug(&mut self, callback: impl Fn(&str) + 'static) { + pub fn on_debug(&mut self, callback: impl Fn(&str) + SendSync + 'static) { self.debug = Box::new(callback); } } diff --git a/src/engine.rs b/src/engine.rs index cb3b0e8c..8ebcfe1d 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::fn_native::{FnCallArgs, PrintCallback, ProgressCallback, SharedFnDef}; +use crate::fn_native::{FnCallArgs, Shared}; use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; @@ -217,7 +217,7 @@ impl<'a> State<'a> { /// /// The key of the `HashMap` is a `u64` hash calculated by the function `calc_fn_hash`. #[derive(Debug, Clone, Default)] -pub struct FunctionsLib(HashMap); +pub struct FunctionsLib(HashMap>); impl FunctionsLib { /// Create a new `FunctionsLib` from a collection of `FnDef`. @@ -275,17 +275,14 @@ impl FunctionsLib { } } -impl From> for FunctionsLib { - fn from(values: Vec<(u64, SharedFnDef)>) -> Self { +impl From)>> for FunctionsLib { + fn from(values: Vec<(u64, Shared)>) -> Self { FunctionsLib(values.into_iter().collect()) } } impl Deref for FunctionsLib { - #[cfg(feature = "sync")] - type Target = HashMap>; - #[cfg(not(feature = "sync"))] - type Target = HashMap>; + type Target = HashMap>; fn deref(&self) -> &Self::Target { &self.0 @@ -293,12 +290,7 @@ impl Deref for FunctionsLib { } impl DerefMut for FunctionsLib { - #[cfg(feature = "sync")] - fn deref_mut(&mut self) -> &mut HashMap> { - &mut self.0 - } - #[cfg(not(feature = "sync"))] - fn deref_mut(&mut self) -> &mut HashMap> { + fn deref_mut(&mut self) -> &mut HashMap> { &mut self.0 } } @@ -333,11 +325,25 @@ pub struct Engine { pub(crate) type_names: HashMap, /// Closure for implementing the `print` command. - pub(crate) print: Box, + #[cfg(not(feature = "sync"))] + pub(crate) print: Box, + /// Closure for implementing the `print` command. + #[cfg(feature = "sync")] + pub(crate) print: Box, + /// Closure for implementing the `debug` command. - pub(crate) debug: Box, + #[cfg(not(feature = "sync"))] + pub(crate) debug: Box, + /// Closure for implementing the `debug` command. + #[cfg(feature = "sync")] + pub(crate) debug: Box, + /// Closure for progress reporting. - pub(crate) progress: Option>, + #[cfg(not(feature = "sync"))] + pub(crate) progress: Option bool + 'static>>, + /// Closure for progress reporting. + #[cfg(feature = "sync")] + pub(crate) progress: Option bool + Send + Sync + 'static>>, /// Optimize the AST after compilation. pub(crate) optimization_level: OptimizationLevel, diff --git a/src/fn_native.rs b/src/fn_native.rs index 262bad7c..1eaab03a 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -4,78 +4,42 @@ use crate::result::EvalAltResult; use crate::stdlib::{boxed::Box, rc::Rc, sync::Arc}; +#[cfg(feature = "sync")] +pub trait SendSync: Send + Sync {} +#[cfg(feature = "sync")] +impl SendSync for T {} + +#[cfg(not(feature = "sync"))] +pub trait SendSync {} +#[cfg(not(feature = "sync"))] +impl SendSync for T {} + +#[cfg(not(feature = "sync"))] +pub type Shared = Rc; +#[cfg(feature = "sync")] +pub type Shared = Arc; + pub type FnCallArgs<'a> = [&'a mut Dynamic]; -#[cfg(feature = "sync")] -pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result> + Send + Sync; #[cfg(not(feature = "sync"))] pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result>; +#[cfg(feature = "sync")] +pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result> + Send + Sync; pub type IteratorFn = fn(Dynamic) -> Box>; -#[cfg(feature = "sync")] -pub type PrintCallback = dyn Fn(&str) + Send + Sync + 'static; -#[cfg(not(feature = "sync"))] -pub type PrintCallback = dyn Fn(&str) + 'static; - -#[cfg(feature = "sync")] -pub type ProgressCallback = dyn Fn(u64) -> bool + Send + Sync + 'static; -#[cfg(not(feature = "sync"))] -pub type ProgressCallback = dyn Fn(u64) -> bool + 'static; - -// Define callback function types -#[cfg(feature = "sync")] -pub trait ObjectGetCallback: Fn(&mut T) -> U + Send + Sync + 'static {} -#[cfg(feature = "sync")] -impl U + Send + Sync + 'static, T, U> ObjectGetCallback for F {} - -#[cfg(not(feature = "sync"))] -pub trait ObjectGetCallback: Fn(&mut T) -> U + 'static {} -#[cfg(not(feature = "sync"))] -impl U + 'static, T, U> ObjectGetCallback for F {} - -#[cfg(feature = "sync")] -pub trait ObjectSetCallback: Fn(&mut T, U) + Send + Sync + 'static {} -#[cfg(feature = "sync")] -impl ObjectSetCallback for F {} - -#[cfg(not(feature = "sync"))] -pub trait ObjectSetCallback: Fn(&mut T, U) + 'static {} -#[cfg(not(feature = "sync"))] -impl ObjectSetCallback for F {} - -#[cfg(feature = "sync")] -pub trait ObjectIndexerCallback: Fn(&mut T, X) -> U + Send + Sync + 'static {} -#[cfg(feature = "sync")] -impl U + Send + Sync + 'static, T, X, U> ObjectIndexerCallback for F {} - -#[cfg(not(feature = "sync"))] -pub trait ObjectIndexerCallback: Fn(&mut T, X) -> U + 'static {} -#[cfg(not(feature = "sync"))] -impl U + 'static, T, X, U> ObjectIndexerCallback for F {} - -#[cfg(not(feature = "sync"))] -pub type SharedNativeFunction = Rc; -#[cfg(feature = "sync")] -pub type SharedNativeFunction = Arc; - -#[cfg(feature = "sync")] -pub type SharedFnDef = Arc; -#[cfg(not(feature = "sync"))] -pub type SharedFnDef = Rc; - /// A type encapsulating a function callable by Rhai. #[derive(Clone)] pub enum CallableFunction { /// A pure native Rust function with all arguments passed by value. - Pure(SharedNativeFunction), + Pure(Shared), /// A native Rust object method with the first argument passed by reference, /// and the rest passed by value. - Method(SharedNativeFunction), + Method(Shared), /// An iterator function. Iterator(IteratorFn), /// A script-defined function. - Script(SharedFnDef), + Script(Shared), } impl CallableFunction { From 3408086240b2f9aeff29b5440766fab9cf87118f Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 22 May 2020 13:08:57 +0800 Subject: [PATCH 13/53] Copy values differently. --- src/engine.rs | 42 +++++++++++++++++++++++------------------- src/unsafe.rs | 12 ++++++++++++ 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 8ebcfe1d..2fa8348f 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -8,7 +8,7 @@ use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; use crate::parser::{Expr, FnAccess, FnDef, ReturnType, Stmt, AST}; -use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; +use crate::r#unsafe::{unsafe_cast_var_name_to_lifetime, unsafe_mut_cast_to_lifetime}; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::Position; @@ -655,27 +655,31 @@ impl Engine { .or_else(|| self.packages.get_fn(hashes.0)) { // Calling pure function in method-call? - let backup: Option = if func.is_pure() && is_ref && args.len() > 0 { - // Backup the original value. It'll be consumed because the function + let mut this_copy: Option; + let mut this_pointer: Option<&mut Dynamic> = None; + + if func.is_pure() && is_ref && args.len() > 0 { + // Clone the original value. It'll be consumed because the function // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) - Some(args[0].clone()) - } else { - None - }; + this_copy = Some(args[0].clone()); + + // Replace the first reference with a reference to the clone, force-casting the lifetime. + // Keep the original reference. Must remember to restore it before existing this function. + this_pointer = Some(mem::replace( + args.get_mut(0).unwrap(), + unsafe_mut_cast_to_lifetime(this_copy.as_mut().unwrap()), + )); + } // Run external function - let result = match func.get_native_fn()(args) { - Ok(r) => { - // Restore the backup value for the first argument since it has been consumed! - if let Some(backup) = backup { - *args[0] = backup; - } - r - } - Err(err) => { - return Err(err.new_position(pos)); - } - }; + let result = func.get_native_fn()(args); + + // Restore the original reference + if let Some(this_pointer) = this_pointer { + mem::replace(args.get_mut(0).unwrap(), this_pointer); + } + + let result = result.map_err(|err| err.new_position(pos))?; // See if the function match print/debug (which requires special processing) return Ok(match fn_name { diff --git a/src/unsafe.rs b/src/unsafe.rs index 46d91198..1d1ac545 100644 --- a/src/unsafe.rs +++ b/src/unsafe.rs @@ -42,6 +42,18 @@ pub fn unsafe_cast_box(item: Box) -> Result, B } } +/// # DANGEROUS!!! +/// +/// A dangerous function that blindly casts a reference from one lifetime to another lifetime. +/// +/// Force-casting a a reference to another lifetime saves on allocations and string cloning, +/// but must be used with the utmost care. +pub fn unsafe_mut_cast_to_lifetime<'a, T>(value: &mut T) -> &'a mut T { + unsafe { mem::transmute::<_, &'a mut T>(value) } +} + +/// # DANGEROUS!!! +/// /// A dangerous function that blindly casts a `&str` from one lifetime to a `Cow` of /// another lifetime. This is mainly used to let us push a block-local variable into the /// current `Scope` without cloning the variable name. Doing this is safe because all local From 2f0ab18b7010804c4d97565dd3e3ed36c5322bc8 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 22 May 2020 13:09:17 +0800 Subject: [PATCH 14/53] Merge register_result_fn and register_dynamic_fn. --- README.md | 50 ++++++++++++++--------------- src/fn_register.rs | 79 ++++++++++------------------------------------ src/lib.rs | 2 +- 3 files changed, 40 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index 8673aba5..fc04a54e 100644 --- a/README.md +++ b/README.md @@ -611,13 +611,12 @@ Traits A number of traits, under the `rhai::` module namespace, provide additional functionalities. -| Trait | Description | Methods | -| ------------------- | -------------------------------------------------------------------------------------- | --------------------------------------- | -| `RegisterFn` | Trait for registering functions | `register_fn` | -| `RegisterDynamicFn` | Trait for registering functions returning [`Dynamic`] | `register_dynamic_fn` | -| `RegisterResultFn` | Trait for registering fallible functions returning `Result<`_T_`, Box>` | `register_result_fn` | -| `Func` | Trait for creating anonymous functions from script | `create_from_ast`, `create_from_script` | -| `ModuleResolver` | Trait implemented by module resolution services | `resolve` | +| Trait | Description | Methods | +| ------------------ | -------------------------------------------------------------------------------------- | --------------------------------------- | +| `RegisterFn` | Trait for registering functions | `register_fn` | +| `RegisterResultFn` | Trait for registering fallible functions returning `Result<`_T_`, Box>` | `register_result_fn` | +| `Func` | Trait for creating anonymous functions from script | `create_from_ast`, `create_from_script` | +| `ModuleResolver` | Trait implemented by module resolution services | `resolve` | Working with functions ---------------------- @@ -628,16 +627,16 @@ To call these functions, they need to be registered with the [`Engine`]. ```rust use rhai::{Dynamic, Engine, EvalAltResult}; use rhai::RegisterFn; // use 'RegisterFn' trait for 'register_fn' -use rhai::{Dynamic, RegisterDynamicFn}; // use 'RegisterDynamicFn' trait for 'register_dynamic_fn' +use rhai::RegisterResultFn; // use 'RegisterResultFn' trait for 'register_result_fn' -// Normal function +// Normal function that returns any value type fn add(x: i64, y: i64) -> i64 { x + y } -// Function that returns a Dynamic value -fn get_an_any() -> Dynamic { - Dynamic::from(42_i64) +// Function that returns a 'Dynamic' value - must return a 'Result' +fn get_any_value() -> Result> { + Ok((42_i64).into()) // standard supported types can use 'into()' } fn main() -> Result<(), Box> @@ -650,10 +649,10 @@ fn main() -> Result<(), Box> println!("Answer: {}", result); // prints 42 - // Functions that return Dynamic values must use register_dynamic_fn() - engine.register_dynamic_fn("get_an_any", get_an_any); + // Functions that return Dynamic values must use register_result_fn() + engine.register_result_fn("get_any_value", get_any_value); - let result = engine.eval::("get_an_any()")?; + let result = engine.eval::("get_any_value()")?; println!("Answer: {}", result); // prints 42 @@ -661,18 +660,15 @@ fn main() -> Result<(), Box> } ``` -To return a [`Dynamic`] value from a Rust function, use the `Dynamic::from` method. +To create a [`Dynamic`] value, use the `Dynamic::from` method. +Standard supported types in Rhai can also use `into()`. ```rust use rhai::Dynamic; -fn decide(yes_no: bool) -> Dynamic { - if yes_no { - Dynamic::from(42_i64) - } else { - Dynamic::from(String::from("hello!")) // remember &str is not supported by Rhai - } -} +let x = (42_i64).into(); // 'into()' works for standard supported types + +let y = Dynamic::from(String::from("hello!")); // remember &str is not supported by Rhai ``` Generic functions @@ -709,7 +705,7 @@ Fallible functions If a function is _fallible_ (i.e. it returns a `Result<_, Error>`), it can be registered with `register_result_fn` (using the `RegisterResultFn` trait). -The function must return `Result<_, Box>`. `Box` implements `From<&str>` and `From` etc. +The function must return `Result>`. `Box` implements `From<&str>` and `From` etc. and the error text gets converted into `Box`. The error values are `Box`-ed in order to reduce memory footprint of the error path, which should be hit rarely. @@ -718,13 +714,13 @@ The error values are `Box`-ed in order to reduce memory footprint of the error p use rhai::{Engine, EvalAltResult, Position}; use rhai::RegisterResultFn; // use 'RegisterResultFn' trait for 'register_result_fn' -// Function that may fail -fn safe_divide(x: i64, y: i64) -> Result> { +// Function that may fail - the result type must be 'Dynamic' +fn safe_divide(x: i64, y: i64) -> Result> { if y == 0 { // Return an error if y is zero Err("Division by zero!".into()) // short-cut to create Box } else { - Ok(x / y) + Ok((x / y).into()) // convert result into 'Dynamic' } } diff --git a/src/fn_register.rs b/src/fn_register.rs index 46dbd166..ec2be474 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -42,49 +42,22 @@ pub trait RegisterFn { fn register_fn(&mut self, name: &str, f: FN); } -/// Trait to register custom functions that return `Dynamic` values with the `Engine`. -pub trait RegisterDynamicFn { - /// Register a custom function returning `Dynamic` values with the `Engine`. - /// - /// # Example - /// - /// ``` - /// # fn main() -> Result<(), Box> { - /// use rhai::{Engine, Dynamic, RegisterDynamicFn}; - /// - /// // Function that returns a Dynamic value - /// fn return_the_same_as_dynamic(x: i64) -> Dynamic { - /// Dynamic::from(x) - /// } - /// - /// let mut engine = Engine::new(); - /// - /// // You must use the trait rhai::RegisterDynamicFn to get this method. - /// engine.register_dynamic_fn("get_any_number", return_the_same_as_dynamic); - /// - /// assert_eq!(engine.eval::("get_any_number(42)")?, 42); - /// # Ok(()) - /// # } - /// ``` - fn register_dynamic_fn(&mut self, name: &str, f: FN); -} - -/// Trait to register fallible custom functions returning `Result<_, Box>` with the `Engine`. -pub trait RegisterResultFn { +/// Trait to register fallible custom functions returning `Result>` with the `Engine`. +pub trait RegisterResultFn { /// Register a custom fallible function with the `Engine`. /// /// # Example /// /// ``` - /// use rhai::{Engine, RegisterResultFn, EvalAltResult}; + /// use rhai::{Engine, Dynamic, RegisterResultFn, EvalAltResult}; /// /// // Normal function - /// fn div(x: i64, y: i64) -> Result> { + /// fn div(x: i64, y: i64) -> Result> { /// if y == 0 { /// // '.into()' automatically converts to 'Box' /// Err("division by zero!".into()) /// } else { - /// Ok(x / y) + /// Ok((x / y).into()) /// } /// } /// @@ -171,12 +144,12 @@ pub fn map_identity(data: Dynamic) -> Result> { Ok(data) } -/// To `Result>` mapping function. +/// To Dynamic mapping function. #[inline(always)] -pub fn map_result( - data: Result>, +pub fn map_result( + data: Result>, ) -> Result> { - data.map(|v| v.into_dynamic()) + data } macro_rules! def_register { @@ -185,10 +158,10 @@ macro_rules! def_register { }; (imp $abi:ident : $($par:ident => $mark:ty => $param:ty => $clone:expr),*) => { // ^ function ABI type - // ^ function parameter generic type name (A, B, C etc.) - // ^ function parameter marker type (T, Ref or Mut) - // ^ function parameter actual type (T, &T or &mut T) - // ^ dereferencing function + // ^ function parameter generic type name (A, B, C etc.) + // ^ function parameter marker type (T, Ref or Mut) + // ^ function parameter actual type (T, &T or &mut T) + // ^ dereferencing function impl< $($par: Variant + Clone,)* @@ -213,30 +186,10 @@ macro_rules! def_register { $($par: Variant + Clone,)* #[cfg(feature = "sync")] - FN: Fn($($param),*) -> Dynamic + Send + Sync + 'static, - + FN: Fn($($param),*) -> Result> + Send + Sync + 'static, #[cfg(not(feature = "sync"))] - FN: Fn($($param),*) -> Dynamic + 'static, - > RegisterDynamicFn for Engine - { - fn register_dynamic_fn(&mut self, name: &str, f: FN) { - self.global_module.set_fn(name.to_string(), FnAccess::Public, - &[$(TypeId::of::<$par>()),*], - CallableFunction::$abi(make_func!(f : map_identity ; $($par => $clone),*)) - ); - } - } - - impl< - $($par: Variant + Clone,)* - - #[cfg(feature = "sync")] - FN: Fn($($param),*) -> Result> + Send + Sync + 'static, - #[cfg(not(feature = "sync"))] - FN: Fn($($param),*) -> Result> + 'static, - - RET: Variant + Clone - > RegisterResultFn for Engine + FN: Fn($($param),*) -> Result> + 'static, + > RegisterResultFn for Engine { fn register_result_fn(&mut self, name: &str, f: FN) { self.global_module.set_fn(name.to_string(), FnAccess::Public, diff --git a/src/lib.rs b/src/lib.rs index b4e71a83..df039436 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,7 +91,7 @@ mod utils; pub use any::Dynamic; pub use engine::Engine; pub use error::{ParseError, ParseErrorType}; -pub use fn_register::{RegisterDynamicFn, RegisterFn, RegisterResultFn}; +pub use fn_register::{RegisterFn, RegisterResultFn}; pub use module::Module; pub use parser::{AST, INT}; pub use result::EvalAltResult; From e224550861d6dc448e97221c9f1ab024266fa7ee Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 22 May 2020 18:28:13 +0800 Subject: [PATCH 15/53] Move boxing of ParseError into ParseErrorType. --- src/api.rs | 12 ++++---- src/engine.rs | 9 +++--- src/error.rs | 10 +++---- src/fn_func.rs | 4 +-- src/parser.rs | 78 +++++++++++++++++++++++++------------------------- src/result.rs | 7 +---- tests/stack.rs | 4 +-- 7 files changed, 59 insertions(+), 65 deletions(-) diff --git a/src/api.rs b/src/api.rs index 555141cf..05dc5ec3 100644 --- a/src/api.rs +++ b/src/api.rs @@ -341,7 +341,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - pub fn compile(&self, script: &str) -> Result> { + pub fn compile(&self, script: &str) -> Result { self.compile_with_scope(&Scope::new(), script) } @@ -383,7 +383,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - pub fn compile_with_scope(&self, scope: &Scope, script: &str) -> Result> { + pub fn compile_with_scope(&self, scope: &Scope, script: &str) -> Result { self.compile_scripts_with_scope(scope, &[script]) } @@ -437,7 +437,7 @@ impl Engine { &self, scope: &Scope, scripts: &[&str], - ) -> Result> { + ) -> Result { self.compile_with_scope_and_optimization_level(scope, scripts, self.optimization_level) } @@ -447,7 +447,7 @@ impl Engine { scope: &Scope, scripts: &[&str], optimization_level: OptimizationLevel, - ) -> Result> { + ) -> Result { let stream = lex(scripts); parse( @@ -614,7 +614,7 @@ impl Engine { /// # Ok(()) /// # } /// ``` - pub fn compile_expression(&self, script: &str) -> Result> { + pub fn compile_expression(&self, script: &str) -> Result { self.compile_expression_with_scope(&Scope::new(), script) } @@ -661,7 +661,7 @@ impl Engine { &self, scope: &Scope, script: &str, - ) -> Result> { + ) -> Result { let scripts = [script]; let stream = lex(&scripts); diff --git a/src/engine.rs b/src/engine.rs index 2fa8348f..cbb7dc02 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -465,8 +465,7 @@ fn default_print(s: &str) { fn search_scope<'a>( scope: &'a mut Scope, name: &str, - #[cfg(not(feature = "no_module"))] modules: Option<(&Box, u64)>, - #[cfg(feature = "no_module")] _: Option<(&ModuleRef, u64)>, + modules: Option<(&ModuleRef, u64)>, index: Option, pos: Position, ) -> Result<(&'a mut Dynamic, ScopeEntryType), Box> { @@ -1146,7 +1145,7 @@ impl Engine { Expr::Variable(x) => { let ((name, pos), modules, hash_var, index) = x.as_ref(); let index = if state.always_search { None } else { *index }; - let mod_and_hash = modules.as_ref().map(|m| (m, *hash_var)); + let mod_and_hash = modules.as_ref().map(|m| (m.as_ref(), *hash_var)); let (target, typ) = search_scope(scope, &name, mod_and_hash, index, *pos)?; self.inc_operations(state, *pos)?; @@ -1393,7 +1392,7 @@ impl Engine { Expr::Variable(x) => { let ((name, pos), modules, hash_var, index) = x.as_ref(); let index = if state.always_search { None } else { *index }; - let mod_and_hash = modules.as_ref().map(|m| (m, *hash_var)); + let mod_and_hash = modules.as_ref().map(|m| (m.as_ref(), *hash_var)); let (val, _) = search_scope(scope, name, mod_and_hash, index, *pos)?; Ok(val.clone()) } @@ -1412,7 +1411,7 @@ impl Engine { Expr::Variable(x) => { let ((name, pos), modules, hash_var, index) = x.as_ref(); let index = if state.always_search { None } else { *index }; - let mod_and_hash = modules.as_ref().map(|m| (m, *hash_var)); + let mod_and_hash = modules.as_ref().map(|m| (m.as_ref(), *hash_var)); let (lhs_ptr, typ) = search_scope(scope, name, mod_and_hash, index, *pos)?; self.inc_operations(state, *pos)?; diff --git a/src/error.rs b/src/error.rs index 8e180c25..7bc60a1c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -120,14 +120,14 @@ pub enum ParseErrorType { impl ParseErrorType { /// Make a `ParseError` using the current type and position. - pub(crate) fn into_err(self, pos: Position) -> Box { - Box::new(ParseError(self, pos)) + pub(crate) fn into_err(self, pos: Position) -> ParseError { + ParseError(Box::new(self), pos) } } /// Error when parsing a script. #[derive(Debug, Eq, PartialEq, Clone, Hash)] -pub struct ParseError(pub(crate) ParseErrorType, pub(crate) Position); +pub struct ParseError(pub(crate) Box, pub(crate) Position); impl ParseError { /// Get the parse error. @@ -141,7 +141,7 @@ impl ParseError { } pub(crate) fn desc(&self) -> &str { - match &self.0 { + match self.0.as_ref() { ParseErrorType::BadInput(p) => p, ParseErrorType::UnexpectedEOF => "Script is incomplete", ParseErrorType::UnknownOperator(_) => "Unknown operator", @@ -173,7 +173,7 @@ impl Error for ParseError {} impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match &self.0 { + match self.0.as_ref() { ParseErrorType::BadInput(s) | ParseErrorType::MalformedCallExpr(s) => { write!(f, "{}", if s.is_empty() { self.desc() } else { s })? } diff --git a/src/fn_func.rs b/src/fn_func.rs index f606b794..bbbab4aa 100644 --- a/src/fn_func.rs +++ b/src/fn_func.rs @@ -80,7 +80,7 @@ pub trait Func { self, script: &str, entry_point: &str, - ) -> Result>; + ) -> Result; } macro_rules! def_anonymous_fn { @@ -103,7 +103,7 @@ macro_rules! def_anonymous_fn { }) } - fn create_from_script(self, script: &str, entry_point: &str) -> Result> { + fn create_from_script(self, script: &str, entry_point: &str) -> Result { let ast = self.compile(script)?; Ok(Func::<($($par,)*), RET>::create_from_ast(self, ast, entry_point)) } diff --git a/src/parser.rs b/src/parser.rs index e03f3686..3c3581eb 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -361,11 +361,6 @@ impl Stmt { } } -#[cfg(not(feature = "no_module"))] -type MRef = Option>; -#[cfg(feature = "no_module")] -type MRef = Option; - /// An expression. /// /// Each variant is at most one pointer in size (for speed), @@ -382,7 +377,14 @@ pub enum Expr { /// String constant. StringConstant(Box<(String, Position)>), /// Variable access - ((variable name, position), optional modules, hash, optional index) - Variable(Box<((String, Position), MRef, u64, Option)>), + Variable( + Box<( + (String, Position), + Option>, + u64, + Option, + )>, + ), /// Property access. Property(Box<((String, String, String), Position)>), /// { stmt } @@ -393,7 +395,7 @@ pub enum Expr { FnCall( Box<( (Cow<'static, str>, Position), - MRef, + Option>, u64, StaticVec, Option, @@ -661,7 +663,7 @@ fn eat_token(input: &mut Peekable, token: Token) -> Position { } /// Match a particular token, consuming it if matched. -fn match_token(input: &mut Peekable, token: Token) -> Result> { +fn match_token(input: &mut Peekable, token: Token) -> Result { let (t, _) = input.peek().unwrap(); if *t == token { eat_token(input, token); @@ -678,7 +680,7 @@ fn parse_paren_expr<'a>( pos: Position, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { if level > state.max_expr_depth { return Err(PERR::ExprTooDeep.into_err(pos)); } @@ -713,7 +715,7 @@ fn parse_call_expr<'a>( begin: Position, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { let (token, pos) = input.peek().unwrap(); if level > state.max_expr_depth { @@ -844,7 +846,7 @@ fn parse_index_chain<'a>( pos: Position, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { if level > state.max_expr_depth { return Err(PERR::ExprTooDeep.into_err(pos)); } @@ -1033,7 +1035,7 @@ fn parse_array_literal<'a>( pos: Position, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { if level > state.max_expr_depth { return Err(PERR::ExprTooDeep.into_err(pos)); } @@ -1081,7 +1083,7 @@ fn parse_map_literal<'a>( pos: Position, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { if level > state.max_expr_depth { return Err(PERR::ExprTooDeep.into_err(pos)); } @@ -1182,7 +1184,7 @@ fn parse_primary<'a>( state: &mut ParseState, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { let (token, pos) = input.peek().unwrap(); let pos = *pos; @@ -1290,7 +1292,7 @@ fn parse_unary<'a>( state: &mut ParseState, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { let (token, pos) = input.peek().unwrap(); let pos = *pos; @@ -1388,7 +1390,7 @@ fn make_assignment_stmt<'a>( lhs: Expr, rhs: Expr, pos: Position, -) -> Result> { +) -> Result { match &lhs { Expr::Variable(x) if x.3.is_none() => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), Expr::Variable(x) => { @@ -1431,7 +1433,7 @@ fn parse_op_assignment_stmt<'a>( lhs: Expr, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { let (token, pos) = input.peek().unwrap(); let pos = *pos; @@ -1482,7 +1484,7 @@ fn make_dot_expr( rhs: Expr, op_pos: Position, is_index: bool, -) -> Result> { +) -> Result { Ok(match (lhs, rhs) { // idx_lhs[idx_rhs].rhs // Attach dot chain to the bottom level of indexing chain @@ -1544,7 +1546,7 @@ fn make_dot_expr( } /// Make an 'in' expression. -fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result> { +fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result { match (&lhs, &rhs) { (_, Expr::IntegerConstant(x)) => { return Err(PERR::MalformedInExpr( @@ -1717,7 +1719,7 @@ fn parse_binary_op<'a>( lhs: Expr, mut level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { if level > state.max_expr_depth { return Err(PERR::ExprTooDeep.into_err(lhs.position())); } @@ -1836,7 +1838,7 @@ fn parse_expr<'a>( state: &mut ParseState, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { let (_, pos) = input.peek().unwrap(); if level > state.max_expr_depth { @@ -1851,7 +1853,7 @@ fn parse_expr<'a>( fn ensure_not_statement_expr<'a>( input: &mut Peekable>, type_name: &str, -) -> Result<(), Box> { +) -> Result<(), ParseError> { match input.peek().unwrap() { // Disallow statement expressions (Token::LeftBrace, pos) | (Token::EOF, pos) => { @@ -1863,9 +1865,7 @@ fn ensure_not_statement_expr<'a>( } /// Make sure that the expression is not a mis-typed assignment (i.e. `a = b` instead of `a == b`). -fn ensure_not_assignment<'a>( - input: &mut Peekable>, -) -> Result<(), Box> { +fn ensure_not_assignment<'a>(input: &mut Peekable>) -> Result<(), ParseError> { match input.peek().unwrap() { (Token::Equals, pos) => { return Err(PERR::BadInput("Possibly a typo of '=='?".to_string()).into_err(*pos)) @@ -1898,7 +1898,7 @@ fn parse_if<'a>( breakable: bool, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { // if ... let pos = eat_token(input, Token::If); @@ -1934,7 +1934,7 @@ fn parse_while<'a>( state: &mut ParseState, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { // while ... let pos = eat_token(input, Token::While); @@ -1957,7 +1957,7 @@ fn parse_loop<'a>( state: &mut ParseState, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { // loop ... let pos = eat_token(input, Token::Loop); @@ -1977,7 +1977,7 @@ fn parse_for<'a>( state: &mut ParseState, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { // for ... let pos = eat_token(input, Token::For); @@ -2030,7 +2030,7 @@ fn parse_let<'a>( var_type: ScopeEntryType, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { // let/const... (specified in `var_type`) let (_, pos) = input.next().unwrap(); @@ -2091,7 +2091,7 @@ fn parse_import<'a>( state: &mut ParseState, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { // import ... let pos = eat_token(input, Token::Import); @@ -2129,7 +2129,7 @@ fn parse_export<'a>( input: &mut Peekable>, state: &mut ParseState, level: usize, -) -> Result> { +) -> Result { let pos = eat_token(input, Token::Export); if level > state.max_expr_depth { @@ -2196,7 +2196,7 @@ fn parse_block<'a>( breakable: bool, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { // Must start with { let pos = match input.next().unwrap() { (Token::LeftBrace, pos) => pos, @@ -2267,7 +2267,7 @@ fn parse_expr_stmt<'a>( state: &mut ParseState, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { let (_, pos) = input.peek().unwrap(); if level > state.max_expr_depth { @@ -2287,7 +2287,7 @@ fn parse_stmt<'a>( is_global: bool, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { use ScopeEntryType::{Constant, Normal}; let (token, pos) = match input.peek().unwrap() { @@ -2376,7 +2376,7 @@ fn parse_fn<'a>( access: FnAccess, level: usize, allow_stmt_expr: bool, -) -> Result> { +) -> Result { let pos = eat_token(input, Token::Fn); if level > state.max_expr_depth { @@ -2467,7 +2467,7 @@ pub fn parse_global_expr<'a>( scope: &Scope, optimization_level: OptimizationLevel, max_expr_depth: usize, -) -> Result> { +) -> Result { let mut state = ParseState::new(max_expr_depth); let expr = parse_expr(input, &mut state, 0, false)?; @@ -2495,7 +2495,7 @@ pub fn parse_global_expr<'a>( fn parse_global_level<'a>( input: &mut Peekable>, max_expr_depth: (usize, usize), -) -> Result<(Vec, HashMap), Box> { +) -> Result<(Vec, HashMap), ParseError> { let mut statements = Vec::::new(); let mut functions = HashMap::::new(); let mut state = ParseState::new(max_expr_depth.0); @@ -2577,7 +2577,7 @@ pub fn parse<'a>( scope: &Scope, optimization_level: OptimizationLevel, max_expr_depth: (usize, usize), -) -> Result> { +) -> Result { let (statements, functions) = parse_global_level(input, max_expr_depth)?; let fn_lib = functions.into_iter().map(|(_, v)| v).collect(); diff --git a/src/result.rs b/src/result.rs index af55535a..2589016b 100644 --- a/src/result.rs +++ b/src/result.rs @@ -23,7 +23,7 @@ use crate::stdlib::path::PathBuf; #[derive(Debug)] pub enum EvalAltResult { /// Syntax error. - ErrorParsing(Box), + ErrorParsing(ParseError), /// Error reading from a script file. Wrapped value is the path of the script file. /// @@ -241,11 +241,6 @@ impl fmt::Display for EvalAltResult { impl From for Box { fn from(err: ParseError) -> Self { - Box::new(EvalAltResult::ErrorParsing(Box::new(err))) - } -} -impl From> for Box { - fn from(err: Box) -> Self { Box::new(EvalAltResult::ErrorParsing(err)) } } diff --git a/tests/stack.rs b/tests/stack.rs index ee7eedc4..c0cb8a38 100644 --- a/tests/stack.rs +++ b/tests/stack.rs @@ -33,7 +33,7 @@ fn test_stack_overflow_parsing() -> Result<(), Box> { let mut engine = Engine::new(); assert!(matches!( - *engine.compile(r" + engine.compile(r" let a = (1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+1)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ").expect_err("should error"), err if err.error_type() == &ParseErrorType::ExprTooDeep @@ -58,7 +58,7 @@ fn test_stack_overflow_parsing() -> Result<(), Box> { )?; assert!(matches!( - *engine.compile(r" + engine.compile(r" 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + From 8d12dc2fc1c80499176c19f39b290b4bba1b3964 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 22 May 2020 21:49:53 +0800 Subject: [PATCH 16/53] Add Dynamic::as_float. --- src/any.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/any.rs b/src/any.rs index 7f209685..c313de6a 100644 --- a/src/any.rs +++ b/src/any.rs @@ -519,6 +519,16 @@ impl Dynamic { } } + /// Cast the `Dynamic` as the system floating-point type `FLOAT` and return it. + /// Returns the name of the actual type if the cast fails. + #[cfg(not(feature = "no_float"))] + pub fn as_float(&self) -> Result { + match self.0 { + Union::Float(n) => Ok(n), + _ => Err(self.type_name()), + } + } + /// Cast the `Dynamic` as a `bool` and return it. /// Returns the name of the actual type if the cast fails. pub fn as_bool(&self) -> Result { From a743c47345ba7891952555ebe6e191d2c430e6b0 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 22 May 2020 21:50:24 +0800 Subject: [PATCH 17/53] Refactor. --- src/fn_register.rs | 4 +- src/module.rs | 108 +++++++++++++++++++++++++++++--------------- src/packages/mod.rs | 26 ++++------- 3 files changed, 83 insertions(+), 55 deletions(-) diff --git a/src/fn_register.rs b/src/fn_register.rs index ec2be474..67763298 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -175,7 +175,7 @@ macro_rules! def_register { > RegisterFn for Engine { fn register_fn(&mut self, name: &str, f: FN) { - self.global_module.set_fn(name.to_string(), FnAccess::Public, + self.global_module.set_fn(name, FnAccess::Public, &[$(TypeId::of::<$par>()),*], CallableFunction::$abi(make_func!(f : map_dynamic ; $($par => $clone),*)) ); @@ -192,7 +192,7 @@ macro_rules! def_register { > RegisterResultFn for Engine { fn register_result_fn(&mut self, name: &str, f: FN) { - self.global_module.set_fn(name.to_string(), FnAccess::Public, + self.global_module.set_fn(name, FnAccess::Public, &[$(TypeId::of::<$par>()),*], CallableFunction::$abi(make_func!(f : map_result ; $($par => $clone),*)) ); diff --git a/src/module.rs b/src/module.rs index 48e6e936..6c366a35 100644 --- a/src/module.rs +++ b/src/module.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; -use crate::fn_native::{CallableFunction as CF, FnCallArgs, IteratorFn}; +use crate::fn_native::{CallableFunction, FnCallArgs, IteratorFn}; use crate::parser::{ FnAccess, FnAccess::{Private, Public}, @@ -47,7 +47,7 @@ pub struct Module { all_variables: HashMap, /// External Rust functions. - functions: HashMap, CF)>, + functions: HashMap, CallableFunction)>, /// Script-defined functions. fn_lib: FunctionsLib, @@ -57,7 +57,7 @@ pub struct Module { /// Flattened collection of all external Rust functions, native or scripted, /// including those in sub-modules. - all_functions: HashMap, + all_functions: HashMap, } impl fmt::Debug for Module { @@ -164,7 +164,7 @@ impl Module { /// module.set_var("answer", 42_i64); /// assert_eq!(module.get_var_value::("answer").unwrap(), 42); /// ``` - pub fn set_var, T: Variant + Clone>(&mut self, name: K, value: T) { + pub fn set_var(&mut self, name: impl Into, value: impl Variant + Clone) { self.variables.insert(name.into(), Dynamic::from(value)); } @@ -244,7 +244,7 @@ impl Module { /// module.set_sub_module("question", sub_module); /// assert!(module.get_sub_module("question").is_some()); /// ``` - pub fn set_sub_module>(&mut self, name: K, sub_module: Module) { + pub fn set_sub_module(&mut self, name: impl Into, sub_module: Module) { self.modules.insert(name.into(), sub_module.into()); } @@ -269,7 +269,15 @@ impl Module { /// Set a Rust function into the module, returning a hash key. /// /// If there is an existing Rust function of the same hash, it is replaced. - pub fn set_fn(&mut self, name: String, access: FnAccess, params: &[TypeId], func: CF) -> u64 { + pub fn set_fn( + &mut self, + name: impl Into, + access: FnAccess, + params: &[TypeId], + func: CallableFunction, + ) -> u64 { + let name = name.into(); + let hash_fn = calc_fn_hash(empty(), &name, params.len(), params.iter().cloned()); let params = params.into_iter().cloned().collect(); @@ -293,15 +301,20 @@ impl Module { /// let hash = module.set_fn_0("calc", || Ok(42_i64)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_0, T: Variant + Clone>( + pub fn set_fn_0( &mut self, - name: K, + name: impl Into, #[cfg(not(feature = "sync"))] func: impl Fn() -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn() -> FuncReturn + Send + Sync + 'static, ) -> u64 { let f = move |_: &mut FnCallArgs| func().map(Dynamic::from); let args = []; - self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) + self.set_fn( + name, + Public, + &args, + CallableFunction::from_pure(Box::new(f)), + ) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -317,16 +330,21 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_1, A: Variant + Clone, T: Variant + Clone>( + pub fn set_fn_1( &mut self, - name: K, + name: impl Into, #[cfg(not(feature = "sync"))] func: impl Fn(A) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A) -> FuncReturn + Send + Sync + 'static, ) -> u64 { let f = move |args: &mut FnCallArgs| func(mem::take(args[0]).cast::()).map(Dynamic::from); let args = [TypeId::of::()]; - self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) + self.set_fn( + name, + Public, + &args, + CallableFunction::from_pure(Box::new(f)), + ) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -342,9 +360,9 @@ impl Module { /// let hash = module.set_fn_1_mut("calc", |x: &mut i64| { *x += 1; Ok(*x) }); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_1_mut, A: Variant + Clone, T: Variant + Clone>( + pub fn set_fn_1_mut( &mut self, - name: K, + name: impl Into, #[cfg(not(feature = "sync"))] func: impl Fn(&mut A) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(&mut A) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -352,7 +370,12 @@ impl Module { func(args[0].downcast_mut::().unwrap()).map(Dynamic::from) }; let args = [TypeId::of::()]; - self.set_fn(name.into(), Public, &args, CF::from_method(Box::new(f))) + self.set_fn( + name, + Public, + &args, + CallableFunction::from_method(Box::new(f)), + ) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -370,9 +393,9 @@ impl Module { /// }); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_2, A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>( + pub fn set_fn_2( &mut self, - name: K, + name: impl Into, #[cfg(not(feature = "sync"))] func: impl Fn(A, B) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A, B) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -383,7 +406,12 @@ impl Module { func(a, b).map(Dynamic::from) }; let args = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) + self.set_fn( + name, + Public, + &args, + CallableFunction::from_pure(Box::new(f)), + ) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -400,14 +428,9 @@ impl Module { /// }); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_2_mut< - K: Into, - A: Variant + Clone, - B: Variant + Clone, - T: Variant + Clone, - >( + pub fn set_fn_2_mut( &mut self, - name: K, + name: impl Into, #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -418,7 +441,12 @@ impl Module { func(a, b).map(Dynamic::from) }; let args = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &args, CF::from_method(Box::new(f))) + self.set_fn( + name, + Public, + &args, + CallableFunction::from_method(Box::new(f)), + ) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -437,14 +465,13 @@ impl Module { /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_3< - K: Into, A: Variant + Clone, B: Variant + Clone, C: Variant + Clone, T: Variant + Clone, >( &mut self, - name: K, + name: impl Into, #[cfg(not(feature = "sync"))] func: impl Fn(A, B, C) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A, B, C) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -456,7 +483,12 @@ impl Module { func(a, b, c).map(Dynamic::from) }; let args = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) + self.set_fn( + name, + Public, + &args, + CallableFunction::from_pure(Box::new(f)), + ) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -476,14 +508,13 @@ impl Module { /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_3_mut< - K: Into, A: Variant + Clone, B: Variant + Clone, C: Variant + Clone, T: Variant + Clone, >( &mut self, - name: K, + name: impl Into, #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, C) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(&mut A, B, C) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -495,7 +526,12 @@ impl Module { func(a, b, c).map(Dynamic::from) }; let args = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &args, CF::from_method(Box::new(f))) + self.set_fn( + name, + Public, + &args, + CallableFunction::from_method(Box::new(f)), + ) } /// Get a Rust function. @@ -512,7 +548,7 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn get_fn(&self, hash_fn: u64) -> Option<&CF> { + pub fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> { self.functions.get(&hash_fn).map(|(_, _, _, v)| v) } @@ -524,7 +560,7 @@ impl Module { &mut self, name: &str, hash_fn_native: u64, - ) -> Result<&CF, Box> { + ) -> Result<&CallableFunction, Box> { self.all_functions.get(&hash_fn_native).ok_or_else(|| { Box::new(EvalAltResult::ErrorFunctionNotFound( name.to_string(), @@ -591,7 +627,7 @@ impl Module { module: &'a Module, qualifiers: &mut Vec<&'a str>, variables: &mut Vec<(u64, Dynamic)>, - functions: &mut Vec<(u64, CF)>, + functions: &mut Vec<(u64, CallableFunction)>, ) { for (name, m) in &module.modules { // Index all the sub-modules first. @@ -640,7 +676,7 @@ impl Module { fn_def.params.len(), empty(), ); - functions.push((hash_fn_def, CF::Script(fn_def.clone()).into())); + functions.push((hash_fn_def, CallableFunction::Script(fn_def.clone()).into())); } } diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 54b6ecea..075a1c68 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -1,6 +1,6 @@ //! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages. -use crate::fn_native::{CallableFunction, IteratorFn}; +use crate::fn_native::{CallableFunction, IteratorFn, Shared}; use crate::module::Module; use crate::utils::StaticVec; @@ -44,35 +44,27 @@ pub trait Package { fn get(&self) -> PackageLibrary; } -/// Type which `Rc`-wraps a `Module` to facilitate sharing library instances. -#[cfg(not(feature = "sync"))] -pub type PackageLibrary = Rc; - -/// Type which `Arc`-wraps a `Module` to facilitate sharing library instances. -#[cfg(feature = "sync")] -pub type PackageLibrary = Arc; +/// A sharable `Module` to facilitate sharing library instances. +pub type PackageLibrary = Shared; /// Type containing a collection of `PackageLibrary` instances. /// All function and type iterator keys in the loaded packages are indexed for fast access. #[derive(Clone, Default)] -pub(crate) struct PackagesCollection { - /// Collection of `PackageLibrary` instances. - packages: StaticVec, -} +pub(crate) struct PackagesCollection(StaticVec); impl PackagesCollection { /// Add a `PackageLibrary` into the `PackagesCollection`. pub fn push(&mut self, package: PackageLibrary) { // Later packages override previous ones. - self.packages.insert(0, package); + self.0.insert(0, package); } /// Does the specified function hash key exist in the `PackagesCollection`? pub fn contains_fn(&self, hash: u64) -> bool { - self.packages.iter().any(|p| p.contains_fn(hash)) + self.0.iter().any(|p| p.contains_fn(hash)) } /// Get specified function via its hash key. pub fn get_fn(&self, hash: u64) -> Option<&CallableFunction> { - self.packages + self.0 .iter() .map(|p| p.get_fn(hash)) .find(|f| f.is_some()) @@ -80,11 +72,11 @@ impl PackagesCollection { } /// Does the specified TypeId iterator exist in the `PackagesCollection`? pub fn contains_iter(&self, id: TypeId) -> bool { - self.packages.iter().any(|p| p.contains_iter(id)) + self.0.iter().any(|p| p.contains_iter(id)) } /// Get the specified TypeId iterator. pub fn get_iter(&self, id: TypeId) -> Option { - self.packages + self.0 .iter() .map(|p| p.get_iter(id)) .find(|f| f.is_some()) From b49e1e199a3531a1f79568b0a2511d04aa8c252a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 23 May 2020 18:59:28 +0800 Subject: [PATCH 18/53] Build-in certain common operators. --- RELEASES.md | 10 ++- benches/engine.rs | 2 +- benches/eval_expression.rs | 4 +- src/engine.rs | 168 +++++++++++++++++++++++++++++++++---- src/optimize.rs | 35 ++++---- src/packages/arithmetic.rs | 138 +++++++++++++++++------------- src/packages/logic.rs | 64 ++++++++------ src/packages/mod.rs | 2 +- src/parser.rs | 60 ++++++------- 9 files changed, 336 insertions(+), 147 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index e8fe628d..87df6bab 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -9,10 +9,18 @@ Regression * Do not optimize script with `eval_expression` - it is assumed to be one-off and short. +Breaking changes +---------------- + +* `Engine::compile_XXX` functions now return `ParseError` instead of `Box`. +* The `RegisterDynamicFn` trait is merged into the `RegisterResutlFn` trait which now always returns + `Result>`. +* Default maximum limit on levels of nested function calls is fine-tuned and set to a different value. + New features ------------ -* Set limits on maximum level of nesting expressions and statements to avoid panics during parsing. +* Set limit on maximum level of nesting expressions and statements to avoid panics during parsing. * New `EvalPackage` to disable `eval`. * More benchmarks. diff --git a/benches/engine.rs b/benches/engine.rs index 254288de..f1ac588b 100644 --- a/benches/engine.rs +++ b/benches/engine.rs @@ -29,7 +29,7 @@ fn bench_engine_new_raw_core(bench: &mut Bencher) { #[bench] fn bench_engine_register_fn(bench: &mut Bencher) { - fn hello(a: INT, b: Array, c: Map) -> bool { + fn hello(_a: INT, _b: Array, _c: Map) -> bool { true } diff --git a/benches/eval_expression.rs b/benches/eval_expression.rs index c647c1b5..8f46fb18 100644 --- a/benches/eval_expression.rs +++ b/benches/eval_expression.rs @@ -86,7 +86,7 @@ fn bench_eval_call_expression(bench: &mut Bencher) { modifierTest + 1000 / 2 > (80 * 100 % 2) "#; - let mut engine = Engine::new(); + let engine = Engine::new(); bench.iter(|| engine.eval_expression::(script).unwrap()); } @@ -101,7 +101,7 @@ fn bench_eval_call(bench: &mut Bencher) { modifierTest + 1000 / 2 > (80 * 100 % 2) "#; - let mut engine = Engine::new(); + let engine = Engine::new(); bench.iter(|| engine.eval::(script).unwrap()); } diff --git a/src/engine.rs b/src/engine.rs index cbb7dc02..18878a65 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -7,13 +7,16 @@ use crate::fn_native::{FnCallArgs, Shared}; use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; -use crate::parser::{Expr, FnAccess, FnDef, ReturnType, Stmt, AST}; +use crate::parser::{Expr, FnAccess, FnDef, ReturnType, Stmt, AST, INT}; use crate::r#unsafe::{unsafe_cast_var_name_to_lifetime, unsafe_mut_cast_to_lifetime}; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::Position; use crate::utils::StaticVec; +#[cfg(not(feature = "no_float"))] +use crate::parser::FLOAT; + #[cfg(not(feature = "no_module"))] use crate::module::{resolvers, ModuleRef, ModuleResolver}; @@ -706,6 +709,14 @@ impl Engine { }); } + // If it is a 2-operand operator, see if it is built in + if args.len() == 2 && args[0].type_id() == args[1].type_id() { + match run_builtin_op(fn_name, args[0], args[1])? { + Some(v) => return Ok((v, false)), + None => (), + } + } + // Return default value (if any) if let Some(val) = def_val { return Ok((val.clone(), false)); @@ -873,6 +884,7 @@ impl Engine { &self, state: &mut State, fn_name: &str, + native_only: bool, hash_fn_def: u64, args: &mut FnCallArgs, is_ref: bool, @@ -887,7 +899,7 @@ impl Engine { args.len(), args.iter().map(|a| a.type_id()), ); - let hashes = (hash_fn, hash_fn_def); + let hashes = (hash_fn, if native_only { 0 } else { hash_fn_def }); match fn_name { // type_of @@ -1003,7 +1015,7 @@ impl Engine { match rhs { // xxx.fn_name(arg_expr_list) Expr::FnCall(x) if x.1.is_none() => { - let ((name, pos), _, hash_fn_def, _, def_val) = x.as_ref(); + let ((name, native, pos), _, hash, _, def_val) = x.as_ref(); let def_val = def_val.as_ref(); let mut arg_values: StaticVec<_> = once(obj) @@ -1016,7 +1028,7 @@ impl Engine { .collect(); let args = arg_values.as_mut(); - self.exec_fn_call(state, name, *hash_fn_def, args, is_ref, def_val, *pos, 0) + self.exec_fn_call(state, name, *native, *hash, args, is_ref, def_val, *pos, 0) } // xxx.module::fn_name(...) - syntax error Expr::FnCall(_) => unreachable!(), @@ -1045,14 +1057,14 @@ impl Engine { Expr::Property(x) if new_val.is_some() => { let ((_, _, setter), pos) = x.as_ref(); let mut args = [obj, new_val.as_mut().unwrap()]; - self.exec_fn_call(state, setter, 0, &mut args, is_ref, None, *pos, 0) + self.exec_fn_call(state, setter, true, 0, &mut args, is_ref, None, *pos, 0) .map(|(v, _)| (v, true)) } // xxx.id Expr::Property(x) => { let ((_, getter, _), pos) = x.as_ref(); let mut args = [obj]; - self.exec_fn_call(state, getter, 0, &mut args, is_ref, None, *pos, 0) + self.exec_fn_call(state, getter, true, 0, &mut args, is_ref, None, *pos, 0) .map(|(v, _)| (v, false)) } #[cfg(not(feature = "no_object"))] @@ -1083,7 +1095,8 @@ impl Engine { let (mut val, updated) = if let Expr::Property(p) = &x.0 { let ((_, getter, _), _) = p.as_ref(); - self.exec_fn_call(state, getter, 0, &mut args[..1], is_ref, None, x.2, 0)? + let args = &mut args[..1]; + self.exec_fn_call(state, getter, true, 0, args, is_ref, None, x.2, 0)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( @@ -1104,7 +1117,7 @@ impl Engine { let ((_, _, setter), _) = p.as_ref(); // Re-use args because the first &mut parameter will not be consumed args[1] = val; - self.exec_fn_call(state, setter, 0, args, is_ref, None, x.2, 0) + self.exec_fn_call(state, setter, true, 0, args, is_ref, None, x.2, 0) .or_else(|err| match *err { // If there is no setter, no need to feed it back because the property is read-only EvalAltResult::ErrorDotExpr(_, _) => Ok(Default::default()), @@ -1306,7 +1319,7 @@ impl Engine { _ => { let type_name = self.map_type_name(val.type_name()); let args = &mut [val, &mut idx]; - self.exec_fn_call(state, FUNC_INDEXER, 0, args, is_ref, None, op_pos, 0) + self.exec_fn_call(state, FUNC_INDEXER, true, 0, args, is_ref, None, op_pos, 0) .map(|(v, _)| v.into()) .map_err(|_| { Box::new(EvalAltResult::ErrorIndexingType(type_name.into(), op_pos)) @@ -1334,7 +1347,6 @@ impl Engine { Dynamic(Union::Array(mut rhs_value)) => { let op = "=="; let def_value = false.into(); - let hash_fn_def = calc_fn_hash(empty(), op, 2, empty()); // Call the `==` operator to compare each value for value in rhs_value.iter_mut() { @@ -1345,7 +1357,7 @@ impl Engine { // Qualifiers (none) + function name + argument `TypeId`'s. let hash_fn = calc_fn_hash(empty(), op, args.len(), args.iter().map(|a| a.type_id())); - let hashes = (hash_fn, hash_fn_def); + let hashes = (hash_fn, 0); let (r, _) = self .call_fn_raw(None, state, op, hashes, args, true, def_value, pos, level)?; @@ -1488,7 +1500,7 @@ impl Engine { // Normal function call Expr::FnCall(x) if x.1.is_none() => { - let ((name, pos), _, hash_fn_def, args_expr, def_val) = x.as_ref(); + let ((name, native, pos), _, hash, args_expr, def_val) = x.as_ref(); let def_val = def_val.as_ref(); let mut arg_values = args_expr @@ -1501,7 +1513,7 @@ impl Engine { if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::() { let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::())); - if !self.has_override(state, (hash_fn, *hash_fn_def)) { + if !self.has_override(state, (hash_fn, *hash)) { // eval - only in function call style let prev_len = scope.len(); let pos = args_expr.get(0).position(); @@ -1521,14 +1533,16 @@ impl Engine { // Normal function call - except for eval (handled above) let args = args.as_mut(); - self.exec_fn_call(state, name, *hash_fn_def, args, false, def_val, *pos, level) - .map(|(v, _)| v) + self.exec_fn_call( + state, name, *native, *hash, args, false, def_val, *pos, level, + ) + .map(|(v, _)| v) } // Module-qualified function call #[cfg(not(feature = "no_module"))] Expr::FnCall(x) if x.1.is_some() => { - let ((name, pos), modules, hash_fn_def, args_expr, def_val) = x.as_ref(); + let ((name, _, pos), modules, hash_fn_def, args_expr, def_val) = x.as_ref(); let modules = modules.as_ref().unwrap(); let mut arg_values = args_expr @@ -1934,3 +1948,125 @@ impl Engine { .unwrap_or(name) } } + +/// Build in certain common operator implementations to avoid the cost of searching through the functions space. +fn run_builtin_op( + op: &str, + x: &Dynamic, + y: &Dynamic, +) -> Result, Box> { + use crate::packages::arithmetic::*; + + if x.type_id() == TypeId::of::() { + let x = x.downcast_ref::().unwrap().clone(); + let y = y.downcast_ref::().unwrap().clone(); + + #[cfg(not(feature = "unchecked"))] + match op { + "+" => return add(x, y).map(Into::::into).map(Some), + "-" => return sub(x, y).map(Into::::into).map(Some), + "*" => return mul(x, y).map(Into::::into).map(Some), + "/" => return div(x, y).map(Into::::into).map(Some), + "%" => return modulo(x, y).map(Into::::into).map(Some), + "~" => return pow_i_i(x, y).map(Into::::into).map(Some), + ">>" => return shr(x, y).map(Into::::into).map(Some), + "<<" => return shl(x, y).map(Into::::into).map(Some), + _ => (), + } + + #[cfg(feature = "unchecked")] + match op { + "+" => return Ok(Some((x + y).into())), + "-" => return Ok(Some((x - y).into())), + "*" => return Ok(Some((x * y).into())), + "/" => return Ok(Some((x / y).into())), + "%" => return Ok(Some((x % y).into())), + "~" => return pow_i_i_u(x, y).map(Into::::into).map(Some), + ">>" => return shr_u(x, y).map(Into::::into).map(Some), + "<<" => return shl_u(x, y).map(Into::::into).map(Some), + _ => (), + } + + match op { + "==" => return Ok(Some((x == y).into())), + "!=" => return Ok(Some((x != y).into())), + ">" => return Ok(Some((x > y).into())), + ">=" => return Ok(Some((x >= y).into())), + "<" => return Ok(Some((x < y).into())), + "<=" => return Ok(Some((x <= y).into())), + "&" => return Ok(Some((x & y).into())), + "|" => return Ok(Some((x | y).into())), + "^" => return Ok(Some((x ^ y).into())), + _ => (), + } + } else if x.type_id() == TypeId::of::() { + let x = x.downcast_ref::().unwrap().clone(); + let y = y.downcast_ref::().unwrap().clone(); + + match op { + "&" => return Ok(Some((x && y).into())), + "|" => return Ok(Some((x || y).into())), + "==" => return Ok(Some((x == y).into())), + "!=" => return Ok(Some((x != y).into())), + _ => (), + } + } else if x.type_id() == TypeId::of::() { + let x = x.downcast_ref::().unwrap(); + let y = y.downcast_ref::().unwrap(); + + match op { + "==" => return Ok(Some((x == y).into())), + "!=" => return Ok(Some((x != y).into())), + ">" => return Ok(Some((x > y).into())), + ">=" => return Ok(Some((x >= y).into())), + "<" => return Ok(Some((x < y).into())), + "<=" => return Ok(Some((x <= y).into())), + _ => (), + } + } else if x.type_id() == TypeId::of::() { + let x = x.downcast_ref::().unwrap().clone(); + let y = y.downcast_ref::().unwrap().clone(); + + match op { + "==" => return Ok(Some((x == y).into())), + "!=" => return Ok(Some((x != y).into())), + ">" => return Ok(Some((x > y).into())), + ">=" => return Ok(Some((x >= y).into())), + "<" => return Ok(Some((x < y).into())), + "<=" => return Ok(Some((x <= y).into())), + _ => (), + } + } else if x.type_id() == TypeId::of::<()>() { + match op { + "==" => return Ok(Some(true.into())), + "!=" | ">" | ">=" | "<" | "<=" => return Ok(Some(false.into())), + _ => (), + } + } + + #[cfg(not(feature = "no_float"))] + { + if x.type_id() == TypeId::of::() { + let x = x.downcast_ref::().unwrap().clone(); + let y = y.downcast_ref::().unwrap().clone(); + + match op { + "+" => return Ok(Some((x + y).into())), + "-" => return Ok(Some((x - y).into())), + "*" => return Ok(Some((x * y).into())), + "/" => return Ok(Some((x / y).into())), + "%" => return Ok(Some((x % y).into())), + "~" => return pow_f_f(x, y).map(Into::::into).map(Some), + "==" => return Ok(Some((x == y).into())), + "!=" => return Ok(Some((x != y).into())), + ">" => return Ok(Some((x > y).into())), + ">=" => return Ok(Some((x >= y).into())), + "<" => return Ok(Some((x < y).into())), + "<=" => return Ok(Some((x <= y).into())), + _ => (), + } + } + } + + Ok(None) +} diff --git a/src/optimize.rs b/src/optimize.rs index 1bba1196..5e9aa911 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -1,11 +1,10 @@ use crate::any::Dynamic; use crate::calc_fn_hash; use crate::engine::{ - Engine, FunctionsLib, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_PRINT, KEYWORD_TYPE_OF, + Engine, FunctionsLib, State as EngineState, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_PRINT, + KEYWORD_TYPE_OF, }; use crate::fn_native::FnCallArgs; -use crate::module::Module; -use crate::packages::PackagesCollection; use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; @@ -112,8 +111,7 @@ impl<'a> State<'a> { /// Call a registered function fn call_fn( - packages: &PackagesCollection, - global_module: &Module, + state: &State, fn_name: &str, args: &mut FnCallArgs, pos: Position, @@ -126,12 +124,21 @@ fn call_fn( args.iter().map(|a| a.type_id()), ); - global_module - .get_fn(hash_fn) - .or_else(|| packages.get_fn(hash_fn)) - .map(|func| func.get_native_fn()(args)) - .transpose() - .map_err(|err| err.new_position(pos)) + state + .engine + .call_fn_raw( + None, + &mut EngineState::new(&Default::default()), + fn_name, + (hash_fn, 0), + args, + true, + None, + pos, + 0, + ) + .map(|(v, _)| Some(v)) + .or_else(|_| Ok(None)) } /// Optimize a statement. @@ -546,10 +553,10 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { && state.optimization_level == OptimizationLevel::Full // full optimizations && x.3.iter().all(|expr| expr.is_constant()) // all arguments are constants => { - let ((name, pos), _, _, args, def_value) = x.as_mut(); + let ((name, native_only, pos), _, _, args, def_value) = x.as_mut(); // First search in script-defined functions (can override built-in) - if state.fn_lib.iter().find(|(id, len)| *id == name && *len == args.len()).is_some() { + if !*native_only && state.fn_lib.iter().find(|(id, len)| *id == name && *len == args.len()).is_some() { // A script-defined function overrides the built-in function - do not make the call x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); return Expr::FnCall(x); @@ -566,7 +573,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { "" }; - call_fn(&state.engine.packages, &state.engine.global_module, name, call_args.as_mut(), *pos).ok() + call_fn(&state, name, call_args.as_mut(), *pos).ok() .and_then(|result| result.or_else(|| { if !arg_for_type_of.is_empty() { diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index e4a26844..8f7a0b5b 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -20,7 +20,7 @@ use crate::stdlib::{ }; // Checked add -fn add(x: T, y: T) -> FuncReturn { +pub(crate) fn add(x: T, y: T) -> FuncReturn { x.checked_add(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Addition overflow: {} + {}", x, y), @@ -29,7 +29,7 @@ fn add(x: T, y: T) -> FuncReturn { }) } // Checked subtract -fn sub(x: T, y: T) -> FuncReturn { +pub(crate) fn sub(x: T, y: T) -> FuncReturn { x.checked_sub(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Subtraction underflow: {} - {}", x, y), @@ -38,7 +38,7 @@ fn sub(x: T, y: T) -> FuncReturn { }) } // Checked multiply -fn mul(x: T, y: T) -> FuncReturn { +pub(crate) fn mul(x: T, y: T) -> FuncReturn { x.checked_mul(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Multiplication overflow: {} * {}", x, y), @@ -47,7 +47,7 @@ fn mul(x: T, y: T) -> FuncReturn { }) } // Checked divide -fn div(x: T, y: T) -> FuncReturn +pub(crate) fn div(x: T, y: T) -> FuncReturn where T: Display + CheckedDiv + PartialEq + Zero, { @@ -67,7 +67,7 @@ where }) } // Checked negative - e.g. -(i32::MIN) will overflow i32::MAX -fn neg(x: T) -> FuncReturn { +pub(crate) fn neg(x: T) -> FuncReturn { x.checked_neg().ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Negation overflow: -{}", x), @@ -76,7 +76,7 @@ fn neg(x: T) -> FuncReturn { }) } // Checked absolute -fn abs(x: T) -> FuncReturn { +pub(crate) fn abs(x: T) -> FuncReturn { // FIX - We don't use Signed::abs() here because, contrary to documentation, it panics // when the number is ::MIN instead of returning ::MIN itself. if x >= ::zero() { @@ -133,7 +133,7 @@ fn binary_xor(x: T, y: T) -> FuncReturn<::Output> { Ok(x ^ y) } // Checked left-shift -fn shl(x: T, y: INT) -> FuncReturn { +pub(crate) fn shl(x: T, y: INT) -> FuncReturn { // Cannot shift by a negative number of bits if y < 0 { return Err(Box::new(EvalAltResult::ErrorArithmetic( @@ -150,7 +150,7 @@ fn shl(x: T, y: INT) -> FuncReturn { }) } // Checked right-shift -fn shr(x: T, y: INT) -> FuncReturn { +pub(crate) fn shr(x: T, y: INT) -> FuncReturn { // Cannot shift by a negative number of bits if y < 0 { return Err(Box::new(EvalAltResult::ErrorArithmetic( @@ -167,15 +167,15 @@ fn shr(x: T, y: INT) -> FuncReturn { }) } // Unchecked left-shift - may panic if shifting by a negative number of bits -fn shl_u>(x: T, y: T) -> FuncReturn<>::Output> { +pub(crate) fn shl_u>(x: T, y: T) -> FuncReturn<>::Output> { Ok(x.shl(y)) } // Unchecked right-shift - may panic if shifting by a negative number of bits -fn shr_u>(x: T, y: T) -> FuncReturn<>::Output> { +pub(crate) fn shr_u>(x: T, y: T) -> FuncReturn<>::Output> { Ok(x.shr(y)) } // Checked modulo -fn modulo(x: T, y: T) -> FuncReturn { +pub(crate) fn modulo(x: T, y: T) -> FuncReturn { x.checked_rem(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Modulo division by zero or overflow: {} % {}", x, y), @@ -188,7 +188,7 @@ fn modulo_u(x: T, y: T) -> FuncReturn<::Output> { Ok(x % y) } // Checked power -fn pow_i_i(x: INT, y: INT) -> FuncReturn { +pub(crate) fn pow_i_i(x: INT, y: INT) -> FuncReturn { #[cfg(not(feature = "only_i32"))] { if y > (u32::MAX as INT) { @@ -229,17 +229,17 @@ fn pow_i_i(x: INT, y: INT) -> FuncReturn { } } // Unchecked integer power - may panic on overflow or if the power index is too high (> u32::MAX) -fn pow_i_i_u(x: INT, y: INT) -> FuncReturn { +pub(crate) fn pow_i_i_u(x: INT, y: INT) -> FuncReturn { Ok(x.pow(y as u32)) } // Floating-point power - always well-defined #[cfg(not(feature = "no_float"))] -fn pow_f_f(x: FLOAT, y: FLOAT) -> FuncReturn { +pub(crate) fn pow_f_f(x: FLOAT, y: FLOAT) -> FuncReturn { Ok(x.powf(y)) } // Checked power #[cfg(not(feature = "no_float"))] -fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn { +pub(crate) fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn { // Raise to power that is larger than an i32 if y > (i32::MAX as INT) { return Err(Box::new(EvalAltResult::ErrorArithmetic( @@ -253,7 +253,7 @@ fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn { // Unchecked power - may be incorrect if the power index is too high (> i32::MAX) #[cfg(feature = "unchecked")] #[cfg(not(feature = "no_float"))] -fn pow_f_i_u(x: FLOAT, y: INT) -> FuncReturn { +pub(crate) fn pow_f_i_u(x: FLOAT, y: INT) -> FuncReturn { Ok(x.powi(y as i32)) } @@ -272,97 +272,118 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { // Checked basic arithmetic #[cfg(not(feature = "unchecked"))] { - reg_op!(lib, "+", add, INT); - reg_op!(lib, "-", sub, INT); - reg_op!(lib, "*", mul, INT); - reg_op!(lib, "/", div, INT); + // reg_op!(lib, "+", add, INT); + // reg_op!(lib, "-", sub, INT); + // reg_op!(lib, "*", mul, INT); + // reg_op!(lib, "/", div, INT); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - reg_op!(lib, "+", add, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "-", sub, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "*", mul, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "/", div, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "+", add, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "-", sub, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "*", mul, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "/", div, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "+", add, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "-", sub, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "*", mul, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "/", div, i8, u8, i16, u16, i32, u32, u64, i128, u128); } } // Unchecked basic arithmetic #[cfg(feature = "unchecked")] { - reg_op!(lib, "+", add_u, INT); - reg_op!(lib, "-", sub_u, INT); - reg_op!(lib, "*", mul_u, INT); - reg_op!(lib, "/", div_u, INT); + // reg_op!(lib, "+", add_u, INT); + // reg_op!(lib, "-", sub_u, INT); + // reg_op!(lib, "*", mul_u, INT); + // reg_op!(lib, "/", div_u, INT); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - reg_op!(lib, "+", add_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "-", sub_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "*", mul_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "/", div_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "+", add_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "-", sub_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "*", mul_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "/", div_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "+", add_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "-", sub_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "*", mul_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "/", div_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); } } // Basic arithmetic for floating-point - no need to check #[cfg(not(feature = "no_float"))] { - reg_op!(lib, "+", add_u, f32, f64); - reg_op!(lib, "-", sub_u, f32, f64); - reg_op!(lib, "*", mul_u, f32, f64); - reg_op!(lib, "/", div_u, f32, f64); + // reg_op!(lib, "+", add_u, f32, f64); + // reg_op!(lib, "-", sub_u, f32, f64); + // reg_op!(lib, "*", mul_u, f32, f64); + // reg_op!(lib, "/", div_u, f32, f64); + reg_op!(lib, "+", add_u, f32); + reg_op!(lib, "-", sub_u, f32); + reg_op!(lib, "*", mul_u, f32); + reg_op!(lib, "/", div_u, f32); } // Bit operations - reg_op!(lib, "|", binary_or, INT); - reg_op!(lib, "&", binary_and, INT); - reg_op!(lib, "^", binary_xor, INT); + // reg_op!(lib, "|", binary_or, INT); + // reg_op!(lib, "&", binary_and, INT); + // reg_op!(lib, "^", binary_xor, INT); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - reg_op!(lib, "|", binary_or, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "&", binary_and, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "^", binary_xor, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "|", binary_or, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "&", binary_and, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "^", binary_xor, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "|", binary_or, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "&", binary_and, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "^", binary_xor, i8, u8, i16, u16, i32, u32, u64, i128, u128); } // Checked bit shifts #[cfg(not(feature = "unchecked"))] { - reg_op!(lib, "<<", shl, INT); - reg_op!(lib, ">>", shr, INT); - reg_op!(lib, "%", modulo, INT); + // reg_op!(lib, "<<", shl, INT); + // reg_op!(lib, ">>", shr, INT); + // reg_op!(lib, "%", modulo, INT); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - reg_op!(lib, "<<", shl, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, ">>", shr, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "%", modulo, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "<<", shl, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, ">>", shr, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "%", modulo, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "<<", shl, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, ">>", shr, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "%", modulo, i8, u8, i16, u16, i32, u32, u64, i128, u128); } } // Unchecked bit shifts #[cfg(feature = "unchecked")] { - reg_op!(lib, "<<", shl_u, INT, INT); - reg_op!(lib, ">>", shr_u, INT, INT); - reg_op!(lib, "%", modulo_u, INT); + // reg_op!(lib, "<<", shl_u, INT, INT); + // reg_op!(lib, ">>", shr_u, INT, INT); + // reg_op!(lib, "%", modulo_u, INT); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - reg_op!(lib, "<<", shl_u, i64, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, ">>", shr_u, i64, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "%", modulo_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "<<", shl_u, i64, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, ">>", shr_u, i64, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "%", modulo_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "<<", shl_u, i64, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, ">>", shr_u, i64, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "%", modulo_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); } } // Checked power #[cfg(not(feature = "unchecked"))] { - lib.set_fn_2("~", pow_i_i); + // lib.set_fn_2("~", pow_i_i); #[cfg(not(feature = "no_float"))] lib.set_fn_2("~", pow_f_i); @@ -371,7 +392,7 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { // Unchecked power #[cfg(feature = "unchecked")] { - lib.set_fn_2("~", pow_i_i_u); + // lib.set_fn_2("~", pow_i_i_u); #[cfg(not(feature = "no_float"))] lib.set_fn_2("~", pow_f_i_u); @@ -380,8 +401,9 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { // Floating-point modulo and power #[cfg(not(feature = "no_float"))] { - reg_op!(lib, "%", modulo_u, f32, f64); - lib.set_fn_2("~", pow_f_f); + // reg_op!(lib, "%", modulo_u, f32, f64); + reg_op!(lib, "%", modulo_u, f32); + // lib.set_fn_2("~", pow_f_f); } // Checked unary diff --git a/src/packages/logic.rs b/src/packages/logic.rs index ef771688..03a12e9d 100644 --- a/src/packages/logic.rs +++ b/src/packages/logic.rs @@ -42,40 +42,52 @@ macro_rules! reg_op { } def_package!(crate:LogicPackage:"Logical operators.", lib, { - reg_op!(lib, "<", lt, INT, char); - reg_op!(lib, "<=", lte, INT, char); - reg_op!(lib, ">", gt, INT, char); - reg_op!(lib, ">=", gte, INT, char); - reg_op!(lib, "==", eq, INT, char, bool, ()); - reg_op!(lib, "!=", ne, INT, char, bool, ()); + // reg_op!(lib, "<", lt, INT, char); + // reg_op!(lib, "<=", lte, INT, char); + // reg_op!(lib, ">", gt, INT, char); + // reg_op!(lib, ">=", gte, INT, char); + // reg_op!(lib, "==", eq, INT, char, bool, ()); + // reg_op!(lib, "!=", ne, INT, char, bool, ()); // Special versions for strings - at least avoid copying the first string - lib.set_fn_2_mut("<", |x: &mut String, y: String| Ok(*x < y)); - lib.set_fn_2_mut("<=", |x: &mut String, y: String| Ok(*x <= y)); - lib.set_fn_2_mut(">", |x: &mut String, y: String| Ok(*x > y)); - lib.set_fn_2_mut(">=", |x: &mut String, y: String| Ok(*x >= y)); - lib.set_fn_2_mut("==", |x: &mut String, y: String| Ok(*x == y)); - lib.set_fn_2_mut("!=", |x: &mut String, y: String| Ok(*x != y)); + // lib.set_fn_2_mut("<", |x: &mut String, y: String| Ok(*x < y)); + // lib.set_fn_2_mut("<=", |x: &mut String, y: String| Ok(*x <= y)); + // lib.set_fn_2_mut(">", |x: &mut String, y: String| Ok(*x > y)); + // lib.set_fn_2_mut(">=", |x: &mut String, y: String| Ok(*x >= y)); + // lib.set_fn_2_mut("==", |x: &mut String, y: String| Ok(*x == y)); + // lib.set_fn_2_mut("!=", |x: &mut String, y: String| Ok(*x != y)); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - reg_op!(lib, "<", lt, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "<=", lte, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, ">", gt, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, ">=", gte, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "==", eq, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "!=", ne, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "<", lt, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "<=", lte, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, ">", gt, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, ">=", gte, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "==", eq, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // reg_op!(lib, "!=", ne, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "<", lt, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "<=", lte, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, ">", gt, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, ">=", gte, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "==", eq, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "!=", ne, i8, u8, i16, u16, i32, u32, u64, i128, u128); } #[cfg(not(feature = "no_float"))] { - reg_op!(lib, "<", lt, f32, f64); - reg_op!(lib, "<=", lte, f32, f64); - reg_op!(lib, ">", gt, f32, f64); - reg_op!(lib, ">=", gte, f32, f64); - reg_op!(lib, "==", eq, f32, f64); - reg_op!(lib, "!=", ne, f32, f64); + // reg_op!(lib, "<", lt, f32, f64); + // reg_op!(lib, "<=", lte, f32, f64); + // reg_op!(lib, ">", gt, f32, f64); + // reg_op!(lib, ">=", gte, f32, f64); + // reg_op!(lib, "==", eq, f32, f64); + // reg_op!(lib, "!=", ne, f32, f64); + reg_op!(lib, "<", lt, f32); + reg_op!(lib, "<=", lte, f32); + reg_op!(lib, ">", gt, f32); + reg_op!(lib, ">=", gte, f32); + reg_op!(lib, "==", eq, f32); + reg_op!(lib, "!=", ne, f32); } // `&&` and `||` are treated specially as they short-circuit. @@ -83,7 +95,7 @@ def_package!(crate:LogicPackage:"Logical operators.", lib, { //reg_op!(lib, "||", or, bool); //reg_op!(lib, "&&", and, bool); - lib.set_fn_2("|", or); - lib.set_fn_2("&", and); + // lib.set_fn_2("|", or); + // lib.set_fn_2("&", and); lib.set_fn_1("!", not); }); diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 075a1c68..ddd6a30d 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -6,7 +6,7 @@ use crate::utils::StaticVec; use crate::stdlib::{any::TypeId, boxed::Box, collections::HashMap, rc::Rc, sync::Arc, vec::Vec}; -mod arithmetic; +pub(crate) mod arithmetic; mod array_basic; mod eval; mod iter_basic; diff --git a/src/parser.rs b/src/parser.rs index 3c3581eb..928a81ff 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -389,12 +389,12 @@ pub enum Expr { Property(Box<((String, String, String), Position)>), /// { stmt } Stmt(Box<(Stmt, Position)>), - /// func(expr, ... ) - ((function name, position), optional modules, hash, arguments, optional default value) + /// func(expr, ... ) - ((function name, native_only, position), optional modules, hash, arguments, optional default value) /// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls /// and the function names are predictable, so no need to allocate a new `String`. FnCall( Box<( - (Cow<'static, str>, Position), + (Cow<'static, str>, bool, Position), Option>, u64, StaticVec, @@ -503,7 +503,7 @@ impl Expr { Self::Property(x) => x.1, Self::Stmt(x) => x.1, Self::Variable(x) => (x.0).1, - Self::FnCall(x) => (x.0).1, + Self::FnCall(x) => (x.0).2, Self::And(x) | Self::Or(x) | Self::In(x) => x.2, @@ -527,7 +527,7 @@ impl Expr { Self::Variable(x) => (x.0).1 = new_pos, Self::Property(x) => x.1 = new_pos, Self::Stmt(x) => x.1 = new_pos, - Self::FnCall(x) => (x.0).1 = new_pos, + Self::FnCall(x) => (x.0).2 = new_pos, Self::And(x) => x.2 = new_pos, Self::Or(x) => x.2 = new_pos, Self::In(x) => x.2 = new_pos, @@ -761,7 +761,7 @@ fn parse_call_expr<'a>( let hash_fn_def = calc_fn_hash(empty(), &id, empty()); return Ok(Expr::FnCall(Box::new(( - (id.into(), begin), + (id.into(), false, begin), modules, hash_fn_def, args, @@ -802,7 +802,7 @@ fn parse_call_expr<'a>( let hash_fn_def = calc_fn_hash(empty(), &id, args_iter); return Ok(Expr::FnCall(Box::new(( - (id.into(), begin), + (id.into(), false, begin), modules, hash_fn_def, args, @@ -1347,7 +1347,7 @@ fn parse_unary<'a>( args.push(expr); Ok(Expr::FnCall(Box::new(( - (op.into(), pos), + (op.into(), true, pos), None, hash, args, @@ -1371,7 +1371,7 @@ fn parse_unary<'a>( let hash = calc_fn_hash(empty(), op, 2, empty()); Ok(Expr::FnCall(Box::new(( - (op.into(), pos), + (op.into(), true, pos), None, hash, args, @@ -1473,7 +1473,7 @@ fn parse_op_assignment_stmt<'a>( args.push(rhs); let hash = calc_fn_hash(empty(), &op, args.len(), empty()); - let rhs_expr = Expr::FnCall(Box::new(((op, pos), None, hash, args, None))); + let rhs_expr = Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))); make_assignment_stmt(state, lhs, rhs_expr, pos) } @@ -1767,26 +1767,30 @@ fn parse_binary_op<'a>( args.push(rhs); root = match op_token { - Token::Plus => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), - Token::Minus => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), - Token::Multiply => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), - Token::Divide => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), + Token::Plus => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), + Token::Minus => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), + Token::Multiply => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), + Token::Divide => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), - Token::LeftShift => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), - Token::RightShift => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), - Token::Modulo => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), - Token::PowerOf => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), + Token::LeftShift => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), + Token::RightShift => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), + Token::Modulo => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), + Token::PowerOf => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), // Comparison operators default to false when passed invalid operands - Token::EqualsTo => Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))), - Token::NotEqualsTo => Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))), - Token::LessThan => Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))), - Token::LessThanEqualsTo => { - Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))) + Token::EqualsTo => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))), + Token::NotEqualsTo => { + Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))) + } + Token::LessThan => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))), + Token::LessThanEqualsTo => { + Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))) + } + Token::GreaterThan => { + Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))) } - Token::GreaterThan => Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))), Token::GreaterThanEqualsTo => { - Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))) + Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))) } Token::Or => { @@ -1799,9 +1803,9 @@ fn parse_binary_op<'a>( let current_lhs = args.pop(); Expr::And(Box::new((current_lhs, rhs, pos))) } - Token::Ampersand => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), - Token::Pipe => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), - Token::XOr => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), + Token::Ampersand => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), + Token::Pipe => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), + Token::XOr => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), Token::In => { let rhs = args.pop(); @@ -1817,7 +1821,7 @@ fn parse_binary_op<'a>( match &mut rhs { // current_lhs.rhs(...) - method call Expr::FnCall(x) => { - let ((id, _), _, hash, args, _) = x.as_mut(); + let ((id, _, _), _, hash, args, _) = x.as_mut(); // Recalculate function call hash because there is an additional argument *hash = calc_fn_hash(empty(), id, args.len() + 1, empty()); } From d56634cac7148132196b7480d177ccecf586b17e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 24 May 2020 00:29:06 +0800 Subject: [PATCH 19/53] Complete built-in operators. --- Cargo.toml | 18 ++++------ README.md | 92 ++++++++++++++++++++++++++---------------------- RELEASES.md | 11 ++++++ src/engine.rs | 26 ++++---------- src/parser.rs | 11 +++--- tests/modules.rs | 91 ++++++++++++++++++++++++----------------------- tests/stack.rs | 4 ++- 7 files changed, 131 insertions(+), 122 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 981f1241..6239e658 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,26 +20,22 @@ categories = [ "no-std", "embedded", "parser-implementations" ] num-traits = { version = "0.2.11", default-features = false } [features] -#default = ["no_stdlib", "no_function", "no_index", "no_object", "no_module", "no_float", "only_i32", "unchecked", "no_optimize", "sync"] +#default = ["unchecked", "sync", "no_optimize", "no_float", "only_i32", "no_index", "no_object", "no_function", "no_module"] default = [] unchecked = [] # unchecked arithmetic -no_index = [] # no arrays and indexing -no_float = [] # no floating-point -no_function = [] # no script-defined functions -no_object = [] # no custom objects +sync = [] # restrict to only types that implement Send + Sync no_optimize = [] # no script optimizer -no_module = [] # no modules +no_float = [] # no floating-point only_i32 = [] # set INT=i32 (useful for 32-bit systems) only_i64 = [] # set INT=i64 (default) and disable support for all other integer types -sync = [] # restrict to only types that implement Send + Sync +no_index = [] # no arrays and indexing +no_object = [] # no custom objects +no_function = [] # no script-defined functions +no_module = [] # no modules # compiling for no-std no_std = [ "num-traits/libm", "hashbrown", "core-error", "libm", "ahash" ] -# other developer features -no_stdlib = [] # do not register the standard library -optimize_full = [] # set optimization level to Full (default is Simple) - this is a feature used only to simplify testing - [profile.release] lto = "fat" codegen-units = 1 diff --git a/README.md b/README.md index fc04a54e..a2f13f3a 100644 --- a/README.md +++ b/README.md @@ -68,19 +68,19 @@ Beware that in order to use pre-releases (e.g. alpha and beta), the exact versio Optional features ----------------- -| Feature | Description | -| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `unchecked` | Exclude arithmetic checking (such as over-flows and division by zero), stack depth limit and operations count limit. Beware that a bad script may panic the entire system! | -| `no_function` | Disable script-defined functions. | -| `no_index` | Disable [arrays] and indexing features. | -| `no_object` | Disable support for custom types and object maps. | -| `no_float` | Disable floating-point numbers and math. | -| `no_optimize` | Disable the script optimizer. | -| `no_module` | Disable modules. | -| `only_i32` | Set the system integer type to `i32` and disable all other integer types. `INT` is set to `i32`. | -| `only_i64` | Set the system integer type to `i64` and disable all other integer types. `INT` is set to `i64`. | -| `no_std` | Build for `no-std`. Notice that additional dependencies will be pulled in to replace `std` features. | -| `sync` | Restrict all values types to those that are `Send + Sync`. Under this feature, all Rhai types, including [`Engine`], [`Scope`] and `AST`, are all `Send + Sync`. | +| Feature | Description | +| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `unchecked` | Disable arithmetic checking (such as over-flows and division by zero), call stack depth limit, operations count limit and modules loading limit. Beware that a bad script may panic the entire system! | +| `sync` | Restrict all values types to those that are `Send + Sync`. Under this feature, all Rhai types, including [`Engine`], [`Scope`] and `AST`, are all `Send + Sync`. | +| `no_optimize` | Disable the script optimizer. | +| `no_float` | Disable floating-point numbers and math. | +| `only_i32` | Set the system integer type to `i32` and disable all other integer types. `INT` is set to `i32`. | +| `only_i64` | Set the system integer type to `i64` and disable all other integer types. `INT` is set to `i64`. | +| `no_index` | Disable [arrays] and indexing features. | +| `no_object` | Disable support for custom types and [object maps]. | +| `no_function` | Disable script-defined functions. | +| `no_module` | Disable loading modules. | +| `no_std` | Build for `no-std`. Notice that additional dependencies will be pulled in to replace `std` features. | By default, Rhai includes all the standard functionalities in a small, tight package. Most features are here to opt-**out** of certain functionalities that are not needed. @@ -88,16 +88,16 @@ Excluding unneeded functionalities can result in smaller, faster builds as well as more control over what a script can (or cannot) do. [`unchecked`]: #optional-features -[`no_index`]: #optional-features -[`no_float`]: #optional-features -[`no_function`]: #optional-features -[`no_object`]: #optional-features +[`sync`]: #optional-features [`no_optimize`]: #optional-features -[`no_module`]: #optional-features +[`no_float`]: #optional-features [`only_i32`]: #optional-features [`only_i64`]: #optional-features +[`no_index`]: #optional-features +[`no_object`]: #optional-features +[`no_function`]: #optional-features +[`no_module`]: #optional-features [`no_std`]: #optional-features -[`sync`]: #optional-features ### Performance builds @@ -133,9 +133,9 @@ Omitting arrays (`no_index`) yields the most code-size savings, followed by floa (`no_float`), checked arithmetic (`unchecked`) and finally object maps and custom types (`no_object`). Disable script-defined functions (`no_function`) only when the feature is not needed because code size savings is minimal. -[`Engine::new_raw`](#raw-engine) creates a _raw_ engine which does not register _any_ utility functions. -This makes the scripting language quite useless as even basic arithmetic operators are not supported. -Selectively include the necessary functionalities by loading specific [packages] to minimize the footprint. +[`Engine::new_raw`](#raw-engine) creates a _raw_ engine. +A _raw_ engine supports, out of the box, only a very restricted set of basic arithmetic and logical operators. +Selectively include other necessary functionalities by loading specific [packages] to minimize the footprint. Packages are sharable (even across threads via the [`sync`] feature), so they only have to be created once. Related @@ -376,7 +376,8 @@ Raw `Engine` `Engine::new` creates a scripting [`Engine`] with common functionalities (e.g. printing to the console via `print`). In many controlled embedded environments, however, these are not needed. -Use `Engine::new_raw` to create a _raw_ `Engine`, in which _nothing_ is added, not even basic arithmetic and logic operators! +Use `Engine::new_raw` to create a _raw_ `Engine`, in which only a minimal set of basic arithmetic and logical operators +are supported. ### Packages @@ -400,20 +401,20 @@ engine.load_package(package.get()); // load the package manually. 'g The follow packages are available: -| Package | Description | In `CorePackage` | In `StandardPackage` | -| ---------------------- | ----------------------------------------------- | :--------------: | :------------------: | -| `ArithmeticPackage` | Arithmetic operators (e.g. `+`, `-`, `*`, `/`) | Yes | Yes | -| `BasicIteratorPackage` | Numeric ranges (e.g. `range(1, 10)`) | Yes | Yes | -| `LogicPackage` | Logic and comparison operators (e.g. `==`, `>`) | Yes | Yes | -| `BasicStringPackage` | Basic string functions | Yes | Yes | -| `BasicTimePackage` | Basic time functions (e.g. [timestamps]) | Yes | Yes | -| `MoreStringPackage` | Additional string functions | No | Yes | -| `BasicMathPackage` | Basic math functions (e.g. `sin`, `sqrt`) | No | Yes | -| `BasicArrayPackage` | Basic [array] functions | No | Yes | -| `BasicMapPackage` | Basic [object map] functions | No | Yes | -| `EvalPackage` | Disable [`eval`] | No | No | -| `CorePackage` | Basic essentials | | | -| `StandardPackage` | Standard library | | | +| Package | Description | In `CorePackage` | In `StandardPackage` | +| ---------------------- | -------------------------------------------------------------------------- | :--------------: | :------------------: | +| `ArithmeticPackage` | Arithmetic operators (e.g. `+`, `-`, `*`, `/`) for different numeric types | Yes | Yes | +| `BasicIteratorPackage` | Numeric ranges (e.g. `range(1, 10)`) | Yes | Yes | +| `LogicPackage` | Logical and comparison operators (e.g. `==`, `>`) | Yes | Yes | +| `BasicStringPackage` | Basic string functions | Yes | Yes | +| `BasicTimePackage` | Basic time functions (e.g. [timestamps]) | Yes | Yes | +| `MoreStringPackage` | Additional string functions | No | Yes | +| `BasicMathPackage` | Basic math functions (e.g. `sin`, `sqrt`) | No | Yes | +| `BasicArrayPackage` | Basic [array] functions | No | Yes | +| `BasicMapPackage` | Basic [object map] functions | No | Yes | +| `EvalPackage` | Disable [`eval`] | No | No | +| `CorePackage` | Basic essentials | | | +| `StandardPackage` | Standard library | | | Packages typically contain Rust functions that are callable within a Rhai script. All functions registered in a package is loaded under the _global namespace_ (i.e. they're available without module qualifiers). @@ -795,7 +796,7 @@ let result: i64 = engine.eval("1 + 1.0"); // prints 2.0 (normally an e ``` Use operator overloading for custom types (described below) only. -Be very careful when overloading built-in operators because script writers expect standard operators to behave in a +Be very careful when overloading built-in operators because script authors expect standard operators to behave in a consistent and predictable manner, and will be annoyed if a calculation for '`+`' turns into a subtraction, for example. Operator overloading also impacts script optimization when using [`OptimizationLevel::Full`]. @@ -2345,6 +2346,10 @@ engine.set_max_modules(5); // allow loading only up to 5 module engine.set_max_modules(0); // allow unlimited modules ``` +A script attempting to load more than the maximum number of modules will terminate with an error result. +This check can be disabled via the [`unchecked`] feature for higher performance +(but higher risks as well). + ### Maximum call stack depth Rhai by default limits function calls to a maximum depth of 128 levels (16 levels in debug build). @@ -2366,6 +2371,8 @@ engine.set_max_call_levels(0); // allow no function calls at all (m ``` A script exceeding the maximum call stack depth will terminate with an error result. +This check can be disabled via the [`unchecked`] feature for higher performance +(but higher risks as well). ### Maximum statement depth @@ -2409,15 +2416,17 @@ Make sure that `C x ( 5 + F ) + S` layered calls do not cause a stack overflow, A script exceeding the maximum nesting depths will terminate with a parsing error. The malignant `AST` will not be able to get past parsing in the first place. -The limits can be disabled via the [`unchecked`] feature for higher performance +This check can be disabled via the [`unchecked`] feature for higher performance (but higher risks as well). ### Checked arithmetic By default, all arithmetic calculations in Rhai are _checked_, meaning that the script terminates with an error whenever it detects a numeric over-flow/under-flow condition or an invalid -floating-point operation, instead of crashing the entire system. This checking can be turned off -via the [`unchecked`] feature for higher performance (but higher risks as well). +floating-point operation, instead of crashing the entire system. + +This checking can be turned off via the [`unchecked`] feature for higher performance +(but higher risks as well). ### Blocking access to external data @@ -2433,7 +2442,6 @@ engine.register_get("add", add); // configure 'engine' let engine = engine; // shadow the variable so that 'engine' is now immutable ``` - Script optimization =================== diff --git a/RELEASES.md b/RELEASES.md index 87df6bab..42d2f77d 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -16,6 +16,8 @@ Breaking changes * The `RegisterDynamicFn` trait is merged into the `RegisterResutlFn` trait which now always returns `Result>`. * Default maximum limit on levels of nested function calls is fine-tuned and set to a different value. +* Some operator functions are now built in (see _Speed enhancements_ below), so they are available even + when under `Engine::new_raw`. New features ------------ @@ -24,6 +26,15 @@ New features * New `EvalPackage` to disable `eval`. * More benchmarks. +Speed enhancements +------------------ + +* Common operators (e.g. `+`, `>`, `==`) now call into highly efficient built-in implementations for standard types + (i.e. `INT`, `FLOAT`, `bool`, `char`, `()` and some `String`) if not overridden by a registered function. + This yields a 5-10% speed benefit depending on script operator usage. +* Implementations of common operators for standard types are removed from the `ArithmeticPackage` and `LogicPackage` + (and therefore the `CorePackage`) because they are now always available, even under `Engine::new_raw`. + Version 0.14.1 ============== diff --git a/src/engine.rs b/src/engine.rs index 18878a65..0e9b41f7 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -392,13 +392,8 @@ impl Default for Engine { optimization_level: OptimizationLevel::None, #[cfg(not(feature = "no_optimize"))] - #[cfg(not(feature = "optimize_full"))] optimization_level: OptimizationLevel::Simple, - #[cfg(not(feature = "no_optimize"))] - #[cfg(feature = "optimize_full")] - optimization_level: OptimizationLevel::Full, - max_call_stack_depth: MAX_CALL_STACK_DEPTH, max_expr_depth: MAX_EXPR_DEPTH, max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, @@ -406,10 +401,6 @@ impl Default for Engine { max_modules: u64::MAX, }; - #[cfg(feature = "no_stdlib")] - engine.load_package(CorePackage::new().get()); - - #[cfg(not(feature = "no_stdlib"))] engine.load_package(StandardPackage::new().get()); engine @@ -534,13 +525,8 @@ impl Engine { optimization_level: OptimizationLevel::None, #[cfg(not(feature = "no_optimize"))] - #[cfg(not(feature = "optimize_full"))] optimization_level: OptimizationLevel::Simple, - #[cfg(not(feature = "no_optimize"))] - #[cfg(feature = "optimize_full")] - optimization_level: OptimizationLevel::Full, - max_call_stack_depth: MAX_CALL_STACK_DEPTH, max_expr_depth: MAX_EXPR_DEPTH, max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, @@ -635,13 +621,15 @@ impl Engine { ) -> Result<(Dynamic, bool), Box> { self.inc_operations(state, pos)?; + let native_only = hashes.1 == 0; + // Check for stack overflow if level > self.max_call_stack_depth { return Err(Box::new(EvalAltResult::ErrorStackOverflow(pos))); } // First search in script-defined functions (can override built-in) - if hashes.1 > 0 { + if !native_only { if let Some(fn_def) = state.get_function(hashes.1) { let (result, state2) = self.call_script_fn(scope, *state, fn_name, fn_def, args, pos, level)?; @@ -710,8 +698,8 @@ impl Engine { } // If it is a 2-operand operator, see if it is built in - if args.len() == 2 && args[0].type_id() == args[1].type_id() { - match run_builtin_op(fn_name, args[0], args[1])? { + if native_only && args.len() == 2 && args[0].type_id() == args[1].type_id() { + match run_builtin_binary_op(fn_name, args[0], args[1])? { Some(v) => return Ok((v, false)), None => (), } @@ -1949,8 +1937,8 @@ impl Engine { } } -/// Build in certain common operator implementations to avoid the cost of searching through the functions space. -fn run_builtin_op( +/// Build in common binary operator implementations to avoid the cost of calling a registered function. +fn run_builtin_binary_op( op: &str, x: &Dynamic, y: &Dynamic, diff --git a/src/parser.rs b/src/parser.rs index 928a81ff..a1d86fc0 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -710,8 +710,7 @@ fn parse_call_expr<'a>( input: &mut Peekable>, state: &mut ParseState, id: String, - #[cfg(not(feature = "no_module"))] mut modules: Option>, - #[cfg(feature = "no_module")] modules: Option, + mut modules: Option>, begin: Position, level: usize, allow_stmt_expr: bool, @@ -753,12 +752,13 @@ fn parse_call_expr<'a>( 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, empty()); + let hash_fn_def = calc_fn_hash(empty(), &id, 0, empty()); return Ok(Expr::FnCall(Box::new(( (id.into(), false, begin), @@ -794,12 +794,13 @@ fn parse_call_expr<'a>( 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 + dummy parameter types (one for each parameter). + // Qualifiers (none) + function name + number of arguments. #[cfg(feature = "no_module")] - let hash_fn_def = calc_fn_hash(empty(), &id, args_iter); + let hash_fn_def = calc_fn_hash(empty(), &id, args.len(), empty()); return Ok(Expr::FnCall(Box::new(( (id.into(), false, begin), diff --git a/tests/modules.rs b/tests/modules.rs index 4e475d1c..6e49a43c 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -80,63 +80,66 @@ fn test_module_resolver() -> Result<(), Box> { 42 ); - engine.set_max_modules(5); + #[cfg(not(feature = "unchecked"))] + { + engine.set_max_modules(5); - assert!(matches!( - *engine - .eval::( - r#" - let sum = 0; + assert!(matches!( + *engine + .eval::( + r#" + let sum = 0; - for x in range(0, 10) { - import "hello" as h; - sum += h::answer; - } + for x in range(0, 10) { + import "hello" as h; + sum += h::answer; + } - sum + sum "# - ) - .expect_err("should error"), - EvalAltResult::ErrorTooManyModules(_) - )); + ) + .expect_err("should error"), + EvalAltResult::ErrorTooManyModules(_) + )); - #[cfg(not(feature = "no_function"))] - assert!(matches!( - *engine - .eval::( - r#" - let sum = 0; + #[cfg(not(feature = "no_function"))] + assert!(matches!( + *engine + .eval::( + r#" + let sum = 0; - fn foo() { - import "hello" as h; - sum += h::answer; - } + fn foo() { + import "hello" as h; + sum += h::answer; + } - for x in range(0, 10) { - foo(); - } + for x in range(0, 10) { + foo(); + } - sum + sum "# - ) - .expect_err("should error"), - EvalAltResult::ErrorInFunctionCall(fn_name, _, _) if fn_name == "foo" - )); + ) + .expect_err("should error"), + EvalAltResult::ErrorInFunctionCall(fn_name, _, _) if fn_name == "foo" + )); - engine.set_max_modules(0); + engine.set_max_modules(0); - #[cfg(not(feature = "no_function"))] - engine.eval::<()>( - r#" - fn foo() { - import "hello" as h; - } + #[cfg(not(feature = "no_function"))] + engine.eval::<()>( + r#" + fn foo() { + import "hello" as h; + } - for x in range(0, 10) { - foo(); - } + for x in range(0, 10) { + foo(); + } "#, - )?; + )?; + } Ok(()) } diff --git a/tests/stack.rs b/tests/stack.rs index c0cb8a38..1ba72e6a 100644 --- a/tests/stack.rs +++ b/tests/stack.rs @@ -1,7 +1,8 @@ -#![cfg(not(feature = "no_function"))] +#![cfg(not(feature = "unchecked"))] use rhai::{Engine, EvalAltResult, ParseErrorType}; #[test] +#[cfg(not(feature = "no_function"))] fn test_stack_overflow_fn_calls() -> Result<(), Box> { let engine = Engine::new(); @@ -73,6 +74,7 @@ fn test_stack_overflow_parsing() -> Result<(), Box> { err if err.error_type() == &ParseErrorType::ExprTooDeep )); + #[cfg(not(feature = "no_function"))] engine.compile("fn abc(x) { x + 1 }")?; Ok(()) From 1798d4d6a0bab3deb938312b191cccd901c356a2 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 24 May 2020 11:57:46 +0800 Subject: [PATCH 20/53] Fix function call optimizations. --- src/engine.rs | 10 +++++----- src/optimize.rs | 22 +++++++++++----------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 0e9b41f7..b708ecf5 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -698,7 +698,7 @@ impl Engine { } // If it is a 2-operand operator, see if it is built in - if native_only && args.len() == 2 && args[0].type_id() == args[1].type_id() { + if !is_ref && native_only && args.len() == 2 && args[0].type_id() == args[1].type_id() { match run_builtin_binary_op(fn_name, args[0], args[1])? { Some(v) => return Ok((v, false)), None => (), @@ -1327,7 +1327,7 @@ impl Engine { ) -> Result> { self.inc_operations(state, rhs.position())?; - let mut lhs_value = self.eval_expr(scope, state, lhs, level)?; + let lhs_value = self.eval_expr(scope, state, lhs, level)?; let rhs_value = self.eval_expr(scope, state, rhs, level)?; match rhs_value { @@ -1338,17 +1338,17 @@ impl Engine { // Call the `==` operator to compare each value for value in rhs_value.iter_mut() { - let args = &mut [&mut lhs_value, value]; + let args = &mut [&mut lhs_value.clone(), value]; let def_value = Some(&def_value); let pos = rhs.position(); - // Qualifiers (none) + function name + argument `TypeId`'s. + // Qualifiers (none) + function name + number of arguments + argument `TypeId`'s. let hash_fn = calc_fn_hash(empty(), op, args.len(), args.iter().map(|a| a.type_id())); let hashes = (hash_fn, 0); let (r, _) = self - .call_fn_raw(None, state, op, hashes, args, true, def_value, pos, level)?; + .call_fn_raw(None, state, op, hashes, args, false, def_value, pos, level)?; if r.as_bool().unwrap_or(false) { return Ok(true.into()); } diff --git a/src/optimize.rs b/src/optimize.rs index 5e9aa911..e2acc6d0 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -110,18 +110,18 @@ impl<'a> State<'a> { } /// Call a registered function -fn call_fn( +fn call_fn_with_constant_arguments( state: &State, fn_name: &str, - args: &mut FnCallArgs, + arg_values: &mut [Dynamic], pos: Position, ) -> Result, Box> { // Search built-in's and external functions let hash_fn = calc_fn_hash( empty(), fn_name, - args.len(), - args.iter().map(|a| a.type_id()), + arg_values.len(), + arg_values.iter().map(|a| a.type_id()), ); state @@ -131,8 +131,8 @@ fn call_fn( &mut EngineState::new(&Default::default()), fn_name, (hash_fn, 0), - args, - true, + arg_values.iter_mut().collect::>().as_mut(), + false, None, pos, 0, @@ -556,24 +556,24 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { let ((name, native_only, pos), _, _, args, def_value) = x.as_mut(); // First search in script-defined functions (can override built-in) - if !*native_only && state.fn_lib.iter().find(|(id, len)| *id == name && *len == args.len()).is_some() { + // Cater for both normal function call style and method call style (one additional arguments) + if !*native_only && state.fn_lib.iter().find(|(id, len)| *id == name && (*len == args.len() || *len == args.len() + 1)).is_some() { // A script-defined function overrides the built-in function - do not make the call x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); return Expr::FnCall(x); } let mut arg_values: StaticVec<_> = args.iter().map(Expr::get_constant_value).collect(); - let mut call_args: StaticVec<_> = arg_values.iter_mut().collect(); // Save the typename of the first argument if it is `type_of()` // This is to avoid `call_args` being passed into the closure - let arg_for_type_of = if name == KEYWORD_TYPE_OF && call_args.len() == 1 { - state.engine.map_type_name(call_args[0].type_name()) + let arg_for_type_of = if name == KEYWORD_TYPE_OF && arg_values.len() == 1 { + state.engine.map_type_name(arg_values[0].type_name()) } else { "" }; - call_fn(&state, name, call_args.as_mut(), *pos).ok() + call_fn_with_constant_arguments(&state, name, arg_values.as_mut(), *pos).ok() .and_then(|result| result.or_else(|| { if !arg_for_type_of.is_empty() { From 65ee262f1b41521208aefd1748deac99ccc8a765 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 24 May 2020 12:40:28 +0800 Subject: [PATCH 21/53] Refine README. --- README.md | 143 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 87 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index a2f13f3a..24027eec 100644 --- a/README.md +++ b/README.md @@ -23,14 +23,14 @@ Rhai's current features set: * Relatively little `unsafe` code (yes there are some for performance reasons, and most `unsafe` code is limited to one single source file, all with names starting with `"unsafe_"`). * Re-entrant scripting [`Engine`] can be made `Send + Sync` (via the [`sync`] feature). -* Sand-boxed - the scripting [`Engine`], if declared immutable, cannot mutate the containing environment without explicit permission. +* Sand-boxed - the scripting [`Engine`], if declared immutable, cannot mutate the containing environment unless explicitly permitted (e.g. via a `RefCell`). * Rugged (protection against [stack-overflow](#maximum-call-stack-depth) and [runaway scripts](#maximum-number-of-operations) etc.). * Track script evaluation [progress](#tracking-progress) and manually terminate a script run. * [`no-std`](#optional-features) support. * [Function overloading](#function-overloading). * [Operator overloading](#operator-overloading). * Organize code base with dynamically-loadable [Modules]. -* Compiled script is [optimized](#script-optimization) for repeated evaluations. +* Scripts are [optimized](#script-optimization) (useful for template-based machine-generated scripts) for repeated evaluations. * Support for [minimal builds](#minimal-builds) by excluding unneeded language [features](#optional-features). * Very few additional dependencies (right now only [`num-traits`](https://crates.io/crates/num-traits/) to do checked arithmetic operations); for [`no-std`](#optional-features) builds, a number of additional dependencies are @@ -55,7 +55,7 @@ Use the latest released crate version on [`crates.io`](https::/crates.io/crates/ rhai = "*" ``` -Crate versions are released on [`crates.io`](https::/crates.io/crates/rhai/) infrequently, so if you want to track the +Crate versions are released on [`crates.io`](https::/crates.io/crates/rhai/) infrequently, so to track the latest features, enhancements and bug fixes, pull directly from GitHub: ```toml @@ -134,7 +134,7 @@ Omitting arrays (`no_index`) yields the most code-size savings, followed by floa Disable script-defined functions (`no_function`) only when the feature is not needed because code size savings is minimal. [`Engine::new_raw`](#raw-engine) creates a _raw_ engine. -A _raw_ engine supports, out of the box, only a very restricted set of basic arithmetic and logical operators. +A _raw_ engine supports, out of the box, only a very [restricted set](#built-in-operators) of basic arithmetic and logical operators. Selectively include other necessary functionalities by loading specific [packages] to minimize the footprint. Packages are sharable (even across threads via the [`sync`] feature), so they only have to be created once. @@ -168,7 +168,7 @@ Examples can be run with the following command: cargo run --example name ``` -The `repl` example is a particularly good one as it allows you to interactively try out Rhai's +The `repl` example is a particularly good one as it allows one to interactively try out Rhai's language features in a standard REPL (**R**ead-**E**val-**P**rint **L**oop). Example Scripts @@ -379,6 +379,17 @@ In many controlled embedded environments, however, these are not needed. Use `Engine::new_raw` to create a _raw_ `Engine`, in which only a minimal set of basic arithmetic and logical operators are supported. +### Built-in operators + +| Operator | Supported for type (see [standard types]) | +| ---------------------------- | -------------------------------------------------------------------- | +| `+`, `-`, `*`, `/`, `%`, `~` | `INT`, `FLOAT` (if not [`no_float`]) | +| `<<`, `>>`, `^` | `INT` | +| `&`, `\|` | `INT`, `bool` | +| `&&`, `\|\|` | `bool` | +| `==`, `!=` | `INT`, `FLOAT` (if not [`no_float`]), `bool`, `char`, `()`, `String` | +| `>`, `>=`, `<`, `<=` | `INT`, `FLOAT` (if not [`no_float`]), `char`, `()`, `String` | + ### Packages [package]: #packages @@ -459,6 +470,7 @@ Values and types [`type_of()`]: #values-and-types [`to_string()`]: #values-and-types [`()`]: #values-and-types +[standard types]: #values-and-types The following primitive types are supported natively: @@ -475,7 +487,7 @@ The following primitive types are supported natively: | **Dynamic value** (i.e. can be anything) | `rhai::Dynamic` | _the actual type_ | _actual value_ | | **System integer** (current configuration) | `rhai::INT` (`i32` or `i64`) | `"i32"` or `"i64"` | `"42"`, `"123"` etc. | | **System floating-point** (current configuration, disabled with [`no_float`]) | `rhai::FLOAT` (`f32` or `f64`) | `"f32"` or `"f64"` | `"123.456"` etc. | -| **Nothing/void/nil/null** (or whatever you want to call it) | `()` | `"()"` | `""` _(empty string)_ | +| **Nothing/void/nil/null** (or whatever it is called) | `()` | `"()"` | `""` _(empty string)_ | All types are treated strictly separate by Rhai, meaning that `i32` and `i64` and `u32` are completely different - they even cannot be added together. This is very similar to Rust. @@ -563,7 +575,7 @@ let value: i64 = item.cast(); // type can also be inferred let value = item.try_cast::().unwrap(); // 'try_cast' does not panic when the cast fails, but returns 'None' ``` -The `type_name` method gets the name of the actual type as a static string slice, which you may match against. +The `type_name` method gets the name of the actual type as a static string slice, which can be `match`-ed against. ```rust let list: Array = engine.eval("...")?; // return type is 'Array' @@ -637,7 +649,7 @@ fn add(x: i64, y: i64) -> i64 { // Function that returns a 'Dynamic' value - must return a 'Result' fn get_any_value() -> Result> { - Ok((42_i64).into()) // standard supported types can use 'into()' + Ok((42_i64).into()) // standard types can use 'into()' } fn main() -> Result<(), Box> @@ -662,12 +674,12 @@ fn main() -> Result<(), Box> ``` To create a [`Dynamic`] value, use the `Dynamic::from` method. -Standard supported types in Rhai can also use `into()`. +[Standard types] in Rhai can also use `into()`. ```rust use rhai::Dynamic; -let x = (42_i64).into(); // 'into()' works for standard supported types +let x = (42_i64).into(); // 'into()' works for standard types let y = Dynamic::from(String::from("hello!")); // remember &str is not supported by Rhai ``` @@ -805,7 +817,7 @@ See the [relevant section](#script-optimization) for more details. Custom types and methods ----------------------- -Here's an more complete example of working with Rust. First the example, then we'll break it into parts: +A more complete example of working with Rust: ```rust use rhai::{Engine, EvalAltResult}; @@ -843,8 +855,8 @@ fn main() -> Result<(), Box> } ``` -All custom types must implement `Clone`. This allows the [`Engine`] to pass by value. -You can turn off support for custom types via the [`no_object`] feature. +All custom types must implement `Clone` as this allows the [`Engine`] to pass by value. +Support for custom types can be turned off via the [`no_object`] feature. ```rust #[derive(Clone)] @@ -853,11 +865,12 @@ struct TestStruct { } ``` -Next, we create a few methods that we'll later use in our scripts. Notice that we register our custom type with the [`Engine`]. +Next, create a few methods for later use in scripts. +Notice that the custom type needs to be _registered_ with the [`Engine`]. ```rust impl TestStruct { - fn update(&mut self) { + fn update(&mut self) { // methods take &mut as first parameter self.field += 41; } @@ -871,19 +884,19 @@ let engine = Engine::new(); engine.register_type::(); ``` -To use native types, methods and functions with the [`Engine`], we need to register them. -There are some convenience functions to help with these. Below, the `update` and `new` methods are registered with the [`Engine`]. +To use native types, methods and functions with the [`Engine`], simply register them using one of the `Engine::register_XXX` API. +Below, the `update` and `new` methods are registered using `Engine::register_fn`. -*Note: [`Engine`] follows the convention that methods use a `&mut` first parameter so that invoking methods -can update the value in memory.* +***Note**: Rhai follows the convention that methods of custom types take a `&mut` first parameter so that invoking methods +can update the custom types. All other parameters in Rhai are passed by value (i.e. clones).* ```rust engine.register_fn("update", TestStruct::update); // registers 'update(&mut TestStruct)' engine.register_fn("new_ts", TestStruct::new); // registers 'new()' ``` -Finally, we call our script. The script can see the function and method we registered earlier. -We need to get the result back out from script land just as before, this time casting to our custom struct type. +The custom type is then ready for us in scripts. Scripts can see the functions and methods registered earlier. +Get the evaluation result back out from script-land just as before, this time casting to the custom type: ```rust let result = engine.eval::("let x = new_ts(); x.update(); x")?; @@ -891,18 +904,19 @@ let result = engine.eval::("let x = new_ts(); x.update(); x")?; println!("result: {}", result.field); // prints 42 ``` -In fact, any function with a first argument (either by copy or via a `&mut` reference) can be used as a method call -on that type because internally they are the same thing: -methods on a type is implemented as a functions taking a `&mut` first argument. +In fact, any function with a first argument that is a `&mut` reference can be used as method calls because +internally they are the same thing: methods on a type is implemented as a functions taking a `&mut` first argument. ```rust fn foo(ts: &mut TestStruct) -> i64 { ts.field } -engine.register_fn("foo", foo); +engine.register_fn("foo", foo); // register ad hoc function with correct signature -let result = engine.eval::("let x = new_ts(); x.foo()")?; +let result = engine.eval::( + "let x = new_ts(); x.foo()" // 'foo' can be called like a method on 'x' +)?; println!("result: {}", result); // prints 1 ``` @@ -916,7 +930,7 @@ let result = engine.eval::("let x = [1, 2, 3]; x.len()")?; ``` [`type_of()`] works fine with custom types and returns the name of the type. -If `register_type_with_name` is used to register the custom type +If `Engine::register_type_with_name` is used to register the custom type with a special "pretty-print" name, [`type_of()`] will return that name instead. ```rust @@ -1238,7 +1252,7 @@ number = -5 - +5; Numeric functions ----------------- -The following standard functions (defined in the [`BasicMathPackage`] but excluded if using a [raw `Engine`]) operate on +The following standard functions (defined in the [`BasicMathPackage`](#packages) but excluded if using a [raw `Engine`]) operate on `i8`, `i16`, `i32`, `i64`, `f32` and `f64` only: | Function | Description | @@ -1701,10 +1715,10 @@ if now.elapsed() > 30.0 { Comparison operators -------------------- -Comparing most values of the same data type work out-of-the-box for standard types supported by the system. +Comparing most values of the same data type work out-of-the-box for all [standard types] supported by the system. -However, if using a [raw `Engine`], comparisons can only be made between restricted system types - -`INT` (`i64` or `i32` depending on [`only_i32`] and [`only_i64`]), `f64` (if not [`no_float`]), [string], [array], `bool`, `char`. +However, if using a [raw `Engine`] without loading any [packages], comparisons can only be made between a limited +set of types (see [built-in operators](#built-in-operators)). ```rust 42 == 42; // true @@ -2269,7 +2283,7 @@ so that it does not consume more resources that it is allowed to. The most important resources to watch out for are: * **Memory**: A malignant script may continuously grow an [array] or [object map] until all memory is consumed. - It may also create a large [array] or [objecct map] literal that exhausts all memory during parsing. + It may also create a large [array] or [object map] literal that exhausts all memory during parsing. * **CPU**: A malignant script may run an infinite tight loop that consumes all CPU cycles. * **Time**: A malignant script may run indefinitely, thereby blocking the calling system which is waiting for a result. * **Stack**: A malignant script may attempt an infinite recursive call that exhausts the call stack. @@ -2459,7 +2473,7 @@ For example, in the following: 123; // eliminated: no effect "hello"; // eliminated: no effect [1, 2, x, x*2, 5]; // eliminated: no effect - foo(42); // NOT eliminated: the function 'foo' may have side effects + foo(42); // NOT eliminated: the function 'foo' may have side-effects 666 // NOT eliminated: this is the return value of the block, // and the block is the last one so this is the return value of the whole script } @@ -2510,7 +2524,7 @@ if DECISION == 1 { // NOT optimized away because you can define } ``` -because no operator functions will be run (in order not to trigger side effects) during the optimization process +because no operator functions will be run (in order not to trigger side-effects) during the optimization process (unless the optimization level is set to [`OptimizationLevel::Full`]). So, instead, do this: ```rust @@ -2532,7 +2546,7 @@ if DECISION_1 { In general, boolean constants are most effective for the optimizer to automatically prune large `if`-`else` branches because they do not depend on operators. -Alternatively, turn the optimizer to [`OptimizationLevel::Full`] +Alternatively, turn the optimizer to [`OptimizationLevel::Full`]. Here be dragons! ================ @@ -2548,7 +2562,7 @@ There are actually three levels of optimizations: `None`, `Simple` and `Full`. * `None` is obvious - no optimization on the AST is performed. -* `Simple` (default) performs relatively _safe_ optimizations without causing side effects +* `Simple` (default) performs only relatively _safe_ optimizations without causing side-effects (i.e. it only relies on static analysis and will not actually perform any function calls). * `Full` is _much_ more aggressive, _including_ running functions on constant arguments to determine their result. @@ -2603,28 +2617,32 @@ let x = (1+2)*3-4/5%6; // <- will be replaced by 'let x = 9' let y = (1>2) || (3<=4); // <- will be replaced by 'let y = true' ``` -Function side effect considerations ----------------------------------- +Side-effect considerations +-------------------------- All of Rhai's built-in functions (and operators which are implemented as functions) are _pure_ (i.e. they do not mutate state -nor cause side any effects, with the exception of `print` and `debug` which are handled specially) so using -[`OptimizationLevel::Full`] is usually quite safe _unless_ you register your own types and functions. +nor cause any side-effects, with the exception of `print` and `debug` which are handled specially) so using +[`OptimizationLevel::Full`] is usually quite safe _unless_ custom types and functions are registered. If custom functions are registered, they _may_ be called (or maybe not, if the calls happen to lie within a pruned code block). -If custom functions are registered to replace built-in operators, they will also be called when the operators are used -(in an `if` statement, for example) and cause side-effects. +If custom functions are registered to overload built-in operators, they will also be called when the operators are used +(in an `if` statement, for example) causing side-effects. -Function volatility considerations ---------------------------------- +Therefore, the rule-of-thumb is: _always_ register custom types and functions _after_ compiling scripts if + [`OptimizationLevel::Full`] is used. _DO NOT_ depend on knowledge that the functions have no side-effects, + because those functions can change later on and, when that happens, existing scripts may break in subtle ways. -Even if a custom function does not mutate state nor cause side effects, it may still be _volatile_, i.e. it _depends_ -on the external environment and is not _pure_. A perfect example is a function that gets the current time - -obviously each run will return a different value! The optimizer, when using [`OptimizationLevel::Full`], _assumes_ that -all functions are _pure_, so when it finds constant arguments it will eagerly execute the function call. -This causes the script to behave differently from the intended semantics because essentially the result of the function call -will always be the same value. +Volatility considerations +------------------------- -Therefore, **avoid using [`OptimizationLevel::Full`]** if you intend to register non-_pure_ custom types and/or functions. +Even if a custom function does not mutate state nor cause side-effects, it may still be _volatile_, +i.e. it _depends_ on the external environment and is not _pure_. +A perfect example is a function that gets the current time - obviously each run will return a different value! +The optimizer, when using [`OptimizationLevel::Full`], will _merrily assume_ that all functions are _pure_, +so when it finds constant arguments (or none) it eagerly executes the function call and replaces it with the result. +This causes the script to behave differently from the intended semantics. + +Therefore, **avoid using [`OptimizationLevel::Full`]** if non-_pure_ custom types and/or functions are involved. Subtle semantic changes ----------------------- @@ -2659,7 +2677,7 @@ print("end!"); In the script above, if `my_decision` holds anything other than a boolean value, the script should have been terminated due to a type error. However, after optimization, the entire `if` statement is removed (because an access to `my_decision` produces -no side effects), thus the script silently runs to completion without errors. +no side-effects), thus the script silently runs to completion without errors. Turning off optimizations ------------------------- @@ -2674,6 +2692,8 @@ let engine = rhai::Engine::new(); engine.set_optimization_level(rhai::OptimizationLevel::None); ``` +Alternatively, turn off optimizations via the [`no_optimize`] feature. + `eval` - or "How to Shoot Yourself in the Foot even Easier" --------------------------------------------------------- @@ -2722,8 +2742,8 @@ x += 32; print(x); ``` -For those who subscribe to the (very sensible) motto of ["`eval` is **evil**"](http://linterrors.com/js/eval-is-evil), -disable `eval` by overriding it, probably with something that throws. +For those who subscribe to the (very sensible) motto of ["`eval` is evil"](http://linterrors.com/js/eval-is-evil), +disable `eval` by overloading it, probably with something that throws. ```rust fn eval(script) { throw "eval is evil! I refuse to run " + script } @@ -2731,7 +2751,7 @@ fn eval(script) { throw "eval is evil! I refuse to run " + script } let x = eval("40 + 2"); // 'eval' here throws "eval is evil! I refuse to run 40 + 2" ``` -Or override it from Rust: +Or overload it from Rust: ```rust fn alt_eval(script: String) -> Result<(), Box> { @@ -2741,4 +2761,15 @@ fn alt_eval(script: String) -> Result<(), Box> { engine.register_result_fn("eval", alt_eval); ``` -There is even a [package] named `EvalPackage` which implements the disabling override. +There is even a package named [`EvalPackage`](#packages) which implements the disabling override: + +```rust +use rhai::Engine; +use rhai::packages::Package // load the 'Package' trait to use packages +use rhai::packages::EvalPackage; // the 'eval' package disables 'eval' + +let mut engine = Engine::new(); +let package = EvalPackage::new(); // create the package + +engine.load_package(package.get()); // load the package +``` From 0374311cf6c2f1bf90e847c58fc93c9e846ed978 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 24 May 2020 16:40:00 +0800 Subject: [PATCH 22/53] Optimize hot path of operators calling. --- src/api.rs | 23 +-- src/engine.rs | 411 ++++++++++++++++++++++++++------------------- src/fn_register.rs | 6 - src/module.rs | 8 +- src/optimize.rs | 85 +++++----- src/parser.rs | 10 +- 6 files changed, 294 insertions(+), 249 deletions(-) diff --git a/src/api.rs b/src/api.rs index 05dc5ec3..89e045c1 100644 --- a/src/api.rs +++ b/src/api.rs @@ -909,12 +909,12 @@ impl Engine { scope: &mut Scope, ast: &AST, ) -> Result<(Dynamic, u64), Box> { - let mut state = State::new(ast.fn_lib()); + let mut state = State::new(); ast.statements() .iter() .try_fold(().into(), |_, stmt| { - self.eval_stmt(scope, &mut state, stmt, 0) + self.eval_stmt(scope, &mut state, ast.lib(), stmt, 0) }) .or_else(|err| match *err { EvalAltResult::Return(out, _) => Ok(out), @@ -980,12 +980,12 @@ impl Engine { scope: &mut Scope, ast: &AST, ) -> Result<(), Box> { - let mut state = State::new(ast.fn_lib()); + let mut state = State::new(); ast.statements() .iter() .try_fold(().into(), |_, stmt| { - self.eval_stmt(scope, &mut state, stmt, 0) + self.eval_stmt(scope, &mut state, ast.lib(), stmt, 0) }) .map_or_else( |err| match *err { @@ -1041,17 +1041,18 @@ impl Engine { ) -> Result> { let mut arg_values = args.into_vec(); let mut args: StaticVec<_> = arg_values.iter_mut().collect(); - let fn_lib = ast.fn_lib(); + let lib = ast.lib(); let pos = Position::none(); - let fn_def = fn_lib + let fn_def = lib .get_function_by_signature(name, args.len(), true) .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.into(), pos)))?; - let state = State::new(fn_lib); + let mut state = State::new(); let args = args.as_mut(); - let (result, _) = self.call_script_fn(Some(scope), state, name, fn_def, args, pos, 0)?; + let result = + self.call_script_fn(Some(scope), &mut state, &lib, name, fn_def, args, pos, 0)?; let return_type = self.map_type_name(result.type_name()); @@ -1081,14 +1082,14 @@ impl Engine { mut ast: AST, optimization_level: OptimizationLevel, ) -> AST { - let fn_lib = ast - .fn_lib() + let lib = ast + .lib() .iter() .map(|(_, fn_def)| fn_def.as_ref().clone()) .collect(); let stmt = mem::take(ast.statements_mut()); - optimize_into_ast(self, scope, stmt, fn_lib, optimization_level) + optimize_into_ast(self, scope, stmt, lib, optimization_level) } /// Register a callback for script evaluation progress. diff --git a/src/engine.rs b/src/engine.rs index b708ecf5..c97146db 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -32,9 +32,7 @@ use crate::stdlib::{ mem, num::NonZeroUsize, ops::{Deref, DerefMut}, - rc::Rc, string::{String, ToString}, - sync::Arc, vec::Vec, }; @@ -171,11 +169,8 @@ impl> From for Target<'_> { /// /// This type uses some unsafe code, mainly for avoiding cloning of local variable names via /// direct lifetime casting. -#[derive(Debug, Clone, Copy)] -pub struct State<'a> { - /// Global script-defined functions. - pub fn_lib: &'a FunctionsLib, - +#[derive(Debug, Clone, Default)] +pub struct State { /// Normally, access to variables are parsed with a relative offset into the scope to avoid a lookup. /// In some situation, e.g. after running an `eval` statement, subsequent offsets may become mis-aligned. /// When that happens, this flag is turned on to force a scope lookup by name. @@ -190,26 +185,39 @@ pub struct State<'a> { /// Number of modules loaded. pub modules: u64, + + /// Times built-in function used. + pub use_builtin_counter: usize, + + /// Number of modules loaded. + pub use_builtin_binary_op: HashMap, } -impl<'a> State<'a> { +impl State { /// Create a new `State`. - pub fn new(fn_lib: &'a FunctionsLib) -> Self { - Self { - fn_lib, - always_search: false, - scope_level: 0, - operations: 0, - modules: 0, + pub fn new() -> Self { + Default::default() + } + /// Cache a built-in binary op function. + pub fn set_builtin_binary_op(&mut self, fn_name: impl Into, typ: TypeId) { + // Only cache when exceeding a certain number of calls + if self.use_builtin_counter < 10 { + self.use_builtin_counter += 1; + } else { + self.use_builtin_binary_op.insert(fn_name.into(), typ); } } - /// Does a certain script-defined function exist in the `State`? - pub fn has_function(&self, hash: u64) -> bool { - self.fn_lib.contains_key(&hash) - } - /// Get a script-defined function definition from the `State`. - pub fn get_function(&self, hash: u64) -> Option<&FnDef> { - self.fn_lib.get(&hash).map(|fn_def| fn_def.as_ref()) + /// Is a binary op known to be built-in? + pub fn use_builtin_binary_op(&self, fn_name: impl AsRef, args: &[&mut Dynamic]) -> bool { + if self.use_builtin_counter < 10 { + false + } else if args.len() != 2 { + false + } else if args[0].type_id() != args[1].type_id() { + false + } else { + self.use_builtin_binary_op.get(fn_name.as_ref()) == Some(&args[0].type_id()) + } } } @@ -378,7 +386,7 @@ impl Default for Engine { #[cfg(feature = "no_std")] module_resolver: None, - type_names: Default::default(), + type_names: HashMap::new(), // default print/debug implementations print: Box::new(default_print), @@ -516,7 +524,7 @@ impl Engine { #[cfg(not(feature = "no_module"))] module_resolver: None, - type_names: Default::default(), + type_names: HashMap::new(), print: Box::new(|_| {}), debug: Box::new(|_| {}), progress: None, @@ -611,6 +619,7 @@ impl Engine { &self, scope: Option<&mut Scope>, state: &mut State, + lib: &FunctionsLib, fn_name: &str, hashes: (u64, u64), args: &mut FnCallArgs, @@ -630,77 +639,94 @@ impl Engine { // First search in script-defined functions (can override built-in) if !native_only { - if let Some(fn_def) = state.get_function(hashes.1) { - let (result, state2) = - self.call_script_fn(scope, *state, fn_name, fn_def, args, pos, level)?; - *state = state2; + if let Some(fn_def) = lib.get(&hashes.1) { + let result = + self.call_script_fn(scope, state, lib, fn_name, fn_def, args, pos, level)?; return Ok((result, false)); } } - // Search built-in's and external functions - if let Some(func) = self - .global_module - .get_fn(hashes.0) - .or_else(|| self.packages.get_fn(hashes.0)) - { - // Calling pure function in method-call? - let mut this_copy: Option; - let mut this_pointer: Option<&mut Dynamic> = None; + let builtin = state.use_builtin_binary_op(fn_name, args); - if func.is_pure() && is_ref && args.len() > 0 { - // Clone the original value. It'll be consumed because the function - // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) - this_copy = Some(args[0].clone()); + if is_ref || !native_only || !builtin { + // Search built-in's and external functions + if let Some(func) = self + .global_module + .get_fn(hashes.0) + .or_else(|| self.packages.get_fn(hashes.0)) + { + // Calling pure function in method-call? + let mut this_copy: Option; + let mut this_pointer: Option<&mut Dynamic> = None; - // Replace the first reference with a reference to the clone, force-casting the lifetime. - // Keep the original reference. Must remember to restore it before existing this function. - this_pointer = Some(mem::replace( - args.get_mut(0).unwrap(), - unsafe_mut_cast_to_lifetime(this_copy.as_mut().unwrap()), - )); + if func.is_pure() && is_ref && args.len() > 0 { + // Clone the original value. It'll be consumed because the function + // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) + this_copy = Some(args[0].clone()); + + // Replace the first reference with a reference to the clone, force-casting the lifetime. + // Keep the original reference. Must remember to restore it before existing this function. + this_pointer = Some(mem::replace( + args.get_mut(0).unwrap(), + unsafe_mut_cast_to_lifetime(this_copy.as_mut().unwrap()), + )); + } + + // Run external function + let result = func.get_native_fn()(args); + + // Restore the original reference + if let Some(this_pointer) = this_pointer { + mem::replace(args.get_mut(0).unwrap(), this_pointer); + } + + let result = result.map_err(|err| err.new_position(pos))?; + + // See if the function match print/debug (which requires special processing) + return Ok(match fn_name { + KEYWORD_PRINT => ( + (self.print)(result.as_str().map_err(|type_name| { + Box::new(EvalAltResult::ErrorMismatchOutputType( + type_name.into(), + pos, + )) + })?) + .into(), + false, + ), + KEYWORD_DEBUG => ( + (self.debug)(result.as_str().map_err(|type_name| { + Box::new(EvalAltResult::ErrorMismatchOutputType( + type_name.into(), + pos, + )) + })?) + .into(), + false, + ), + _ => (result, func.is_method()), + }); } - - // Run external function - let result = func.get_native_fn()(args); - - // Restore the original reference - if let Some(this_pointer) = this_pointer { - mem::replace(args.get_mut(0).unwrap(), this_pointer); - } - - let result = result.map_err(|err| err.new_position(pos))?; - - // See if the function match print/debug (which requires special processing) - return Ok(match fn_name { - KEYWORD_PRINT => ( - (self.print)(result.as_str().map_err(|type_name| { - Box::new(EvalAltResult::ErrorMismatchOutputType( - type_name.into(), - pos, - )) - })?) - .into(), - false, - ), - KEYWORD_DEBUG => ( - (self.debug)(result.as_str().map_err(|type_name| { - Box::new(EvalAltResult::ErrorMismatchOutputType( - type_name.into(), - pos, - )) - })?) - .into(), - false, - ), - _ => (result, func.is_method()), - }); } - // If it is a 2-operand operator, see if it is built in - if !is_ref && native_only && args.len() == 2 && args[0].type_id() == args[1].type_id() { + // If it is a 2-operand operator, see if it is built in. + // Only consider situations where: + // 1) It is not a method call, + // 2) the call explicitly specifies `native_only` because, remember, an `eval` may create a new function, so you + // can never predict what functions lie in `lib`! + // 3) It is already a built-in run once before, or both parameter types are the same + if !is_ref + && native_only + && (builtin || (args.len() == 2 && args[0].type_id() == args[1].type_id())) + { match run_builtin_binary_op(fn_name, args[0], args[1])? { - Some(v) => return Ok((v, false)), + Some(v) => { + // Mark the function call as built-in + if !builtin { + state.set_builtin_binary_op(fn_name, args[0].type_id()); + } + return Ok((v, false)); + } None => (), } } @@ -753,16 +779,17 @@ impl Engine { /// Function call arguments may be _consumed_ when the function requires them to be passed by value. /// All function arguments not in the first position are always passed by value and thus consumed. /// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`! - pub(crate) fn call_script_fn<'s>( + pub(crate) fn call_script_fn( &self, scope: Option<&mut Scope>, - mut state: State<'s>, + state: &mut State, + lib: &FunctionsLib, fn_name: &str, fn_def: &FnDef, args: &mut FnCallArgs, pos: Position, level: usize, - ) -> Result<(Dynamic, State<'s>), Box> { + ) -> Result> { let orig_scope_level = state.scope_level; state.scope_level += 1; @@ -779,15 +806,14 @@ impl Engine { .iter() .zip(args.iter_mut().map(|v| mem::take(*v))) .map(|(name, value)| { - let var_name = - unsafe_cast_var_name_to_lifetime(name.as_str(), &mut state); + let var_name = unsafe_cast_var_name_to_lifetime(name.as_str(), state); (var_name, ScopeEntryType::Normal, value) }), ); // Evaluate the function at one higher level of call depth let result = self - .eval_stmt(scope, &mut state, &fn_def.body, level + 1) + .eval_stmt(scope, state, lib, &fn_def.body, level + 1) .or_else(|err| match *err { // Convert return statement to return value EvalAltResult::Return(x, _) => Ok(x), @@ -809,7 +835,7 @@ impl Engine { scope.rewind(scope_len); state.scope_level = orig_scope_level; - return result.map(|v| (v, state)); + return result; } // No new scope - create internal scope _ => { @@ -827,7 +853,7 @@ impl Engine { // Evaluate the function at one higher level of call depth let result = self - .eval_stmt(&mut scope, &mut state, &fn_def.body, level + 1) + .eval_stmt(&mut scope, state, lib, &fn_def.body, level + 1) .or_else(|err| match *err { // Convert return statement to return value EvalAltResult::Return(x, _) => Ok(x), @@ -846,19 +872,19 @@ impl Engine { }); state.scope_level = orig_scope_level; - return result.map(|v| (v, state)); + return result; } } } // Has a system function an override? - fn has_override(&self, state: &State, hashes: (u64, u64)) -> bool { + fn has_override(&self, lib: &FunctionsLib, hashes: (u64, u64)) -> bool { // First check registered functions self.global_module.contains_fn(hashes.0) // Then check packages || self.packages.contains_fn(hashes.0) // Then check script-defined functions - || state.has_function(hashes.1) + || lib.contains_key(&hashes.1) } // Perform an actual function call, taking care of special functions @@ -871,6 +897,7 @@ impl Engine { fn exec_fn_call( &self, state: &mut State, + lib: &FunctionsLib, fn_name: &str, native_only: bool, hash_fn_def: u64, @@ -891,13 +918,13 @@ impl Engine { match fn_name { // type_of - KEYWORD_TYPE_OF if args.len() == 1 && !self.has_override(state, hashes) => Ok(( + KEYWORD_TYPE_OF if args.len() == 1 && !self.has_override(lib, hashes) => Ok(( self.map_type_name(args[0].type_name()).to_string().into(), false, )), // eval - reaching this point it must be a method-style call - KEYWORD_EVAL if args.len() == 1 && !self.has_override(state, hashes) => { + KEYWORD_EVAL if args.len() == 1 && !self.has_override(lib, hashes) => { Err(Box::new(EvalAltResult::ErrorRuntime( "'eval' should not be called in method style. Try eval(...);".into(), pos, @@ -906,7 +933,7 @@ impl Engine { // Normal function call _ => self.call_fn_raw( - None, state, fn_name, hashes, args, is_ref, def_val, pos, level, + None, state, lib, fn_name, hashes, args, is_ref, def_val, pos, level, ), } } @@ -916,6 +943,7 @@ impl Engine { &self, scope: &mut Scope, state: &mut State, + lib: &FunctionsLib, script: &Dynamic, pos: Position, ) -> Result> { @@ -932,14 +960,14 @@ impl Engine { )?; // If new functions are defined within the eval string, it is an error - if ast.fn_lib().len() > 0 { + if ast.lib().len() > 0 { return Err(Box::new(EvalAltResult::ErrorParsing( ParseErrorType::WrongFnDefinition.into_err(pos), ))); } let statements = mem::take(ast.statements_mut()); - let ast = AST::new(statements, state.fn_lib.clone()); + let ast = AST::new(statements, lib.clone()); // Evaluate the AST let (result, operations) = self @@ -956,6 +984,7 @@ impl Engine { fn eval_dot_index_chain_helper( &self, state: &mut State, + lib: &FunctionsLib, target: &mut Target, rhs: &Expr, idx_values: &mut StaticVec, @@ -979,24 +1008,33 @@ impl Engine { let is_idx = matches!(rhs, Expr::Index(_)); let pos = x.0.position(); let this_ptr = &mut self - .get_indexed_mut(state, obj, is_ref, idx_val, pos, op_pos, false)?; + .get_indexed_mut(state, lib, obj, is_ref, idx_val, pos, op_pos, false)?; self.eval_dot_index_chain_helper( - state, this_ptr, &x.1, idx_values, is_idx, x.2, level, new_val, + state, lib, this_ptr, &x.1, idx_values, is_idx, x.2, level, new_val, ) } // xxx[rhs] = new_val _ if new_val.is_some() => { let pos = rhs.position(); let this_ptr = &mut self - .get_indexed_mut(state, obj, is_ref, idx_val, pos, op_pos, true)?; + .get_indexed_mut(state, lib, obj, is_ref, idx_val, pos, op_pos, true)?; this_ptr.set_value(new_val.unwrap(), rhs.position())?; Ok((Default::default(), true)) } // xxx[rhs] _ => self - .get_indexed_mut(state, obj, is_ref, idx_val, rhs.position(), op_pos, false) + .get_indexed_mut( + state, + lib, + obj, + is_ref, + idx_val, + rhs.position(), + op_pos, + false, + ) .map(|v| (v.clone_into_dynamic(), false)), } } else { @@ -1016,7 +1054,9 @@ impl Engine { .collect(); let args = arg_values.as_mut(); - self.exec_fn_call(state, name, *native, *hash, args, is_ref, def_val, *pos, 0) + self.exec_fn_call( + state, lib, name, *native, *hash, args, is_ref, def_val, *pos, 0, + ) } // xxx.module::fn_name(...) - syntax error Expr::FnCall(_) => unreachable!(), @@ -1026,7 +1066,7 @@ impl Engine { let ((prop, _, _), pos) = x.as_ref(); let index = prop.clone().into(); let mut val = - self.get_indexed_mut(state, obj, is_ref, index, *pos, op_pos, true)?; + self.get_indexed_mut(state, lib, obj, is_ref, index, *pos, op_pos, true)?; val.set_value(new_val.unwrap(), rhs.position())?; Ok((Default::default(), true)) @@ -1037,7 +1077,7 @@ impl Engine { let ((prop, _, _), pos) = x.as_ref(); let index = prop.clone().into(); let val = - self.get_indexed_mut(state, obj, is_ref, index, *pos, op_pos, false)?; + self.get_indexed_mut(state, lib, obj, is_ref, index, *pos, op_pos, false)?; Ok((val.clone_into_dynamic(), false)) } @@ -1045,15 +1085,19 @@ impl Engine { Expr::Property(x) if new_val.is_some() => { let ((_, _, setter), pos) = x.as_ref(); let mut args = [obj, new_val.as_mut().unwrap()]; - self.exec_fn_call(state, setter, true, 0, &mut args, is_ref, None, *pos, 0) - .map(|(v, _)| (v, true)) + self.exec_fn_call( + state, lib, setter, true, 0, &mut args, is_ref, None, *pos, 0, + ) + .map(|(v, _)| (v, true)) } // xxx.id Expr::Property(x) => { let ((_, getter, _), pos) = x.as_ref(); let mut args = [obj]; - self.exec_fn_call(state, getter, true, 0, &mut args, is_ref, None, *pos, 0) - .map(|(v, _)| (v, false)) + self.exec_fn_call( + state, lib, getter, true, 0, &mut args, is_ref, None, *pos, 0, + ) + .map(|(v, _)| (v, false)) } #[cfg(not(feature = "no_object"))] // {xxx:map}.idx_lhs[idx_expr] | {xxx:map}.dot_lhs.rhs @@ -1063,7 +1107,7 @@ impl Engine { let mut val = if let Expr::Property(p) = &x.0 { let ((prop, _, _), _) = p.as_ref(); let index = prop.clone().into(); - self.get_indexed_mut(state, obj, is_ref, index, x.2, op_pos, false)? + self.get_indexed_mut(state, lib, obj, is_ref, index, x.2, op_pos, false)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( @@ -1073,7 +1117,7 @@ impl Engine { }; self.eval_dot_index_chain_helper( - state, &mut val, &x.1, idx_values, is_idx, x.2, level, new_val, + state, lib, &mut val, &x.1, idx_values, is_idx, x.2, level, new_val, ) } // xxx.idx_lhs[idx_expr] | xxx.dot_lhs.rhs @@ -1084,7 +1128,7 @@ impl Engine { let (mut val, updated) = if let Expr::Property(p) = &x.0 { let ((_, getter, _), _) = p.as_ref(); let args = &mut args[..1]; - self.exec_fn_call(state, getter, true, 0, args, is_ref, None, x.2, 0)? + self.exec_fn_call(state, lib, getter, true, 0, args, is_ref, None, x.2, 0)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( @@ -1096,7 +1140,7 @@ impl Engine { let target = &mut val.into(); let (result, may_be_changed) = self.eval_dot_index_chain_helper( - state, target, &x.1, idx_values, is_idx, x.2, level, new_val, + state, lib, target, &x.1, idx_values, is_idx, x.2, level, new_val, )?; // Feed the value back via a setter just in case it has been updated @@ -1105,12 +1149,14 @@ impl Engine { let ((_, _, setter), _) = p.as_ref(); // Re-use args because the first &mut parameter will not be consumed args[1] = val; - self.exec_fn_call(state, setter, true, 0, args, is_ref, None, x.2, 0) - .or_else(|err| match *err { - // If there is no setter, no need to feed it back because the property is read-only - EvalAltResult::ErrorDotExpr(_, _) => Ok(Default::default()), - err => Err(Box::new(err)), - })?; + self.exec_fn_call( + state, lib, setter, true, 0, args, is_ref, None, x.2, 0, + ) + .or_else(|err| match *err { + // If there is no setter, no need to feed it back because the property is read-only + EvalAltResult::ErrorDotExpr(_, _) => Ok(Default::default()), + err => Err(Box::new(err)), + })?; } } @@ -1130,6 +1176,7 @@ impl Engine { &self, scope: &mut Scope, state: &mut State, + lib: &FunctionsLib, dot_lhs: &Expr, dot_rhs: &Expr, is_index: bool, @@ -1139,7 +1186,7 @@ impl Engine { ) -> Result> { let idx_values = &mut StaticVec::new(); - self.eval_indexed_chain(scope, state, dot_rhs, idx_values, 0, level)?; + self.eval_indexed_chain(scope, state, lib, dot_rhs, idx_values, 0, level)?; match dot_lhs { // id.??? or id[???] @@ -1164,7 +1211,7 @@ impl Engine { let this_ptr = &mut target.into(); self.eval_dot_index_chain_helper( - state, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, + state, lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, ) .map(|(v, _)| v) } @@ -1176,10 +1223,10 @@ impl Engine { } // {expr}.??? or {expr}[???] expr => { - let val = self.eval_expr(scope, state, expr, level)?; + let val = self.eval_expr(scope, state, lib, expr, level)?; let this_ptr = &mut val.into(); self.eval_dot_index_chain_helper( - state, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, + state, lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, ) .map(|(v, _)| v) } @@ -1195,6 +1242,7 @@ impl Engine { &self, scope: &mut Scope, state: &mut State, + lib: &FunctionsLib, expr: &Expr, idx_values: &mut StaticVec, size: usize, @@ -1206,7 +1254,7 @@ impl Engine { Expr::FnCall(x) if x.1.is_none() => { let arg_values = x.3.iter() - .map(|arg_expr| self.eval_expr(scope, state, arg_expr, level)) + .map(|arg_expr| self.eval_expr(scope, state, lib, arg_expr, level)) .collect::, _>>()?; idx_values.push(Dynamic::from(arg_values)); @@ -1217,15 +1265,15 @@ impl Engine { // Evaluate in left-to-right order let lhs_val = match x.0 { Expr::Property(_) => Default::default(), // Store a placeholder in case of a property - _ => self.eval_expr(scope, state, &x.0, level)?, + _ => self.eval_expr(scope, state, lib, &x.0, level)?, }; // Push in reverse order - self.eval_indexed_chain(scope, state, &x.1, idx_values, size, level)?; + self.eval_indexed_chain(scope, state, lib, &x.1, idx_values, size, level)?; idx_values.push(lhs_val); } - _ => idx_values.push(self.eval_expr(scope, state, expr, level)?), + _ => idx_values.push(self.eval_expr(scope, state, lib, expr, level)?), } Ok(()) @@ -1235,6 +1283,7 @@ impl Engine { fn get_indexed_mut<'a>( &self, state: &mut State, + lib: &FunctionsLib, val: &'a mut Dynamic, is_ref: bool, mut idx: Dynamic, @@ -1305,9 +1354,10 @@ impl Engine { } _ => { + let fn_name = FUNC_INDEXER; let type_name = self.map_type_name(val.type_name()); let args = &mut [val, &mut idx]; - self.exec_fn_call(state, FUNC_INDEXER, true, 0, args, is_ref, None, op_pos, 0) + self.exec_fn_call(state, lib, fn_name, true, 0, args, is_ref, None, op_pos, 0) .map(|(v, _)| v.into()) .map_err(|_| { Box::new(EvalAltResult::ErrorIndexingType(type_name.into(), op_pos)) @@ -1321,14 +1371,15 @@ impl Engine { &self, scope: &mut Scope, state: &mut State, + lib: &FunctionsLib, lhs: &Expr, rhs: &Expr, level: usize, ) -> Result> { self.inc_operations(state, rhs.position())?; - let lhs_value = self.eval_expr(scope, state, lhs, level)?; - let rhs_value = self.eval_expr(scope, state, rhs, level)?; + let lhs_value = self.eval_expr(scope, state, lib, lhs, level)?; + let rhs_value = self.eval_expr(scope, state, lib, rhs, level)?; match rhs_value { #[cfg(not(feature = "no_index"))] @@ -1347,8 +1398,9 @@ impl Engine { calc_fn_hash(empty(), op, args.len(), args.iter().map(|a| a.type_id())); let hashes = (hash_fn, 0); - let (r, _) = self - .call_fn_raw(None, state, op, hashes, args, false, def_value, pos, level)?; + let (r, _) = self.call_fn_raw( + None, state, lib, op, hashes, args, false, def_value, pos, level, + )?; if r.as_bool().unwrap_or(false) { return Ok(true.into()); } @@ -1378,6 +1430,7 @@ impl Engine { &self, scope: &mut Scope, state: &mut State, + lib: &FunctionsLib, expr: &Expr, level: usize, ) -> Result> { @@ -1399,12 +1452,12 @@ impl Engine { Expr::Property(_) => unreachable!(), // Statement block - Expr::Stmt(stmt) => self.eval_stmt(scope, state, &stmt.0, level), + Expr::Stmt(stmt) => self.eval_stmt(scope, state, lib, &stmt.0, level), // lhs = rhs Expr::Assignment(x) => { let op_pos = x.2; - let rhs_val = self.eval_expr(scope, state, &x.1, level)?; + let rhs_val = self.eval_expr(scope, state, lib, &x.1, level)?; match &x.0 { // name = rhs @@ -1432,7 +1485,7 @@ impl Engine { Expr::Index(x) => { let new_val = Some(rhs_val); self.eval_dot_index_chain( - scope, state, &x.0, &x.1, true, x.2, level, new_val, + scope, state, lib, &x.0, &x.1, true, x.2, level, new_val, ) } // dot_lhs.dot_rhs = rhs @@ -1440,7 +1493,7 @@ impl Engine { Expr::Dot(x) => { let new_val = Some(rhs_val); self.eval_dot_index_chain( - scope, state, &x.0, &x.1, false, op_pos, level, new_val, + scope, state, lib, &x.0, &x.1, false, op_pos, level, new_val, ) } // Error assignment to constant @@ -1460,19 +1513,19 @@ impl Engine { // lhs[idx_expr] #[cfg(not(feature = "no_index"))] Expr::Index(x) => { - self.eval_dot_index_chain(scope, state, &x.0, &x.1, true, x.2, level, None) + self.eval_dot_index_chain(scope, state, lib, &x.0, &x.1, true, x.2, level, None) } // lhs.dot_rhs #[cfg(not(feature = "no_object"))] Expr::Dot(x) => { - self.eval_dot_index_chain(scope, state, &x.0, &x.1, false, x.2, level, None) + self.eval_dot_index_chain(scope, state, lib, &x.0, &x.1, false, x.2, level, None) } #[cfg(not(feature = "no_index"))] Expr::Array(x) => Ok(Dynamic(Union::Array(Box::new( x.0.iter() - .map(|item| self.eval_expr(scope, state, item, level)) + .map(|item| self.eval_expr(scope, state, lib, item, level)) .collect::, _>>()?, )))), @@ -1480,7 +1533,7 @@ impl Engine { Expr::Map(x) => Ok(Dynamic(Union::Map(Box::new( x.0.iter() .map(|((key, _), expr)| { - self.eval_expr(scope, state, expr, level) + self.eval_expr(scope, state, lib, expr, level) .map(|val| (key.clone(), val)) }) .collect::, _>>()?, @@ -1493,7 +1546,7 @@ impl Engine { let mut arg_values = args_expr .iter() - .map(|expr| self.eval_expr(scope, state, expr, level)) + .map(|expr| self.eval_expr(scope, state, lib, expr, level)) .collect::, _>>()?; let mut args: StaticVec<_> = arg_values.iter_mut().collect(); @@ -1501,13 +1554,13 @@ impl Engine { if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::() { let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::())); - if !self.has_override(state, (hash_fn, *hash)) { + if !self.has_override(lib, (hash_fn, *hash)) { // eval - only in function call style let prev_len = scope.len(); let pos = args_expr.get(0).position(); // Evaluate the text string as a script - let result = self.eval_script_expr(scope, state, args.pop(), pos); + let result = self.eval_script_expr(scope, state, lib, args.pop(), pos); if scope.len() != prev_len { // IMPORTANT! If the eval defines new variables in the current scope, @@ -1522,7 +1575,7 @@ impl Engine { // Normal function call - except for eval (handled above) let args = args.as_mut(); self.exec_fn_call( - state, name, *native, *hash, args, false, def_val, *pos, level, + state, lib, name, *native, *hash, args, false, def_val, *pos, level, ) .map(|(v, _)| v) } @@ -1535,7 +1588,7 @@ impl Engine { let mut arg_values = args_expr .iter() - .map(|expr| self.eval_expr(scope, state, expr, level)) + .map(|expr| self.eval_expr(scope, state, lib, expr, level)) .collect::, _>>()?; let mut args: StaticVec<_> = arg_values.iter_mut().collect(); @@ -1579,9 +1632,8 @@ impl Engine { Ok(x) if x.is_script() => { let args = args.as_mut(); let fn_def = x.get_fn_def(); - let (result, state2) = - self.call_script_fn(None, *state, name, fn_def, args, *pos, level)?; - *state = state2; + let result = + self.call_script_fn(None, state, lib, name, fn_def, args, *pos, level)?; Ok(result) } Ok(x) => x.get_native_fn()(args.as_mut()).map_err(|err| err.new_position(*pos)), @@ -1595,19 +1647,19 @@ impl Engine { } } - Expr::In(x) => self.eval_in_expr(scope, state, &x.0, &x.1, level), + Expr::In(x) => self.eval_in_expr(scope, state, lib, &x.0, &x.1, level), Expr::And(x) => { let (lhs, rhs, _) = x.as_ref(); Ok((self - .eval_expr(scope, state, lhs, level)? + .eval_expr(scope, state, lib, lhs, level)? .as_bool() .map_err(|_| { EvalAltResult::ErrorBooleanArgMismatch("AND".into(), lhs.position()) })? && // Short-circuit using && self - .eval_expr(scope, state, rhs, level)? + .eval_expr(scope, state, lib, rhs, level)? .as_bool() .map_err(|_| { EvalAltResult::ErrorBooleanArgMismatch("AND".into(), rhs.position()) @@ -1618,14 +1670,14 @@ impl Engine { Expr::Or(x) => { let (lhs, rhs, _) = x.as_ref(); Ok((self - .eval_expr(scope, state, lhs, level)? + .eval_expr(scope, state, lib, lhs, level)? .as_bool() .map_err(|_| { EvalAltResult::ErrorBooleanArgMismatch("OR".into(), lhs.position()) })? || // Short-circuit using || self - .eval_expr(scope, state, rhs, level)? + .eval_expr(scope, state, lib, rhs, level)? .as_bool() .map_err(|_| { EvalAltResult::ErrorBooleanArgMismatch("OR".into(), rhs.position()) @@ -1646,6 +1698,7 @@ impl Engine { &self, scope: &mut Scope<'s>, state: &mut State, + lib: &FunctionsLib, stmt: &Stmt, level: usize, ) -> Result> { @@ -1657,7 +1710,7 @@ impl Engine { // Expression as statement Stmt::Expr(expr) => { - let result = self.eval_expr(scope, state, expr, level)?; + let result = self.eval_expr(scope, state, lib, expr, level)?; Ok(match expr.as_ref() { // If it is an assignment, erase the result at the root @@ -1672,7 +1725,7 @@ impl Engine { state.scope_level += 1; let result = x.0.iter().try_fold(Default::default(), |_, stmt| { - self.eval_stmt(scope, state, stmt, level) + self.eval_stmt(scope, state, lib, stmt, level) }); scope.rewind(prev_len); @@ -1689,14 +1742,14 @@ impl Engine { Stmt::IfThenElse(x) => { let (expr, if_block, else_block) = x.as_ref(); - self.eval_expr(scope, state, expr, level)? + self.eval_expr(scope, state, lib, expr, level)? .as_bool() .map_err(|_| Box::new(EvalAltResult::ErrorLogicGuard(expr.position()))) .and_then(|guard_val| { if guard_val { - self.eval_stmt(scope, state, if_block, level) + self.eval_stmt(scope, state, lib, if_block, level) } else if let Some(stmt) = else_block { - self.eval_stmt(scope, state, stmt, level) + self.eval_stmt(scope, state, lib, stmt, level) } else { Ok(Default::default()) } @@ -1707,8 +1760,8 @@ impl Engine { Stmt::While(x) => loop { let (expr, body) = x.as_ref(); - match self.eval_expr(scope, state, expr, level)?.as_bool() { - Ok(true) => match self.eval_stmt(scope, state, body, level) { + match self.eval_expr(scope, state, lib, expr, level)?.as_bool() { + Ok(true) => match self.eval_stmt(scope, state, lib, body, level) { Ok(_) => (), Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), @@ -1725,7 +1778,7 @@ impl Engine { // Loop statement Stmt::Loop(body) => loop { - match self.eval_stmt(scope, state, body, level) { + match self.eval_stmt(scope, state, lib, body, level) { Ok(_) => (), Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), @@ -1738,7 +1791,7 @@ impl Engine { // For loop Stmt::For(x) => { let (name, expr, stmt) = x.as_ref(); - let iter_type = self.eval_expr(scope, state, expr, level)?; + let iter_type = self.eval_expr(scope, state, lib, expr, level)?; let tid = iter_type.type_id(); if let Some(func) = self @@ -1756,7 +1809,7 @@ impl Engine { *scope.get_mut(index).0 = loop_var; self.inc_operations(state, stmt.position())?; - match self.eval_stmt(scope, state, stmt, level) { + match self.eval_stmt(scope, state, lib, stmt, level) { Ok(_) => (), Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), @@ -1783,7 +1836,7 @@ impl Engine { // Return value Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Return => { Err(Box::new(EvalAltResult::Return( - self.eval_expr(scope, state, x.1.as_ref().unwrap(), level)?, + self.eval_expr(scope, state, lib, x.1.as_ref().unwrap(), level)?, (x.0).1, ))) } @@ -1795,7 +1848,7 @@ impl Engine { // Throw value Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Exception => { - let val = self.eval_expr(scope, state, x.1.as_ref().unwrap(), level)?; + let val = self.eval_expr(scope, state, lib, x.1.as_ref().unwrap(), level)?; Err(Box::new(EvalAltResult::ErrorRuntime( val.take_string().unwrap_or_else(|_| "".into()), (x.0).1, @@ -1812,7 +1865,7 @@ impl Engine { // Let statement Stmt::Let(x) if x.1.is_some() => { let ((var_name, _), expr) = x.as_ref(); - let val = self.eval_expr(scope, state, expr.as_ref().unwrap(), level)?; + let val = self.eval_expr(scope, state, lib, expr.as_ref().unwrap(), level)?; let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state); scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false); Ok(Default::default()) @@ -1828,7 +1881,7 @@ impl Engine { // Const statement Stmt::Const(x) if x.1.is_constant() => { let ((var_name, _), expr) = x.as_ref(); - let val = self.eval_expr(scope, state, &expr, level)?; + let val = self.eval_expr(scope, state, lib, &expr, level)?; let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state); scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true); Ok(Default::default()) @@ -1852,7 +1905,7 @@ impl Engine { } if let Some(path) = self - .eval_expr(scope, state, &expr, level)? + .eval_expr(scope, state, lib, &expr, level)? .try_cast::() { if let Some(resolver) = &self.module_resolver { @@ -1945,7 +1998,11 @@ fn run_builtin_binary_op( ) -> Result, Box> { use crate::packages::arithmetic::*; - if x.type_id() == TypeId::of::() { + let args_type = x.type_id(); + + assert_eq!(y.type_id(), args_type); + + if args_type == TypeId::of::() { let x = x.downcast_ref::().unwrap().clone(); let y = y.downcast_ref::().unwrap().clone(); @@ -1987,7 +2044,7 @@ fn run_builtin_binary_op( "^" => return Ok(Some((x ^ y).into())), _ => (), } - } else if x.type_id() == TypeId::of::() { + } else if args_type == TypeId::of::() { let x = x.downcast_ref::().unwrap().clone(); let y = y.downcast_ref::().unwrap().clone(); @@ -1998,7 +2055,7 @@ fn run_builtin_binary_op( "!=" => return Ok(Some((x != y).into())), _ => (), } - } else if x.type_id() == TypeId::of::() { + } else if args_type == TypeId::of::() { let x = x.downcast_ref::().unwrap(); let y = y.downcast_ref::().unwrap(); @@ -2011,7 +2068,7 @@ fn run_builtin_binary_op( "<=" => return Ok(Some((x <= y).into())), _ => (), } - } else if x.type_id() == TypeId::of::() { + } else if args_type == TypeId::of::() { let x = x.downcast_ref::().unwrap().clone(); let y = y.downcast_ref::().unwrap().clone(); @@ -2024,7 +2081,7 @@ fn run_builtin_binary_op( "<=" => return Ok(Some((x <= y).into())), _ => (), } - } else if x.type_id() == TypeId::of::<()>() { + } else if args_type == TypeId::of::<()>() { match op { "==" => return Ok(Some(true.into())), "!=" | ">" | ">=" | "<" | "<=" => return Ok(Some(false.into())), @@ -2034,7 +2091,7 @@ fn run_builtin_binary_op( #[cfg(not(feature = "no_float"))] { - if x.type_id() == TypeId::of::() { + if args_type == TypeId::of::() { let x = x.downcast_ref::().unwrap().clone(); let y = y.downcast_ref::().unwrap().clone(); diff --git a/src/fn_register.rs b/src/fn_register.rs index 67763298..1cfe4506 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -138,12 +138,6 @@ pub fn map_dynamic(data: T) -> Result Result> { - Ok(data) -} - /// To Dynamic mapping function. #[inline(always)] pub fn map_result( diff --git a/src/module.rs b/src/module.rs index 6c366a35..f233e35a 100644 --- a/src/module.rs +++ b/src/module.rs @@ -50,7 +50,7 @@ pub struct Module { functions: HashMap, CallableFunction)>, /// Script-defined functions. - fn_lib: FunctionsLib, + lib: FunctionsLib, /// Iterator functions, keyed by the type producing the iterator. type_iterators: HashMap, @@ -67,7 +67,7 @@ impl fmt::Debug for Module { "", self.variables, self.functions.len(), - self.fn_lib.len() + self.lib.len() ) } } @@ -614,7 +614,7 @@ impl Module { }, ); - module.fn_lib = module.fn_lib.merge(ast.fn_lib()); + module.lib = module.lib.merge(ast.lib()); Ok(module) } @@ -663,7 +663,7 @@ impl Module { functions.push((hash_fn_native, func.clone())); } // Index all script-defined functions - for fn_def in module.fn_lib.values() { + for fn_def in module.lib.values() { match fn_def.access { // Private functions are not exported Private => continue, diff --git a/src/optimize.rs b/src/optimize.rs index e2acc6d0..5cb0006d 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -1,10 +1,9 @@ use crate::any::Dynamic; use crate::calc_fn_hash; use crate::engine::{ - Engine, FunctionsLib, State as EngineState, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_PRINT, + Engine, FunctionsLib, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_PRINT, KEYWORD_TYPE_OF, }; -use crate::fn_native::FnCallArgs; use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; @@ -53,23 +52,19 @@ struct State<'a> { /// An `Engine` instance for eager function evaluation. engine: &'a Engine, /// Library of script-defined functions. - fn_lib: &'a [(&'a str, usize)], + lib: &'a FunctionsLib, /// Optimization level. optimization_level: OptimizationLevel, } impl<'a> State<'a> { /// Create a new State. - pub fn new( - engine: &'a Engine, - fn_lib: &'a [(&'a str, usize)], - level: OptimizationLevel, - ) -> Self { + pub fn new(engine: &'a Engine, lib: &'a FunctionsLib, level: OptimizationLevel) -> Self { Self { changed: false, constants: vec![], engine, - fn_lib, + lib, optimization_level: level, } } @@ -128,7 +123,8 @@ fn call_fn_with_constant_arguments( .engine .call_fn_raw( None, - &mut EngineState::new(&Default::default()), + &mut Default::default(), + state.lib, fn_name, (hash_fn, 0), arg_values.iter_mut().collect::>().as_mut(), @@ -557,7 +553,10 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // First search in script-defined functions (can override built-in) // Cater for both normal function call style and method call style (one additional arguments) - if !*native_only && state.fn_lib.iter().find(|(id, len)| *id == name && (*len == args.len() || *len == args.len() + 1)).is_some() { + if !*native_only && state.lib.values().find(|f| + &f.name == name + && (args.len()..=args.len() + 1).contains(&f.params.len()) + ).is_some() { // A script-defined function overrides the built-in function - do not make the call x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); return Expr::FnCall(x); @@ -615,11 +614,11 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { } } -fn optimize<'a>( +fn optimize( statements: Vec, engine: &Engine, scope: &Scope, - fn_lib: &'a [(&'a str, usize)], + lib: &FunctionsLib, level: OptimizationLevel, ) -> Vec { // If optimization level is None then skip optimizing @@ -628,7 +627,7 @@ fn optimize<'a>( } // Set up the state - let mut state = State::new(engine, fn_lib, level); + let mut state = State::new(engine, lib, level); // Add constants from the scope into the state scope @@ -713,41 +712,35 @@ pub fn optimize_into_ast( const level: OptimizationLevel = OptimizationLevel::None; #[cfg(not(feature = "no_function"))] - let fn_lib_values: StaticVec<_> = functions - .iter() - .map(|fn_def| (fn_def.name.as_str(), fn_def.params.len())) - .collect(); - - #[cfg(not(feature = "no_function"))] - let fn_lib = fn_lib_values.as_ref(); - - #[cfg(feature = "no_function")] - const fn_lib: &[(&str, usize)] = &[]; - - #[cfg(not(feature = "no_function"))] - let lib = FunctionsLib::from_iter(functions.iter().cloned().map(|mut fn_def| { + let lib = { if !level.is_none() { - let pos = fn_def.body.position(); + let lib = FunctionsLib::from_iter(functions.iter().cloned()); - // Optimize the function body - let mut body = optimize(vec![fn_def.body], engine, &Scope::new(), fn_lib, level); + FunctionsLib::from_iter(functions.into_iter().map(|mut fn_def| { + let pos = fn_def.body.position(); - // {} -> Noop - fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) { - // { return val; } -> val - Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Return => { - Stmt::Expr(Box::new(x.1.unwrap())) - } - // { return; } -> () - Stmt::ReturnWithVal(x) if x.1.is_none() && (x.0).0 == ReturnType::Return => { - Stmt::Expr(Box::new(Expr::Unit((x.0).1))) - } - // All others - stmt => stmt, - }; + // Optimize the function body + let mut body = optimize(vec![fn_def.body], engine, &Scope::new(), &lib, level); + + // {} -> Noop + fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) { + // { return val; } -> val + Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Return => { + Stmt::Expr(Box::new(x.1.unwrap())) + } + // { return; } -> () + Stmt::ReturnWithVal(x) if x.1.is_none() && (x.0).0 == ReturnType::Return => { + Stmt::Expr(Box::new(Expr::Unit((x.0).1))) + } + // All others + stmt => stmt, + }; + fn_def + })) + } else { + FunctionsLib::from_iter(functions.into_iter()) } - fn_def - })); + }; #[cfg(feature = "no_function")] let lib: FunctionsLib = Default::default(); @@ -756,7 +749,7 @@ pub fn optimize_into_ast( match level { OptimizationLevel::None => statements, OptimizationLevel::Simple | OptimizationLevel::Full => { - optimize(statements, engine, &scope, fn_lib, level) + optimize(statements, engine, &scope, &lib, level) } }, lib, diff --git a/src/parser.rs b/src/parser.rs index a1d86fc0..68ea09f7 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -63,8 +63,8 @@ pub struct AST( impl AST { /// Create a new `AST`. - pub fn new(statements: Vec, fn_lib: FunctionsLib) -> Self { - Self(statements, fn_lib) + pub fn new(statements: Vec, lib: FunctionsLib) -> Self { + Self(statements, lib) } /// Get the statements. @@ -78,7 +78,7 @@ impl AST { } /// Get the script-defined functions. - pub(crate) fn fn_lib(&self) -> &FunctionsLib { + pub(crate) fn lib(&self) -> &FunctionsLib { &self.1 } @@ -2585,10 +2585,10 @@ pub fn parse<'a>( ) -> Result { let (statements, functions) = parse_global_level(input, max_expr_depth)?; - let fn_lib = functions.into_iter().map(|(_, v)| v).collect(); + let lib = functions.into_iter().map(|(_, v)| v).collect(); Ok( // Optimize AST - optimize_into_ast(engine, scope, statements, fn_lib, optimization_level), + optimize_into_ast(engine, scope, statements, lib, optimization_level), ) } From fd6dad02535592d863bf5b6595a5683c6bd923f5 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 24 May 2020 23:42:16 +0800 Subject: [PATCH 23/53] Remove builtin check. --- src/engine.rs | 176 +++++++++++++++++++------------------------------- 1 file changed, 68 insertions(+), 108 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index c97146db..54fd0a75 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -172,7 +172,7 @@ impl> From for Target<'_> { #[derive(Debug, Clone, Default)] pub struct State { /// Normally, access to variables are parsed with a relative offset into the scope to avoid a lookup. - /// In some situation, e.g. after running an `eval` statement, subsequent offsets may become mis-aligned. + /// In some situation, e.g. after running an `eval` statement, subsequent offsets become mis-aligned. /// When that happens, this flag is turned on to force a scope lookup by name. pub always_search: bool, @@ -185,12 +185,6 @@ pub struct State { /// Number of modules loaded. pub modules: u64, - - /// Times built-in function used. - pub use_builtin_counter: usize, - - /// Number of modules loaded. - pub use_builtin_binary_op: HashMap, } impl State { @@ -198,27 +192,6 @@ impl State { pub fn new() -> Self { Default::default() } - /// Cache a built-in binary op function. - pub fn set_builtin_binary_op(&mut self, fn_name: impl Into, typ: TypeId) { - // Only cache when exceeding a certain number of calls - if self.use_builtin_counter < 10 { - self.use_builtin_counter += 1; - } else { - self.use_builtin_binary_op.insert(fn_name.into(), typ); - } - } - /// Is a binary op known to be built-in? - pub fn use_builtin_binary_op(&self, fn_name: impl AsRef, args: &[&mut Dynamic]) -> bool { - if self.use_builtin_counter < 10 { - false - } else if args.len() != 2 { - false - } else if args[0].type_id() != args[1].type_id() { - false - } else { - self.use_builtin_binary_op.get(fn_name.as_ref()) == Some(&args[0].type_id()) - } - } } /// A type that holds a library (`HashMap`) of script-defined functions. @@ -514,8 +487,8 @@ impl Engine { Default::default() } - /// Create a new `Engine` with _no_ built-in functions. - /// Use the `load_package` method to load packages of functions. + /// Create a new `Engine` with minimal built-in functions. + /// Use the `load_package` method to load additional packages of functions. pub fn new_raw() -> Self { Self { packages: Default::default(), @@ -612,7 +585,7 @@ impl Engine { /// /// ## WARNING /// - /// Function call arguments may be _consumed_ when the function requires them to be passed by value. + /// Function call arguments be _consumed_ when the function requires them to be passed by value. /// All function arguments not in the first position are always passed by value and thus consumed. /// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`! pub(crate) fn call_fn_raw( @@ -646,87 +619,72 @@ impl Engine { } } - let builtin = state.use_builtin_binary_op(fn_name, args); + // Search built-in's and external functions + if let Some(func) = self + .global_module + .get_fn(hashes.0) + .or_else(|| self.packages.get_fn(hashes.0)) + { + // Calling pure function in method-call? + let mut this_copy: Option; + let mut this_pointer: Option<&mut Dynamic> = None; - if is_ref || !native_only || !builtin { - // Search built-in's and external functions - if let Some(func) = self - .global_module - .get_fn(hashes.0) - .or_else(|| self.packages.get_fn(hashes.0)) - { - // Calling pure function in method-call? - let mut this_copy: Option; - let mut this_pointer: Option<&mut Dynamic> = None; + if func.is_pure() && is_ref && !args.is_empty() { + // Clone the original value. It'll be consumed because the function + // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) + this_copy = Some(args[0].clone()); - if func.is_pure() && is_ref && args.len() > 0 { - // Clone the original value. It'll be consumed because the function - // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) - this_copy = Some(args[0].clone()); - - // Replace the first reference with a reference to the clone, force-casting the lifetime. - // Keep the original reference. Must remember to restore it before existing this function. - this_pointer = Some(mem::replace( - args.get_mut(0).unwrap(), - unsafe_mut_cast_to_lifetime(this_copy.as_mut().unwrap()), - )); - } - - // Run external function - let result = func.get_native_fn()(args); - - // Restore the original reference - if let Some(this_pointer) = this_pointer { - mem::replace(args.get_mut(0).unwrap(), this_pointer); - } - - let result = result.map_err(|err| err.new_position(pos))?; - - // See if the function match print/debug (which requires special processing) - return Ok(match fn_name { - KEYWORD_PRINT => ( - (self.print)(result.as_str().map_err(|type_name| { - Box::new(EvalAltResult::ErrorMismatchOutputType( - type_name.into(), - pos, - )) - })?) - .into(), - false, - ), - KEYWORD_DEBUG => ( - (self.debug)(result.as_str().map_err(|type_name| { - Box::new(EvalAltResult::ErrorMismatchOutputType( - type_name.into(), - pos, - )) - })?) - .into(), - false, - ), - _ => (result, func.is_method()), - }); + // Replace the first reference with a reference to the clone, force-casting the lifetime. + // Keep the original reference. Must remember to restore it before existing this function. + this_pointer = Some(mem::replace( + args.get_mut(0).unwrap(), + unsafe_mut_cast_to_lifetime(this_copy.as_mut().unwrap()), + )); } + + // Run external function + let result = func.get_native_fn()(args); + + // Restore the original reference + if let Some(this_pointer) = this_pointer { + mem::replace(args.get_mut(0).unwrap(), this_pointer); + } + + let result = result.map_err(|err| err.new_position(pos))?; + + // See if the function match print/debug (which requires special processing) + return Ok(match fn_name { + KEYWORD_PRINT => ( + (self.print)(result.as_str().map_err(|type_name| { + Box::new(EvalAltResult::ErrorMismatchOutputType( + type_name.into(), + pos, + )) + })?) + .into(), + false, + ), + KEYWORD_DEBUG => ( + (self.debug)(result.as_str().map_err(|type_name| { + Box::new(EvalAltResult::ErrorMismatchOutputType( + type_name.into(), + pos, + )) + })?) + .into(), + false, + ), + _ => (result, func.is_method()), + }); } - // If it is a 2-operand operator, see if it is built in. - // Only consider situations where: + // See if it is built in. Only consider situations where: // 1) It is not a method call, - // 2) the call explicitly specifies `native_only` because, remember, an `eval` may create a new function, so you - // can never predict what functions lie in `lib`! - // 3) It is already a built-in run once before, or both parameter types are the same - if !is_ref - && native_only - && (builtin || (args.len() == 2 && args[0].type_id() == args[1].type_id())) - { + // 2) the call explicitly specifies `native_only`, + // 3) there are two parameters. + if !is_ref && native_only && args.len() == 2 { match run_builtin_binary_op(fn_name, args[0], args[1])? { - Some(v) => { - // Mark the function call as built-in - if !builtin { - state.set_builtin_binary_op(fn_name, args[0].type_id()); - } - return Ok((v, false)); - } + Some(v) => return Ok((v, false)), None => (), } } @@ -795,7 +753,7 @@ impl Engine { match scope { // Extern scope passed in which is not empty - Some(scope) if scope.len() > 0 => { + Some(scope) if !scope.is_empty() => { let scope_len = scope.len(); // Put arguments into scope as variables @@ -960,7 +918,7 @@ impl Engine { )?; // If new functions are defined within the eval string, it is an error - if ast.lib().len() > 0 { + if !ast.lib().is_empty() { return Err(Box::new(EvalAltResult::ErrorParsing( ParseErrorType::WrongFnDefinition.into_err(pos), ))); @@ -2000,7 +1958,9 @@ fn run_builtin_binary_op( let args_type = x.type_id(); - assert_eq!(y.type_id(), args_type); + if y.type_id() != args_type { + return Ok(None); + } if args_type == TypeId::of::() { let x = x.downcast_ref::().unwrap().clone(); From bbed4c6ef45a986163c75474a1a91a22a12c5c2c Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 24 May 2020 23:42:40 +0800 Subject: [PATCH 24/53] Docs. --- README.md | 16 +++++++++++++++- RELEASES.md | 5 +++-- src/token.rs | 4 ++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 24027eec..7801ebbd 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ Rhai - Embedded Scripting for Rust Rhai is an embedded scripting language and evaluation engine for Rust that gives a safe and easy way to add scripting to any application. -Rhai's current features set: +Features +-------- * Easy-to-use language similar to JS+Rust with dynamic typing but _no_ garbage collector. * Tight integration with native Rust [functions](#working-with-functions) and [types](#custom-types-and-methods), @@ -38,6 +39,19 @@ Rhai's current features set: **Note:** Currently, the version is 0.14.2, so the language and API's may change before they stabilize. +What Rhai doesn't do +-------------------- + +Rhai's purpose is to provide a dynamic layer over Rust code, in the same spirit of _zero cost abstractions_. +It doesn't attempt to be a new language. For example: + +* No classes. Well, Rust doesn't either. On the other hand... +* No traits... so it is also not Rust. Do your Rusty stuff in Rust. +* No structures - definte your types in Rust instead; Rhai can seamless work with _any Rust type_. +* No first-class functions - Code your functions in Rust instead, and register them with Rhai. +* No closures - do your closure magic in Rust instead; [turn a Rhai scripted function into a Rust closure](#calling-rhai-functions-from-rust). +* It is best to expose an API in Rhai for scripts to call. All your core functionalities should be in Rust. + Installation ------------ diff --git a/RELEASES.md b/RELEASES.md index 42d2f77d..d5251d9c 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -17,7 +17,7 @@ Breaking changes `Result>`. * Default maximum limit on levels of nested function calls is fine-tuned and set to a different value. * Some operator functions are now built in (see _Speed enhancements_ below), so they are available even - when under `Engine::new_raw`. + under `Engine::new_raw`. New features ------------ @@ -31,7 +31,8 @@ Speed enhancements * Common operators (e.g. `+`, `>`, `==`) now call into highly efficient built-in implementations for standard types (i.e. `INT`, `FLOAT`, `bool`, `char`, `()` and some `String`) if not overridden by a registered function. - This yields a 5-10% speed benefit depending on script operator usage. + This yields a 5-10% speed benefit depending on script operator usage. Scripts running tight loops will see + significant speed-up. * Implementations of common operators for standard types are removed from the `ArithmeticPackage` and `LogicPackage` (and therefore the `CorePackage`) because they are now always available, even under `Engine::new_raw`. diff --git a/src/token.rs b/src/token.rs index 97df2d54..dda005a9 100644 --- a/src/token.rs +++ b/src/token.rs @@ -21,8 +21,8 @@ type LERR = LexError; /// A location (line number + character position) in the input script. /// -/// In order to keep footprint small, both line number and character position have 16-bit resolution, -/// meaning they go up to a maximum of 65,535 lines/characters per line. +/// In order to keep footprint small, both line number and character position have 16-bit unsigned resolution, +/// meaning they go up to a maximum of 65,535 lines and characters per line. /// Advancing beyond the maximum line length or maximum number of lines is not an error but has no effect. #[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] pub struct Position { From 99ea2b33c971efe05b2387607d1b0828da616cd3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 25 May 2020 13:44:28 +0800 Subject: [PATCH 25/53] Use immutable strings. --- Cargo.toml | 2 +- README.md | 61 ++++++----- src/any.rs | 56 ++++++++--- src/engine.rs | 31 +++--- src/lib.rs | 2 +- src/optimize.rs | 6 +- src/packages/array_basic.rs | 10 +- src/packages/eval.rs | 6 +- src/packages/logic.rs | 16 ++- src/packages/map_basic.rs | 6 +- src/packages/string_basic.rs | 40 ++++---- src/packages/string_more.rs | 190 ++++++++++++++++++----------------- src/parser.rs | 10 +- tests/get_set.rs | 6 +- 14 files changed, 251 insertions(+), 191 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6239e658..23f4aa90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rhai" -version = "0.14.2" +version = "0.15.0" edition = "2018" authors = ["Jonathan Turner", "Lukáš Hozda", "Stephen Chung"] description = "Embedded scripting for Rust" diff --git a/README.md b/README.md index 7801ebbd..c8f5fb73 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Features to do checked arithmetic operations); for [`no-std`](#optional-features) builds, a number of additional dependencies are pulled in to provide for functionalities that used to be in `std`. -**Note:** Currently, the version is 0.14.2, so the language and API's may change before they stabilize. +**Note:** Currently, the version is 0.15.0, so the language and API's may change before they stabilize. What Rhai doesn't do -------------------- @@ -59,7 +59,7 @@ Install the Rhai crate by adding this line to `dependencies`: ```toml [dependencies] -rhai = "0.14.2" +rhai = "0.15.0" ``` Use the latest released crate version on [`crates.io`](https::/crates.io/crates/rhai/): @@ -395,14 +395,14 @@ are supported. ### Built-in operators -| Operator | Supported for type (see [standard types]) | -| ---------------------------- | -------------------------------------------------------------------- | -| `+`, `-`, `*`, `/`, `%`, `~` | `INT`, `FLOAT` (if not [`no_float`]) | -| `<<`, `>>`, `^` | `INT` | -| `&`, `\|` | `INT`, `bool` | -| `&&`, `\|\|` | `bool` | -| `==`, `!=` | `INT`, `FLOAT` (if not [`no_float`]), `bool`, `char`, `()`, `String` | -| `>`, `>=`, `<`, `<=` | `INT`, `FLOAT` (if not [`no_float`]), `char`, `()`, `String` | +| Operator | Supported for type (see [standard types]) | +| ---------------------------- | ----------------------------------------------------------------------------- | +| `+`, `-`, `*`, `/`, `%`, `~` | `INT`, `FLOAT` (if not [`no_float`]) | +| `<<`, `>>`, `^` | `INT` | +| `&`, `\|` | `INT`, `bool` | +| `&&`, `\|\|` | `bool` | +| `==`, `!=` | `INT`, `FLOAT` (if not [`no_float`]), `bool`, `char`, `()`, `ImmutableString` | +| `>`, `>=`, `<`, `<=` | `INT`, `FLOAT` (if not [`no_float`]), `char`, `()`, `ImmutableString` | ### Packages @@ -494,7 +494,7 @@ The following primitive types are supported natively: | **Floating-point number** (disabled with [`no_float`]) | `f32`, `f64` _(default)_ | `"f32"` or `"f64"` | `"123.4567"` etc. | | **Boolean value** | `bool` | `"bool"` | `"true"` or `"false"` | | **Unicode character** | `char` | `"char"` | `"A"`, `"x"` etc. | -| **Unicode string** | `String` (_not_ `&str`) | `"string"` | `"hello"` etc. | +| **Immutable Unicode string** | `rhai::ImmutableString` (implemented as `Rc` or `Arc`, _not_ `&str`) | `"string"` | `"hello"` etc. | | **Array** (disabled with [`no_index`]) | `rhai::Array` | `"array"` | `"[ ?, ?, ? ]"` | | **Object map** (disabled with [`no_object`]) | `rhai::Map` | `"map"` | `#{ "a": 1, "b": 2 }` | | **Timestamp** (implemented in the [`BasicTimePackage`](#packages)) | `std::time::Instant` | `"timestamp"` | _not supported_ | @@ -514,6 +514,10 @@ This is useful on some 32-bit targets where using 64-bit integers incur a perfor If no floating-point is needed or supported, use the [`no_float`] feature to remove it. +Strings in Rhai are _immutable_, meaning that they can be shared but not modified. In actual, the `ImmutableString` type +is implemented as an `Rc`- or `Arc`-wrapped `String`. Any modification done to a Rhai string will cause the string to be cloned +and the modifications made to the copy. + The `to_string` function converts a standard type into a [string] for display purposes. The `type_of` function detects the actual type of a value. This is useful because all variables are [`Dynamic`] in nature. @@ -638,12 +642,12 @@ Traits A number of traits, under the `rhai::` module namespace, provide additional functionalities. -| Trait | Description | Methods | -| ------------------ | -------------------------------------------------------------------------------------- | --------------------------------------- | -| `RegisterFn` | Trait for registering functions | `register_fn` | -| `RegisterResultFn` | Trait for registering fallible functions returning `Result<`_T_`, Box>` | `register_result_fn` | -| `Func` | Trait for creating anonymous functions from script | `create_from_ast`, `create_from_script` | -| `ModuleResolver` | Trait implemented by module resolution services | `resolve` | +| Trait | Description | Methods | +| ------------------ | ---------------------------------------------------------------------------------------- | --------------------------------------- | +| `RegisterFn` | Trait for registering functions | `register_fn` | +| `RegisterResultFn` | Trait for registering fallible functions returning `Result>` | `register_result_fn` | +| `Func` | Trait for creating anonymous functions from script | `create_from_ast`, `create_from_script` | +| `ModuleResolver` | Trait implemented by module resolution services | `resolve` | Working with functions ---------------------- @@ -967,20 +971,23 @@ Similarly, custom types can expose members by registering a `get` and/or `set` f ```rust #[derive(Clone)] struct TestStruct { - field: i64 + field: String } +// Remember Rhai uses 'ImmutableString' instead of 'String' impl TestStruct { - fn get_field(&mut self) -> i64 { - self.field + fn get_field(&mut self) -> ImmutableString { + // Make an 'ImmutableString' from a 'String' + self.field.into(0) } - fn set_field(&mut self, new_val: i64) { - self.field = new_val; + fn set_field(&mut self, new_val: ImmutableString) { + // Get a 'String' from an 'ImmutableString' + self.field = (*new_val).clone(); } fn new() -> Self { - TestStruct { field: 1 } + TestStruct { field: "hello" } } } @@ -991,7 +998,8 @@ engine.register_type::(); engine.register_get_set("xyz", TestStruct::get_field, TestStruct::set_field); engine.register_fn("new_ts", TestStruct::new); -let result = engine.eval::("let a = new_ts(); a.xyz = 42; a.xyz")?; +// Return result can be 'String' - Rhai will automatically convert it from 'ImmutableString' +let result = engine.eval::(r#"let a = new_ts(); a.xyz = "42"; a.xyz"#)?; println!("Answer: {}", result); // prints 42 ``` @@ -1033,7 +1041,7 @@ println!("Answer: {}", result); // prints 42 Needless to say, `register_type`, `register_type_with_name`, `register_get`, `register_set`, `register_get_set` and `register_indexer` are not available when the [`no_object`] feature is turned on. -`register_indexer` is also not available when the [`no_index`] feature is turned on. +`register_indexer` is also not available when the [`no_index`] feature is turned on. `Scope` - Initializing and maintaining state ------------------------------------------- @@ -1324,6 +1332,9 @@ Unicode characters. Individual characters within a Rhai string can also be replaced just as if the string is an array of Unicode characters. In Rhai, there is also no separate concepts of `String` and `&str` as in Rust. +Rhai strings are _immutable_ and can be shared. +Modifying a Rhai string actually causes it first to be cloned, and then the modification made to the copy. + Strings can be built up from other strings and types via the `+` operator (provided by the [`MoreStringPackage`](#packages) but excluded if using a [raw `Engine`]). This is particularly useful when printing output. diff --git a/src/any.rs b/src/any.rs index c313de6a..768c66bb 100644 --- a/src/any.rs +++ b/src/any.rs @@ -1,6 +1,6 @@ //! Helper module which defines the `Any` trait to to allow dynamic value handling. -use crate::parser::INT; +use crate::parser::{ImmutableString, INT}; use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast}; #[cfg(not(feature = "no_module"))] @@ -151,7 +151,7 @@ pub struct Dynamic(pub(crate) Union); pub enum Union { Unit(()), Bool(bool), - Str(Box), + Str(ImmutableString), Char(char), Int(INT), #[cfg(not(feature = "no_float"))] @@ -178,6 +178,10 @@ impl Dynamic { /// Is the value held by this `Dynamic` a particular type? pub fn is(&self) -> bool { self.type_id() == TypeId::of::() + || match self.0 { + Union::Str(_) => TypeId::of::() == TypeId::of::(), + _ => false, + } } /// Get the TypeId of the value held by this `Dynamic`. @@ -185,7 +189,7 @@ impl Dynamic { match &self.0 { Union::Unit(_) => TypeId::of::<()>(), Union::Bool(_) => TypeId::of::(), - Union::Str(_) => TypeId::of::(), + Union::Str(_) => TypeId::of::(), Union::Char(_) => TypeId::of::(), Union::Int(_) => TypeId::of::(), #[cfg(not(feature = "no_float"))] @@ -342,6 +346,12 @@ impl Dynamic { return Self(result); } else if let Some(result) = dyn_value.downcast_ref::().cloned().map(Union::Char) { return Self(result); + } else if let Some(result) = dyn_value + .downcast_ref::() + .cloned() + .map(Union::Str) + { + return Self(result); } #[cfg(not(feature = "no_float"))] @@ -358,7 +368,7 @@ impl Dynamic { Err(var) => var, }; var = match unsafe_cast_box::<_, String>(var) { - Ok(s) => return Self(Union::Str(s)), + Ok(s) => return Self(Union::Str(s.into())), Err(var) => var, }; #[cfg(not(feature = "no_index"))] @@ -395,14 +405,22 @@ impl Dynamic { /// assert_eq!(x.try_cast::().unwrap(), 42); /// ``` pub fn try_cast(self) -> Option { - if TypeId::of::() == TypeId::of::() { + let type_id = TypeId::of::(); + + if type_id == TypeId::of::() { return unsafe_cast_box::<_, T>(Box::new(self)).ok().map(|v| *v); } match self.0 { Union::Unit(value) => unsafe_try_cast(value), Union::Bool(value) => unsafe_try_cast(value), - Union::Str(value) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v), + Union::Str(value) => { + if type_id == TypeId::of::() { + unsafe_try_cast(value) + } else { + unsafe_try_cast((*value).clone()) + } + } Union::Char(value) => unsafe_try_cast(value), Union::Int(value) => unsafe_try_cast(value), #[cfg(not(feature = "no_float"))] @@ -434,16 +452,21 @@ impl Dynamic { /// assert_eq!(x.cast::(), 42); /// ``` pub fn cast(self) -> T { - //self.try_cast::().unwrap() + let type_id = TypeId::of::(); - if TypeId::of::() == TypeId::of::() { + if type_id == TypeId::of::() { return *unsafe_cast_box::<_, T>(Box::new(self)).unwrap(); } match self.0 { Union::Unit(value) => unsafe_try_cast(value).unwrap(), Union::Bool(value) => unsafe_try_cast(value).unwrap(), - Union::Str(value) => *unsafe_cast_box::<_, T>(value).unwrap(), + Union::Str(value) => if type_id == TypeId::of::() { + unsafe_try_cast(value) + } else { + unsafe_try_cast((*value).clone()) + } + .unwrap(), Union::Char(value) => unsafe_try_cast(value).unwrap(), Union::Int(value) => unsafe_try_cast(value).unwrap(), #[cfg(not(feature = "no_float"))] @@ -469,7 +492,9 @@ impl Dynamic { match &self.0 { Union::Unit(value) => (value as &dyn Any).downcast_ref::(), Union::Bool(value) => (value as &dyn Any).downcast_ref::(), - Union::Str(value) => (value.as_ref() as &dyn Any).downcast_ref::(), + Union::Str(value) => (value as &dyn Any) + .downcast_ref::() + .or_else(|| (value.as_ref() as &dyn Any).downcast_ref::()), Union::Char(value) => (value as &dyn Any).downcast_ref::(), Union::Int(value) => (value as &dyn Any).downcast_ref::(), #[cfg(not(feature = "no_float"))] @@ -495,7 +520,7 @@ impl Dynamic { match &mut self.0 { Union::Unit(value) => (value as &mut dyn Any).downcast_mut::(), Union::Bool(value) => (value as &mut dyn Any).downcast_mut::(), - Union::Str(value) => (value.as_mut() as &mut dyn Any).downcast_mut::(), + Union::Str(value) => (value as &mut dyn Any).downcast_mut::(), Union::Char(value) => (value as &mut dyn Any).downcast_mut::(), Union::Int(value) => (value as &mut dyn Any).downcast_mut::(), #[cfg(not(feature = "no_float"))] @@ -560,7 +585,7 @@ impl Dynamic { /// Returns the name of the actual type if the cast fails. pub fn take_string(self) -> Result { match self.0 { - Union::Str(s) => Ok(*s), + Union::Str(s) => Ok((*s).clone()), _ => Err(self.type_name()), } } @@ -594,7 +619,12 @@ impl From for Dynamic { } impl From for Dynamic { fn from(value: String) -> Self { - Self(Union::Str(Box::new(value))) + Self(Union::Str(value.into())) + } +} +impl From for Dynamic { + fn from(value: ImmutableString) -> Self { + Self(Union::Str(value)) } } #[cfg(not(feature = "no_index"))] diff --git a/src/engine.rs b/src/engine.rs index 54fd0a75..64f7f0d8 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -7,7 +7,7 @@ use crate::fn_native::{FnCallArgs, Shared}; use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; -use crate::parser::{Expr, FnAccess, FnDef, ReturnType, Stmt, AST, INT}; +use crate::parser::{Expr, FnAccess, FnDef, ImmutableString, ReturnType, Stmt, AST, INT}; use crate::r#unsafe::{unsafe_cast_var_name_to_lifetime, unsafe_mut_cast_to_lifetime}; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; @@ -129,7 +129,7 @@ impl Target<'_> { Target::Value(_) => { return Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS(pos))) } - Target::StringChar(Dynamic(Union::Str(s)), index, _) => { + Target::StringChar(Dynamic(Union::Str(ref mut s)), index, _) => { // Replace the character at the specified index position let new_ch = new_val .as_char() @@ -141,8 +141,7 @@ impl Target<'_> { // See if changed - if so, update the String if ch != new_ch { chars[*index] = new_ch; - s.clear(); - chars.iter().for_each(|&ch| s.push(ch)); + *s = chars.iter().collect::().into(); } } _ => unreachable!(), @@ -1277,14 +1276,18 @@ impl Engine { #[cfg(not(feature = "no_object"))] Dynamic(Union::Map(map)) => { // val_map[idx] - let index = idx - .take_string() - .map_err(|_| EvalAltResult::ErrorStringIndexExpr(idx_pos))?; - Ok(if create { + let index = idx + .take_string() + .map_err(|_| EvalAltResult::ErrorStringIndexExpr(idx_pos))?; + map.entry(index).or_insert(Default::default()).into() } else { - map.get_mut(&index) + let index = idx + .downcast_ref::() + .ok_or_else(|| EvalAltResult::ErrorStringIndexExpr(idx_pos))?; + + map.get_mut(index) .map(Target::from) .unwrap_or_else(|| Target::from(())) }) @@ -1864,7 +1867,7 @@ impl Engine { if let Some(path) = self .eval_expr(scope, state, lib, &expr, level)? - .try_cast::() + .try_cast::() { if let Some(resolver) = &self.module_resolver { // Use an empty scope to create a module @@ -1879,7 +1882,7 @@ impl Engine { Ok(Default::default()) } else { Err(Box::new(EvalAltResult::ErrorModuleNotFound( - path, + path.to_string(), expr.position(), ))) } @@ -2015,9 +2018,9 @@ fn run_builtin_binary_op( "!=" => return Ok(Some((x != y).into())), _ => (), } - } else if args_type == TypeId::of::() { - let x = x.downcast_ref::().unwrap(); - let y = y.downcast_ref::().unwrap(); + } else if args_type == TypeId::of::() { + let x = x.downcast_ref::().unwrap(); + let y = y.downcast_ref::().unwrap(); match op { "==" => return Ok(Some((x == y).into())), diff --git a/src/lib.rs b/src/lib.rs index df039436..454e3d13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,7 +93,7 @@ pub use engine::Engine; pub use error::{ParseError, ParseErrorType}; pub use fn_register::{RegisterFn, RegisterResultFn}; pub use module::Module; -pub use parser::{AST, INT}; +pub use parser::{ImmutableString, AST, INT}; pub use result::EvalAltResult; pub use scope::Scope; pub use token::Position; diff --git a/src/optimize.rs b/src/optimize.rs index 5cb0006d..1d182c19 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -438,7 +438,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // All other items can be thrown away. state.set_dirty(); let pos = m.1; - m.0.into_iter().find(|((name, _), _)| name == &s.0) + m.0.into_iter().find(|((name, _), _)| name == s.0.as_ref()) .map(|(_, expr)| expr.set_position(pos)) .unwrap_or_else(|| Expr::Unit(pos)) } @@ -466,7 +466,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // "xxx" in "xxxxx" (Expr::StringConstant(a), Expr::StringConstant(b)) => { state.set_dirty(); - if b.0.contains(&a.0) { Expr::True(a.1) } else { Expr::False(a.1) } + if b.0.contains(a.0.as_ref()) { Expr::True(a.1) } else { Expr::False(a.1) } } // 'x' in "xxxxx" (Expr::CharConstant(a), Expr::StringConstant(b)) => { @@ -476,7 +476,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // "xxx" in #{...} (Expr::StringConstant(a), Expr::Map(b)) => { state.set_dirty(); - if b.0.iter().find(|((name, _), _)| name == &a.0).is_some() { + if b.0.iter().find(|((name, _), _)| name == a.0.as_ref()).is_some() { Expr::True(a.1) } else { Expr::False(a.1) diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index eb56bbf4..1abdd3be 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -4,9 +4,9 @@ use crate::any::{Dynamic, Variant}; use crate::def_package; use crate::engine::Array; use crate::module::FuncReturn; -use crate::parser::INT; +use crate::parser::{ImmutableString, INT}; -use crate::stdlib::{any::TypeId, boxed::Box, string::String}; +use crate::stdlib::{any::TypeId, boxed::Box}; // Register array utility functions fn push(list: &mut Array, item: T) -> FuncReturn<()> { @@ -45,9 +45,9 @@ macro_rules! reg_tri { #[cfg(not(feature = "no_index"))] def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { - reg_op!(lib, "push", push, INT, bool, char, String, Array, ()); - reg_tri!(lib, "pad", pad, INT, bool, char, String, Array, ()); - reg_tri!(lib, "insert", ins, INT, bool, char, String, Array, ()); + reg_op!(lib, "push", push, INT, bool, char, ImmutableString, Array, ()); + reg_tri!(lib, "pad", pad, INT, bool, char, ImmutableString, Array, ()); + reg_tri!(lib, "insert", ins, INT, bool, char, ImmutableString, Array, ()); lib.set_fn_2_mut("append", |x: &mut Array, y: Array| { x.extend(y); diff --git a/src/packages/eval.rs b/src/packages/eval.rs index a6756d96..6f97901d 100644 --- a/src/packages/eval.rs +++ b/src/packages/eval.rs @@ -1,11 +1,11 @@ use crate::def_package; use crate::module::FuncReturn; -use crate::stdlib::string::String; +use crate::parser::ImmutableString; def_package!(crate:EvalPackage:"Disable 'eval'.", lib, { - lib.set_fn_1_mut( + lib.set_fn_1( "eval", - |_: &mut String| -> FuncReturn<()> { + |_: ImmutableString| -> FuncReturn<()> { Err("eval is evil!".into()) }, ); diff --git a/src/packages/logic.rs b/src/packages/logic.rs index 03a12e9d..50d74466 100644 --- a/src/packages/logic.rs +++ b/src/packages/logic.rs @@ -1,8 +1,6 @@ use crate::def_package; use crate::module::FuncReturn; -use crate::parser::INT; - -use crate::stdlib::string::String; +use crate::parser::{ImmutableString, INT}; // Comparison operators pub fn lt(x: T, y: T) -> FuncReturn { @@ -50,12 +48,12 @@ def_package!(crate:LogicPackage:"Logical operators.", lib, { // reg_op!(lib, "!=", ne, INT, char, bool, ()); // Special versions for strings - at least avoid copying the first string - // lib.set_fn_2_mut("<", |x: &mut String, y: String| Ok(*x < y)); - // lib.set_fn_2_mut("<=", |x: &mut String, y: String| Ok(*x <= y)); - // lib.set_fn_2_mut(">", |x: &mut String, y: String| Ok(*x > y)); - // lib.set_fn_2_mut(">=", |x: &mut String, y: String| Ok(*x >= y)); - // lib.set_fn_2_mut("==", |x: &mut String, y: String| Ok(*x == y)); - // lib.set_fn_2_mut("!=", |x: &mut String, y: String| Ok(*x != y)); + // lib.set_fn_2("<", |x: ImmutableString, y: ImmutableString| Ok(*x < y)); + // lib.set_fn_2("<=", |x: ImmutableString, y: ImmutableString| Ok(*x <= y)); + // lib.set_fn_2(">", |x: ImmutableString, y: ImmutableString| Ok(*x > y)); + // lib.set_fn_2(">=", |x: ImmutableString, y: ImmutableString| Ok(*x >= y)); + // lib.set_fn_2("==", |x: ImmutableString, y: ImmutableString| Ok(*x == y)); + // lib.set_fn_2("!=", |x: ImmutableString, y: ImmutableString| Ok(*x != y)); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index c9f4e894..cc2e4c1c 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -4,7 +4,7 @@ use crate::any::Dynamic; use crate::def_package; use crate::engine::Map; use crate::module::FuncReturn; -use crate::parser::INT; +use crate::parser::{ImmutableString, INT}; use crate::stdlib::{ string::{String, ToString}, @@ -22,7 +22,7 @@ fn map_get_values(map: &mut Map) -> FuncReturn> { def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, { lib.set_fn_2_mut( "has", - |map: &mut Map, prop: String| Ok(map.contains_key(&prop)), + |map: &mut Map, prop: ImmutableString| Ok(map.contains_key(prop.as_str())), ); lib.set_fn_1_mut("len", |map: &mut Map| Ok(map.len() as INT)); lib.set_fn_1_mut("clear", |map: &mut Map| { @@ -31,7 +31,7 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, { }); lib.set_fn_2_mut( "remove", - |x: &mut Map, name: String| Ok(x.remove(&name).unwrap_or_else(|| ().into())), + |x: &mut Map, name: ImmutableString| Ok(x.remove(name.as_str()).unwrap_or_else(|| ().into())), ); lib.set_fn_2_mut( "mixin", diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index 3b1689cf..18839aed 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -1,7 +1,7 @@ use crate::def_package; use crate::engine::{FUNC_TO_STRING, KEYWORD_DEBUG, KEYWORD_PRINT}; use crate::module::FuncReturn; -use crate::parser::INT; +use crate::parser::{ImmutableString, INT}; #[cfg(not(feature = "no_index"))] use crate::engine::Array; @@ -16,15 +16,15 @@ use crate::stdlib::{ }; // Register print and debug -fn to_debug(x: &mut T) -> FuncReturn { - Ok(format!("{:?}", x)) +fn to_debug(x: &mut T) -> FuncReturn { + Ok(format!("{:?}", x).into()) } -fn to_string(x: &mut T) -> FuncReturn { - Ok(format!("{}", x)) +fn to_string(x: &mut T) -> FuncReturn { + Ok(format!("{}", x).into()) } #[cfg(not(feature = "no_object"))] -fn format_map(x: &mut Map) -> FuncReturn { - Ok(format!("#{:?}", x)) +fn format_map(x: &mut Map) -> FuncReturn { + Ok(format!("#{:?}", x).into()) } macro_rules! reg_op { @@ -41,10 +41,10 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin lib.set_fn_1(KEYWORD_PRINT, |_: ()| Ok("".to_string())); lib.set_fn_1(FUNC_TO_STRING, |_: ()| Ok("".to_string())); - lib.set_fn_1_mut(KEYWORD_PRINT, |s: &mut String| Ok(s.clone())); - lib.set_fn_1_mut(FUNC_TO_STRING, |s: &mut String| Ok(s.clone())); + lib.set_fn_1(KEYWORD_PRINT, |s: ImmutableString| Ok(s.clone())); + lib.set_fn_1(FUNC_TO_STRING, |s: ImmutableString| Ok(s.clone())); - reg_op!(lib, KEYWORD_DEBUG, to_debug, INT, bool, (), char, String); + reg_op!(lib, KEYWORD_DEBUG, to_debug, INT, bool, (), char, ImmutableString); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] @@ -80,26 +80,32 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin lib.set_fn_2( "+", - |mut s: String, ch: char| { + |s: ImmutableString, ch: char| { + let mut s = (*s).clone(); s.push(ch); Ok(s) }, ); lib.set_fn_2( "+", - |mut s: String, s2: String| { - s.push_str(&s2); + |s:ImmutableString, s2:ImmutableString| { + let mut s = (*s).clone(); + s.push_str(s2.as_str()); Ok(s) }, ); - lib.set_fn_2_mut("append", |s: &mut String, ch: char| { - s.push(ch); + lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| { + let mut copy = (**s).clone(); + copy.push(ch); + *s = copy.into(); Ok(()) }); lib.set_fn_2_mut( "append", - |s: &mut String, s2: String| { - s.push_str(&s2); + |s: &mut ImmutableString, s2: ImmutableString| { + let mut copy = (**s).clone(); + copy.push_str(s2.as_str()); + *s = copy.into(); Ok(()) } ); diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 2c90bea5..0217fe63 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -1,6 +1,6 @@ use crate::def_package; use crate::module::FuncReturn; -use crate::parser::INT; +use crate::parser::{ImmutableString, INT}; use crate::utils::StaticVec; #[cfg(not(feature = "no_index"))] @@ -10,22 +10,21 @@ use crate::stdlib::{ fmt::Display, format, string::{String, ToString}, - vec::Vec, }; -fn prepend(x: T, y: String) -> FuncReturn { - Ok(format!("{}{}", x, y)) +fn prepend(x: T, y: ImmutableString) -> FuncReturn { + Ok(format!("{}{}", x, y).into()) } -fn append(x: String, y: T) -> FuncReturn { - Ok(format!("{}{}", x, y)) +fn append(x: ImmutableString, y: T) -> FuncReturn { + Ok(format!("{}{}", x, y).into()) } -fn sub_string(s: &mut String, start: INT, len: INT) -> FuncReturn { +fn sub_string(s: ImmutableString, start: INT, len: INT) -> FuncReturn { let offset = if s.is_empty() || len <= 0 { - return Ok("".to_string()); + return Ok("".to_string().into()); } else if start < 0 { 0 } else if (start as usize) >= s.chars().count() { - return Ok("".to_string()); + return Ok("".to_string().into()); } else { start as usize }; @@ -38,37 +37,45 @@ fn sub_string(s: &mut String, start: INT, len: INT) -> FuncReturn { len as usize }; - Ok(chars.iter().skip(offset).take(len).cloned().collect()) -} -fn crop_string(s: &mut String, start: INT, len: INT) -> FuncReturn<()> { - let offset = if s.is_empty() || len <= 0 { - s.clear(); - return Ok(()); - } else if start < 0 { - 0 - } else if (start as usize) >= s.chars().count() { - s.clear(); - return Ok(()); - } else { - start as usize - }; - - let chars: StaticVec<_> = s.chars().collect(); - - let len = if offset + (len as usize) > chars.len() { - chars.len() - offset - } else { - len as usize - }; - - s.clear(); - - chars + Ok(chars .iter() .skip(offset) .take(len) - .for_each(|&ch| s.push(ch)); + .cloned() + .collect::() + .into()) +} +fn crop_string(s: &mut ImmutableString, start: INT, len: INT) -> FuncReturn<()> { + let mut copy = (**s).clone(); + let offset = if copy.is_empty() || len <= 0 { + copy.clear(); + *s = copy.into(); + return Ok(()); + } else if start < 0 { + 0 + } else if (start as usize) >= copy.chars().count() { + copy.clear(); + *s = copy.into(); + return Ok(()); + } else { + start as usize + }; + + let chars: StaticVec<_> = copy.chars().collect(); + + let len = if offset + (len as usize) > chars.len() { + chars.len() - offset + } else { + len as usize + }; + + *s = chars + .iter() + .skip(offset) + .take(len) + .collect::() + .into(); Ok(()) } @@ -80,10 +87,10 @@ macro_rules! reg_op { def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, { reg_op!(lib, "+", append, INT, bool, char); - lib.set_fn_2_mut( "+", |x: &mut String, _: ()| Ok(x.clone())); + lib.set_fn_2( "+", |x: ImmutableString, _: ()| Ok(x)); reg_op!(lib, "+", prepend, INT, bool, char); - lib.set_fn_2("+", |_: (), y: String| Ok(y)); + lib.set_fn_2("+", |_: (), y: ImmutableString| Ok(y)); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] @@ -100,22 +107,22 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str #[cfg(not(feature = "no_index"))] { - lib.set_fn_2("+", |x: String, y: Array| Ok(format!("{}{:?}", x, y))); - lib.set_fn_2("+", |x: Array, y: String| Ok(format!("{:?}{}", x, y))); + lib.set_fn_2("+", |x: ImmutableString, y: Array| Ok(format!("{}{:?}", x, y))); + lib.set_fn_2_mut("+", |x: &mut Array, y: ImmutableString| Ok(format!("{:?}{}", x, y))); } - lib.set_fn_1_mut("len", |s: &mut String| Ok(s.chars().count() as INT)); - lib.set_fn_2_mut( + lib.set_fn_1("len", |s: ImmutableString| Ok(s.chars().count() as INT)); + lib.set_fn_2( "contains", - |s: &mut String, ch: char| Ok(s.contains(ch)), + |s: ImmutableString, ch: char| Ok(s.contains(ch)), ); - lib.set_fn_2_mut( + lib.set_fn_2( "contains", - |s: &mut String, find: String| Ok(s.contains(&find)), + |s: ImmutableString, find: ImmutableString| Ok(s.contains(find.as_str())), ); - lib.set_fn_3_mut( + lib.set_fn_3( "index_of", - |s: &mut String, ch: char, start: INT| { + |s: ImmutableString, ch: char, start: INT| { let start = if start < 0 { 0 } else if (start as usize) >= s.chars().count() { @@ -130,17 +137,17 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str .unwrap_or(-1 as INT)) }, ); - lib.set_fn_2_mut( + lib.set_fn_2( "index_of", - |s: &mut String, ch: char| { + |s: ImmutableString, ch: char| { Ok(s.find(ch) .map(|index| s[0..index].chars().count() as INT) .unwrap_or(-1 as INT)) }, ); - lib.set_fn_3_mut( + lib.set_fn_3( "index_of", - |s: &mut String, find: String, start: INT| { + |s: ImmutableString, find: ImmutableString, start: INT| { let start = if start < 0 { 0 } else if (start as usize) >= s.chars().count() { @@ -150,109 +157,108 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str }; Ok(s[start..] - .find(&find) + .find(find.as_str()) .map(|index| s[0..start + index].chars().count() as INT) .unwrap_or(-1 as INT)) }, ); - lib.set_fn_2_mut( + lib.set_fn_2( "index_of", - |s: &mut String, find: String| { - Ok(s.find(&find) + |s: ImmutableString, find: ImmutableString| { + Ok(s.find(find.as_str()) .map(|index| s[0..index].chars().count() as INT) .unwrap_or(-1 as INT)) }, ); - lib.set_fn_1_mut("clear", |s: &mut String| { - s.clear(); + lib.set_fn_1_mut("clear", |s: &mut ImmutableString| { + *s = "".to_string().into(); Ok(()) }); - lib.set_fn_2_mut( "append", |s: &mut String, ch: char| { - s.push(ch); + lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| { + let mut copy = (**s).clone(); + copy.push(ch); + *s = copy.into(); Ok(()) }); lib.set_fn_2_mut( "append", - |s: &mut String, add: String| { - s.push_str(&add); + |s: &mut ImmutableString, add: ImmutableString| { + let mut copy = (**s).clone(); + copy.push_str(add.as_str()); + *s = copy.into(); Ok(()) } ); - lib.set_fn_3_mut( "sub_string", sub_string); - lib.set_fn_2_mut( + lib.set_fn_3("sub_string", sub_string); + lib.set_fn_2( "sub_string", - |s: &mut String, start: INT| sub_string(s, start, s.len() as INT), + |s: ImmutableString, start: INT| { + let len = s.len() as INT; + sub_string(s, start, len) + }, ); - lib.set_fn_3_mut( "crop", crop_string); + lib.set_fn_3_mut("crop", crop_string); lib.set_fn_2_mut( "crop", - |s: &mut String, start: INT| crop_string(s, start, s.len() as INT), + |s: &mut ImmutableString, start: INT| crop_string(s, start, s.len() as INT), ); lib.set_fn_2_mut( "truncate", - |s: &mut String, len: INT| { + |s: &mut ImmutableString, len: INT| { if len >= 0 { - let chars: StaticVec<_> = s.chars().take(len as usize).collect(); - s.clear(); - chars.iter().for_each(|&ch| s.push(ch)); + *s = (**s).clone().chars().take(len as usize).collect::().into(); } else { - s.clear(); + *s = "".to_string().into(); } Ok(()) }, ); lib.set_fn_3_mut( "pad", - |s: &mut String, len: INT, ch: char| { - for _ in 0..s.chars().count() - len as usize { - s.push(ch); + |s: &mut ImmutableString, len: INT, ch: char| { + let mut copy = (**s).clone(); + for _ in 0..copy.chars().count() - len as usize { + copy.push(ch); } + *s = copy.into(); Ok(()) }, ); lib.set_fn_3_mut( "replace", - |s: &mut String, find: String, sub: String| { - let new_str = s.replace(&find, &sub); - s.clear(); - s.push_str(&new_str); + |s: &mut ImmutableString, find: ImmutableString, sub: ImmutableString| { + *s = s.replace(find.as_str(), sub.as_str()).into(); Ok(()) }, ); lib.set_fn_3_mut( "replace", - |s: &mut String, find: String, sub: char| { - let new_str = s.replace(&find, &sub.to_string()); - s.clear(); - s.push_str(&new_str); + |s: &mut ImmutableString, find: ImmutableString, sub: char| { + *s = s.replace(find.as_str(), &sub.to_string()).into(); Ok(()) }, ); lib.set_fn_3_mut( "replace", - |s: &mut String, find: char, sub: String| { - let new_str = s.replace(&find.to_string(), &sub); - s.clear(); - s.push_str(&new_str); + |s: &mut ImmutableString, find: char, sub: ImmutableString| { + *s = s.replace(&find.to_string(), sub.as_str()).into(); Ok(()) }, ); lib.set_fn_3_mut( "replace", - |s: &mut String, find: char, sub: char| { - let new_str = s.replace(&find.to_string(), &sub.to_string()); - s.clear(); - s.push_str(&new_str); + |s: &mut ImmutableString, find: char, sub: char| { + *s = s.replace(&find.to_string(), &sub.to_string()).into(); Ok(()) }, ); lib.set_fn_1_mut( "trim", - |s: &mut String| { + |s: &mut ImmutableString| { let trimmed = s.trim(); if trimmed.len() < s.len() { - *s = trimmed.to_string(); + *s = trimmed.to_string().into(); } Ok(()) }, diff --git a/src/parser.rs b/src/parser.rs index 68ea09f7..4180ffbf 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,6 +4,7 @@ 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::fn_native::Shared; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token, TokenIterator}; @@ -48,6 +49,9 @@ pub type INT = i32; #[cfg(not(feature = "no_float"))] pub type FLOAT = f64; +/// The system immutable string type. +pub type ImmutableString = Shared; + type PERR = ParseErrorType; /// Compiled AST (abstract syntax tree) of a Rhai script. @@ -375,7 +379,7 @@ pub enum Expr { /// Character constant. CharConstant(Box<(char, Position)>), /// String constant. - StringConstant(Box<(String, Position)>), + StringConstant(Box<(ImmutableString, Position)>), /// Variable access - ((variable name, position), optional modules, hash, optional index) Variable( Box<( @@ -1208,7 +1212,7 @@ fn parse_primary<'a>( #[cfg(not(feature = "no_float"))] Token::FloatConstant(x) => Expr::FloatConstant(Box::new((x, pos))), Token::CharConstant(c) => Expr::CharConstant(Box::new((c, pos))), - Token::StringConst(s) => Expr::StringConstant(Box::new((s, pos))), + Token::StringConst(s) => Expr::StringConstant(Box::new((s.into(), pos))), Token::Identifier(s) => { let index = state.find(&s); Expr::Variable(Box::new(((s, pos), None, 0, index))) @@ -2603,7 +2607,7 @@ pub fn map_dynamic_to_expr(value: Dynamic, pos: Position) -> Option { Union::Unit(_) => Some(Expr::Unit(pos)), Union::Int(value) => Some(Expr::IntegerConstant(Box::new((value, pos)))), Union::Char(value) => Some(Expr::CharConstant(Box::new((value, pos)))), - Union::Str(value) => Some(Expr::StringConstant(Box::new(((*value).clone(), pos)))), + Union::Str(value) => Some(Expr::StringConstant(Box::new((value.clone(), pos)))), Union::Bool(true) => Some(Expr::True(pos)), Union::Bool(false) => Some(Expr::False(pos)), #[cfg(not(feature = "no_index"))] diff --git a/tests/get_set.rs b/tests/get_set.rs index 94c3064a..2df59671 100644 --- a/tests/get_set.rs +++ b/tests/get_set.rs @@ -1,6 +1,6 @@ #![cfg(not(feature = "no_object"))] -use rhai::{Engine, EvalAltResult, RegisterFn, INT}; +use rhai::{Engine, EvalAltResult, ImmutableString, RegisterFn, INT}; #[test] fn test_get_set() -> Result<(), Box> { @@ -43,7 +43,9 @@ fn test_get_set() -> Result<(), Box> { engine.register_fn("new_ts", TestStruct::new); #[cfg(not(feature = "no_index"))] - engine.register_indexer(|value: &mut TestStruct, index: String| value.array[index.len()]); + engine.register_indexer(|value: &mut TestStruct, index: ImmutableString| { + value.array[index.len()] + }); assert_eq!(engine.eval::("let a = new_ts(); a.x = 500; a.x")?, 500); assert_eq!(engine.eval::("let a = new_ts(); a.x.add(); a.x")?, 42); From fca140ef5560a29bfa97357eac26f11444ad1583 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 25 May 2020 17:01:39 +0800 Subject: [PATCH 26/53] Refine immutable strings. --- benches/eval_expression.rs | 51 ++++++++++++++++++++++++++++++++++++ src/any.rs | 5 +++- src/engine.rs | 5 ++-- src/fn_native.rs | 22 ++++++++++++++++ src/packages/string_basic.rs | 28 +++++++++++++------- src/packages/string_more.rs | 47 ++++++++++++++------------------- 6 files changed, 119 insertions(+), 39 deletions(-) diff --git a/benches/eval_expression.rs b/benches/eval_expression.rs index 8f46fb18..affb25bb 100644 --- a/benches/eval_expression.rs +++ b/benches/eval_expression.rs @@ -105,3 +105,54 @@ fn bench_eval_call(bench: &mut Bencher) { bench.iter(|| engine.eval::(script).unwrap()); } + +#[bench] +fn bench_eval_loop_number(bench: &mut Bencher) { + let script = r#" + let s = 0; + for x in range(0, 10000) { + s += 1; + } + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_loop_strings_build(bench: &mut Bencher) { + let script = r#" + let s = 0; + for x in range(0, 10000) { + s += "x"; + } + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_loop_strings_no_build(bench: &mut Bencher) { + let script = r#" + let s = "hello"; + for x in range(0, 10000) { + s += ""; + } + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} diff --git a/src/any.rs b/src/any.rs index 768c66bb..5a72e957 100644 --- a/src/any.rs +++ b/src/any.rs @@ -1,5 +1,6 @@ //! Helper module which defines the `Any` trait to to allow dynamic value handling. +use crate::fn_native::shared_unwrap; use crate::parser::{ImmutableString, INT}; use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast}; @@ -20,7 +21,9 @@ use crate::stdlib::{ boxed::Box, collections::HashMap, fmt, + rc::Rc, string::String, + sync::Arc, vec::Vec, }; @@ -585,7 +588,7 @@ impl Dynamic { /// Returns the name of the actual type if the cast fails. pub fn take_string(self) -> Result { match self.0 { - Union::Str(s) => Ok((*s).clone()), + Union::Str(s) => Ok(shared_unwrap(s).unwrap_or_else(|s| (*s).clone())), _ => Err(self.type_name()), } } diff --git a/src/engine.rs b/src/engine.rs index 64f7f0d8..9e134769 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1512,8 +1512,9 @@ impl Engine { let mut args: StaticVec<_> = arg_values.iter_mut().collect(); - if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::() { - let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::())); + if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::() { + let hash_fn = + calc_fn_hash(empty(), name, 1, once(TypeId::of::())); if !self.has_override(lib, (hash_fn, *hash)) { // eval - only in function call style diff --git a/src/fn_native.rs b/src/fn_native.rs index 1eaab03a..1975a3c5 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -19,6 +19,28 @@ pub type Shared = Rc; #[cfg(feature = "sync")] pub type Shared = Arc; +pub fn shared_make_mut(value: &mut Shared) -> &mut T { + #[cfg(not(feature = "sync"))] + { + Rc::make_mut(value) + } + #[cfg(feature = "sync")] + { + Arc::make_mut(value) + } +} + +pub fn shared_unwrap(value: Shared) -> Result> { + #[cfg(not(feature = "sync"))] + { + Rc::try_unwrap(value) + } + #[cfg(feature = "sync")] + { + Arc::try_unwrap(value) + } +} + pub type FnCallArgs<'a> = [&'a mut Dynamic]; #[cfg(not(feature = "sync"))] diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index 18839aed..f7ae8930 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -1,5 +1,6 @@ use crate::def_package; use crate::engine::{FUNC_TO_STRING, KEYWORD_DEBUG, KEYWORD_PRINT}; +use crate::fn_native::shared_make_mut; use crate::module::FuncReturn; use crate::parser::{ImmutableString, INT}; @@ -41,8 +42,8 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin lib.set_fn_1(KEYWORD_PRINT, |_: ()| Ok("".to_string())); lib.set_fn_1(FUNC_TO_STRING, |_: ()| Ok("".to_string())); - lib.set_fn_1(KEYWORD_PRINT, |s: ImmutableString| Ok(s.clone())); - lib.set_fn_1(FUNC_TO_STRING, |s: ImmutableString| Ok(s.clone())); + lib.set_fn_1(KEYWORD_PRINT, |s: ImmutableString| Ok(s)); + lib.set_fn_1(FUNC_TO_STRING, |s: ImmutableString| Ok(s)); reg_op!(lib, KEYWORD_DEBUG, to_debug, INT, bool, (), char, ImmutableString); @@ -81,6 +82,10 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin lib.set_fn_2( "+", |s: ImmutableString, ch: char| { + if s.is_empty() { + return Ok(ch.to_string().into()); + } + let mut s = (*s).clone(); s.push(ch); Ok(s) @@ -89,23 +94,28 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin lib.set_fn_2( "+", |s:ImmutableString, s2:ImmutableString| { + if s.is_empty() { + return Ok(s2); + } else if s2.is_empty() { + return Ok(s); + } + let mut s = (*s).clone(); s.push_str(s2.as_str()); - Ok(s) + Ok(s.into()) }, ); lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| { - let mut copy = (**s).clone(); - copy.push(ch); - *s = copy.into(); + shared_make_mut(s).push(ch); Ok(()) }); lib.set_fn_2_mut( "append", |s: &mut ImmutableString, s2: ImmutableString| { - let mut copy = (**s).clone(); - copy.push_str(s2.as_str()); - *s = copy.into(); + if !s2.is_empty() { + shared_make_mut(s).push_str(s2.as_str()); + } + Ok(()) } ); diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 0217fe63..666f9061 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -1,4 +1,5 @@ use crate::def_package; +use crate::fn_native::shared_make_mut; use crate::module::FuncReturn; use crate::parser::{ImmutableString, INT}; use crate::utils::StaticVec; @@ -46,23 +47,19 @@ fn sub_string(s: ImmutableString, start: INT, len: INT) -> FuncReturn FuncReturn<()> { - let mut copy = (**s).clone(); - - let offset = if copy.is_empty() || len <= 0 { - copy.clear(); - *s = copy.into(); + let offset = if s.is_empty() || len <= 0 { + shared_make_mut(s).clear(); return Ok(()); } else if start < 0 { 0 - } else if (start as usize) >= copy.chars().count() { - copy.clear(); - *s = copy.into(); + } else if (start as usize) >= s.chars().count() { + shared_make_mut(s).clear(); return Ok(()); } else { start as usize }; - let chars: StaticVec<_> = copy.chars().collect(); + let chars: StaticVec<_> = s.chars().collect(); let len = if offset + (len as usize) > chars.len() { chars.len() - offset @@ -70,12 +67,10 @@ fn crop_string(s: &mut ImmutableString, start: INT, len: INT) -> FuncReturn<()> len as usize }; - *s = chars - .iter() - .skip(offset) - .take(len) - .collect::() - .into(); + let copy = shared_make_mut(s); + copy.clear(); + copy.extend(chars.iter().skip(offset).take(len)); + Ok(()) } @@ -171,21 +166,17 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str }, ); lib.set_fn_1_mut("clear", |s: &mut ImmutableString| { - *s = "".to_string().into(); + shared_make_mut(s).clear(); Ok(()) }); lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| { - let mut copy = (**s).clone(); - copy.push(ch); - *s = copy.into(); + shared_make_mut(s).push(ch); Ok(()) }); lib.set_fn_2_mut( "append", |s: &mut ImmutableString, add: ImmutableString| { - let mut copy = (**s).clone(); - copy.push_str(add.as_str()); - *s = copy.into(); + shared_make_mut(s).push_str(add.as_str()); Ok(()) } ); @@ -205,10 +196,13 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str lib.set_fn_2_mut( "truncate", |s: &mut ImmutableString, len: INT| { - if len >= 0 { - *s = (**s).clone().chars().take(len as usize).collect::().into(); + if len > 0 { + let chars: StaticVec<_> = s.chars().collect(); + let copy = shared_make_mut(s); + copy.clear(); + copy.extend(chars.into_iter().take(len as usize)); } else { - *s = "".to_string().into(); + shared_make_mut(s).clear(); } Ok(()) }, @@ -216,11 +210,10 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str lib.set_fn_3_mut( "pad", |s: &mut ImmutableString, len: INT, ch: char| { - let mut copy = (**s).clone(); + let copy = shared_make_mut(s); for _ in 0..copy.chars().count() - len as usize { copy.push(ch); } - *s = copy.into(); Ok(()) }, ); From 95e67c48bd0735ed7aeb050da0adc5bca4d5724a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 25 May 2020 20:14:31 +0800 Subject: [PATCH 27/53] Optimize op-assignment statement. --- README.md | 72 +++++------ benches/iterations.rs | 2 +- scripts/loop.rhai | 2 +- scripts/speed_test.rhai | 2 +- scripts/while.rhai | 2 +- src/engine.rs | 207 ++++++++++++++++++++++------- src/optimize.rs | 18 +-- src/packages/array_basic.rs | 4 + src/packages/map_basic.rs | 9 ++ src/packages/string_basic.rs | 14 ++ src/parser.rs | 243 +++++++++++++++-------------------- 11 files changed, 341 insertions(+), 234 deletions(-) diff --git a/README.md b/README.md index c8f5fb73..a2720f62 100644 --- a/README.md +++ b/README.md @@ -1388,19 +1388,19 @@ record == "Bob X. Davis: age 42 ❤\n"; The following standard methods (defined in the [`MoreStringPackage`](#packages) but excluded if using a [raw `Engine`]) operate on strings: -| Function | Parameter(s) | Description | -| ------------ | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | -| `len` | _none_ | returns the number of characters (not number of bytes) in the string | -| `pad` | character to pad, target length | pads the string with an character to at least a specified length | -| `append` | character/string to append | Adds a character or a string to the end of another string | -| `clear` | _none_ | empties the string | -| `truncate` | target length | cuts off the string at exactly a specified number of characters | -| `contains` | character/sub-string to search for | checks if a certain character or sub-string occurs in the string | -| `index_of` | character/sub-string to search for, start index _(optional)_ | returns the index that a certain character or sub-string occurs in the string, or -1 if not found | -| `sub_string` | start index, length _(optional)_ | extracts a sub-string (to the end of the string if length is not specified) | -| `crop` | start index, length _(optional)_ | retains only a portion of the string (to the end of the string if length is not specified) | -| `replace` | target character/sub-string, replacement character/string | replaces a sub-string with another | -| `trim` | _none_ | trims the string of whitespace at the beginning and end | +| Function | Parameter(s) | Description | +| ----------------------- | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| `len` | _none_ | returns the number of characters (not number of bytes) in the string | +| `pad` | character to pad, target length | pads the string with an character to at least a specified length | +| `+=` operator, `append` | character/string to append | Adds a character or a string to the end of another string | +| `clear` | _none_ | empties the string | +| `truncate` | target length | cuts off the string at exactly a specified number of characters | +| `contains` | character/sub-string to search for | checks if a certain character or sub-string occurs in the string | +| `index_of` | character/sub-string to search for, start index _(optional)_ | returns the index that a certain character or sub-string occurs in the string, or -1 if not found | +| `sub_string` | start index, length _(optional)_ | extracts a sub-string (to the end of the string if length is not specified) | +| `crop` | start index, length _(optional)_ | retains only a portion of the string (to the end of the string if length is not specified) | +| `replace` | target character/sub-string, replacement character/string | replaces a sub-string with another | +| `trim` | _none_ | trims the string of whitespace at the beginning and end | ### Examples @@ -1463,19 +1463,19 @@ Arrays are disabled via the [`no_index`] feature. The following methods (defined in the [`BasicArrayPackage`](#packages) but excluded if using a [raw `Engine`]) operate on arrays: -| Function | Parameter(s) | Description | -| ------------ | --------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | -| `push` | element to insert | inserts an element at the end | -| `append` | array to append | concatenates the second array to the end of the first | -| `+` operator | first array, second array | concatenates the first array with the second | -| `insert` | element to insert, position
(beginning if <= 0, end if >= length) | insert an element at a certain index | -| `pop` | _none_ | removes the last element and returns it ([`()`] if empty) | -| `shift` | _none_ | removes the first element and returns it ([`()`] if empty) | -| `remove` | index | removes an element at a particular index and returns it, or returns [`()`] if the index is not valid | -| `len` | _none_ | returns the number of elements | -| `pad` | element to pad, target length | pads the array with an element to at least a specified length | -| `clear` | _none_ | empties the array | -| `truncate` | target length | cuts off the array at exactly a specified length (discarding all subsequent elements) | +| Function | Parameter(s) | Description | +| ----------------------- | --------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | +| `push` | element to insert | inserts an element at the end | +| `+=` operator, `append` | array to append | concatenates the second array to the end of the first | +| `+` operator | first array, second array | concatenates the first array with the second | +| `insert` | element to insert, position
(beginning if <= 0, end if >= length) | insert an element at a certain index | +| `pop` | _none_ | removes the last element and returns it ([`()`] if empty) | +| `shift` | _none_ | removes the first element and returns it ([`()`] if empty) | +| `remove` | index | removes an element at a particular index and returns it, or returns [`()`] if the index is not valid | +| `len` | _none_ | returns the number of elements | +| `pad` | element to pad, target length | pads the array with an element to at least a specified length | +| `clear` | _none_ | empties the array | +| `truncate` | target length | cuts off the array at exactly a specified length (discarding all subsequent elements) | ### Examples @@ -1585,16 +1585,16 @@ Object maps are disabled via the [`no_object`] feature. The following methods (defined in the [`BasicMapPackage`](#packages) but excluded if using a [raw `Engine`]) operate on object maps: -| Function | Parameter(s) | Description | -| ------------ | ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | -| `has` | property name | does the object map contain a property of a particular name? | -| `len` | _none_ | returns the number of properties | -| `clear` | _none_ | empties the object map | -| `remove` | property name | removes a certain property and returns it ([`()`] if the property does not exist) | -| `mixin` | second object map | mixes in all the properties of the second object map to the first (values of properties with the same names replace the existing values) | -| `+` operator | first object map, second object map | merges the first object map with the second | -| `keys` | _none_ | returns an [array] of all the property names (in random order), not available under [`no_index`] | -| `values` | _none_ | returns an [array] of all the property values (in random order), not available under [`no_index`] | +| Function | Parameter(s) | Description | +| ---------------------- | ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | +| `has` | property name | does the object map contain a property of a particular name? | +| `len` | _none_ | returns the number of properties | +| `clear` | _none_ | empties the object map | +| `remove` | property name | removes a certain property and returns it ([`()`] if the property does not exist) | +| `+=` operator, `mixin` | second object map | mixes in all the properties of the second object map to the first (values of properties with the same names replace the existing values) | +| `+` operator | first object map, second object map | merges the first object map with the second | +| `keys` | _none_ | returns an [array] of all the property names (in random order), not available under [`no_index`] | +| `values` | _none_ | returns an [array] of all the property values (in random order), not available under [`no_index`] | ### Examples diff --git a/benches/iterations.rs b/benches/iterations.rs index 23eb7621..b70e9432 100644 --- a/benches/iterations.rs +++ b/benches/iterations.rs @@ -12,7 +12,7 @@ fn bench_iterations_1000(bench: &mut Bencher) { let x = 1_000; while x > 0 { - x = x - 1; + x -= 1; } "#; diff --git a/scripts/loop.rhai b/scripts/loop.rhai index d173b86f..1b514baf 100644 --- a/scripts/loop.rhai +++ b/scripts/loop.rhai @@ -6,7 +6,7 @@ let x = 10; loop { print(x); - x = x - 1; + x -= 1; if x <= 0 { break; } } diff --git a/scripts/speed_test.rhai b/scripts/speed_test.rhai index 5709d6ac..e07bd7ec 100644 --- a/scripts/speed_test.rhai +++ b/scripts/speed_test.rhai @@ -7,7 +7,7 @@ let x = 1_000_000; print("Ready... Go!"); while x > 0 { - x = x - 1; + x -= 1; } print("Finished. Run time = " + now.elapsed() + " seconds."); diff --git a/scripts/while.rhai b/scripts/while.rhai index 3f58f6b6..e17493b2 100644 --- a/scripts/while.rhai +++ b/scripts/while.rhai @@ -4,5 +4,5 @@ let x = 10; while x > 0 { print(x); - x = x - 1; + x -= 1; } diff --git a/src/engine.rs b/src/engine.rs index 9e134769..40930d6f 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1354,10 +1354,11 @@ impl Engine { let def_value = Some(&def_value); let pos = rhs.position(); - // Qualifiers (none) + function name + number of arguments + argument `TypeId`'s. - let hash_fn = - calc_fn_hash(empty(), op, args.len(), args.iter().map(|a| a.type_id())); - let hashes = (hash_fn, 0); + let hashes = ( + // Qualifiers (none) + function name + number of arguments + argument `TypeId`'s. + calc_fn_hash(empty(), op, args.len(), args.iter().map(|a| a.type_id())), + 0, + ); let (r, _) = self.call_fn_raw( None, state, lib, op, hashes, args, false, def_value, pos, level, @@ -1417,57 +1418,87 @@ impl Engine { // lhs = rhs Expr::Assignment(x) => { - let op_pos = x.2; - let rhs_val = self.eval_expr(scope, state, lib, &x.1, level)?; + let lhs_expr = &x.0; + let op = x.1.as_ref(); + let op_pos = x.3; + let mut rhs_val = self.eval_expr(scope, state, lib, &x.2, level)?; - match &x.0 { - // name = rhs - Expr::Variable(x) => { - let ((name, pos), modules, hash_var, index) = x.as_ref(); - let index = if state.always_search { None } else { *index }; - let mod_and_hash = modules.as_ref().map(|m| (m.as_ref(), *hash_var)); - let (lhs_ptr, typ) = search_scope(scope, name, mod_and_hash, index, *pos)?; - self.inc_operations(state, *pos)?; + // name op= rhs + if let Expr::Variable(x) = &x.0 { + let ((name, pos), modules, hash_var, index) = x.as_ref(); + let index = if state.always_search { None } else { *index }; + let mod_and_hash = modules.as_ref().map(|m| (m.as_ref(), *hash_var)); + let (lhs_ptr, typ) = search_scope(scope, name, mod_and_hash, index, *pos)?; + self.inc_operations(state, *pos)?; - match typ { - ScopeEntryType::Constant => Err(Box::new( - EvalAltResult::ErrorAssignmentToConstant(name.clone(), *pos), - )), - ScopeEntryType::Normal => { - *lhs_ptr = rhs_val; - Ok(Default::default()) + match typ { + ScopeEntryType::Constant => Err(Box::new( + EvalAltResult::ErrorAssignmentToConstant(name.clone(), *pos), + )), + ScopeEntryType::Normal if !op.is_empty() => { + // Complex op-assignment + if run_builtin_op_assignment(op, lhs_ptr, &rhs_val)?.is_none() { + // Not built in, do function call + let mut lhs_val = lhs_ptr.clone(); + let args = &mut [&mut lhs_val, &mut rhs_val]; + let hash = calc_fn_hash(empty(), op, 2, empty()); + *lhs_ptr = self + .exec_fn_call( + state, lib, op, true, hash, args, false, None, op_pos, + level, + ) + .map(|(v, _)| v)?; } - // End variable cannot be a module - ScopeEntryType::Module => unreachable!(), + Ok(Default::default()) } + ScopeEntryType::Normal => { + *lhs_ptr = rhs_val; + Ok(Default::default()) + } + // End variable cannot be a module + ScopeEntryType::Module => unreachable!(), } - // idx_lhs[idx_expr] = rhs - #[cfg(not(feature = "no_index"))] - Expr::Index(x) => { - let new_val = Some(rhs_val); - self.eval_dot_index_chain( + } else { + let new_val = Some(if op.is_empty() { + rhs_val + } else { + // Complex op-assignment - always do function call + let args = &mut [ + &mut self.eval_expr(scope, state, lib, lhs_expr, level)?, + &mut rhs_val, + ]; + let hash = calc_fn_hash(empty(), op, 2, empty()); + self.exec_fn_call( + state, lib, op, true, hash, args, false, None, op_pos, level, + ) + .map(|(v, _)| v)? + }); + + match &x.0 { + // name op= rhs + Expr::Variable(_) => unreachable!(), + // idx_lhs[idx_expr] op= rhs + #[cfg(not(feature = "no_index"))] + Expr::Index(x) => self.eval_dot_index_chain( scope, state, lib, &x.0, &x.1, true, x.2, level, new_val, - ) - } - // dot_lhs.dot_rhs = rhs - #[cfg(not(feature = "no_object"))] - Expr::Dot(x) => { - let new_val = Some(rhs_val); - self.eval_dot_index_chain( + ), + // dot_lhs.dot_rhs op= rhs + #[cfg(not(feature = "no_object"))] + Expr::Dot(x) => self.eval_dot_index_chain( scope, state, lib, &x.0, &x.1, false, op_pos, level, new_val, - ) - } - // Error assignment to constant - expr if expr.is_constant() => { - Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( - expr.get_constant_str(), + ), + // Error assignment to constant + expr if expr.is_constant() => { + Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( + expr.get_constant_str(), + expr.position(), + ))) + } + // Syntax error + expr => Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS( expr.position(), - ))) + ))), } - // Syntax error - expr => Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS( - expr.position(), - ))), } } @@ -1675,7 +1706,7 @@ impl Engine { let result = self.eval_expr(scope, state, lib, expr, level)?; Ok(match expr.as_ref() { - // If it is an assignment, erase the result at the root + // If it is a simple assignment, erase the result at the root Expr::Assignment(_) => Default::default(), _ => result, }) @@ -2079,3 +2110,85 @@ fn run_builtin_binary_op( Ok(None) } + +/// Build in common operator assignment implementations to avoid the cost of calling a registered function. +fn run_builtin_op_assignment( + op: &str, + x: &mut Dynamic, + y: &Dynamic, +) -> Result, Box> { + use crate::packages::arithmetic::*; + + let args_type = x.type_id(); + + if y.type_id() != args_type { + return Ok(None); + } + + if args_type == TypeId::of::() { + let x = x.downcast_mut::().unwrap(); + let y = y.downcast_ref::().unwrap().clone(); + + #[cfg(not(feature = "unchecked"))] + match op { + "+" => return Ok(Some(*x = add(*x, y)?)), + "-" => return Ok(Some(*x = sub(*x, y)?)), + "*" => return Ok(Some(*x = mul(*x, y)?)), + "/" => return Ok(Some(*x = div(*x, y)?)), + "%" => return Ok(Some(*x = modulo(*x, y)?)), + "~" => return Ok(Some(*x = pow_i_i(*x, y)?)), + ">>" => return Ok(Some(*x = shr(*x, y)?)), + "<<" => return Ok(Some(*x = shl(*x, y)?)), + _ => (), + } + + #[cfg(feature = "unchecked")] + match op { + "+" => return Ok(Some(*x += y)), + "-" => return Ok(Some(*x -= y)), + "*" => return Ok(Some(*x *= y)), + "/" => return Ok(Some(*x /= y)), + "%" => return Ok(Some(*x %= y)), + "~" => return Ok(Some(*x = pow_i_i_u(x, y))), + ">>" => return Ok(Some(*x = shr_u(x, y))), + "<<" => return Ok(Some(*x = shl_u(x, y))), + _ => (), + } + + match op { + "&" => return Ok(Some(*x &= y)), + "|" => return Ok(Some(*x |= y)), + "^" => return Ok(Some(*x ^= y)), + _ => (), + } + } else if args_type == TypeId::of::() { + let x = x.downcast_mut::().unwrap(); + let y = y.downcast_ref::().unwrap().clone(); + + match op { + "&" => return Ok(Some(*x = *x && y)), + "|" => return Ok(Some(*x = *x || y)), + _ => (), + } + } + + #[cfg(not(feature = "no_float"))] + { + if args_type == TypeId::of::() { + let x = x.downcast_mut::().unwrap(); + let y = y.downcast_ref::().unwrap().clone(); + + match op { + "+" => return Ok(Some(*x += y)), + "-" => return Ok(Some(*x -= y)), + "*" => return Ok(Some(*x *= y)), + "/" => return Ok(Some(*x /= y)), + "%" => return Ok(Some(*x %= y)), + "~" => return Ok(Some(*x = pow_f_f(*x, y)?)), + _ => (), + } + } + } + + Ok(None) +} diff --git a/src/optimize.rs b/src/optimize.rs index 1d182c19..dedd1135 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -380,26 +380,26 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { stmt => Expr::Stmt(Box::new((stmt, x.1))), }, // id = expr - Expr::Assignment(x) => match x.1 { - //id = id2 = expr2 - Expr::Assignment(x2) => match (x.0, x2.0) { - // var = var = expr2 -> var = expr2 + Expr::Assignment(x) => match x.2 { + //id = id2 op= expr2 + Expr::Assignment(x2) if x.1 == "=" => match (x.0, x2.0) { + // var = var op= expr2 -> var op= expr2 (Expr::Variable(a), Expr::Variable(b)) if a.1.is_none() && b.1.is_none() && a.0 == b.0 && a.3 == b.3 => { // Assignment to the same variable - fold state.set_dirty(); - Expr::Assignment(Box::new((Expr::Variable(a), optimize_expr(x2.1, state), x.2))) + Expr::Assignment(Box::new((Expr::Variable(a), x2.1, optimize_expr(x2.2, state), x.3))) } - // id1 = id2 = expr2 + // id1 = id2 op= expr2 (id1, id2) => { Expr::Assignment(Box::new(( - id1, Expr::Assignment(Box::new((id2, optimize_expr(x2.1, state), x2.2))), x.2, + id1, x.1, Expr::Assignment(Box::new((id2, x2.1, optimize_expr(x2.2, state), x2.3))), x.3, ))) } }, - // id = expr - expr => Expr::Assignment(Box::new((x.0, optimize_expr(expr, state), x.2))), + // id op= expr + expr => Expr::Assignment(Box::new((x.0, x.1, optimize_expr(expr, state), x.3))), }, // lhs.rhs diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 1abdd3be..5b96ffe2 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -53,6 +53,10 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { x.extend(y); Ok(()) }); + lib.set_fn_2_mut("+=", |x: &mut Array, y: Array| { + x.extend(y); + Ok(()) + }); lib.set_fn_2( "+", |mut x: Array, y: Array| { diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index cc2e4c1c..6f7fb0ef 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -42,6 +42,15 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, { Ok(()) }, ); + lib.set_fn_2_mut( + "+=", + |map1: &mut Map, map2: Map| { + map2.into_iter().for_each(|(key, value)| { + map1.insert(key, value); + }); + Ok(()) + }, + ); lib.set_fn_2( "+", |mut map1: Map, map2: Map| { diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index f7ae8930..f4275467 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -105,10 +105,24 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin Ok(s.into()) }, ); + lib.set_fn_2_mut("+=", |s: &mut ImmutableString, ch: char| { + shared_make_mut(s).push(ch); + Ok(()) + }); lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| { shared_make_mut(s).push(ch); Ok(()) }); + lib.set_fn_2_mut( + "+=", + |s: &mut ImmutableString, s2: ImmutableString| { + if !s2.is_empty() { + shared_make_mut(s).push_str(s2.as_str()); + } + + Ok(()) + } + ); lib.set_fn_2_mut( "append", |s: &mut ImmutableString, s2: ImmutableString| { diff --git a/src/parser.rs b/src/parser.rs index 4180ffbf..bbb5c808 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -405,8 +405,8 @@ pub enum Expr { Option, )>, ), - /// expr = expr - Assignment(Box<(Expr, Expr, Position)>), + /// expr op= expr + Assignment(Box<(Expr, Cow<'static, str>, Expr, Position)>), /// lhs.rhs Dot(Box<(Expr, Expr, Position)>), /// expr[expr] @@ -508,12 +508,13 @@ impl Expr { Self::Stmt(x) => x.1, Self::Variable(x) => (x.0).1, Self::FnCall(x) => (x.0).2, + Self::Assignment(x) => x.0.position(), Self::And(x) | Self::Or(x) | Self::In(x) => x.2, Self::True(pos) | Self::False(pos) | Self::Unit(pos) => *pos, - Self::Assignment(x) | Self::Dot(x) | Self::Index(x) => x.0.position(), + Self::Dot(x) | Self::Index(x) => x.0.position(), } } @@ -538,7 +539,7 @@ impl Expr { Self::True(pos) => *pos = new_pos, Self::False(pos) => *pos = new_pos, Self::Unit(pos) => *pos = new_pos, - Self::Assignment(x) => x.2 = new_pos, + Self::Assignment(x) => x.3 = new_pos, Self::Dot(x) => x.2 = new_pos, Self::Index(x) => x.2 = new_pos, } @@ -879,30 +880,25 @@ fn parse_index_chain<'a>( } #[cfg(not(feature = "no_float"))] - Expr::FloatConstant(x) => { + Expr::FloatConstant(_) => { return Err(PERR::MalformedIndexExpr( "Only arrays, object maps and strings can be indexed".into(), ) - .into_err(x.1)) + .into_err(lhs.position())) } - Expr::CharConstant(x) => { + Expr::CharConstant(_) + | Expr::Assignment(_) + | Expr::And(_) + | Expr::Or(_) + | Expr::In(_) + | Expr::True(_) + | Expr::False(_) + | Expr::Unit(_) => { return Err(PERR::MalformedIndexExpr( "Only arrays, object maps and strings can be indexed".into(), ) - .into_err(x.1)) - } - Expr::Assignment(x) | Expr::And(x) | Expr::Or(x) | Expr::In(x) => { - return Err(PERR::MalformedIndexExpr( - "Only arrays, object maps and strings can be indexed".into(), - ) - .into_err(x.2)) - } - Expr::True(pos) | Expr::False(pos) | Expr::Unit(pos) => { - return Err(PERR::MalformedIndexExpr( - "Only arrays, object maps and strings can be indexed".into(), - ) - .into_err(pos)) + .into_err(lhs.position())) } _ => (), @@ -920,32 +916,25 @@ fn parse_index_chain<'a>( } #[cfg(not(feature = "no_float"))] - Expr::FloatConstant(x) => { + Expr::FloatConstant(_) => { return Err(PERR::MalformedIndexExpr( "Only arrays, object maps and strings can be indexed".into(), ) - .into_err(x.1)) + .into_err(lhs.position())) } - Expr::CharConstant(x) => { + Expr::CharConstant(_) + | Expr::Assignment(_) + | Expr::And(_) + | Expr::Or(_) + | Expr::In(_) + | Expr::True(_) + | Expr::False(_) + | Expr::Unit(_) => { return Err(PERR::MalformedIndexExpr( "Only arrays, object maps and strings can be indexed".into(), ) - .into_err(x.1)) - } - - Expr::Assignment(x) | Expr::And(x) | Expr::Or(x) | Expr::In(x) => { - return Err(PERR::MalformedIndexExpr( - "Only arrays, object maps and strings can be indexed".into(), - ) - .into_err(x.2)) - } - - Expr::True(pos) | Expr::False(pos) | Expr::Unit(pos) => { - return Err(PERR::MalformedIndexExpr( - "Only arrays, object maps and strings can be indexed".into(), - ) - .into_err(pos)) + .into_err(lhs.position())) } _ => (), @@ -953,46 +942,46 @@ fn parse_index_chain<'a>( // lhs[float] #[cfg(not(feature = "no_float"))] - Expr::FloatConstant(x) => { + x @ Expr::FloatConstant(_) => { return Err(PERR::MalformedIndexExpr( "Array access expects integer index, not a float".into(), ) - .into_err(x.1)) + .into_err(x.position())) } // lhs[char] - Expr::CharConstant(x) => { + x @ Expr::CharConstant(_) => { return Err(PERR::MalformedIndexExpr( "Array access expects integer index, not a character".into(), ) - .into_err(x.1)) + .into_err(x.position())) } // lhs[??? = ??? ] - Expr::Assignment(x) => { + x @ Expr::Assignment(_) => { return Err(PERR::MalformedIndexExpr( - "Array access expects integer index, not ()".into(), + "Array access expects integer index, not an assignment".into(), ) - .into_err(x.2)) + .into_err(x.position())) } // lhs[()] - Expr::Unit(pos) => { + x @ Expr::Unit(_) => { return Err(PERR::MalformedIndexExpr( "Array access expects integer index, not ()".into(), ) - .into_err(*pos)) + .into_err(x.position())) } // lhs[??? && ???], lhs[??? || ???], lhs[??? in ???] - Expr::And(x) | Expr::Or(x) | Expr::In(x) => { + x @ Expr::And(_) | x @ Expr::Or(_) | x @ Expr::In(_) => { return Err(PERR::MalformedIndexExpr( "Array access expects integer index, not a boolean".into(), ) - .into_err(x.2)) + .into_err(x.position())) } // lhs[true], lhs[false] - Expr::True(pos) | Expr::False(pos) => { + x @ Expr::True(_) | x @ Expr::False(_) => { return Err(PERR::MalformedIndexExpr( "Array access expects integer index, not a boolean".into(), ) - .into_err(*pos)) + .into_err(x.position())) } // All other expressions _ => (), @@ -1391,17 +1380,22 @@ fn parse_unary<'a>( } fn make_assignment_stmt<'a>( + fn_name: Cow<'static, str>, state: &mut ParseState, lhs: Expr, rhs: Expr, pos: Position, ) -> Result { match &lhs { - Expr::Variable(x) if x.3.is_none() => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), + Expr::Variable(x) if x.3.is_none() => { + Ok(Expr::Assignment(Box::new((lhs, fn_name.into(), rhs, pos)))) + } Expr::Variable(x) => { let ((name, name_pos), _, _, index) = x.as_ref(); match state.stack[(state.len() - index.unwrap().get())].1 { - ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), + ScopeEntryType::Normal => { + Ok(Expr::Assignment(Box::new((lhs, fn_name.into(), rhs, pos)))) + } // Constant values cannot be assigned to ScopeEntryType::Constant => { Err(PERR::AssignmentToConstant(name.clone()).into_err(*name_pos)) @@ -1410,11 +1404,15 @@ fn make_assignment_stmt<'a>( } } Expr::Index(x) | Expr::Dot(x) => match &x.0 { - Expr::Variable(x) if x.3.is_none() => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), + Expr::Variable(x) if x.3.is_none() => { + Ok(Expr::Assignment(Box::new((lhs, fn_name.into(), rhs, pos)))) + } Expr::Variable(x) => { let ((name, name_pos), _, _, index) = x.as_ref(); match state.stack[(state.len() - index.unwrap().get())].1 { - ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), + ScopeEntryType::Normal => { + Ok(Expr::Assignment(Box::new((lhs, fn_name.into(), rhs, pos)))) + } // Constant values cannot be assigned to ScopeEntryType::Constant => { Err(PERR::AssignmentToConstant(name.clone()).into_err(*name_pos)) @@ -1447,11 +1445,7 @@ fn parse_op_assignment_stmt<'a>( } let op = match token { - Token::Equals => { - let pos = eat_token(input, Token::Equals); - let rhs = parse_expr(input, state, level + 1, allow_stmt_expr)?; - return make_assignment_stmt(state, lhs, rhs, pos); - } + Token::Equals => "".into(), Token::PlusAssign => Token::Plus.syntax(), Token::MinusAssign => Token::Minus.syntax(), Token::MultiplyAssign => Token::Multiply.syntax(), @@ -1467,20 +1461,9 @@ fn parse_op_assignment_stmt<'a>( _ => return Ok(lhs), }; - input.next(); - - let lhs_copy = lhs.clone(); + let (_, pos) = input.next().unwrap(); let rhs = parse_expr(input, state, level + 1, allow_stmt_expr)?; - - // lhs op= rhs -> lhs = op(lhs, rhs) - let mut args = StaticVec::new(); - args.push(lhs_copy); - args.push(rhs); - - let hash = calc_fn_hash(empty(), &op, args.len(), empty()); - let rhs_expr = Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))); - - make_assignment_stmt(state, lhs, rhs_expr, pos) + make_assignment_stmt(op, state, lhs, rhs, pos) } /// Make a dot expression. @@ -1553,33 +1536,26 @@ fn make_dot_expr( /// Make an 'in' expression. fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result { match (&lhs, &rhs) { - (_, Expr::IntegerConstant(x)) => { + (_, x @ Expr::IntegerConstant(_)) + | (_, x @ Expr::And(_)) + | (_, x @ Expr::Or(_)) + | (_, x @ Expr::In(_)) + | (_, x @ Expr::Assignment(_)) + | (_, x @ Expr::True(_)) + | (_, x @ Expr::False(_)) + | (_, x @ Expr::Unit(_)) => { return Err(PERR::MalformedInExpr( "'in' expression expects a string, array or object map".into(), ) - .into_err(x.1)) - } - - (_, Expr::And(x)) | (_, Expr::Or(x)) | (_, Expr::In(x)) | (_, Expr::Assignment(x)) => { - return Err(PERR::MalformedInExpr( - "'in' expression expects a string, array or object map".into(), - ) - .into_err(x.2)) - } - - (_, Expr::True(pos)) | (_, Expr::False(pos)) | (_, Expr::Unit(pos)) => { - return Err(PERR::MalformedInExpr( - "'in' expression expects a string, array or object map".into(), - ) - .into_err(*pos)) + .into_err(x.position())) } #[cfg(not(feature = "no_float"))] - (_, Expr::FloatConstant(x)) => { + (_, x @ Expr::FloatConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression expects a string, array or object map".into(), ) - .into_err(x.1)) + .into_err(x.position())) } // "xxx" in "xxxx", 'x' in "xxxx" - OK! @@ -1588,63 +1564,58 @@ fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result { + (x @ Expr::FloatConstant(_), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not a float".into(), ) - .into_err(x.1)) + .into_err(x.position())) } // 123 in "xxxx" - (Expr::IntegerConstant(x), Expr::StringConstant(_)) => { + (x @ Expr::IntegerConstant(_), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not a number".into(), ) - .into_err(x.1)) + .into_err(x.position())) } // (??? && ???) in "xxxx", (??? || ???) in "xxxx", (??? in ???) in "xxxx", - (Expr::And(x), Expr::StringConstant(_)) - | (Expr::Or(x), Expr::StringConstant(_)) - | (Expr::In(x), Expr::StringConstant(_)) => { - return Err(PERR::MalformedInExpr( - "'in' expression for a string expects a string, not a boolean".into(), - ) - .into_err(x.2)) - } // true in "xxxx", false in "xxxx" - (Expr::True(pos), Expr::StringConstant(_)) - | (Expr::False(pos), Expr::StringConstant(_)) => { + (x @ Expr::And(_), Expr::StringConstant(_)) + | (x @ Expr::Or(_), Expr::StringConstant(_)) + | (x @ Expr::In(_), Expr::StringConstant(_)) + | (x @ Expr::True(_), Expr::StringConstant(_)) + | (x @ Expr::False(_), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not a boolean".into(), ) - .into_err(*pos)) + .into_err(x.position())) } // [???, ???, ???] in "xxxx" - (Expr::Array(x), Expr::StringConstant(_)) => { + (x @ Expr::Array(_), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not an array".into(), ) - .into_err(x.1)) + .into_err(x.position())) } // #{...} in "xxxx" - (Expr::Map(x), Expr::StringConstant(_)) => { + (x @ Expr::Map(_), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not an object map".into(), ) - .into_err(x.1)) + .into_err(x.position())) } // (??? = ???) in "xxxx" - (Expr::Assignment(x), Expr::StringConstant(_)) => { + (x @ Expr::Assignment(_), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( - "'in' expression for a string expects a string, not ()".into(), + "'in' expression for a string expects a string, not an assignment".into(), ) - .into_err(x.2)) + .into_err(x.position())) } // () in "xxxx" - (Expr::Unit(pos), Expr::StringConstant(_)) => { + (x @ Expr::Unit(_), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not ()".into(), ) - .into_err(*pos)) + .into_err(x.position())) } // "xxx" in #{...}, 'x' in #{...} - OK! @@ -1652,62 +1623,58 @@ fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result { + (x @ Expr::FloatConstant(_), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not a float".into(), ) - .into_err(x.1)) + .into_err(x.position())) } // 123 in #{...} - (Expr::IntegerConstant(x), Expr::Map(_)) => { + (x @ Expr::IntegerConstant(_), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not a number".into(), ) - .into_err(x.1)) + .into_err(x.position())) } // (??? && ???) in #{...}, (??? || ???) in #{...}, (??? in ???) in #{...}, - (Expr::And(x), Expr::Map(_)) - | (Expr::Or(x), Expr::Map(_)) - | (Expr::In(x), Expr::Map(_)) => { - return Err(PERR::MalformedInExpr( - "'in' expression for an object map expects a string, not a boolean".into(), - ) - .into_err(x.2)) - } // true in #{...}, false in #{...} - (Expr::True(pos), Expr::Map(_)) | (Expr::False(pos), Expr::Map(_)) => { + (x @ Expr::And(_), Expr::Map(_)) + | (x @ Expr::Or(_), Expr::Map(_)) + | (x @ Expr::In(_), Expr::Map(_)) + | (x @ Expr::True(_), Expr::Map(_)) + | (x @ Expr::False(_), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not a boolean".into(), ) - .into_err(*pos)) + .into_err(x.position())) } // [???, ???, ???] in #{..} - (Expr::Array(x), Expr::Map(_)) => { + (x @ Expr::Array(_), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not an array".into(), ) - .into_err(x.1)) + .into_err(x.position())) } // #{...} in #{..} - (Expr::Map(x), Expr::Map(_)) => { + (x @ Expr::Map(_), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not an object map".into(), ) - .into_err(x.1)) + .into_err(x.position())) } // (??? = ???) in #{...} - (Expr::Assignment(x), Expr::Map(_)) => { + (x @ Expr::Assignment(_), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( - "'in' expression for an object map expects a string, not ()".into(), + "'in' expression for an object map expects a string, not an assignment".into(), ) - .into_err(x.2)) + .into_err(x.position())) } // () in #{...} - (Expr::Unit(pos), Expr::Map(_)) => { + (x @ Expr::Unit(_), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not ()".into(), ) - .into_err(*pos)) + .into_err(x.position())) } _ => (), From b34d5fe3a18d0769a3f09abd21ae884d8086b9a4 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 26 May 2020 14:14:03 +0800 Subject: [PATCH 28/53] Complete ImmutableString. --- README.md | 7 +- RELEASES.md | 10 +- src/any.rs | 22 +-- src/engine.rs | 1 + src/fn_native.rs | 13 +- src/fn_register.rs | 2 +- src/lib.rs | 24 +-- src/packages/arithmetic.rs | 163 ++++++--------------- src/packages/logic.rs | 41 ------ src/packages/map_basic.rs | 5 +- src/packages/mod.rs | 2 +- src/packages/string_basic.rs | 62 +------- src/packages/string_more.rs | 19 ++- src/parser.rs | 6 +- src/utils.rs | 276 ++++++++++++++++++++++++++++++++++- 15 files changed, 379 insertions(+), 274 deletions(-) diff --git a/README.md b/README.md index a2720f62..8120cb6c 100644 --- a/README.md +++ b/README.md @@ -514,9 +514,9 @@ This is useful on some 32-bit targets where using 64-bit integers incur a perfor If no floating-point is needed or supported, use the [`no_float`] feature to remove it. -Strings in Rhai are _immutable_, meaning that they can be shared but not modified. In actual, the `ImmutableString` type -is implemented as an `Rc`- or `Arc`-wrapped `String`. Any modification done to a Rhai string will cause the string to be cloned -and the modifications made to the copy. +[Strings] in Rhai are _immutable_, meaning that they can be shared but not modified. In actual, the `ImmutableString` type +is an alias to `Rc` or `Arc` (depending on the [`sync`] feature). +Any modification done to a Rhai string will cause the string to be cloned and the modifications made to the copy. The `to_string` function converts a standard type into a [string] for display purposes. @@ -612,6 +612,7 @@ The following conversion traits are implemented for `Dynamic`: * `From` (`i32` if [`only_i32`]) * `From` (if not [`no_float`]) * `From` +* `From` * `From` * `From` * `From>` (into an [array]) diff --git a/RELEASES.md b/RELEASES.md index d5251d9c..56e04b6b 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -4,8 +4,8 @@ Rhai Release Notes Version 0.14.2 ============== -Regression ----------- +Regression fix +-------------- * Do not optimize script with `eval_expression` - it is assumed to be one-off and short. @@ -18,6 +18,10 @@ Breaking changes * Default maximum limit on levels of nested function calls is fine-tuned and set to a different value. * Some operator functions are now built in (see _Speed enhancements_ below), so they are available even under `Engine::new_raw`. +* Strings are now immutable. The type `rhai::ImmutableString` is used instead of `std::string::String`. + This is to avoid excessive cloning of strings. All native-Rust functions taking string parameters + should switch to `rhai::ImmutableString` (which is either `Rc` or `Arc` depending on + whether the `sync` feature is used). New features ------------ @@ -35,6 +39,8 @@ Speed enhancements significant speed-up. * Implementations of common operators for standard types are removed from the `ArithmeticPackage` and `LogicPackage` (and therefore the `CorePackage`) because they are now always available, even under `Engine::new_raw`. +* Operator-assignment statements (e.g. `+=`) are now handled directly and much faster. +* Strings are now _immutable_ and use the `rhai::ImmutableString` type, eliminating large amounts of cloning. Version 0.14.1 diff --git a/src/any.rs b/src/any.rs index 5a72e957..3bcbb049 100644 --- a/src/any.rs +++ b/src/any.rs @@ -1,6 +1,5 @@ //! Helper module which defines the `Any` trait to to allow dynamic value handling. -use crate::fn_native::shared_unwrap; use crate::parser::{ImmutableString, INT}; use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast}; @@ -21,9 +20,7 @@ use crate::stdlib::{ boxed::Box, collections::HashMap, fmt, - rc::Rc, string::String, - sync::Arc, vec::Vec, }; @@ -417,13 +414,10 @@ impl Dynamic { match self.0 { Union::Unit(value) => unsafe_try_cast(value), Union::Bool(value) => unsafe_try_cast(value), - Union::Str(value) => { - if type_id == TypeId::of::() { - unsafe_try_cast(value) - } else { - unsafe_try_cast((*value).clone()) - } + Union::Str(value) if type_id == TypeId::of::() => { + unsafe_try_cast(value) } + Union::Str(value) => unsafe_try_cast(value.into_owned()), Union::Char(value) => unsafe_try_cast(value), Union::Int(value) => unsafe_try_cast(value), #[cfg(not(feature = "no_float"))] @@ -464,12 +458,10 @@ impl Dynamic { match self.0 { Union::Unit(value) => unsafe_try_cast(value).unwrap(), Union::Bool(value) => unsafe_try_cast(value).unwrap(), - Union::Str(value) => if type_id == TypeId::of::() { - unsafe_try_cast(value) - } else { - unsafe_try_cast((*value).clone()) + Union::Str(value) if type_id == TypeId::of::() => { + unsafe_try_cast(value).unwrap() } - .unwrap(), + Union::Str(value) => unsafe_try_cast(value.into_owned()).unwrap(), Union::Char(value) => unsafe_try_cast(value).unwrap(), Union::Int(value) => unsafe_try_cast(value).unwrap(), #[cfg(not(feature = "no_float"))] @@ -588,7 +580,7 @@ impl Dynamic { /// Returns the name of the actual type if the cast fails. pub fn take_string(self) -> Result { match self.0 { - Union::Str(s) => Ok(shared_unwrap(s).unwrap_or_else(|s| (*s).clone())), + Union::Str(s) => Ok(s.into_owned()), _ => Err(self.type_name()), } } diff --git a/src/engine.rs b/src/engine.rs index 40930d6f..54907c0b 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -2055,6 +2055,7 @@ fn run_builtin_binary_op( let y = y.downcast_ref::().unwrap(); match op { + "+" => return Ok(Some((x + y).into())), "==" => return Ok(Some((x == y).into())), "!=" => return Ok(Some((x != y).into())), ">" => return Ok(Some((x > y).into())), diff --git a/src/fn_native.rs b/src/fn_native.rs index 1975a3c5..6c43adc6 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -19,6 +19,8 @@ pub type Shared = Rc; #[cfg(feature = "sync")] pub type Shared = Arc; +/// Consume a `Shared` resource and return a mutable reference to the wrapped value. +/// If the resource is shared (i.e. has other outstanding references), a cloned copy is used. pub fn shared_make_mut(value: &mut Shared) -> &mut T { #[cfg(not(feature = "sync"))] { @@ -30,14 +32,19 @@ pub fn shared_make_mut(value: &mut Shared) -> &mut T { } } -pub fn shared_unwrap(value: Shared) -> Result> { +/// Consume a `Shared` resource, assuming that it is unique (i.e. not shared). +/// +/// # Panics +/// +/// Panics if the resource is shared (i.e. has other outstanding references). +pub fn shared_take(value: Shared) -> T { #[cfg(not(feature = "sync"))] { - Rc::try_unwrap(value) + Rc::try_unwrap(value).map_err(|_| ()).unwrap() } #[cfg(feature = "sync")] { - Arc::try_unwrap(value) + Arc::try_unwrap(value).map_err(|_| ()).unwrap() } } diff --git a/src/fn_register.rs b/src/fn_register.rs index 1cfe4506..cb208d12 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -8,7 +8,7 @@ use crate::fn_native::{CallableFunction, FnAny, FnCallArgs}; use crate::parser::FnAccess; use crate::result::EvalAltResult; -use crate::stdlib::{any::TypeId, boxed::Box, mem, string::ToString}; +use crate::stdlib::{any::TypeId, boxed::Box, mem}; /// Trait to register custom functions with the `Engine`. pub trait RegisterFn { diff --git a/src/lib.rs b/src/lib.rs index 454e3d13..106e83d4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,18 +49,18 @@ //! //! ## Optional features //! -//! | Feature | Description | -//! | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | -//! | `unchecked` | Exclude arithmetic checking (such as overflows and division by zero). Beware that a bad script may panic the entire system! | -//! | `no_function` | Disable script-defined functions if not needed. | -//! | `no_index` | Disable arrays and indexing features if not needed. | -//! | `no_object` | Disable support for custom types and objects. | -//! | `no_float` | Disable floating-point numbers and math if not needed. | -//! | `no_optimize` | Disable the script optimizer. | -//! | `only_i32` | Set the system integer type to `i32` and disable all other integer types. `INT` is set to `i32`. | -//! | `only_i64` | Set the system integer type to `i64` and disable all other integer types. `INT` is set to `i64`. | -//! | `no_std` | Build for `no-std`. Notice that additional dependencies will be pulled in to replace `std` features. | -//! | `sync` | Restrict all values types to those that are `Send + Sync`. Under this feature, `Engine`, `Scope` and `AST` are all `Send + Sync`. | +//! | Feature | Description | +//! | ------------- | ----------------------------------------------------------------------------------------------------------------------------------| +//! | `unchecked` | Exclude arithmetic checking (such as overflows and division by zero). Beware that a bad script may panic the entire system! | +//! | `no_function` | Disable script-defined functions if not needed. | +//! | `no_index` | Disable arrays and indexing features if not needed. | +//! | `no_object` | Disable support for custom types and objects. | +//! | `no_float` | Disable floating-point numbers and math if not needed. | +//! | `no_optimize` | Disable the script optimizer. | +//! | `only_i32` | Set the system integer type to `i32` and disable all other integer types. `INT` is set to `i32`. | +//! | `only_i64` | Set the system integer type to `i64` and disable all other integer types. `INT` is set to `i64`. | +//! | `no_std` | Build for `no-std`. Notice that additional dependencies will be pulled in to replace `std` features. | +//! | `sync` | Restrict all values types to those that are `Send + Sync`. Under this feature, `Engine`, `Scope` and `AST` are all `Send + Sync`. | //! //! [Check out the README on GitHub for details on the Rhai language!](https://github.com/jonathandturner/rhai) diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index 8f7a0b5b..ead54f8c 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -269,141 +269,69 @@ macro_rules! reg_op { } def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { - // Checked basic arithmetic - #[cfg(not(feature = "unchecked"))] + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] { - // reg_op!(lib, "+", add, INT); - // reg_op!(lib, "-", sub, INT); - // reg_op!(lib, "*", mul, INT); - // reg_op!(lib, "/", div, INT); - - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] + #[cfg(not(feature = "unchecked"))] { - // reg_op!(lib, "+", add, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "-", sub, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "*", mul, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "/", div, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // Checked basic arithmetic reg_op!(lib, "+", add, i8, u8, i16, u16, i32, u32, u64, i128, u128); reg_op!(lib, "-", sub, i8, u8, i16, u16, i32, u32, u64, i128, u128); reg_op!(lib, "*", mul, i8, u8, i16, u16, i32, u32, u64, i128, u128); reg_op!(lib, "/", div, i8, u8, i16, u16, i32, u32, u64, i128, u128); - } - } - - // Unchecked basic arithmetic - #[cfg(feature = "unchecked")] - { - // reg_op!(lib, "+", add_u, INT); - // reg_op!(lib, "-", sub_u, INT); - // reg_op!(lib, "*", mul_u, INT); - // reg_op!(lib, "/", div_u, INT); - - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { - // reg_op!(lib, "+", add_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "-", sub_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "*", mul_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "/", div_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "+", add_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); - reg_op!(lib, "-", sub_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); - reg_op!(lib, "*", mul_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); - reg_op!(lib, "/", div_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); - } - } - - // Basic arithmetic for floating-point - no need to check - #[cfg(not(feature = "no_float"))] - { - // reg_op!(lib, "+", add_u, f32, f64); - // reg_op!(lib, "-", sub_u, f32, f64); - // reg_op!(lib, "*", mul_u, f32, f64); - // reg_op!(lib, "/", div_u, f32, f64); - reg_op!(lib, "+", add_u, f32); - reg_op!(lib, "-", sub_u, f32); - reg_op!(lib, "*", mul_u, f32); - reg_op!(lib, "/", div_u, f32); - } - - // Bit operations - // reg_op!(lib, "|", binary_or, INT); - // reg_op!(lib, "&", binary_and, INT); - // reg_op!(lib, "^", binary_xor, INT); - - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { - // reg_op!(lib, "|", binary_or, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "&", binary_and, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "^", binary_xor, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op!(lib, "|", binary_or, i8, u8, i16, u16, i32, u32, u64, i128, u128); - reg_op!(lib, "&", binary_and, i8, u8, i16, u16, i32, u32, u64, i128, u128); - reg_op!(lib, "^", binary_xor, i8, u8, i16, u16, i32, u32, u64, i128, u128); - } - - // Checked bit shifts - #[cfg(not(feature = "unchecked"))] - { - // reg_op!(lib, "<<", shl, INT); - // reg_op!(lib, ">>", shr, INT); - // reg_op!(lib, "%", modulo, INT); - - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { - // reg_op!(lib, "<<", shl, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, ">>", shr, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "%", modulo, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // Checked bit shifts reg_op!(lib, "<<", shl, i8, u8, i16, u16, i32, u32, u64, i128, u128); reg_op!(lib, ">>", shr, i8, u8, i16, u16, i32, u32, u64, i128, u128); reg_op!(lib, "%", modulo, i8, u8, i16, u16, i32, u32, u64, i128, u128); } - } - // Unchecked bit shifts - #[cfg(feature = "unchecked")] - { - // reg_op!(lib, "<<", shl_u, INT, INT); - // reg_op!(lib, ">>", shr_u, INT, INT); - // reg_op!(lib, "%", modulo_u, INT); - - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] + #[cfg(feature = "unchecked")] { - // reg_op!(lib, "<<", shl_u, i64, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, ">>", shr_u, i64, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "%", modulo_u, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + // Unchecked basic arithmetic + reg_op!(lib, "+", add_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "-", sub_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "*", mul_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "/", div_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); + // Unchecked bit shifts reg_op!(lib, "<<", shl_u, i64, i8, u8, i16, u16, i32, u32, u64, i128, u128); reg_op!(lib, ">>", shr_u, i64, i8, u8, i16, u16, i32, u32, u64, i128, u128); reg_op!(lib, "%", modulo_u, i8, u8, i16, u16, i32, u32, u64, i128, u128); } } - // Checked power - #[cfg(not(feature = "unchecked"))] - { - // lib.set_fn_2("~", pow_i_i); - - #[cfg(not(feature = "no_float"))] - lib.set_fn_2("~", pow_f_i); - } - - // Unchecked power - #[cfg(feature = "unchecked")] - { - // lib.set_fn_2("~", pow_i_i_u); - - #[cfg(not(feature = "no_float"))] - lib.set_fn_2("~", pow_f_i_u); - } - - // Floating-point modulo and power + // Basic arithmetic for floating-point - no need to check #[cfg(not(feature = "no_float"))] { - // reg_op!(lib, "%", modulo_u, f32, f64); + reg_op!(lib, "+", add_u, f32); + reg_op!(lib, "-", sub_u, f32); + reg_op!(lib, "*", mul_u, f32); + reg_op!(lib, "/", div_u, f32); + } + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_op!(lib, "|", binary_or, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "&", binary_and, i8, u8, i16, u16, i32, u32, u64, i128, u128); + reg_op!(lib, "^", binary_xor, i8, u8, i16, u16, i32, u32, u64, i128, u128); + } + + #[cfg(not(feature = "no_float"))] + { + // Checked power + #[cfg(not(feature = "unchecked"))] + lib.set_fn_2("~", pow_f_i); + + // Unchecked power + #[cfg(feature = "unchecked")] + lib.set_fn_2("~", pow_f_i_u); + + // Floating-point modulo and power reg_op!(lib, "%", modulo_u, f32); - // lib.set_fn_2("~", pow_f_f); + + // Floating-point unary + reg_unary!(lib, "-", neg_u, f32, f64); + reg_unary!(lib, "abs", abs_u, f32, f64); } // Checked unary @@ -433,11 +361,4 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { reg_unary!(lib, "abs", abs_u, i8, i16, i32, i64, i128); } } - - // Floating-point unary - #[cfg(not(feature = "no_float"))] - { - reg_unary!(lib, "-", neg_u, f32, f64); - reg_unary!(lib, "abs", abs_u, f32, f64); - } }); diff --git a/src/packages/logic.rs b/src/packages/logic.rs index 50d74466..41ca035f 100644 --- a/src/packages/logic.rs +++ b/src/packages/logic.rs @@ -1,6 +1,5 @@ use crate::def_package; use crate::module::FuncReturn; -use crate::parser::{ImmutableString, INT}; // Comparison operators pub fn lt(x: T, y: T) -> FuncReturn { @@ -23,12 +22,6 @@ pub fn ne(x: T, y: T) -> FuncReturn { } // Logic operators -fn and(x: bool, y: bool) -> FuncReturn { - Ok(x && y) -} -fn or(x: bool, y: bool) -> FuncReturn { - Ok(x || y) -} fn not(x: bool) -> FuncReturn { Ok(!x) } @@ -40,30 +33,9 @@ macro_rules! reg_op { } def_package!(crate:LogicPackage:"Logical operators.", lib, { - // reg_op!(lib, "<", lt, INT, char); - // reg_op!(lib, "<=", lte, INT, char); - // reg_op!(lib, ">", gt, INT, char); - // reg_op!(lib, ">=", gte, INT, char); - // reg_op!(lib, "==", eq, INT, char, bool, ()); - // reg_op!(lib, "!=", ne, INT, char, bool, ()); - - // Special versions for strings - at least avoid copying the first string - // lib.set_fn_2("<", |x: ImmutableString, y: ImmutableString| Ok(*x < y)); - // lib.set_fn_2("<=", |x: ImmutableString, y: ImmutableString| Ok(*x <= y)); - // lib.set_fn_2(">", |x: ImmutableString, y: ImmutableString| Ok(*x > y)); - // lib.set_fn_2(">=", |x: ImmutableString, y: ImmutableString| Ok(*x >= y)); - // lib.set_fn_2("==", |x: ImmutableString, y: ImmutableString| Ok(*x == y)); - // lib.set_fn_2("!=", |x: ImmutableString, y: ImmutableString| Ok(*x != y)); - #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - // reg_op!(lib, "<", lt, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "<=", lte, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, ">", gt, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, ">=", gte, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "==", eq, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - // reg_op!(lib, "!=", ne, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); reg_op!(lib, "<", lt, i8, u8, i16, u16, i32, u32, u64, i128, u128); reg_op!(lib, "<=", lte, i8, u8, i16, u16, i32, u32, u64, i128, u128); reg_op!(lib, ">", gt, i8, u8, i16, u16, i32, u32, u64, i128, u128); @@ -74,12 +46,6 @@ def_package!(crate:LogicPackage:"Logical operators.", lib, { #[cfg(not(feature = "no_float"))] { - // reg_op!(lib, "<", lt, f32, f64); - // reg_op!(lib, "<=", lte, f32, f64); - // reg_op!(lib, ">", gt, f32, f64); - // reg_op!(lib, ">=", gte, f32, f64); - // reg_op!(lib, "==", eq, f32, f64); - // reg_op!(lib, "!=", ne, f32, f64); reg_op!(lib, "<", lt, f32); reg_op!(lib, "<=", lte, f32); reg_op!(lib, ">", gt, f32); @@ -88,12 +54,5 @@ def_package!(crate:LogicPackage:"Logical operators.", lib, { reg_op!(lib, "!=", ne, f32); } - // `&&` and `||` are treated specially as they short-circuit. - // They are implemented as special `Expr` instances, not function calls. - //reg_op!(lib, "||", or, bool); - //reg_op!(lib, "&&", and, bool); - - // lib.set_fn_2("|", or); - // lib.set_fn_2("&", and); lib.set_fn_1("!", not); }); diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index 6f7fb0ef..9be0fb42 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -6,10 +6,7 @@ use crate::engine::Map; use crate::module::FuncReturn; use crate::parser::{ImmutableString, INT}; -use crate::stdlib::{ - string::{String, ToString}, - vec::Vec, -}; +use crate::stdlib::{string::ToString, vec::Vec}; fn map_get_keys(map: &mut Map) -> FuncReturn> { Ok(map.iter().map(|(k, _)| k.to_string().into()).collect()) diff --git a/src/packages/mod.rs b/src/packages/mod.rs index ddd6a30d..3b89b9f4 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -4,7 +4,7 @@ use crate::fn_native::{CallableFunction, IteratorFn, Shared}; use crate::module::Module; use crate::utils::StaticVec; -use crate::stdlib::{any::TypeId, boxed::Box, collections::HashMap, rc::Rc, sync::Arc, vec::Vec}; +use crate::stdlib::any::TypeId; pub(crate) mod arithmetic; mod array_basic; diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index f4275467..ad51e02c 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -1,6 +1,5 @@ use crate::def_package; use crate::engine::{FUNC_TO_STRING, KEYWORD_DEBUG, KEYWORD_PRINT}; -use crate::fn_native::shared_make_mut; use crate::module::FuncReturn; use crate::parser::{ImmutableString, INT}; @@ -13,7 +12,7 @@ use crate::engine::Map; use crate::stdlib::{ fmt::{Debug, Display}, format, - string::{String, ToString}, + string::ToString, }; // Register print and debug @@ -79,58 +78,9 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin lib.set_fn_1_mut(KEYWORD_DEBUG, format_map); } - lib.set_fn_2( - "+", - |s: ImmutableString, ch: char| { - if s.is_empty() { - return Ok(ch.to_string().into()); - } - - let mut s = (*s).clone(); - s.push(ch); - Ok(s) - }, - ); - lib.set_fn_2( - "+", - |s:ImmutableString, s2:ImmutableString| { - if s.is_empty() { - return Ok(s2); - } else if s2.is_empty() { - return Ok(s); - } - - let mut s = (*s).clone(); - s.push_str(s2.as_str()); - Ok(s.into()) - }, - ); - lib.set_fn_2_mut("+=", |s: &mut ImmutableString, ch: char| { - shared_make_mut(s).push(ch); - Ok(()) - }); - lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| { - shared_make_mut(s).push(ch); - Ok(()) - }); - lib.set_fn_2_mut( - "+=", - |s: &mut ImmutableString, s2: ImmutableString| { - if !s2.is_empty() { - shared_make_mut(s).push_str(s2.as_str()); - } - - Ok(()) - } - ); - lib.set_fn_2_mut( - "append", - |s: &mut ImmutableString, s2: ImmutableString| { - if !s2.is_empty() { - shared_make_mut(s).push_str(s2.as_str()); - } - - Ok(()) - } - ); + lib.set_fn_2("+", |s: ImmutableString, ch: char| Ok(s + ch)); + lib.set_fn_2_mut("+=", |s: &mut ImmutableString, ch: char| { *s += ch; Ok(()) }); + lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| { *s += ch; Ok(()) }); + lib.set_fn_2_mut("+=", |s: &mut ImmutableString, s2: ImmutableString| { *s += &s2; Ok(()) }); + lib.set_fn_2_mut("append", |s: &mut ImmutableString, s2: ImmutableString| { *s += &s2; Ok(()) }); }); diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 666f9061..c3c22c31 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -1,5 +1,4 @@ use crate::def_package; -use crate::fn_native::shared_make_mut; use crate::module::FuncReturn; use crate::parser::{ImmutableString, INT}; use crate::utils::StaticVec; @@ -48,12 +47,12 @@ fn sub_string(s: ImmutableString, start: INT, len: INT) -> FuncReturn FuncReturn<()> { let offset = if s.is_empty() || len <= 0 { - shared_make_mut(s).clear(); + s.make_mut().clear(); return Ok(()); } else if start < 0 { 0 } else if (start as usize) >= s.chars().count() { - shared_make_mut(s).clear(); + s.make_mut().clear(); return Ok(()); } else { start as usize @@ -67,7 +66,7 @@ fn crop_string(s: &mut ImmutableString, start: INT, len: INT) -> FuncReturn<()> len as usize }; - let copy = shared_make_mut(s); + let copy = s.make_mut(); copy.clear(); copy.extend(chars.iter().skip(offset).take(len)); @@ -166,17 +165,17 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str }, ); lib.set_fn_1_mut("clear", |s: &mut ImmutableString| { - shared_make_mut(s).clear(); + s.make_mut().clear(); Ok(()) }); lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| { - shared_make_mut(s).push(ch); + s.make_mut().push(ch); Ok(()) }); lib.set_fn_2_mut( "append", |s: &mut ImmutableString, add: ImmutableString| { - shared_make_mut(s).push_str(add.as_str()); + s.make_mut().push_str(add.as_str()); Ok(()) } ); @@ -198,11 +197,11 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str |s: &mut ImmutableString, len: INT| { if len > 0 { let chars: StaticVec<_> = s.chars().collect(); - let copy = shared_make_mut(s); + let copy = s.make_mut(); copy.clear(); copy.extend(chars.into_iter().take(len as usize)); } else { - shared_make_mut(s).clear(); + s.make_mut().clear(); } Ok(()) }, @@ -210,7 +209,7 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str lib.set_fn_3_mut( "pad", |s: &mut ImmutableString, len: INT, ch: char| { - let copy = shared_make_mut(s); + let copy = s.make_mut(); for _ in 0..copy.chars().count() - len as usize { copy.push(ch); } diff --git a/src/parser.rs b/src/parser.rs index bbb5c808..02e83c97 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,7 +4,6 @@ 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::fn_native::Shared; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token, TokenIterator}; @@ -49,11 +48,10 @@ pub type INT = i32; #[cfg(not(feature = "no_float"))] pub type FLOAT = f64; -/// The system immutable string type. -pub type ImmutableString = Shared; - type PERR = ParseErrorType; +pub use crate::utils::ImmutableString; + /// Compiled AST (abstract syntax tree) of a Rhai script. /// /// Currently, `AST` is neither `Send` nor `Sync`. Turn on the `sync` feature to make it `Send + Sync`. diff --git a/src/utils.rs b/src/utils.rs index 126f6809..f4bca92e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,14 +4,18 @@ //! //! The `StaticVec` type has some `unsafe` blocks to handle conversions between `MaybeUninit` and regular types. +use crate::fn_native::{shared_make_mut, shared_take, Shared}; + use crate::stdlib::{ any::TypeId, + borrow::Borrow, fmt, hash::{Hash, Hasher}, iter::FromIterator, mem, mem::MaybeUninit, - ops::{Drop, Index, IndexMut}, + ops::{Add, AddAssign, Deref, Drop, Index, IndexMut}, + str::FromStr, vec::Vec, }; @@ -560,3 +564,273 @@ impl From> for StaticVec { arr } } + +/// The system immutable string type. +/// +/// An `ImmutableString` wraps an `Rc` (or `Arc` under the `sync` feature) +/// so that it can be simply shared and not cloned. +/// +/// # Examples +/// +/// ``` +/// use rhai::ImmutableString; +/// +/// let s1: ImmutableString = "hello".into(); +/// +/// // No actual cloning of the string is involved below. +/// let s2 = s1.clone(); +/// let s3 = s2.clone(); +/// +/// assert_eq!(s1, s2); +/// +/// // Clones the underlying string (because it is already shared) and extracts it. +/// let mut s: String = s1.into_owned(); +/// +/// // Changing the clone has no impact on the previously shared version. +/// s.push_str(", world!"); +/// +/// // The old version still exists. +/// assert_eq!(s2, s3); +/// assert_eq!(s2.as_str(), "hello"); +/// +/// // Not equals! +/// assert_ne!(s2.as_str(), s.as_str()); +/// assert_eq!(s, "hello, world!"); +/// ``` +#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)] +pub struct ImmutableString(Shared); + +impl Deref for ImmutableString { + type Target = String; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl AsRef for ImmutableString { + fn as_ref(&self) -> &String { + &self.0 + } +} + +impl Borrow for ImmutableString { + fn borrow(&self) -> &str { + self.0.as_str() + } +} + +impl From<&str> for ImmutableString { + fn from(value: &str) -> Self { + Self(value.to_string().into()) + } +} +impl From for ImmutableString { + fn from(value: String) -> Self { + Self(value.into()) + } +} + +impl From> for ImmutableString { + fn from(value: Box) -> Self { + Self(value.into()) + } +} + +impl From for String { + fn from(value: ImmutableString) -> Self { + value.into_owned() + } +} + +impl FromStr for ImmutableString { + type Err = (); + + fn from_str(s: &str) -> Result { + Ok(Self(s.to_string().into())) + } +} + +impl FromIterator for ImmutableString { + fn from_iter>(iter: T) -> Self { + Self(iter.into_iter().collect::().into()) + } +} + +impl<'a> FromIterator<&'a char> for ImmutableString { + fn from_iter>(iter: T) -> Self { + Self(iter.into_iter().cloned().collect::().into()) + } +} + +impl<'a> FromIterator<&'a str> for ImmutableString { + fn from_iter>(iter: T) -> Self { + Self(iter.into_iter().collect::().into()) + } +} + +impl<'a> FromIterator for ImmutableString { + fn from_iter>(iter: T) -> Self { + Self(iter.into_iter().collect::().into()) + } +} + +impl fmt::Display for ImmutableString { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self.0.as_str(), f) + } +} + +impl fmt::Debug for ImmutableString { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(self.0.as_str(), f) + } +} + +impl Add for ImmutableString { + type Output = Self; + + fn add(mut self, rhs: Self) -> Self::Output { + if rhs.is_empty() { + self + } else if self.is_empty() { + rhs + } else { + self.make_mut().push_str(rhs.0.as_str()); + self + } + } +} + +impl Add for &ImmutableString { + type Output = ImmutableString; + + fn add(self, rhs: Self) -> Self::Output { + if rhs.is_empty() { + self.clone() + } else if self.is_empty() { + rhs.clone() + } else { + let mut s = self.clone(); + s.make_mut().push_str(rhs.0.as_str()); + s + } + } +} + +impl AddAssign<&ImmutableString> for ImmutableString { + fn add_assign(&mut self, rhs: &ImmutableString) { + if !rhs.is_empty() { + if self.is_empty() { + self.0 = rhs.0.clone(); + } else { + self.make_mut().push_str(rhs.0.as_str()); + } + } + } +} + +impl Add<&str> for ImmutableString { + type Output = Self; + + fn add(mut self, rhs: &str) -> Self::Output { + if rhs.is_empty() { + self + } else { + self.make_mut().push_str(rhs); + self + } + } +} + +impl Add<&str> for &ImmutableString { + type Output = ImmutableString; + + fn add(self, rhs: &str) -> Self::Output { + if rhs.is_empty() { + self.clone() + } else { + let mut s = self.clone(); + s.make_mut().push_str(rhs); + s + } + } +} + +impl AddAssign<&str> for ImmutableString { + fn add_assign(&mut self, rhs: &str) { + if !rhs.is_empty() { + self.make_mut().push_str(rhs); + } + } +} + +impl Add for ImmutableString { + type Output = Self; + + fn add(mut self, rhs: String) -> Self::Output { + if rhs.is_empty() { + self + } else if self.is_empty() { + rhs.into() + } else { + self.make_mut().push_str(&rhs); + self + } + } +} + +impl Add for &ImmutableString { + type Output = ImmutableString; + + fn add(self, rhs: String) -> Self::Output { + if rhs.is_empty() { + self.clone() + } else if self.is_empty() { + rhs.into() + } else { + let mut s = self.clone(); + s.make_mut().push_str(&rhs); + s + } + } +} + +impl Add for ImmutableString { + type Output = Self; + + fn add(mut self, rhs: char) -> Self::Output { + self.make_mut().push(rhs); + self + } +} + +impl Add for &ImmutableString { + type Output = ImmutableString; + + fn add(self, rhs: char) -> Self::Output { + let mut s = self.clone(); + s.make_mut().push(rhs); + s + } +} + +impl AddAssign for ImmutableString { + fn add_assign(&mut self, rhs: char) { + self.make_mut().push(rhs); + } +} + +impl ImmutableString { + /// Consume the `ImmutableString` and convert it into a `String`. + /// If there are other references to the same string, a cloned copy is returned. + pub fn into_owned(mut self) -> String { + self.make_mut(); // Make sure it is unique reference + shared_take(self.0) // Should succeed + } + /// Make sure that the `ImmutableString` is unique (i.e. no other outstanding references). + /// Then return a mutable reference to the `String`. + pub fn make_mut(&mut self) -> &mut String { + shared_make_mut(&mut self.0) + } +} From 27c7cc4af7f59ad9a95eac58f30be09b7c64e489 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 26 May 2020 23:05:21 +0800 Subject: [PATCH 29/53] Fix syntax error. --- src/engine.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 54907c0b..f4f85b9d 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -2150,9 +2150,9 @@ fn run_builtin_op_assignment( "*" => return Ok(Some(*x *= y)), "/" => return Ok(Some(*x /= y)), "%" => return Ok(Some(*x %= y)), - "~" => return Ok(Some(*x = pow_i_i_u(x, y))), - ">>" => return Ok(Some(*x = shr_u(x, y))), - "<<" => return Ok(Some(*x = shl_u(x, y))), + "~" => return Ok(Some(*x = pow_i_i_u(*x, y)?)), + ">>" => return Ok(Some(*x = shr_u(*x, y)?)), + "<<" => return Ok(Some(*x = shl_u(*x, y)?)), _ => (), } From 24a93ef824081ddecbadb2825d32e4b18309aaed Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 26 May 2020 23:05:44 +0800 Subject: [PATCH 30/53] Add missing imports. --- src/utils.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils.rs b/src/utils.rs index f4bca92e..c396df19 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -9,6 +9,7 @@ use crate::fn_native::{shared_make_mut, shared_take, Shared}; use crate::stdlib::{ any::TypeId, borrow::Borrow, + boxed::Box, fmt, hash::{Hash, Hasher}, iter::FromIterator, @@ -16,6 +17,7 @@ use crate::stdlib::{ mem::MaybeUninit, ops::{Add, AddAssign, Deref, Drop, Index, IndexMut}, str::FromStr, + string::{String, ToString}, vec::Vec, }; From 854634afa0bf8ef36e5b19815efe682b8901fde9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 27 May 2020 13:22:10 +0800 Subject: [PATCH 31/53] Allow native overriding implementation of assignment operators. --- README.md | 45 ++++---- RELEASES.md | 4 +- src/engine.rs | 279 ++++++++++++++++++++++++++------------------------ src/parser.rs | 23 +++-- 4 files changed, 185 insertions(+), 166 deletions(-) diff --git a/README.md b/README.md index 8120cb6c..ad4a5037 100644 --- a/README.md +++ b/README.md @@ -395,14 +395,15 @@ are supported. ### Built-in operators -| Operator | Supported for type (see [standard types]) | -| ---------------------------- | ----------------------------------------------------------------------------- | -| `+`, `-`, `*`, `/`, `%`, `~` | `INT`, `FLOAT` (if not [`no_float`]) | -| `<<`, `>>`, `^` | `INT` | -| `&`, `\|` | `INT`, `bool` | -| `&&`, `\|\|` | `bool` | -| `==`, `!=` | `INT`, `FLOAT` (if not [`no_float`]), `bool`, `char`, `()`, `ImmutableString` | -| `>`, `>=`, `<`, `<=` | `INT`, `FLOAT` (if not [`no_float`]), `char`, `()`, `ImmutableString` | +| Operators | Assignment operators | Supported for type (see [standard types]) | +| ------------------------ | ---------------------------- | ----------------------------------------------------------------------------- | +| `+`, | `+=` | `INT`, `FLOAT` (if not [`no_float`]), `ImmutableString` | +| `-`, `*`, `/`, `%`, `~`, | `-=`, `*=`, `/=`, `%=`, `~=` | `INT`, `FLOAT` (if not [`no_float`]) | +| `<<`, `>>`, `^`, | `<<=`, `>>=`, `^=` | `INT` | +| `&`, `\|`, | `&=`, `|=` | `INT`, `bool` | +| `&&`, `\|\|` | | `bool` | +| `==`, `!=` | | `INT`, `FLOAT` (if not [`no_float`]), `bool`, `char`, `()`, `ImmutableString` | +| `>`, `>=`, `<`, `<=` | | `INT`, `FLOAT` (if not [`no_float`]), `char`, `()`, `ImmutableString` | ### Packages @@ -426,20 +427,20 @@ engine.load_package(package.get()); // load the package manually. 'g The follow packages are available: -| Package | Description | In `CorePackage` | In `StandardPackage` | -| ---------------------- | -------------------------------------------------------------------------- | :--------------: | :------------------: | -| `ArithmeticPackage` | Arithmetic operators (e.g. `+`, `-`, `*`, `/`) for different numeric types | Yes | Yes | -| `BasicIteratorPackage` | Numeric ranges (e.g. `range(1, 10)`) | Yes | Yes | -| `LogicPackage` | Logical and comparison operators (e.g. `==`, `>`) | Yes | Yes | -| `BasicStringPackage` | Basic string functions | Yes | Yes | -| `BasicTimePackage` | Basic time functions (e.g. [timestamps]) | Yes | Yes | -| `MoreStringPackage` | Additional string functions | No | Yes | -| `BasicMathPackage` | Basic math functions (e.g. `sin`, `sqrt`) | No | Yes | -| `BasicArrayPackage` | Basic [array] functions | No | Yes | -| `BasicMapPackage` | Basic [object map] functions | No | Yes | -| `EvalPackage` | Disable [`eval`] | No | No | -| `CorePackage` | Basic essentials | | | -| `StandardPackage` | Standard library | | | +| Package | Description | In `CorePackage` | In `StandardPackage` | +| ---------------------- | ------------------------------------------------------------------------------------------------------ | :--------------: | :------------------: | +| `ArithmeticPackage` | Arithmetic operators (e.g. `+`, `-`, `*`, `/`) for numeric types that are not built in (e.g. `u16`) | Yes | Yes | +| `BasicIteratorPackage` | Numeric ranges (e.g. `range(1, 10)`) | Yes | Yes | +| `LogicPackage` | Logical and comparison operators (e.g. `==`, `>`) for numeric types that are not built in (e.g. `u16`) | Yes | Yes | +| `BasicStringPackage` | Basic string functions (e.g. `print`, `debug`, `len`) that are not built in | Yes | Yes | +| `BasicTimePackage` | Basic time functions (e.g. [timestamps]) | Yes | Yes | +| `MoreStringPackage` | Additional string functions, including converting common types to string | No | Yes | +| `BasicMathPackage` | Basic math functions (e.g. `sin`, `sqrt`) | No | Yes | +| `BasicArrayPackage` | Basic [array] functions (not available under `no_index`) | No | Yes | +| `BasicMapPackage` | Basic [object map] functions (not available under `no_object`) | No | Yes | +| `EvalPackage` | Disable [`eval`] | No | No | +| `CorePackage` | Basic essentials | Yes | Yes | +| `StandardPackage` | Standard library | No | Yes | Packages typically contain Rust functions that are callable within a Rhai script. All functions registered in a package is loaded under the _global namespace_ (i.e. they're available without module qualifiers). diff --git a/RELEASES.md b/RELEASES.md index 56e04b6b..bc5a2125 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -34,9 +34,11 @@ Speed enhancements ------------------ * Common operators (e.g. `+`, `>`, `==`) now call into highly efficient built-in implementations for standard types - (i.e. `INT`, `FLOAT`, `bool`, `char`, `()` and some `String`) if not overridden by a registered function. + (i.e. `INT`, `FLOAT`, `bool`, `char`, `()` and `ImmutableString`) if not overridden by a registered function. This yields a 5-10% speed benefit depending on script operator usage. Scripts running tight loops will see significant speed-up. +* Common assignment operators (e.g. `+=`, `%=`) now call into highly efficient built-in implementations for + standard types (i.e. `INT`, `FLOAT`, `bool`, `char`, `()` and `ImmutableString`) if not overridden by a registered function. * Implementations of common operators for standard types are removed from the `ArithmeticPackage` and `LogicPackage` (and therefore the `CorePackage`) because they are now always available, even under `Engine::new_raw`. * Operator-assignment statements (e.g. `+=`) are now handled directly and much faster. diff --git a/src/engine.rs b/src/engine.rs index f4f85b9d..276ce86c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::fn_native::{FnCallArgs, Shared}; +use crate::fn_native::{CallableFunction, FnCallArgs, Shared}; use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; @@ -438,14 +438,18 @@ fn default_print(s: &str) { /// Search for a variable within the scope fn search_scope<'a>( scope: &'a mut Scope, - name: &str, - modules: Option<(&ModuleRef, u64)>, - index: Option, - pos: Position, -) -> Result<(&'a mut Dynamic, ScopeEntryType), Box> { + state: &mut State, + expr: &'a Expr, +) -> Result<(&'a mut Dynamic, &'a str, ScopeEntryType, Position), Box> { + let ((name, pos), modules, hash_var, index) = match expr { + Expr::Variable(x) => x.as_ref(), + _ => unreachable!(), + }; + let index = if state.always_search { None } else { *index }; + #[cfg(not(feature = "no_module"))] { - if let Some((modules, hash_var)) = modules { + if let Some(modules) = modules.as_ref() { let module = if let Some(index) = modules.index() { scope .get_mut(scope.len() - index.get()) @@ -461,9 +465,11 @@ fn search_scope<'a>( }; return Ok(( - module.get_qualified_var_mut(name, hash_var, pos)?, + module.get_qualified_var_mut(name, *hash_var, *pos)?, + name, // Module variables are constant ScopeEntryType::Constant, + *pos, )); } } @@ -473,11 +479,12 @@ fn search_scope<'a>( } else { scope .get_index(name) - .ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(name.into(), pos)))? + .ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(name.into(), *pos)))? .0 }; - Ok(scope.get_mut(index)) + let (val, typ) = scope.get_mut(index); + Ok((val, name, typ, *pos)) } impl Engine { @@ -677,11 +684,8 @@ impl Engine { }); } - // See if it is built in. Only consider situations where: - // 1) It is not a method call, - // 2) the call explicitly specifies `native_only`, - // 3) there are two parameters. - if !is_ref && native_only && args.len() == 2 { + // See if it is built in. + if args.len() == 2 { match run_builtin_binary_op(fn_name, args[0], args[1])? { Some(v) => return Ok((v, false)), None => (), @@ -1147,20 +1151,17 @@ impl Engine { match dot_lhs { // id.??? or id[???] - Expr::Variable(x) => { - let ((name, pos), modules, hash_var, index) = x.as_ref(); - let index = if state.always_search { None } else { *index }; - let mod_and_hash = modules.as_ref().map(|m| (m.as_ref(), *hash_var)); - let (target, typ) = search_scope(scope, &name, mod_and_hash, index, *pos)?; - self.inc_operations(state, *pos)?; + Expr::Variable(_) => { + let (target, name, typ, pos) = search_scope(scope, state, dot_lhs)?; + self.inc_operations(state, pos)?; // Constants cannot be modified match typ { ScopeEntryType::Module => unreachable!(), ScopeEntryType::Constant if new_val.is_some() => { return Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( - name.clone(), - *pos, + name.to_string(), + pos, ))); } ScopeEntryType::Constant | ScopeEntryType::Normal => (), @@ -1404,11 +1405,8 @@ impl Engine { Expr::FloatConstant(x) => Ok(x.0.into()), Expr::StringConstant(x) => Ok(x.0.to_string().into()), Expr::CharConstant(x) => Ok(x.0.into()), - Expr::Variable(x) => { - let ((name, pos), modules, hash_var, index) = x.as_ref(); - let index = if state.always_search { None } else { *index }; - let mod_and_hash = modules.as_ref().map(|m| (m.as_ref(), *hash_var)); - let (val, _) = search_scope(scope, name, mod_and_hash, index, *pos)?; + Expr::Variable(_) => { + let (val, _, _, _) = search_scope(scope, state, expr)?; Ok(val.clone()) } Expr::Property(_) => unreachable!(), @@ -1416,89 +1414,106 @@ impl Engine { // Statement block Expr::Stmt(stmt) => self.eval_stmt(scope, state, lib, &stmt.0, level), - // lhs = rhs + // var op= rhs + Expr::Assignment(x) if matches!(x.0, Expr::Variable(_)) => { + let (lhs_expr, op, rhs_expr, op_pos) = x.as_ref(); + let mut rhs_val = self.eval_expr(scope, state, lib, rhs_expr, level)?; + let (lhs_ptr, name, typ, pos) = search_scope(scope, state, lhs_expr)?; + self.inc_operations(state, pos)?; + + match typ { + // Assignment to constant variable + ScopeEntryType::Constant => Err(Box::new( + EvalAltResult::ErrorAssignmentToConstant(name.to_string(), pos), + )), + // Normal assignment + ScopeEntryType::Normal if op.is_empty() => { + *lhs_ptr = rhs_val; + Ok(Default::default()) + } + // Op-assignment - in order of precedence: + ScopeEntryType::Normal => { + // 1) Native registered overriding function + // 2) Built-in implementation + // 3) Map to `var = var op rhs` + + // Qualifiers (none) + function name + number of arguments + argument `TypeId`'s. + let arg_types = once(lhs_ptr.type_id()).chain(once(rhs_val.type_id())); + let hash_fn = calc_fn_hash(empty(), op, 2, arg_types); + + if let Some(CallableFunction::Method(func)) = self + .global_module + .get_fn(hash_fn) + .or_else(|| self.packages.get_fn(hash_fn)) + { + // Overriding exact implementation + func(&mut [lhs_ptr, &mut rhs_val])?; + } else if run_builtin_op_assignment(op, lhs_ptr, &rhs_val)?.is_none() { + // Not built in, map to `var = var op rhs` + let op = &op[..op.len() - 1]; // extract operator without = + let hash = calc_fn_hash(empty(), op, 2, empty()); + let args = &mut [&mut lhs_ptr.clone(), &mut rhs_val]; + + // Set variable value + *lhs_ptr = self + .exec_fn_call( + state, lib, op, true, hash, args, false, None, *op_pos, level, + ) + .map(|(v, _)| v)?; + } + Ok(Default::default()) + } + // A module cannot be assigned to + ScopeEntryType::Module => unreachable!(), + } + } + + // lhs op= rhs Expr::Assignment(x) => { - let lhs_expr = &x.0; - let op = x.1.as_ref(); - let op_pos = x.3; - let mut rhs_val = self.eval_expr(scope, state, lib, &x.2, level)?; + let (lhs_expr, op, rhs_expr, op_pos) = x.as_ref(); + let mut rhs_val = self.eval_expr(scope, state, lib, rhs_expr, level)?; - // name op= rhs - if let Expr::Variable(x) = &x.0 { - let ((name, pos), modules, hash_var, index) = x.as_ref(); - let index = if state.always_search { None } else { *index }; - let mod_and_hash = modules.as_ref().map(|m| (m.as_ref(), *hash_var)); - let (lhs_ptr, typ) = search_scope(scope, name, mod_and_hash, index, *pos)?; - self.inc_operations(state, *pos)?; - - match typ { - ScopeEntryType::Constant => Err(Box::new( - EvalAltResult::ErrorAssignmentToConstant(name.clone(), *pos), - )), - ScopeEntryType::Normal if !op.is_empty() => { - // Complex op-assignment - if run_builtin_op_assignment(op, lhs_ptr, &rhs_val)?.is_none() { - // Not built in, do function call - let mut lhs_val = lhs_ptr.clone(); - let args = &mut [&mut lhs_val, &mut rhs_val]; - let hash = calc_fn_hash(empty(), op, 2, empty()); - *lhs_ptr = self - .exec_fn_call( - state, lib, op, true, hash, args, false, None, op_pos, - level, - ) - .map(|(v, _)| v)?; - } - Ok(Default::default()) - } - ScopeEntryType::Normal => { - *lhs_ptr = rhs_val; - Ok(Default::default()) - } - // End variable cannot be a module - ScopeEntryType::Module => unreachable!(), - } + let new_val = Some(if op.is_empty() { + // Normal assignment + rhs_val } else { - let new_val = Some(if op.is_empty() { - rhs_val - } else { - // Complex op-assignment - always do function call - let args = &mut [ - &mut self.eval_expr(scope, state, lib, lhs_expr, level)?, - &mut rhs_val, - ]; - let hash = calc_fn_hash(empty(), op, 2, empty()); - self.exec_fn_call( - state, lib, op, true, hash, args, false, None, op_pos, level, - ) - .map(|(v, _)| v)? - }); + // Op-assignment - always map to `lhs = lhs op rhs` + let op = &op[..op.len() - 1]; // extract operator without = + let hash = calc_fn_hash(empty(), op, 2, empty()); + let args = &mut [ + &mut self.eval_expr(scope, state, lib, lhs_expr, level)?, + &mut rhs_val, + ]; + self.exec_fn_call( + state, lib, op, true, hash, args, false, None, *op_pos, level, + ) + .map(|(v, _)| v)? + }); - match &x.0 { - // name op= rhs - Expr::Variable(_) => unreachable!(), - // idx_lhs[idx_expr] op= rhs - #[cfg(not(feature = "no_index"))] - Expr::Index(x) => self.eval_dot_index_chain( - scope, state, lib, &x.0, &x.1, true, x.2, level, new_val, - ), - // dot_lhs.dot_rhs op= rhs - #[cfg(not(feature = "no_object"))] - Expr::Dot(x) => self.eval_dot_index_chain( - scope, state, lib, &x.0, &x.1, false, op_pos, level, new_val, - ), - // Error assignment to constant - expr if expr.is_constant() => { - Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( - expr.get_constant_str(), - expr.position(), - ))) - } - // Syntax error - expr => Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS( + match lhs_expr { + // name op= rhs + Expr::Variable(_) => unreachable!(), + // idx_lhs[idx_expr] op= rhs + #[cfg(not(feature = "no_index"))] + Expr::Index(x) => self.eval_dot_index_chain( + scope, state, lib, &x.0, &x.1, true, x.2, level, new_val, + ), + // dot_lhs.dot_rhs op= rhs + #[cfg(not(feature = "no_object"))] + Expr::Dot(x) => self.eval_dot_index_chain( + scope, state, lib, &x.0, &x.1, false, *op_pos, level, new_val, + ), + // Error assignment to constant + expr if expr.is_constant() => { + Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( + expr.get_constant_str(), expr.position(), - ))), + ))) } + // Syntax error + expr => Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS( + expr.position(), + ))), } } @@ -2132,34 +2147,34 @@ fn run_builtin_op_assignment( #[cfg(not(feature = "unchecked"))] match op { - "+" => return Ok(Some(*x = add(*x, y)?)), - "-" => return Ok(Some(*x = sub(*x, y)?)), - "*" => return Ok(Some(*x = mul(*x, y)?)), - "/" => return Ok(Some(*x = div(*x, y)?)), - "%" => return Ok(Some(*x = modulo(*x, y)?)), - "~" => return Ok(Some(*x = pow_i_i(*x, y)?)), - ">>" => return Ok(Some(*x = shr(*x, y)?)), - "<<" => return Ok(Some(*x = shl(*x, y)?)), + "+=" => return Ok(Some(*x = add(*x, y)?)), + "-=" => return Ok(Some(*x = sub(*x, y)?)), + "*=" => return Ok(Some(*x = mul(*x, y)?)), + "/=" => return Ok(Some(*x = div(*x, y)?)), + "%=" => return Ok(Some(*x = modulo(*x, y)?)), + "~=" => return Ok(Some(*x = pow_i_i(*x, y)?)), + ">>=" => return Ok(Some(*x = shr(*x, y)?)), + "<<=" => return Ok(Some(*x = shl(*x, y)?)), _ => (), } #[cfg(feature = "unchecked")] match op { - "+" => return Ok(Some(*x += y)), - "-" => return Ok(Some(*x -= y)), - "*" => return Ok(Some(*x *= y)), - "/" => return Ok(Some(*x /= y)), - "%" => return Ok(Some(*x %= y)), - "~" => return Ok(Some(*x = pow_i_i_u(*x, y)?)), - ">>" => return Ok(Some(*x = shr_u(*x, y)?)), - "<<" => return Ok(Some(*x = shl_u(*x, y)?)), + "+=" => return Ok(Some(*x += y)), + "-=" => return Ok(Some(*x -= y)), + "*=" => return Ok(Some(*x *= y)), + "/=" => return Ok(Some(*x /= y)), + "%=" => return Ok(Some(*x %= y)), + "~=" => return Ok(Some(*x = pow_i_i_u(*x, y)?)), + ">>=" => return Ok(Some(*x = shr_u(*x, y)?)), + "<<=" => return Ok(Some(*x = shl_u(*x, y)?)), _ => (), } match op { - "&" => return Ok(Some(*x &= y)), - "|" => return Ok(Some(*x |= y)), - "^" => return Ok(Some(*x ^= y)), + "&=" => return Ok(Some(*x &= y)), + "|=" => return Ok(Some(*x |= y)), + "^=" => return Ok(Some(*x ^= y)), _ => (), } } else if args_type == TypeId::of::() { @@ -2167,8 +2182,8 @@ fn run_builtin_op_assignment( let y = y.downcast_ref::().unwrap().clone(); match op { - "&" => return Ok(Some(*x = *x && y)), - "|" => return Ok(Some(*x = *x || y)), + "&=" => return Ok(Some(*x = *x && y)), + "|=" => return Ok(Some(*x = *x || y)), _ => (), } } @@ -2180,12 +2195,12 @@ fn run_builtin_op_assignment( let y = y.downcast_ref::().unwrap().clone(); match op { - "+" => return Ok(Some(*x += y)), - "-" => return Ok(Some(*x -= y)), - "*" => return Ok(Some(*x *= y)), - "/" => return Ok(Some(*x /= y)), - "%" => return Ok(Some(*x %= y)), - "~" => return Ok(Some(*x = pow_f_f(*x, y)?)), + "+=" => return Ok(Some(*x += y)), + "-=" => return Ok(Some(*x -= y)), + "*=" => return Ok(Some(*x *= y)), + "/=" => return Ok(Some(*x /= y)), + "%=" => return Ok(Some(*x %= y)), + "~=" => return Ok(Some(*x = pow_f_f(*x, y)?)), _ => (), } } diff --git a/src/parser.rs b/src/parser.rs index 02e83c97..24e6f76e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1444,17 +1444,18 @@ fn parse_op_assignment_stmt<'a>( let op = match token { Token::Equals => "".into(), - Token::PlusAssign => Token::Plus.syntax(), - Token::MinusAssign => Token::Minus.syntax(), - Token::MultiplyAssign => Token::Multiply.syntax(), - Token::DivideAssign => Token::Divide.syntax(), - Token::LeftShiftAssign => Token::LeftShift.syntax(), - Token::RightShiftAssign => Token::RightShift.syntax(), - Token::ModuloAssign => Token::Modulo.syntax(), - Token::PowerOfAssign => Token::PowerOf.syntax(), - Token::AndAssign => Token::Ampersand.syntax(), - Token::OrAssign => Token::Pipe.syntax(), - Token::XOrAssign => Token::XOr.syntax(), + + Token::PlusAssign + | Token::MinusAssign + | Token::MultiplyAssign + | Token::DivideAssign + | Token::LeftShiftAssign + | Token::RightShiftAssign + | Token::ModuloAssign + | Token::PowerOfAssign + | Token::AndAssign + | Token::OrAssign + | Token::XOrAssign => token.syntax(), _ => return Ok(lhs), }; From 0b259d0062d0b066f0d371147dee82730ec5fbe9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 28 May 2020 10:33:28 +0800 Subject: [PATCH 32/53] Move += for ImmutableString to Engine. --- src/engine.rs | 14 +++++++++----- src/packages/string_basic.rs | 1 - 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 276ce86c..a96bae7b 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -18,10 +18,7 @@ use crate::utils::StaticVec; use crate::parser::FLOAT; #[cfg(not(feature = "no_module"))] -use crate::module::{resolvers, ModuleRef, ModuleResolver}; - -#[cfg(feature = "no_module")] -use crate::parser::ModuleRef; +use crate::module::{resolvers, ModuleResolver}; use crate::stdlib::{ any::TypeId, @@ -30,7 +27,6 @@ use crate::stdlib::{ format, iter::{empty, once}, mem, - num::NonZeroUsize, ops::{Deref, DerefMut}, string::{String, ToString}, vec::Vec, @@ -2186,6 +2182,14 @@ fn run_builtin_op_assignment( "|=" => return Ok(Some(*x = *x || y)), _ => (), } + } else if args_type == TypeId::of::() { + let x = x.downcast_mut::().unwrap(); + let y = y.downcast_ref::().unwrap(); + + match op { + "+=" => return Ok(Some(*x += y)), + _ => (), + } } #[cfg(not(feature = "no_float"))] diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index ad51e02c..262a4f3a 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -81,6 +81,5 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin lib.set_fn_2("+", |s: ImmutableString, ch: char| Ok(s + ch)); lib.set_fn_2_mut("+=", |s: &mut ImmutableString, ch: char| { *s += ch; Ok(()) }); lib.set_fn_2_mut("append", |s: &mut ImmutableString, ch: char| { *s += ch; Ok(()) }); - lib.set_fn_2_mut("+=", |s: &mut ImmutableString, s2: ImmutableString| { *s += &s2; Ok(()) }); lib.set_fn_2_mut("append", |s: &mut ImmutableString, s2: ImmutableString| { *s += &s2; Ok(()) }); }); From 30782212e4819374a2663bde9ddb55e7681a5cd9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 28 May 2020 14:07:34 +0800 Subject: [PATCH 33/53] Add set_fn_4/mut for modules. --- src/module.rs | 103 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 2 deletions(-) diff --git a/src/module.rs b/src/module.rs index f233e35a..457f1b1c 100644 --- a/src/module.rs +++ b/src/module.rs @@ -534,6 +534,105 @@ impl Module { ) } + /// Set a Rust function taking four parameters into the module, returning a hash key. + /// + /// If there is a similar existing Rust function, it is replaced. + /// + /// # Examples + /// + /// ``` + /// use rhai::Module; + /// + /// let mut module = Module::new(); + /// let hash = module.set_fn_3("calc", |x: i64, y: String, z: i64, _w: ()| { + /// Ok(x + y.len() as i64 + z) + /// }); + /// assert!(module.get_fn(hash).is_some()); + /// ``` + pub fn set_fn_4< + A: Variant + Clone, + B: Variant + Clone, + C: Variant + Clone, + D: Variant + Clone, + T: Variant + Clone, + >( + &mut self, + name: impl Into, + #[cfg(not(feature = "sync"))] func: impl Fn(A, B, C, D) -> FuncReturn + 'static, + #[cfg(feature = "sync")] func: impl Fn(A, B, C, D) -> FuncReturn + Send + Sync + 'static, + ) -> u64 { + let f = move |args: &mut FnCallArgs| { + let a = mem::take(args[0]).cast::
(); + let b = mem::take(args[1]).cast::(); + let c = mem::take(args[2]).cast::(); + let d = mem::take(args[3]).cast::(); + + func(a, b, c, d).map(Dynamic::from) + }; + let args = [ + TypeId::of::(), + TypeId::of::(), + TypeId::of::(), + TypeId::of::(), + ]; + self.set_fn( + name, + Public, + &args, + CallableFunction::from_pure(Box::new(f)), + ) + } + + /// Set a Rust function taking three parameters (the first one mutable) into the module, + /// returning a hash key. + /// + /// If there is a similar existing Rust function, it is replaced. + /// + /// # Examples + /// + /// ``` + /// use rhai::Module; + /// + /// let mut module = Module::new(); + /// let hash = module.set_fn_3_mut("calc", |x: &mut i64, y: String, z: i64, _w: ()| { + /// *x += y.len() as i64 + z; Ok(*x) + /// }); + /// assert!(module.get_fn(hash).is_some()); + /// ``` + pub fn set_fn_4_mut< + A: Variant + Clone, + B: Variant + Clone, + C: Variant + Clone, + D: Variant + Clone, + T: Variant + Clone, + >( + &mut self, + name: impl Into, + #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, C, D) -> FuncReturn + 'static, + #[cfg(feature = "sync")] func: impl Fn(&mut A, B, C, D) -> FuncReturn + Send + Sync + 'static, + ) -> u64 { + let f = move |args: &mut FnCallArgs| { + let b = mem::take(args[1]).cast::(); + let c = mem::take(args[2]).cast::(); + let d = mem::take(args[3]).cast::(); + let a = args[0].downcast_mut::().unwrap(); + + func(a, b, c, d).map(Dynamic::from) + }; + let args = [ + TypeId::of::(), + TypeId::of::(), + TypeId::of::(), + TypeId::of::(), + ]; + self.set_fn( + name, + Public, + &args, + CallableFunction::from_method(Box::new(f)), + ) + } + /// Get a Rust function. /// /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. @@ -554,8 +653,8 @@ impl Module { /// Get a modules-qualified function. /// - /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. - /// It is also returned by the `set_fn_XXX` calls. + /// The `u64` hash is calculated by the function `crate::calc_fn_hash` and must match + /// the hash calculated by `index_all_sub_modules`. pub(crate) fn get_qualified_fn( &mut self, name: &str, From e84d4a88e9947975643a0098a7385e4f6f1969ad Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 28 May 2020 14:08:07 +0800 Subject: [PATCH 34/53] Do not check function call depth if no_function. --- src/engine.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index a96bae7b..80b84110 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -608,10 +608,13 @@ impl Engine { let native_only = hashes.1 == 0; // Check for stack overflow - if level > self.max_call_stack_depth { - return Err(Box::new(EvalAltResult::ErrorStackOverflow(pos))); + #[cfg(not(feature = "no_function"))] + #[cfg(not(feature = "unchecked"))] + { + if level > self.max_call_stack_depth { + return Err(Box::new(EvalAltResult::ErrorStackOverflow(pos))); + } } - // First search in script-defined functions (can override built-in) if !native_only { if let Some(fn_def) = lib.get(&hashes.1) { From d7c69c4f5134e3e48b1eab2894daff037f781d02 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 28 May 2020 14:08:21 +0800 Subject: [PATCH 35/53] More tests. --- tests/float.rs | 15 ++++++++++++++- tests/modules.rs | 5 ++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/float.rs b/tests/float.rs index 8d70c7b3..211828d0 100644 --- a/tests/float.rs +++ b/tests/float.rs @@ -22,7 +22,7 @@ fn test_float() -> Result<(), Box> { #[test] #[cfg(not(feature = "no_object"))] -fn struct_with_float() -> Result<(), Box> { +fn test_struct_with_float() -> Result<(), Box> { #[derive(Clone)] struct TestStruct { x: f64, @@ -64,3 +64,16 @@ fn struct_with_float() -> Result<(), Box> { Ok(()) } + +#[test] +fn test_float_func() -> Result<(), Box> { + let mut engine = Engine::new(); + + engine.register_fn("sum", |x: FLOAT, y: FLOAT, z: FLOAT, w: FLOAT| { + x + y + z + w + }); + + assert_eq!(engine.eval::("sum(1.0, 2.0, 3.0, 4.0)")?, 10.0); + + Ok(()) +} diff --git a/tests/modules.rs b/tests/modules.rs index 6e49a43c..61273f53 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -63,6 +63,9 @@ fn test_module_resolver() -> Result<(), Box> { let mut module = Module::new(); module.set_var("answer", 42 as INT); + module.set_fn_4("sum".to_string(), |x: INT, y: INT, z: INT, w: INT| { + Ok(x + y + z + w) + }); resolver.insert("hello".to_string(), module); @@ -74,7 +77,7 @@ fn test_module_resolver() -> Result<(), Box> { r#" import "hello" as h1; import "hello" as h2; - h2::answer + h1::sum(h2::answer, -10, 3, 7) "# )?, 42 From acd46851450c3949b203324ea99eee57331f7fd6 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 28 May 2020 23:57:09 +0800 Subject: [PATCH 36/53] Convert function calls to method calls to use &mut first argument. --- README.md | 57 ++++++++++++++++++++-------- src/any.rs | 8 ++++ src/engine.rs | 75 +++++++++++++++++++++++++++---------- src/module.rs | 3 ++ src/packages/string_more.rs | 28 +++++++------- 5 files changed, 122 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index ad4a5037..89da8bad 100644 --- a/README.md +++ b/README.md @@ -1122,6 +1122,7 @@ Comments -------- Comments are C-style, including '`/*` ... `*/`' pairs and '`//`' for comments to the end of the line. +Comments can be nested. ```rust let /* intruder comment */ name = "Bob"; @@ -1138,14 +1139,35 @@ let /* intruder comment */ name = "Bob"; */ ``` +Keywords +-------- + +The following are reserved keywords in Rhai: + +| Keywords | Usage | Not available under feature | +| ------------------------------------------------- | --------------------- | :-------------------------: | +| `true`, `false` | Boolean constants | | +| `let`, `const` | Variable declarations | | +| `if`, `else` | Control flow | | +| `while`, `loop`, `for`, `in`, `continue`, `break` | Looping | | +| `fn`, `private` | Functions | [`no_function`] | +| `return` | Return values | | +| `throw` | Return errors | | +| `import`, `export`, `as` | Modules | [`no_module`] | + +Keywords cannot be the name of a [function] or [variable], unless the relevant exclusive feature is enabled. +For example, `fn` is a valid variable name if the [`no_function`] feature is used. + Statements ---------- -Statements are terminated by semicolons '`;`' - they are mandatory, except for the _last_ statement where it can be omitted. +Statements are terminated by semicolons '`;`' and they are mandatory, +except for the _last_ statement in a _block_ (enclosed by '`{`' .. '`}`' pairs) where it can be omitted. -A statement can be used anywhere where an expression is expected. The _last_ statement of a statement block -(enclosed by '`{`' .. '`}`' pairs) is always the return value of the statement. If a statement has no return value -(e.g. variable definitions, assignments) then the value will be [`()`]. +A statement can be used anywhere where an expression is expected. These are called, for lack of a more +creative name, "statement expressions." The _last_ statement of a statement block is _always_ the block's +return value when used as a statement. +If the last statement has no return value (e.g. variable definitions, assignments) then it is assumed to be [`()`]. ```rust let a = 42; // normal assignment statement @@ -1153,16 +1175,17 @@ let a = foo(42); // normal function call statement foo < 42; // normal expression as statement let a = { 40 + 2 }; // 'a' is set to the value of the statement block, which is the value of the last statement -// ^ notice that the last statement does not require a terminating semicolon (although it also works with it) -// ^ notice that a semicolon is required here to terminate the assignment statement; it is syntax error without it +// ^ the last statement does not require a terminating semicolon (although it also works with it) +// ^ semicolon required here to terminate the assignment statement; it is a syntax error without it -4 * 10 + 2 // this is also a statement, which is an expression, with no ending semicolon because - // it is the last statement of the whole block +4 * 10 + 2 // a statement which is just one expression; no ending semicolon is OK + // because it is the last statement of the whole block ``` Variables --------- +[variable]: #variables [variables]: #variables Variables in Rhai follow normal C naming rules (i.e. must contain only ASCII letters, digits and underscores '`_`'). @@ -1307,8 +1330,8 @@ Strings and Chars [strings]: #strings-and-chars [char]: #strings-and-chars -String and char literals follow C-style formatting, with support for Unicode ('`\u`_xxxx_' or '`\U`_xxxxxxxx_') and -hex ('`\x`_xx_') escape sequences. +String and character literals follow C-style formatting, with support for Unicode ('`\u`_xxxx_' or '`\U`_xxxxxxxx_') +and hex ('`\x`_xx_') escape sequences. Hex sequences map to ASCII characters, while '`\u`' maps to 16-bit common Unicode code points and '`\U`' maps the full, 32-bit extended Unicode code points. @@ -1388,7 +1411,7 @@ record == "Bob X. Davis: age 42 ❤\n"; ### Built-in functions -The following standard methods (defined in the [`MoreStringPackage`](#packages) but excluded if using a [raw `Engine`]) operate on strings: +The following standard methods (mostly defined in the [`MoreStringPackage`](#packages) but excluded if using a [raw `Engine`]) operate on strings: | Function | Parameter(s) | Description | | ----------------------- | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | @@ -1463,7 +1486,7 @@ Arrays are disabled via the [`no_index`] feature. ### Built-in functions -The following methods (defined in the [`BasicArrayPackage`](#packages) but excluded if using a [raw `Engine`]) operate on arrays: +The following methods (mostly defined in the [`BasicArrayPackage`](#packages) but excluded if using a [raw `Engine`]) operate on arrays: | Function | Parameter(s) | Description | | ----------------------- | --------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | @@ -1963,6 +1986,9 @@ println!(result); // prints "Runtime error: 42 is too large! (line 5, Functions --------- +[function]: #functions +[functions]: #functions + Rhai supports defining functions in script (unless disabled with [`no_function`]): ```rust @@ -2007,8 +2033,9 @@ fn foo() { x } // <- syntax error: variable 'x' doesn't exist Functions defined in script always take [`Dynamic`] parameters (i.e. the parameter can be of any type). It is important to remember that all arguments are passed by _value_, so all functions are _pure_ -(i.e. they never modifytheir arguments). -Any update to an argument will **not** be reflected back to the caller. This can introduce subtle bugs, if not careful. +(i.e. they never modify their arguments). +Any update to an argument will **not** be reflected back to the caller. +This can introduce subtle bugs, if not careful, especially when using the _method-call_ style. ```rust fn change(s) { // 's' is passed by value @@ -2016,7 +2043,7 @@ fn change(s) { // 's' is passed by value } let x = 500; -x.change(); // de-sugars to change(x) +x.change(); // de-sugars to 'change(x)' x == 500; // 'x' is NOT changed! ``` diff --git a/src/any.rs b/src/any.rs index 3bcbb049..f685c303 100644 --- a/src/any.rs +++ b/src/any.rs @@ -630,6 +630,14 @@ impl From> for Dynamic { ))) } } +#[cfg(not(feature = "no_index"))] +impl From<&[T]> for Dynamic { + fn from(value: &[T]) -> Self { + Self(Union::Array(Box::new( + value.iter().cloned().map(Dynamic::from).collect(), + ))) + } +} #[cfg(not(feature = "no_object"))] impl From> for Dynamic { fn from(value: HashMap) -> Self { diff --git a/src/engine.rs b/src/engine.rs index 80b84110..b843c85d 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1550,14 +1550,8 @@ impl Engine { let ((name, native, pos), _, hash, args_expr, def_val) = x.as_ref(); let def_val = def_val.as_ref(); - let mut arg_values = args_expr - .iter() - .map(|expr| self.eval_expr(scope, state, lib, expr, level)) - .collect::, _>>()?; - - let mut args: StaticVec<_> = arg_values.iter_mut().collect(); - - if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::() { + // Handle eval + if name == KEYWORD_EVAL && args_expr.len() == 1 { let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::())); @@ -1567,7 +1561,8 @@ impl Engine { let pos = args_expr.get(0).position(); // Evaluate the text string as a script - let result = self.eval_script_expr(scope, state, lib, args.pop(), pos); + let script = self.eval_expr(scope, state, lib, args_expr.get(0), level)?; + let result = self.eval_script_expr(scope, state, lib, &script, pos); if scope.len() != prev_len { // IMPORTANT! If the eval defines new variables in the current scope, @@ -1580,6 +1575,46 @@ impl Engine { } // Normal function call - except for eval (handled above) + let mut arg_values: StaticVec; + let mut args: StaticVec<_>; + + if args_expr.is_empty() { + // No arguments + args = Default::default(); + } else { + // See if the first argument is a variable, if so, convert to method-call style + // in order to leverage potential &mut first argument and avoid cloning the value + match args_expr.get(0) { + // func(x, ...) -> x.func(...) + lhs @ Expr::Variable(_) => { + arg_values = args_expr + .iter() + .skip(1) + .map(|expr| self.eval_expr(scope, state, lib, expr, level)) + .collect::>()?; + + let (target, _, typ, pos) = search_scope(scope, state, lhs)?; + self.inc_operations(state, pos)?; + + match typ { + ScopeEntryType::Module => unreachable!(), + ScopeEntryType::Constant | ScopeEntryType::Normal => (), + } + + args = once(target).chain(arg_values.iter_mut()).collect(); + } + // func(..., ...) + _ => { + arg_values = args_expr + .iter() + .map(|expr| self.eval_expr(scope, state, lib, expr, level)) + .collect::>()?; + + args = arg_values.iter_mut().collect(); + } + } + } + let args = args.as_mut(); self.exec_fn_call( state, lib, name, *native, *hash, args, false, def_val, *pos, level, @@ -2012,8 +2047,8 @@ fn run_builtin_binary_op( } if args_type == TypeId::of::() { - let x = x.downcast_ref::().unwrap().clone(); - let y = y.downcast_ref::().unwrap().clone(); + let x = *x.downcast_ref::().unwrap(); + let y = *y.downcast_ref::().unwrap(); #[cfg(not(feature = "unchecked"))] match op { @@ -2054,8 +2089,8 @@ fn run_builtin_binary_op( _ => (), } } else if args_type == TypeId::of::() { - let x = x.downcast_ref::().unwrap().clone(); - let y = y.downcast_ref::().unwrap().clone(); + let x = *x.downcast_ref::().unwrap(); + let y = *y.downcast_ref::().unwrap(); match op { "&" => return Ok(Some((x && y).into())), @@ -2079,8 +2114,8 @@ fn run_builtin_binary_op( _ => (), } } else if args_type == TypeId::of::() { - let x = x.downcast_ref::().unwrap().clone(); - let y = y.downcast_ref::().unwrap().clone(); + let x = *x.downcast_ref::().unwrap(); + let y = *y.downcast_ref::().unwrap(); match op { "==" => return Ok(Some((x == y).into())), @@ -2102,8 +2137,8 @@ fn run_builtin_binary_op( #[cfg(not(feature = "no_float"))] { if args_type == TypeId::of::() { - let x = x.downcast_ref::().unwrap().clone(); - let y = y.downcast_ref::().unwrap().clone(); + let x = *x.downcast_ref::().unwrap(); + let y = *y.downcast_ref::().unwrap(); match op { "+" => return Ok(Some((x + y).into())), @@ -2142,7 +2177,7 @@ fn run_builtin_op_assignment( if args_type == TypeId::of::() { let x = x.downcast_mut::().unwrap(); - let y = y.downcast_ref::().unwrap().clone(); + let y = *y.downcast_ref::().unwrap(); #[cfg(not(feature = "unchecked"))] match op { @@ -2178,7 +2213,7 @@ fn run_builtin_op_assignment( } } else if args_type == TypeId::of::() { let x = x.downcast_mut::().unwrap(); - let y = y.downcast_ref::().unwrap().clone(); + let y = *y.downcast_ref::().unwrap(); match op { "&=" => return Ok(Some(*x = *x && y)), @@ -2199,7 +2234,7 @@ fn run_builtin_op_assignment( { if args_type == TypeId::of::() { let x = x.downcast_mut::().unwrap(); - let y = y.downcast_ref::().unwrap().clone(); + let y = *y.downcast_ref::().unwrap(); match op { "+=" => return Ok(Some(*x += y)), diff --git a/src/module.rs b/src/module.rs index 457f1b1c..57bca2e3 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1044,6 +1044,9 @@ mod stat { /// Module resolution service that serves modules added into it. /// + /// `StaticModuleResolver` is a smart pointer to a `HashMap`. + /// It can simply be treated as `&HashMap`. + /// /// # Examples /// /// ``` diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index c3c22c31..6f3cbc40 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -101,22 +101,22 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str #[cfg(not(feature = "no_index"))] { - lib.set_fn_2("+", |x: ImmutableString, y: Array| Ok(format!("{}{:?}", x, y))); + lib.set_fn_2_mut("+", |x: &mut ImmutableString, y: Array| Ok(format!("{}{:?}", x, y))); lib.set_fn_2_mut("+", |x: &mut Array, y: ImmutableString| Ok(format!("{:?}{}", x, y))); } - lib.set_fn_1("len", |s: ImmutableString| Ok(s.chars().count() as INT)); - lib.set_fn_2( + lib.set_fn_1_mut("len", |s: &mut ImmutableString| Ok(s.chars().count() as INT)); + lib.set_fn_2_mut( "contains", - |s: ImmutableString, ch: char| Ok(s.contains(ch)), + |s: &mut ImmutableString, ch: char| Ok(s.contains(ch)), ); - lib.set_fn_2( + lib.set_fn_2_mut( "contains", - |s: ImmutableString, find: ImmutableString| Ok(s.contains(find.as_str())), + |s: &mut ImmutableString, find: ImmutableString| Ok(s.contains(find.as_str())), ); - lib.set_fn_3( + lib.set_fn_3_mut( "index_of", - |s: ImmutableString, ch: char, start: INT| { + |s: &mut ImmutableString, ch: char, start: INT| { let start = if start < 0 { 0 } else if (start as usize) >= s.chars().count() { @@ -131,17 +131,17 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str .unwrap_or(-1 as INT)) }, ); - lib.set_fn_2( + lib.set_fn_2_mut( "index_of", - |s: ImmutableString, ch: char| { + |s: &mut ImmutableString, ch: char| { Ok(s.find(ch) .map(|index| s[0..index].chars().count() as INT) .unwrap_or(-1 as INT)) }, ); - lib.set_fn_3( + lib.set_fn_3_mut( "index_of", - |s: ImmutableString, find: ImmutableString, start: INT| { + |s: &mut ImmutableString, find: ImmutableString, start: INT| { let start = if start < 0 { 0 } else if (start as usize) >= s.chars().count() { @@ -156,9 +156,9 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str .unwrap_or(-1 as INT)) }, ); - lib.set_fn_2( + lib.set_fn_2_mut( "index_of", - |s: ImmutableString, find: ImmutableString| { + |s: &mut ImmutableString, find: ImmutableString| { Ok(s.find(find.as_str()) .map(|index| s[0..index].chars().count() as INT) .unwrap_or(-1 as INT)) From 9616452c00401f3f33e73b8cacea3a0c4eb58fa6 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 29 May 2020 00:53:30 +0800 Subject: [PATCH 37/53] Fix bug in calling script function in method style. --- src/engine.rs | 28 +++++++++++++++++++++++++++- tests/functions.rs | 17 +++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/engine.rs b/src/engine.rs index b843c85d..8903e4a3 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -618,8 +618,31 @@ impl Engine { // First search in script-defined functions (can override built-in) if !native_only { if let Some(fn_def) = lib.get(&hashes.1) { + let mut this_copy: Option; + let mut this_pointer: Option<&mut Dynamic> = None; + + if is_ref && !args.is_empty() { + // Clone the original value. It'll be consumed because the function + // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) + this_copy = Some(args[0].clone()); + + // Replace the first reference with a reference to the clone, force-casting the lifetime. + // Keep the original reference. Must remember to restore it before existing this function. + this_pointer = Some(mem::replace( + args.get_mut(0).unwrap(), + unsafe_mut_cast_to_lifetime(this_copy.as_mut().unwrap()), + )); + } + + // Run scripted function let result = self.call_script_fn(scope, state, lib, fn_name, fn_def, args, pos, level)?; + + // Restore the original reference + if let Some(this_pointer) = this_pointer { + mem::replace(args.get_mut(0).unwrap(), this_pointer); + } + return Ok((result, false)); } } @@ -1577,6 +1600,7 @@ impl Engine { // Normal function call - except for eval (handled above) let mut arg_values: StaticVec; let mut args: StaticVec<_>; + let mut is_ref = false; if args_expr.is_empty() { // No arguments @@ -1602,6 +1626,8 @@ impl Engine { } args = once(target).chain(arg_values.iter_mut()).collect(); + + is_ref = true; } // func(..., ...) _ => { @@ -1617,7 +1643,7 @@ impl Engine { let args = args.as_mut(); self.exec_fn_call( - state, lib, name, *native, *hash, args, false, def_val, *pos, level, + state, lib, name, *native, *hash, args, is_ref, def_val, *pos, level, ) .map(|(v, _)| v) } diff --git a/tests/functions.rs b/tests/functions.rs index 26d9b387..ae12e052 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -7,12 +7,23 @@ fn test_functions() -> Result<(), Box> { assert_eq!(engine.eval::("fn add(x, n) { x + n } add(40, 2)")?, 42); + assert_eq!( + engine.eval::("fn add(x, n) { x + n } let a = 40; add(a, 2); a")?, + 40 + ); + #[cfg(not(feature = "no_object"))] assert_eq!( engine.eval::("fn add(x, n) { x + n } let x = 40; x.add(2)")?, 42 ); + #[cfg(not(feature = "no_object"))] + assert_eq!( + engine.eval::("fn add(x, n) { x += n; } let x = 40; x.add(2); x")?, + 40 + ); + assert_eq!(engine.eval::("fn mul2(x) { x * 2 } mul2(21)")?, 42); #[cfg(not(feature = "no_object"))] @@ -21,5 +32,11 @@ fn test_functions() -> Result<(), Box> { 42 ); + #[cfg(not(feature = "no_object"))] + assert_eq!( + engine.eval::("fn mul2(x) { x *= 2; } let x = 21; x.mul2(); x")?, + 21 + ); + Ok(()) } From e1242df5c8bfea4dd0142c844218dccf890ab17a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 29 May 2020 18:15:58 +0800 Subject: [PATCH 38/53] Extract copy/restore of first argument in method call. --- README.md | 20 ++++++++--- RELEASES.md | 10 +++++- src/engine.rs | 82 +++++++++++++++++++++++++------------------- src/module.rs | 6 ++-- tests/method_call.rs | 2 +- 5 files changed, 74 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 89da8bad..89728438 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ It doesn't attempt to be a new language. For example: * No classes. Well, Rust doesn't either. On the other hand... * No traits... so it is also not Rust. Do your Rusty stuff in Rust. -* No structures - definte your types in Rust instead; Rhai can seamless work with _any Rust type_. +* No structures - define your types in Rust instead; Rhai can seamlessly work with _any Rust type_. * No first-class functions - Code your functions in Rust instead, and register them with Rhai. * No closures - do your closure magic in Rust instead; [turn a Rhai scripted function into a Rust closure](#calling-rhai-functions-from-rust). * It is best to expose an API in Rhai for scripts to call. All your core functionalities should be in Rust. @@ -2094,14 +2094,24 @@ Members and methods ------------------- Properties and methods in a Rust custom type registered with the [`Engine`] can be called just like in Rust. +Unlike functions defined in script (for which all arguments are passed by _value_), +native Rust functions may mutate the object (or the first argument if called in normal function call style). ```rust let a = new_ts(); // constructor function -a.field = 500; // property access -a.update(); // method call, 'a' can be changed +a.field = 500; // property setter +a.update(); // method call, 'a' can be modified -update(a); // this works, but 'a' is unchanged because only - // a COPY of 'a' is passed to 'update' by VALUE +update(a); // <- this de-sugars to 'a.update()' this if 'a' is a simple variable + // unlike scripted functions, 'a' can be modified and is not a copy + +let array = [ a ]; + +update(array[0]); // <- 'array[0]' is an expression returning a calculated value, + // a transient (i.e. a copy) so this statement has no effect + // except waste a lot of time cloning + +array[0].update(); // <- call this method-call style will update 'a' ``` Custom types, properties and methods can be disabled via the [`no_object`] feature. diff --git a/RELEASES.md b/RELEASES.md index bc5a2125..79e0af4c 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -22,13 +22,16 @@ Breaking changes This is to avoid excessive cloning of strings. All native-Rust functions taking string parameters should switch to `rhai::ImmutableString` (which is either `Rc` or `Arc` depending on whether the `sync` feature is used). +* Native Rust functions registered with the `Engine` also mutates the first argument when called in + normal function-call style (previously the first argument will be passed by _value_ if not called + in method-call style). Of course, if the first argument is a calculated value (e.g. result of an + expression), then mutating it has no effect, but at least it is not cloned. New features ------------ * Set limit on maximum level of nesting expressions and statements to avoid panics during parsing. * New `EvalPackage` to disable `eval`. -* More benchmarks. Speed enhancements ------------------ @@ -43,6 +46,11 @@ Speed enhancements (and therefore the `CorePackage`) because they are now always available, even under `Engine::new_raw`. * Operator-assignment statements (e.g. `+=`) are now handled directly and much faster. * Strings are now _immutable_ and use the `rhai::ImmutableString` type, eliminating large amounts of cloning. +* For Native Rust functions taking a first `&mut` parameter, the first argument is passed by reference instead of + by value, even if not called in method-call style. This allows many functions declared with `&mut` parameter to avoid + excessive cloning. For example, if `a` is a large array, getting its length in this manner: `len(a)` used to result + in a full clone of `a` before taking the length and throwing the copy away. Now, `a` is simply passed by reference, + avoiding the cloning altogether. Version 0.14.1 diff --git a/src/engine.rs b/src/engine.rs index 8903e4a3..433cd77e 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -615,33 +615,54 @@ impl Engine { return Err(Box::new(EvalAltResult::ErrorStackOverflow(pos))); } } + + let mut this_copy: Dynamic = Default::default(); + let mut old_this_ptr: Option<&mut Dynamic> = None; + + /// This function replaces the first argument of a method call with a clone copy. + /// This is to prevent a pure function unintentionally consuming the first argument. + fn normalize_first_arg<'a>( + normalize: bool, + this_copy: &mut Dynamic, + old_this_ptr: &mut Option<&'a mut Dynamic>, + args: &mut FnCallArgs<'a>, + ) { + // Only do it for method calls with arguments. + if !normalize || args.is_empty() { + return; + } + + // Clone the original value. + *this_copy = args[0].clone(); + + // Replace the first reference with a reference to the clone, force-casting the lifetime. + // Keep the original reference. Must remember to restore it later with `restore_first_arg_of_method_call`. + let this_pointer = mem::replace( + args.get_mut(0).unwrap(), + unsafe_mut_cast_to_lifetime(this_copy), + ); + + *old_this_ptr = Some(this_pointer); + } + + /// This function restores the first argument that was replaced by `normalize_first_arg_of_method_call`. + fn restore_first_arg<'a>(old_this_ptr: Option<&'a mut Dynamic>, args: &mut FnCallArgs<'a>) { + if let Some(this_pointer) = old_this_ptr { + mem::replace(args.get_mut(0).unwrap(), this_pointer); + } + } + // First search in script-defined functions (can override built-in) if !native_only { if let Some(fn_def) = lib.get(&hashes.1) { - let mut this_copy: Option; - let mut this_pointer: Option<&mut Dynamic> = None; - - if is_ref && !args.is_empty() { - // Clone the original value. It'll be consumed because the function - // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) - this_copy = Some(args[0].clone()); - - // Replace the first reference with a reference to the clone, force-casting the lifetime. - // Keep the original reference. Must remember to restore it before existing this function. - this_pointer = Some(mem::replace( - args.get_mut(0).unwrap(), - unsafe_mut_cast_to_lifetime(this_copy.as_mut().unwrap()), - )); - } + normalize_first_arg(is_ref, &mut this_copy, &mut old_this_ptr, args); // Run scripted function let result = self.call_script_fn(scope, state, lib, fn_name, fn_def, args, pos, level)?; // Restore the original reference - if let Some(this_pointer) = this_pointer { - mem::replace(args.get_mut(0).unwrap(), this_pointer); - } + restore_first_arg(old_this_ptr, args); return Ok((result, false)); } @@ -654,29 +675,18 @@ impl Engine { .or_else(|| self.packages.get_fn(hashes.0)) { // Calling pure function in method-call? - let mut this_copy: Option; - let mut this_pointer: Option<&mut Dynamic> = None; - - if func.is_pure() && is_ref && !args.is_empty() { - // Clone the original value. It'll be consumed because the function - // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) - this_copy = Some(args[0].clone()); - - // Replace the first reference with a reference to the clone, force-casting the lifetime. - // Keep the original reference. Must remember to restore it before existing this function. - this_pointer = Some(mem::replace( - args.get_mut(0).unwrap(), - unsafe_mut_cast_to_lifetime(this_copy.as_mut().unwrap()), - )); - } + normalize_first_arg( + func.is_pure() && is_ref, + &mut this_copy, + &mut old_this_ptr, + args, + ); // Run external function let result = func.get_native_fn()(args); // Restore the original reference - if let Some(this_pointer) = this_pointer { - mem::replace(args.get_mut(0).unwrap(), this_pointer); - } + restore_first_arg(old_this_ptr, args); let result = result.map_err(|err| err.new_position(pos))?; diff --git a/src/module.rs b/src/module.rs index 57bca2e3..5c5d3d67 100644 --- a/src/module.rs +++ b/src/module.rs @@ -544,7 +544,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_3("calc", |x: i64, y: String, z: i64, _w: ()| { + /// let hash = module.set_fn_4("calc", |x: i64, y: String, z: i64, _w: ()| { /// Ok(x + y.len() as i64 + z) /// }); /// assert!(module.get_fn(hash).is_some()); @@ -583,7 +583,7 @@ impl Module { ) } - /// Set a Rust function taking three parameters (the first one mutable) into the module, + /// Set a Rust function taking four parameters (the first one mutable) into the module, /// returning a hash key. /// /// If there is a similar existing Rust function, it is replaced. @@ -594,7 +594,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_3_mut("calc", |x: &mut i64, y: String, z: i64, _w: ()| { + /// let hash = module.set_fn_4_mut("calc", |x: &mut i64, y: String, z: i64, _w: ()| { /// *x += y.len() as i64 + z; Ok(*x) /// }); /// assert!(module.get_fn(hash).is_some()); diff --git a/tests/method_call.rs b/tests/method_call.rs index 850cf247..02c7a950 100644 --- a/tests/method_call.rs +++ b/tests/method_call.rs @@ -33,7 +33,7 @@ fn test_method_call() -> Result<(), Box> { assert_eq!( engine.eval::("let x = new_ts(); update(x, 1000); x")?, - TestStruct { x: 1 } + TestStruct { x: 1001 } ); Ok(()) From 2bcc51cc457b7f321ee25a4cfdcd892e9b96af9d Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 30 May 2020 10:27:48 +0800 Subject: [PATCH 39/53] Fix bug in index expressions. --- src/api.rs | 3 +- src/engine.rs | 151 ++++++++++++++++++------------------------------ src/optimize.rs | 8 ++- src/parser.rs | 36 +++++++++++- tests/string.rs | 7 +-- 5 files changed, 98 insertions(+), 107 deletions(-) diff --git a/src/api.rs b/src/api.rs index 89e045c1..8a30a480 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1051,8 +1051,7 @@ impl Engine { let mut state = State::new(); let args = args.as_mut(); - let result = - self.call_script_fn(Some(scope), &mut state, &lib, name, fn_def, args, pos, 0)?; + let result = self.call_script_fn(scope, &mut state, &lib, name, fn_def, args, pos, 0)?; let return_type = self.map_type_name(result.type_name()); diff --git a/src/engine.rs b/src/engine.rs index 433cd77e..2c761c7e 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -432,11 +432,11 @@ fn default_print(s: &str) { } /// Search for a variable within the scope -fn search_scope<'a>( - scope: &'a mut Scope, +fn search_scope<'s, 'a>( + scope: &'s mut Scope, state: &mut State, expr: &'a Expr, -) -> Result<(&'a mut Dynamic, &'a str, ScopeEntryType, Position), Box> { +) -> Result<(&'s mut Dynamic, &'a str, ScopeEntryType, Position), Box> { let ((name, pos), modules, hash_var, index) = match expr { Expr::Variable(x) => x.as_ref(), _ => unreachable!(), @@ -592,7 +592,7 @@ impl Engine { /// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`! pub(crate) fn call_fn_raw( &self, - scope: Option<&mut Scope>, + scope: &mut Scope, state: &mut State, lib: &FunctionsLib, fn_name: &str, @@ -774,7 +774,7 @@ impl Engine { /// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`! pub(crate) fn call_script_fn( &self, - scope: Option<&mut Scope>, + scope: &mut Scope, state: &mut State, lib: &FunctionsLib, fn_name: &str, @@ -786,88 +786,42 @@ impl Engine { let orig_scope_level = state.scope_level; state.scope_level += 1; - match scope { - // Extern scope passed in which is not empty - Some(scope) if !scope.is_empty() => { - let scope_len = scope.len(); + let scope_len = scope.len(); - // Put arguments into scope as variables - // Actually consume the arguments instead of cloning them - scope.extend( - fn_def - .params - .iter() - .zip(args.iter_mut().map(|v| mem::take(*v))) - .map(|(name, value)| { - let var_name = unsafe_cast_var_name_to_lifetime(name.as_str(), state); - (var_name, ScopeEntryType::Normal, value) - }), - ); + // Put arguments into scope as variables + // Actually consume the arguments instead of cloning them + scope.extend( + fn_def + .params + .iter() + .zip(args.iter_mut().map(|v| mem::take(*v))) + .map(|(name, value)| { + let var_name = unsafe_cast_var_name_to_lifetime(name.as_str(), state); + (var_name, ScopeEntryType::Normal, value) + }), + ); - // Evaluate the function at one higher level of call depth - let result = self - .eval_stmt(scope, state, lib, &fn_def.body, level + 1) - .or_else(|err| match *err { - // Convert return statement to return value - EvalAltResult::Return(x, _) => Ok(x), - EvalAltResult::ErrorInFunctionCall(name, err, _) => { - Err(Box::new(EvalAltResult::ErrorInFunctionCall( - format!("{} > {}", fn_name, name), - err, - pos, - ))) - } - _ => Err(Box::new(EvalAltResult::ErrorInFunctionCall( - fn_name.to_string(), - err, - pos, - ))), - }); + // Evaluate the function at one higher level of call depth + let result = self + .eval_stmt(scope, state, lib, &fn_def.body, level + 1) + .or_else(|err| match *err { + // Convert return statement to return value + EvalAltResult::Return(x, _) => Ok(x), + EvalAltResult::ErrorInFunctionCall(name, err, _) => Err(Box::new( + EvalAltResult::ErrorInFunctionCall(format!("{} > {}", fn_name, name), err, pos), + )), + _ => Err(Box::new(EvalAltResult::ErrorInFunctionCall( + fn_name.to_string(), + err, + pos, + ))), + }); - // Remove all local variables - scope.rewind(scope_len); - state.scope_level = orig_scope_level; + // Remove all local variables + scope.rewind(scope_len); + state.scope_level = orig_scope_level; - return result; - } - // No new scope - create internal scope - _ => { - let mut scope = Scope::new(); - - // Put arguments into scope as variables - // Actually consume the arguments instead of cloning them - scope.extend( - fn_def - .params - .iter() - .zip(args.iter_mut().map(|v| mem::take(*v))) - .map(|(name, value)| (name, ScopeEntryType::Normal, value)), - ); - - // Evaluate the function at one higher level of call depth - let result = self - .eval_stmt(&mut scope, state, lib, &fn_def.body, level + 1) - .or_else(|err| match *err { - // Convert return statement to return value - EvalAltResult::Return(x, _) => Ok(x), - EvalAltResult::ErrorInFunctionCall(name, err, _) => { - Err(Box::new(EvalAltResult::ErrorInFunctionCall( - format!("{} > {}", fn_name, name), - err, - pos, - ))) - } - _ => Err(Box::new(EvalAltResult::ErrorInFunctionCall( - fn_name.to_string(), - err, - pos, - ))), - }); - - state.scope_level = orig_scope_level; - return result; - } - } + result } // Has a system function an override? @@ -925,9 +879,12 @@ impl Engine { } // Normal function call - _ => self.call_fn_raw( - None, state, lib, fn_name, hashes, args, is_ref, def_val, pos, level, - ), + _ => { + let mut scope = Scope::new(); + self.call_fn_raw( + &mut scope, state, lib, fn_name, hashes, args, is_ref, def_val, pos, level, + ) + } } } @@ -1252,14 +1209,16 @@ impl Engine { Expr::FnCall(_) => unreachable!(), Expr::Property(_) => idx_values.push(()), // Store a placeholder - no need to copy the property name Expr::Index(x) | Expr::Dot(x) => { + let (lhs, rhs, _) = x.as_ref(); + // Evaluate in left-to-right order - let lhs_val = match x.0 { + let lhs_val = match lhs { Expr::Property(_) => Default::default(), // Store a placeholder in case of a property - _ => self.eval_expr(scope, state, lib, &x.0, level)?, + _ => self.eval_expr(scope, state, lib, lhs, level)?, }; // Push in reverse order - self.eval_indexed_chain(scope, state, lib, &x.1, idx_values, size, level)?; + self.eval_indexed_chain(scope, state, lib, rhs, idx_values, size, level)?; idx_values.push(lhs_val); } @@ -1380,6 +1339,7 @@ impl Engine { Dynamic(Union::Array(mut rhs_value)) => { let op = "=="; let def_value = false.into(); + let mut scope = Scope::new(); // Call the `==` operator to compare each value for value in rhs_value.iter_mut() { @@ -1394,7 +1354,7 @@ impl Engine { ); let (r, _) = self.call_fn_raw( - None, state, lib, op, hashes, args, false, def_value, pos, level, + &mut scope, state, lib, op, hashes, args, false, def_value, pos, level, )?; if r.as_bool().unwrap_or(false) { return Ok(true.into()); @@ -1432,6 +1392,8 @@ impl Engine { self.inc_operations(state, expr.position())?; match expr { + Expr::Expr(x) => self.eval_expr(scope, state, lib, x.as_ref(), level), + Expr::IntegerConstant(x) => Ok(x.0.into()), #[cfg(not(feature = "no_float"))] Expr::FloatConstant(x) => Ok(x.0.into()), @@ -1710,9 +1672,8 @@ impl Engine { Ok(x) if x.is_script() => { let args = args.as_mut(); let fn_def = x.get_fn_def(); - let result = - self.call_script_fn(None, state, lib, name, fn_def, args, *pos, level)?; - Ok(result) + let mut scope = Scope::new(); + self.call_script_fn(&mut scope, state, lib, name, fn_def, args, *pos, level) } Ok(x) => x.get_native_fn()(args.as_mut()).map_err(|err| err.new_position(*pos)), Err(err) @@ -1772,9 +1733,9 @@ impl Engine { } /// Evaluate a statement - pub(crate) fn eval_stmt<'s>( + pub(crate) fn eval_stmt( &self, - scope: &mut Scope<'s>, + scope: &mut Scope, state: &mut State, lib: &FunctionsLib, stmt: &Stmt, diff --git a/src/optimize.rs b/src/optimize.rs index dedd1135..f8b0d1bb 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -122,7 +122,7 @@ fn call_fn_with_constant_arguments( state .engine .call_fn_raw( - None, + &mut Scope::new(), &mut Default::default(), state.lib, fn_name, @@ -138,7 +138,7 @@ fn call_fn_with_constant_arguments( } /// Optimize a statement. -fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) -> Stmt { +fn optimize_stmt(stmt: Stmt, state: &mut State, preserve_result: bool) -> Stmt { match stmt { // if expr { Noop } Stmt::IfThenElse(x) if matches!(x.1, Stmt::Noop(_)) => { @@ -359,11 +359,13 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - } /// Optimize an expression. -fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { +fn optimize_expr(expr: Expr, state: &mut State) -> Expr { // These keywords are handled specially const DONT_EVAL_KEYWORDS: [&str; 3] = [KEYWORD_PRINT, KEYWORD_DEBUG, KEYWORD_EVAL]; match expr { + // expr - do not promote because there is a reason it is wrapped in an `Expr::Expr` + Expr::Expr(x) => Expr::Expr(Box::new(optimize_expr(*x, state))), // ( stmt ) Expr::Stmt(x) => match optimize_stmt(x.0, state, true) { // ( Noop ) -> () diff --git a/src/parser.rs b/src/parser.rs index 24e6f76e..7a2ad059 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -23,6 +23,7 @@ use crate::stdlib::{ collections::HashMap, format, iter::{empty, Peekable}, + mem, num::NonZeroUsize, ops::{Add, Deref, DerefMut}, string::{String, ToString}, @@ -391,6 +392,8 @@ pub enum Expr { Property(Box<((String, String, String), Position)>), /// { stmt } Stmt(Box<(Stmt, Position)>), + /// Wrapped expression - should not be optimized away. + Expr(Box), /// func(expr, ... ) - ((function name, native_only, position), optional modules, hash, arguments, optional default value) /// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls /// and the function names are predictable, so no need to allocate a new `String`. @@ -441,6 +444,8 @@ impl Expr { /// Panics when the expression is not constant. pub fn get_constant_value(&self) -> Dynamic { match self { + Self::Expr(x) => x.get_constant_value(), + Self::IntegerConstant(x) => x.0.into(), #[cfg(not(feature = "no_float"))] Self::FloatConstant(x) => x.0.into(), @@ -475,6 +480,8 @@ impl Expr { /// Panics when the expression is not constant. pub fn get_constant_str(&self) -> String { match self { + Self::Expr(x) => x.get_constant_str(), + #[cfg(not(feature = "no_float"))] Self::FloatConstant(x) => x.0.to_string(), @@ -494,6 +501,8 @@ impl Expr { /// Get the `Position` of the expression. pub fn position(&self) -> Position { match self { + Self::Expr(x) => x.position(), + #[cfg(not(feature = "no_float"))] Self::FloatConstant(x) => x.1, @@ -519,6 +528,11 @@ impl Expr { /// Override the `Position` of the expression. pub(crate) fn set_position(mut self, new_pos: Position) -> Self { match &mut self { + Self::Expr(ref mut x) => { + let expr = mem::take(x); + *x = Box::new(expr.set_position(new_pos)); + } + #[cfg(not(feature = "no_float"))] Self::FloatConstant(x) => x.1 = new_pos, @@ -550,6 +564,8 @@ impl Expr { /// A pure expression has no side effects. pub fn is_pure(&self) -> bool { match self { + Self::Expr(x) => x.is_pure(), + Self::Array(x) => x.0.iter().all(Self::is_pure), Self::Index(x) | Self::And(x) | Self::Or(x) | Self::In(x) => { @@ -568,6 +584,8 @@ impl Expr { /// Is the expression a constant? pub fn is_constant(&self) -> bool { match self { + Self::Expr(x) => x.is_constant(), + #[cfg(not(feature = "no_float"))] Self::FloatConstant(_) => true, @@ -598,6 +616,8 @@ impl Expr { /// Is a particular token allowed as a postfix operator to this expression? pub fn is_valid_postfix(&self, token: &Token) -> bool { match self { + Self::Expr(x) => x.is_valid_postfix(token), + #[cfg(not(feature = "no_float"))] Self::FloatConstant(_) => false, @@ -996,7 +1016,7 @@ fn parse_index_chain<'a>( (Token::LeftBracket, _) => { let idx_pos = eat_token(input, Token::LeftBracket); // Recursively parse the indexing chain, right-binding each - let idx = parse_index_chain( + let idx_expr = parse_index_chain( input, state, idx_expr, @@ -1005,10 +1025,20 @@ fn parse_index_chain<'a>( allow_stmt_expr, )?; // Indexing binds to right - Ok(Expr::Index(Box::new((lhs, idx, pos)))) + Ok(Expr::Index(Box::new((lhs, idx_expr, pos)))) } // Otherwise terminate the indexing chain - _ => Ok(Expr::Index(Box::new((lhs, idx_expr, pos)))), + _ => { + match idx_expr { + // Terminate with an `Expr::Expr` wrapper to prevent the last index expression + // inside brackets to be mis-parsed as another level of indexing, or a + // dot expression/function call to be mis-parsed as following the indexing chain. + Expr::Index(_) | Expr::Dot(_) | Expr::FnCall(_) => Ok(Expr::Index( + Box::new((lhs, Expr::Expr(Box::new(idx_expr)), pos)), + )), + _ => Ok(Expr::Index(Box::new((lhs, idx_expr, pos)))), + } + } } } (Token::LexError(err), pos) => return Err(PERR::BadInput(err.to_string()).into_err(*pos)), diff --git a/tests/string.rs b/tests/string.rs index 981d19eb..b868f050 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -20,21 +20,20 @@ fn test_string() -> Result<(), Box> { assert!(engine.eval::(r#"let y = "hello, world!"; 'w' in y"#)?); assert!(!engine.eval::(r#"let y = "hello, world!"; "hey" in y"#)?); - #[cfg(not(feature = "no_stdlib"))] assert_eq!(engine.eval::(r#""foo" + 123"#)?, "foo123"); - #[cfg(not(feature = "no_stdlib"))] #[cfg(not(feature = "no_object"))] assert_eq!(engine.eval::("(42).to_string()")?, "42"); + #[cfg(not(feature = "no_object"))] + assert_eq!(engine.eval::(r#"let y = "hello"; y[y.len-1]"#)?, 'o'); + #[cfg(not(feature = "no_float"))] - #[cfg(not(feature = "no_stdlib"))] assert_eq!(engine.eval::(r#""foo" + 123.4556"#)?, "foo123.4556"); Ok(()) } -#[cfg(not(feature = "no_stdlib"))] #[cfg(not(feature = "no_object"))] #[test] fn test_string_substring() -> Result<(), Box> { From 666a618e403b63e7d0e4cd815f306432eca36809 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 30 May 2020 10:28:17 +0800 Subject: [PATCH 40/53] Add register getter/setter/indexer to modules. --- RELEASES.md | 9 +++++ src/module.rs | 106 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 101 insertions(+), 14 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 79e0af4c..286aac4a 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -9,6 +9,12 @@ Regression fix * Do not optimize script with `eval_expression` - it is assumed to be one-off and short. +Bug fixes +--------- + +* Indexing with an index or dot expression now works property (it compiled wrongly before). + For example, `let s = "hello"; s[s.len-1] = 'x';` now works property instead of an error. + Breaking changes ---------------- @@ -26,12 +32,15 @@ Breaking changes normal function-call style (previously the first argument will be passed by _value_ if not called in method-call style). Of course, if the first argument is a calculated value (e.g. result of an expression), then mutating it has no effect, but at least it is not cloned. +* Some built-in methods (e.g. `len` for string, `floor` for `FLOAT`) now have _property_ versions in + addition to methods to simplify coding. New features ------------ * Set limit on maximum level of nesting expressions and statements to avoid panics during parsing. * New `EvalPackage` to disable `eval`. +* `Module::set_getter_fn`, `Module::set_setter_fn` and `Module:set_indexer_fn` to register getter/setter/indexer functions. Speed enhancements ------------------ diff --git a/src/module.rs b/src/module.rs index 5c5d3d67..498739b7 100644 --- a/src/module.rs +++ b/src/module.rs @@ -2,7 +2,7 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; -use crate::engine::{Engine, FunctionsLib}; +use crate::engine::{make_getter, make_setter, Engine, FunctionsLib, FUNC_INDEXER}; use crate::fn_native::{CallableFunction, FnCallArgs, IteratorFn}; use crate::parser::{ FnAccess, @@ -378,9 +378,9 @@ impl Module { ) } - /// Set a Rust function taking two parameters into the module, returning a hash key. + /// Set a Rust getter function taking one mutable parameter, returning a hash key. /// - /// If there is a similar existing Rust function, it is replaced. + /// If there is a similar existing Rust getter function, it is replaced. /// /// # Examples /// @@ -388,7 +388,30 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_2("calc", |x: i64, y: String| { + /// let hash = module.set_getter_fn("value", |x: &mut i64| { Ok(*x) }); + /// assert!(module.get_fn(hash).is_some()); + /// ``` + #[cfg(not(feature = "no_object"))] + pub fn set_getter_fn( + &mut self, + name: impl Into, + #[cfg(not(feature = "sync"))] func: impl Fn(&mut A) -> FuncReturn + 'static, + #[cfg(feature = "sync")] func: impl Fn(&mut A) -> FuncReturn + Send + Sync + 'static, + ) -> u64 { + self.set_fn_1_mut(make_getter(&name.into()), func) + } + + /// Set a Rust function taking two parameters into the module, returning a hash key. + /// + /// If there is a similar existing Rust function, it is replaced. + /// + /// # Examples + /// + /// ``` + /// use rhai::{Module, ImmutableString}; + /// + /// let mut module = Module::new(); + /// let hash = module.set_fn_2("calc", |x: i64, y: ImmutableString| { /// Ok(x + y.len() as i64) /// }); /// assert!(module.get_fn(hash).is_some()); @@ -417,13 +440,15 @@ impl Module { /// Set a Rust function taking two parameters (the first one mutable) into the module, /// returning a hash key. /// + /// If there is a similar existing Rust function, it is replaced. + /// /// # Examples /// /// ``` - /// use rhai::Module; + /// use rhai::{Module, ImmutableString}; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_2_mut("calc", |x: &mut i64, y: String| { + /// let hash = module.set_fn_2_mut("calc", |x: &mut i64, y: ImmutableString| { /// *x += y.len() as i64; Ok(*x) /// }); /// assert!(module.get_fn(hash).is_some()); @@ -449,6 +474,59 @@ impl Module { ) } + /// Set a Rust setter function taking two parameters (the first one mutable) into the module, + /// returning a hash key. + /// + /// If there is a similar existing setter Rust function, it is replaced. + /// + /// # Examples + /// + /// ``` + /// use rhai::{Module, ImmutableString}; + /// + /// let mut module = Module::new(); + /// let hash = module.set_setter_fn("value", |x: &mut i64, y: ImmutableString| { + /// *x = y.len() as i64; + /// Ok(()) + /// }); + /// assert!(module.get_fn(hash).is_some()); + /// ``` + #[cfg(not(feature = "no_object"))] + pub fn set_setter_fn( + &mut self, + name: impl Into, + #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn<()> + 'static, + #[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn<()> + Send + Sync + 'static, + ) -> u64 { + self.set_fn_2_mut(make_setter(&name.into()), func) + } + + /// Set a Rust indexer function taking two parameters (the first one mutable) into the module, + /// returning a hash key. + /// + /// If there is a similar existing setter Rust function, it is replaced. + /// + /// # Examples + /// + /// ``` + /// use rhai::{Module, ImmutableString}; + /// + /// let mut module = Module::new(); + /// let hash = module.set_indexer_fn(|x: &mut i64, y: ImmutableString| { + /// Ok(*x + y.len() as i64) + /// }); + /// assert!(module.get_fn(hash).is_some()); + /// ``` + #[cfg(not(feature = "no_object"))] + #[cfg(not(feature = "no_index"))] + pub fn set_indexer_fn( + &mut self, + #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn + 'static, + #[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn + Send + Sync + 'static, + ) -> u64 { + self.set_fn_2_mut(FUNC_INDEXER, func) + } + /// Set a Rust function taking three parameters into the module, returning a hash key. /// /// If there is a similar existing Rust function, it is replaced. @@ -456,10 +534,10 @@ impl Module { /// # Examples /// /// ``` - /// use rhai::Module; + /// use rhai::{Module, ImmutableString}; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_3("calc", |x: i64, y: String, z: i64| { + /// let hash = module.set_fn_3("calc", |x: i64, y: ImmutableString, z: i64| { /// Ok(x + y.len() as i64 + z) /// }); /// assert!(module.get_fn(hash).is_some()); @@ -499,10 +577,10 @@ impl Module { /// # Examples /// /// ``` - /// use rhai::Module; + /// use rhai::{Module, ImmutableString}; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_3_mut("calc", |x: &mut i64, y: String, z: i64| { + /// let hash = module.set_fn_3_mut("calc", |x: &mut i64, y: ImmutableString, z: i64| { /// *x += y.len() as i64 + z; Ok(*x) /// }); /// assert!(module.get_fn(hash).is_some()); @@ -541,10 +619,10 @@ impl Module { /// # Examples /// /// ``` - /// use rhai::Module; + /// use rhai::{Module, ImmutableString}; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_4("calc", |x: i64, y: String, z: i64, _w: ()| { + /// let hash = module.set_fn_4("calc", |x: i64, y: ImmutableString, z: i64, _w: ()| { /// Ok(x + y.len() as i64 + z) /// }); /// assert!(module.get_fn(hash).is_some()); @@ -591,10 +669,10 @@ impl Module { /// # Examples /// /// ``` - /// use rhai::Module; + /// use rhai::{Module, ImmutableString}; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_4_mut("calc", |x: &mut i64, y: String, z: i64, _w: ()| { + /// let hash = module.set_fn_4_mut("calc", |x: &mut i64, y: ImmutableString, z: i64, _w: ()| { /// *x += y.len() as i64 + z; Ok(*x) /// }); /// assert!(module.get_fn(hash).is_some()); From 4c46c7e26ba59a85c8efbd0370d07c63bf187f16 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 30 May 2020 10:30:21 +0800 Subject: [PATCH 41/53] Register property versions of some methods. --- README.md | 120 ++++++++++++++++++------------------ benches/eval_expression.rs | 8 +-- benches/parsing.rs | 8 +-- scripts/fibonacci.rhai | 2 +- scripts/mat_mul.rhai | 14 ++--- scripts/primes.rhai | 2 +- scripts/speed_test.rhai | 2 +- scripts/string.rhai | 6 +- src/packages/array_basic.rs | 4 ++ src/packages/math_basic.rs | 12 ++++ src/packages/string_more.rs | 4 ++ src/packages/time_basic.rs | 45 ++++++++------ tests/arrays.rs | 2 +- tests/for.rs | 2 +- tests/time.rs | 1 - 15 files changed, 128 insertions(+), 104 deletions(-) diff --git a/README.md b/README.md index 89728438..97c65279 100644 --- a/README.md +++ b/README.md @@ -300,7 +300,7 @@ let ast = engine.compile(true, r" // a function with two parameters: String and i64 fn hello(x, y) { - x.len() + y + x.len + y } // functions can be overloaded: this one takes only one parameter @@ -355,7 +355,7 @@ use rhai::{Engine, Func}; // use 'Func' for 'create_from_s let engine = Engine::new(); // create a new 'Engine' just for this -let script = "fn calc(x, y) { x + y.len() < 42 }"; +let script = "fn calc(x, y) { x + y.len < 42 }"; // Func takes two type parameters: // 1) a tuple made up of the types of the script function's parameters @@ -945,8 +945,8 @@ If the [`no_object`] feature is turned on, however, the _method_ style of functi (i.e. calling a function as an object-method) is no longer supported. ```rust -// Below is a syntax error under 'no_object' because 'len' cannot be called in method style. -let result = engine.eval::("let x = [1, 2, 3]; x.len()")?; +// Below is a syntax error under 'no_object' because 'clear' cannot be called in method style. +let result = engine.eval::<()>("let x = [1, 2, 3]; x.clear()")?; ``` [`type_of()`] works fine with custom types and returns the name of the type. @@ -1082,7 +1082,7 @@ fn main() -> Result<(), Box> // First invocation engine.eval_with_scope::<()>(&mut scope, r" - let x = 4 + 5 - y + z + s.len(); + let x = 4 + 5 - y + z + s.len; y = 1; ")?; @@ -1312,16 +1312,16 @@ Floating-point functions The following standard functions (defined in the [`BasicMathPackage`](#packages) but excluded if using a [raw `Engine`]) operate on `f64` only: -| Category | Functions | -| ---------------- | ------------------------------------------------------------ | -| Trigonometry | `sin`, `cos`, `tan`, `sinh`, `cosh`, `tanh` in degrees | -| Arc-trigonometry | `asin`, `acos`, `atan`, `asinh`, `acosh`, `atanh` in degrees | -| Square root | `sqrt` | -| Exponential | `exp` (base _e_) | -| Logarithmic | `ln` (base _e_), `log10` (base 10), `log` (any base) | -| Rounding | `floor`, `ceiling`, `round`, `int`, `fraction` | -| Conversion | [`to_int`] | -| Testing | `is_nan`, `is_finite`, `is_infinite` | +| Category | Functions | +| ---------------- | --------------------------------------------------------------------- | +| Trigonometry | `sin`, `cos`, `tan`, `sinh`, `cosh`, `tanh` in degrees | +| Arc-trigonometry | `asin`, `acos`, `atan`, `asinh`, `acosh`, `atanh` in degrees | +| Square root | `sqrt` | +| Exponential | `exp` (base _e_) | +| Logarithmic | `ln` (base _e_), `log10` (base 10), `log` (any base) | +| Rounding | `floor`, `ceiling`, `round`, `int`, `fraction` methods and properties | +| Conversion | [`to_int`] | +| Testing | `is_nan`, `is_finite`, `is_infinite` methods and properties | Strings and Chars ----------------- @@ -1413,32 +1413,32 @@ record == "Bob X. Davis: age 42 ❤\n"; The following standard methods (mostly defined in the [`MoreStringPackage`](#packages) but excluded if using a [raw `Engine`]) operate on strings: -| Function | Parameter(s) | Description | -| ----------------------- | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | -| `len` | _none_ | returns the number of characters (not number of bytes) in the string | -| `pad` | character to pad, target length | pads the string with an character to at least a specified length | -| `+=` operator, `append` | character/string to append | Adds a character or a string to the end of another string | -| `clear` | _none_ | empties the string | -| `truncate` | target length | cuts off the string at exactly a specified number of characters | -| `contains` | character/sub-string to search for | checks if a certain character or sub-string occurs in the string | -| `index_of` | character/sub-string to search for, start index _(optional)_ | returns the index that a certain character or sub-string occurs in the string, or -1 if not found | -| `sub_string` | start index, length _(optional)_ | extracts a sub-string (to the end of the string if length is not specified) | -| `crop` | start index, length _(optional)_ | retains only a portion of the string (to the end of the string if length is not specified) | -| `replace` | target character/sub-string, replacement character/string | replaces a sub-string with another | -| `trim` | _none_ | trims the string of whitespace at the beginning and end | +| Function | Parameter(s) | Description | +| ------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| `len` method and property | _none_ | returns the number of characters (not number of bytes) in the string | +| `pad` | character to pad, target length | pads the string with an character to at least a specified length | +| `+=` operator, `append` | character/string to append | Adds a character or a string to the end of another string | +| `clear` | _none_ | empties the string | +| `truncate` | target length | cuts off the string at exactly a specified number of characters | +| `contains` | character/sub-string to search for | checks if a certain character or sub-string occurs in the string | +| `index_of` | character/sub-string to search for, start index _(optional)_ | returns the index that a certain character or sub-string occurs in the string, or -1 if not found | +| `sub_string` | start index, length _(optional)_ | extracts a sub-string (to the end of the string if length is not specified) | +| `crop` | start index, length _(optional)_ | retains only a portion of the string (to the end of the string if length is not specified) | +| `replace` | target character/sub-string, replacement character/string | replaces a sub-string with another | +| `trim` | _none_ | trims the string of whitespace at the beginning and end | ### Examples ```rust let full_name == " Bob C. Davis "; -full_name.len() == 14; +full_name.len == 14; full_name.trim(); -full_name.len() == 12; +full_name.len == 12; full_name == "Bob C. Davis"; full_name.pad(15, '$'); -full_name.len() == 15; +full_name.len == 15; full_name == "Bob C. Davis$$$"; let n = full_name.index_of('$'); @@ -1449,11 +1449,11 @@ full_name.index_of("$$", n + 1) == 13; full_name.sub_string(n, 3) == "$$$"; full_name.truncate(6); -full_name.len() == 6; +full_name.len == 6; full_name == "Bob C."; full_name.replace("Bob", "John"); -full_name.len() == 7; +full_name.len == 7; full_name == "John C."; full_name.contains('C') == true; @@ -1466,7 +1466,7 @@ full_name.crop(0, 1); full_name == "C"; full_name.clear(); -full_name.len() == 0; +full_name.len == 0; ``` Arrays @@ -1488,19 +1488,19 @@ Arrays are disabled via the [`no_index`] feature. The following methods (mostly defined in the [`BasicArrayPackage`](#packages) but excluded if using a [raw `Engine`]) operate on arrays: -| Function | Parameter(s) | Description | -| ----------------------- | --------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | -| `push` | element to insert | inserts an element at the end | -| `+=` operator, `append` | array to append | concatenates the second array to the end of the first | -| `+` operator | first array, second array | concatenates the first array with the second | -| `insert` | element to insert, position
(beginning if <= 0, end if >= length) | insert an element at a certain index | -| `pop` | _none_ | removes the last element and returns it ([`()`] if empty) | -| `shift` | _none_ | removes the first element and returns it ([`()`] if empty) | -| `remove` | index | removes an element at a particular index and returns it, or returns [`()`] if the index is not valid | -| `len` | _none_ | returns the number of elements | -| `pad` | element to pad, target length | pads the array with an element to at least a specified length | -| `clear` | _none_ | empties the array | -| `truncate` | target length | cuts off the array at exactly a specified length (discarding all subsequent elements) | +| Function | Parameter(s) | Description | +| ------------------------- | --------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | +| `push` | element to insert | inserts an element at the end | +| `+=` operator, `append` | array to append | concatenates the second array to the end of the first | +| `+` operator | first array, second array | concatenates the first array with the second | +| `insert` | element to insert, position
(beginning if <= 0, end if >= length) | insert an element at a certain index | +| `pop` | _none_ | removes the last element and returns it ([`()`] if empty) | +| `shift` | _none_ | removes the first element and returns it ([`()`] if empty) | +| `remove` | index | removes an element at a particular index and returns it, or returns [`()`] if the index is not valid | +| `len` method and property | _none_ | returns the number of elements | +| `pad` | element to pad, target length | pads the array with an element to at least a specified length | +| `clear` | _none_ | empties the array | +| `truncate` | target length | cuts off the array at exactly a specified length (discarding all subsequent elements) | ### Examples @@ -1510,7 +1510,7 @@ let y = [2, 3]; // array literal with 2 elements y.insert(0, 1); // insert element at the beginning y.insert(999, 4); // insert element at the end -y.len() == 4; +y.len == 4; y[0] == 1; y[1] == 2; @@ -1527,7 +1527,7 @@ y[1] = 42; // array elements can be reassigned y.remove(2) == 3; // remove element -y.len() == 3; +y.len == 3; y[2] == 4; // elements after the removed element are shifted @@ -1551,7 +1551,7 @@ foo == 1; y.push(4); // 4 elements y.push(5); // 5 elements -y.len() == 5; +y.len == 5; let first = y.shift(); // remove the first element, 4 elements remaining first == 1; @@ -1559,7 +1559,7 @@ first == 1; let last = y.pop(); // remove the last element, 3 elements remaining last == 5; -y.len() == 3; +y.len == 3; for item in y { // arrays can be iterated with a 'for' statement print(item); @@ -1567,15 +1567,15 @@ for item in y { // arrays can be iterated with a 'for' statement y.pad(10, "hello"); // pad the array up to 10 elements -y.len() == 10; +y.len == 10; y.truncate(5); // truncate the array to 5 elements -y.len() == 5; +y.len == 5; y.clear(); // empty the array -y.len() == 0; +y.len == 0; ``` `push` and `pad` are only defined for standard built-in types. For custom types, type-specific versions must be registered: @@ -1745,10 +1745,10 @@ The Rust type of a timestamp is `std::time::Instant`. [`type_of()`] a timestamp The following methods (defined in the [`BasicTimePackage`](#packages) but excluded if using a [raw `Engine`]) operate on timestamps: -| Function | Parameter(s) | Description | -| ------------ | ---------------------------------- | -------------------------------------------------------- | -| `elapsed` | _none_ | returns the number of seconds since the timestamp | -| `-` operator | later timestamp, earlier timestamp | returns the number of seconds between the two timestamps | +| Function | Parameter(s) | Description | +| ------------------ | ---------------------------------- | -------------------------------------------------------- | +| `elapsed` property | _none_ | returns the number of seconds since the timestamp | +| `-` operator | later timestamp, earlier timestamp | returns the number of seconds between the two timestamps | ### Examples @@ -1757,7 +1757,7 @@ let now = timestamp(); // Do some lengthy operation... -if now.elapsed() > 30.0 { +if now.elapsed > 30.0 { print("takes too long (over 30 seconds)!") } ``` @@ -2281,7 +2281,7 @@ let ast = engine.compile(r#" x + 1 } fn add_len(x, y) { - x + y.len() + x + y.len } // Imported modules can become sub-modules diff --git a/benches/eval_expression.rs b/benches/eval_expression.rs index affb25bb..ceaaaa17 100644 --- a/benches/eval_expression.rs +++ b/benches/eval_expression.rs @@ -48,7 +48,7 @@ fn bench_eval_expression_optimized_simple(bench: &mut Bencher) { 2 > 1 && "something" != "nothing" || "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && - [array, with, spaces].len() <= #{prop:name}.len() && + [array, with, spaces].len <= #{prop:name}.len && modifierTest + 1000 / 2 > (80 * 100 % 2) "#; @@ -65,7 +65,7 @@ fn bench_eval_expression_optimized_full(bench: &mut Bencher) { 2 > 1 && "something" != "nothing" || "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && - [array, with, spaces].len() <= #{prop:name}.len() && + [array, with, spaces].len <= #{prop:name}.len && modifierTest + 1000 / 2 > (80 * 100 % 2) "#; @@ -82,7 +82,7 @@ fn bench_eval_call_expression(bench: &mut Bencher) { 2 > 1 && "something" != "nothing" || "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && - [array, with, spaces].len() <= #{prop:name}.len() && + [array, with, spaces].len <= #{prop:name}.len && modifierTest + 1000 / 2 > (80 * 100 % 2) "#; @@ -97,7 +97,7 @@ fn bench_eval_call(bench: &mut Bencher) { 2 > 1 && "something" != "nothing" || "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && - [array, with, spaces].len() <= #{prop:name}.len() && + [array, with, spaces].len <= #{prop:name}.len && modifierTest + 1000 / 2 > (80 * 100 % 2) "#; diff --git a/benches/parsing.rs b/benches/parsing.rs index 01011877..8e84bd8d 100644 --- a/benches/parsing.rs +++ b/benches/parsing.rs @@ -32,7 +32,7 @@ fn bench_parse_full(bench: &mut Bencher) { 2 > 1 && "something" != "nothing" || "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && - [array, with, spaces].len() <= #{prop:name}.len() && + [array, with, spaces].len <= #{prop:name}.len && modifierTest + 1000 / 2 > (80 * 100 % 2) "#; @@ -94,7 +94,7 @@ fn bench_parse_primes(bench: &mut Bencher) { } print("Total " + total_primes_found + " primes."); - print("Run time = " + now.elapsed() + " seconds."); + print("Run time = " + now.elapsed + " seconds."); "#; let mut engine = Engine::new(); @@ -109,7 +109,7 @@ fn bench_parse_optimize_simple(bench: &mut Bencher) { 2 > 1 && "something" != "nothing" || "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && - [array, with, spaces].len() <= #{prop:name}.len() && + [array, with, spaces].len <= #{prop:name}.len && modifierTest + 1000 / 2 > (80 * 100 % 2) "#; @@ -125,7 +125,7 @@ fn bench_parse_optimize_full(bench: &mut Bencher) { 2 > 1 && "something" != "nothing" || "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && - [array, with, spaces].len() <= #{prop:name}.len() && + [array, with, spaces].len <= #{prop:name}.len && modifierTest + 1000 / 2 > (80 * 100 % 2) "#; diff --git a/scripts/fibonacci.rhai b/scripts/fibonacci.rhai index 730dadae..3455c5f6 100644 --- a/scripts/fibonacci.rhai +++ b/scripts/fibonacci.rhai @@ -17,7 +17,7 @@ let now = timestamp(); let result = fib(target); -print("Finished. Run time = " + now.elapsed() + " seconds."); +print("Finished. Run time = " + now.elapsed + " seconds."); print("Fibonacci number #" + target + " = " + result); diff --git a/scripts/mat_mul.rhai b/scripts/mat_mul.rhai index 59011e22..b93a1a5e 100644 --- a/scripts/mat_mul.rhai +++ b/scripts/mat_mul.rhai @@ -24,9 +24,9 @@ fn mat_gen(n) { } fn mat_mul(a, b) { - let m = a.len(); - let n = a[0].len(); - let p = b[0].len(); + let m = a.len; + let n = a[0].len; + let p = b[0].len; let b2 = new_mat(n, p); @@ -38,13 +38,13 @@ fn mat_mul(a, b) { let c = new_mat(m, p); - for i in range(0, c.len()) { + for i in range(0, c.len) { let ci = c[i]; - for j in range(0, ci.len()) { + for j in range(0, ci.len) { let b2j = b2[j]; ci[j] = 0.0; - for z in range(0, a[i].len()) { + for z in range(0, a[i].len) { let x = a[i][z]; let y = b2j[z]; ci[j] += x * y; @@ -66,4 +66,4 @@ for i in range(0, SIZE) { print(c[i]); } -print("Finished. Run time = " + now.elapsed() + " seconds."); +print("Finished. Run time = " + now.elapsed + " seconds."); diff --git a/scripts/primes.rhai b/scripts/primes.rhai index 2b67ec50..7f66976f 100644 --- a/scripts/primes.rhai +++ b/scripts/primes.rhai @@ -26,7 +26,7 @@ for p in range(2, MAX_NUMBER_TO_CHECK) { } print("Total " + total_primes_found + " primes <= " + MAX_NUMBER_TO_CHECK); -print("Run time = " + now.elapsed() + " seconds."); +print("Run time = " + now.elapsed + " seconds."); if total_primes_found != 9_592 { print("The answer is WRONG! Should be 9,592!"); diff --git a/scripts/speed_test.rhai b/scripts/speed_test.rhai index e07bd7ec..edb9bd0e 100644 --- a/scripts/speed_test.rhai +++ b/scripts/speed_test.rhai @@ -10,4 +10,4 @@ while x > 0 { x -= 1; } -print("Finished. Run time = " + now.elapsed() + " seconds."); +print("Finished. Run time = " + now.elapsed + " seconds."); diff --git a/scripts/string.rhai b/scripts/string.rhai index ab30a29a..835b2556 100644 --- a/scripts/string.rhai +++ b/scripts/string.rhai @@ -11,7 +11,7 @@ print("foo" >= "bar"); // string comparison print("the answer is " + 42); // string building using non-string types let s = "hello, world!"; // string variable -print("length=" + s.len()); // should be 13 +print("length=" + s.len); // should be 13 -s[s.len()-1] = '?'; // change the string -print(s); // should print 'hello, world?' +s[s.len-1] = '?'; // change the string +print("Question: " + s); // should print 'Question: hello, world?' diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 5b96ffe2..19206e36 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -105,6 +105,10 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { }, ); lib.set_fn_1_mut("len", |list: &mut Array| Ok(list.len() as INT)); + + #[cfg(not(feature = "no_object"))] + lib.set_getter_fn("len", |list: &mut Array| Ok(list.len() as INT)); + lib.set_fn_1_mut("clear", |list: &mut Array| { list.clear(); Ok(()) diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 9c7313ed..eea49727 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -43,6 +43,18 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { lib.set_fn_1("is_finite", |x: FLOAT| Ok(x.is_finite())); lib.set_fn_1("is_infinite", |x: FLOAT| Ok(x.is_infinite())); + #[cfg(not(feature = "no_object"))] + { + lib.set_getter_fn("floor", |x: &mut FLOAT| Ok(x.floor())); + lib.set_getter_fn("ceiling", |x: &mut FLOAT| Ok(x.ceil())); + lib.set_getter_fn("round", |x: &mut FLOAT| Ok(x.ceil())); + lib.set_getter_fn("int", |x: &mut FLOAT| Ok(x.trunc())); + lib.set_getter_fn("fraction", |x: &mut FLOAT| Ok(x.fract())); + lib.set_getter_fn("is_nan", |x: &mut FLOAT| Ok(x.is_nan())); + lib.set_getter_fn("is_finite", |x: &mut FLOAT| Ok(x.is_finite())); + lib.set_getter_fn("is_infinite", |x: &mut FLOAT| Ok(x.is_infinite())); + } + // Register conversion functions lib.set_fn_1("to_float", |x: INT| Ok(x as FLOAT)); lib.set_fn_1("to_float", |x: f32| Ok(x as FLOAT)); diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 6f3cbc40..e86f5fb1 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -106,6 +106,10 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str } lib.set_fn_1_mut("len", |s: &mut ImmutableString| Ok(s.chars().count() as INT)); + + #[cfg(not(feature = "no_object"))] + lib.set_getter_fn("len", |s: &mut ImmutableString| Ok(s.chars().count() as INT)); + lib.set_fn_2_mut( "contains", |s: &mut ImmutableString, ch: char| Ok(s.contains(ch)), diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index 7d80344c..b8e13604 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -10,6 +10,9 @@ use crate::token::Position; #[cfg(not(feature = "no_std"))] use crate::stdlib::time::Instant; +#[cfg(not(feature = "no_float"))] +use crate::parser::FLOAT; + #[cfg(not(feature = "no_std"))] def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, { // Register date/time functions @@ -70,27 +73,29 @@ def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, { lib.set_fn_2("==", eq::); lib.set_fn_2("!=", ne::); - lib.set_fn_1( - "elapsed", - |timestamp: Instant| { - #[cfg(not(feature = "no_float"))] - return Ok(timestamp.elapsed().as_secs_f64()); + #[cfg(not(feature = "no_float"))] + fn elapsed (timestamp: &mut Instant) -> Result> { + Ok(timestamp.elapsed().as_secs_f64()) + } - #[cfg(feature = "no_float")] - { - let seconds = timestamp.elapsed().as_secs(); + #[cfg(feature = "no_float")] + fn elapsed (timestamp: &mut Instant) -> Result> { + let seconds = timestamp.elapsed().as_secs(); - #[cfg(not(feature = "unchecked"))] - { - if seconds > (MAX_INT as u64) { - return Err(Box::new(EvalAltResult::ErrorArithmetic( - format!("Integer overflow for timestamp.elapsed(): {}", seconds), - Position::none(), - ))); - } - } - return Ok(seconds as INT); + #[cfg(not(feature = "unchecked"))] + { + if seconds > (MAX_INT as u64) { + return Err(Box::new(EvalAltResult::ErrorArithmetic( + format!("Integer overflow for timestamp.elapsed: {}", seconds), + Position::none(), + ))); } - }, - ); + } + Ok(seconds as INT) + } + + lib.set_fn_1_mut("elapsed", elapsed); + + #[cfg(not(feature = "no_object"))] + lib.set_getter_fn("elapsed", elapsed); }); diff --git a/tests/arrays.rs b/tests/arrays.rs index ea710eba..741156e9 100644 --- a/tests/arrays.rs +++ b/tests/arrays.rs @@ -26,7 +26,7 @@ fn test_arrays() -> Result<(), Box> { let y = [4, 5]; x.append(y); - x.len() + r + x.len + r " )?, 14 diff --git a/tests/for.rs b/tests/for.rs index 2512ec72..cddff6ed 100644 --- a/tests/for.rs +++ b/tests/for.rs @@ -48,7 +48,7 @@ fn test_for_object() -> Result<(), Box> { sum += value; } - keys.len() + sum + keys.len + sum "#; assert_eq!(engine.eval::(script)?, 9); diff --git a/tests/time.rs b/tests/time.rs index aa5239b1..2590e174 100644 --- a/tests/time.rs +++ b/tests/time.rs @@ -1,4 +1,3 @@ -#![cfg(not(feature = "no_stdlib"))] #![cfg(not(feature = "no_std"))] use rhai::{Engine, EvalAltResult, INT}; From acd0f6b56b93d3f7b0821a772a61bc02e77d25ae Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 30 May 2020 13:49:40 +0800 Subject: [PATCH 42/53] != defaults to true for different parameter types. --- src/parser.rs | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 7a2ad059..688dc7b1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1762,37 +1762,34 @@ fn parse_binary_op<'a>( let cmp_def = Some(false.into()); let op = op_token.syntax(); let hash = calc_fn_hash(empty(), &op, 2, empty()); + let op = (op, true, pos); let mut args = StaticVec::new(); args.push(root); args.push(rhs); root = match op_token { - Token::Plus => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), - Token::Minus => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), - Token::Multiply => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), - Token::Divide => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), + Token::Plus + | Token::Minus + | Token::Multiply + | Token::Divide + | Token::LeftShift + | Token::RightShift + | Token::Modulo + | Token::PowerOf + | Token::Ampersand + | Token::Pipe + | Token::XOr => Expr::FnCall(Box::new((op, None, hash, args, None))), - Token::LeftShift => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), - Token::RightShift => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), - Token::Modulo => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), - Token::PowerOf => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), + // '!=' defaults to true when passed invalid operands + Token::NotEqualsTo => Expr::FnCall(Box::new((op, None, hash, args, Some(true.into())))), // Comparison operators default to false when passed invalid operands - Token::EqualsTo => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))), - Token::NotEqualsTo => { - Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))) - } - Token::LessThan => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))), - Token::LessThanEqualsTo => { - Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))) - } - Token::GreaterThan => { - Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))) - } - Token::GreaterThanEqualsTo => { - Expr::FnCall(Box::new(((op, true, pos), None, hash, args, cmp_def))) - } + Token::EqualsTo + | Token::LessThan + | Token::LessThanEqualsTo + | Token::GreaterThan + | Token::GreaterThanEqualsTo => Expr::FnCall(Box::new((op, None, hash, args, cmp_def))), Token::Or => { let rhs = args.pop(); @@ -1804,10 +1801,6 @@ fn parse_binary_op<'a>( let current_lhs = args.pop(); Expr::And(Box::new((current_lhs, rhs, pos))) } - Token::Ampersand => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), - Token::Pipe => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), - Token::XOr => Expr::FnCall(Box::new(((op, true, pos), None, hash, args, None))), - Token::In => { let rhs = args.pop(); let current_lhs = args.pop(); From 439053b153edcc28633e099213558f8123a28b2c Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 30 May 2020 22:25:01 +0800 Subject: [PATCH 43/53] Refine write-up on functions overloading. --- README.md | 71 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 97c65279..a7b129f0 100644 --- a/README.md +++ b/README.md @@ -704,11 +704,16 @@ let x = (42_i64).into(); // 'into()' works for standard t let y = Dynamic::from(String::from("hello!")); // remember &str is not supported by Rhai ``` +Functions registered with the [`Engine`] can be _overloaded_ as long as the _signature_ is unique, +i.e. different functions can have the same name as long as their parameters are of different types +and/or different number. +New definitions _overwrite_ previous definitions of the same name and same number/types of parameters. + Generic functions ----------------- Rust generic functions can be used in Rhai, but separate instances for each concrete type must be registered separately. -This is essentially function overloading (Rhai does not natively support generics). +This essentially overloads the function with different parameter types (Rhai does not natively support generics). ```rust use std::fmt::Display; @@ -729,8 +734,8 @@ fn main() } ``` -This example shows how to register multiple functions (or, in this case, multiple overloaded versions of the same function) -under the same name. This enables function overloading based on the number and types of parameters. +The above example shows how to register multiple functions (or, in this case, multiple overloaded versions of the same function) +under the same name. Fallible functions ------------------ @@ -773,7 +778,8 @@ fn main() Overriding built-in functions ---------------------------- -Any similarly-named function defined in a script overrides any built-in function. +Any similarly-named function defined in a script overrides any built-in function and any registered +native Rust function of the same name and number of parameters. ```rust // Override the built-in function 'to_int' @@ -784,11 +790,13 @@ fn to_int(num) { print(to_int(123)); // what happens? ``` +A registered function, in turn, overrides any built-in function of the same name and number/types of parameters. + Operator overloading -------------------- In Rhai, a lot of functionalities are actually implemented as functions, including basic operations such as arithmetic calculations. -For example, in the expression "`a + b`", the `+` operator is _not_ built-in, but calls a function named "`+`" instead! +For example, in the expression "`a + b`", the `+` operator is _not_ built in, but calls a function named "`+`" instead! ```rust let x = a + b; @@ -801,7 +809,7 @@ overriding them has no effect at all. Operator functions cannot be defined as a script function (because operators syntax are not valid function names). However, operator functions _can_ be registered to the [`Engine`] via the methods `Engine::register_fn`, `Engine::register_result_fn` etc. -When a custom operator function is registered with the same name as an operator, it _overloads_ (or overrides) the built-in version. +When a custom operator function is registered with the same name as an operator, it overrides the built-in version. ```rust use rhai::{Engine, EvalAltResult, RegisterFn}; @@ -828,7 +836,7 @@ let result: i64 = engine.eval("1 + 1.0"); // prints 2.0 (normally an e ``` Use operator overloading for custom types (described below) only. -Be very careful when overloading built-in operators because script authors expect standard operators to behave in a +Be very careful when overriding built-in operators because script authors expect standard operators to behave in a consistent and predictable manner, and will be annoyed if a calculation for '`+`' turns into a subtraction, for example. Operator overloading also impacts script optimization when using [`OptimizationLevel::Full`]. @@ -915,7 +923,7 @@ engine.register_fn("update", TestStruct::update); // registers 'update(&mut Te engine.register_fn("new_ts", TestStruct::new); // registers 'new()' ``` -The custom type is then ready for us in scripts. Scripts can see the functions and methods registered earlier. +The custom type is then ready for use in scripts. Scripts can see the functions and methods registered earlier. Get the evaluation result back out from script-land just as before, this time casting to the custom type: ```rust @@ -1745,10 +1753,10 @@ The Rust type of a timestamp is `std::time::Instant`. [`type_of()`] a timestamp The following methods (defined in the [`BasicTimePackage`](#packages) but excluded if using a [raw `Engine`]) operate on timestamps: -| Function | Parameter(s) | Description | -| ------------------ | ---------------------------------- | -------------------------------------------------------- | -| `elapsed` property | _none_ | returns the number of seconds since the timestamp | -| `-` operator | later timestamp, earlier timestamp | returns the number of seconds between the two timestamps | +| Function | Parameter(s) | Description | +| ----------------------------- | ---------------------------------- | -------------------------------------------------------- | +| `elapsed` method and property | _none_ | returns the number of seconds since the timestamp | +| `-` operator | later timestamp, earlier timestamp | returns the number of seconds between the two timestamps | ### Examples @@ -1777,15 +1785,19 @@ set of types (see [built-in operators](#built-in-operators)). "42" == 42; // false ``` -Comparing two values of _different_ data types, or of unknown data types, always results in `false`. +Comparing two values of _different_ data types, or of unknown data types, always results in `false`, +except for '`!=`' (not equals) which results in `true`. This is in line with intuition. ```rust -42 == 42.0; // false - i64 is different from f64 -42 > "42"; // false - i64 is different from string -42 <= "42"; // false again +42 == 42.0; // false - i64 cannot be compared with f64 +42 != 42.0; // true - i64 cannot be compared with f64 + +42 > "42"; // false - i64 cannot be compared with string +42 <= "42"; // false - i64 cannot be compared with string let ts = new_ts(); // custom type -ts == 42; // false - types are not the same +ts == 42; // false - types cannot be compared +ts != 42; // true - types cannot be compared ``` Boolean operators @@ -2073,8 +2085,8 @@ This is similar to Rust and many other modern languages. ### Function overloading -Functions can be _overloaded_ and are resolved purely upon the function's _name_ and the _number_ of parameters -(but not parameter _types_, since all parameters are the same type - [`Dynamic`]). +Functions defined in script can be _overloaded_ by _arity_ (i.e. they are resolved purely upon the function's _name_ +and _number_ of parameters, but not parameter _types_ since all parameters are the same type - [`Dynamic`]). New definitions _overwrite_ previous definitions of the same name and number of parameters. ```rust @@ -2346,19 +2358,20 @@ so that it does not consume more resources that it is allowed to. The most important resources to watch out for are: -* **Memory**: A malignant script may continuously grow an [array] or [object map] until all memory is consumed. +* **Memory**: A malicous script may continuously grow an [array] or [object map] until all memory is consumed. It may also create a large [array] or [object map] literal that exhausts all memory during parsing. -* **CPU**: A malignant script may run an infinite tight loop that consumes all CPU cycles. -* **Time**: A malignant script may run indefinitely, thereby blocking the calling system which is waiting for a result. -* **Stack**: A malignant script may attempt an infinite recursive call that exhausts the call stack. +* **CPU**: A malicous script may run an infinite tight loop that consumes all CPU cycles. +* **Time**: A malicous script may run indefinitely, thereby blocking the calling system which is waiting for a result. +* **Stack**: A malicous script may attempt an infinite recursive call that exhausts the call stack. Alternatively, it may create a degenerated deep expression with so many levels that the parser exhausts the call stack when parsing the expression; or even deeply-nested statement blocks, if nested deep enough. -* **Overflows**: A malignant script may deliberately cause numeric over-flows and/or under-flows, divide by zero, and/or +* **Overflows**: A malicous script may deliberately cause numeric over-flows and/or under-flows, divide by zero, and/or create bad floating-point representations, in order to crash the system. -* **Files**: A malignant script may continuously [`import`] an external module within an infinite loop, +* **Files**: A malicous script may continuously [`import`] an external module within an infinite loop, thereby putting heavy load on the file-system (or even the network if the file is not local). + Furthermore, the module script may simply [`import`] itself in an infinite recursion. Even when modules are not created from files, they still typically consume a lot of resources to load. -* **Data**: A malignant script may attempt to read from and/or write to data that it does not own. If this happens, +* **Data**: A malicous script may attempt to read from and/or write to data that it does not own. If this happens, it is a severe security breach and may put the entire system at risk. ### Maximum number of operations @@ -2434,7 +2447,7 @@ Rhai by default limits function calls to a maximum depth of 128 levels (16 level This limit may be changed via the `Engine::set_max_call_levels` method. When setting this limit, care must be also taken to the evaluation depth of each _statement_ -within the function. It is entirely possible for a malignant script to embed an recursive call deep +within the function. It is entirely possible for a malicous script to embed an recursive call deep inside a nested expression or statement block (see [maximum statement depth](#maximum-statement-depth)). The limit can be disabled via the [`unchecked`] feature for higher performance @@ -2479,7 +2492,7 @@ engine.set_max_expr_depths(50, 5); // allow nesting up to 50 layers of Beware that there may be multiple layers for a simple language construct, even though it may correspond to only one AST node. That is because the Rhai _parser_ internally runs a recursive chain of function calls -and it is important that a malignant script does not panic the parser in the first place. +and it is important that a malicous script does not panic the parser in the first place. Functions are placed under stricter limits because of the multiplicative effect of recursion. A script can effectively call itself while deep inside an expression chain within the function body, @@ -2492,7 +2505,7 @@ Make sure that `C x ( 5 + F ) + S` layered calls do not cause a stack overflow, * `S` = maximum statement depth at global level. A script exceeding the maximum nesting depths will terminate with a parsing error. -The malignant `AST` will not be able to get past parsing in the first place. +The malicous `AST` will not be able to get past parsing in the first place. This check can be disabled via the [`unchecked`] feature for higher performance (but higher risks as well). From c9de37e8d1a930df585d1129d89fc746bde1b1ba Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 31 May 2020 00:02:23 +0800 Subject: [PATCH 44/53] Hash functions only once via custom hasher. --- src/engine.rs | 70 +++++++++++++++++++++------------------------------ src/module.rs | 14 +++++++---- src/parser.rs | 67 ++++++++++++++++++++---------------------------- src/utils.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 130 insertions(+), 87 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 2c761c7e..3b740b71 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -12,7 +12,7 @@ use crate::r#unsafe::{unsafe_cast_var_name_to_lifetime, unsafe_mut_cast_to_lifet use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::Position; -use crate::utils::StaticVec; +use crate::utils::{StaticVec, StraightHasherBuilder}; #[cfg(not(feature = "no_float"))] use crate::parser::FLOAT; @@ -196,7 +196,7 @@ impl State { /// /// The key of the `HashMap` is a `u64` hash calculated by the function `calc_fn_hash`. #[derive(Debug, Clone, Default)] -pub struct FunctionsLib(HashMap>); +pub struct FunctionsLib(HashMap, StraightHasherBuilder>); impl FunctionsLib { /// Create a new `FunctionsLib` from a collection of `FnDef`. @@ -261,7 +261,7 @@ impl From)>> for FunctionsLib { } impl Deref for FunctionsLib { - type Target = HashMap>; + type Target = HashMap, StraightHasherBuilder>; fn deref(&self) -> &Self::Target { &self.0 @@ -269,7 +269,7 @@ impl Deref for FunctionsLib { } impl DerefMut for FunctionsLib { - fn deref_mut(&mut self) -> &mut HashMap> { + fn deref_mut(&mut self) -> &mut HashMap, StraightHasherBuilder> { &mut self.0 } } @@ -952,21 +952,24 @@ impl Engine { let mut idx_val = idx_values.pop(); if is_index { + let pos = rhs.position(); + match rhs { - // xxx[idx].dot_rhs... | xxx[idx][dot_rhs]... + // xxx[idx].expr... | xxx[idx][expr]... Expr::Dot(x) | Expr::Index(x) => { + let (idx, expr, pos) = x.as_ref(); let is_idx = matches!(rhs, Expr::Index(_)); - let pos = x.0.position(); - let this_ptr = &mut self - .get_indexed_mut(state, lib, obj, is_ref, idx_val, pos, op_pos, false)?; + let idx_pos = idx.position(); + let this_ptr = &mut self.get_indexed_mut( + state, lib, obj, is_ref, idx_val, idx_pos, op_pos, false, + )?; self.eval_dot_index_chain_helper( - state, lib, this_ptr, &x.1, idx_values, is_idx, x.2, level, new_val, + state, lib, this_ptr, expr, idx_values, is_idx, *pos, level, new_val, ) } // xxx[rhs] = new_val _ if new_val.is_some() => { - let pos = rhs.position(); let this_ptr = &mut self .get_indexed_mut(state, lib, obj, is_ref, idx_val, pos, op_pos, true)?; @@ -975,16 +978,7 @@ impl Engine { } // xxx[rhs] _ => self - .get_indexed_mut( - state, - lib, - obj, - is_ref, - idx_val, - rhs.position(), - op_pos, - false, - ) + .get_indexed_mut(state, lib, obj, is_ref, idx_val, pos, op_pos, false) .map(|v| (v.clone_into_dynamic(), false)), } } else { @@ -1050,57 +1044,51 @@ impl Engine { .map(|(v, _)| (v, false)) } #[cfg(not(feature = "no_object"))] - // {xxx:map}.idx_lhs[idx_expr] | {xxx:map}.dot_lhs.rhs + // {xxx:map}.prop[expr] | {xxx:map}.prop.expr Expr::Index(x) | Expr::Dot(x) if obj.is::() => { + let (prop, expr, pos) = x.as_ref(); let is_idx = matches!(rhs, Expr::Index(_)); - let mut val = if let Expr::Property(p) = &x.0 { + let mut val = if let Expr::Property(p) = prop { let ((prop, _, _), _) = p.as_ref(); let index = prop.clone().into(); - self.get_indexed_mut(state, lib, obj, is_ref, index, x.2, op_pos, false)? + self.get_indexed_mut(state, lib, obj, is_ref, index, *pos, op_pos, false)? } else { - // Syntax error - return Err(Box::new(EvalAltResult::ErrorDotExpr( - "".into(), - rhs.position(), - ))); + unreachable!(); }; self.eval_dot_index_chain_helper( - state, lib, &mut val, &x.1, idx_values, is_idx, x.2, level, new_val, + state, lib, &mut val, expr, idx_values, is_idx, *pos, level, new_val, ) } - // xxx.idx_lhs[idx_expr] | xxx.dot_lhs.rhs + // xxx.prop[expr] | xxx.prop.expr Expr::Index(x) | Expr::Dot(x) => { - let is_idx = matches!(rhs, Expr::Index(_)); + let (prop, expr, pos) = x.as_ref(); + let is_idx = matches!(expr, Expr::Index(_)); let args = &mut [obj, &mut Default::default()]; - let (mut val, updated) = if let Expr::Property(p) = &x.0 { + let (mut val, updated) = if let Expr::Property(p) = prop { let ((_, getter, _), _) = p.as_ref(); let args = &mut args[..1]; - self.exec_fn_call(state, lib, getter, true, 0, args, is_ref, None, x.2, 0)? + self.exec_fn_call(state, lib, getter, true, 0, args, is_ref, None, *pos, 0)? } else { - // Syntax error - return Err(Box::new(EvalAltResult::ErrorDotExpr( - "".into(), - rhs.position(), - ))); + unreachable!(); }; let val = &mut val; let target = &mut val.into(); let (result, may_be_changed) = self.eval_dot_index_chain_helper( - state, lib, target, &x.1, idx_values, is_idx, x.2, level, new_val, + state, lib, target, expr, idx_values, is_idx, *pos, level, new_val, )?; // Feed the value back via a setter just in case it has been updated if updated || may_be_changed { - if let Expr::Property(p) = &x.0 { + if let Expr::Property(p) = prop { let ((_, _, setter), _) = p.as_ref(); // Re-use args because the first &mut parameter will not be consumed args[1] = val; self.exec_fn_call( - state, lib, setter, true, 0, args, is_ref, None, x.2, 0, + state, lib, setter, true, 0, args, is_ref, None, *pos, 0, ) .or_else(|err| match *err { // If there is no setter, no need to feed it back because the property is read-only diff --git a/src/module.rs b/src/module.rs index 498739b7..9e16e276 100644 --- a/src/module.rs +++ b/src/module.rs @@ -12,7 +12,7 @@ use crate::parser::{ use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token}; -use crate::utils::StaticVec; +use crate::utils::{StaticVec, StraightHasherBuilder}; use crate::stdlib::{ any::TypeId, @@ -44,10 +44,14 @@ pub struct Module { variables: HashMap, /// Flattened collection of all module variables, including those in sub-modules. - all_variables: HashMap, + all_variables: HashMap, /// External Rust functions. - functions: HashMap, CallableFunction)>, + functions: HashMap< + u64, + (String, FnAccess, StaticVec, CallableFunction), + StraightHasherBuilder, + >, /// Script-defined functions. lib: FunctionsLib, @@ -57,7 +61,7 @@ pub struct Module { /// Flattened collection of all external Rust functions, native or scripted, /// including those in sub-modules. - all_functions: HashMap, + all_functions: HashMap, } impl fmt::Debug for Module { @@ -101,7 +105,7 @@ impl Module { /// ``` pub fn new_with_capacity(capacity: usize) -> Self { Self { - functions: HashMap::with_capacity(capacity), + functions: HashMap::with_capacity_and_hasher(capacity, StraightHasherBuilder), ..Default::default() } } diff --git a/src/parser.rs b/src/parser.rs index 688dc7b1..bf668759 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -7,7 +7,7 @@ use crate::error::{LexError, ParseError, ParseErrorType}; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token, TokenIterator}; -use crate::utils::StaticVec; +use crate::utils::{StaticVec, StraightHasherBuilder}; #[cfg(not(feature = "no_module"))] use crate::module::ModuleRef; @@ -1496,22 +1496,21 @@ fn parse_op_assignment_stmt<'a>( } /// Make a dot expression. -fn make_dot_expr( - lhs: Expr, - rhs: Expr, - op_pos: Position, - is_index: bool, -) -> Result { +fn make_dot_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result { Ok(match (lhs, rhs) { - // idx_lhs[idx_rhs].rhs + // idx_lhs[idx_expr].rhs // Attach dot chain to the bottom level of indexing chain (Expr::Index(x), rhs) => { - Expr::Index(Box::new((x.0, make_dot_expr(x.1, rhs, op_pos, true)?, x.2))) + let (idx_lhs, idx_expr, pos) = *x; + Expr::Index(Box::new(( + idx_lhs, + make_dot_expr(idx_expr, rhs, op_pos)?, + pos, + ))) } // lhs.id (lhs, Expr::Variable(x)) if x.1.is_none() => { let (name, pos) = x.0; - let lhs = if is_index { lhs.into_property() } else { lhs }; let getter = make_getter(&name); let setter = make_setter(&name); @@ -1519,11 +1518,6 @@ fn make_dot_expr( Expr::Dot(Box::new((lhs, rhs, op_pos))) } - (lhs, Expr::Property(x)) => { - let lhs = if is_index { lhs.into_property() } else { lhs }; - let rhs = Expr::Property(x); - Expr::Dot(Box::new((lhs, rhs, op_pos))) - } // lhs.module::id - syntax error (_, Expr::Variable(x)) if x.1.is_some() => { #[cfg(feature = "no_module")] @@ -1531,34 +1525,30 @@ fn make_dot_expr( #[cfg(not(feature = "no_module"))] return Err(PERR::PropertyExpected.into_err(x.1.unwrap().get(0).1)); } + // lhs.prop + (lhs, prop @ Expr::Property(_)) => Expr::Dot(Box::new((lhs, prop, op_pos))), // lhs.dot_lhs.dot_rhs (lhs, Expr::Dot(x)) => { let (dot_lhs, dot_rhs, pos) = *x; Expr::Dot(Box::new(( lhs, - Expr::Dot(Box::new(( - dot_lhs.into_property(), - dot_rhs.into_property(), - pos, - ))), + Expr::Dot(Box::new((dot_lhs.into_property(), dot_rhs, pos))), op_pos, ))) } // lhs.idx_lhs[idx_rhs] (lhs, Expr::Index(x)) => { - let (idx_lhs, idx_rhs, pos) = *x; + let (dot_lhs, dot_rhs, pos) = *x; Expr::Dot(Box::new(( lhs, - Expr::Index(Box::new(( - idx_lhs.into_property(), - idx_rhs.into_property(), - pos, - ))), + Expr::Index(Box::new((dot_lhs.into_property(), dot_rhs, pos))), op_pos, ))) } + // lhs.func() + (lhs, func @ Expr::FnCall(_)) => Expr::Dot(Box::new((lhs, func, op_pos))), // lhs.rhs - (lhs, rhs) => Expr::Dot(Box::new((lhs, rhs.into_property(), op_pos))), + _ => unreachable!(), }) } @@ -1822,7 +1812,7 @@ fn parse_binary_op<'a>( _ => (), } - make_dot_expr(current_lhs, rhs, pos, false)? + make_dot_expr(current_lhs, rhs, pos)? } token => return Err(PERR::UnknownOperator(token.into()).into_err(pos)), @@ -2493,22 +2483,20 @@ pub fn parse_global_expr<'a>( fn parse_global_level<'a>( input: &mut Peekable>, max_expr_depth: (usize, usize), -) -> Result<(Vec, HashMap), ParseError> { +) -> Result<(Vec, Vec), ParseError> { let mut statements = Vec::::new(); - let mut functions = HashMap::::new(); + let mut functions = HashMap::with_hasher(StraightHasherBuilder); let mut state = ParseState::new(max_expr_depth.0); while !input.peek().unwrap().0.is_eof() { // Collect all the function definitions #[cfg(not(feature = "no_function"))] { - let mut access = FnAccess::Public; - let mut must_be_fn = false; - - if match_token(input, Token::Private)? { - access = FnAccess::Private; - must_be_fn = true; - } + let (access, must_be_fn) = if match_token(input, Token::Private)? { + (FnAccess::Private, true) + } else { + (FnAccess::Public, false) + }; match input.peek().unwrap() { (Token::Fn, _) => { @@ -2565,7 +2553,7 @@ fn parse_global_level<'a>( } } - Ok((statements, functions)) + Ok((statements, functions.into_iter().map(|(_, v)| v).collect())) } /// Run the parser on an input stream, returning an AST. @@ -2576,9 +2564,8 @@ pub fn parse<'a>( optimization_level: OptimizationLevel, max_expr_depth: (usize, usize), ) -> Result { - let (statements, functions) = parse_global_level(input, max_expr_depth)?; + let (statements, lib) = parse_global_level(input, max_expr_depth)?; - let lib = functions.into_iter().map(|(_, v)| v).collect(); Ok( // Optimize AST optimize_into_ast(engine, scope, statements, lib, optimization_level), diff --git a/src/utils.rs b/src/utils.rs index c396df19..ccd3d6be 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -11,7 +11,7 @@ use crate::stdlib::{ borrow::Borrow, boxed::Box, fmt, - hash::{Hash, Hasher}, + hash::{BuildHasher, Hash, Hasher}, iter::FromIterator, mem, mem::MaybeUninit, @@ -27,6 +27,48 @@ use crate::stdlib::collections::hash_map::DefaultHasher; #[cfg(feature = "no_std")] use ahash::AHasher; +/// A hasher that only takes one single `u64` and returns it as a hash key. +/// +/// # Panics +/// +/// Panics when hashing any data type other than a `u64`. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Default)] +pub struct StraightHasher(u64); + +impl Hasher for StraightHasher { + #[inline(always)] + fn finish(&self) -> u64 { + self.0 + } + #[inline] + fn write(&mut self, bytes: &[u8]) { + let mut key = [0_u8; 8]; + key.copy_from_slice(&bytes[..8]); // Panics if fewer than 8 bytes + self.0 = u64::from_le_bytes(key); + } +} + +impl StraightHasher { + /// Create a `StraightHasher`. + #[inline(always)] + pub fn new() -> Self { + Self(0) + } +} + +/// A hash builder for `StraightHasher`. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Default)] +pub struct StraightHasherBuilder; + +impl BuildHasher for StraightHasherBuilder { + type Hasher = StraightHasher; + + #[inline(always)] + fn build_hasher(&self) -> Self::Hasher { + StraightHasher::new() + } +} + /// Calculate a `u64` hash key from a module-qualified function name and parameter types. /// /// Module names are passed in via `&str` references from an iterator. @@ -108,6 +150,7 @@ pub struct StaticVec { const MAX_STATIC_VEC: usize = 4; impl Drop for StaticVec { + #[inline(always)] fn drop(&mut self) { self.clear(); } @@ -174,6 +217,7 @@ impl FromIterator for StaticVec { impl StaticVec { /// Create a new `StaticVec`. + #[inline(always)] pub fn new() -> Self { Default::default() } @@ -189,6 +233,7 @@ impl StaticVec { self.len = 0; } /// Extract a `MaybeUninit` into a concrete initialized type. + #[inline(always)] fn extract(value: MaybeUninit) -> T { unsafe { value.assume_init() } } @@ -250,6 +295,7 @@ impl StaticVec { ); } /// Is data stored in fixed-size storage? + #[inline(always)] fn is_fixed_storage(&self) -> bool { self.len <= MAX_STATIC_VEC } @@ -359,10 +405,12 @@ impl StaticVec { result } /// Get the number of items in this `StaticVec`. + #[inline(always)] pub fn len(&self) -> usize { self.len } /// Is this `StaticVec` empty? + #[inline(always)] pub fn is_empty(&self) -> bool { self.len == 0 } @@ -605,41 +653,48 @@ pub struct ImmutableString(Shared); impl Deref for ImmutableString { type Target = String; + #[inline(always)] fn deref(&self) -> &Self::Target { &self.0 } } impl AsRef for ImmutableString { + #[inline(always)] fn as_ref(&self) -> &String { &self.0 } } impl Borrow for ImmutableString { + #[inline(always)] fn borrow(&self) -> &str { self.0.as_str() } } impl From<&str> for ImmutableString { + #[inline(always)] fn from(value: &str) -> Self { Self(value.to_string().into()) } } impl From for ImmutableString { + #[inline(always)] fn from(value: String) -> Self { Self(value.into()) } } impl From> for ImmutableString { + #[inline(always)] fn from(value: Box) -> Self { Self(value.into()) } } impl From for String { + #[inline(always)] fn from(value: ImmutableString) -> Self { value.into_owned() } @@ -648,42 +703,49 @@ impl From for String { impl FromStr for ImmutableString { type Err = (); + #[inline(always)] fn from_str(s: &str) -> Result { Ok(Self(s.to_string().into())) } } impl FromIterator for ImmutableString { + #[inline(always)] fn from_iter>(iter: T) -> Self { Self(iter.into_iter().collect::().into()) } } impl<'a> FromIterator<&'a char> for ImmutableString { + #[inline(always)] fn from_iter>(iter: T) -> Self { Self(iter.into_iter().cloned().collect::().into()) } } impl<'a> FromIterator<&'a str> for ImmutableString { + #[inline(always)] fn from_iter>(iter: T) -> Self { Self(iter.into_iter().collect::().into()) } } impl<'a> FromIterator for ImmutableString { + #[inline(always)] fn from_iter>(iter: T) -> Self { Self(iter.into_iter().collect::().into()) } } impl fmt::Display for ImmutableString { + #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(self.0.as_str(), f) } } impl fmt::Debug for ImmutableString { + #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(self.0.as_str(), f) } @@ -818,6 +880,7 @@ impl Add for &ImmutableString { } impl AddAssign for ImmutableString { + #[inline(always)] fn add_assign(&mut self, rhs: char) { self.make_mut().push(rhs); } @@ -832,6 +895,7 @@ impl ImmutableString { } /// Make sure that the `ImmutableString` is unique (i.e. no other outstanding references). /// Then return a mutable reference to the `String`. + #[inline(always)] pub fn make_mut(&mut self) -> &mut String { shared_make_mut(&mut self.0) } From 13c49387efc0342c585be0ef48835588c05bf4d1 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 31 May 2020 12:36:31 +0800 Subject: [PATCH 45/53] Add OptimizationLevel::is_simple --- src/optimize.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/optimize.rs b/src/optimize.rs index f8b0d1bb..20ccfa3c 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -37,6 +37,10 @@ impl OptimizationLevel { pub fn is_none(self) -> bool { self == Self::None } + /// Is the `OptimizationLevel` Simple. + pub fn is_simple(self) -> bool { + self == Self::Simple + } /// Is the `OptimizationLevel` Full. pub fn is_full(self) -> bool { self == Self::Full From 5f727335a6b878b43d518a756794078062e391c4 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 31 May 2020 12:36:42 +0800 Subject: [PATCH 46/53] Add type info. --- src/parser.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index bf668759..2a20422e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2485,7 +2485,7 @@ fn parse_global_level<'a>( max_expr_depth: (usize, usize), ) -> Result<(Vec, Vec), ParseError> { let mut statements = Vec::::new(); - let mut functions = HashMap::with_hasher(StraightHasherBuilder); + let mut functions = HashMap::::with_hasher(StraightHasherBuilder); let mut state = ParseState::new(max_expr_depth.0); while !input.peek().unwrap().0.is_eof() { From 76d792011f72d239f81c22012638b95391c87bf5 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 31 May 2020 12:37:19 +0800 Subject: [PATCH 47/53] Add Engine::call_fn_dynamic. --- README.md | 10 ++++++++ RELEASES.md | 3 +++ src/api.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a7b129f0..50792756 100644 --- a/README.md +++ b/README.md @@ -341,6 +341,16 @@ let result: i64 = engine.call_fn(&mut scope, &ast, "hello", () )?; let result: () = engine.call_fn(&mut scope, &ast, "hidden", ())?; ``` +For more control, construct all arguments as `Dynamic` values and use `Engine::call_fn_dynamic`: + +```rust +let result: Dynamic = engine.call_fn_dynamic(&mut scope, &ast, "hello", + &mut [ String::from("abc").into(), 123_i64.into() ])?; +``` + +However, beware that `Engine::call_fn_dynamic` _consumes_ its arguments, meaning that all arguments passed to it +will be replaced by `()` afterwards. To re-use the arguments, clone them beforehand and pass in the clone. + ### Creating Rust anonymous functions from Rhai script [`Func`]: #creating-rust-anonymous-functions-from-rhai-script diff --git a/RELEASES.md b/RELEASES.md index 286aac4a..d0a50a9a 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -41,6 +41,7 @@ New features * Set limit on maximum level of nesting expressions and statements to avoid panics during parsing. * New `EvalPackage` to disable `eval`. * `Module::set_getter_fn`, `Module::set_setter_fn` and `Module:set_indexer_fn` to register getter/setter/indexer functions. +* `Engine::call_fn_dynamic` for more control in calling script functions. Speed enhancements ------------------ @@ -60,6 +61,8 @@ Speed enhancements excessive cloning. For example, if `a` is a large array, getting its length in this manner: `len(a)` used to result in a full clone of `a` before taking the length and throwing the copy away. Now, `a` is simply passed by reference, avoiding the cloning altogether. +* A custom hasher simply passes through `u64` keys without hashing to avoid function call hash keys + (which as by themselves `u64`) being hashed twice. Version 0.14.1 diff --git a/src/api.rs b/src/api.rs index 8a30a480..1d593023 100644 --- a/src/api.rs +++ b/src/api.rs @@ -997,6 +997,7 @@ impl Engine { } /// Call a script function defined in an `AST` with multiple arguments. + /// Arguments are passed as a tuple. /// /// # Example /// @@ -1040,6 +1041,67 @@ impl Engine { args: A, ) -> Result> { let mut arg_values = args.into_vec(); + let result = self.call_fn_dynamic(scope, ast, name, arg_values.as_mut())?; + + let return_type = self.map_type_name(result.type_name()); + + return result.try_cast().ok_or_else(|| { + Box::new(EvalAltResult::ErrorMismatchOutputType( + return_type.into(), + Position::none(), + )) + }); + } + + /// Call a script function defined in an `AST` with multiple `Dynamic` arguments. + /// + /// ## WARNING + /// + /// All the arguments are _consumed_, meaning that they're replaced by `()`. + /// This is to avoid unnecessarily cloning the arguments. + /// Do you use the arguments after this call. If you need them afterwards, + /// clone them _before_ calling this function. + /// + /// # Example + /// + /// ``` + /// # fn main() -> Result<(), Box> { + /// # #[cfg(not(feature = "no_function"))] + /// # { + /// use rhai::{Engine, Scope}; + /// + /// let engine = Engine::new(); + /// + /// let ast = engine.compile(r" + /// fn add(x, y) { len(x) + y + foo } + /// fn add1(x) { len(x) + 1 + foo } + /// fn bar() { foo/2 } + /// ")?; + /// + /// let mut scope = Scope::new(); + /// scope.push("foo", 42_i64); + /// + /// // Call the script-defined function + /// let result = engine.call_fn_dynamic(&mut scope, &ast, "add", &mut [ String::from("abc").into(), 123_i64.into() ])?; + /// assert_eq!(result.cast::(), 168); + /// + /// let result = engine.call_fn_dynamic(&mut scope, &ast, "add1", &mut [ String::from("abc").into() ])?; + /// assert_eq!(result.cast::(), 46); + /// + /// let result= engine.call_fn_dynamic(&mut scope, &ast, "bar", &mut [])?; + /// assert_eq!(result.cast::(), 21); + /// # } + /// # Ok(()) + /// # } + /// ``` + #[cfg(not(feature = "no_function"))] + pub fn call_fn_dynamic( + &self, + scope: &mut Scope, + ast: &AST, + name: &str, + arg_values: &mut [Dynamic], + ) -> Result> { let mut args: StaticVec<_> = arg_values.iter_mut().collect(); let lib = ast.lib(); let pos = Position::none(); @@ -1051,16 +1113,7 @@ impl Engine { let mut state = State::new(); let args = args.as_mut(); - let result = self.call_script_fn(scope, &mut state, &lib, name, fn_def, args, pos, 0)?; - - let return_type = self.map_type_name(result.type_name()); - - return result.try_cast().ok_or_else(|| { - Box::new(EvalAltResult::ErrorMismatchOutputType( - return_type.into(), - pos, - )) - }); + self.call_script_fn(scope, &mut state, &lib, name, fn_def, args, pos, 0) } /// Optimize the `AST` with constants defined in an external Scope. From d7d49a5196f969ed611079cb5a8ea5f8a267deeb Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 31 May 2020 14:27:16 +0800 Subject: [PATCH 48/53] Fix bug in chained dot/index expression. --- src/engine.rs | 4 ++-- tests/maps.rs | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 3b740b71..0fdd0467 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1064,7 +1064,7 @@ impl Engine { // xxx.prop[expr] | xxx.prop.expr Expr::Index(x) | Expr::Dot(x) => { let (prop, expr, pos) = x.as_ref(); - let is_idx = matches!(expr, Expr::Index(_)); + let is_idx = matches!(rhs, Expr::Index(_)); let args = &mut [obj, &mut Default::default()]; let (mut val, updated) = if let Expr::Property(p) = prop { @@ -1102,7 +1102,7 @@ impl Engine { } // Syntax error _ => Err(Box::new(EvalAltResult::ErrorDotExpr( - "".into(), + format!("{:?}", rhs), rhs.position(), ))), } diff --git a/tests/maps.rs b/tests/maps.rs index 64c5a4c2..a7aef8dd 100644 --- a/tests/maps.rs +++ b/tests/maps.rs @@ -21,6 +21,10 @@ fn test_map_indexing() -> Result<(), Box> { )?, 'o' ); + assert_eq!( + engine.eval::(r#"let a = [#{s:"hello"}]; a[0].s[2] = 'X'; a[0].s"#)?, + "heXlo" + ); } assert_eq!( From 840afe74bb574763f408c45726fbdfa7dc134fcb Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 31 May 2020 15:51:26 +0800 Subject: [PATCH 49/53] Simplify eval_dot_index_chain. --- src/engine.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 0fdd0467..03521f4f 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -441,7 +441,6 @@ fn search_scope<'s, 'a>( Expr::Variable(x) => x.as_ref(), _ => unreachable!(), }; - let index = if state.always_search { None } else { *index }; #[cfg(not(feature = "no_module"))] { @@ -470,6 +469,8 @@ fn search_scope<'s, 'a>( } } + let index = if state.always_search { None } else { *index }; + let index = if let Some(index) = index { scope.len() - index.get() } else { @@ -1102,7 +1103,7 @@ impl Engine { } // Syntax error _ => Err(Box::new(EvalAltResult::ErrorDotExpr( - format!("{:?}", rhs), + "".into(), rhs.position(), ))), } @@ -1115,13 +1116,16 @@ impl Engine { scope: &mut Scope, state: &mut State, lib: &FunctionsLib, - dot_lhs: &Expr, - dot_rhs: &Expr, - is_index: bool, - op_pos: Position, + expr: &Expr, level: usize, new_val: Option, ) -> Result> { + let ((dot_lhs, dot_rhs, op_pos), is_index) = match expr { + Expr::Index(x) => (x.as_ref(), true), + Expr::Dot(x) => (x.as_ref(), false), + _ => unreachable!(), + }; + let idx_values = &mut StaticVec::new(); self.eval_indexed_chain(scope, state, lib, dot_rhs, idx_values, 0, level)?; @@ -1146,7 +1150,7 @@ impl Engine { let this_ptr = &mut target.into(); self.eval_dot_index_chain_helper( - state, lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, + state, lib, this_ptr, dot_rhs, idx_values, is_index, *op_pos, level, new_val, ) .map(|(v, _)| v) } @@ -1161,7 +1165,7 @@ impl Engine { let val = self.eval_expr(scope, state, lib, expr, level)?; let this_ptr = &mut val.into(); self.eval_dot_index_chain_helper( - state, lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, + state, lib, this_ptr, dot_rhs, idx_values, is_index, *op_pos, level, new_val, ) .map(|(v, _)| v) } @@ -1477,14 +1481,14 @@ impl Engine { Expr::Variable(_) => unreachable!(), // idx_lhs[idx_expr] op= rhs #[cfg(not(feature = "no_index"))] - Expr::Index(x) => self.eval_dot_index_chain( - scope, state, lib, &x.0, &x.1, true, x.2, level, new_val, - ), + Expr::Index(_) => { + self.eval_dot_index_chain(scope, state, lib, lhs_expr, level, new_val) + } // dot_lhs.dot_rhs op= rhs #[cfg(not(feature = "no_object"))] - Expr::Dot(x) => self.eval_dot_index_chain( - scope, state, lib, &x.0, &x.1, false, *op_pos, level, new_val, - ), + Expr::Dot(_) => { + self.eval_dot_index_chain(scope, state, lib, lhs_expr, level, new_val) + } // Error assignment to constant expr if expr.is_constant() => { Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( @@ -1501,15 +1505,11 @@ impl Engine { // lhs[idx_expr] #[cfg(not(feature = "no_index"))] - Expr::Index(x) => { - self.eval_dot_index_chain(scope, state, lib, &x.0, &x.1, true, x.2, level, None) - } + Expr::Index(_) => self.eval_dot_index_chain(scope, state, lib, expr, level, None), // lhs.dot_rhs #[cfg(not(feature = "no_object"))] - Expr::Dot(x) => { - self.eval_dot_index_chain(scope, state, lib, &x.0, &x.1, false, x.2, level, None) - } + Expr::Dot(_) => self.eval_dot_index_chain(scope, state, lib, expr, level, None), #[cfg(not(feature = "no_index"))] Expr::Array(x) => Ok(Dynamic(Union::Array(Box::new( From 697bb39a7f30db37f4bb16b2bca5b8d877327476 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 31 May 2020 15:55:02 +0800 Subject: [PATCH 50/53] Add writeup on Rhai usage scenarios. --- README.md | 20 ++++++++++++++++---- RELEASES.md | 6 +++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 50792756..a127d857 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,8 @@ Features including [getters/setters](#getters-and-setters), [methods](#members-and-methods) and [indexers](#indexers). * Freely pass Rust variables/constants into a script via an external [`Scope`]. * Easily [call a script-defined function](#calling-rhai-functions-from-rust) from Rust. -* Low compile-time overhead (~0.6 sec debug/~3 sec release for `rhai_runner` sample app). -* Fairly efficient evaluation (1 million iterations in 0.75 sec on my 5 year old laptop). +* Fairly low compile-time overhead. +* Fairly efficient evaluation (1 million iterations in 0.25 sec on a single core, 2.3 GHz Linux VM). * Relatively little `unsafe` code (yes there are some for performance reasons, and most `unsafe` code is limited to one single source file, all with names starting with `"unsafe_"`). * Re-entrant scripting [`Engine`] can be made `Send + Sync` (via the [`sync`] feature). @@ -47,10 +47,22 @@ It doesn't attempt to be a new language. For example: * No classes. Well, Rust doesn't either. On the other hand... * No traits... so it is also not Rust. Do your Rusty stuff in Rust. -* No structures - define your types in Rust instead; Rhai can seamlessly work with _any Rust type_. +* No structures/records - define your types in Rust instead; Rhai can seamlessly work with _any Rust type_. + There is, however, a built-in [object map] type which is adequate for most uses. * No first-class functions - Code your functions in Rust instead, and register them with Rhai. +* No garbage collection - this should be expected, so... * No closures - do your closure magic in Rust instead; [turn a Rhai scripted function into a Rust closure](#calling-rhai-functions-from-rust). -* It is best to expose an API in Rhai for scripts to call. All your core functionalities should be in Rust. +* No byte-codes/JIT - Rhai has an AST-walking interpreter which will not win any speed races. The purpose of Rhai is not + to be extremely _fast_, but to make it as easy as possible to integrate with native Rust programs. + +Due to this intended usage, Rhai deliberately keeps the language simple and small by omitting advanced language features +such as classes, inheritance, first-class functions, closures, concurrency, byte-codes, JIT etc. +Avoid the temptation to write full-fledge program logic entirely in Rhai - that use case is best fulfilled by +more complete languages such as JS or Lua. + +Therefore, in actual practice, it is usually best to expose a Rust API into Rhai for scripts to call. +All your core functionalities should be in Rust. +This is similar to some dynamic languages where most of the core functionalities reside in a C/C++ standard library. Installation ------------ diff --git a/RELEASES.md b/RELEASES.md index d0a50a9a..1208c720 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -13,13 +13,13 @@ Bug fixes --------- * Indexing with an index or dot expression now works property (it compiled wrongly before). - For example, `let s = "hello"; s[s.len-1] = 'x';` now works property instead of an error. + For example, `let s = "hello"; s[s.len-1] = 'x';` now works property instead of causing a runtime error. Breaking changes ---------------- * `Engine::compile_XXX` functions now return `ParseError` instead of `Box`. -* The `RegisterDynamicFn` trait is merged into the `RegisterResutlFn` trait which now always returns +* The `RegisterDynamicFn` trait is merged into the `RegisterResultFn` trait which now always returns `Result>`. * Default maximum limit on levels of nested function calls is fine-tuned and set to a different value. * Some operator functions are now built in (see _Speed enhancements_ below), so they are available even @@ -62,7 +62,7 @@ Speed enhancements in a full clone of `a` before taking the length and throwing the copy away. Now, `a` is simply passed by reference, avoiding the cloning altogether. * A custom hasher simply passes through `u64` keys without hashing to avoid function call hash keys - (which as by themselves `u64`) being hashed twice. + (which are by themselves `u64`) being hashed twice. Version 0.14.1 From 7fa05f3886eb842de7e569ea885a6413483d477b Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 31 May 2020 15:57:53 +0800 Subject: [PATCH 51/53] Do not print to avoid skewing the run timing. --- scripts/mat_mul.rhai | 2 ++ scripts/primes.rhai | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/mat_mul.rhai b/scripts/mat_mul.rhai index b93a1a5e..54bca636 100644 --- a/scripts/mat_mul.rhai +++ b/scripts/mat_mul.rhai @@ -62,8 +62,10 @@ let a = mat_gen(SIZE); let b = mat_gen(SIZE); let c = mat_mul(a, b); +/* for i in range(0, SIZE) { print(c[i]); } +*/ print("Finished. Run time = " + now.elapsed + " seconds."); diff --git a/scripts/primes.rhai b/scripts/primes.rhai index 7f66976f..fc4acc7e 100644 --- a/scripts/primes.rhai +++ b/scripts/primes.rhai @@ -15,7 +15,7 @@ let total_primes_found = 0; for p in range(2, MAX_NUMBER_TO_CHECK) { if !prime_mask[p] { continue; } - print(p); + //print(p); total_primes_found += 1; From 331513f5e08e8ce138bbb7ee038bfb8347d1074c Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 31 May 2020 22:47:39 +0800 Subject: [PATCH 52/53] Increase to prime numbers <= 1 million. --- scripts/primes.rhai | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/primes.rhai b/scripts/primes.rhai index fc4acc7e..25fa0690 100644 --- a/scripts/primes.rhai +++ b/scripts/primes.rhai @@ -2,7 +2,7 @@ let now = timestamp(); -const MAX_NUMBER_TO_CHECK = 100_000; // 9592 primes <= 100000 +const MAX_NUMBER_TO_CHECK = 1_000_000; // 9592 primes <= 100000 let prime_mask = []; prime_mask.pad(MAX_NUMBER_TO_CHECK, true); @@ -28,6 +28,6 @@ for p in range(2, MAX_NUMBER_TO_CHECK) { print("Total " + total_primes_found + " primes <= " + MAX_NUMBER_TO_CHECK); print("Run time = " + now.elapsed + " seconds."); -if total_primes_found != 9_592 { - print("The answer is WRONG! Should be 9,592!"); +if total_primes_found != 78_498 { + print("The answer is WRONG! Should be 78,498!"); } \ No newline at end of file From c6e5f672c937ea245d0fe70dfa2660b1b22268f5 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 31 May 2020 23:44:49 +0800 Subject: [PATCH 53/53] More benchmarks and examples. --- README.md | 63 +++++++++++++----------- benches/eval_array.rs | 24 +++++++++ scripts/for2.rhai | 22 +++++++++ scripts/strings_map.rhai | 103 +++++++++++++++++++++++++++++++++++++++ src/optimize.rs | 16 +++--- 5 files changed, 189 insertions(+), 39 deletions(-) create mode 100644 scripts/for2.rhai create mode 100644 scripts/strings_map.rhai diff --git a/README.md b/README.md index a127d857..f894ea7b 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Features to do checked arithmetic operations); for [`no-std`](#optional-features) builds, a number of additional dependencies are pulled in to provide for functionalities that used to be in `std`. -**Note:** Currently, the version is 0.15.0, so the language and API's may change before they stabilize. +**Note:** Currently, the version is `0.15.0`, so the language and API's may change before they stabilize. What Rhai doesn't do -------------------- @@ -67,7 +67,7 @@ This is similar to some dynamic languages where most of the core functionalities Installation ------------ -Install the Rhai crate by adding this line to `dependencies`: +Install the Rhai crate on [`crates.io`](https::/crates.io/crates/rhai/) by adding this line to `dependencies`: ```toml [dependencies] @@ -191,33 +191,35 @@ A number of examples can be found in the `examples` folder: Examples can be run with the following command: ```bash -cargo run --example name +cargo run --example {example_name} ``` The `repl` example is a particularly good one as it allows one to interactively try out Rhai's language features in a standard REPL (**R**ead-**E**val-**P**rint **L**oop). -Example Scripts +Example scripts --------------- There are also a number of examples scripts that showcase Rhai's features, all in the `scripts` folder: -| Language feature scripts | Description | -| ---------------------------------------------------- | ------------------------------------------------------------- | -| [`array.rhai`](scripts/array.rhai) | [arrays] in Rhai | -| [`assignment.rhai`](scripts/assignment.rhai) | variable declarations | -| [`comments.rhai`](scripts/comments.rhai) | just comments | -| [`for1.rhai`](scripts/for1.rhai) | for loops | -| [`function_decl1.rhai`](scripts/function_decl1.rhai) | a function without parameters | -| [`function_decl2.rhai`](scripts/function_decl2.rhai) | a function with two parameters | -| [`function_decl3.rhai`](scripts/function_decl3.rhai) | a function with many parameters | -| [`if1.rhai`](scripts/if1.rhai) | if example | -| [`loop.rhai`](scripts/loop.rhai) | endless loop in Rhai, this example emulates a do..while cycle | -| [`op1.rhai`](scripts/op1.rhai) | just a simple addition | -| [`op2.rhai`](scripts/op2.rhai) | simple addition and multiplication | -| [`op3.rhai`](scripts/op3.rhai) | change evaluation order with parenthesis | -| [`string.rhai`](scripts/string.rhai) | [string] operations | -| [`while.rhai`](scripts/while.rhai) | while loop | +| Language feature scripts | Description | +| ---------------------------------------------------- | ----------------------------------------------------------------------------- | +| [`array.rhai`](scripts/array.rhai) | [arrays] in Rhai | +| [`assignment.rhai`](scripts/assignment.rhai) | variable declarations | +| [`comments.rhai`](scripts/comments.rhai) | just comments | +| [`for1.rhai`](scripts/for1.rhai) | [`for`](#for-loop) loops | +| [`for2.rhai`](scripts/for2.rhai) | [`for`](#for-loop) loops on [arrays] | +| [`function_decl1.rhai`](scripts/function_decl1.rhai) | a [function] without parameters | +| [`function_decl2.rhai`](scripts/function_decl2.rhai) | a [function] with two parameters | +| [`function_decl3.rhai`](scripts/function_decl3.rhai) | a [function] with many parameters | +| [`if1.rhai`](scripts/if1.rhai) | [`if`](#if-statement) example | +| [`loop.rhai`](scripts/loop.rhai) | count-down [`loop`](#infinite-loop) in Rhai, emulating a `do` .. `while` loop | +| [`op1.rhai`](scripts/op1.rhai) | just simple addition | +| [`op2.rhai`](scripts/op2.rhai) | simple addition and multiplication | +| [`op3.rhai`](scripts/op3.rhai) | change evaluation order with parenthesis | +| [`string.rhai`](scripts/string.rhai) | [string] operations | +| [`strings_map.rhai`](scripts/strings_map.rhai) | [string] and [object map] operations | +| [`while.rhai`](scripts/while.rhai) | [`while`](#while-loop) loop | | Example scripts | Description | | -------------------------------------------- | ---------------------------------------------------------------------------------- | @@ -247,6 +249,7 @@ fn main() -> Result<(), Box> let engine = Engine::new(); let result = engine.eval::("40 + 2")?; + // ^^^^^^^ cast the result to an 'i64', this is required println!("Answer: {}", result); // prints 42 @@ -303,6 +306,8 @@ let ast = engine.compile_file("hello_world.rhai".into())?; ### Calling Rhai functions from Rust +[`private`]: #calling-rhai-functions-from-rust + Rhai also allows working _backwards_ from the other direction - i.e. calling a Rhai-scripted function from Rust via `Engine::call_fn`. Functions declared with `private` are hidden and cannot be called from Rust (see also [modules]). @@ -1870,8 +1875,8 @@ my_str += 12345; my_str == "abcABC12345" ``` -`if` statements ---------------- +`if` statement +-------------- ```rust if foo(x) { @@ -1906,8 +1911,8 @@ let x = if decision { 42 }; // no else branch defaults to '()' x == (); ``` -`while` loops -------------- +`while` loop +------------ ```rust let x = 10; @@ -1934,8 +1939,8 @@ loop { } ``` -`for` loops ------------ +`for` loop +---------- Iterating through a range or an [array] is provided by the `for` ... `in` loop. @@ -2206,8 +2211,8 @@ Modules can be disabled via the [`no_module`] feature. A _module_ is a single script (or pre-compiled `AST`) containing global variables and functions. The `export` statement, which can only be at global level, exposes selected variables as members of a module. Variables not exported are _private_ and invisible to the outside. -On the other hand, all functions are automatically exported, _unless_ it is explicitly opt-out with the `private` prefix. -Functions declared `private` are invisible to the outside. +On the other hand, all functions are automatically exported, _unless_ it is explicitly opt-out with the [`private`] prefix. +Functions declared [`private`] are invisible to the outside. Everything exported from a module is **constant** (**read-only**). @@ -2301,7 +2306,7 @@ engine.eval_expression_with_scope::(&scope, "question::inc(question::answer It is easy to convert a pre-compiled `AST` into a module: just use `Module::eval_ast_as_new`. Don't forget the `export` statement, otherwise there will be no variables exposed by the module -other than non-`private` functions (unless that's intentional). +other than non-[`private`] functions (unless that's intentional). ```rust use rhai::{Engine, Module}; diff --git a/benches/eval_array.rs b/benches/eval_array.rs index d97e1164..0689bf4f 100644 --- a/benches/eval_array.rs +++ b/benches/eval_array.rs @@ -61,3 +61,27 @@ fn bench_eval_array_large_set(bench: &mut Bencher) { bench.iter(|| engine.consume_ast(&ast).unwrap()); } + +#[bench] +fn bench_eval_array_loop(bench: &mut Bencher) { + let script = r#" + let list = []; + + for i in range(0, 10_000) { + list.push(i); + } + + let sum = 0; + + for i in list { + sum += i; + } + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::None); + + let ast = engine.compile(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} diff --git a/scripts/for2.rhai b/scripts/for2.rhai new file mode 100644 index 00000000..8547e7d5 --- /dev/null +++ b/scripts/for2.rhai @@ -0,0 +1,22 @@ +const MAX = 1_000_000; + +print("Iterating an array with " + MAX + " items..."); + +print("Ready... Go!"); + +let now = timestamp(); + +let list = []; + +for i in range(0, MAX) { + list.push(i); +} + +let sum = 0; + +for i in list { + sum += i; +} + +print("Sum = " + sum); +print("Finished. Run time = " + now.elapsed + " seconds."); diff --git a/scripts/strings_map.rhai b/scripts/strings_map.rhai new file mode 100644 index 00000000..04cd81db --- /dev/null +++ b/scripts/strings_map.rhai @@ -0,0 +1,103 @@ +print("Ready... Go!"); + +let now = timestamp(); + +let adverbs = [ "moderately", "really", "slightly", "very" ]; + +let adjectives = [ + "abandoned", "able", "absolute", "academic", "acceptable", "acclaimed", + "accomplished", "accurate", "aching", "acidic", "acrobatic", "active", + "actual", "adept", "admirable", "admired", "adolescent", "adorable", "adored", + "advanced", "adventurous", "affectionate", "afraid", "aged", "aggravating", + "aggressive", "agile", "agitated", "agonizing", "agreeable", "ajar", + "alarmed", "alarming", "alert", "alienated", "alive", "all", "altruistic", + "amazing", "ambitious", "ample", "amused", "amusing", "anchored", "ancient", + "angelic", "angry", "anguished", "animated", "annual", "another", "antique", + "anxious", "any", "apprehensive", "appropriate", "apt", "arctic", "arid", + "aromatic", "artistic", "ashamed", "assured", "astonishing", "athletic", + "attached", "attentive", "attractive", "austere", "authentic", "authorized", + "automatic", "avaricious", "average", "aware", "awesome", "awful", "awkward", + "babyish", "back", "bad", "baggy", "bare", "barren", "basic", "beautiful", + "belated", "beloved", "beneficial", "best", "better", "bewitched", "big", + "big-hearted", "biodegradable", "bite-sized", "bitter", "black", + "black-and-white", "bland", "blank", "blaring", "bleak", "blind", "blissful", + "blond", "blue", "blushing", "bogus", "boiling", "bold", "bony", "boring", + "bossy", "both", "bouncy", "bountiful", "bowed", "brave", "breakable", + "brief", "bright", "brilliant", "brisk", "broken", "bronze", "brown", + "bruised", "bubbly", "bulky", "bumpy", "buoyant", "burdensome", "burly", + "bustling", "busy", "buttery", "buzzing", "calculating", "calm", "candid", + "canine", "capital", "carefree", "careful", "careless", "caring", "cautious", + "cavernous", "celebrated", "charming", "cheap", "cheerful", "cheery", "chief", + "chilly", "chubby", "circular", "classic", "clean", "clear", "clear-cut", + "clever", "close", "closed", "cloudy", "clueless", "clumsy", "cluttered", + "coarse", "cold", "colorful", "colorless", "colossal", "comfortable", + "common", "compassionate", "competent", "complete", "complex", "complicated", + "composed", "concerned", "concrete", "confused", "conscious", "considerate", + "constant", "content", "conventional", "cooked", "cool", "cooperative", + "coordinated", "corny", "corrupt", "costly", "courageous", "courteous", + "crafty" +]; + +let animals = [ + "aardvark", "african buffalo", "albatross", "alligator", "alpaca", "ant", + "anteater", "antelope", "ape", "armadillo", "baboon", "badger", "barracuda", + "bat", "bear", "beaver", "bee", "bison", "black panther", "blue jay", "boar", + "butterfly", "camel", "capybara", "carduelis", "caribou", "cassowary", "cat", + "caterpillar", "cattle", "chamois", "cheetah", "chicken", "chimpanzee", + "chinchilla", "chough", "clam", "cobra", "cockroach", "cod", "cormorant", + "coyote", "crab", "crane", "crocodile", "crow", "curlew", "deer", "dinosaur", + "dog", "dolphin", "domestic pig", "donkey", "dotterel", "dove", "dragonfly", + "duck", "dugong", "dunlin", "eagle", "echidna", "eel", "elephant seal", + "elephant", "elk", "emu", "falcon", "ferret", "finch", "fish", "flamingo", + "fly", "fox", "frog", "gaur", "gazelle", "gerbil", "giant panda", "giraffe", + "gnat", "goat", "goldfish", "goose", "gorilla", "goshawk", "grasshopper", + "grouse", "guanaco", "guinea fowl", "guinea pig", "gull", "hamster", "hare", + "hawk", "hedgehog", "heron", "herring", "hippopotamus", "hornet", "horse", + "human", "hummingbird", "hyena", "ibex", "ibis", "jackal", "jaguar", "jay", + "jellyfish", "kangaroo", "kingfisher", "koala", "komodo dragon", "kookabura", + "kouprey", "kudu", "lapwing", "lark", "lemur", "leopard", "lion", "llama", + "lobster", "locust", "loris", "louse", "lyrebird", "magpie", "mallard", + "manatee", "mandrill", "mantis", "marten", "meerkat", "mink", "mole", + "mongoose", "monkey", "moose", "mosquito", "mouse", "mule", "narwhal", "newt", + "nightingale", "octopus", "okapi", "opossum", "oryx", "ostrich", "otter", + "owl", "oyster", "parrot", "partridge", "peafowl", "pelican", "penguin", + "pheasant", "pigeon", "pinniped", "polar bear", "pony", "porcupine", + "porpoise", "prairie dog", "quail", "quelea", "quetzal", "rabbit", "raccoon", + "ram", "rat", "raven", "red deer", "red panda", "reindeer", "rhinoceros", + "rook", "salamander", "salmon", "sand dollar", "sandpiper", "sardine", + "scorpion", "sea lion", "sea urchin", "seahorse", "shark", "sheep", "shrew", + "skunk", "snail", "snake", "sparrow", "spider", "spoonbill", "squid", + "wallaby", "wildebeest" +]; + +let keys = []; + +for animal in animals { + for adjective in adjectives { + for adverb in adverbs { + keys.push(adverb + " " + adjective + " " + animal) + } + } +} + +let map = #{}; + +let i = 0; + +for key in keys { + map[key] = i; + i += 1; +} + +let sum = 0; + +for key in keys { + sum += map[key]; +} + +for key in keys { + map.remove(key); +} + +print("Sum = " + sum); +print("Finished. Run time = " + now.elapsed + " seconds."); diff --git a/src/optimize.rs b/src/optimize.rs index 20ccfa3c..78613ace 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -385,10 +385,10 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr { // ( stmt ) stmt => Expr::Stmt(Box::new((stmt, x.1))), }, - // id = expr + // id op= expr Expr::Assignment(x) => match x.2 { - //id = id2 op= expr2 - Expr::Assignment(x2) if x.1 == "=" => match (x.0, x2.0) { + //id = id2 op= rhs + Expr::Assignment(x2) if x.1.is_empty() => match (x.0, &x2.0) { // var = var op= expr2 -> var op= expr2 (Expr::Variable(a), Expr::Variable(b)) if a.1.is_none() && b.1.is_none() && a.0 == b.0 && a.3 == b.3 => @@ -397,14 +397,10 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr { state.set_dirty(); Expr::Assignment(Box::new((Expr::Variable(a), x2.1, optimize_expr(x2.2, state), x.3))) } - // id1 = id2 op= expr2 - (id1, id2) => { - Expr::Assignment(Box::new(( - id1, x.1, Expr::Assignment(Box::new((id2, x2.1, optimize_expr(x2.2, state), x2.3))), x.3, - ))) - } + // expr1 = expr2 op= rhs + (expr1, _) => Expr::Assignment(Box::new((expr1, x.1, optimize_expr(Expr::Assignment(x2), state), x.3))), }, - // id op= expr + // expr = rhs expr => Expr::Assignment(Box::new((x.0, x.1, optimize_expr(expr, state), x.3))), },