Skip to content

Commit c2d4d02

Browse files
committed
support executeTestFunction
1 parent e70b6a1 commit c2d4d02

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.vscode
2+
.cache
23

34
/node_modules
45
/dist

instrumentation/MockInstrumentationWalker.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "MockInstrumentationWalker.hpp"
2+
#include <binaryen-c.h>
23
#include <string_view>
34
#include <support/index.h>
5+
#include <wasm-type.h>
46
#include <wasm.h>
57
// mock test will be tested with wasm-testing-framework project, escape this class
68
// LCOV_EXCL_START
@@ -86,13 +88,28 @@ bool MockInstrumentationWalker::mockFunctionDuplicateImportedCheck() const noexc
8688
return checkRepeat;
8789
}
8890

91+
void MockInstrumentationWalker::addExecuteTestFunction() noexcept {
92+
std::vector<BinaryenExpressionRef> operands{};
93+
BinaryenExpressionRef body = moduleBuilder.makeCallIndirect(
94+
module->tables[0]->name,
95+
BinaryenLocalGet(module, 0, wasm::Type::i32),
96+
operands,
97+
wasm::Signature(wasm::Type::none, wasm::Type::none)
98+
);
99+
100+
body->finalize();
101+
BinaryenAddFunction(module, "executeTestFunction", BinaryenTypeInt32(), BinaryenTypeNone(), {}, 0, body);
102+
BinaryenAddFunctionExport(module, "executeTestFunction", "executeTestFunction");
103+
}
104+
89105
uint32_t MockInstrumentationWalker::mockWalk() noexcept {
90106
if (mockFunctionDuplicateImportedCheck()) {
91107
return 1U; // failed
92108
} else {
93109
wasm::ModuleUtils::iterDefinedFunctions(*module, [this](wasm::Function *const func) noexcept {
94110
walkFunctionInModule(func, this->module);
95111
});
112+
addExecuteTestFunction();
96113
return 0U;
97114
}
98115
}

instrumentation/MockInstrumentationWalker.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ class MockInstrumentationWalker final : public wasm::PostWalker<MockInstrumentat
130130
/// @brief bool, true when the mock function is duplicated imported
131131
bool mockFunctionDuplicateImportedCheck() const noexcept;
132132

133+
///
134+
/// @brief Add export function executeTestFunction()
135+
///
136+
void addExecuteTestFunction() noexcept;
137+
133138
///
134139
/// @brief Main API for mock instrumentation
135140
///

src/core/execute.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ async function nodeExecutor(wasm: string, outFolder: string, imports?: Imports):
4545
importsArg.exports = ins.exports;
4646
try {
4747
wasi.start(ins);
48+
const execTestFunction = ins.exports["executeTestFunction"];
49+
if (typeof execTestFunction === "function") {
50+
for (const fncs of executionRecorder.registerFunctions) {
51+
const functions = fncs[1];
52+
(execTestFunction as (a: number) => void)(functions);
53+
mockInstrumentFunc["mockFunctionStatus.clear"]();
54+
}
55+
}
4856
} catch (error) {
4957
if (error instanceof Error) {
5058
console.error(error.stack);

0 commit comments

Comments
 (0)