Skip to content

Commit a9d3c4d

Browse files
committed
supply default function
1 parent 85fa158 commit a9d3c4d

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

assembly/default.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function emptyFunction(): void {
2+
// this is an empty function used to make sure default.test.ts will get run
3+
}

src/core/execute.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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 } from "../utils/import.js";
9+
import { mockInstruFunc, covInstruFunc, parseWasmImports } from "../utils/import.js";
1010
const readFile = promises.readFile;
1111

1212
function nodeExecutor(wasms: string[], outFolder: string, imports: Imports) {
@@ -30,6 +30,24 @@ function nodeExecutor(wasms: string[], outFolder: string, imports: Imports) {
3030
...userDefinedImportsObject,
3131
} as ASImports;
3232
const binary = await readFile(wasm);
33+
// getting a list of wasm import functions
34+
const importList = await parseWasmImports(binary);
35+
// supplying default function here, so no more need to define all of them in as-test.js
36+
for (const imp of importList) {
37+
if (imp.kind === "function") {
38+
const moduleName = imp.module;
39+
const funcName = imp.name;
40+
if (!importObject[moduleName]) {
41+
importObject[moduleName] = {};
42+
}
43+
if (!importObject[moduleName][funcName]) {
44+
importObject[moduleName][funcName] = (...args: any[]): any => {
45+
console.log(`Default stub called for ${moduleName}.${funcName}, args:`, args);
46+
return 0;
47+
};
48+
}
49+
}
50+
}
3351
const ins = await instantiate(binary, importObject);
3452
importsArg.module = ins.module;
3553
importsArg.instance = ins.instance;

src/utils/import.ts

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

tests/as/default.test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, test, expect, endTest } from "../../assembly";
2+
import { emptyFunction } from "../../assembly/default";
3+
4+
// Default Stub Injection Tests
5+
// Making sure that external functions can supplied with an empty default function so that users will be free of supplying their own
6+
7+
@external("myenv", "processEvent")
8+
declare function processEvent(eventId: i32): void;
9+
10+
@external("externalMath", "add")
11+
declare function add(a: i32, b: i32): i32;
12+
13+
@external("system", "getStatus")
14+
declare function getStatus(): i32;
15+
16+
@external("logger", "logWarning")
17+
declare function logWarning(messageId: i32): i32;
18+
19+
@external("customOps", "combineValues")
20+
declare function combineValues(a: i32, b: f32, c: i64): i32;
21+
22+
describe("Default Stub Injection Tests", () => {
23+
test("Test processEvent (i32) -> void", () => {
24+
processEvent(101);
25+
});
26+
27+
test("Test add (i32, i32) -> i32", () => {
28+
const result = add(10, 5);
29+
expect(result).equal(0);
30+
});
31+
32+
test("Test getStatus () -> i32", () => {
33+
const result = getStatus();
34+
expect(result).equal(0);
35+
});
36+
37+
test("Test logWarning (i32) -> i32", () => {
38+
const result = logWarning(12345);
39+
expect(result).equal(0);
40+
});
41+
42+
test("Test combineValues (i32, f32, i64) -> i32", () => {
43+
const result = combineValues(1, 2.5, 1000);
44+
expect(result).equal(0);
45+
});
46+
});
47+
48+
endTest();

0 commit comments

Comments
 (0)