mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 07:14:46 +00:00
Wasm: Fix section parsing bug
This commit is contained in:
parent
3067358a33
commit
f9fbe461d1
2 changed files with 26 additions and 9 deletions
|
@ -760,11 +760,10 @@ mod tests {
|
||||||
let mut original_serialized = Vec::with_capacity_in(6 + original.bytes.len(), arena);
|
let mut original_serialized = Vec::with_capacity_in(6 + original.bytes.len(), arena);
|
||||||
original.serialize(&mut original_serialized);
|
original.serialize(&mut original_serialized);
|
||||||
|
|
||||||
debug_assert!(original_serialized[0] == SectionId::Type as u8);
|
// Reconstruct a new TypeSection by "pre-loading" the bytes of the original
|
||||||
|
let mut cursor = 0;
|
||||||
// Reconstruct a new TypeSection by "pre-loading" the bytes of the original!
|
let mut preloaded = TypeSection::preload(arena, &original_serialized, &mut cursor);
|
||||||
let body = &original_serialized[6..];
|
preloaded.cache_offsets();
|
||||||
let preloaded = TypeSection::cache_offsets(arena, body);
|
|
||||||
|
|
||||||
debug_assert_eq!(original.offsets, preloaded.offsets);
|
debug_assert_eq!(original.offsets, preloaded.offsets);
|
||||||
debug_assert_eq!(original.bytes, preloaded.bytes);
|
debug_assert_eq!(original.bytes, preloaded.bytes);
|
||||||
|
|
|
@ -255,9 +255,8 @@ pub fn decode_u32_or_panic(bytes: &[u8]) -> (u32, usize) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_u32_or_panic(bytes: &[u8], cursor: &mut usize) -> u32 {
|
pub fn parse_u32_or_panic(bytes: &[u8], cursor: &mut usize) -> u32 {
|
||||||
let (value, new_offset) =
|
let (value, len) = decode_u32(&bytes[*cursor..]).unwrap_or_else(|e| internal_error!("{}", e));
|
||||||
decode_u32(&bytes[*cursor..]).unwrap_or_else(|e| internal_error!("{}", e));
|
*cursor += len;
|
||||||
*cursor = new_offset;
|
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -445,9 +444,28 @@ mod tests {
|
||||||
assert_eq!(decode_u32(&[0x80, 0x01]), Ok((0x80, 2)));
|
assert_eq!(decode_u32(&[0x80, 0x01]), Ok((0x80, 2)));
|
||||||
assert_eq!(decode_u32(&[0xff, 0x7f]), Ok((0x3fff, 2)));
|
assert_eq!(decode_u32(&[0xff, 0x7f]), Ok((0x3fff, 2)));
|
||||||
assert_eq!(decode_u32(&[0x80, 0x80, 0x01]), Ok((0x4000, 3)));
|
assert_eq!(decode_u32(&[0x80, 0x80, 0x01]), Ok((0x4000, 3)));
|
||||||
assert_eq!(decode_u32(&[0xff, 0xff, 0xff, 0xff, 0x0f]), Ok((u32::MAX, 5)));
|
assert_eq!(
|
||||||
|
decode_u32(&[0xff, 0xff, 0xff, 0xff, 0x0f]),
|
||||||
|
Ok((u32::MAX, 5))
|
||||||
|
);
|
||||||
assert!(matches!(decode_u32(&[0x80; 6]), Err(_)));
|
assert!(matches!(decode_u32(&[0x80; 6]), Err(_)));
|
||||||
assert!(matches!(decode_u32(&[0x80; 2]), Err(_)));
|
assert!(matches!(decode_u32(&[0x80; 2]), Err(_)));
|
||||||
assert!(matches!(decode_u32(&[]), Err(_)));
|
assert!(matches!(decode_u32(&[]), Err(_)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_u32_sequence() {
|
||||||
|
let bytes = &[0, 0x80, 0x01, 0xff, 0xff, 0xff, 0xff, 0x0f];
|
||||||
|
let expected = [0, 128, u32::MAX];
|
||||||
|
let mut cursor = 0;
|
||||||
|
|
||||||
|
assert_eq!(parse_u32_or_panic(bytes, &mut cursor), expected[0]);
|
||||||
|
assert_eq!(cursor, 1);
|
||||||
|
|
||||||
|
assert_eq!(parse_u32_or_panic(bytes, &mut cursor), expected[1]);
|
||||||
|
assert_eq!(cursor, 3);
|
||||||
|
|
||||||
|
assert_eq!(parse_u32_or_panic(bytes, &mut cursor), expected[2]);
|
||||||
|
assert_eq!(cursor, 8);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue