Minor refactor.

This commit is contained in:
Stephen Chung 2022-03-09 09:25:55 +08:00
parent 89426f8b3a
commit 1e4abd012c
11 changed files with 86 additions and 74 deletions

View File

@ -293,7 +293,7 @@ impl Engine {
self self
} }
/// Register an type iterator for an iterable type with the [`Engine`]. /// Register an type iterator for an iterable type with the [`Engine`].
/// This is an advanced feature. /// This is an advanced API.
#[inline(always)] #[inline(always)]
pub fn register_iterator<T>(&mut self) -> &mut Self pub fn register_iterator<T>(&mut self) -> &mut Self
where where

View File

@ -18,14 +18,14 @@ bitflags! {
/// Exported under the `internals` feature only. /// Exported under the `internals` feature only.
pub struct ASTFlags: u8 { pub struct ASTFlags: u8 {
/// No options for the [`AST`][crate::AST] node. /// No options for the [`AST`][crate::AST] node.
const NONE = 0b0000_0000; const NONE = 0b_0000_0000;
/// The [`AST`][crate::AST] node is read-only. /// The [`AST`][crate::AST] node is read-only.
const CONSTANT = 0b0000_0001; const CONSTANT = 0b_0000_0001;
/// The [`AST`][crate::AST] node is exposed to the outside (i.e. public). /// The [`AST`][crate::AST] node is exposed to the outside (i.e. public).
const EXPORTED = 0b0000_0010; const EXPORTED = 0b_0000_0010;
/// The [`AST`][crate::AST] node is negated (i.e. whatever information is the opposite). /// The [`AST`][crate::AST] node is negated (i.e. whatever information is the opposite).
const NEGATED = 0b0000_0100; const NEGATED = 0b_0000_0100;
/// The [`AST`][crate::AST] node breaks out of normal control flow. /// The [`AST`][crate::AST] node breaks out of normal control flow.
const BREAK = 0b0000_1000; const BREAK = 0b_0000_1000;
} }
} }

View File

@ -621,7 +621,7 @@ impl Stmt {
/// upper block. /// upper block.
/// ///
/// Currently only variable definitions (i.e. `let` and `const`), `import`/`export` statements, /// Currently only variable definitions (i.e. `let` and `const`), `import`/`export` statements,
/// and `eval` calls (which may in turn call define variables) fall under this category. /// and `eval` calls (which may in turn define variables) fall under this category.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn is_block_dependent(&self) -> bool { pub fn is_block_dependent(&self) -> bool {

View File

@ -922,22 +922,20 @@ impl Engine {
if idx.is::<crate::ExclusiveRange>() || idx.is::<crate::InclusiveRange>() => if idx.is::<crate::ExclusiveRange>() || idx.is::<crate::InclusiveRange>() =>
{ {
// val_int[range] // val_int[range]
const BITS: usize = std::mem::size_of::<crate::INT>() * 8;
let (shift, mask) = if let Some(range) = idx.read_lock::<crate::ExclusiveRange>() { let (shift, mask) = if let Some(range) = idx.read_lock::<crate::ExclusiveRange>() {
let start = range.start; let start = range.start;
let end = range.end; let end = range.end;
let start = super::calc_index(BITS, start, false, || { let start = super::calc_index(crate::INT_BITS, start, false, || {
ERR::ErrorBitFieldBounds(BITS, start, idx_pos).into() ERR::ErrorBitFieldBounds(crate::INT_BITS, start, idx_pos).into()
})?; })?;
let end = super::calc_index(BITS, end, false, || { let end = super::calc_index(crate::INT_BITS, end, false, || {
ERR::ErrorBitFieldBounds(BITS, end, idx_pos).into() ERR::ErrorBitFieldBounds(crate::INT_BITS, end, idx_pos).into()
})?; })?;
if end <= start { if end <= start {
(0, 0) (0, 0)
} else if end == BITS && start == 0 { } else if end == crate::INT_BITS && start == 0 {
// -1 = all bits set // -1 = all bits set
(0, -1) (0, -1)
} else { } else {
@ -953,16 +951,16 @@ impl Engine {
let start = *range.start(); let start = *range.start();
let end = *range.end(); let end = *range.end();
let start = super::calc_index(BITS, start, false, || { let start = super::calc_index(crate::INT_BITS, start, false, || {
ERR::ErrorBitFieldBounds(BITS, start, idx_pos).into() ERR::ErrorBitFieldBounds(crate::INT_BITS, start, idx_pos).into()
})?; })?;
let end = super::calc_index(BITS, end, false, || { let end = super::calc_index(crate::INT_BITS, end, false, || {
ERR::ErrorBitFieldBounds(BITS, end, idx_pos).into() ERR::ErrorBitFieldBounds(crate::INT_BITS, end, idx_pos).into()
})?; })?;
if end < start { if end < start {
(0, 0) (0, 0)
} else if end == BITS - 1 && start == 0 { } else if end == crate::INT_BITS - 1 && start == 0 {
// -1 = all bits set // -1 = all bits set
(0, -1) (0, -1)
} else { } else {
@ -995,10 +993,8 @@ impl Engine {
.as_int() .as_int()
.map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, idx_pos))?; .map_err(|typ| self.make_type_mismatch_err::<crate::INT>(typ, idx_pos))?;
const BITS: usize = std::mem::size_of::<crate::INT>() * 8; let bit = super::calc_index(crate::INT_BITS, index, true, || {
ERR::ErrorBitFieldBounds(crate::INT_BITS, index, idx_pos).into()
let bit = super::calc_index(BITS, index, true, || {
ERR::ErrorBitFieldBounds(BITS, index, idx_pos).into()
})?; })?;
let bit_value = (*value & (1 << bit)) != 0; let bit_value = (*value & (1 << bit)) != 0;

View File

@ -121,6 +121,16 @@ type UNSIGNED_INT = u64;
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
type UNSIGNED_INT = u32; type UNSIGNED_INT = u32;
/// Number of bits in [`INT`].
///
/// It is 64 unless the `only_i32` feature is enabled when it will be 32.
const INT_BITS: usize = std::mem::size_of::<INT>() * 8;
/// Number of bytes that make up an [`INT`].
///
/// It is 8 unless the `only_i32` feature is enabled when it will be 4.
const INT_BYTES: usize = std::mem::size_of::<INT>();
/// The system floating-point type. It is defined as [`f64`]. /// The system floating-point type. It is defined as [`f64`].
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
@ -140,6 +150,12 @@ pub type FLOAT = f64;
#[cfg(feature = "f32_float")] #[cfg(feature = "f32_float")]
pub type FLOAT = f32; pub type FLOAT = f32;
/// Number of bytes that make up a [`FLOAT`].
///
/// It is 8 unless the `f32_float` feature is enabled when it will be 4.
#[cfg(not(feature = "no_float"))]
const FLOAT_BYTES: usize = std::mem::size_of::<FLOAT>();
/// An exclusive integer range. /// An exclusive integer range.
type ExclusiveRange = std::ops::Range<INT>; type ExclusiveRange = std::ops::Range<INT>;

View File

@ -10,7 +10,7 @@ use crate::tokenizer::{Span, Token};
use crate::types::dynamic::AccessMode; use crate::types::dynamic::AccessMode;
use crate::{ use crate::{
calc_fn_hash, calc_fn_params_hash, combine_hashes, Dynamic, Engine, FnPtr, Position, Scope, calc_fn_hash, calc_fn_params_hash, combine_hashes, Dynamic, Engine, FnPtr, Position, Scope,
StaticVec, AST, INT, StaticVec, AST, INT, INT_BITS,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -940,16 +940,16 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) {
.unwrap_or_else(|| Expr::Unit(*pos)); .unwrap_or_else(|| Expr::Unit(*pos));
} }
// int[int] // int[int]
(Expr::IntegerConstant(n, pos), Expr::IntegerConstant(i, ..)) if *i >= 0 && (*i as usize) < (std::mem::size_of_val(n) * 8) => { (Expr::IntegerConstant(n, pos), Expr::IntegerConstant(i, ..)) if *i >= 0 && (*i as usize) < INT_BITS => {
// Bit-field literal indexing - get the bit // Bit-field literal indexing - get the bit
state.set_dirty(); state.set_dirty();
*expr = Expr::BoolConstant((*n & (1 << (*i as usize))) != 0, *pos); *expr = Expr::BoolConstant((*n & (1 << (*i as usize))) != 0, *pos);
} }
// int[-int] // 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) => { (Expr::IntegerConstant(n, pos), Expr::IntegerConstant(i, ..)) if *i < 0 && i.checked_abs().map(|i| i as usize <= INT_BITS).unwrap_or(false) => {
// Bit-field literal indexing - get the bit // Bit-field literal indexing - get the bit
state.set_dirty(); state.set_dirty();
*expr = Expr::BoolConstant((*n & (1 << (std::mem::size_of_val(n) * 8 - i.abs() as usize))) != 0, *pos); *expr = Expr::BoolConstant((*n & (1 << (INT_BITS - i.abs() as usize))) != 0, *pos);
} }
// string[int] // string[int]
(Expr::StringConstant(s, pos), Expr::IntegerConstant(i, ..)) if *i >= 0 && (*i as usize) < s.chars().count() => { (Expr::StringConstant(s, pos), Expr::IntegerConstant(i, ..)) if *i >= 0 && (*i as usize) < s.chars().count() => {

View File

@ -1,7 +1,8 @@
use crate::eval::calc_index; use crate::eval::calc_index;
use crate::plugin::*; use crate::plugin::*;
use crate::{ use crate::{
def_package, ExclusiveRange, InclusiveRange, Position, RhaiResultOf, ERR, INT, UNSIGNED_INT, def_package, ExclusiveRange, InclusiveRange, Position, RhaiResultOf, ERR, INT, INT_BITS,
UNSIGNED_INT,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -17,8 +18,6 @@ def_package! {
#[export_module] #[export_module]
mod bit_field_functions { mod bit_field_functions {
const BITS: usize = std::mem::size_of::<INT>() * 8;
/// Return `true` if the specified `bit` in the number is set. /// Return `true` if the specified `bit` in the number is set.
/// ///
/// If `bit` < 0, position counts from the MSB (Most Significant Bit). /// If `bit` < 0, position counts from the MSB (Most Significant Bit).
@ -36,8 +35,8 @@ mod bit_field_functions {
/// ``` /// ```
#[rhai_fn(return_raw)] #[rhai_fn(return_raw)]
pub fn get_bit(value: INT, bit: INT) -> RhaiResultOf<bool> { pub fn get_bit(value: INT, bit: INT) -> RhaiResultOf<bool> {
let bit = calc_index(BITS, bit, true, || { let bit = calc_index(INT_BITS, bit, true, || {
ERR::ErrorBitFieldBounds(BITS, bit, Position::NONE).into() ERR::ErrorBitFieldBounds(INT_BITS, bit, Position::NONE).into()
})?; })?;
Ok((value & (1 << bit)) != 0) Ok((value & (1 << bit)) != 0)
@ -66,8 +65,8 @@ mod bit_field_functions {
/// ``` /// ```
#[rhai_fn(return_raw)] #[rhai_fn(return_raw)]
pub fn set_bit(value: &mut INT, bit: INT, new_value: bool) -> RhaiResultOf<()> { pub fn set_bit(value: &mut INT, bit: INT, new_value: bool) -> RhaiResultOf<()> {
let bit = calc_index(BITS, bit, true, || { let bit = calc_index(INT_BITS, bit, true, || {
ERR::ErrorBitFieldBounds(BITS, bit, Position::NONE).into() ERR::ErrorBitFieldBounds(INT_BITS, bit, Position::NONE).into()
})?; })?;
let mask = 1 << bit; let mask = 1 << bit;
@ -128,17 +127,17 @@ mod bit_field_functions {
return Ok(0); return Ok(0);
} }
let bit = calc_index(BITS, start, true, || { let bit = calc_index(INT_BITS, start, true, || {
ERR::ErrorBitFieldBounds(BITS, start, Position::NONE).into() ERR::ErrorBitFieldBounds(INT_BITS, start, Position::NONE).into()
})?; })?;
let bits = if bit + bits as usize > BITS { let bits = if bit + bits as usize > INT_BITS {
BITS - bit INT_BITS - bit
} else { } else {
bits as usize bits as usize
}; };
if bit == 0 && bits == BITS { if bit == 0 && bits == INT_BITS {
return Ok(value); return Ok(value);
} }
@ -214,17 +213,17 @@ mod bit_field_functions {
return Ok(()); return Ok(());
} }
let bit = calc_index(BITS, bit, true, || { let bit = calc_index(INT_BITS, bit, true, || {
ERR::ErrorBitFieldBounds(BITS, bit, Position::NONE).into() ERR::ErrorBitFieldBounds(INT_BITS, bit, Position::NONE).into()
})?; })?;
let bits = if bit + bits as usize > BITS { let bits = if bit + bits as usize > INT_BITS {
BITS - bit INT_BITS - bit
} else { } else {
bits as usize bits as usize
}; };
if bit == 0 && bits == BITS { if bit == 0 && bits == INT_BITS {
*value = new_value; *value = new_value;
return Ok(()); return Ok(());
} }

View File

@ -4,19 +4,14 @@ use crate::eval::{calc_index, calc_offset_len};
use crate::plugin::*; use crate::plugin::*;
use crate::{ use crate::{
def_package, Array, Blob, Dynamic, ExclusiveRange, InclusiveRange, NativeCallContext, Position, def_package, Array, Blob, Dynamic, ExclusiveRange, InclusiveRange, NativeCallContext, Position,
RhaiResultOf, INT, RhaiResultOf, INT, INT_BYTES,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{any::TypeId, mem}; use std::{any::TypeId, mem};
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
use crate::FLOAT; use crate::{FLOAT, FLOAT_BYTES};
const INT_BYTES: usize = mem::size_of::<INT>();
#[cfg(not(feature = "no_float"))]
const FLOAT_BYTES: usize = mem::size_of::<FLOAT>();
def_package! { def_package! {
/// Package of basic BLOB utilities. /// Package of basic BLOB utilities.

View File

@ -1,7 +1,7 @@
use crate::eval::calc_index; use crate::eval::calc_index;
use crate::plugin::*; use crate::plugin::*;
use crate::types::dynamic::Variant; use crate::types::dynamic::Variant;
use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, INT}; use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, INT, INT_BITS};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{ use std::{
@ -117,18 +117,16 @@ impl<T> FusedIterator for StepRange<T> where
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
struct BitRange(INT, INT, usize); struct BitRange(INT, INT, usize);
const BITS: usize = std::mem::size_of::<INT>() * 8;
impl BitRange { impl BitRange {
pub fn new(value: INT, from: INT, len: INT) -> RhaiResultOf<Self> { pub fn new(value: INT, from: INT, len: INT) -> RhaiResultOf<Self> {
let from = calc_index(BITS, from, true, || { let from = calc_index(INT_BITS, from, true, || {
crate::ERR::ErrorBitFieldBounds(BITS, from, Position::NONE).into() crate::ERR::ErrorBitFieldBounds(INT_BITS, from, Position::NONE).into()
})?; })?;
let len = if len < 0 { let len = if len < 0 {
0 0
} else if from + (len as usize) > BITS { } else if from + (len as usize) > INT_BITS {
BITS - from INT_BITS - from
} else { } else {
len as usize len as usize
}; };

View File

@ -31,6 +31,16 @@ mod time_functions {
} }
/// Return the number of seconds between the current system time and the timestamp. /// Return the number of seconds between the current system time and the timestamp.
///
/// # Example
///
/// ```rhai
/// let now = timestamp();
///
/// sleep(10.0); // sleep for 10 seconds
///
/// print(now.elapsed); // prints 10.???
/// ```
#[rhai_fn(name = "elapsed", get = "elapsed", return_raw)] #[rhai_fn(name = "elapsed", get = "elapsed", return_raw)]
pub fn elapsed(timestamp: Instant) -> RhaiResult { pub fn elapsed(timestamp: Instant) -> RhaiResult {
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]

View File

@ -9,66 +9,64 @@ fn test_blobs() -> Result<(), Box<EvalAltResult>> {
a.push(3); a.push(3);
let engine = Engine::new(); let engine = Engine::new();
let mut orig_scope = Scope::new(); let mut scope = Scope::new();
orig_scope.push("x", a); scope.push("x", a);
let mut scope = orig_scope.clone();
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[1]")?, 2); assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[1]")?, 2);
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[0]")?, 1); assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[0]")?, 1);
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[-1]")?, 3); assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[-1]")?, 3);
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[-3]")?, 1); assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[-3]")?, 1);
assert_eq!( assert_eq!(
engine.eval_with_scope::<INT>(&mut scope, "x += 4; x[3]")?, engine.eval_with_scope::<INT>(&mut scope.clone(), "x += 4; x[3]")?,
4 4
); );
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
{ {
assert_eq!( assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x.push(4); x")?, engine.eval_with_scope::<Blob>(&mut scope.clone(), "x.push(4); x")?,
[1, 2, 3, 4] [1, 2, 3, 4]
); );
assert_eq!( assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x.insert(0, 4); x")?, engine.eval_with_scope::<Blob>(&mut scope.clone(), "x.insert(0, 4); x")?,
[4, 1, 2, 3] [4, 1, 2, 3]
); );
assert_eq!( assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x.insert(999, 4); x")?, engine.eval_with_scope::<Blob>(&mut scope.clone(), "x.insert(999, 4); x")?,
[1, 2, 3, 4] [1, 2, 3, 4]
); );
assert_eq!( assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x.insert(-2, 4); x")?, engine.eval_with_scope::<Blob>(&mut scope.clone(), "x.insert(-2, 4); x")?,
[1, 4, 2, 3] [1, 4, 2, 3]
); );
assert_eq!( assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x.insert(-999, 4); x")?, engine.eval_with_scope::<Blob>(&mut scope.clone(), "x.insert(-999, 4); x")?,
[4, 1, 2, 3] [4, 1, 2, 3]
); );
assert_eq!( assert_eq!(
engine.eval_with_scope::<INT>(&mut orig_scope.clone(), "let z = [42]; x[z.len]")?, engine.eval_with_scope::<INT>(&mut scope.clone(), "let z = [42]; x[z.len]")?,
2 2
); );
assert_eq!( assert_eq!(
engine.eval_with_scope::<INT>(&mut orig_scope.clone(), "let z = [2]; x[z[0]]")?, engine.eval_with_scope::<INT>(&mut scope.clone(), "let z = [2]; x[z[0]]")?,
3 3
); );
} }
assert_eq!( assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x += x; x")?, engine.eval_with_scope::<Blob>(&mut scope.clone(), "x += x; x")?,
[1, 2, 3, 1, 2, 3] [1, 2, 3, 1, 2, 3]
); );
assert_eq!( assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x + x")?, engine.eval_with_scope::<Blob>(&mut scope.clone(), "x + x")?,
[1, 2, 3, 1, 2, 3] [1, 2, 3, 1, 2, 3]
); );
assert_eq!( assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x += 999; x")?, engine.eval_with_scope::<Blob>(&mut scope.clone(), "x += 999; x")?,
[1, 2, 3, 0xe7] [1, 2, 3, 0xe7]
); );
assert_eq!( assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x[2] = 999; x")?, engine.eval_with_scope::<Blob>(&mut scope.clone(), "x[2] = 999; x")?,
[1, 2, 0xe7] [1, 2, 0xe7]
); );