LibWeb/WebAssembly: Implement Module::customSections(module)

This commit is contained in:
Ali Mohammad Pur 2025-04-22 11:17:48 +02:00 committed by Andrew Kaster
parent f341bb0522
commit a786269687
Notes: github-actions[bot] 2025-04-22 14:44:52 +00:00
3 changed files with 26 additions and 1 deletions

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/Intrinsics.h>
@ -95,6 +96,29 @@ WebIDL::ExceptionOr<Vector<ModuleExportDescriptor>> Module::exports(JS::VM&, GC:
return export_objects;
}
// https://webassembly.github.io/threads/js-api/index.html#dom-module-customsections
WebIDL::ExceptionOr<GC::RootVector<GC::Ref<JS::ArrayBuffer>>> Module::custom_sections(JS::VM& vm, GC::Ref<Module> module_object, String section_name)
{
// 1. Let bytes be moduleObject.[[Bytes]].
// 2. Let customSections be « ».
GC::RootVector<GC::Ref<JS::ArrayBuffer>> 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<Detail::CompiledWebAssemblyModule> compiled_module)
: Bindings::PlatformObject(realm)
, m_compiled_module(move(compiled_module))