aboutsummaryrefslogtreecommitdiff
path: root/ui/src/engine/wasm_bridge.ts
diff options
context:
space:
mode:
Diffstat (limited to 'ui/src/engine/wasm_bridge.ts')
-rw-r--r--ui/src/engine/wasm_bridge.ts60
1 files changed, 26 insertions, 34 deletions
diff --git a/ui/src/engine/wasm_bridge.ts b/ui/src/engine/wasm_bridge.ts
index fe896b169..97a36927c 100644
--- a/ui/src/engine/wasm_bridge.ts
+++ b/ui/src/engine/wasm_bridge.ts
@@ -23,6 +23,10 @@ import * as init_trace_processor from '../gen/trace_processor';
// HEAPU8[reqBufferAddr, +REQ_BUFFER_SIZE].
const REQ_BUF_SIZE = 32 * 1024 * 1024;
+function writeToUIConsole(line: string) {
+ console.log(line);
+}
+
export interface WasmBridgeRequest {
id: number;
methodName: string;
@@ -31,6 +35,7 @@ export interface WasmBridgeRequest {
export interface WasmBridgeResponse {
id: number;
+ aborted: boolean; // If true the WASM module crashed.
data: Uint8Array;
}
@@ -42,7 +47,6 @@ export class WasmBridge {
private currentRequestResult: WasmBridgeResponse|null;
private connection: init_trace_processor.Module;
private reqBufferAddr = 0;
- private lastStderr: string[] = [];
constructor(init: init_trace_processor.InitWasm) {
this.aborted = false;
@@ -51,12 +55,13 @@ export class WasmBridge {
const deferredRuntimeInitialized = defer<void>();
this.connection = init({
locateFile: (s: string) => s,
- print: (line: string) => console.log(line),
- printErr: (line: string) => this.appendAndLogErr(line),
+ print: writeToUIConsole,
+ printErr: writeToUIConsole,
onRuntimeInitialized: () => deferredRuntimeInitialized.resolve(),
+ onAbort: () => this.aborted = true,
});
this.whenInitialized = deferredRuntimeInitialized.then(() => {
- const fn = this.connection.addFunction(this.onReply.bind(this), 'vii');
+ const fn = this.connection.addFunction(this.onReply.bind(this), 'iii');
this.reqBufferAddr = this.connection.ccall(
'Initialize',
/*return=*/ 'number',
@@ -67,31 +72,26 @@ export class WasmBridge {
callWasm(req: WasmBridgeRequest): WasmBridgeResponse {
if (this.aborted) {
- throw new Error('Wasm module crashed');
+ return {
+ id: req.id,
+ aborted: true,
+ data: new Uint8Array(),
+ };
}
assertTrue(req.data.length <= REQ_BUF_SIZE);
const endAddr = this.reqBufferAddr + req.data.length;
this.connection.HEAPU8.subarray(this.reqBufferAddr, endAddr).set(req.data);
- try {
- this.connection.ccall(
- req.methodName, // C method name.
- 'void', // Return type.
- ['number'], // Arg types.
- [req.data.length] // Args.
- );
- const result = assertExists(this.currentRequestResult);
- this.currentRequestResult = null;
- result.id = req.id;
- return result;
- } catch (err) {
- this.aborted = true;
- let abortReason = `${err}`;
- if (err instanceof Error) {
- abortReason = `${err.name}: ${err.message}\n${err.stack}`;
- }
- abortReason += '\n\nstderr: \n' + this.lastStderr.join('\n');
- throw new Error(abortReason);
- }
+ this.connection.ccall(
+ req.methodName, // C method name.
+ 'void', // Return type.
+ ['number'], // Arg types.
+ [req.data.length] // Args.
+ );
+
+ const result = assertExists(this.currentRequestResult);
+ this.currentRequestResult = null;
+ result.id = req.id;
+ return result;
}
// This is invoked from ccall in the same call stack as callWasm.
@@ -99,16 +99,8 @@ export class WasmBridge {
const data = this.connection.HEAPU8.slice(heapPtr, heapPtr + size);
this.currentRequestResult = {
id: 0, // Will be set by callWasm()'s epilogue.
+ aborted: false,
data,
};
}
-
- private appendAndLogErr(line: string) {
- console.warn(line);
- // Keep the last N lines in the |lastStderr| buffer.
- this.lastStderr.push(line);
- if (this.lastStderr.length > 512) {
- this.lastStderr.shift();
- }
- }
}