Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit d6f5074

Browse files
authored
Update explainer on stack trace support in JS API (#197)
This updates the explainer on - We add stack trace option to `Exception` class in the JS API - `Exception` can contain an optional `externref` to carry a stack trace - `Exception` is not a subclass of JS `Error` The WebIDL syntax is suggested by @eqrion in #183 (comment) and #183 (comment). Addresses #183 and #189.
1 parent f767b4e commit d6f5074

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

proposals/exception-handling/Exceptions.md

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,6 @@ within `()` after `delegate`s are the label operands in immediate values.
394394

395395
### JS API
396396

397-
#### Stack traces
398-
399-
When an exception is thrown, the runtime will pop the stack across function
400-
calls until a corresponding, enclosing try block is found. Some runtimes,
401-
especially web VMs may also associate a stack trace that can be used to report
402-
uncaught exceptions. However, the details of this are left to the embedder.
403-
404397
#### Traps
405398

406399
The `catch`/`catch_all` instruction catches exceptions generated by the `throw`
@@ -455,27 +448,51 @@ access to the data fields of a `Exception` if a matching tag is given. This last
455448
check ensures that without access to a WebAssembly module's exported exception
456449
tag, the associated data fields cannot be read.
457450

451+
The `Exception` constructor can take an optional `ExceptionOptions` argument,
452+
which can optionally contain `traceStack` entry. When `traceStack` is `true`,
453+
web VMs can attach a stack trace string to `Exception.stack` field, as in
454+
JavaScript's `Error` class. While `Exception` is not a subclass of JavaScript's
455+
`Error` and it can be used to represent normal control flow constructs,
456+
`traceStack` field can be set when we use it to represent errors. The format of
457+
stack trace strings conform to the [WebAssembly stack trace
458+
conventions](https://webassembly.github.io/spec/web-api/index.html#conventions).
459+
When `ExceptionOption` is not provided or it does not contain `traceStack`
460+
entry, `traceStack` is considered `false` by default.
461+
462+
To preserve stack trace info when crossing the JS to Wasm boundary, `Exception`
463+
can internally contain an optional `externref` value containing a stack trace
464+
string, which is propagated when caught by `catch` and rethrown by `rethrow`.
465+
458466
More formally, the added interfaces look like the following:
459467

460468
```WebIDL
469+
dictionary TagType {
470+
required sequence<ValueType> parameters;
471+
};
472+
461473
[LegacyNamespace=WebAssembly, Exposed=(Window,Worker,Worklet)]
462474
interface Tag {
463475
constructor(TagType type);
464476
TagType type();
465477
};
466478
479+
dictionary ExceptionOptions {
480+
boolean traceStack = false;
481+
};
482+
467483
[LegacyNamespace=WebAssembly, Exposed=(Window,Worker,Worklet)]
468484
interface Exception {
469-
constructor(Tag tag, sequence<any> payload);
485+
constructor(Tag tag, sequence<any> payload, optional ExceptionOptions options);
470486
any getArg(Tag tag, unsigned long index);
471487
boolean is(Tag tag);
488+
readonly attribute (DOMString or undefined) stack;
472489
};
473490
```
474491

475-
Where `type TagType = {parameters: ValueType[]}`, following the format of the
476-
type reflection proposal (`TagType` corresponds to a `FunctionType` without a
477-
`results` property). `TagType` could be extended in the future for other
478-
proposals that require a richer type specification.
492+
`TagType` corresponds to a `FunctionType` in [the type reflection
493+
proposal](https://github.com/WebAssembly/js-types/blob/main/proposals/js-types/Overview.md),
494+
without a `results` property). `TagType` could be extended in the future for
495+
other proposals that require a richer type specification.
479496

480497
## Changes to the text format
481498

0 commit comments

Comments
 (0)