Skip to content

Commit 9749435

Browse files
WIP: Add support for logging errors
1 parent 41b01fb commit 9749435

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/services/VirtualFsService.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { WasmFs } from '@wasmer/wasmfs';
2+
3+
const CONSOLE_FDS = [0, 1, 2];
4+
5+
class VirtualFs {
6+
private logs?: string = undefined;
7+
8+
constructor(private wasmFs: WasmFs) {}
9+
10+
getFs() {
11+
const fs = this.wasmFs.fs;
12+
13+
const originalWriteSync = fs.writeSync;
14+
fs.writeSync = (...args: any[]) => {
15+
const fd = args[0];
16+
17+
if (CONSOLE_FDS.includes(fd)) {
18+
if (this.logs) {
19+
this.logs += new TextDecoder().decode(args[1]);
20+
} else {
21+
this.logs = new TextDecoder().decode(args[1]);
22+
}
23+
}
24+
25+
// @ts-ignore
26+
return originalWriteSync(...args);
27+
}
28+
29+
return fs;
30+
}
31+
32+
getLogOutput(): string[] {
33+
return this.logs?.split('\n') ?? [];
34+
}
35+
}
36+
37+
export default VirtualFs;

src/services/VirtualMachineService.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { alea } from 'seedrandom';
55
import { VmContext } from "../models/Context";
66
import { ExecuteResult } from '../models/ExecuteResult';
77
import { generateFixedBuffer } from './CryptoService';
8+
import VirtualFs from './VirtualFsService';
89
import WasmImports from './WasmImports';
910

1011
const metering = require('wasm-metering');
@@ -19,6 +20,7 @@ export function prepareWasmBinary(binary: Uint8Array): Uint8Array {
1920

2021
export async function executeWasm(context: VmContext): Promise<ExecuteResult> {
2122
const wasmFs = new WasmFs();
23+
const virtualFs = new VirtualFs(wasmFs);
2224

2325
try {
2426
const random = alea(context.randomSeed);
@@ -27,7 +29,7 @@ export async function executeWasm(context: VmContext): Promise<ExecuteResult> {
2729
env: context.env,
2830
bindings: {
2931
...WASIBindings,
30-
fs: wasmFs.fs,
32+
fs: virtualFs.getFs(),
3133
hrtime: () => BigInt(context.timestamp),
3234
randomFillSync: (buffer: any, offset, size) => {
3335
const randomBuffer = generateFixedBuffer(random.int32().toString(), size ?? buffer.length - offset);
@@ -46,25 +48,15 @@ export async function executeWasm(context: VmContext): Promise<ExecuteResult> {
4648
wasmImports.setMemory(instance.exports.memory as WebAssembly.Memory);
4749
wasi.start(instance);
4850

49-
const logs = [];
50-
const stdout = await wasmFs.getStdOut() as string;
51-
52-
if (stdout) {
53-
logs.push(...stdout.split("\n"));
54-
}
51+
const logs = virtualFs.getLogOutput();
5552

5653
return {
5754
code: 0,
5855
logs,
5956
gasUsed: context.gasUsed.toString(),
6057
}
6158
} catch(error: any) {
62-
const stdout = await wasmFs.getStdOut();
63-
const logs = [];
64-
65-
if (stdout) {
66-
logs.push(stdout);
67-
}
59+
const logs = virtualFs.getLogOutput();
6860

6961
logs.push(error?.message);
7062

0 commit comments

Comments
 (0)