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

This commit is contained in:
Ali Mohammad Pur 2025-04-22 11:00:50 +02:00 committed by Andrew Kaster
commit 95cc2bd810
Notes: github-actions[bot] 2025-04-22 14:45:04 +00:00
3 changed files with 36 additions and 1 deletions

View file

@ -66,6 +66,35 @@ WebIDL::ExceptionOr<Vector<ModuleImportDescriptor>> Module::imports(JS::VM&, GC:
return import_objects; return import_objects;
} }
// https://webassembly.github.io/threads/js-api/index.html#dom-module-exports
WebIDL::ExceptionOr<Vector<ModuleExportDescriptor>> Module::exports(JS::VM&, GC::Ref<Module> module_object)
{
// 1. Let module be moduleObject.[[Module]].
// 2. Let exports be « ».
Vector<ModuleExportDescriptor> 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<Detail::CompiledWebAssemblyModule> compiled_module) Module::Module(JS::Realm& realm, NonnullRefPtr<Detail::CompiledWebAssemblyModule> compiled_module)
: Bindings::PlatformObject(realm) : Bindings::PlatformObject(realm)
, m_compiled_module(move(compiled_module)) , m_compiled_module(move(compiled_module))

View file

@ -29,6 +29,11 @@ struct ModuleImportDescriptor {
Bindings::ImportExportKind kind; Bindings::ImportExportKind kind;
}; };
struct ModuleExportDescriptor {
String name;
Bindings::ImportExportKind kind;
};
class Module : public Bindings::PlatformObject { class Module : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(Module, Bindings::PlatformObject); WEB_PLATFORM_OBJECT(Module, Bindings::PlatformObject);
GC_DECLARE_ALLOCATOR(Module); GC_DECLARE_ALLOCATOR(Module);
@ -36,6 +41,7 @@ class Module : public Bindings::PlatformObject {
public: public:
static WebIDL::ExceptionOr<GC::Ref<Module>> construct_impl(JS::Realm&, GC::Root<WebIDL::BufferSource>& bytes); static WebIDL::ExceptionOr<GC::Ref<Module>> construct_impl(JS::Realm&, GC::Root<WebIDL::BufferSource>& bytes);
static WebIDL::ExceptionOr<Vector<ModuleImportDescriptor>> imports(JS::VM&, GC::Ref<Module>); static WebIDL::ExceptionOr<Vector<ModuleImportDescriptor>> imports(JS::VM&, GC::Ref<Module>);
static WebIDL::ExceptionOr<Vector<ModuleExportDescriptor>> exports(JS::VM&, GC::Ref<Module>);
NonnullRefPtr<Detail::CompiledWebAssemblyModule> compiled_module() const { return m_compiled_module; } NonnullRefPtr<Detail::CompiledWebAssemblyModule> compiled_module() const { return m_compiled_module; }

View file

@ -21,7 +21,7 @@ dictionary ModuleImportDescriptor {
interface Module { interface Module {
constructor(BufferSource bytes); constructor(BufferSource bytes);
[FIXME] static sequence<ModuleExportDescriptor> exports(Module moduleObject); static sequence<ModuleExportDescriptor> exports(Module moduleObject);
static sequence<ModuleImportDescriptor> imports(Module moduleObject); static sequence<ModuleImportDescriptor> imports(Module moduleObject);
[FIXME] static sequence<ArrayBuffer> customSections(Module moduleObject, DOMString sectionName); [FIXME] static sequence<ArrayBuffer> customSections(Module moduleObject, DOMString sectionName);
}; };