mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-28 11:49:44 +00:00
LibWeb/WebAssembly: Implement Module::imports(module)
This commit is contained in:
parent
3a6b6a732e
commit
f984c1cc51
Notes:
github-actions[bot]
2025-04-22 14:45:11 +00:00
Author: https://github.com/alimpfard
Commit: f984c1cc51
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4423
Reviewed-by: https://github.com/ADKaster ✅
3 changed files with 52 additions and 6 deletions
|
@ -33,6 +33,39 @@ WebIDL::ExceptionOr<GC::Ref<Module>> Module::construct_impl(JS::Realm& realm, GC
|
|||
return realm.create<Module>(realm, move(compiled_module));
|
||||
}
|
||||
|
||||
// https://webassembly.github.io/threads/js-api/index.html#dom-module-imports
|
||||
WebIDL::ExceptionOr<Vector<ModuleImportDescriptor>> Module::imports(JS::VM&, GC::Ref<Module> module_object)
|
||||
{
|
||||
// 1. Let module be moduleObject.[[Module]].
|
||||
// 2. Let imports be « ».
|
||||
Vector<ModuleImportDescriptor> import_objects;
|
||||
|
||||
// 3. For each (moduleName, name, type) of module_imports(module),
|
||||
auto& imports = module_object->m_compiled_module->module->import_section().imports();
|
||||
import_objects.ensure_capacity(imports.size());
|
||||
for (auto& import : imports) {
|
||||
// 3.1. Let kind be the string value of the extern type type.
|
||||
auto const kind = import.description().visit(
|
||||
[](Wasm::TypeIndex) { return Bindings::ImportExportKind::Function; },
|
||||
[](Wasm::TableType) { return Bindings::ImportExportKind::Table; },
|
||||
[](Wasm::MemoryType) { return Bindings::ImportExportKind::Memory; },
|
||||
[](Wasm::GlobalType) { return Bindings::ImportExportKind::Global; },
|
||||
[](Wasm::FunctionType) { return Bindings::ImportExportKind::Function; });
|
||||
|
||||
// 3.2. Let obj be «[ "module" → moduleName, "name" → name, "kind" → kind ]».
|
||||
ModuleImportDescriptor descriptor {
|
||||
.module = String::from_utf8_with_replacement_character(import.module()),
|
||||
.name = String::from_utf8_with_replacement_character(import.name()),
|
||||
.kind = kind,
|
||||
};
|
||||
|
||||
// 3.3. Append obj to imports.
|
||||
import_objects.append(move(descriptor));
|
||||
}
|
||||
// 4. Return imports.
|
||||
return import_objects;
|
||||
}
|
||||
|
||||
Module::Module(JS::Realm& realm, NonnullRefPtr<Detail::CompiledWebAssemblyModule> compiled_module)
|
||||
: Bindings::PlatformObject(realm)
|
||||
, m_compiled_module(move(compiled_module))
|
||||
|
|
|
@ -10,19 +10,32 @@
|
|||
#include <LibGC/Ptr.h>
|
||||
#include <LibGC/Root.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
#include <LibWasm/Types.h>
|
||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
#include <LibWeb/WebAssembly/WebAssembly.h>
|
||||
|
||||
namespace Web::Bindings {
|
||||
enum class ImportExportKind : u8;
|
||||
}
|
||||
|
||||
namespace Web::WebAssembly {
|
||||
|
||||
// FIXME: This should really be generated by the IDL generator.
|
||||
struct ModuleImportDescriptor {
|
||||
String module;
|
||||
String name;
|
||||
Bindings::ImportExportKind kind;
|
||||
};
|
||||
|
||||
class Module : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(Module, Bindings::PlatformObject);
|
||||
GC_DECLARE_ALLOCATOR(Module);
|
||||
|
||||
public:
|
||||
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>);
|
||||
|
||||
NonnullRefPtr<Detail::CompiledWebAssemblyModule> compiled_module() const { return m_compiled_module; }
|
||||
|
||||
|
|
|
@ -6,14 +6,14 @@ enum ImportExportKind {
|
|||
};
|
||||
|
||||
dictionary ModuleExportDescriptor {
|
||||
required USVString name;
|
||||
required ImportExportKind kind;
|
||||
required [GenerateAsRequired] USVString name;
|
||||
required [GenerateAsRequired] ImportExportKind kind;
|
||||
};
|
||||
|
||||
dictionary ModuleImportDescriptor {
|
||||
required USVString module;
|
||||
required USVString name;
|
||||
required ImportExportKind kind;
|
||||
required [GenerateAsRequired] USVString module;
|
||||
required [GenerateAsRequired] USVString name;
|
||||
required [GenerateAsRequired] ImportExportKind kind;
|
||||
};
|
||||
|
||||
// https://webassembly.github.io/spec/js-api/#modules
|
||||
|
@ -22,6 +22,6 @@ interface Module {
|
|||
constructor(BufferSource bytes);
|
||||
|
||||
[FIXME] static sequence<ModuleExportDescriptor> exports(Module moduleObject);
|
||||
[FIXME] static sequence<ModuleImportDescriptor> imports(Module moduleObject);
|
||||
static sequence<ModuleImportDescriptor> imports(Module moduleObject);
|
||||
[FIXME] static sequence<ArrayBuffer> customSections(Module moduleObject, DOMString sectionName);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue