chore: integrate concurrent main changes with the dylib fix
Preserve the upstream WAT framing optimization alongside the qualified dylib fix. The combined tree passes 14 compiled dylib CLI tests and the full check:ci gate.
This commit is contained in:
commit
3a0e91dd91
8 files changed
+944
-22
No files matched your search
@@ -7,7 +7,7 @@ import type {
|
||||
WasmInterfaceSignature,
|
||||
WasmInterfaceValueType,
|
||||
} from "../domain/wasm/wasmInterface.js";
|
||||
import { watString } from "./WabtString.js";
|
||||
import { watString, watStringEnd } from "./WabtString.js";
|
||||
|
||||
/** Split direct WAT children only; function instruction bodies are never materialized. */
|
||||
const parts = (form: string): string[] => {
|
||||
@@ -40,12 +40,12 @@ const parts = (form: string): string[] => {
|
||||
}
|
||||
const start = i;
|
||||
if (form[i] === '"') {
|
||||
i = watString(form, i + 1).end;
|
||||
i = watStringEnd(form, i + 1);
|
||||
} else if (form[i] === "(") {
|
||||
let depth = 1;
|
||||
i++;
|
||||
while (i < form.length && depth > 0) {
|
||||
if (form[i] === '"') i = watString(form, i + 1).end;
|
||||
if (form[i] === '"') i = watStringEnd(form, i + 1);
|
||||
else {
|
||||
if (form[i] === "(") depth++;
|
||||
else if (form[i] === ")") depth--;
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { parseWabtWat } from "./WabtWat.js";
|
||||
import { watString, watStringEnd } from "./WabtString.js";
|
||||
|
||||
it("frames escaped quotes and trailing backslashes without consuming the next form", () => {
|
||||
const source = String.raw`prefix "a\"b\\" (export "next" (func 0))`;
|
||||
const end = source.indexOf(" (export");
|
||||
expect(watStringEnd(source, 8)).toBe(end);
|
||||
expect(watString(source, 8)).toEqual({
|
||||
bytes: Buffer.from('a"b\\'),
|
||||
end,
|
||||
});
|
||||
expect(watStringEnd('""', 1)).toBe(2);
|
||||
for (const count of [1, 2, 3, 4, 5]) {
|
||||
for (const tail of ['"tail', ""]) {
|
||||
const value = "\\".repeat(count) + tail;
|
||||
const encoded = JSON.stringify(value);
|
||||
const source = encoded + ' (export "next" (func 0))';
|
||||
expect(watStringEnd(source, 1)).toBe(encoded.length);
|
||||
expect(watString(source, 1).bytes).toEqual(Buffer.from(value));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects missing terminators, including a dangling escape or escaped last quote", () => {
|
||||
for (const source of ['"', '"payload', '"payload\\', '"payload\\"']) {
|
||||
expect(() => watStringEnd(source, 1)).toThrow("Incomplete WABT string");
|
||||
expect(() => watString(source, 1)).toThrow("Incomplete WABT string");
|
||||
expect(() => parseWabtWat(`(module (import ${source}`)).toThrow();
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves quoted WAT punctuation, nested comments, escaped names and unknown signatures", () => {
|
||||
const result = parseWabtWat(String.raw`(module
|
||||
(; outer " (; inner ;) ;)
|
||||
(import "env" "a\"b\\(;);;\00" (func (param i32)))
|
||||
(memory 1 (future-proposal "ignored\ff"))
|
||||
(export "memory" (memory 0))
|
||||
(data (i32.const 0) "(; );) ;; \" \\"))`);
|
||||
expect(result.interface.entries).toMatchObject([
|
||||
{
|
||||
direction: "import",
|
||||
name: 'a"b\\(;);;\0',
|
||||
signature: { kind: "func", params: ["i32"] },
|
||||
},
|
||||
{
|
||||
direction: "export",
|
||||
name: "memory",
|
||||
signature: null,
|
||||
issue: expect.any(String),
|
||||
},
|
||||
]);
|
||||
expect(() => parseWabtWat('(module (import "env" "\\q" (func)))')).toThrow(
|
||||
"Unsupported WABT name escape",
|
||||
);
|
||||
});
|
||||
+18
-10
@@ -1,13 +1,21 @@
|
||||
/** Decode a producer WAT string to frame objdump's raw display text. */
|
||||
export const watString = (form: string, start: number) => {
|
||||
let end = start;
|
||||
while (end < form.length && form[end] !== '"') {
|
||||
if (form[end] === "\\") end++;
|
||||
end++;
|
||||
/** Locate a WAT string's exclusive end without decoding or copying its payload. */
|
||||
export const watStringEnd = (form: string, start: number): number => {
|
||||
for (
|
||||
let end = form.indexOf('"', start);
|
||||
end !== -1;
|
||||
end = form.indexOf('"', end + 1)
|
||||
) {
|
||||
let escapes = end;
|
||||
while (escapes > start && form[escapes - 1] === "\\") escapes--;
|
||||
if ((end - escapes) % 2 === 0) return end + 1;
|
||||
}
|
||||
if (end === form.length)
|
||||
throw new Error("Incomplete WABT custom-section name.");
|
||||
const name = form.slice(start, end);
|
||||
throw new Error("Incomplete WABT string.");
|
||||
};
|
||||
|
||||
/** Decode a producer WAT string only where its bytes are interpreted. */
|
||||
export const watString = (form: string, start: number) => {
|
||||
const end = watStringEnd(form, start);
|
||||
const name = form.slice(start, end - 1);
|
||||
// Escapes shrink; literal UTF-8 has the same byte extent as its source.
|
||||
const bytes = Buffer.alloc(Buffer.byteLength(name, "utf8"));
|
||||
let length = 0;
|
||||
@@ -42,5 +50,5 @@ export const watString = (form: string, start: number) => {
|
||||
index += 2;
|
||||
}
|
||||
}
|
||||
return { bytes: bytes.subarray(0, length), end: end + 1 };
|
||||
return { bytes: bytes.subarray(0, length), end };
|
||||
};
|
||||
+11
-9
@@ -1,5 +1,5 @@
|
||||
import { parseWabtInterface } from "./WabtInterface.js";
|
||||
import { watString } from "./WabtString.js";
|
||||
import { watString, watStringEnd } from "./WabtString.js";
|
||||
|
||||
/** Read only module-name metadata from WABT's preserved name-section payload. */
|
||||
const moduleNames = (bytes: Uint8Array): string[] => {
|
||||
@@ -48,22 +48,24 @@ export const parseWabtWat = (text: string) => {
|
||||
const debugModuleNames: string[] = [];
|
||||
let depth = 0;
|
||||
let start = 0;
|
||||
let quoted = false;
|
||||
let commentDepth = 0;
|
||||
let lineComment = false;
|
||||
let closed = false;
|
||||
const boundaries = /[()";]/gu;
|
||||
for (let index = 0; index < text.length; index++) {
|
||||
// Instructions and whitespace inside a form cannot change its framing.
|
||||
if (depth > 0 && !lineComment && commentDepth === 0) {
|
||||
boundaries.lastIndex = index;
|
||||
const boundary = boundaries.exec(text);
|
||||
if (boundary === null) break;
|
||||
index = boundary.index;
|
||||
}
|
||||
const char = text[index];
|
||||
const next = text[index + 1];
|
||||
if (lineComment) {
|
||||
if (char === "\n") lineComment = false;
|
||||
continue;
|
||||
}
|
||||
if (quoted) {
|
||||
if (char === "\\") index++;
|
||||
else if (char === '"') quoted = false;
|
||||
continue;
|
||||
}
|
||||
if (commentDepth > 0) {
|
||||
if (char === "(" && next === ";") {
|
||||
commentDepth++;
|
||||
@@ -87,7 +89,7 @@ export const parseWabtWat = (text: string) => {
|
||||
if (char === '"') {
|
||||
if (depth === 0)
|
||||
throw new Error("Unexpected string outside WABT WAT module.");
|
||||
quoted = true;
|
||||
index = watStringEnd(text, index + 1) - 1;
|
||||
continue;
|
||||
}
|
||||
if (char === "(") {
|
||||
@@ -133,7 +135,7 @@ export const parseWabtWat = (text: string) => {
|
||||
} else if (depth === 0 && char !== undefined && !/\s/u.test(char))
|
||||
throw new Error("Unexpected text outside WABT WAT module.");
|
||||
}
|
||||
if (depth !== 0 || quoted || commentDepth !== 0)
|
||||
if (depth !== 0 || commentDepth !== 0)
|
||||
throw new Error("Incomplete WABT WAT output.");
|
||||
return {
|
||||
imports,
|
||||
|
||||
Reference in new issue
Block a user