Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions tests/latch/core/endianness_0.asserts.wast
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
(assert_return (invoke "i64_load32_u" (i64.const 42424242)) (i64.const 42424242))
(assert_return (invoke "i64_load" (i64.const -1)) (i64.const -1))
(assert_return (invoke "i64_load" (i64.const -42424242)) (i64.const -42424242))
(assert_return (invoke "i64_load" (i64.const 0xABAD1DEA)) (i64.const 0xABAD1DEA))
(assert_return (invoke "i32_store16" (i32.const -1)) (i32.const 0xFFFF))
(assert_return (invoke "i32_store16" (i32.const -4242)) (i32.const 61294))
(assert_return (invoke "i32_store16" (i32.const 42)) (i32.const 42))
Expand All @@ -43,4 +42,3 @@
(assert_return (invoke "i64_store32" (i64.const 42424242)) (i64.const 42424242))
(assert_return (invoke "i64_store" (i64.const -1)) (i64.const -1))
(assert_return (invoke "i64_store" (i64.const -42424242)) (i64.const -42424242))
(assert_return (invoke "i64_store" (i64.const 0xABAD1DEA)) (i64.const 0xABAD1DEA))
2 changes: 0 additions & 2 deletions tests/latch/core/i64_0.asserts.wast
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,10 @@
(assert_return (invoke "and" (i64.const 1) (i64.const 1)) (i64.const 1))
(assert_return (invoke "and" (i64.const 0) (i64.const 0)) (i64.const 0))
(assert_return (invoke "and" (i64.const 0x7fffffffffffffff) (i64.const 0x8000000000000000)) (i64.const 0))
(assert_return (invoke "and" (i64.const 0xf0f0ffff) (i64.const 0xfffff0f0)) (i64.const 0xf0f0f0f0))
(assert_return (invoke "or" (i64.const 1) (i64.const 0)) (i64.const 1))
(assert_return (invoke "or" (i64.const 0) (i64.const 1)) (i64.const 1))
(assert_return (invoke "or" (i64.const 1) (i64.const 1)) (i64.const 1))
(assert_return (invoke "or" (i64.const 0) (i64.const 0)) (i64.const 0))
(assert_return (invoke "or" (i64.const 0xf0f0ffff) (i64.const 0xfffff0f0)) (i64.const 0xffffffff))
(assert_return (invoke "xor" (i64.const 1) (i64.const 0)) (i64.const 1))
(assert_return (invoke "xor" (i64.const 0) (i64.const 1)) (i64.const 1))
(assert_return (invoke "xor" (i64.const 1) (i64.const 1)) (i64.const 0))
Expand Down
Binary file removed tests/latch/latch-0.5.1.tgz
Binary file not shown.
Binary file added tests/latch/latch-0.6.0.tgz
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/latch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"comptest": "npx ts-node ./src/comp.test.ts"
},
"devDependencies": {
"latch": "file:./latch-0.5.1.tgz",
"latch": "file:./latch-0.6.0.tgz",
"mqtt": "^4.3.7",
"serialport": "^10.4.0",
"typescript": "^4.5.5"
Expand Down
4 changes: 2 additions & 2 deletions tests/latch/src/spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ function createTest(module: string, asserts: string[]): TestScenario {
for (const assert of asserts) {
const cursor = {value: 0};
const func: string = find(/invoke "([^"]+)"/, assert);
const args: WASM.Value[] = parseArguments(assert.replace(`(invoke "${func} "`, ''), cursor);
const result: WASM.Value | undefined = parseResult(assert.slice(cursor.value));
const args: WASM.Value<WASM.Type>[] = parseArguments(assert.replace(`(invoke "${func} "`, ''), cursor);
const result: WASM.Value<WASM.Type> | undefined = parseResult(assert.slice(cursor.value));

steps.push({
title: assert,
Expand Down
60 changes: 32 additions & 28 deletions tests/latch/src/util/spec.util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {WASM} from "latch";
import {readFileSync} from "fs";
import Value = WASM.Value;
import WasmInt = WASM.WasmInt;

interface Cursor {
value: number;
}

export function parseResult(input: string): WASM.Value | undefined {
export function parseResult(input: string): WASM.Value<WASM.Type> | undefined {
let cursor = 0;
let delta: number = consume(input, cursor, /\(/d);
if (delta === 0) {
Expand All @@ -14,27 +16,32 @@ export function parseResult(input: string): WASM.Value | undefined {
cursor += delta;

delta = consume(input, cursor, /^[^.)]*/d);
const type: WASM.Type = WASM.typing.get(input.slice(cursor, cursor + delta)) ?? WASM.Type.i64;
let type: WASM.Type = WASM.typing.get(input.slice(cursor, cursor + delta)) ?? WASM.Integer.i64;

cursor += delta + consume(input, cursor + delta);

let value;
delta = consume(input, cursor, /^[^)]*/d);
if (type === WASM.Type.f32 || type === WASM.Type.f64) {
value = parseHexFloat(input.slice(cursor, cursor + delta));
} else {
value = parseInteger(input.slice(cursor, cursor + delta));
}
return parseValue(input.slice(cursor, cursor + delta), type);
}

if (value === undefined) {
return value;
function parseValue(input: string, type: WASM.Type): Value<WASM.Type> | undefined {
let value: number | bigint;
switch (type) {
case WASM.Float.f32:
case WASM.Float.f64:
return {value: parseHexFloat(input), type};
case WASM.Integer.u32:
case WASM.Integer.i32:
case WASM.Integer.u64:
case WASM.Integer.i64:
return {value: parseInteger(input, type), type};
default:
return undefined;
}

return {type, value};
}

export function parseArguments(input: string, index: Cursor): WASM.Value[] {
const args: WASM.Value[] = [];
export function parseArguments(input: string, index: Cursor): WASM.Value<WASM.Type>[] {
const args: WASM.Value<WASM.Type>[] = [];

let cursor: number = consume(input, 0, /invoke "[^"]+"/d);
while (cursor < input.length) {
Expand All @@ -45,19 +52,14 @@ export function parseArguments(input: string, index: Cursor): WASM.Value[] {
cursor += delta;

delta = consume(input, cursor, /^[^.)]*/d);
const type: WASM.Type = WASM.typing.get(input.slice(cursor + delta - 3, cursor + delta)) ?? WASM.Type.i64;
const type: WASM.Type = WASM.typing.get(input.slice(cursor + delta - 3, cursor + delta)) ?? WASM.Integer.i64;

cursor += delta + consume(input, cursor + delta, /^[^)]*const /d);
delta = consume(input, cursor, /^[^)]*/d);
let maybe: number | undefined;
if (type === WASM.Type.f32 || type === WASM.Type.f64) {
maybe = parseHexFloat(input.slice(cursor, cursor + delta));
} else {
maybe = parseInteger(input.slice(cursor, cursor + delta));
}
let maybe = parseValue(input.slice(cursor, cursor + delta), type);

if (maybe !== undefined) {
args.push({type, value: maybe});
args.push(maybe);
}

cursor += consume(input, cursor, /\)/d);
Expand Down Expand Up @@ -131,16 +133,18 @@ function parseHexFloat(input: string): number {
return mantissa * Math.pow(2, exponent);
}

function parseInteger(hex: string, bytes: number = 4): number {
function parseInteger(hex: string, type: WASM.Integer): WasmInt {
const bytes = type === WASM.Integer.u32 || type === WASM.Integer.i32 ? 4 : 8;
if (!hex.includes('0x')) {
return parseInt(hex);
const n: number = parseInt(hex);
return typeof n !== 'bigint' && isNaN(n) ? WasmInt.nan() : typeof n !== 'bigint' && n === Infinity ? WasmInt.infinity() : WasmInt.finite(BigInt(hex));
}
const mask = parseInt('0x80' + '00'.repeat(bytes - 1), 16);
let integer = parseInt(hex, 16);
const mask = BigInt(parseInt('0x80' + '00'.repeat(bytes - 1), 16));
let integer = BigInt(parseInt(hex, 16));
if (integer >= mask) {
integer = integer - mask * 2;
integer = integer - mask * 2n;
}
return integer;
return WasmInt.finite(integer);
}

export function find(regex: RegExp, input: string) {
Expand Down
Loading