Skip to content

Commit bfd044d

Browse files
committed
Revert "Fix precision loss integers (#49)"
This reverts commit 44b8b21.
1 parent 16378d3 commit bfd044d

10 files changed

Lines changed: 77 additions & 103 deletions

File tree

File renamed without changes.

.github/workflows/lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ jobs:
2828
env:
2929
SARIF_ESLINT_IGNORE_SUPPRESSED: "true"
3030
run: npx eslint src/**/*
31-
--config eslint.config.mjs
31+
--config .eslint.config.mjs
3232
--format @microsoft/eslint-formatter-sarif
3333
--output-file eslint-results.sarif
3434
continue-on-error: true
3535

3636
- name: Upload analysis results to GitHub
37-
uses: github/codeql-action/upload-sarif@v4
37+
uses: github/codeql-action/upload-sarif@v3
3838
with:
3939
sarif_file: eslint-results.sarif
4040
wait-for-processing: true

src/debug/WARDuino.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export namespace WARDuino {
55
import Frame = WASM.Frame;
66
import Table = WASM.Table;
77
import Memory = WASM.Memory;
8-
import Type = WASM.Type;
98

109
export interface CallbackMapping {
1110
callbackid: string;
@@ -78,9 +77,9 @@ export namespace WARDuino {
7877
pc_error?: number;
7978
exception_msg?: string;
8079
breakpoints?: number[];
81-
stack?: Value<Type>[];
80+
stack?: Value[];
8281
callstack?: Frame[];
83-
globals?: Value<Type>[];
82+
globals?: Value[];
8483
table?: Table;
8584
memory?: Memory;
8685
br_table?: BRTable;

src/framework/Testee.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function timeout<T>(label: string, time: number, promise: Promise<T>): Pr
2727
* @param field dot string describing the field of the value (or path)
2828
*/
2929
export function getValue(object: any, field: string): any {
30-
if (object?.type == WASM.Special.nothing) {
30+
if (object?.type == WASM.Type.nothing) {
3131
return undefined;
3232
}
3333

src/framework/scenario/Invoker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class Invoker implements Step {
1212
readonly expected?: Expectation[];
1313
readonly target?: Target;
1414

15-
constructor(func: string, args: Value<Type>[], result: Value<Type> | undefined, target?: Target) {
15+
constructor(func: string, args: Value[], result: Value | undefined, target?: Target) {
1616
let prefix = '';
1717
this.instruction = invoke(func, args);
1818
this.expected = (result == undefined) ? returns(nothing) : returns(result);
@@ -24,12 +24,12 @@ export class Invoker implements Step {
2424
}
2525
}
2626

27-
export function invoke(func: string, args: Value<Type>[]): Instruction {
27+
export function invoke(func: string, args: Value[]): Instruction {
2828
return {kind: Kind.Request, value: Message.invoke(func, args)};
2929
}
3030

31-
export function returns(n: Value<Type>): Expectation[] {
32-
if (n.type === WASM.Special.nothing) {
31+
export function returns(n: Value): Expectation[] {
32+
if (n.type == Type.nothing) {
3333
return [{'value': {kind: 'primitive', value: undefined} as Expected<undefined>}]
3434
}
3535
return [{'value': {kind: 'primitive', value: n.value} as Expected<number>}]

src/messaging/Message.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {WARDuino} from '../debug/WARDuino';
22
import {ackParser, breakpointParser, invokeParser, stateParser} from './Parsers';
33
import {Breakpoint} from '../debug/Breakpoint';
44
import {WASM} from '../sourcemap/Wasm';
5-
import ieee754 from 'ieee754';
5+
import {write} from 'ieee754';
66
import {SourceMap} from '../sourcemap/SourceMap';
77
import {readFileSync} from 'fs';
88
import {CompileOutput, CompilerFactory} from '../manage/Compiler';
@@ -11,7 +11,6 @@ import Interrupt = WARDuino.Interrupt;
1111
import State = WARDuino.State;
1212
import Value = WASM.Value;
1313
import Type = WASM.Type;
14-
import Float = WASM.Float;
1514

1615
// An acknowledgement returned by the debugger
1716
export interface Ack {
@@ -28,7 +27,6 @@ export interface Request<R> {
2827
}
2928

3029
export namespace Message {
31-
import Float = WASM.Float;
3230
import Inspect = WARDuino.Inspect;
3331
export const run: Request<Ack> = {
3432
type: Interrupt.run,
@@ -138,7 +136,7 @@ export namespace Message {
138136
export function updateModule(wasm: string): Request<Ack> {
139137
function payload(binary: Buffer): string {
140138
const w = new Uint8Array(binary);
141-
const sizeHex: string = WASM.leb128(BigInt(w.length));
139+
const sizeHex: string = WASM.leb128(w.length);
142140
const sizeBuffer = Buffer.allocUnsafe(4);
143141
sizeBuffer.writeUint32BE(w.length);
144142
const wasmHex = Buffer.from(w).toString('hex');
@@ -164,7 +162,7 @@ export namespace Message {
164162
}
165163
}
166164

167-
export function invoke(func: string, args: Value<Type>[]): Request<WASM.Value<Type> | Exception> {
165+
export function invoke(func: string, args: Value[]): Request<WASM.Value | Exception> {
168166
function fidx(map: SourceMap.Mapping, func: string): number {
169167
const fidx: number | void = map.functions.find((closure: SourceMap.Closure) => closure.name === func)?.index;
170168
if (fidx === undefined) {
@@ -173,28 +171,23 @@ export namespace Message {
173171
return fidx!;
174172
}
175173

176-
function convert(args: Value<Type>[]) {
174+
function convert(args: Value[]) {
177175
let payload: string = '';
178-
args.forEach((arg: Value<Type>) => {
179-
switch (arg.type) {
180-
case WASM.Float.f32:
181-
case WASM.Float.f64:
182-
payload += ieeefloat(<Value<Float>>arg)
183-
break;
184-
case WASM.Integer.i32:
185-
case WASM.Integer.i64:
186-
payload += WASM.leb128(<bigint>arg.value);
187-
break;
188-
default:
189-
break;
176+
args.forEach((arg: Value) => {
177+
if (arg.type === Type.i32 || arg.type === Type.i64) {
178+
payload += WASM.leb128(arg.value);
179+
} else {
180+
const buff = Buffer.alloc(arg.type === Type.f32 ? 4 : 8);
181+
write(buff, arg.value, 0, true, arg.type === Type.f32 ? 23 : 52, buff.length);
182+
payload += buff.toString('hex');
190183
}
191184
});
192185
return payload;
193186
}
194187

195188
return {
196189
type: Interrupt.invoke,
197-
payload: (map: SourceMap.Mapping) => `${WASM.leb128(BigInt(fidx(map, func)))}${convert(args)}`,
190+
payload: (map: SourceMap.Mapping) => `${WASM.leb128(fidx(map, func))}${convert(args)}`,
198191
parser: invokeParser
199192
}
200193
}
@@ -226,9 +219,3 @@ export namespace Message {
226219
}
227220
};
228221
}
229-
230-
function ieeefloat(arg: Value<Float>): String {
231-
const buff = Buffer.alloc(arg.type === Float.f32 ? 4 : 8);
232-
ieee754.write(buff, <number>arg.value, 0, true, arg.type === Float.f32 ? 23 : 52, buff.length); // TODO write BigInt without loss of precision (don't use ieee754.write)
233-
return buff.toString('hex');
234-
}

src/messaging/Parsers.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {Breakpoint} from '../debug/Breakpoint';
55
import {WARDuino} from '../debug/WARDuino';
66
import State = WARDuino.State;
77
import nothing = WASM.nothing;
8-
import Type = WASM.Type;
98

109
export function identityParser(text: string) {
1110
return stripEnd(text);
@@ -15,7 +14,7 @@ export function stateParser(text: string): State {
1514
return JSON.parse(text);
1615
}
1716

18-
export function invokeParser(text: string): WASM.Value<Type> | Exception {
17+
export function invokeParser(text: string): WASM.Value | Exception {
1918
if (exception(text)) {
2019
return {text: text};
2120
}
@@ -59,21 +58,21 @@ export function breakpointHitParser(text: string): Breakpoint {
5958
throw new Error('Could not messaging BREAKPOINT address in ack.');
6059
}
6160

62-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
63-
function stacking(objects: {value: any, type: any}[]): WASM.Value<Type>[] {
64-
const stacked: WASM.Value<Type>[] = [];
61+
function stacking(objects: {value: any, type: any}[]): WASM.Value[] {
62+
const stacked: WASM.Value[] = [];
6563
for (const object of objects) {
6664
let value: number = object.value;
67-
const type: WASM.Type = WASM.typing.get(object.type.toLowerCase()) ?? WASM.Special.unknown;
68-
if (type === WASM.Float.f32 || type === WASM.Float.f64) {
65+
const type: WASM.Type = WASM.typing.get(object.type.toLowerCase()) ?? WASM.Type.unknown;
66+
if (type === WASM.Type.f32 || type === WASM.Type.f64) {
6967
const buff = Buffer.from(object.value, 'hex');
70-
value = ieee754.read(buff, 0, false, type === WASM.Float.f32 ? 23 : 52, buff.length);
68+
value = ieee754.read(buff, 0, false, type === WASM.Type.f32 ? 23 : 52, buff.length);
7169
}
7270
stacked.push({value: value, type: type});
7371
}
7472
return stacked;
7573
}
7674

75+
7776
// Strips all trailing newlines
7877
function stripEnd(text: string): string {
7978
return text.replace(/\s+$/g, '');

src/sourcemap/Wasm.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,45 @@
1-
import * as leb from "@thi.ng/leb128";
2-
31
export namespace WASM {
4-
5-
export enum Float {
2+
export enum Type {
63
f32,
74
f64,
8-
}
9-
10-
export enum Integer {
115
i32,
126
i64,
13-
}
14-
15-
export enum Special {
167
nothing,
178
unknown
189
}
1910

20-
export type Type = Float | Integer | Special;
21-
22-
2311
export const typing = new Map<string, Type>([
24-
['f32', Float.f32],
25-
['f64', Float.f64],
26-
['i32', Integer.i32],
27-
['i64', Integer.i64]
12+
['f32', Type.f32],
13+
['f64', Type.f64],
14+
['i32', Type.i32],
15+
['i64', Type.i64]
2816
]);
2917

30-
export interface Value<T extends Type> {
31-
type: T;
32-
value: T extends Float ? number : bigint;
18+
export interface Value {
19+
type: Type;
20+
value: number;
3321
}
3422

35-
export type Nothing = Value<Type>
23+
export interface Nothing extends Value {}
3624

3725
export const nothing: Nothing = {
38-
type: Special.nothing, value: 0
26+
type: Type.nothing, value: 0
3927
}
4028

41-
export function i32(n: bigint): WASM.Value<Integer> {
42-
return {value: n, type: Integer.i32};
29+
export function i32(n: number): WASM.Value {
30+
return {value: n, type: Type.i32};
4331
}
4432

45-
export function f32(n: number): WASM.Value<Float> {
46-
return {value: n, type: Float.f32};
33+
export function f32(n: number): WASM.Value {
34+
return {value: n, type: Type.f32};
4735
}
4836

49-
export function f64(n: number): WASM.Value<Float> {
50-
return {value: n, type: Float.f64};
37+
export function f64(n: number): WASM.Value {
38+
return {value: n, type: Type.f64};
5139
}
5240

53-
export function i64(n: bigint): WASM.Value<Integer> {
54-
return {value: n, type: Integer.i64};
41+
export function i64(n: number): WASM.Value {
42+
return {value: n, type: Type.i64};
5543
}
5644

5745
export interface Frame {
@@ -77,5 +65,21 @@ export namespace WASM {
7765
bytes: Uint8Array;
7866
}
7967

80-
export const leb128 = (v: number | bigint) => Buffer.from(leb.encodeSLEB128(v)).toString('hex').toUpperCase().padStart(2, '0')
68+
export function leb128(a: number): string { // TODO can only handle 32 bit
69+
a |= 0;
70+
const result = [];
71+
while (true) {
72+
const byte_ = a & 0x7f;
73+
a >>= 7;
74+
if (
75+
(a === 0 && (byte_ & 0x40) === 0) ||
76+
(a === -1 && (byte_ & 0x40) !== 0)
77+
) {
78+
result.push(byte_.toString(16).padStart(2, '0'));
79+
return result.join('').toUpperCase();
80+
}
81+
result.push((byte_ | 0x80).toString(16).padStart(2, '0'));
82+
}
83+
}
84+
8185
}

tests/examples/example.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ spec.testee('emulator[:8100]', new EmulatorSpecification(8100));
2525
const steps: Step[] = [];
2626

2727
// ✔ ((invoke "8u_good1" (i32.const 0)) (i32.const 97))
28-
steps.push(new Invoker('8u_good1', [WASM.i32(BigInt(0))], WASM.i32(BigInt(97))));
28+
steps.push(new Invoker('8u_good1', [WASM.i32(0)], WASM.i32(97)));
2929

3030
// ✔ ((invoke "8u_good3" (i32.const 0)) (i32.const 98))
31-
steps.push(new Invoker('8u_good3', [WASM.i32(BigInt(0))], WASM.i32(BigInt(98))));
31+
steps.push(new Invoker('8u_good3', [WASM.i32(0)], WASM.i32(98)));
3232

3333
// ✔ ((invoke "func-unwind-by-br"))
3434
steps.push(new Invoker('func-unwind-by-br', [], undefined));
@@ -110,10 +110,10 @@ reverse.test({
110110
}
111111
}],
112112
},
113-
new Invoker('read', [WASM.i32(BigInt(15))], WASM.i32(BigInt(0))),
114-
new Invoker('write', [WASM.i32(BigInt(15)), WASM.i32(BigInt(1))], undefined),
115-
new Invoker('read', [WASM.i32(BigInt(15))], WASM.i32(BigInt(1)))]
113+
new Invoker('read', [WASM.i32(15)], WASM.i32(0)),
114+
new Invoker('write', [WASM.i32(15), WASM.i32(1)], undefined),
115+
new Invoker('read', [WASM.i32(15)], WASM.i32(1))]
116116
})
117117

118118
framework.reporter.verbosity(Verbosity.debug);
119-
framework.analyse([spec, debug, reverse]);
119+
framework.analyse([spec, debug]);

tests/unit/sourcemap.test.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,19 @@ import {SourceMap} from '../../src/sourcemap/SourceMap';
55
import {WABT} from '../../src/util/env';
66
import {copyFileSync, mkdtempSync, readFileSync, rmSync} from 'fs';
77

8-
import * as leb from '@thi.ng/leb128';
9-
108
const artifacts = `${__dirname}/../../../tests/artifacts`;
119

1210
/**
1311
* Check LEB 128 encoding
1412
*/
15-
test('[leb128] : test encode positive numbers', t => {
16-
t.is(WASM.leb128(BigInt(0)), '00');
17-
t.is(WASM.leb128(BigInt(1)), '01');
18-
t.is(WASM.leb128(BigInt(8)), '08');
19-
t.is(WASM.leb128(BigInt(32)), '20');
20-
t.is(WASM.leb128(BigInt(64)), 'C000');
21-
t.is(WASM.leb128(BigInt(127)), 'FF00');
22-
t.is(WASM.leb128(BigInt(128)), '8001');
23-
t.is(WASM.leb128(BigInt(1202)), 'B209');
24-
t.is(WASM.leb128(BigInt(2147483647)), 'FFFFFFFF07');
25-
t.is(WASM.leb128(BigInt(4294967295)), 'FFFFFFFF0F');
26-
t.is(WASM.leb128(Number.MAX_SAFE_INTEGER), 'FFFFFFFFFFFFFF0F');
27-
});
28-
29-
test('[leb128] : test encode negative numbers', t => {
30-
t.is(WASM.leb128(BigInt(-1)), '7F');
31-
t.is(WASM.leb128(BigInt(-8)), '78');
32-
t.is(WASM.leb128(BigInt(-32)), '60');
33-
t.is(WASM.leb128(BigInt(-64)), '40');
34-
t.is(WASM.leb128(BigInt(-127)), '817F');
35-
t.is(WASM.leb128(BigInt(-128)), '807F');
13+
test('[leb128] : test encoding', t => {
14+
t.is(WASM.leb128(0), '00');
15+
t.is(WASM.leb128(1), '01');
16+
t.is(WASM.leb128(8), '08');
17+
t.is(WASM.leb128(32), '20');
18+
t.is(WASM.leb128(64), 'C000');
19+
t.is(WASM.leb128(128), '8001');
20+
t.is(WASM.leb128(1202), 'B209');
3621
});
3722

3823
test('[extractLineInfo] : test against artifacts (1)', async t => {

0 commit comments

Comments
 (0)