Skip to content

Commit b76a6f8

Browse files
committed
Revert "Fix support for scientific notation in latch spec tests"
This reverts commit 820678c.
1 parent 820678c commit b76a6f8

File tree

1 file changed

+12
-35
lines changed

1 file changed

+12
-35
lines changed

tests/latch/src/util/spec.util.ts

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ export function parseResult(input: string): WASM.Value | undefined {
2121
let value;
2222
delta = consume(input, cursor, /^[^)]*/d);
2323
if (type === WASM.Type.f32 || type === WASM.Type.f64) {
24-
value = parseFloat(input.slice(cursor, cursor + delta));
24+
value = parseHexFloat(input.slice(cursor, cursor + delta));
2525
} else {
26-
const bytes = type === WASM.Type.i64 ? 8 : 4;
27-
value = parseInteger(input.slice(cursor, cursor + delta), bytes);
26+
value = parseInteger(input.slice(cursor, cursor + delta));
2827
}
2928

3029
if (value === undefined) {
@@ -52,10 +51,9 @@ export function parseArguments(input: string, index: Cursor): WASM.Value[] {
5251
delta = consume(input, cursor, /^[^)]*/d);
5352
let maybe: number | undefined;
5453
if (type === WASM.Type.f32 || type === WASM.Type.f64) {
55-
maybe = parseFloat(input.slice(cursor, cursor + delta));
54+
maybe = parseHexFloat(input.slice(cursor, cursor + delta));
5655
} else {
57-
const bytes = type === WASM.Type.i64 ? 8 : 4;
58-
maybe = parseInteger(input.slice(cursor, cursor + delta), bytes);
56+
maybe = parseInteger(input.slice(cursor, cursor + delta));
5957
}
6058

6159
if (maybe !== undefined) {
@@ -99,7 +97,7 @@ function sign(integer: number): number {
9997
return Math.sign(integer) || 1;
10098
}
10199

102-
function parseFloat(input: string): number {
100+
function parseHexFloat(input: string): number {
103101
if (input.includes('-inf')) {
104102
return -Infinity;
105103
}
@@ -108,17 +106,8 @@ function parseFloat(input: string): number {
108106
return Infinity;
109107
}
110108

111-
if (input.includes('0x')) {
112-
return parseHexFloat(input);
113-
}
114-
115-
return Number(input);
116-
}
117-
118-
function parseHexFloat(input: string): number {
119-
const radix = 16;
120-
let base: string = input;
121-
let exponent = 0;
109+
const radix: number = input.includes('0x') ? 16 : 10;
110+
let base: string = input, mantissa, exponent = 0;
122111

123112
const splitIndex = input.indexOf('p');
124113
if (splitIndex !== -1) {
@@ -127,8 +116,6 @@ function parseHexFloat(input: string): number {
127116
}
128117

129118
const dotIndex = base.indexOf('.');
130-
let mantissa: number;
131-
132119
if (dotIndex !== -1) {
133120
const [integer, fractional] = base.split('.').map(hexStr => parseInt(hexStr, radix));
134121
const fraction = fractional / Math.pow(radix, base.length - dotIndex - 1);
@@ -144,22 +131,12 @@ function parseInteger(hex: string, bytes: number = 4): number {
144131
if (!hex.includes('0x')) {
145132
return parseInt(hex);
146133
}
147-
148-
const bigIntValue = BigInt(hex);
149-
const bitSize = bytes * 8;
150-
const signBit = BigInt(1) << BigInt(bitSize - 1);
151-
const mask = (BigInt(1) << BigInt(bitSize)) - BigInt(1);
152-
153-
// mask to the correct bit width
154-
const masked = bigIntValue & mask;
155-
// check if negative (sign bit is set)
156-
if (masked >= signBit) {
157-
// convert from two's complement
158-
const result = masked - (BigInt(1) << BigInt(bitSize));
159-
return Number(result);
134+
const mask = parseInt('0x80' + '00'.repeat(bytes - 1), 16);
135+
let integer = parseInt(hex, 16);
136+
if (integer >= mask) {
137+
integer = integer - mask * 2;
160138
}
161-
162-
return Number(masked);
139+
return integer;
163140
}
164141

165142
export function find(regex: RegExp, input: string) {

0 commit comments

Comments
 (0)