You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
[js-api] Should rethrow/br_on_exn trap on undefined? #109
We decided to make rethrow and br_on_exn trap on a null value in #90. Should we do the same when a given exnref is an undefined value thrown from JavaScript?
Oh shoot, apparently that's already not the case. From what I can tell of the JS API in #86, which lines up with the linked Chromium issue, if JavaScript throws null then the exnref caught by a surrounding try-catch in wasm will be ref.null.
This is not about null but about undefined I think? And #86 does not seem to say anything about neither null nor undefined case. (And #86 was written before we decided on #93, so it needs revision too, but it's not relevant with this issue)
I was trying to figure out what the interaction of wasm exception handling and JS throws was in more detail so that I could understand the problem more, and #86 was the most recent description I could find. In particular, it says that a JS value being thrown is converted to a wasm value via ToWebAssemblyValue. I then noticed that ToWebAssemblyValue converts a JS null to ref.null, hence the follow-up comment above. (It looks like #93 doesn't change that observation.)
So I don't know what to do about my first comment. It seems like a useful principle to always be able to assume you can rethrow an exception you caught. But I didn't realize at the time that the principle is incompatible with the existing design, so I'm at a loss for suggestions. My original thinking was that the semantics of rethrow and br_on_exn should be independent of what JS value was thrown, but it already depends on whether the JS value was null or not, so I'm not sure what general principle to apply. Sorry for my thinking out loud.
Hmm, I realized that the JS-interop strategy here has the ability to turn any wasm reference into an exnref. First, give the wasm reference to JS code so that it goes through ToJSValue, then throw the resulting reference in JS code. When this exception enters a wasm stack frame, it will go through ToWebAssemblyValue, which is nearly the inverse of ToJSValue. Then wasm code can catch that value as an exnref.
To make this concrete, the following module instantiated with the subsequent JS importObj uses this trick to turn a funcref into an exnref. It then uses a similar trick to turn that exnref back into a funcref so that it can be called. Thus the following is a very roundabout way to call $foo with the argument 5.
(module
(import "js" "throw_funcref" (func $throw_funcref (param funcref)))
(import "js" "exnref_to_funcref" (func $exnref_to_funcref (param exnref) (result funcref)))
(func $call_indirect_i32 (param funcref i32) ...) ;; uses a funcref table and call_indirect to call the given funcref with the given i32 as its argument
(func $foo (param i32) ...)
(func $main
(try
(call $throw_funcref (ref.func $foo))
catch
(call $exnref_to_funcref)
(i32.const 5)
(call $call_indirect_i32)
)
)
(start $main)
)
var importObj = {js: {
throw_funcref: (ref) => throw ref,
exnref_to_funcref: (ref) => return ref // intended to trap when ref is not funcref
}};
I'm guessing this behavior was not intended? In particular, it seems to imply that is_exnref would have to always return true if it were ever added.
Since catch is specified to catch any host exception, and JS can throw any reference, we have to assume that exnref could hold arbitrary host references.
What we need to clarify here is what Wasm can do with these references. I agree with Ross that Wasm should generally be able to rethrow them. If it's a wasm exception, then of course br_on_exn also needs to work. Apart from that, we should probably handle them as opaque references (meaning they don't trap if you br_on_exn or rethrow them).
null seems to be the only special case we need to think about, since this is a value that can be produced from Wasm.
I don't really see how this discussion relates to function references though. Ross' example assumes that funcref can be passed to the host (JS in this case), which means that ToJSValue provides a host reference to the respective funcref, and ToWebAssemblyValue converts back to funcref. This is independent of exception handling, and should be discussed in the reference types proposal.
The use of function references in my example is just to make things concrete (and its JS API is in the reference types proposal, which I don't think anyone wants to change). The point of my example is that, with the current JS API for exceptions, an exnref can be an arbitrary wasm reference, not just host reference.
The issue I'm pointing out, the issue raised with "undefined", and the existing issue with null are all related because they all have to do with how wasm/JS values are coerced as the cross the wasm/JS boundaries.
It occurs to me that when C++ programs throw an exception in wasm, they have to specify an event mark, e.g. __cpp_exception, and the exnref is the event mark paired with the payload. Similarly, the above issues would all be addressed by having JS exceptions be some (abstract, universal) event mark, e.g. __js_exception, paired with a (possibly null) JS-reference payload.
@backes According to ToWebAssemblyValue and ToJSValue in reference types JS API spec, when funcref goes from wasm to JS, it becomes an ExportedFunction, and when an ExportedFunction goes from JS to wasm, it becomes funcref.
Then when a JS function throws an ExportedFunction object (which might have been originally funcref and became ExportedFunction via ToJSValue), does it become funcref again? But it should be an exnref to be caught. Is that exnref that has funcref in it? Or is that exnref that has some JS object (ExportedFunction) in it?
I think some of the recent comments are based on domain errors. An exnref is not the value caught, it contains the value caught! Throwing null from JS still materialises as a non-null exnref when caught in Wasm. That contains some abstract host value, which happens to be the JS null value (though there is no way to observe that). That is unrelated to null exnrefs, which are merely uninitialised exnrefs.
Similarly if you throw an exported function.
ToJSValue and inverse do not even enter the picture.
Ah, so it sounds like you had in mind something in line with what I was suggesting with making throwing JS exceptions analogous to throwing C++ exceptions.
But I think it's oversimplifying to say ToJSValue and ToWebAssemblyValue do not enter the picture. One has to consider what should happen if a caught-in-wasm wasm-thrown exception exits to JS as an exnref or anyref and then is thrown by JS code, and similarly if the exception was JS-thrown. From the intuition you suggest, if JS code throws a value v, wasm catches it as an exnref, then the exnref gets passed back to JS and JS throws that value e, then e and v should be distinct values. In particular, e should be an exnref-wrapping of v. On the other hand, if wasm catches the JS-thrown value v and then rethrows it, and then JS code catches that as e, then e and v should be the same value. So this means ToJSValue (or the concept thereof) works differently on values exiting wasm through normal means versus values exiting by being thrown, since in the former case exnrefs are left untouched whereas in the latter case they are unwrapped. That's not necessarily a problem; I am just pointing out that wasm-to-JS and JS-to-wasm conversions are very much part of the picture.
What you're saying boils down to the question what ToJSValue does for exnref values passed back to JS normally. If that were to coerce exnref values to their contained value then that would be an explicit unwrapping that could not be reverted by ToWAValue. That is, passing the value back to JS would not give you an equivalent exnref. That's probably undesirable.
From the intuition you suggest, if JS code throws a value v, wasm catches it as an exnref, then the exnref gets passed back to JS and JS throws that value e, then e and v should be distinct values.
Fair point, but I think separable. We could still define special behaviour when catching an exnref thrown from JS (because JS itself cannot create such values). Whether that's necessary I'm not sure.
A better alternative might be to provide a special throw method in the JS API for throwing a proper Wasm exception.
In practice, I don't see much of a use case for passing exnrefs to JS, and I'm not sure it should be encouraged.
If that were to coerce exnref values to their contained value then that would be an explicit unwrapping that could not be reverted by ToWAValue.
Oh, to clarify. I meant to say that specifically JS-values-wrapped-as-exnrefs should be unwrapped when thrown from wasm to JS, not arbitrary exnrefs.
That is, passing the value back to JS would not give you an equivalent exnref.
Even give the above clarification, this point of yours suggests that exnref should not be equatable.
In practice, I don't see much of a use case for passing exnrefs to JS, and I'm not sure it should be encouraged.
Nonetheless, it is possible to do and so needs to be specified. And, being a web standard, we cannot really change this specification later on. This means we have to try to anticipate all of the ways it could be practical now and design for those patterns.
Thank you for the clarification. So let me check a few more points:
When exnref is rethrown to JS, it is unwrapped, but it is passed to JS by a function argument, it stays as is, right? So I guess we need ToJSValue for exnref I guess, because even if it is not a desired usage, it is possible. This will likely to be a new object in JS. Does it make sense?
What happens exnref that is not created by JS is rethrown to JS? Does it get unwrapped or stays that way?
I guess these are the points we should state in our JS API.
@aheejin, conceptually, I would say that an exnref is unwrapped at the moment it's rethrown in Wasm (which is why that instruction traps on null), regardless of where it's caught. If caught in Wasm, a new exnref is produced at the catch site. If caught in JS, you either get the JS value contained or some opaque object representing the Wasm exception.
We could define that throwing a Wasm exception on the JS side behaves like a rethrow in Wasm.
Yes, the JS API certainly needs to define something. It'll probably need to define a new form of Wasm exception exotic objects to represent Wasm exceptions on the JS side.
If we do the conversion regardless of the type (exnref vs externref (previously anyref)), this means that within wasm exnpackage(X) must compare equal to X, right?
No, ExceptionRef(exnpackage(X)) is in a 1-to-1 correlation with the exnpackage(X) object, not with X. When you pass the former to Wasm, you get the exnpackage, not X. Inside Wasm, you cannot ever observe X directly. If it is a Wasm exception, you can access its arguments using br_on_exn. If X is a JS value then you cannot observe it in Wasm. The only way to observe it is by rethrowing and catching it in JS.
unlike the special rule for ExceptionRef. That is, exnrefs are opaque in JS (as ExceptionRef objects), whereas RuntimeExceptions are opaque in Wasm (as externrefs).
What is the relationship between exnref and ExceptionRef? It sounds like exnref (wasm) and ExceptionRef (JS) are corresponding equals in each side, but what you said is exnpackage is the counterpart of ExceptionRef instead. Why?
Why is WACatch's return value is not exnref but exnpackage?
I'm not sure if terms are defined and people are using the same terms for same meaning at this point.
None of the explanation I gave was related to object identity. That’s another issue on top of the issues I discussed. Watching kids so can’t help more until later.
Hm, waiting for more explanation here then. I thought your main concerns are the invariants posted in #109 (comment):
Suppose $jsreturn and $jsthrow are imported JS functions that respectively just return or throw the passed-in reference. Then here are some things it seems like you'd expect:
$jsreturn always returns the (observably) same reference it was provided.
try {...} catch {call $jsthrow} (in wasm) is equivalent to try {...} catch {rethrow;} (in wasm).
A JS catch will catch the same (modulo identity?) value that was thrown by a JS throw if all intermediate wasm catches rethrow the exnref they caught.
I'm having a hard time making all these work out simultaneously though.
exnpackage (or "exception package") is a wasm value of type exnref. It's created by a wasm throw, in which case it holds wasm values and the corresponding exception tag (event). It's also created when catching a host exception. How exactly an exception package is implemented is not defined. In web engines, it could directly be backed by a WebAssembly.RuntimeException, but that's only one option to do it.
WebAssembly.RuntimeException is the JS type for caught wasm exceptions, potentially also used when passing exceptions to the host as parameters / return values.
WebAssembly.ExceptionReference is an alternative proposal for the JS type you get when passing wasm exceptions as parameter / return value. It would behave slightly different to WebAssembly.RuntimeException.
The latter two (if we choose to separate them) are only observed in JS. They don't have any meaning in core wasm.
Can you remind me why in these two cases, we need a new exnpackage
Because the Wasm semantics should avoid the hacky JS semantics of mutating an existing object, e.g., to attach a different stack trace when caught and rethrown. So each Wasm catch must observably produce a new, immutable exnref object.
exnpackage (or "exception package") is a wasm value of type exnref. It's created by a wasm throw, in which case it holds wasm values and the corresponding exception tag (event). It's also created when catching a host exception. How exactly an exception package is implemented is not defined.
I would rather say that an exnpackage is created each time an exception is caught in Wasm, regardless of what kind. Or, in the second variant, also when a Wasm exception is caught in JS.
I would rather say that an exnpackage is created each time an exception is caught in Wasm, regardless of what kind. Or, in the second variant, also when a Wasm exception is caught in JS.
Can you remind me why in these two cases, we need a new exnpackage
Because the Wasm semantics should avoid the hacky JS semantics of mutating an existing object, e.g., to attach a different stack trace when caught and rethrown. So each Wasm catch must observably produce a new, immutable exnref object.
Ok, understood. I guess I don't care too much about this. We are talking about the JS API here, so inheriting "hacky JS semantics" does not really worry me :)
EDIT: Just realize this is not true. This would be core wasm semantics. Still not fully convinced that this is the best semantics.
exnpackage (or "exception package") is a wasm value of type exnref. It's created by a wasm throw, in which case it holds wasm values and the corresponding exception tag (event). It's also created when catching a host exception. How exactly an exception package is implemented is not defined.
I would rather say that an exnpackage is created each time an exception is caught in Wasm, regardless of what kind. Or, in the second variant, also when a Wasm exception is caught in JS.
Agreed, if a wasm catch creates a new exnpackage anyway. Otherwise, I would have said that the exnpackage is created exactly once (on wasm throw), so we can more easily maintain identity. Instead of WA.RuntimeException(exn, wasmval*), we would then have WA.RuntimeException(exn, exnpackage(wasmval*)).
Still not fully convinced that this is the best semantics.
The JS hack is just that, a hack. There should not be any mutation behind your back. It particularly doesn't make sense as long as exnrefs aren't linear. For example, that creates pathological behaviour when you rethrow an exn twice, the second time after you have already caught the first:
try ... catch (e1) {
try rethrow e1 catch (e2) {
try rethrow e1 catch (e3) {
// what's the stack trace of e2 at this point?
}
}
}
And this would likely become way more problematic if we wanted to introduce resumption.
I'm not sure why we introduced the concept of exnpackage? @backes said exnpackage is a value and exnref is a type, but in JS side we don't have two of them separately. RuntimeException is a type but we also use it as a value too. exnpackage was introduced in the middle of this discussion thread and was not defined until just now.
Still not fully convinced that this is the best semantics.
The JS hack is just that, a hack. There should not be any mutation behind your back. It particularly doesn't make sense as long as exnrefs aren't linear. For example, that creates pathological behaviour when you rethrow an exn twice, the second time after you have already caught the first: [...]
Well, it's not completely "behind your back" since stack trace modification only happens on a (re)throw. And there is still quite some "magic" happening behind the scenes anyway.
I am not sure, but stack traces would be fully unspec'ed anyway, right? And there is no way to inspect them from wasm directly. So in the end, it is just a JS problem, and JS is already weird. Since it's totally up to the engine whether and how to provide stack traces, I don't see why we need to worry about this here.
Is it correct that with the modification proposed in #109 (comment) (i.e. preserving exnpackage identity across throw/catch/rethrow), we would fully implement @RossTate's wish list?
If so, it's a "we don't want to copy JS hacks (in our JS API)" vs "we want to preserve exnpackage identity" tradeoff. Both has value, but I would consider the latter more useful.
I am not sure, but stack traces would be fully unspec'ed anyway, right? And there is no way to inspect them from wasm directly. So in the end, it is just a JS problem
Well, for now, but I wouldn't assume that it will always stay that way. And more likely, stack traces may occur in some API. So I would be cautious to spec something for JS interop that isn't consistent with a well-behaved internal semantics.
@backes raised some important issues that have implications beyond the scope of exception handling. In particular, what to do about object identity, which is actually relevant to the following
If we do the conversion regardless of the type (exnref vs externref (previously anyref))
and to the question of whether exnref can be immutable or not. Sorry to be cryptic, but this conversation is already complicated enough, and trying to break down cross-cutting issues here is going to make that much much worse. I'll work on writing up the higher-level topic elsewhere, but in the meanwhile it might be best to pause this conversation for a bit.
Thanks for pausing this. It took me a while to figure out how to boil down the cross-cutting concerns to digestible discussion topics, but I believe the community's answers to WebAssembly/design#1351 and WebAssembly/design#1353 will help inform the design decisions to make here.
Activity
RossTate commentedon Apr 23, 2020
I think wasm programs should be able to expect that they can
rethrowanyexnrefthat was caught by atry-catch.RossTate commentedon Apr 23, 2020
Oh shoot, apparently that's already not the case. From what I can tell of the JS API in #86, which lines up with the linked Chromium issue, if JavaScript throws
nullthen theexnrefcaught by a surroundingtry-catchin wasm will beref.null.aheejin commentedon Apr 23, 2020
This is not about null but about undefined I think? And #86 does not seem to say anything about neither null nor undefined case. (And #86 was written before we decided on #93, so it needs revision too, but it's not relevant with this issue)
RossTate commentedon Apr 23, 2020
I was trying to figure out what the interaction of wasm exception handling and JS throws was in more detail so that I could understand the problem more, and #86 was the most recent description I could find. In particular, it says that a JS value being thrown is converted to a wasm value via ToWebAssemblyValue. I then noticed that ToWebAssemblyValue converts a JS null to
ref.null, hence the follow-up comment above. (It looks like #93 doesn't change that observation.)So I don't know what to do about my first comment. It seems like a useful principle to always be able to assume you can rethrow an exception you caught. But I didn't realize at the time that the principle is incompatible with the existing design, so I'm at a loss for suggestions. My original thinking was that the semantics of
rethrowandbr_on_exnshould be independent of what JS value was thrown, but it already depends on whether the JS value was null or not, so I'm not sure what general principle to apply. Sorry for my thinking out loud.RossTate commentedon Apr 24, 2020
Hmm, I realized that the JS-interop strategy here has the ability to turn any wasm reference into an
exnref. First, give the wasm reference to JS code so that it goes throughToJSValue, then throw the resulting reference in JS code. When this exception enters a wasm stack frame, it will go throughToWebAssemblyValue, which is nearly the inverse ofToJSValue. Then wasm code can catch that value as anexnref.To make this concrete, the following module instantiated with the subsequent JS
importObjuses this trick to turn afuncrefinto anexnref. It then uses a similar trick to turn thatexnrefback into afuncrefso that it can be called. Thus the following is a very roundabout way to call$foowith the argument5.I'm guessing this behavior was not intended? In particular, it seems to imply that
is_exnrefwould have to always return true if it were ever added.backes commentedon Apr 24, 2020
Since
catchis specified to catch any host exception, and JS can throw any reference, we have to assume thatexnrefcould hold arbitrary host references.What we need to clarify here is what Wasm can do with these references. I agree with Ross that Wasm should generally be able to rethrow them. If it's a wasm exception, then of course
br_on_exnalso needs to work. Apart from that, we should probably handle them as opaque references (meaning they don't trap if youbr_on_exnorrethrowthem).nullseems to be the only special case we need to think about, since this is a value that can be produced from Wasm.I don't really see how this discussion relates to function references though. Ross' example assumes that
funcrefcan be passed to the host (JS in this case), which means thatToJSValueprovides a host reference to the respectivefuncref, andToWebAssemblyValueconverts back tofuncref. This is independent of exception handling, and should be discussed in the reference types proposal.RossTate commentedon Apr 24, 2020
The use of function references in my example is just to make things concrete (and its JS API is in the reference types proposal, which I don't think anyone wants to change). The point of my example is that, with the current JS API for exceptions, an
exnrefcan be an arbitrary wasm reference, not just host reference.The issue I'm pointing out, the issue raised with "undefined", and the existing issue with null are all related because they all have to do with how wasm/JS values are coerced as the cross the wasm/JS boundaries.
RossTate commentedon Apr 24, 2020
It occurs to me that when C++ programs throw an exception in wasm, they have to specify an event mark, e.g.
__cpp_exception, and theexnrefis the event mark paired with the payload. Similarly, the above issues would all be addressed by having JS exceptions be some (abstract, universal) event mark, e.g.__js_exception, paired with a (possibly null) JS-reference payload.aheejin commentedon Apr 24, 2020
@backes According to ToWebAssemblyValue and ToJSValue in reference types JS API spec, when
funcrefgoes from wasm to JS, it becomes anExportedFunction, and when anExportedFunctiongoes from JS to wasm, it becomesfuncref.Then when a JS function throws an
ExportedFunctionobject (which might have been originallyfuncrefand becameExportedFunctionviaToJSValue), does it becomefuncrefagain? But it should be anexnrefto be caught. Is thatexnrefthat hasfuncrefin it? Or is thatexnrefthat has some JS object (ExportedFunction) in it?rossberg commentedon Apr 27, 2020
I think some of the recent comments are based on domain errors. An exnref is not the value caught, it contains the value caught! Throwing null from JS still materialises as a non-null exnref when caught in Wasm. That contains some abstract host value, which happens to be the JS null value (though there is no way to observe that). That is unrelated to null exnrefs, which are merely uninitialised exnrefs.
Similarly if you throw an exported function.
ToJSValue and inverse do not even enter the picture.
RossTate commentedon Apr 27, 2020
Ah, so it sounds like you had in mind something in line with what I was suggesting with making throwing JS exceptions analogous to throwing C++ exceptions.
But I think it's oversimplifying to say
ToJSValueandToWebAssemblyValuedo not enter the picture. One has to consider what should happen if a caught-in-wasm wasm-thrown exception exits to JS as anexnreforanyrefand then is thrown by JS code, and similarly if the exception was JS-thrown. From the intuition you suggest, if JS code throws a value v, wasm catches it as anexnref, then theexnrefgets passed back to JS and JS throws that value e, then e and v should be distinct values. In particular, e should be anexnref-wrapping of v. On the other hand, if wasm catches the JS-thrown value v and then rethrows it, and then JS code catches that as e, then e and v should be the same value. So this meansToJSValue(or the concept thereof) works differently on values exiting wasm through normal means versus values exiting by being thrown, since in the former caseexnrefs are left untouched whereas in the latter case they are unwrapped. That's not necessarily a problem; I am just pointing out that wasm-to-JS and JS-to-wasm conversions are very much part of the picture.rossberg commentedon Apr 27, 2020
What you're saying boils down to the question what ToJSValue does for exnref values passed back to JS normally. If that were to coerce exnref values to their contained value then that would be an explicit unwrapping that could not be reverted by ToWAValue. That is, passing the value back to JS would not give you an equivalent exnref. That's probably undesirable.
Fair point, but I think separable. We could still define special behaviour when catching an exnref thrown from JS (because JS itself cannot create such values). Whether that's necessary I'm not sure.
A better alternative might be to provide a special throw method in the JS API for throwing a proper Wasm exception.
In practice, I don't see much of a use case for passing exnrefs to JS, and I'm not sure it should be encouraged.
RossTate commentedon Apr 27, 2020
Oh, to clarify. I meant to say that specifically JS-values-wrapped-as-exnrefs should be unwrapped when thrown from wasm to JS, not arbitrary exnrefs.
Even give the above clarification, this point of yours suggests that
exnrefshould not be equatable.Nonetheless, it is possible to do and so needs to be specified. And, being a web standard, we cannot really change this specification later on. This means we have to try to anticipate all of the ways it could be practical now and design for those patterns.
aheejin commentedon Apr 28, 2020
Thank you for the clarification. So let me check a few more points:
exnrefis rethrown to JS, it is unwrapped, but it is passed to JS by a function argument, it stays as is, right? So I guess we needToJSValueforexnrefI guess, because even if it is not a desired usage, it is possible. This will likely to be a new object in JS. Does it make sense?exnrefthat is not created by JS is rethrown to JS? Does it get unwrapped or stays that way?I guess these are the points we should state in our JS API.
rossberg commentedon Apr 28, 2020
@aheejin, conceptually, I would say that an exnref is unwrapped at the moment it's rethrown in Wasm (which is why that instruction traps on null), regardless of where it's caught. If caught in Wasm, a new exnref is produced at the catch site. If caught in JS, you either get the JS value contained or some opaque object representing the Wasm exception.
We could define that throwing a Wasm exception on the JS side behaves like a rethrow in Wasm.
Yes, the JS API certainly needs to define something. It'll probably need to define a new form of Wasm exception exotic objects to represent Wasm exceptions on the JS side.
61 remaining items
aheejin commentedon Jun 8, 2020
@rossberg
I'm totally at loss at this point.
ExceptionRefhere? You said in [js-api] Should rethrow/br_on_exn trap on undefined? #109 (comment) thatexnrefis created by wrapping by wasmcatchand unwrapped by wasmrethrow. Then why would passingexnpackageto JS side createExceptionRef?exnpackage?exnrefandExceptionRef? It sounds likeexnref(wasm) andExceptionRef(JS) are corresponding equals in each side, but what you said isexnpackageis the counterpart ofExceptionRefinstead. Why?WACatch's return value is notexnrefbutexnpackage?I'm not sure if terms are defined and people are using the same terms for same meaning at this point.
backes commentedon Jun 8, 2020
Hm, waiting for more explanation here then. I thought your main concerns are the invariants posted in #109 (comment):
The latest proposal from @rossberg (#109 (comment)) with the modification proposed by me in #109 (comment) should satisfy all three points.
backes commentedon Jun 8, 2020
@aheejin
exnrefis a wasm type.exnpackage(or "exception package") is a wasm value of typeexnref. It's created by a wasmthrow, in which case it holds wasm values and the corresponding exception tag (event). It's also created when catching a host exception. How exactly an exception package is implemented is not defined. In web engines, it could directly be backed by aWebAssembly.RuntimeException, but that's only one option to do it.WebAssembly.RuntimeExceptionis the JS type for caught wasm exceptions, potentially also used when passing exceptions to the host as parameters / return values.WebAssembly.ExceptionReferenceis an alternative proposal for the JS type you get when passing wasm exceptions as parameter / return value. It would behave slightly different toWebAssembly.RuntimeException.rossberg commentedon Jun 8, 2020
@backes:
Because the Wasm semantics should avoid the hacky JS semantics of mutating an existing object, e.g., to attach a different stack trace when caught and rethrown. So each Wasm catch must observably produce a new, immutable exnref object.
I would rather say that an exnpackage is created each time an exception is caught in Wasm, regardless of what kind. Or, in the second variant, also when a Wasm exception is caught in JS.
aheejin commentedon Jun 8, 2020
@rossberg
Doesn't
catchcreateexnref?backes commentedon Jun 8, 2020
Ok, understood. I guess I don't care too much about this. We are talking about the JS API here, so inheriting "hacky JS semantics" does not really worry me :)
EDIT: Just realize this is not true. This would be core wasm semantics. Still not fully convinced that this is the best semantics.
Agreed, if a wasm catch creates a new exnpackage anyway. Otherwise, I would have said that the exnpackage is created exactly once (on wasm throw), so we can more easily maintain identity. Instead of
WA.RuntimeException(exn, wasmval*), we would then haveWA.RuntimeException(exn, exnpackage(wasmval*)).rossberg commentedon Jun 8, 2020
@aheejin:
Yes, it creates an exnpackage value, which are the values of type exnref.
@backes:
The JS hack is just that, a hack. There should not be any mutation behind your back. It particularly doesn't make sense as long as exnrefs aren't linear. For example, that creates pathological behaviour when you rethrow an exn twice, the second time after you have already caught the first:
And this would likely become way more problematic if we wanted to introduce resumption.
aheejin commentedon Jun 8, 2020
I'm not sure why we introduced the concept of
exnpackage? @backes saidexnpackageis a value andexnrefis a type, but in JS side we don't have two of them separately.RuntimeExceptionis a type but we also use it as a value too.exnpackagewas introduced in the middle of this discussion thread and was not defined until just now.backes commentedon Jun 8, 2020
Well, it's not completely "behind your back" since stack trace modification only happens on a (re)throw. And there is still quite some "magic" happening behind the scenes anyway.
I am not sure, but stack traces would be fully unspec'ed anyway, right? And there is no way to inspect them from wasm directly. So in the end, it is just a JS problem, and JS is already weird. Since it's totally up to the engine whether and how to provide stack traces, I don't see why we need to worry about this here.
Is it correct that with the modification proposed in #109 (comment) (i.e. preserving
exnpackageidentity acrossthrow/catch/rethrow), we would fully implement @RossTate's wish list?If so, it's a "we don't want to copy JS hacks (in our JS API)" vs "we want to preserve
exnpackageidentity" tradeoff. Both has value, but I would consider the latter more useful.rossberg commentedon Jun 8, 2020
@backes:
Well, for now, but I wouldn't assume that it will always stay that way. And more likely, stack traces may occur in some API. So I would be cautious to spec something for JS interop that isn't consistent with a well-behaved internal semantics.
RossTate commentedon Jun 8, 2020
@backes raised some important issues that have implications beyond the scope of exception handling. In particular, what to do about object identity, which is actually relevant to the following
and to the question of whether
exnrefcan be immutable or not. Sorry to be cryptic, but this conversation is already complicated enough, and trying to break down cross-cutting issues here is going to make that much much worse. I'll work on writing up the higher-level topic elsewhere, but in the meanwhile it might be best to pause this conversation for a bit.RossTate commentedon Jun 15, 2020
Thanks for pausing this. It took me a while to figure out how to boil down the cross-cutting concerns to digestible discussion topics, but I believe the community's answers to WebAssembly/design#1351 and WebAssembly/design#1353 will help inform the design decisions to make here.
aheejin commentedon Oct 12, 2020
Closing this, as we decided to remove
exnrefin Sep 15 CG meeting.Merge pull request WebAssembly#109 from turbolent/patch-1