Skip to content

Commit

Permalink
add tests and support for wasm stack frames
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed May 1, 2022
1 parent 727cbe6 commit da3e63f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,12 @@ function wrapCallSite(frame, state) {
// from getScriptNameOrSourceURL() instead
var source = frame.getFileName() || frame.getScriptNameOrSourceURL();
if (source) {
// v8 does not expose its internal isWasm, etc methods, so we do this instead.
if(source.startsWith('wasm://')) {
state.curPosition = null;
return frame;
}

var line = frame.getLineNumber();
var column = frame.getColumnNumber() - 1;

Expand Down
12 changes: 12 additions & 0 deletions test-fixtures/wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
To test support for WASM stack traces, we have a tiny WASM module that we call
into.

It imports a JS function and exports a WASM function
that, when called, will call the JS function.

When we call the wasm function, it calls back into JS. We can throw an error
and know that one of the stack frames will be wasm.

The module is described in both text and binary formats. Compilation from text
to binary format was done using an online tool. I didn't bother to set up a
build script, opting instead ot store the binary in version control.
14 changes: 14 additions & 0 deletions test-fixtures/wasm/wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require('fs');
const path = require('path');

exports.call_js_function = async function(fn) {
const mod = await WebAssembly.instantiate(
fs.readFileSync(path.resolve(__dirname, 'wasm.wasm')),
{
jsapi: {
fn
}
}
);
mod.instance.exports.call_js_function();
}
Binary file added test-fixtures/wasm/wasm.wasm
Binary file not shown.
6 changes: 6 additions & 0 deletions test-fixtures/wasm/wasm.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(module
(import "jsapi" "fn" (func $jsapi_fn))
(func (export "call_js_function")
call $jspapi_fn
)
)
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,17 @@ it('async stack frames: Promise.any', async function() {
]);
});

it('wasm stack frames', async function() {
await compareStackTrace(createMultiLineSourceMap(), [
'return require("./test-fixtures/wasm/wasm.js").call_js_function(() => { throw new Error("test"); });'
], [
'Error: test',
re`^ at ${stackFramePathStartsWith()}(?:.*[/\\])?line1.js:1001:101$`,
re`^ at wasm:\/\/wasm\/c2de0ab2:wasm-function\[1\]:0x3b$`,
re`^ at Object\.exports\.call_js_function \(.*[/\\]wasm\.js:13:24\)$`,
]);
});

it('throw with empty source map', async function() {
await compareStackTrace(createEmptySourceMap(), [
'throw new Error("test");'
Expand Down

0 comments on commit da3e63f

Please sign in to comment.