From 5e8dba07a601dfcd2b423c7d0913521bcbcc51d7 Mon Sep 17 00:00:00 2001 From: Diego <96022404+dzfrias@users.noreply.github.com> Date: Mon, 8 Jul 2024 18:09:00 -0700 Subject: [PATCH] LibWasm: Give names to functions exported to JS via `ref.func` https://webassembly.github.io/spec/js-api/index.html#name-of-the-webassembly-function (cherry picked from commit e8fd8982f82e91f97b24523f3ee60eef774990dd) --- Tests/LibWasm/test-wasm.cpp | 3 ++- .../LibWasm/AbstractMachine/AbstractMachine.h | 5 ++++- Userland/Libraries/LibWasm/WASI/Wasi.cpp | 3 ++- .../LibWeb/WebAssembly/WebAssembly.cpp | 20 +++++++++++++++---- Userland/Utilities/wasm.cpp | 3 ++- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index 01695053b8cede..0756f4d7626eba 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -142,7 +142,8 @@ class WebAssemblyModule final : public JS::Object { // Noop, this just needs to exist. return Wasm::Result { Vector {} }; }, - type }); + type, + "__TEST" }); } static HashMap s_spec_test_namespace; diff --git a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h index 63e7f5bffface2..ed702a9d585f53 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h @@ -348,18 +348,21 @@ class WasmFunction { class HostFunction { public: - explicit HostFunction(AK::Function&)> function, FunctionType const& type) + explicit HostFunction(AK::Function&)> function, FunctionType const& type, ByteString name) : m_function(move(function)) , m_type(type) + , m_name(move(name)) { } auto& function() { return m_function; } auto& type() const { return m_type; } + auto& name() const { return m_name; } private: AK::Function&)> m_function; FunctionType m_type; + ByteString m_name; }; using FunctionInstance = Variant; diff --git a/Userland/Libraries/LibWasm/WASI/Wasi.cpp b/Userland/Libraries/LibWasm/WASI/Wasi.cpp index cb3d7baec1cfca..91e097eaf38522 100644 --- a/Userland/Libraries/LibWasm/WASI/Wasi.cpp +++ b/Userland/Libraries/LibWasm/WASI/Wasi.cpp @@ -971,7 +971,8 @@ struct InvocationOf { FunctionType { move(arguments_types), { ValueType(ValueType::I32) }, - }); + }, + function_name); } }; diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssembly.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssembly.cpp index eb69cdb00fadd6..df468a886fd915 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssembly.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssembly.cpp @@ -219,7 +219,8 @@ JS::ThrowCompletionOr> instantiate_module(JS return Wasm::Result { move(wasm_values) }; }, - type + type, + ByteString::formatted("func{}", resolved_imports.size()), }; auto address = cache.abstract_machine().store().allocate(move(host_function)); dbgln("Resolved to {}", address->value()); @@ -448,9 +449,20 @@ JS::Value to_js_value(JS::VM& vm, Wasm::Value& wasm_value) return JS::Value(wasm_value.to().value()); case Wasm::ValueType::F32: return JS::Value(static_cast(wasm_value.to().value())); - case Wasm::ValueType::FunctionReference: - // FIXME: What's the name of a function reference that isn't exported? - return create_native_function(vm, wasm_value.to().value().address, "FIXME_IHaveNoIdeaWhatThisShouldBeCalled"); + case Wasm::ValueType::FunctionReference: { + auto address = wasm_value.to().value().address; + auto& cache = get_cache(realm); + auto* function = cache.abstract_machine().store().get(address); + auto name = function->visit( + [&](Wasm::WasmFunction& wasm_function) { + auto index = *wasm_function.module().functions().find_first_index(address); + return ByteString::formatted("func{}", index); + }, + [](Wasm::HostFunction& host_function) { + return host_function.name(); + }); + return create_native_function(vm, address, name); + } case Wasm::ValueType::V128: case Wasm::ValueType::ExternReference: TODO(); diff --git a/Userland/Utilities/wasm.cpp b/Userland/Utilities/wasm.cpp index 9417b721cc156b..5925a5084b8250 100644 --- a/Userland/Utilities/wasm.cpp +++ b/Userland/Utilities/wasm.cpp @@ -695,7 +695,8 @@ ErrorOr serenity_main(Main::Arguments arguments) result.append(Wasm::Value { result_type, 0ull }); return Wasm::Result { move(result) }; }, - type)); + type, + entry.name)); exports.set(entry, *address); }