Skip to content

Commit

Permalink
LibWasm: Give names to functions exported to JS via ref.func
Browse files Browse the repository at this point in the history
https://webassembly.github.io/spec/js-api/index.html#name-of-the-webassembly-function
(cherry picked from commit e8fd8982f82e91f97b24523f3ee60eef774990dd)
  • Loading branch information
dzfrias authored and alimpfard committed Jul 9, 2024
1 parent f5a27dd commit 5e8dba0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Tests/LibWasm/test-wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ class WebAssemblyModule final : public JS::Object {
// Noop, this just needs to exist.
return Wasm::Result { Vector<Wasm::Value> {} };
},
type });
type,
"__TEST" });
}

static HashMap<Wasm::Linker::Name, Wasm::ExternValue> s_spec_test_namespace;
Expand Down
5 changes: 4 additions & 1 deletion Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,21 @@ class WasmFunction {

class HostFunction {
public:
explicit HostFunction(AK::Function<Result(Configuration&, Vector<Value>&)> function, FunctionType const& type)
explicit HostFunction(AK::Function<Result(Configuration&, Vector<Value>&)> 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<Result(Configuration&, Vector<Value>&)> m_function;
FunctionType m_type;
ByteString m_name;
};

using FunctionInstance = Variant<WasmFunction, HostFunction>;
Expand Down
3 changes: 2 additions & 1 deletion Userland/Libraries/LibWasm/WASI/Wasi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,8 @@ struct InvocationOf<impl> {
FunctionType {
move(arguments_types),
{ ValueType(ValueType::I32) },
});
},
function_name);
}
};

Expand Down
20 changes: 16 additions & 4 deletions Userland/Libraries/LibWeb/WebAssembly/WebAssembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> 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());
Expand Down Expand Up @@ -448,9 +449,20 @@ JS::Value to_js_value(JS::VM& vm, Wasm::Value& wasm_value)
return JS::Value(wasm_value.to<double>().value());
case Wasm::ValueType::F32:
return JS::Value(static_cast<double>(wasm_value.to<float>().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<Wasm::Reference::Func>().value().address, "FIXME_IHaveNoIdeaWhatThisShouldBeCalled");
case Wasm::ValueType::FunctionReference: {
auto address = wasm_value.to<Wasm::Reference::Func>().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();
Expand Down
3 changes: 2 additions & 1 deletion Userland/Utilities/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,8 @@ ErrorOr<int> 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);
}

Expand Down

0 comments on commit 5e8dba0

Please sign in to comment.