From c95337296fbcae258afad5c13da38b65586e4a56 Mon Sep 17 00:00:00 2001 From: Tom Lauwaerts Date: Wed, 10 Jun 2026 14:54:57 +0200 Subject: [PATCH] Fix NaN and Infinity encoding --- src/messaging/Message.ts | 4 ++-- src/sourcemap/Wasm.ts | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/messaging/Message.ts b/src/messaging/Message.ts index 4e72f8a..888570d 100644 --- a/src/messaging/Message.ts +++ b/src/messaging/Message.ts @@ -11,6 +11,7 @@ import Interrupt = WARDuino.Interrupt; import State = WARDuino.State; import Value = WASM.Value; import Type = WASM.Type; +import Special = WASM.Special; // An acknowledgement returned by the debugger export interface Ack { @@ -28,7 +29,6 @@ export interface Request { export namespace Message { import Inspect = WARDuino.Inspect; - import Integer = WASM.Integer; import Float = WASM.Float; export const run: Request = { type: Interrupt.run, @@ -176,7 +176,7 @@ export namespace Message { function convert(args: Value[]) { let payload: string = ''; args.forEach((arg: Value) => { - if (arg.type === Float.f32 || arg.type === Float.f64) { + if (arg.type === Float.f32 || arg.type === Float.f64 || arg.type === Special.nan || arg.type === Special.infinity) { const buff = Buffer.alloc(arg.type === Float.f32 ? 4 : 8); write(buff, Number(arg.value), 0, true, arg.type === Float.f32 ? 23 : 52, buff.length); // todo fix precision loss payload += buff.toString('hex'); diff --git a/src/sourcemap/Wasm.ts b/src/sourcemap/Wasm.ts index 46a3a40..e99e5a4 100644 --- a/src/sourcemap/Wasm.ts +++ b/src/sourcemap/Wasm.ts @@ -29,7 +29,7 @@ export namespace WASM { ['i64', Integer.i64] ]); - export interface Value { + export interface Value { type: T; value: T extends Integer ? bigint : number; } @@ -49,12 +49,21 @@ export namespace WASM { } } - export interface Nothing extends Value {} + export interface Nothing extends Value { + } export const nothing: Nothing = { type: Special.nothing, value: 0 } + export const nan: WASM.Value = {value: NaN, type: Special.nan}; + + export const negnan: WASM.Value = {value: -NaN, type: Special.nan}; + + export const infinity: WASM.Value = {value: Infinity, type: Special.infinity}; + + export const neginfinity: WASM.Value = {value: -Infinity, type: Special.infinity}; + export function u32(n: bigint): WASM.Value { return {value: n, type: Integer.u32}; } @@ -63,12 +72,14 @@ export namespace WASM { return {value: n, type: Integer.i32}; } - export function f32(n: number): WASM.Value { - return {value: n, type: Float.f32}; + const determineType: (n: number) => WASM.Type = (n: number) => n === Infinity || n === -Infinity ? Special.infinity : (isNaN(n) ? Special.nan : Float.f64); + + export function f32(n: number): WASM.Value { + return {value: n, type: determineType(n)}; } - export function f64(n: number): WASM.Value { - return {value: n, type: Float.f64}; + export function f64(n: number): WASM.Value { + return {value: n, type: determineType(n)}; } export function u64(n: bigint): WASM.Value {