Skip to content

Commit

Permalink
Merge pull request #3477 from wasmerio/js-custom-sections
Browse files Browse the repository at this point in the history
Added support for Wasm Module custom sections in js
  • Loading branch information
syrusakbary authored Jan 12, 2023
2 parents aaa23c6 + 692bfab commit 945d917
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
19 changes: 8 additions & 11 deletions lib/api/src/js/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,17 +566,14 @@ impl Module {
/// custom sections. That's why an iterator (rather than one element)
/// is returned.
pub fn custom_sections<'a>(&'a self, name: &'a str) -> impl Iterator<Item = Box<[u8]>> + 'a {
// TODO: implement on JavaScript
DefaultCustomSectionsIterator {}
}
}

pub struct DefaultCustomSectionsIterator {}

impl Iterator for DefaultCustomSectionsIterator {
type Item = Box<[u8]>;
fn next(&mut self) -> Option<Self::Item> {
None
WebAssembly::Module::custom_sections(&self.module, name)
.iter()
.map(move |(buf_val)| {
let typebuf: js_sys::Uint8Array = js_sys::Uint8Array::new(&buf_val);
typebuf.to_vec().into_boxed_slice()
})
.collect::<Vec<Box<[u8]>>>()
.into_iter()
}
}

Expand Down
15 changes: 15 additions & 0 deletions lib/api/tests/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,18 @@ fn calling_host_functions_with_negative_values_works() -> Result<(), String> {

Ok(())
}

#[universal_test]
fn module_custom_sections() -> Result<(), String> {
let store = Store::default();
let custom_section_wasm_bytes = include_bytes!("simple-name-section.wasm");
let module = Module::new(&store, custom_section_wasm_bytes).map_err(|e| format!("{e:?}"))?;
let sections = module.custom_sections("name");
let sections_vec: Vec<Box<[u8]>> = sections.collect();
assert_eq!(sections_vec.len(), 1);
assert_eq!(
sections_vec[0],
vec![2, 2, 36, 105, 1, 0, 0, 0].into_boxed_slice()
);
Ok(())
}
Binary file added lib/api/tests/simple-name-section.wasm
Binary file not shown.
14 changes: 9 additions & 5 deletions lib/compiler/src/translator/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,15 @@ pub fn translate_module<'data>(
data,
data_offset,
..
} => parse_name_section(
NameSectionReader::new(data, data_offset)
.map_err(from_binaryreadererror_wasmerror)?,
environ,
)?,
} => {
// We still add the custom section data, but also read it as name section reader
environ.custom_section("name", data)?;
parse_name_section(
NameSectionReader::new(data, data_offset)
.map_err(from_binaryreadererror_wasmerror)?,
environ,
)?
}

Payload::CustomSection { name, data, .. } => environ.custom_section(name, data)?,

Expand Down

0 comments on commit 945d917

Please sign in to comment.