Skip to content

Commit d361125

Browse files
add tracing feature
1 parent 44063d2 commit d361125

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

examples/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { WASI, useAll } from "uwasi";
1+
import { WASI, useAll, useTrace } from "uwasi";
22
import fs from "node:fs/promises";
33

44
async function main() {
55
const wasi = new WASI({
66
args: process.argv.slice(2),
7-
features: [useAll()],
7+
features: [useTrace([useAll()])],
88
});
99
const bytes = await fs.readFile(process.argv[2]);
1010
const { instance } = await WebAssembly.instantiate(bytes, {

src/features/tracing.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { WASIFeatureProvider } from "../options";
2+
3+
export function useTrace(features: WASIFeatureProvider[]): WASIFeatureProvider {
4+
return (options, abi, memoryView) => {
5+
let wasiImport: WebAssembly.ModuleImports = {};
6+
for (const useFeature of features) {
7+
const imports = useFeature(options, abi, memoryView);
8+
wasiImport = { ...wasiImport, ...imports };
9+
}
10+
for (const key in wasiImport) {
11+
const original = wasiImport[key];
12+
if (typeof original !== 'function') {
13+
continue;
14+
}
15+
wasiImport[key] = (...args: any[]) => {
16+
const result = original(...args);
17+
console.log(`[uwasi-tracing] ${key}(${args.map(a => JSON.stringify(a)).join(', ')}) => ${result}`);
18+
return result;
19+
}
20+
}
21+
return wasiImport;
22+
}
23+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export * from "./features/environ";
88
export * from "./features/fd";
99
export * from "./features/proc";
1010
export * from "./features/random";
11+
export * from "./features/tracing";
1112

1213
export class WASI {
1314
/**

0 commit comments

Comments
 (0)