Fix bug in blob's.

This commit is contained in:
Stephen Chung 2021-12-07 19:03:04 +08:00
parent 5df2628eec
commit 4421f33b2c
4 changed files with 15 additions and 12 deletions

View File

@ -7,16 +7,12 @@ Version 1.3.0
Compiler requirement Compiler requirement
-------------------- --------------------
* Minimum compiler version is bumped to 1.51. * Minimum compiler version is now 1.51.
Bug fixes
---------
* BLOB's no longer panic when accessed with an out-of-bounds index.
New features New features
------------ ------------
* `BLOB` (essentially a byte array) is added as a supported primitive value type parallel to arrays.
* New options for `Engine` which allows disabling `if`-expressions, `switch`-expressions, statement expressions, anonymous functions and/or looping (i.e. `while`, `loop`, `do` and `for` statements): * New options for `Engine` which allows disabling `if`-expressions, `switch`-expressions, statement expressions, anonymous functions and/or looping (i.e. `while`, `loop`, `do` and `for` statements):
* `Engine::set_allow_if_expression` * `Engine::set_allow_if_expression`
* `Engine::set_allow_switch_expression` * `Engine::set_allow_switch_expression`
@ -30,7 +26,6 @@ Enhancements
* Added `into_array` and `into_typed_array` for `Dynamic`. * Added `into_array` and `into_typed_array` for `Dynamic`.
* Added `FnPtr::call` and `FnPtr::call_within_context` to simplify calling a function pointer. * Added `FnPtr::call` and `FnPtr::call_within_context` to simplify calling a function pointer.
* BLob's can now be deserialized (using `from_dynamic`) into `Vec<u8>` via [`serde_bytes`](https://crates.io/crates/serde_bytes).
* A function's hashes are included in its JSON metadata to assist in debugging. Each function's `hashBase` field in the JSON object should map directly to the pre-calculated hash in the function call. * A function's hashes are included in its JSON metadata to assist in debugging. Each function's `hashBase` field in the JSON object should map directly to the pre-calculated hash in the function call.
* `Expression` now derefs to `Expr`. * `Expression` now derefs to `Expr`.

View File

@ -637,7 +637,7 @@ impl<'a> Target<'a> {
let index = *index; let index = *index;
if index < value.len() { if index < value.len() {
value[index] = (new_byte & 0x000f) as u8; value[index] = (new_byte & 0x00ff) as u8;
} else { } else {
unreachable!("blob index out of bounds: {}", index); unreachable!("blob index out of bounds: {}", index);
} }

View File

@ -48,7 +48,7 @@ mod blob_functions {
} }
let mut blob = Blob::new(); let mut blob = Blob::new();
blob.resize(len, (value & 0x000f) as u8); blob.resize(len, (value & 0x00ff) as u8);
Ok(blob) Ok(blob)
} }
#[rhai_fn(name = "len", get = "len", pure)] #[rhai_fn(name = "len", get = "len", pure)]
@ -57,7 +57,7 @@ mod blob_functions {
} }
#[rhai_fn(name = "push", name = "+=")] #[rhai_fn(name = "push", name = "+=")]
pub fn push(blob: &mut Blob, item: INT) { pub fn push(blob: &mut Blob, item: INT) {
let item = (item & 0x000f) as u8; let item = (item & 0x00ff) as u8;
blob.push(item); blob.push(item);
} }
#[rhai_fn(name = "append", name = "+=")] #[rhai_fn(name = "append", name = "+=")]
@ -82,7 +82,7 @@ mod blob_functions {
blob blob
} }
pub fn insert(blob: &mut Blob, position: INT, item: INT) { pub fn insert(blob: &mut Blob, position: INT, item: INT) {
let item = (item & 0x000f) as u8; let item = (item & 0x00ff) as u8;
if blob.is_empty() { if blob.is_empty() {
blob.push(item); blob.push(item);
@ -113,7 +113,7 @@ mod blob_functions {
return Ok(()); return Ok(());
} }
let item = (item & 0x000f) as u8; let item = (item & 0x00ff) as u8;
let _ctx = ctx; let _ctx = ctx;
// Check if blob will be over max size limit // Check if blob will be over max size limit

View File

@ -63,6 +63,14 @@ fn test_blobs() -> Result<(), Box<EvalAltResult>> {
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x + x")?, engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x + x")?,
[1, 2, 3, 1, 2, 3] [1, 2, 3, 1, 2, 3]
); );
assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x += 999; x")?,
[1, 2, 3, 0xe7]
);
assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x[2] = 999; x")?,
[1, 2, 0xe7]
);
Ok(()) Ok(())
} }