diff --git a/CHANGELOG.md b/CHANGELOG.md index d2dafe5b..990ceab6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ Rhai Release Notes Version 0.20.3 ============== +Bug fixes +--------- + +* Fixed incorrect optimization regarding chain-indexing with non-numeric index. + +New features +------------ + +* An integer value can now be indexed to get/set a single bit. + Version 0.20.2 ============== diff --git a/src/ast.rs b/src/ast.rs index 3fd9f6fd..7a0dfd73 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -2014,13 +2014,13 @@ impl Expr { Self::DynamicConstant(_, _) | Self::BoolConstant(_, _) - | Self::IntegerConstant(_, _) | Self::CharConstant(_, _) | Self::And(_, _) | Self::Or(_, _) | Self::Unit(_) => false, - Self::StringConstant(_, _) + Self::IntegerConstant(_, _) + | Self::StringConstant(_, _) | Self::InterpolatedString(_) | Self::FnCall(_, _) | Self::Stmt(_) diff --git a/src/bin/rhai-run.rs b/src/bin/rhai-run.rs index 887cffc4..e2867515 100644 --- a/src/bin/rhai-run.rs +++ b/src/bin/rhai-run.rs @@ -83,7 +83,7 @@ fn main() { if let Err(err) = engine .compile(contents) - .map_err(|err| Box::new(err.into()) as Box) + .map_err(|err| err.into()) .and_then(|mut ast| { ast.set_source(filename.to_string_lossy().to_string()); engine.consume_ast(&ast) diff --git a/src/engine.rs b/src/engine.rs index 7e73572c..023729b7 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -336,6 +336,10 @@ pub enum Target<'a> { LockGuard((crate::dynamic::DynamicWriteLock<'a, Dynamic>, Dynamic)), /// The target is a temporary `Dynamic` value (i.e. the mutation can cause no side effects). Value(Dynamic), + /// The target is a bit inside an [`INT`][crate::INT]. + /// This is necessary because directly pointing to a bit inside an [`INT`][crate::INT] is impossible. + #[cfg(not(feature = "no_index"))] + BitField(&'a mut Dynamic, usize, Dynamic), /// The target is a character inside a String. /// This is necessary because directly pointing to a char inside a String is impossible. #[cfg(not(feature = "no_index"))] @@ -354,6 +358,8 @@ impl<'a> Target<'a> { Self::LockGuard(_) => true, Self::Value(_) => false, #[cfg(not(feature = "no_index"))] + Self::BitField(_, _, _) => false, + #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, _) => false, } } @@ -368,6 +374,8 @@ impl<'a> Target<'a> { Self::LockGuard(_) => false, Self::Value(_) => true, #[cfg(not(feature = "no_index"))] + Self::BitField(_, _, _) => false, + #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, _) => false, } } @@ -382,6 +390,8 @@ impl<'a> Target<'a> { Self::LockGuard(_) => true, Self::Value(r) => r.is_shared(), #[cfg(not(feature = "no_index"))] + Self::BitField(_, _, _) => false, + #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, _) => false, } } @@ -396,6 +406,8 @@ impl<'a> Target<'a> { Self::LockGuard((r, _)) => r.is::(), Self::Value(r) => r.is::(), #[cfg(not(feature = "no_index"))] + Self::BitField(_, _, _) => TypeId::of::() == TypeId::of::(), + #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, _) => TypeId::of::() == TypeId::of::(), } } @@ -409,6 +421,8 @@ impl<'a> Target<'a> { Self::LockGuard((_, orig)) => orig, // Original value is simply taken Self::Value(v) => v, // Owned value is simply taken #[cfg(not(feature = "no_index"))] + Self::BitField(_, _, value) => value, // Boolean is taken + #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, ch) => ch, // Character is taken } } @@ -435,9 +449,14 @@ impl<'a> Target<'a> { #[cfg(not(feature = "no_closure"))] Self::LockGuard(_) => Ok(()), #[cfg(not(feature = "no_index"))] + Self::BitField(_, _, value) => { + let new_val = value.clone(); + self.set_value(new_val, Position::NONE) + } + #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, ch) => { - let char_value = ch.clone(); - self.set_value(char_value, Position::NONE) + let new_val = ch.clone(); + self.set_value(new_val, Position::NONE) } } } @@ -454,6 +473,32 @@ impl<'a> Target<'a> { Self::LockGuard((r, _)) => **r = new_val, Self::Value(_) => panic!("cannot update a value"), #[cfg(not(feature = "no_index"))] + Self::BitField(value, index, _) => { + let value = &mut *value + .write_lock::() + .expect("never fails because `BitField` always holds an `INT`"); + + // Replace the bit at the specified index position + let new_bit = new_val.as_bool().map_err(|err| { + Box::new(EvalAltResult::ErrorMismatchDataType( + "bool".to_string(), + err.to_string(), + _pos, + )) + })?; + + let index = *index; + + if index < std::mem::size_of_val(value) * 8 { + let mask = 1 << index; + if new_bit { + *value |= mask; + } else { + *value &= !mask; + } + } + } + #[cfg(not(feature = "no_index"))] Self::StringChar(s, index, _) => { let s = &mut *s .write_lock::() @@ -514,6 +559,8 @@ impl Deref for Target<'_> { Self::LockGuard((r, _)) => &**r, Self::Value(ref r) => r, #[cfg(not(feature = "no_index"))] + Self::BitField(_, _, ref r) => r, + #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, ref r) => r, } } @@ -536,6 +583,8 @@ impl DerefMut for Target<'_> { Self::LockGuard((r, _)) => r.deref_mut(), Self::Value(ref mut r) => r, #[cfg(not(feature = "no_index"))] + Self::BitField(_, _, ref mut r) => r, + #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, ref mut r) => r, } } @@ -1818,6 +1867,43 @@ impl Engine { .unwrap_or_else(|| Target::from(Dynamic::UNIT))) } + #[cfg(not(feature = "no_index"))] + Dynamic(Union::Int(value, _, _)) => { + // val_int[idx] + let index = idx + .as_int() + .map_err(|err| self.make_type_mismatch_err::(err, idx_pos))?; + + let bits = std::mem::size_of_val(value) * 8; + + let (bit_value, offset) = if index >= 0 { + let offset = index as usize; + ( + if offset >= bits { + return EvalAltResult::ErrorBitFieldBounds(bits, index, idx_pos).into(); + } else { + (*value & (1 << offset)) != 0 + }, + offset, + ) + } else if let Some(abs_index) = index.checked_abs() { + let offset = abs_index as usize; + ( + // Count from end if negative + if offset > bits { + return EvalAltResult::ErrorBitFieldBounds(bits, index, idx_pos).into(); + } else { + (*value & (1 << (bits - offset))) != 0 + }, + offset, + ) + } else { + return EvalAltResult::ErrorBitFieldBounds(bits, index, idx_pos).into(); + }; + + Ok(Target::BitField(target, offset, bit_value.into())) + } + #[cfg(not(feature = "no_index"))] Dynamic(Union::Str(s, _, _)) => { // val_string[idx] @@ -1826,7 +1912,6 @@ impl Engine { .map_err(|err| self.make_type_mismatch_err::(err, idx_pos))?; let (ch, offset) = if index >= 0 { - // Count from end if negative let offset = index as usize; ( s.chars().nth(offset).ok_or_else(|| { @@ -1835,9 +1920,10 @@ impl Engine { })?, offset, ) - } else if let Some(index) = index.checked_abs() { - let offset = index as usize; + } else if let Some(abs_index) = index.checked_abs() { + let offset = abs_index as usize; ( + // Count from end if negative s.chars().rev().nth(offset - 1).ok_or_else(|| { let chars_len = s.chars().count(); EvalAltResult::ErrorStringBounds(chars_len, index, idx_pos) @@ -2153,8 +2239,8 @@ impl Engine { op_pos: Position, mut target: Target, root: (&str, Position), - mut new_value: Dynamic, - new_value_pos: Position, + mut new_val: Dynamic, + new_val_pos: Position, ) -> Result<(), Box> { if target.is_read_only() { // Assignment to constant variable @@ -2167,48 +2253,51 @@ impl Engine { op, }) = op_info { - let mut lock_guard; - let lhs_ptr_inner; + { + let mut lock_guard; + let lhs_ptr_inner; - #[cfg(not(feature = "no_closure"))] - let target_is_shared = target.is_shared(); - #[cfg(feature = "no_closure")] - let target_is_shared = false; + #[cfg(not(feature = "no_closure"))] + let target_is_shared = target.is_shared(); + #[cfg(feature = "no_closure")] + let target_is_shared = false; - if target_is_shared { - lock_guard = target - .write_lock::() - .expect("never fails when casting to `Dynamic`"); - lhs_ptr_inner = &mut *lock_guard; - } else { - lhs_ptr_inner = &mut *target; - } - - let hash = hash_op_assign; - let args = &mut [lhs_ptr_inner, &mut new_value]; - - match self.call_native_fn(mods, state, lib, op, hash, args, true, true, op_pos) { - Ok(_) => (), - Err(err) if matches!(err.as_ref(), EvalAltResult::ErrorFunctionNotFound(f, _) if f.starts_with(op)) => - { - // Expand to `var = var op rhs` - let op = &op[..op.len() - 1]; // extract operator without = - - // Run function - let (value, _) = self - .call_native_fn(mods, state, lib, op, hash_op, args, true, false, op_pos)?; - - *args[0] = value.flatten(); + if target_is_shared { + lock_guard = target + .write_lock::() + .expect("never fails when casting to `Dynamic`"); + lhs_ptr_inner = &mut *lock_guard; + } else { + lhs_ptr_inner = &mut *target; + } + + let hash = hash_op_assign; + let args = &mut [lhs_ptr_inner, &mut new_val]; + + match self.call_native_fn(mods, state, lib, op, hash, args, true, true, op_pos) { + Err(err) if matches!(err.as_ref(), EvalAltResult::ErrorFunctionNotFound(f, _) if f.starts_with(op)) => + { + // Expand to `var = var op rhs` + let op = &op[..op.len() - 1]; // extract operator without = + + // Run function + let (value, _) = self.call_native_fn( + mods, state, lib, op, hash_op, args, true, false, op_pos, + )?; + + *args[0] = value.flatten(); + } + err => return err.map(|_| ()), } - err => return err.map(|_| ()), } - Ok(()) + target.propagate_changed_value()?; } else { // Normal assignment - target.set_value(new_value, new_value_pos)?; - Ok(()) + target.set_value(new_val, new_val_pos)?; } + + Ok(()) } /// Evaluate a statement. diff --git a/src/fn_call.rs b/src/fn_call.rs index 88e42578..27cc2116 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -1400,6 +1400,7 @@ impl Engine { arg_values[0] = target.take_or_clone().flatten(); args = arg_values.iter_mut().collect(); } else { + // Turn it into a method call only if the object is not shared and not a simple value let (first, rest) = arg_values .split_first_mut() .expect("never fails because the arguments list is not empty"); diff --git a/src/optimize.rs b/src/optimize.rs index 90fae768..d78f5ca4 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -202,7 +202,7 @@ fn optimize_stmt_block( match stmt { // Add constant literals into the state Stmt::Const(value_expr, x, _, _) => { - optimize_expr(value_expr, state); + optimize_expr(value_expr, state, false); if value_expr.is_constant() { state.push_var( @@ -214,7 +214,7 @@ fn optimize_stmt_block( } // Add variables into the state Stmt::Let(value_expr, x, _, _) => { - optimize_expr(value_expr, state); + optimize_expr(value_expr, state, false); state.push_var(&x.name, AccessMode::ReadWrite, None); } // Optimize the statement @@ -392,10 +392,10 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { // expr op= expr Stmt::Assignment(x, _) => match x.0 { - Expr::Variable(_, _, _) => optimize_expr(&mut x.2, state), + Expr::Variable(_, _, _) => optimize_expr(&mut x.2, state, false), _ => { - optimize_expr(&mut x.0, state); - optimize_expr(&mut x.2, state); + optimize_expr(&mut x.0, state, false); + optimize_expr(&mut x.2, state, false); } }, @@ -405,7 +405,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { let pos = condition.position(); let mut expr = mem::take(condition); - optimize_expr(&mut expr, state); + optimize_expr(&mut expr, state, false); *stmt = if preserve_result { // -> { expr, Noop } @@ -440,7 +440,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { } // if expr { if_block } else { else_block } Stmt::If(condition, x, _) => { - optimize_expr(condition, state); + optimize_expr(condition, state, false); let if_block = mem::take(x.0.statements()).into_vec(); *x.0.statements() = optimize_stmt_block(if_block, state, preserve_result, true, false).into(); @@ -462,7 +462,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { if let Some(block) = table.get_mut(&hash) { if let Some(mut condition) = mem::take(&mut block.0) { // switch const { case if condition => stmt, _ => def } => if condition { stmt } else { def } - optimize_expr(&mut condition, state); + optimize_expr(&mut condition, state, false); let def_block = mem::take(&mut *x.1).into_vec(); let def_stmt = optimize_stmt_block(def_block, state, true, true, false); @@ -502,12 +502,12 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { } // switch Stmt::Switch(match_expr, x, _) => { - optimize_expr(match_expr, state); + optimize_expr(match_expr, state, false); x.0.values_mut().for_each(|block| { let condition = mem::take(&mut block.0).map_or_else( || Expr::Unit(Position::NONE), |mut condition| { - optimize_expr(&mut condition, state); + optimize_expr(&mut condition, state, false); condition }, ); @@ -550,7 +550,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { } // while expr { block } Stmt::While(condition, body, _) => { - optimize_expr(condition, state); + optimize_expr(condition, state, false); let block = mem::take(body.statements()).into_vec(); *body.statements() = optimize_stmt_block(block, state, false, true, false).into(); @@ -587,21 +587,21 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { } // do { block } while|until expr Stmt::Do(body, condition, _, _) => { - optimize_expr(condition, state); + optimize_expr(condition, state, false); let block = mem::take(body.statements()).into_vec(); *body.statements() = optimize_stmt_block(block, state, false, true, false).into(); } // for id in expr { block } Stmt::For(iterable, x, _) => { - optimize_expr(iterable, state); + optimize_expr(iterable, state, false); let body = mem::take(x.1.statements()).into_vec(); *x.1.statements() = optimize_stmt_block(body, state, false, true, false).into(); } // let id = expr; - Stmt::Let(expr, _, _, _) => optimize_expr(expr, state), + Stmt::Let(expr, _, _, _) => optimize_expr(expr, state, false), // import expr as var; #[cfg(not(feature = "no_module"))] - Stmt::Import(expr, _, _) => optimize_expr(expr, state), + Stmt::Import(expr, _, _) => optimize_expr(expr, state, false), // { block } Stmt::Block(statements, pos) => { let statements = mem::take(statements).into_vec(); @@ -640,7 +640,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { } // func(...) Stmt::Expr(expr @ Expr::FnCall(_, _)) => { - optimize_expr(expr, state); + optimize_expr(expr, state, false); match expr { Expr::FnCall(x, pos) => { state.set_dirty(); @@ -660,9 +660,9 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { *stmt = mem::take(x.as_mut()).into(); } // expr; - Stmt::Expr(expr) => optimize_expr(expr, state), + Stmt::Expr(expr) => optimize_expr(expr, state, false), // return expr; - Stmt::Return(_, Some(ref mut expr), _) => optimize_expr(expr, state), + Stmt::Return(_, Some(ref mut expr), _) => optimize_expr(expr, state, false), // All other statements - skip _ => (), @@ -670,7 +670,7 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut State, preserve_result: bool) { } /// Optimize an [expression][Expr]. -fn optimize_expr(expr: &mut Expr, state: &mut State) { +fn optimize_expr(expr: &mut Expr, state: &mut State, indexing: bool) { // These keywords are handled specially const DONT_EVAL_KEYWORDS: &[&str] = &[ KEYWORD_PRINT, // side effects @@ -693,7 +693,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { } // lhs.rhs #[cfg(not(feature = "no_object"))] - Expr::Dot(x, _) => match (&mut x.lhs, &mut x.rhs) { + Expr::Dot(x, _) if !indexing => match (&mut x.lhs, &mut x.rhs) { // map.string (Expr::Map(m, pos), Expr::Property(p)) if m.0.iter().all(|(_, x)| x.is_pure()) => { let prop = p.2.0.as_str(); @@ -705,14 +705,17 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { .unwrap_or_else(|| Expr::Unit(*pos)); } // var.rhs - (Expr::Variable(_, _, _), rhs) => optimize_expr(rhs, state), + (Expr::Variable(_, _, _), rhs) => optimize_expr(rhs, state, true), // lhs.rhs - (lhs, rhs) => { optimize_expr(lhs, state); optimize_expr(rhs, state); } + (lhs, rhs) => { optimize_expr(lhs, state, false); optimize_expr(rhs, state, true); } } + // ....lhs.rhs + #[cfg(not(feature = "no_object"))] + Expr::Dot(x, _) => { optimize_expr(&mut x.lhs, state, false); optimize_expr(&mut x.rhs, state, indexing); } // lhs[rhs] #[cfg(not(feature = "no_index"))] - Expr::Index(x, _) => match (&mut x.lhs, &mut x.rhs) { + Expr::Index(x, _) if !indexing => match (&mut x.lhs, &mut x.rhs) { // array[int] (Expr::Array(a, pos), Expr::IntegerConstant(i, _)) if *i >= 0 && (*i as usize) < a.len() && a.iter().all(Expr::is_pure) => @@ -744,6 +747,18 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { .map(|(_, mut expr)| { expr.set_position(*pos); expr }) .unwrap_or_else(|| Expr::Unit(*pos)); } + // int[int] + (Expr::IntegerConstant(n, pos), Expr::IntegerConstant(i, _)) if *i >= 0 && (*i as usize) < (std::mem::size_of_val(n) * 8) => { + // Bit-field literal indexing - get the bit + state.set_dirty(); + *expr = Expr::BoolConstant((*n & (1 << (*i as usize))) != 0, *pos); + } + // int[-int] + (Expr::IntegerConstant(n, pos), Expr::IntegerConstant(i, _)) if *i < 0 && i.checked_abs().map(|i| i as usize <= (std::mem::size_of_val(n) * 8)).unwrap_or(false) => { + // Bit-field literal indexing - get the bit + state.set_dirty(); + *expr = Expr::BoolConstant((*n & (1 << (std::mem::size_of_val(n) * 8 - i.abs() as usize))) != 0, *pos); + } // string[int] (Expr::StringConstant(s, pos), Expr::IntegerConstant(i, _)) if *i >= 0 && (*i as usize) < s.chars().count() => { // String literal indexing - get the character @@ -757,10 +772,13 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { *expr = Expr::CharConstant(s.chars().rev().nth(i.abs() as usize - 1).unwrap(), *pos); } // var[rhs] - (Expr::Variable(_, _, _), rhs) => optimize_expr(rhs, state), + (Expr::Variable(_, _, _), rhs) => optimize_expr(rhs, state, true), // lhs[rhs] - (lhs, rhs) => { optimize_expr(lhs, state); optimize_expr(rhs, state); } + (lhs, rhs) => { optimize_expr(lhs, state, false); optimize_expr(rhs, state, true); } }, + // ...[lhs][rhs] + #[cfg(not(feature = "no_index"))] + Expr::Index(x, _) => { optimize_expr(&mut x.lhs, state, false); optimize_expr(&mut x.rhs, state, indexing); } // `` Expr::InterpolatedString(x) if x.is_empty() => { state.set_dirty(); @@ -773,7 +791,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { } // `... ${ ... } ...` Expr::InterpolatedString(x) => { - x.iter_mut().for_each(|expr| optimize_expr(expr, state)); + x.iter_mut().for_each(|expr| optimize_expr(expr, state, false)); let mut n= 0; @@ -824,7 +842,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { } // [ items .. ] #[cfg(not(feature = "no_index"))] - Expr::Array(x, _) => x.iter_mut().for_each(|expr| optimize_expr(expr, state)), + Expr::Array(x, _) => x.iter_mut().for_each(|expr| optimize_expr(expr, state, false)), // #{ key:constant, .. } #[cfg(not(feature = "no_object"))] Expr::Map(_, _) if expr.is_constant() => { @@ -833,13 +851,13 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { } // #{ key:value, .. } #[cfg(not(feature = "no_object"))] - Expr::Map(x, _) => x.0.iter_mut().for_each(|(_, expr)| optimize_expr(expr, state)), + Expr::Map(x, _) => x.0.iter_mut().for_each(|(_, expr)| optimize_expr(expr, state, false)), // lhs && rhs Expr::And(x, _) => match (&mut x.lhs, &mut x.rhs) { // true && rhs -> rhs (Expr::BoolConstant(true, _), rhs) => { state.set_dirty(); - optimize_expr(rhs, state); + optimize_expr(rhs, state, false); *expr = mem::take(rhs); } // false && rhs -> false @@ -850,18 +868,18 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { // lhs && true -> lhs (lhs, Expr::BoolConstant(true, _)) => { state.set_dirty(); - optimize_expr(lhs, state); + optimize_expr(lhs, state, false); *expr = mem::take(lhs); } // lhs && rhs - (lhs, rhs) => { optimize_expr(lhs, state); optimize_expr(rhs, state); } + (lhs, rhs) => { optimize_expr(lhs, state, false); optimize_expr(rhs, state, false); } }, // lhs || rhs Expr::Or(ref mut x, _) => match (&mut x.lhs, &mut x.rhs) { // false || rhs -> rhs (Expr::BoolConstant(false, _), rhs) => { state.set_dirty(); - optimize_expr(rhs, state); + optimize_expr(rhs, state, false); *expr = mem::take(rhs); } // true || rhs -> true @@ -872,11 +890,11 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { // lhs || false (lhs, Expr::BoolConstant(false, _)) => { state.set_dirty(); - optimize_expr(lhs, state); + optimize_expr(lhs, state, false); *expr = mem::take(lhs); } // lhs || rhs - (lhs, rhs) => { optimize_expr(lhs, state); optimize_expr(rhs, state); } + (lhs, rhs) => { optimize_expr(lhs, state, false); optimize_expr(rhs, state, false); } }, // eval! @@ -899,7 +917,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { // Do not call some special keywords Expr::FnCall(x, _) if DONT_EVAL_KEYWORDS.contains(&x.name.as_ref()) => { - x.args.iter_mut().for_each(|a| optimize_expr(a, state)); + x.args.iter_mut().for_each(|a| optimize_expr(a, state, false)); } // Call built-in operators @@ -933,7 +951,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { } } - x.args.iter_mut().for_each(|a| optimize_expr(a, state)); + x.args.iter_mut().for_each(|a| optimize_expr(a, state, false)); // Move constant arguments to the right while x.args.last().map(Expr::is_constant).unwrap_or(false) { @@ -989,12 +1007,12 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { } } - x.args.iter_mut().for_each(|a| optimize_expr(a, state)); + x.args.iter_mut().for_each(|a| optimize_expr(a, state, false)); } // id(args ..) -> optimize function call arguments Expr::FnCall(x, _) => { - x.args.iter_mut().for_each(|a| optimize_expr(a, state)); + x.args.iter_mut().for_each(|a| optimize_expr(a, state, false)); // Move constant arguments to the right while x.args.last().map(Expr::is_constant).unwrap_or(false) { @@ -1021,7 +1039,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) { if x.scope_changed { state.propagate_constants = false; } - x.keywords.iter_mut().for_each(|expr| optimize_expr(expr, state)); + x.keywords.iter_mut().for_each(|expr| optimize_expr(expr, state, false)); } // All other expressions - skip diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 30280c42..9dd26425 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -1,5 +1,5 @@ use crate::dynamic::Variant; -use crate::{def_package, EvalAltResult, INT}; +use crate::{def_package, EvalAltResult, Position, INT}; use std::ops::Range; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -29,9 +29,9 @@ where Default::default(), Box::new(EvalAltResult::ErrorArithmetic( "step value cannot be zero".to_string(), - crate::Position::NONE, + Position::NONE, )), - crate::Position::NONE, + Position::NONE, ) .into(); } @@ -126,6 +126,68 @@ where } } +// Register range function with step +#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] +struct BitRange(INT, INT, usize); + +const BITS: usize = std::mem::size_of::() * 8; + +impl BitRange { + pub fn new(value: INT, from: INT, len: INT) -> Result> { + let from = if from >= 0 { + let offset = from as usize; + + #[cfg(not(feature = "unchecked"))] + if offset >= BITS { + return EvalAltResult::ErrorBitFieldBounds(BITS, from, Position::NONE).into(); + } + offset + } else { + #[cfg(not(feature = "unchecked"))] + if let Some(abs_from) = from.checked_abs() { + if (abs_from as usize) > BITS { + return EvalAltResult::ErrorBitFieldBounds(BITS, from, Position::NONE).into(); + } + BITS - (abs_from as usize) + } else { + return EvalAltResult::ErrorBitFieldBounds(BITS, from, Position::NONE).into(); + } + + #[cfg(feature = "unchecked")] + { + BITS - (from.abs() as usize) + } + }; + + let len = if len < 0 { + 0 + } else if from + (len as usize) > BITS { + BITS - from + } else { + len as usize + }; + + Ok(Self(value, 1 << from, len)) + } +} + +impl Iterator for BitRange { + type Item = bool; + + fn next(&mut self) -> Option { + let Self(value, mask, len) = *self; + + if len == 0 { + None + } else { + let r = (value & mask) != 0; + self.1 <<= 1; + self.2 -= 1; + Some(r) + } + } +} + macro_rules! reg_range { ($lib:ident | $x:expr => $( $y:ty ),*) => { $( @@ -193,8 +255,8 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { #[cfg(not(feature = "unchecked"))] if step == 0.0 { return EvalAltResult::ErrorInFunctionCall("range".to_string(), "".to_string(), - Box::new(EvalAltResult::ErrorArithmetic("step value cannot be zero".to_string(), crate::Position::NONE)), - crate::Position::NONE, + Box::new(EvalAltResult::ErrorArithmetic("step value cannot be zero".to_string(), Position::NONE)), + Position::NONE, ).into(); } @@ -255,8 +317,8 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { #[cfg(not(feature = "unchecked"))] if step.is_zero() { return EvalAltResult::ErrorInFunctionCall("range".to_string(), "".to_string(), - Box::new(EvalAltResult::ErrorArithmetic("step value cannot be zero".to_string(), crate::Position::NONE)), - crate::Position::NONE, + Box::new(EvalAltResult::ErrorArithmetic("step value cannot be zero".to_string(), Position::NONE)), + Position::NONE, ).into(); } @@ -304,4 +366,18 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { #[cfg(feature = "metadata")] lib.update_fn_metadata(_hash, &["from: Decimal", "to: Decimal", "step: Decimal", "Iterator"]); } + + lib.set_iterator::(); + + let _hash = lib.set_native_fn("bits", |value, from, len| BitRange::new(value, from, len)); + #[cfg(feature = "metadata")] + lib.update_fn_metadata(_hash, &["value: INT", "from: Decimal", "len: Decimal", "Iterator"]); + + let _hash = lib.set_native_fn("bits", |value, from| BitRange::new(value, from, INT::MAX)); + #[cfg(feature = "metadata")] + lib.update_fn_metadata(_hash, &["value: INT", "from: Decimal", "Iterator"]); + + let _hash = lib.set_native_fn("bits", |value| BitRange::new(value, 0, INT::MAX)); + #[cfg(feature = "metadata")] + lib.update_fn_metadata(_hash, &["value: INT", "Iterator"]); }); diff --git a/src/packages/logic.rs b/src/packages/logic.rs index 56339aa4..a1fd8d8f 100644 --- a/src/packages/logic.rs +++ b/src/packages/logic.rs @@ -1,7 +1,7 @@ #![allow(non_snake_case)] -use crate::def_package; use crate::plugin::*; +use crate::{def_package, EvalAltResult, INT}; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -59,6 +59,8 @@ def_package!(crate:LogicPackage:"Logical operators.", lib, { } set_exported_fn!(lib, "!", not); + + combine_with_exported_module!(lib, "bit_field", bit_field_functions); }); // Logic operators @@ -87,8 +89,6 @@ gen_cmp_functions!(float => f64); #[cfg(not(feature = "no_float"))] #[export_module] mod f32_functions { - use crate::INT; - #[rhai_fn(name = "==")] pub fn eq_if(x: INT, y: f32) -> bool { (x as f32) == (y as f32) @@ -142,8 +142,6 @@ mod f32_functions { #[cfg(not(feature = "no_float"))] #[export_module] mod f64_functions { - use crate::INT; - #[rhai_fn(name = "==")] pub fn eq_if(x: INT, y: f64) -> bool { (x as f64) == (y as f64) @@ -193,3 +191,159 @@ mod f64_functions { (x as f64) <= (y as f64) } } + +#[export_module] +mod bit_field_functions { + const BITS: usize = std::mem::size_of::() * 8; + + #[rhai_fn(return_raw)] + pub fn get_bit(value: INT, index: INT) -> Result> { + if index >= 0 { + let offset = index as usize; + + if offset >= BITS { + EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into() + } else { + Ok((value & (1 << offset)) != 0) + } + } else if let Some(abs_index) = index.checked_abs() { + let offset = abs_index as usize; + + // Count from end if negative + if offset > BITS { + EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into() + } else { + Ok((value & (1 << (BITS - offset))) != 0) + } + } else { + EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into() + } + } + #[rhai_fn(return_raw)] + pub fn set_bit(value: &mut INT, index: INT, new_value: bool) -> Result<(), Box> { + if index >= 0 { + let offset = index as usize; + + if offset >= BITS { + EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into() + } else { + let mask = 1 << offset; + if new_value { + *value |= mask; + } else { + *value &= !mask; + } + Ok(()) + } + } else if let Some(abs_index) = index.checked_abs() { + let offset = abs_index as usize; + + // Count from end if negative + if offset > BITS { + EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into() + } else { + let mask = 1 << offset; + if new_value { + *value |= mask; + } else { + *value &= !mask; + } + Ok(()) + } + } else { + EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into() + } + } + #[rhai_fn(return_raw)] + pub fn get_bits(value: INT, index: INT, bits: INT) -> Result> { + if bits < 1 { + return Ok(0); + } + + let offset = if index >= 0 { + let offset = index as usize; + + if offset >= BITS { + return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into(); + } + + offset + } else if let Some(abs_index) = index.checked_abs() { + let offset = abs_index as usize; + + // Count from end if negative + if offset > BITS { + return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into(); + } + BITS - offset + } else { + return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into(); + }; + + let bits = if offset + bits as usize > BITS { + BITS - offset + } else { + bits as usize + }; + + let mut base = 1; + let mut mask = 0; + + for _ in 0..bits { + mask |= base; + base <<= 1; + } + + Ok(((value & (mask << index)) >> index) & mask) + } + #[rhai_fn(return_raw)] + pub fn set_bits( + value: &mut INT, + index: INT, + bits: INT, + new_value: INT, + ) -> Result<(), Box> { + if bits < 1 { + return Ok(()); + } + + let offset = if index >= 0 { + let offset = index as usize; + + if offset >= BITS { + return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into(); + } + + offset + } else if let Some(abs_index) = index.checked_abs() { + let offset = abs_index as usize; + + // Count from end if negative + if offset > BITS { + return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into(); + } + BITS - offset + } else { + return EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into(); + }; + + let bits = if offset + bits as usize > BITS { + BITS - offset + } else { + bits as usize + }; + + let mut base = 1; + let mut mask = 0; + + for _ in 0..bits { + mask |= base; + base <<= 1; + } + + *value &= !(mask << index); + *value |= (new_value & mask) << index; + + Ok(()) + } +} diff --git a/src/parser.rs b/src/parser.rs index 6ea22399..69084efd 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -455,7 +455,10 @@ fn parse_index_chain( // Check type of indexing - must be integer or string match &idx_expr { Expr::IntegerConstant(_, pos) => match lhs { - Expr::Array(_, _) | Expr::StringConstant(_, _) | Expr::InterpolatedString(_) => (), + Expr::IntegerConstant(_, _) + | Expr::Array(_, _) + | Expr::StringConstant(_, _) + | Expr::InterpolatedString(_) => (), Expr::Map(_, _) => { return Err(PERR::MalformedIndexExpr( diff --git a/src/result.rs b/src/result.rs index 172e9834..51d692fc 100644 --- a/src/result.rs +++ b/src/result.rs @@ -56,6 +56,9 @@ pub enum EvalAltResult { /// String indexing out-of-bounds. /// Wrapped values are the current number of characters in the string and the index number. ErrorStringBounds(usize, INT, Position), + /// Bit-field indexing out-of-bounds. + /// Wrapped values are the current number of bits in the bit-field and the index number. + ErrorBitFieldBounds(usize, INT, Position), /// Trying to index into a type that is not an array, an object map, or a string, and has no /// indexer function defined. Wrapped value is the type name. ErrorIndexingType(String, Position), @@ -109,6 +112,7 @@ impl EvalAltResult { Self::ErrorArrayBounds(_, _, _) => "Array index out of bounds", Self::ErrorStringBounds(0, _, _) => "Empty string has nothing to index", Self::ErrorStringBounds(_, _, _) => "String index out of bounds", + Self::ErrorBitFieldBounds(_, _, _) => "Bit-field index out of bounds", Self::ErrorFor(_) => "For loop expects an array, object map, or range", Self::ErrorVariableNotFound(_, _) => "Variable not found", Self::ErrorModuleNotFound(_, _) => "Module not found", @@ -233,6 +237,11 @@ impl fmt::Display for EvalAltResult { "String index {} out of bounds: only {} characters in the string", index, max )?, + Self::ErrorBitFieldBounds(max, index, _) => write!( + f, + "Bit-field index {} out of bounds: only {} bits in the bit-field", + index, max + )?, Self::ErrorDataTooLarge(typ, _) => write!(f, "{} exceeds maximum limit", typ)?, } @@ -289,6 +298,7 @@ impl EvalAltResult { | Self::ErrorMismatchDataType(_, _, _) | Self::ErrorArrayBounds(_, _, _) | Self::ErrorStringBounds(_, _, _) + | Self::ErrorBitFieldBounds(_, _, _) | Self::ErrorIndexingType(_, _) | Self::ErrorFor(_) | Self::ErrorVariableNotFound(_, _) @@ -372,7 +382,9 @@ impl EvalAltResult { map.insert("requested".into(), r.into()); map.insert("actual".into(), a.into()); } - Self::ErrorArrayBounds(n, i, _) | Self::ErrorStringBounds(n, i, _) => { + Self::ErrorArrayBounds(n, i, _) + | Self::ErrorStringBounds(n, i, _) + | Self::ErrorBitFieldBounds(n, i, _) => { map.insert("length".into(), (*n as INT).into()); map.insert("index".into(), (*i as INT).into()); } @@ -412,6 +424,7 @@ impl EvalAltResult { | Self::ErrorMismatchDataType(_, _, pos) | Self::ErrorArrayBounds(_, _, pos) | Self::ErrorStringBounds(_, _, pos) + | Self::ErrorBitFieldBounds(_, _, pos) | Self::ErrorIndexingType(_, pos) | Self::ErrorFor(pos) | Self::ErrorVariableNotFound(_, pos) @@ -452,6 +465,7 @@ impl EvalAltResult { | Self::ErrorMismatchDataType(_, _, pos) | Self::ErrorArrayBounds(_, _, pos) | Self::ErrorStringBounds(_, _, pos) + | Self::ErrorBitFieldBounds(_, _, pos) | Self::ErrorIndexingType(_, pos) | Self::ErrorFor(pos) | Self::ErrorVariableNotFound(_, pos) diff --git a/tests/bit_shift.rs b/tests/bit_shift.rs index 0b888e32..c35b33d5 100644 --- a/tests/bit_shift.rs +++ b/tests/bit_shift.rs @@ -13,3 +13,38 @@ fn test_right_shift() -> Result<(), Box> { assert_eq!(engine.eval::("9 >> 1")?, 4); Ok(()) } + +#[cfg(not(feature = "no_index"))] +#[test] +fn test_bit_fields() -> Result<(), Box> { + let engine = Engine::new(); + assert!(!engine.eval::("let x = 10; x[0]")?); + assert!(engine.eval::("let x = 10; x[1]")?); + assert!(!engine.eval::("let x = 10; x[-1]")?); + assert_eq!( + engine.eval::("let x = 10; x[0] = true; x[1] = false; x")?, + 9 + ); + assert_eq!(engine.eval::("let x = 10; get_bits(x, 1, 3)")?, 5); + assert_eq!( + engine.eval::("let x = 10; set_bits(x, 1, 3, 7); x")?, + 14 + ); + assert_eq!( + engine.eval::( + " + let x = 0b001101101010001; + let count = 0; + + for b in bits(x, 2, 10) { + if b { count += 1; } + } + + count + " + )?, + 5 + ); + + Ok(()) +}