Details of the JS API for exception handling #150
Description
Now that the semantics for the current iteration of the proposal are fairly solid and two browser implementations are underway, I was wondering if this is a good time to figure out the details for the JS API for exceptions.
@Ms2ger has written up draft JS API spec text which provides a good baseline to expand on. This draft was made during the previous iteration of the semantics, so it needs an update to remove exnref
. But it already describes WebAssembly.Exception
and WebAssembly.RuntimeException
that are discussed below and additional API features can be incorporated into it.
Here are some of the aspects that the final JS API could/should likely provide, along with some design questions on how these aspects should work:
- Provide an interface for exported exception types such as
WebAssembly.Exception
.- Previous discussions have generally assumed this should exist, and be called
WA.Exception
. - Should it support the type reflection API proposal? i.e., a
type()
method on exceptions. - Should it be possible to construct these in JS (given a function type or just a list of parameters from the type reflection API), to create a unique exception tag on the JS side? (which a Wasm module could import)
- Previous discussions have generally assumed this should exist, and be called
- Provide an interface for Wasm exceptions caught in JS such as
WebAssembly.RuntimeException
.- A previous discussion reached a consensus to call this
WebAssembly.RuntimeException
and to keep it separate fromWebAssembly.RuntimeError
. - Should it allow access to exception values? (e.g.,
values()
orgetArg()
method, usingToJSValue
to coerce Wasm values to JS as usual) - If values are accessible, it should require a capability (i.e., the exception type export) to access. See this comment for why.
- As discussed in the link in the previous bullet, should it have a method to let you check the tag against an exception type?
- Should JS be allowed to construct these? (e.g., given an exception type, and values that would be coerced via
ToWasmValue
)
- A previous discussion reached a consensus to call this
In terms of features, IMO it would make sense to allow accessing the values (requiring the exception type for access) and to allow constructing exception objects. Not adding this functionality won't actually prevent JS hosts from doing these operations (both limitations can be hacked around by constructing a new Wasm module) but adding it will make it more convenient.
There was some past discussion here and here on how/whether to allow constructing exception objects in JS, given the values to put in the exception. Ms2ger's draft assumes that the WA.RuntimeException
constructor would also take a WA.Exception
argument as well. IMO this would make sense to keep in the API in order to extract the tag out of the exception type. Not having this argument would make the constructor pretty useless, as without a tag such exceptions could only be caught by catch_all
.
Are there any other aspects of the JS API that should also be discussed?