Skip to content

Commit f38a724

Browse files
committed
rewrite default test and refactor
1 parent 8663dce commit f38a724

File tree

6 files changed

+75
-80
lines changed

6 files changed

+75
-80
lines changed

.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66
/tests/ts/fixture
77
/third_party
88
/tests/cpp/lit/**/*.json
9-
/tests/as/default.test.ts
109
/.cache
1110
/.github/CODEOWNERS

src/core/execute.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { instantiate, Imports as ASImports } from "@assemblyscript/loader";
66
import { AssertResult } from "../assertResult.js";
77
import { Imports, ImportsArgument } from "../index.js";
88
import { IAssertResult, InstrumentResult } from "../interface.js";
9-
import { mockInstruFunc, covInstruFunc, parseWasmImports } from "../utils/import.js";
9+
import { mockInstruFunc, covInstruFunc } from "../utils/import.js";
10+
import { parseWasmImports, supplyDefaultFunction } from "../utils/index.js";
1011
const readFile = promises.readFile;
1112

1213
function nodeExecutor(wasms: string[], outFolder: string, imports: Imports) {
@@ -32,23 +33,7 @@ function nodeExecutor(wasms: string[], outFolder: string, imports: Imports) {
3233
const binary = await readFile(wasm);
3334
const importList = await parseWasmImports(binary);
3435
// supplying default function here, so no more need to define all of them in as-test.js
35-
for (const imp of importList) {
36-
if (imp.kind === "function") {
37-
const moduleName = imp.module;
38-
const funcName = imp.name;
39-
if (!importObject[moduleName]?.[funcName]) {
40-
if (!importObject[moduleName]) {
41-
importObject[moduleName] = {};
42-
}
43-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
44-
(importObject[moduleName] as any)[funcName] = (...args: any[]): any => {
45-
// notify that a default function has been called
46-
console.log(`Default stub called for ${moduleName}.${funcName}, args:`, args);
47-
return 0;
48-
};
49-
}
50-
}
51-
}
36+
supplyDefaultFunction(importList, importObject);
5237
const ins = await instantiate(binary, importObject);
5338
importsArg.module = ins.module;
5439
importsArg.instance = ins.instance;

src/utils/import.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,3 @@ export function covInstruFunc(wasm: string) {
8787
};
8888
return { covInstrument };
8989
}
90-
91-
// list imports of a given wasm binary (buffer)
92-
// importList format should be as follows:
93-
// [
94-
// { module: 'env', name: 'memory', kind: 'memory' },
95-
// { module: 'env', name: 'myFunction', kind: 'function' },
96-
// ...
97-
// ]
98-
export async function parseWasmImports(binary: Buffer) {
99-
const mod = await WebAssembly.compile(binary);
100-
const importList = WebAssembly.Module.imports(mod);
101-
102-
return importList;
103-
}

src/utils/index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Imports as ASImports } from "@assemblyscript/loader";
2+
13
export function json2map<V>(json: Record<string, V>): Map<string, V> {
24
const res = new Map<string, V>();
35
for (const key in json) {
@@ -40,3 +42,37 @@ export function checkGenerics(functionName: string): string | undefined {
4042
}
4143
return;
4244
}
45+
46+
// list imports of a given wasm binary (buffer)
47+
// importList format should be as follows:
48+
// [
49+
// { module: 'env', name: 'memory', kind: 'memory' },
50+
// { module: 'env', name: 'myFunction', kind: 'function' },
51+
// ...
52+
// ]
53+
export async function parseWasmImports(binary: Buffer) {
54+
const mod = await WebAssembly.compile(binary);
55+
const importList = WebAssembly.Module.imports(mod);
56+
57+
return importList;
58+
}
59+
60+
export function supplyDefaultFunction(importList: WebAssembly.ModuleImportDescriptor[], importObject: ASImports) {
61+
for (const imp of importList) {
62+
if (imp.kind === "function") {
63+
const moduleName = imp.module;
64+
const funcName = imp.name;
65+
if (!importObject[moduleName]?.[funcName]) {
66+
if (!importObject[moduleName]) {
67+
importObject[moduleName] = {};
68+
}
69+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
70+
(importObject[moduleName] as any)[funcName] = (...args: any[]): any => {
71+
// notify that a default function has been called
72+
console.log(`Default stub called for ${moduleName}.${funcName}, args:`, args);
73+
return 0;
74+
};
75+
}
76+
}
77+
}
78+
}

tests/as/default.test.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

tests/ts/test/utils/index.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { jest } from "@jest/globals";
2+
import { Imports as ASImports } from "@assemblyscript/loader";
3+
4+
const mockWriteFile = jest.fn();
5+
jest.unstable_mockModule("node:fs", () => ({
6+
writeFileSync: mockWriteFile,
7+
}));
8+
9+
const { supplyDefaultFunction } = await import("../../../../src/utils/index.js");
10+
11+
describe("supplyDefaultFunction", () => {
12+
test("supplyTest", () => {
13+
const mockImportList: WebAssembly.ModuleImportDescriptor[] = [
14+
{ kind: "function", module: "myenv", name: "processEvent" },
15+
{ kind: "function", module: "externalMath", name: "add" },
16+
{ kind: "function", module: "system", name: "getStatus" },
17+
{ kind: "function", module: "logger", name: "logWarning" },
18+
{ kind: "function", module: "customOps", name: "combineValues" },
19+
];
20+
21+
const mockImportObject: ASImports = {
22+
myenv: {},
23+
externalMath: {},
24+
system: {},
25+
logger: {},
26+
customOps: {},
27+
};
28+
29+
supplyDefaultFunction(mockImportList, mockImportObject);
30+
31+
expect(typeof mockImportObject["myenv"]?.["processEvent"]).toBe("function");
32+
expect(typeof mockImportObject["system"]?.["getStatus"]).toBe("function");
33+
expect(typeof mockImportObject["logger"]?.["logWarning"]).toBe("function");
34+
expect(typeof mockImportObject["customOps"]?.["combineValues"]).toBe("function");
35+
});
36+
});

0 commit comments

Comments
 (0)