Skip to content

Supply default function #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
/tests/ts/fixture
/third_party
/tests/cpp/lit/**/*.json
/tests/as/default.test.ts
/.cache
/.github/CODEOWNERS
21 changes: 20 additions & 1 deletion src/core/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { instantiate, Imports as ASImports } from "@assemblyscript/loader";
import { AssertResult } from "../assertResult.js";
import { Imports, ImportsArgument } from "../index.js";
import { IAssertResult, InstrumentResult } from "../interface.js";
import { mockInstruFunc, covInstruFunc } from "../utils/import.js";
import { mockInstruFunc, covInstruFunc, parseWasmImports } from "../utils/import.js";
const readFile = promises.readFile;

function nodeExecutor(wasms: string[], outFolder: string, imports: Imports) {
Expand All @@ -30,6 +30,25 @@ function nodeExecutor(wasms: string[], outFolder: string, imports: Imports) {
...userDefinedImportsObject,
} as ASImports;
const binary = await readFile(wasm);
const importList = await parseWasmImports(binary);
// supplying default function here, so no more need to define all of them in as-test.js
for (const imp of importList) {
if (imp.kind === "function") {
const moduleName = imp.module;
const funcName = imp.name;
if (!importObject[moduleName]?.[funcName]) {
if (!importObject[moduleName]) {
importObject[moduleName] = {};
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
(importObject[moduleName] as any)[funcName] = (...args: any[]): any => {
// notify that a default function has been called
console.log(`Default stub called for ${moduleName}.${funcName}, args:`, args);
return 0;
};
}
}
}
const ins = await instantiate(binary, importObject);
importsArg.module = ins.module;
importsArg.instance = ins.instance;
Expand Down
14 changes: 14 additions & 0 deletions src/utils/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,17 @@ export function covInstruFunc(wasm: string) {
};
return { covInstrument };
}

// list imports of a given wasm binary (buffer)
// importList format should be as follows:
// [
// { module: 'env', name: 'memory', kind: 'memory' },
// { module: 'env', name: 'myFunction', kind: 'function' },
// ...
// ]
export async function parseWasmImports(binary: Buffer) {
const mod = await WebAssembly.compile(binary);
const importList = WebAssembly.Module.imports(mod);

return importList;
}
47 changes: 47 additions & 0 deletions tests/as/default.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, test, expect, endTest } from "../../assembly";

// Default Stub Injection Tests
// Making sure that external functions can supplied with an empty default function so that users will be free of supplying their own

@external("myenv", "processEvent")
declare function processEvent(eventId: i32): void;

@external("externalMath", "add")
declare function add(a: i32, b: i32): i32;

@external("system", "getStatus")
declare function getStatus(): i32;

@external("logger", "logWarning")
declare function logWarning(messageId: i32): i32;

@external("customOps", "combineValues")
declare function combineValues(a: i32, b: f32, c: i64): i32;

describe("Default Stub Injection Tests", () => {
test("Test processEvent (i32) -> void", () => {
processEvent(101);
});

test("Test add (i32, i32) -> i32", () => {
const result = add(10, 5);
expect(result).equal(0);
});

test("Test getStatus () -> i32", () => {
const result = getStatus();
expect(result).equal(0);
});

test("Test logWarning (i32) -> i32", () => {
const result = logWarning(12345);
expect(result).equal(0);
});

test("Test combineValues (i32, f32, i64) -> i32", () => {
const result = combineValues(1, 2.5, 1000);
expect(result).equal(0);
});
});

endTest();