Skip to content

Commit

Permalink
make const tests pass
Browse files Browse the repository at this point in the history
with the help of some wabt upstream fixes: WebAssembly/wabt#2342
  • Loading branch information
rvagg committed Dec 1, 2023
1 parent 482f98d commit 176172c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 85 deletions.
16 changes: 8 additions & 8 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,10 @@ pub enum InstructionData {

// Numeric instructions¶ ----------------------------------------------------
I32Instruction {
value: i32,
value: u32,
},
I64Instruction {
value: i64,
value: u64,
},
F64Instruction {
value: f64,
Expand Down Expand Up @@ -1426,15 +1426,15 @@ pub fn get_codings() -> &'static Vec<InstructionCoding> {
Arc::new(
|bytes: &mut super::reader::Reader| -> Result<InstructionData, io::Error> {
Ok(InstructionData::I32Instruction {
value: bytes.read_vs32()?,
value: bytes.read_vs32()? as u32,
})
},
),
Arc::new(|data: &InstructionData| {
let mut bytes = vec![0x41];
if let InstructionData::I32Instruction { value } = &data {
let mut i32_bytes = reader::emit_vs32(*value);
bytes.append(&mut i32_bytes);
let mut u32_bytes = reader::emit_vs32(*value as i32);
bytes.append(&mut u32_bytes);
} else {
panic!("expected i32 instruction");
}
Expand All @@ -1456,15 +1456,15 @@ pub fn get_codings() -> &'static Vec<InstructionCoding> {
Arc::new(
|bytes: &mut super::reader::Reader| -> Result<InstructionData, io::Error> {
Ok(InstructionData::I64Instruction {
value: bytes.read_vs64()?,
value: bytes.read_vs64()? as u64,
})
},
),
Arc::new(|data: &InstructionData| {
let mut bytes = vec![0x42];
if let InstructionData::I64Instruction { value } = &data {
let mut i64_bytes = reader::emit_vs64(*value);
bytes.append(&mut i64_bytes);
let mut u64_bytes = reader::emit_vs64(*value as i64);
bytes.append(&mut u64_bytes);
} else {
panic!("expected i64 instruction");
}
Expand Down
1 change: 0 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod ast;
pub mod module;
pub mod reader;
mod validate;
mod fhex;

use std::io;
use std::u32;
Expand Down
30 changes: 11 additions & 19 deletions src/parser/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,8 @@ where
let max_bytes = ((size as f64) / 7_f64).ceil() as usize;

for i in 0..max_bytes {
let b = reader()? as u32;
let b = reader()? as u64;
let value = (b & 0x7f) << (7 * i);
if value > (u64::MAX - result) as u32 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"integer overflow",
));
}
result |= value as u64;
if (b & 0x80) == 0 {
break;
Expand All @@ -369,7 +363,7 @@ where
}

fn emit_vu(v: u64) -> Vec<u8> {
// signed leb128
// unsigned leb128
let mut result: Vec<u8> = vec![];
let mut value = v;
let mut more = true;
Expand Down Expand Up @@ -477,22 +471,21 @@ where
let mut result: i64 = 0;
let mut shift = 0;
let max_bytes = ((size as f64) / 7_f64).ceil() as usize;
let mut all_bytes_have_msb_set = true;
let mut b = 0;

for _ in 0..max_bytes {
let b = reader()? as u32;
b = reader()? as u32;
result |= ((b & !0x80) as i64) << shift;
shift += 7;
if (b & 0x80) == 0 {
all_bytes_have_msb_set = false;
}
result = result | (((b & 0x7f) as i64) << shift) as i64;
shift = shift + 7;
if !all_bytes_have_msb_set {
if (shift < size) && (b & 0x40) != 0 {
result = result | -((1 as i64) << shift) as i64;
}
break;
}
}

if (b & 0x40) == 0x40 && shift < size {
result |= !0 << shift;
}

Ok(result)
}

Expand Down Expand Up @@ -582,7 +575,6 @@ fn test_emit_vs64() {
);
}

#[test]
#[test]
fn test_read_vs32() {
let read = |v: Vec<u8>| {
Expand Down
Loading

0 comments on commit 176172c

Please sign in to comment.