diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2023-02-25 11:44:43 +0330 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2023-02-26 10:54:23 +0330 |
commit | 5bc1f135b7f2bee581eea8291f09e927964251d7 (patch) | |
tree | efd71b02afd96d84f58c942734d290c571abed21 /Tests/LibWasm | |
parent | 8a36d8826fff0ed13d5d8d62ad93b9c9355113c9 (diff) | |
download | serenity-5bc1f135b7f2bee581eea8291f09e927964251d7.zip |
Tests: Implement multi-value returns in test-wasm
Diffstat (limited to 'Tests/LibWasm')
-rw-r--r-- | Tests/LibWasm/test-wasm.cpp | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index c882b95a3f..81bd472774 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -252,15 +252,22 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke) if (result.values().is_empty()) return JS::js_null(); - JS::Value return_value; - result.values().first().value().visit( - [&](auto const& value) { return_value = JS::Value(static_cast<double>(value)); }, - [&](i32 value) { return_value = JS::Value(static_cast<double>(value)); }, - [&](i64 value) { return_value = JS::Value(JS::BigInt::create(vm, Crypto::SignedBigInteger { value })); }, - [&](Wasm::Reference const& reference) { - reference.ref().visit( - [&](const Wasm::Reference::Null&) { return_value = JS::js_null(); }, - [&](const auto& ref) { return_value = JS::Value(static_cast<double>(ref.address.value())); }); - }); - return return_value; + auto to_js_value = [&](Wasm::Value const& value) { + return value.value().visit( + [](auto const& value) { return JS::Value(static_cast<double>(value)); }, + [](i32 value) { return JS::Value(static_cast<double>(value)); }, + [&](i64 value) { return JS::Value(JS::BigInt::create(vm, Crypto::SignedBigInteger { value })); }, + [](Wasm::Reference const& reference) { + return reference.ref().visit( + [](const Wasm::Reference::Null&) { return JS::js_null(); }, + [](const auto& ref) { return JS::Value(static_cast<double>(ref.address.value())); }); + }); + }; + + if (result.values().size() == 1) + return to_js_value(result.values().first()); + + return JS::Array::create_from<Wasm::Value>(*vm.current_realm(), result.values(), [&](Wasm::Value value) { + return to_js_value(value); + }); } |