mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
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:
parent
63a5ff70e5
commit
6493acf2f4
Notes:
sideshowbarker
2024-07-17 07:08:37 +09:00
Author: https://github.com/dzfrias Commit: https://github.com/LadybirdBrowser/ladybird/commit/6493acf2f4 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/459 Reviewed-by: https://github.com/alimpfard
@ -200,7 +200,7 @@ def make_description(input_path: Path, name: str, out_path: Path) -> WastDescrip
|
|||||||
return parse(description)
|
return parse(description)
|
||||||
|
|
||||||
|
|
||||||
def gen_value(value: WasmValue) -> str:
|
def gen_value(value: WasmValue, as_arg=False) -> str:
|
||||||
def unsigned_to_signed(uint: int, bits: int) -> int:
|
def unsigned_to_signed(uint: int, bits: int) -> int:
|
||||||
max_value = 2**bits
|
max_value = 2**bits
|
||||||
if uint >= 2 ** (bits - 1):
|
if uint >= 2 ** (bits - 1):
|
||||||
@ -221,7 +221,13 @@ def gen_value(value: WasmValue) -> str:
|
|||||||
f = struct.unpack("d", b)[0]
|
f = struct.unpack("d", b)[0]
|
||||||
return f
|
return f
|
||||||
|
|
||||||
def float_to_str(f: float) -> str:
|
def float_to_str(f: float, preserve_nan_sign=False) -> str:
|
||||||
|
if math.isnan(f) and preserve_nan_sign:
|
||||||
|
f_bytes = struct.pack("d", f)
|
||||||
|
# -NaN does not preserve the sign bit in JavaScript land, so if
|
||||||
|
# we want to preserve NaN "sign", we pass in raw bytes
|
||||||
|
return f"new Uint8Array({list(f_bytes)})"
|
||||||
|
|
||||||
if math.isnan(f) and math.copysign(1.0, f) < 0:
|
if math.isnan(f) and math.copysign(1.0, f) < 0:
|
||||||
return "-NaN"
|
return "-NaN"
|
||||||
elif math.isnan(f):
|
elif math.isnan(f):
|
||||||
@ -234,8 +240,6 @@ def gen_value(value: WasmValue) -> str:
|
|||||||
|
|
||||||
if value.value.startswith("nan"):
|
if value.value.startswith("nan"):
|
||||||
return "NaN"
|
return "NaN"
|
||||||
elif value.value.startswith("-nan"):
|
|
||||||
return "-NaN"
|
|
||||||
elif value.value == "inf":
|
elif value.value == "inf":
|
||||||
return "Infinity"
|
return "Infinity"
|
||||||
elif value.value == "-inf":
|
elif value.value == "-inf":
|
||||||
@ -247,9 +251,9 @@ def gen_value(value: WasmValue) -> str:
|
|||||||
case "i64":
|
case "i64":
|
||||||
return str(unsigned_to_signed(int(value.value), 64)) + "n"
|
return str(unsigned_to_signed(int(value.value), 64)) + "n"
|
||||||
case "f32":
|
case "f32":
|
||||||
return float_to_str(int_to_float_bitcast(int(value.value)))
|
return float_to_str(int_to_float_bitcast(int(value.value)), as_arg)
|
||||||
case "f64":
|
case "f64":
|
||||||
return float_to_str(int_to_float64_bitcast(int(value.value)))
|
return float_to_str(int_to_float64_bitcast(int(value.value)), as_arg)
|
||||||
case "externref" | "funcref" | "v128":
|
case "externref" | "funcref" | "v128":
|
||||||
return value.value
|
return value.value
|
||||||
case _:
|
case _:
|
||||||
@ -257,7 +261,7 @@ def gen_value(value: WasmValue) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def gen_args(args: list[WasmValue]) -> str:
|
def gen_args(args: list[WasmValue]) -> str:
|
||||||
return ",".join(gen_value(arg) for arg in args)
|
return ",".join(gen_value(arg, True) for arg in args)
|
||||||
|
|
||||||
|
|
||||||
def gen_module_command(command: ModuleCommand, ctx: Context):
|
def gen_module_command(command: ModuleCommand, ctx: Context):
|
||||||
|
@ -256,7 +256,17 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
|
|||||||
for (auto& param : type->parameters()) {
|
for (auto& param : type->parameters()) {
|
||||||
auto argument = vm.argument(index++);
|
auto argument = vm.argument(index++);
|
||||||
double double_value = 0;
|
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));
|
double_value = TRY(argument.to_double(vm));
|
||||||
switch (param.kind()) {
|
switch (param.kind()) {
|
||||||
case Wasm::ValueType::Kind::I32:
|
case Wasm::ValueType::Kind::I32:
|
||||||
|
Loading…
Reference in New Issue
Block a user