Skip to content

Commit 4eeb963

Browse files
authored
refactor: merge Promise.all inside and outside of executor (#35)
1 parent 3f6537e commit 4eeb963

File tree

2 files changed

+38
-48
lines changed

2 files changed

+38
-48
lines changed

src/core/execute.ts

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,40 @@ import { supplyDefaultFunction } from "../utils/index.js";
1111
import { parseImportFunctionInfo } from "../utils/wasmparser.js";
1212
const readFile = promises.readFile;
1313

14-
function nodeExecutor(wasms: string[], outFolder: string, imports: Imports) {
15-
return Promise.all(
16-
wasms.map(async (wasm) => {
17-
const wasi = new WASI({
18-
args: ["node", basename(wasm)],
19-
env: process.env,
20-
preopens: {
21-
"/": outFolder,
22-
},
23-
version: "preview1",
24-
});
14+
async function nodeExecutor(wasm: string, outFolder: string, imports: Imports) {
15+
const wasi = new WASI({
16+
args: ["node", basename(wasm)],
17+
env: process.env,
18+
preopens: {
19+
"/": outFolder,
20+
},
21+
version: "preview1",
22+
});
2523

26-
const importsArg = new ImportsArgument();
27-
const userDefinedImportsObject = imports === null ? {} : imports(importsArg);
28-
const importObject: ASImports = {
29-
wasi_snapshot_preview1: wasi.wasiImport,
30-
mockInstrument: mockInstruFunc,
31-
...covInstruFunc(wasm),
32-
...userDefinedImportsObject,
33-
} as ASImports;
34-
const binaryBuffer = await readFile(wasm);
35-
const binary = binaryBuffer.buffer.slice(
36-
binaryBuffer.byteOffset,
37-
binaryBuffer.byteOffset + binaryBuffer.byteLength
38-
);
39-
const importFuncList = parseImportFunctionInfo(binary as ArrayBuffer);
40-
supplyDefaultFunction(importFuncList, importObject);
41-
const ins = await instantiate(binary, importObject);
42-
importsArg.module = ins.module;
43-
importsArg.instance = ins.instance;
44-
importsArg.exports = ins.exports;
45-
try {
46-
wasi.start(ins);
47-
} catch (error) {
48-
if (error instanceof Error) {
49-
console.error(error.stack);
50-
}
51-
throw new Error("node executor abort.");
52-
}
53-
})
54-
);
24+
const importsArg = new ImportsArgument();
25+
const userDefinedImportsObject = imports === null ? {} : imports(importsArg);
26+
const importObject: ASImports = {
27+
wasi_snapshot_preview1: wasi.wasiImport,
28+
mockInstrument: mockInstruFunc,
29+
...covInstruFunc(wasm),
30+
...userDefinedImportsObject,
31+
} as ASImports;
32+
const binaryBuffer = await readFile(wasm);
33+
const binary = binaryBuffer.buffer.slice(binaryBuffer.byteOffset, binaryBuffer.byteOffset + binaryBuffer.byteLength);
34+
const importFuncList = parseImportFunctionInfo(binary as ArrayBuffer);
35+
supplyDefaultFunction(importFuncList, importObject);
36+
const ins = await instantiate(binary, importObject);
37+
importsArg.module = ins.module;
38+
importsArg.instance = ins.instance;
39+
importsArg.exports = ins.exports;
40+
try {
41+
wasi.start(ins);
42+
} catch (error) {
43+
if (error instanceof Error) {
44+
console.error(error.stack);
45+
}
46+
throw new Error("node executor abort.");
47+
}
5548
}
5649

5750
export async function execWasmBinarys(
@@ -61,13 +54,9 @@ export async function execWasmBinarys(
6154
): Promise<AssertResult> {
6255
const assertRes = new AssertResult();
6356
ensureDirSync(outFolder);
64-
65-
const wasmPaths = instrumentResult.map((res) => res.instrumentedWasm);
66-
67-
await nodeExecutor(wasmPaths, outFolder, imports);
68-
69-
await Promise.all(
70-
instrumentResult.map(async (res) => {
57+
await Promise.all<void>(
58+
instrumentResult.map(async (res): Promise<void> => {
59+
await nodeExecutor(res.instrumentedWasm, outFolder, imports);
7160
const { instrumentedWasm, expectInfo } = res;
7261
const assertLogFilePath = join(outFolder, basename(instrumentedWasm).slice(0, -4).concat("assert.log"));
7362

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"noUncheckedIndexedAccess": true,
2626
"noImplicitOverride": true,
2727
"noPropertyAccessFromIndexSignature": true,
28-
"skipLibCheck": true
28+
"skipLibCheck": true,
29+
"isolatedModules": true
2930
},
3031
"include": ["src/**/*.ts", "transform", "tests/ts/**/*.ts"],
3132
"exclude": ["assembly", "example"]

0 commit comments

Comments
 (0)