wasm_module: create WasmModule::new for testing

This commit is contained in:
Brian Carroll 2022-11-21 18:24:49 +00:00
parent 2ca74e5070
commit 26cce05bbe
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0
5 changed files with 133 additions and 6 deletions

View file

@ -1,4 +1,4 @@
use super::serialize::MAX_SIZE_ENCODED_U32;
use super::serialize::{MAX_SIZE_ENCODED_U32, MAX_SIZE_ENCODED_U64};
use bumpalo::collections::vec::Vec;
use bumpalo::Bump;
@ -92,6 +92,43 @@ impl Parse<()> for i32 {
}
}
/// Decode a signed 64-bit integer from the provided buffer in LEB-128 format
/// Return the integer itself and the offset after it ends
fn decode_i64(bytes: &[u8]) -> Result<(i64, usize), ()> {
let mut value = 0;
let mut shift = 0;
for (i, byte) in bytes.iter().take(MAX_SIZE_ENCODED_U64).enumerate() {
value |= ((byte & 0x7f) as i64) << shift;
if (byte & 0x80) == 0 {
let is_negative = byte & 0x40 != 0;
if shift < MAX_SIZE_ENCODED_U64 && is_negative {
value |= -1 << shift;
}
return Ok((value, i + 1));
}
shift += 7;
}
Err(())
}
impl Parse<()> for i64 {
fn parse(_ctx: (), bytes: &[u8], cursor: &mut usize) -> Result<Self, ParseError> {
match decode_i64(&bytes[*cursor..]) {
Ok((value, len)) => {
*cursor += len;
Ok(value)
}
Err(()) => Err(ParseError {
offset: *cursor,
message: format!(
"Failed to decode i64 as LEB-128 from bytes: {:2x?}",
&bytes[*cursor..][..MAX_SIZE_ENCODED_U64]
),
}),
}
}
}
impl<'a> Parse<&'a Bump> for &'a str {
fn parse(arena: &'a Bump, bytes: &[u8], cursor: &mut usize) -> Result<Self, ParseError> {
let len = u32::parse((), bytes, cursor)?;