From 8d3383ee400c02c8d2bb7b2dd4fb13e37622dd48 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 29 May 2025 16:13:34 +0800 Subject: [PATCH] refactor: merge Promise.all inside and outside of executor 2 layers promise make code hard to understand and refactor --- src/core/execute.ts | 83 ++++++++++++++++++++------------------------- tsconfig.json | 3 +- 2 files changed, 38 insertions(+), 48 deletions(-) diff --git a/src/core/execute.ts b/src/core/execute.ts index e8bd1a5..2afafc2 100644 --- a/src/core/execute.ts +++ b/src/core/execute.ts @@ -11,47 +11,40 @@ import { supplyDefaultFunction } from "../utils/index.js"; import { parseImportFunctionInfo } from "../utils/wasmparser.js"; const readFile = promises.readFile; -function nodeExecutor(wasms: string[], outFolder: string, imports: Imports) { - return Promise.all( - wasms.map(async (wasm) => { - const wasi = new WASI({ - args: ["node", basename(wasm)], - env: process.env, - preopens: { - "/": outFolder, - }, - version: "preview1", - }); +async function nodeExecutor(wasm: string, outFolder: string, imports: Imports) { + const wasi = new WASI({ + args: ["node", basename(wasm)], + env: process.env, + preopens: { + "/": outFolder, + }, + version: "preview1", + }); - const importsArg = new ImportsArgument(); - const userDefinedImportsObject = imports === null ? {} : imports(importsArg); - const importObject: ASImports = { - wasi_snapshot_preview1: wasi.wasiImport, - mockInstrument: mockInstruFunc, - ...covInstruFunc(wasm), - ...userDefinedImportsObject, - } as ASImports; - const binaryBuffer = await readFile(wasm); - const binary = binaryBuffer.buffer.slice( - binaryBuffer.byteOffset, - binaryBuffer.byteOffset + binaryBuffer.byteLength - ); - const importFuncList = parseImportFunctionInfo(binary as ArrayBuffer); - supplyDefaultFunction(importFuncList, importObject); - const ins = await instantiate(binary, importObject); - importsArg.module = ins.module; - importsArg.instance = ins.instance; - importsArg.exports = ins.exports; - try { - wasi.start(ins); - } catch (error) { - if (error instanceof Error) { - console.error(error.stack); - } - throw new Error("node executor abort."); - } - }) - ); + const importsArg = new ImportsArgument(); + const userDefinedImportsObject = imports === null ? {} : imports(importsArg); + const importObject: ASImports = { + wasi_snapshot_preview1: wasi.wasiImport, + mockInstrument: mockInstruFunc, + ...covInstruFunc(wasm), + ...userDefinedImportsObject, + } as ASImports; + const binaryBuffer = await readFile(wasm); + const binary = binaryBuffer.buffer.slice(binaryBuffer.byteOffset, binaryBuffer.byteOffset + binaryBuffer.byteLength); + const importFuncList = parseImportFunctionInfo(binary as ArrayBuffer); + supplyDefaultFunction(importFuncList, importObject); + const ins = await instantiate(binary, importObject); + importsArg.module = ins.module; + importsArg.instance = ins.instance; + importsArg.exports = ins.exports; + try { + wasi.start(ins); + } catch (error) { + if (error instanceof Error) { + console.error(error.stack); + } + throw new Error("node executor abort."); + } } export async function execWasmBinarys( @@ -61,13 +54,9 @@ export async function execWasmBinarys( ): Promise { const assertRes = new AssertResult(); ensureDirSync(outFolder); - - const wasmPaths = instrumentResult.map((res) => res.instrumentedWasm); - - await nodeExecutor(wasmPaths, outFolder, imports); - - await Promise.all( - instrumentResult.map(async (res) => { + await Promise.all( + instrumentResult.map(async (res): Promise => { + await nodeExecutor(res.instrumentedWasm, outFolder, imports); const { instrumentedWasm, expectInfo } = res; const assertLogFilePath = join(outFolder, basename(instrumentedWasm).slice(0, -4).concat("assert.log")); diff --git a/tsconfig.json b/tsconfig.json index d3ec056..4c1e6f6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,7 +25,8 @@ "noUncheckedIndexedAccess": true, "noImplicitOverride": true, "noPropertyAccessFromIndexSignature": true, - "skipLibCheck": true + "skipLibCheck": true, + "isolatedModules": true }, "include": ["src/**/*.ts", "transform", "tests/ts/**/*.ts"], "exclude": ["assembly", "example"]