Skip to content

Commit c86f7b7

Browse files
authored
Fix NaN and Infinity encoding (#81)
1 parent 96283a2 commit c86f7b7

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/messaging/Message.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Interrupt = WARDuino.Interrupt;
1111
import State = WARDuino.State;
1212
import Value = WASM.Value;
1313
import Type = WASM.Type;
14+
import Special = WASM.Special;
1415

1516
// An acknowledgement returned by the debugger
1617
export interface Ack {
@@ -28,7 +29,6 @@ export interface Request<R> {
2829

2930
export namespace Message {
3031
import Inspect = WARDuino.Inspect;
31-
import Integer = WASM.Integer;
3232
import Float = WASM.Float;
3333
export const run: Request<Ack> = {
3434
type: Interrupt.run,
@@ -176,7 +176,7 @@ export namespace Message {
176176
function convert(args: Value<Type>[]) {
177177
let payload: string = '';
178178
args.forEach((arg: Value<Type>) => {
179-
if (arg.type === Float.f32 || arg.type === Float.f64) {
179+
if (arg.type === Float.f32 || arg.type === Float.f64 || arg.type === Special.nan || arg.type === Special.infinity) {
180180
const buff = Buffer.alloc(arg.type === Float.f32 ? 4 : 8);
181181
write(buff, Number(arg.value), 0, true, arg.type === Float.f32 ? 23 : 52, buff.length); // todo fix precision loss
182182
payload += buff.toString('hex');

src/sourcemap/Wasm.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export namespace WASM {
2929
['i64', Integer.i64]
3030
]);
3131

32-
export interface Value<T extends Type> {
32+
export interface Value<T extends Type> {
3333
type: T;
3434
value: T extends Integer ? bigint : number;
3535
}
@@ -49,12 +49,21 @@ export namespace WASM {
4949
}
5050
}
5151

52-
export interface Nothing extends Value<Special> {}
52+
export interface Nothing extends Value<Special> {
53+
}
5354

5455
export const nothing: Nothing = {
5556
type: Special.nothing, value: 0
5657
}
5758

59+
export const nan: WASM.Value<Special> = {value: NaN, type: Special.nan};
60+
61+
export const negnan: WASM.Value<Special> = {value: -NaN, type: Special.nan};
62+
63+
export const infinity: WASM.Value<Special> = {value: Infinity, type: Special.infinity};
64+
65+
export const neginfinity: WASM.Value<Special> = {value: -Infinity, type: Special.infinity};
66+
5867
export function u32(n: bigint): WASM.Value<Integer> {
5968
return {value: n, type: Integer.u32};
6069
}
@@ -63,12 +72,14 @@ export namespace WASM {
6372
return {value: n, type: Integer.i32};
6473
}
6574

66-
export function f32(n: number): WASM.Value<Float> {
67-
return {value: n, type: Float.f32};
75+
const determineType: (n: number) => WASM.Type = (n: number) => n === Infinity || n === -Infinity ? Special.infinity : (isNaN(n) ? Special.nan : Float.f64);
76+
77+
export function f32(n: number): WASM.Value<Type> {
78+
return {value: n, type: determineType(n)};
6879
}
6980

70-
export function f64(n: number): WASM.Value<Float> {
71-
return {value: n, type: Float.f64};
81+
export function f64(n: number): WASM.Value<Type> {
82+
return {value: n, type: determineType(n)};
7283
}
7384

7485
export function u64(n: bigint): WASM.Value<Integer> {

0 commit comments

Comments
 (0)