Skip to content

Commit d4fdc85

Browse files
committed
Implement missing parts
1 parent 558fbdc commit d4fdc85

6 files changed

Lines changed: 138 additions & 15 deletions

File tree

‎src/framework/Testee.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,19 @@ export class Testee { // TODO unified with testbed interface
319319
recover: (t: Testee) => Promise<any>,
320320
retries: number = 0): Promise<object | void> {
321321
let result: object | void = undefined;
322-
let error;
323-
while (0 <= retries && result === undefined) {
324-
result = await attempt(testee, step, map).catch(async (err) => {
322+
let error: unknown;
323+
let succeeded = false;
324+
while (0 <= retries && !succeeded) {
325+
try {
326+
result = await attempt(testee, step, map);
327+
succeeded = true;
328+
} catch (err) {
325329
error = err;
326330
await recover(testee);
327-
});
328-
retries--;
331+
retries--;
332+
}
329333
}
330-
return (result === undefined) ? Promise.reject(error) : result;
334+
return succeeded ? result! : Promise.reject(error);
331335
}
332336

333337
public skipall(): Testee {

‎src/messaging/Message.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {WARDuino} from '../debug/WARDuino';
22
import {Breakpoint} from '../debug/Breakpoint';
33
import {WASM} from '../sourcemap/Wasm';
44
import {SourceMap} from '../sourcemap/SourceMap';
5-
import {readFileSync} from 'fs';
5+
import {readFileSync} from "fs";
6+
import {remoteFunctionResultParser} from "./Parsers";
67
import {CompileOutput, CompilerFactory} from '../manage/Compiler';
78
import {WABT} from '../util/env';
89
import WasmValue = WASM.Value;
@@ -22,7 +23,6 @@ import {
2223
NotificationType,
2324
OperationResult,
2425
RemoteFunctionCall,
25-
RemoteFunctionResult,
2626
Snapshot,
2727
Value as ProtocolValue
2828
} from '../protocol/vendor/debug';
@@ -54,7 +54,7 @@ export const notificationParsers = {
5454
[NotificationType.NOTIFICATION_MALFORMED]: emptyNotification,
5555
[NotificationType.NOTIFICATION_UNKNOWN_COMMAND]: emptyNotification,
5656
[NotificationType.NOTIFICATION_OPERATION_RESULT]: OperationResult.decode,
57-
[NotificationType.NOTIFICATION_REMOTE_FUNCTION_RESULT]: RemoteFunctionResult.decode,
57+
[NotificationType.NOTIFICATION_REMOTE_FUNCTION_RESULT]: remoteFunctionResultParser,
5858
[NotificationType.NOTIFICATION_CHECKPOINT]: Checkpoint.decode
5959
} as const;
6060

@@ -191,7 +191,7 @@ export namespace Message {
191191
}
192192
}
193193

194-
export function invoke(func: string, args: WasmValue<Type>[]): Request<RemoteFunctionResult> {
194+
export function invoke(func: string, args: WasmValue<Type>[]): Request<WasmValue<Type> | Exception> {
195195
function fidx(map: SourceMap.Mapping, func: string): number {
196196
const index: number | void = map.functions.find((closure: SourceMap.Closure) => closure.name === func)?.index;
197197
if (index === undefined) {

‎src/messaging/Parsers.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import nothing = WASM.nothing;
99
import Type = WASM.Type;
1010
import WasmInt = WASM.WasmInt;
1111
import ieee754 from "ieee754";
12+
import {RemoteFunctionResult, Value as ProtocolValue} from "../protocol/vendor/debug";
1213

1314
export function identityParser(text: string) {
1415
return stripEnd(text);
@@ -29,6 +30,57 @@ export function invokeParser(text: string): WASM.Value<Type> | Exception {
2930
return stacking(stack)[stack.length - 1];
3031
}
3132

33+
/** Convert a protobuf remote-function response into the Latch WASM value contract. */
34+
export function remoteFunctionResultParser(payload: Uint8Array): WASM.Value<Type> | Exception {
35+
return remoteFunctionResult(RemoteFunctionResult.decode(payload));
36+
}
37+
38+
export function remoteFunctionResult(result: RemoteFunctionResult): WASM.Value<Type> | Exception {
39+
if (!result.success) {
40+
return {text: result.error.toString("utf8") || "Remote function invocation failed."};
41+
}
42+
if (result.results.length === 0) {
43+
return nothing;
44+
}
45+
return protocolValue(result.results[result.results.length - 1]);
46+
}
47+
48+
function protocolValue(value: ProtocolValue): WASM.Value<Type> {
49+
const fields = [
50+
value.i32Bits === undefined ? undefined : "i32",
51+
value.i64Bits === undefined ? undefined : "i64",
52+
value.f32Bits === undefined ? undefined : "f32",
53+
value.f64Bits === undefined ? undefined : "f64"
54+
].filter((field): field is string => field !== undefined);
55+
56+
if (fields.length !== 1 || value.raw !== undefined) {
57+
throw Error("Remote function result contains no supported WASM value.");
58+
}
59+
60+
switch (fields[0]) {
61+
case "i32":
62+
return {value: WasmInt.finite(signed(BigInt(value.i32Bits!), 32)), type: WASM.Integer.i32};
63+
case "i64":
64+
return {value: WasmInt.finite(signed(value.i64Bits!, 64)), type: WASM.Integer.i64};
65+
case "f32":
66+
return {value: floatFromBits(value.f32Bits!, 4), type: WASM.Float.f32};
67+
case "f64":
68+
return {value: floatFromBits(value.f64Bits!, 8), type: WASM.Float.f64};
69+
default:
70+
throw Error("Remote function result contains no supported WASM value.");
71+
}
72+
}
73+
74+
function floatFromBits(bits: number | bigint, bytes: 4 | 8): number {
75+
const buffer = Buffer.allocUnsafe(bytes);
76+
if (bytes === 4) {
77+
buffer.writeUInt32LE(Number(bits));
78+
return buffer.readFloatLE();
79+
}
80+
buffer.writeBigUInt64LE(BigInt(bits));
81+
return buffer.readDoubleLE();
82+
}
83+
3284
function exception(text: string): boolean {
3385
return text.length > 1 && text.toLowerCase().includes('exception') && text.trim()[0] !== '{';
3486
}

‎tests/unit/interface.test.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
NotificationType,
1212
OperationResult
1313
} from '../../src/protocol/vendor/debug';
14-
import {WARDuino} from '../../src/debug/WARDuino';
14+
import {WARDuino} from "../../src/debug/WARDuino";
15+
import {Testee} from "../../src/framework/Testee";
16+
import {EmulatorSpecification} from "../../src/testbeds/TestbedSpecification";
1517

1618
class TestChannel extends Duplex {
1719
readonly sent: Buffer[] = [];
@@ -43,6 +45,23 @@ class TestPlatform extends Platform {
4345

4446
const tick = () => new Promise<void>(resolve => setImmediate(resolve));
4547

48+
test("[testee] successful void requests are not retried", async t => {
49+
const testee = new Testee("test", new EmulatorSpecification(0), 0, 0);
50+
const recoverable = (testee as unknown as {recoverable: (...args: any[]) => Promise<unknown>}).recoverable;
51+
let attempts = 0;
52+
let recoveries = 0;
53+
54+
const result = await recoverable(
55+
testee, Message.run, new SourceMap.Mapping(),
56+
async () => { attempts++; },
57+
async () => { recoveries++; }, 1
58+
);
59+
60+
t.is(result, undefined);
61+
t.is(attempts, 1);
62+
t.is(recoveries, 0);
63+
});
64+
4665
// file is currently excluded from tests
4766

4867
test('[warduino] start emulator', t => {

‎tests/unit/messaging.test.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {MessageQueue} from '../../src/messaging/MessageQueue';
33
import {Message} from '../../src/messaging/Message';
44
import {WASM} from '../../src/sourcemap/Wasm';
55
import {SourceMap} from '../../src/sourcemap/SourceMap';
6-
import {RemoteFunctionCall} from '../../src/protocol/vendor/debug';
6+
import {RemoteFunctionCall, RemoteFunctionResult} from "../../src/protocol/vendor/debug";
77

88
const alphanumerical = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.split('');
99

@@ -27,7 +27,22 @@ test('[Message.invoke] : encode 64-bit integer arguments without precision loss'
2727
]);
2828
});
2929

30-
test('[MessageQueue] : test EOM detection', t => {
30+
test("[Message.invoke] : decodes an i64 reply to the legacy value contract", t => {
31+
const request = Message.invoke("unused", []);
32+
const result = request.parser(RemoteFunctionResult.encode({
33+
success: true,
34+
results: [{i64Bits: 0n, index: 0}],
35+
error: Buffer.alloc(0)
36+
}).finish());
37+
38+
t.false("text" in result);
39+
if ("text" in result) return;
40+
t.is(result.type, WASM.Integer.i64);
41+
t.is((result.value as WASM.WasmInt).toBigInt(), 0n);
42+
t.false("success" in result);
43+
});
44+
45+
test("[MessageQueue] : test EOM detection", t => {
3146
const fuzzer = fuzzy(alphanumerical);
3247
const newline = new MessageQueue('\n');
3348
newline.push(fuzzer(9));

‎tests/unit/parsing.test.ts‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from 'ava';
2-
import {invokeParser, signed} from "../../src/messaging/Parsers";
2+
import {invokeParser, remoteFunctionResultParser, signed} from "../../src/messaging/Parsers";
33
import {Exception, Expected, Kind, Message, Step, WASM} from "../../src";
4+
import {RemoteFunctionResult} from "../../src/protocol/vendor/debug";
45
import {Verifier} from "../../src/framework/Verifier";
56
import Type = WASM.Type;
67
import WasmInt = WASM.WasmInt;
@@ -112,7 +113,39 @@ test('[invoke parser] : f32 decimal bit pattern with alpha hex equivalent', t =>
112113
t.is(result.value, -8.881785255792436e-16);
113114
});
114115

115-
test('[verifier] : numeric mismatch is reported as failure, not missing field', t => {
116+
test("[protobuf invoke result] : decodes signed i64 boundaries", t => {
117+
for (const [bits, expected] of [[0n, 0n], [0xffffffffffffffffn, -1n], [0x8000000000000000n, -9223372036854775808n]]) {
118+
const result = remoteFunctionResultParser(RemoteFunctionResult.encode({success: true, results: [{i64Bits: bits, index: 0}], error: Buffer.alloc(0)}).finish());
119+
t.false("text" in result);
120+
if ("text" in result) return;
121+
t.is(result.type, WASM.Integer.i64);
122+
t.is((result.value as WasmInt).toBigInt(), expected);
123+
}
124+
});
125+
126+
test("[protobuf invoke result] : decodes IEEE-754 float bits", t => {
127+
const f32 = remoteFunctionResultParser(RemoteFunctionResult.encode({success: true, results: [{f32Bits: 0x3fc00000, index: 0}], error: Buffer.alloc(0)}).finish());
128+
const f64 = remoteFunctionResultParser(RemoteFunctionResult.encode({success: true, results: [{f64Bits: 0x4004000000000000n, index: 0}], error: Buffer.alloc(0)}).finish());
129+
130+
t.false("text" in f32);
131+
t.false("text" in f64);
132+
if ("text" in f32 || "text" in f64) return;
133+
t.deepEqual(f32, {type: WASM.Float.f32, value: 1.5});
134+
t.deepEqual(f64, {type: WASM.Float.f64, value: 2.5});
135+
});
136+
137+
test("[protobuf invoke result] : maps void, malformed, and failed responses", t => {
138+
const voidResult = remoteFunctionResultParser(RemoteFunctionResult.encode({success: true, results: [], error: Buffer.alloc(0)}).finish());
139+
t.deepEqual(voidResult, WASM.nothing);
140+
141+
const failure = remoteFunctionResultParser(RemoteFunctionResult.encode({success: false, results: [], error: Buffer.from("division by zero")}).finish());
142+
t.deepEqual(failure, {text: "division by zero"});
143+
144+
const malformed = RemoteFunctionResult.encode({success: true, results: [{index: 0}], error: Buffer.alloc(0)}).finish();
145+
t.throws(() => remoteFunctionResultParser(malformed), {message: /no supported WASM value/});
146+
});
147+
148+
test("[verifier] : numeric mismatch is reported as failure, not missing field", t => {
116149
const step: Step = {
117150
title: 'CHECK: numeric mismatch',
118151
instruction: {

0 commit comments

Comments
 (0)