--
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-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/3E0EEA6F-D458-4771-8443-FD4E242808D4%40googlemail.com.
Can you share the link flags you are are using for your project? We know this works in at least some cases because we have a test for it:See:and:I imagine there is some issue with the settings you are using.Do you get any kind of backtrace when you see the `Aborted(native code called abort())` message? Normally I would expect to see a backtrace? Does happens with you build with `-O0` vs `-O3`? Any more information?
--
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-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/E09B6FEF-33E9-4E0F-95C1-F2A73CAD1EA3%40googlemail.com.
Taking a quick look at rethrow_exception, it looks like it calls `terminate` if the `ptr` is NULL:Can you trace the value of the `std::exception_ptr` you are trying to rethrow to confirm?
Regarding handling C++ exceptions in JS, I think you will always need to do things like getExceptionMessage to unpack information if all you have is a thrown object from C++.
Can you explain what you mean by "if I throw a C++ exception in its JS incarnation".. what is a JS incarnation?
When you talk of ` $$` and `class handle` and you talking about embind/emval objects?
Taking a quick look at rethrow_exception, it looks like it calls `terminate` if the `ptr` is NULL:Can you trace the value of the `std::exception_ptr` you are trying to rethrow to confirm?That was spot on Sam! The error is thrown in JS, so calling `std::current_exception()` in C++ returns a null ptr. What I do now is to forward the thrown exception from JS (which is one of the C++ exceptions) and call std::make_exception_ptr to convert it to an exception pointer (to create a nested exception).Regarding handling C++ exceptions in JS, I think you will always need to do things like getExceptionMessage to unpack information if all you have is a thrown object from C++.Well, the thrown error has its name set to the C++ exception, so what I do now is:private isCancellationException(e: Error): boolean {return e.name === "std::__nested<antlr4::ParseCancellationException>";}Not really elegant, nor type safe, but it works.Can you explain what you mean by "if I throw a C++ exception in its JS incarnation".. what is a JS incarnation?Sure. There are 4 ways you can throw an exception/error with wasm:- throw JSError in JS
- throw C++ exception in C++
- throw C++ exception in JS
- throw a JS error in C++ (I believe, never tried that)
To throw a C++ exception in JS that exception must be bound as usual with embind (and that thrown JS error is the JS incarnation of the C++ exception).
--
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-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/FC886282-090A-4B9B-9EDD-9D09D82BF6E3%40googlemail.com.
On Tue, Aug 22, 2023 at 1:56 AM 'Mike Lischke' via emscripten-discuss <emscripte...@googlegroups.com> wrote:Taking a quick look at rethrow_exception, it looks like it calls `terminate` if the `ptr` is NULL:Can you trace the value of the `std::exception_ptr` you are trying to rethrow to confirm?That was spot on Sam! The error is thrown in JS, so calling `std::current_exception()` in C++ returns a null ptr. What I do now is to forward the thrown exception from JS (which is one of the C++ exceptions) and call std::make_exception_ptr to convert it to an exception pointer (to create a nested exception).Regarding handling C++ exceptions in JS, I think you will always need to do things like getExceptionMessage to unpack information if all you have is a thrown object from C++.Well, the thrown error has its name set to the C++ exception, so what I do now is:private isCancellationException(e: Error): boolean {return e.name === "std::__nested<antlr4::ParseCancellationException>";}Not really elegant, nor type safe, but it works.Can you explain what you mean by "if I throw a C++ exception in its JS incarnation".. what is a JS incarnation?Sure. There are 4 ways you can throw an exception/error with wasm:- throw JSError in JSMy JSError do you mean something that is the result of calling `new Error` in JS? I don't think that can or should interact at all with the C++ exception handling mechanism. I see them as two orthogonal systems and trying to mix them or conflate them is likely to lead to trouble.- throw C++ exception in C++This should clearly work.- throw C++ exception in JSI 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 can probably say more on this.
- throw a JS error in C++ (I believe, never tried that)IIRC would just be throwing C++ object that happened to be a val that pointed to JS object.. which I think should work fine.To throw a C++ exception in JS that exception must be bound as usual with embind (and that thrown JS error is the JS incarnation of the C++ exception).I don't see why embind would need to be involved necessarily. Not all programs use embind, and its possible to refer to C++ object via simple pointers in JS. If you have a simple pointer to C++ object I don't think there is anything stopping you from just doing `throw ptr` in JS. However, as I say above that is not that same as throwing in C++.. and what you would really want to do is callback into native code. e.g. `_throw_in_cpp(ptr)`.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAL_va28LoU_7vRRJBNRPUyFxsCSC0X20ZDBfo2c4LfLpOgZ0qg%40mail.gmail.com.
- throw JSError in JSMy JSError do you mean something that is the result of calling `new Error` in JS? I don't think that can or should interact at all with the C++ exception handling mechanism. I see them as two orthogonal systems and trying to mix them or conflate them is likely to lead to trouble.
- throw C++ exception in JSI 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 can probably say more on this.
I don't see why embind would need to be involved necessarily. Not all programs use embind, and its possible to refer to C++ object via simple pointers in JS.
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?
Also I don't understand what you mean by "JS incarnation of C++ exceptions" either...
- throw C++ exception in JSI 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 can probably say more on this.You can do that for Wasm exceptions by using the JS API.An example of such code is in our JS library: https://github.com/emscripten-core/emscripten/blob/9bb02c0182bcfc32450ea15d5be69eab494042df/src/library_exceptions.js#L328-L344This 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.
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.Exceptionobjects.