1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 03:27:44 +00:00

LibWeb: Avoid resolving the wasm call address type on every invocation

This is a waste of time, and it's not a cheap operation.
This commit is contained in:
Ali Mohammad Pur 2021-06-13 21:33:10 +04:30 committed by Ali Mohammad Pur
parent 0d2aba07aa
commit baa4195daa

View file

@ -317,18 +317,19 @@ Optional<Wasm::Value> to_webassembly_value(JS::Value value, const Wasm::ValueTyp
JS::NativeFunction* create_native_function(Wasm::FunctionAddress address, String name, JS::GlobalObject& global_object) JS::NativeFunction* create_native_function(Wasm::FunctionAddress address, String name, JS::GlobalObject& global_object)
{ {
Optional<Wasm::FunctionType> type;
WebAssemblyObject::s_abstract_machine.store().get(address)->visit([&](const auto& value) { type = value.type(); });
// FIXME: Cache these. // FIXME: Cache these.
return JS::NativeFunction::create( return JS::NativeFunction::create(
global_object, global_object,
name, name,
[address](JS::VM& vm, JS::GlobalObject& global_object) -> JS::Value { [address, type = type.release_value()](JS::VM& vm, JS::GlobalObject& global_object) -> JS::Value {
Vector<Wasm::Value> values; Vector<Wasm::Value> values;
Optional<Wasm::FunctionType> type; values.ensure_capacity(type.parameters().size());
WebAssemblyObject::s_abstract_machine.store().get(address)->visit([&](const auto& value) { type = value.type(); });
// Grab as many values as needed and convert them. // Grab as many values as needed and convert them.
size_t index = 0; size_t index = 0;
for (auto& type : type.value().parameters()) { for (auto& type : type.parameters()) {
auto result = to_webassembly_value(vm.argument(index++), type, global_object); auto result = to_webassembly_value(vm.argument(index++), type, global_object);
if (result.has_value()) if (result.has_value())
values.append(result.release_value()); values.append(result.release_value());