diff --git a/lib/api/src/js/module.rs b/lib/api/src/js/module.rs index 36c7f355361..78096584b25 100644 --- a/lib/api/src/js/module.rs +++ b/lib/api/src/js/module.rs @@ -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> + 'a { - // TODO: implement on JavaScript - DefaultCustomSectionsIterator {} - } -} - -pub struct DefaultCustomSectionsIterator {} - -impl Iterator for DefaultCustomSectionsIterator { - type Item = Box<[u8]>; - fn next(&mut self) -> Option { - 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::>>() + .into_iter() } } diff --git a/lib/api/tests/module.rs b/lib/api/tests/module.rs index 13ccad34cbb..299b619e037 100644 --- a/lib/api/tests/module.rs +++ b/lib/api/tests/module.rs @@ -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> = 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(()) +} diff --git a/lib/api/tests/simple-name-section.wasm b/lib/api/tests/simple-name-section.wasm new file mode 100644 index 00000000000..2969b62d6b0 Binary files /dev/null and b/lib/api/tests/simple-name-section.wasm differ diff --git a/lib/compiler/src/translator/module.rs b/lib/compiler/src/translator/module.rs index daf58f7005e..b092aa46c9e 100644 --- a/lib/compiler/src/translator/module.rs +++ b/lib/compiler/src/translator/module.rs @@ -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)?,