LibWasm: Preserve sign bit across JS boundary in test-wasm

A `Uint8Array` can now be passed in the Wasm testjs files to be
transmuted into a Wasm value.
This commit is contained in:
Diego 2024-07-03 14:08:49 -07:00 committed by Ali Mohammad Pur
commit 6493acf2f4
Notes: sideshowbarker 2024-07-17 07:08:37 +09:00
2 changed files with 22 additions and 8 deletions

View file

@ -256,7 +256,17 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
for (auto& param : type->parameters()) {
auto argument = vm.argument(index++);
double double_value = 0;
if (!argument.is_bigint())
if (argument.is_object()) {
auto object = MUST(argument.to_object(vm));
// Uint8Array allows for raw bytes to be passed into Wasm. This is
// particularly useful for preserving the sign bit of a NaN
if (!is<JS::Uint8Array>(*object))
return vm.throw_completion<JS::TypeError>("Expected a Uint8Array object"sv);
auto& array = static_cast<JS::Uint8Array&>(*object);
if (array.array_length().length() != 8)
return vm.throw_completion<JS::TypeError>("Expected a Uint8Array of size 8"sv);
memcpy(&double_value, array.data().data(), sizeof(double));
} else if (!argument.is_bigint())
double_value = TRY(argument.to_double(vm));
switch (param.kind()) {
case Wasm::ValueType::Kind::I32: