From 95cc2bd81076facbd839e3303e4d2815a0c13955 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Tue, 22 Apr 2025 11:00:50 +0200 Subject: [PATCH] LibWeb/WebAssembly: Implement Module::exports(module) --- Libraries/LibWeb/WebAssembly/Module.cpp | 29 +++++++++++++++++++++++++ Libraries/LibWeb/WebAssembly/Module.h | 6 +++++ Libraries/LibWeb/WebAssembly/Module.idl | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Libraries/LibWeb/WebAssembly/Module.cpp b/Libraries/LibWeb/WebAssembly/Module.cpp index 63f5e79b69b..16c3a487af9 100644 --- a/Libraries/LibWeb/WebAssembly/Module.cpp +++ b/Libraries/LibWeb/WebAssembly/Module.cpp @@ -66,6 +66,35 @@ WebIDL::ExceptionOr> Module::imports(JS::VM&, GC: return import_objects; } +// https://webassembly.github.io/threads/js-api/index.html#dom-module-exports +WebIDL::ExceptionOr> Module::exports(JS::VM&, GC::Ref module_object) +{ + // 1. Let module be moduleObject.[[Module]]. + // 2. Let exports be « ». + Vector export_objects; + + // 3. For each (name, type) of module_exports(module), + auto& exports = module_object->m_compiled_module->module->export_section().entries(); + export_objects.ensure_capacity(exports.size()); + for (auto& entry : exports) { + // 3.1. Let kind be the string value of the extern type type. + auto const kind = entry.description().visit( + [](Wasm::FunctionIndex) { return Bindings::ImportExportKind::Function; }, + [](Wasm::TableIndex) { return Bindings::ImportExportKind::Table; }, + [](Wasm::MemoryIndex) { return Bindings::ImportExportKind::Memory; }, + [](Wasm::GlobalIndex) { return Bindings::ImportExportKind::Global; }); + // 3.2. Let obj be «[ "name" → name, "kind" → kind ]». + ModuleExportDescriptor descriptor { + .name = String::from_utf8_with_replacement_character(entry.name()), + .kind = kind, + }; + // 3.3. Append obj to exports. + export_objects.append(move(descriptor)); + } + // 4. Return exports. + return export_objects; +} + 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 d7072fce792..347fa2deb77 100644 --- a/Libraries/LibWeb/WebAssembly/Module.h +++ b/Libraries/LibWeb/WebAssembly/Module.h @@ -29,6 +29,11 @@ struct ModuleImportDescriptor { Bindings::ImportExportKind kind; }; +struct ModuleExportDescriptor { + String name; + Bindings::ImportExportKind kind; +}; + class Module : public Bindings::PlatformObject { WEB_PLATFORM_OBJECT(Module, Bindings::PlatformObject); GC_DECLARE_ALLOCATOR(Module); @@ -36,6 +41,7 @@ class Module : public Bindings::PlatformObject { 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); NonnullRefPtr compiled_module() const { return m_compiled_module; } diff --git a/Libraries/LibWeb/WebAssembly/Module.idl b/Libraries/LibWeb/WebAssembly/Module.idl index dc7035c9727..77d5e49a690 100644 --- a/Libraries/LibWeb/WebAssembly/Module.idl +++ b/Libraries/LibWeb/WebAssembly/Module.idl @@ -21,7 +21,7 @@ dictionary ModuleImportDescriptor { interface Module { constructor(BufferSource bytes); - [FIXME] static sequence exports(Module moduleObject); + static sequence exports(Module moduleObject); static sequence imports(Module moduleObject); [FIXME] static sequence customSections(Module moduleObject, DOMString sectionName); };