Hey Heejin,

> 
> I'm not sure if I understand the whole thread. For starters, 
> BailErrorStrategy::recover in the first post takes two arguments and it looks 
> you only provided one, making the second argument null automatically?

During the time this e-mail thread ran I tried a number of changes, to find a 
solution. The original code tried to get the exception ptr from C++ in JS and 
forwarded that to the recover() method. That didn't work, so I changed the code 
and passed no exception in, while on C++ side `std::current_exception()` was 
used to get this exception pointer. Also that failed, but a hint from Sam gave 
me the right idea (there can't be a current exception in C++, if no C++ 
exception was thrown **in C++**). That's why I ended up with passing the C++ 
exception that was thrown in JS to C++, where I could then do all the 
processing successfully.

> Also I don't understand what you mean by "JS incarnation of C++ exceptions" 
> either... 

In one of my mails I explained what I meant. It's about the JS object for a C++ 
exception class (or any C++ class, for that matter).

>> 
>>  
>>> - throw C++ exception in JS
>> 
>> I don't think it possible (or should be possible) to use a JS `throw` 
>> statement to throw C++ exception.. C++ exceptions must be thrown by the 
>> native `__cxa_throw` mechanism.  In order words even if you have a handle to 
>> C++ object you want to throw, I believe the only way to throw is to call 
>> back into native code to perform a C++ throw.
>> 
>> I could be wrong about this though.. @Heejin Ahn <mailto:ahee...@google.com> 
>> can probably say more on this.
> 
> You can do that for Wasm exceptions by using the JS API 
> <https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Exception>.
> 
> An example of such code is in our JS library: 
> https://github.com/emscripten-core/emscripten/blob/9bb02c0182bcfc32450ea15d5be69eab494042df/src/library_exceptions.js#L328-L344
> This code does some string manipulation to make the stack trace tidy, but 
> what you need for creating an exception and throwing is just these lines:
> ```
> var e = new WebAssembly.Exception(getCppExceptionTag(), [ex], {traceStack: 
> true});
> throw e;
> ```
> I'm not sure you can do that for the old way of Emscripten exceptions. 

Well, I guess we have to make sure we talk about the same use cases. In my case 
I throw these exceptions in JS only to interrupt JS code, not C++ code. 
However, I have to know which exception was thrown from JS code in C++, as 
further handling depends on that (hence that call to `recover()` with the 
catched exception in JS). To recap, this is how it looks (and works) now:

        catch (re) {
            if (re instanceof RecognitionException) {
                this.getErrorHandler().reportError(this, re);
                this.getErrorHandler().recover(this, re);
            } else {
                throw re;
            }
        }

`re` is a sub class of the wrapped C++ `RecognitionException` and recover() 
will throw a nested exception in C++ as the final action, which is catched 
somewhere else.

>> 
> I'm also not sure why embind should be involved. By the way, Wasm exceptions 
> will not be represented as simple integer pointers in the JS world. They are 
> WebAssembly.Exception 
> <https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Exception>objects.

Yes, I experimented with both variants (-fexceptions and -fwasm-exceptions), 
but I have no clear picture which one is better. However, the WebAssembly 
exceptions seem to require more work (getting information and manually delete 
them) and the documentation is pretty thin for either way.

Anyway, the discussion here is rather about how to make the `instanceof` 
operator work on a catched exception that was thrown in C++.

Thanks for all the insight!

Mike
-- 
www.soft-gems.net

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to emscripten-discuss+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/emscripten-discuss/2582B3B4-4B82-4FFA-81DC-321016F74A4B%40googlemail.com.

Reply via email to