From 382ed04be20d4663d4ee9f9df703ab5cab337f1a Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 12 Jan 2023 11:05:22 +0000 Subject: [PATCH 1/4] Added support for Wasm Module custom sections in js --- lib/api/src/js/module.rs | 20 +++++++++----------- lib/api/tests/module.rs | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/api/src/js/module.rs b/lib/api/src/js/module.rs index 36c7f355361..296e7eeb6fa 100644 --- a/lib/api/src/js/module.rs +++ b/lib/api/src/js/module.rs @@ -566,17 +566,15 @@ 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)| { + // assert!(buf_val.is_instance_of::()); + 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(()) +} From a4b5ad74f7f2586a56b7b24370ac56e56f4fb0ed Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 12 Jan 2023 11:20:47 +0000 Subject: [PATCH 2/4] Added missing wasm file --- lib/api/tests/simple-name-section.wasm | Bin 0 -> 93 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/api/tests/simple-name-section.wasm diff --git a/lib/api/tests/simple-name-section.wasm b/lib/api/tests/simple-name-section.wasm new file mode 100644 index 0000000000000000000000000000000000000000..2969b62d6b09edc6540678e8d34989fba83d8e7f GIT binary patch literal 93 zcmZQbEY4+QU|?Y6U`k-DXGmaRV3K5H&&(~zFDfbKh0v)f@oA-b$qWq4OpJ`|f{eVW h6(DJtFe4WSBO8OGmH-1c120QnVs0uElS(GYPyo=r6T$!h literal 0 HcmV?d00001 From 19b2efdc7ffe60353342b80f610d0525d6ddd485 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Thu, 12 Jan 2023 21:36:54 +0800 Subject: [PATCH 3/4] Remove an unneeded comment. --- lib/api/src/js/module.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/api/src/js/module.rs b/lib/api/src/js/module.rs index 296e7eeb6fa..78096584b25 100644 --- a/lib/api/src/js/module.rs +++ b/lib/api/src/js/module.rs @@ -569,7 +569,6 @@ impl Module { WebAssembly::Module::custom_sections(&self.module, name) .iter() .map(move |(buf_val)| { - // assert!(buf_val.is_instance_of::()); let typebuf: js_sys::Uint8Array = js_sys::Uint8Array::new(&buf_val); typebuf.to_vec().into_boxed_slice() }) From 1cdb3ebf5a4bbfa846f3008051af677174a6118c Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 12 Jan 2023 19:59:35 +0100 Subject: [PATCH 4/4] =?UTF-8?q?Fixed=20custom=20section=20being=20skipped?= =?UTF-8?q?=20if=20the=20section=20name=20is=20=E2=80=9Cname=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compiler/src/translator/module.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) 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)?,