LibWasm: Remove type information from Value

Gets fib(30) from 380ms to 340ms.
This commit is contained in:
Diego Frias 2024-08-04 08:06:50 -07:00 committed by Ali Mohammad Pur
commit a58704296c
Notes: github-actions[bot] 2024-08-06 23:11:13 +00:00
12 changed files with 349 additions and 287 deletions

View file

@ -32,7 +32,7 @@ static Wasm::ValueType table_kind_to_value_type(Bindings::TableKind kind)
static JS::ThrowCompletionOr<Wasm::Value> value_to_reference(JS::VM& vm, JS::Value value, Wasm::ValueType const& reference_type)
{
if (value.is_undefined())
return Wasm::Value(reference_type, 0ull);
return Wasm::Value();
return Detail::to_webassembly_value(vm, value, reference_type);
}
@ -51,7 +51,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Table>> Table::construct_impl(JS::Realm& re
if (!address.has_value())
return vm.throw_completion<JS::TypeError>("Wasm Table allocation failed"sv);
auto const& reference = reference_value.value().get<Wasm::Reference>();
auto const& reference = reference_value.to<Wasm::Reference>();
auto& table = *cache.abstract_machine().store().get(*address);
for (auto& element : table.elements())
element = reference;
@ -84,7 +84,7 @@ WebIDL::ExceptionOr<u32> Table::grow(u32 delta, JS::Value value)
auto initial_size = table->elements().size();
auto reference_value = TRY(value_to_reference(vm, value, table->type().element_type()));
auto const& reference = reference_value.value().get<Wasm::Reference>();
auto const& reference = reference_value.to<Wasm::Reference>();
if (!table->grow(delta, reference))
return vm.throw_completion<JS::RangeError>("Failed to grow table"sv);
@ -108,7 +108,7 @@ WebIDL::ExceptionOr<JS::Value> Table::get(u32 index) const
auto& ref = table->elements()[index];
Wasm::Value wasm_value { ref };
return Detail::to_js_value(vm, wasm_value);
return Detail::to_js_value(vm, wasm_value, table->type().element_type());
}
// https://webassembly.github.io/spec/js-api/#dom-table-set
@ -125,7 +125,7 @@ WebIDL::ExceptionOr<void> Table::set(u32 index, JS::Value value)
return vm.throw_completion<JS::RangeError>("Table element index out of range"sv);
auto reference_value = TRY(value_to_reference(vm, value, table->type().element_type()));
auto const& reference = reference_value.value().get<Wasm::Reference>();
auto const& reference = reference_value.to<Wasm::Reference>();
table->elements()[index] = reference;

View file

@ -193,8 +193,11 @@ JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS
Wasm::HostFunction host_function {
[&](auto&, auto& arguments) -> Wasm::Result {
JS::MarkedVector<JS::Value> argument_values { vm.heap() };
for (auto& entry : arguments)
argument_values.append(to_js_value(vm, entry));
size_t index = 0;
for (auto& entry : arguments) {
argument_values.append(to_js_value(vm, entry, type.parameters()[index]));
++index;
}
auto result = TRY(JS::call(vm, function, JS::js_undefined(), argument_values.span()));
if (type.results().is_empty())
@ -378,11 +381,13 @@ JS::NativeFunction* create_native_function(JS::VM& vm, Wasm::FunctionAddress add
return JS::js_undefined();
if (result.values().size() == 1)
return to_js_value(vm, result.values().first());
return to_js_value(vm, result.values().first(), type.results().first());
return JS::Value(JS::Array::create_from<Wasm::Value>(realm, result.values(), [&](Wasm::Value value) {
return to_js_value(vm, value);
}));
VERIFY_NOT_REACHED();
// TODO
/*return JS::Value(JS::Array::create_from<Wasm::Value>(realm, result.values(), [&](Wasm::Value value) {*/
/* return to_js_value(vm, value);*/
/*}));*/
});
cache.add_function_instance(address, function);
@ -417,7 +422,7 @@ JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM& vm, JS::Value va
}
case Wasm::ValueType::FunctionReference: {
if (value.is_null())
return Wasm::Value { Wasm::ValueType(Wasm::ValueType::FunctionReference), 0ull };
return Wasm::Value();
if (value.is_function()) {
auto& function = value.as_function();
@ -439,20 +444,20 @@ JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM& vm, JS::Value va
VERIFY_NOT_REACHED();
}
JS::Value to_js_value(JS::VM& vm, Wasm::Value& wasm_value)
JS::Value to_js_value(JS::VM& vm, Wasm::Value& wasm_value, Wasm::ValueType type)
{
auto& realm = *vm.current_realm();
switch (wasm_value.type().kind()) {
switch (type.kind()) {
case Wasm::ValueType::I64:
return realm.heap().allocate<JS::BigInt>(realm, ::Crypto::SignedBigInteger { wasm_value.to<i64>().value() });
return realm.heap().allocate<JS::BigInt>(realm, ::Crypto::SignedBigInteger { wasm_value.to<i64>() });
case Wasm::ValueType::I32:
return JS::Value(wasm_value.to<i32>().value());
return JS::Value(wasm_value.to<i32>());
case Wasm::ValueType::F64:
return JS::Value(wasm_value.to<double>().value());
return JS::Value(wasm_value.to<double>());
case Wasm::ValueType::F32:
return JS::Value(static_cast<double>(wasm_value.to<float>().value()));
return JS::Value(static_cast<double>(wasm_value.to<float>()));
case Wasm::ValueType::FunctionReference: {
auto ref_ = *wasm_value.to<Wasm::Reference>();
auto ref_ = wasm_value.to<Wasm::Reference>();
if (ref_.ref().has<Wasm::Reference::Null>())
return JS::js_null();
auto address = ref_.ref().get<Wasm::Reference::Func>().address;

View file

@ -62,7 +62,7 @@ JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS
JS::ThrowCompletionOr<NonnullRefPtr<CompiledWebAssemblyModule>> parse_module(JS::VM&, JS::Object* buffer);
JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, ByteString const& name, Instance* instance = nullptr);
JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type);
JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value);
JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value, Wasm::ValueType type);
extern HashMap<JS::GCPtr<JS::Object>, WebAssemblyCache> s_caches;