Bug 1759217 Comment 10 Edit History

Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.

Thanks Ryan. That's a huge help. I'm pretty sure I get most of this now, which is nothing short of a miracle :-). 
A few confirmations:
1. You say that a tag has identity. I did not understand that from the explainer!  What I think you are saying is that if I create a bunch of tags in WASM and javascript (say as below), even though these have the same data-types, they are different exceptions. Right? Presumably that difference is created by their index?
   ```
   (module
     (tag $mytag (param i32 f32) )  ;;0
     (tag (param i32 f32) )   ;;1
   )

     /// and in JS
    let tag2 = new WebAssembly.Tag(parameters: {['i32','f32']})
   ```
   - And presumably that means that you have to keep a handle on a Tag you create in javascript while you might potentially get an exception, because if you lose that particular tag2 it will NOT be the same as another tag you might create and you won't be able to inspect the exception?
2. My understanding (now) is that you only need the tag to throw an exception and to decode it. So if you create a tag in JS and then that JS is called from WASM and throws, you only need to also import the tag into the module IFF you want the module to be able to interpret the exception. You don't have to if you're happy for the exception to propagate up to JS to handle, or for it to be caught in a catch_all - right?
3. 
   >>> The attribute can currently just indicate "an exception thrown from javascript (or not)", but might indicate something else in future.

   >> I'm a bit confused by this because I would have thought the "where is this thrown from" would belong to the runtime exception not the tag.

   > What attribute is this? WA.Tags only have 'identity' and 'sequence of data types'.
  
   Note sure! I (think) I  was thinking of the explainer text: _Each tag has an attribute and a type. Currently, the attribute can only specify that the tag is for an exception. In the future, additional attribute values may be added when other kinds of tags are added to WebAssembly._" Sorry - let's ignore.

4. How do you run that `wasmTextToBinary` sample? Is there a shell or something where this is defined? I've been trying to run this in https://webassembly.github.io/wabt/demo/wat2wasm/  but that ignores the "enable exceptions" settings. I've had more luck in  [wabt tools](https://github.com/webassembly/wabt) directly but setting up a full test setup for this is a pain.

5. So I can see how this works for hand crafted code. What about if I want to port a C++ app, that happens to throw a particular exception if it (say) can't open a file. Say I decide to run that through emscripten or some other tool and get some WASM. Does the tool take care of creating all the exceptions and mimic the internal exception handling of the original application? Or is this something that is mostly going to be done "manually"?
Thanks Ryan. That's a huge help. I'm pretty sure I get most of this now, which is nothing short of a miracle :-). 
A few confirmations:
1. You say that a tag has identity. I did not understand that from the explainer!  What I think you are saying is that if I create a bunch of tags in WASM and javascript (say as below), even though these have the same data-types, they are different exceptions. Right? Presumably that difference is created by their index?
   ```
   (module
     (tag $mytag (param i32 f32) )  ;;0
     (tag (param i32 f32) )   ;;1
   )

     /// and in JS
    let tag2 = new WebAssembly.Tag(parameters: {['i32','f32']})
   ```
   - And presumably that means that you have to keep a handle on a Tag you create in javascript while you might potentially get an exception, because if you lose that particular tag2 it will NOT be the same as another tag you might create and you won't be able to inspect the exception?
2. My understanding (now) is that you only need the tag to throw an exception and to decode it. So if you create a tag in JS and then that JS is called from WASM and throws, you only need to also import the tag into the module IFF you want the module to be able to interpret the exception. You don't have to if you're happy for the exception to propagate up to JS to handle, or for it to be caught in a catch_all - right?
3. 
   >>> The attribute can currently just indicate "an exception thrown from javascript (or not)", but might indicate something else in future.

   >> I'm a bit confused by this because I would have thought the "where is this thrown from" would belong to the runtime exception not the tag.

   > What attribute is this? WA.Tags only have 'identity' and 'sequence of data types'.
  
   Note sure! I (think) I  was thinking of the explainer text: _Each tag has an attribute and a type. Currently, the attribute can only specify that the tag is for an exception. In the future, additional attribute values may be added when other kinds of tags are added to WebAssembly._" Sorry - let's ignore.

4. How do you run that `wasmTextToBinary` sample? Is there a shell or something where this is defined? I've been trying to run this in https://webassembly.github.io/wabt/demo/wat2wasm/  but that ignores the "enable exceptions" settings. I've had more luck in  [wabt tools](https://github.com/webassembly/wabt) directly but setting up a full test setup for this is a pain (not that big a pain, just have to round trip things more than I'd like).

5. So I can see how this works for hand crafted code. What about if I want to port a C++ app, that happens to throw a particular exception if it (say) can't open a file. Say I decide to run that through emscripten or some other tool and get some WASM. Does the tool take care of creating all the exceptions and mimic the internal exception handling of the original application? Or is this something that is mostly going to be done "manually"?
Thanks Ryan. That's a huge help. I'm pretty sure I get most of this now, which is nothing short of a miracle :-). 

I have done the JavaScript class stuff in https://github.com/mdn/content/pull/14925 - feel free to check it out. 
Still a bit rough around the edges.
After this I will provide additional context, which will be a lot of info from your post 2 above.

A few notes/questions
1. The fact that tags have identity makes a lot of things more clear. Thanks.
2. You only need the tag to throw an exception and to decode it. So if you create a tag in JS and then that JS is called from WASM and throws, you only need to also import the tag into the module IFF you want the module to be able to interpret the exception. You don't have to if you're happy for the exception to propagate up to JS to handle, or for it to be caught in a catch_all. That's what the examples do now in the linked docs anyway.
3. FYI only ...
   > What attribute is this? WA.Tags only have 'identity' and 'sequence of data types'.
  
   I am no longer sure. Ignore please.

4. How do you run that `wasmTextToBinary` sample? Is there a shell or something where this method is defined?
   I'm running wat2wsm locally because https://webassembly.github.io/wabt/demo/wat2wasm/is not working for exceptions, but that is a bit slow.

5. I can see how this works for me as a test code writer. What if I want to port a C++ app, that happens to throw a particular exception. Would it be that a tool author for C++ (say) would define some standard exceptions and map the standard libraries etc such that there would be a whole lot of exported "standard exceptions"? I guess I'm trying to understand if this is just a fundamental precondition that people writing code use, or is it something that will be an intermediate step for emscripten and others? Hope that makes sense.

Back to Bug 1759217 Comment 10