From a7862696876e610690820246fbae96c10521e788 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Tue, 22 Apr 2025 11:17:48 +0200 Subject: [PATCH] LibWeb/WebAssembly: Implement Module::customSections(module) --- Libraries/LibWeb/WebAssembly/Module.cpp | 24 ++++++++++++++++++++++++ Libraries/LibWeb/WebAssembly/Module.h | 1 + Libraries/LibWeb/WebAssembly/Module.idl | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Libraries/LibWeb/WebAssembly/Module.cpp b/Libraries/LibWeb/WebAssembly/Module.cpp index 16c3a487af9..080bc50b486 100644 --- a/Libraries/LibWeb/WebAssembly/Module.cpp +++ b/Libraries/LibWeb/WebAssembly/Module.cpp @@ -5,6 +5,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -95,6 +96,29 @@ WebIDL::ExceptionOr> Module::exports(JS::VM&, GC: return export_objects; } +// https://webassembly.github.io/threads/js-api/index.html#dom-module-customsections +WebIDL::ExceptionOr>> Module::custom_sections(JS::VM& vm, GC::Ref module_object, String section_name) +{ + // 1. Let bytes be moduleObject.[[Bytes]]. + // 2. Let customSections be « ». + GC::RootVector> array_buffers { vm.heap() }; + + // 3. For each custom section customSection of bytes, interpreted according to the module grammar, + auto& custom_sections = module_object->m_compiled_module->module->custom_sections(); + for (auto& section : custom_sections) { + // 3.1. Let name be the name of customSection, decoded as UTF-8. + // 3.2. Assert: name is not failure (moduleObject.[[Module]] is valid). + auto name = MUST(String::from_utf8(section.name().bytes())); + // 3.3. If name equals sectionName as string values, + if (section_name == name) { + // 3.3.1. Append a new ArrayBuffer containing a copy of the bytes in bytes for the range matched by this customsec production to customSections. + array_buffers.append(JS::ArrayBuffer::create(module_object->realm(), section.contents())); + } + } + // 4. Return customSections. + return array_buffers; +} + Module::Module(JS::Realm& realm, NonnullRefPtr compiled_module) : Bindings::PlatformObject(realm) , m_compiled_module(move(compiled_module)) diff --git a/Libraries/LibWeb/WebAssembly/Module.h b/Libraries/LibWeb/WebAssembly/Module.h index 347fa2deb77..1205d7f75cd 100644 --- a/Libraries/LibWeb/WebAssembly/Module.h +++ b/Libraries/LibWeb/WebAssembly/Module.h @@ -42,6 +42,7 @@ public: static WebIDL::ExceptionOr> construct_impl(JS::Realm&, GC::Root& bytes); static WebIDL::ExceptionOr> imports(JS::VM&, GC::Ref); static WebIDL::ExceptionOr> exports(JS::VM&, GC::Ref); + static WebIDL::ExceptionOr>> custom_sections(JS::VM&, GC::Ref, String section_name); NonnullRefPtr compiled_module() const { return m_compiled_module; } diff --git a/Libraries/LibWeb/WebAssembly/Module.idl b/Libraries/LibWeb/WebAssembly/Module.idl index 77d5e49a690..25996f6da3c 100644 --- a/Libraries/LibWeb/WebAssembly/Module.idl +++ b/Libraries/LibWeb/WebAssembly/Module.idl @@ -23,5 +23,5 @@ interface Module { static sequence exports(Module moduleObject); static sequence imports(Module moduleObject); - [FIXME] static sequence customSections(Module moduleObject, DOMString sectionName); + static sequence customSections(Module moduleObject, DOMString sectionName); };