Skip to content

Commit 1f6b136

Browse files
authored
[EH] Remove 'terminating' from exception message (#17058)
#17003 added "terminating" to exception messages, but some people might want to use the message in non-terminating cases. This changes `emscripten_format_exception` to `__get_exception_message`, and separately adds `__get_terminating_exception_message`. Adds two underscores not to clash with the global namespace. `__get_exception_message` prints messages like ``` exception of type char exception of type std::runtime_error: abc exception of type myexception: My exception happened ``` `__get_terminating_exception_message` prints messages like ``` terminating with uncaught exception of type char terminating with uncaught exception of type std::runtime_error: abc terminating with uncaught exception of type myexception: My exception happened ```
1 parent 1bb6a3a commit 1f6b136

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

emcc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2138,7 +2138,7 @@ def phase_linker_setup(options, state, newargs, user_settings):
21382138
# TODO: Move this into the library JS file once it becomes possible.
21392139
# See https://github.com/emscripten-core/emscripten/pull/15982
21402140
if settings.INCLUDE_FULL_LIBRARY and not settings.DISABLE_EXCEPTION_CATCHING:
2141-
settings.EXPORTED_FUNCTIONS += ['_emscripten_format_exception', '_free']
2141+
settings.EXPORTED_FUNCTIONS += ['___get_exception_message', '_free']
21422142

21432143
if settings.WASM_WORKERS:
21442144
# TODO: After #15982 is resolved, these dependencies can be declared in library_wasm_worker.js

src/library_exceptions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,9 @@ var LibraryExceptions = {
392392
},
393393

394394
#if !DISABLE_EXCEPTION_CATCHING
395-
$formatException__deps: ['emscripten_format_exception', 'free'],
396-
$formatException: function(excPtr) {
397-
var utf8_addr = _emscripten_format_exception(excPtr);
395+
$getExceptionMessage__deps: ['__get_exception_message', 'free'],
396+
$getExceptionMessage: function(excPtr) {
397+
var utf8_addr = ___get_exception_message(excPtr);
398398
var result = UTF8ToString(utf8_addr);
399399
_free(utf8_addr);
400400
return result;

system/lib/libcxxabi/src/cxa_exception_emscripten.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ cxa_exception_from_thrown_object(void* thrown_object) {
2424

2525
extern "C" {
2626

27-
char* emscripten_format_exception(void* thrown_object) {
27+
char* __get_exception_message(void* thrown_object, bool terminate=false) {
2828
__cxa_exception* exception_header =
2929
cxa_exception_from_thrown_object(thrown_object);
3030
const __shim_type_info* thrown_type =
@@ -45,19 +45,27 @@ char* emscripten_format_exception(void* thrown_object) {
4545
const char* what =
4646
static_cast<const std::exception*>(thrown_object)->what();
4747
asprintf(&result,
48-
"terminating with uncaught exception of type %s: %s",
48+
(terminate ? "terminating with uncaught exception of type %s: %s"
49+
: "exception of type %s: %s"),
4950
type_name,
5051
what);
5152
} else {
52-
asprintf(
53-
&result, "terminating with uncaught exception of type %s", type_name);
53+
asprintf(&result,
54+
(terminate ? "terminating with uncaught exception of type %s"
55+
: "exception of type %s"),
56+
type_name);
5457
}
5558

5659
if (demangled_buf) {
5760
free(demangled_buf);
5861
}
5962
return result;
6063
}
64+
65+
char* __get_exception_terminate_message(void *thrown_object) {
66+
return __get_exception_message(thrown_object, true);
67+
}
68+
6169
}
6270

6371
#endif // __USING_EMSCRIPTEN_EXCEPTIONS__

tests/test_core.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,10 +1608,10 @@ def test_exceptions_rethrow_missing(self):
16081608
self.do_runf('main.cpp', None, assert_returncode=NON_ZERO)
16091609

16101610
@no_wasm64('MEMORY64 does not yet support exceptions')
1611-
def test_format_exception(self):
1611+
def test_exception_message(self):
16121612
self.set_setting('DISABLE_EXCEPTION_CATCHING', 0)
1613-
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', ['$formatException', '__cxa_decrement_exception_refcount', '__cxa_increment_exception_refcount'])
1614-
self.set_setting('EXPORTED_FUNCTIONS', ['_main', 'formatException', '_emscripten_format_exception', '_free'])
1613+
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', ['$getExceptionMessage', '__cxa_decrement_exception_refcount', '__cxa_increment_exception_refcount'])
1614+
self.set_setting('EXPORTED_FUNCTIONS', ['_main', 'getExceptionMessage', '___get_exception_message', '_free'])
16151615
self.maybe_closure()
16161616
src = '''
16171617
#include <emscripten.h>
@@ -1645,26 +1645,26 @@ class myexception : public exception {
16451645
EM_ASM({
16461646
for (let i = 1; i < 6; i++){
16471647
try {
1648-
Module["_throw_exc"](i);
1648+
_throw_exc(i);
16491649
} catch(p) {
16501650
// Because we are catching and handling the exception in JS, the normal
16511651
// exception catching C++ code doesn't kick in, so we need to make sure we free
16521652
// the exception, if necessary. By incrementing and decrementing the refcount
16531653
// we trigger the free'ing of the exception if its refcount was zero.
16541654
___cxa_increment_exception_refcount(p);
1655-
console.log(Module["formatException"](p).replace(/0x[0-9a-f]*/, "xxx"));
1655+
console.log(getExceptionMessage(p));
16561656
___cxa_decrement_exception_refcount(p);
16571657
}
16581658
}
16591659
});
16601660
}
16611661
'''
16621662
expected = '''\
1663-
terminating with uncaught exception of type int
1664-
terminating with uncaught exception of type char
1665-
terminating with uncaught exception of type std::runtime_error: abc
1666-
terminating with uncaught exception of type myexception: My exception happened
1667-
terminating with uncaught exception of type char const*
1663+
exception of type int
1664+
exception of type char
1665+
exception of type std::runtime_error: abc
1666+
exception of type myexception: My exception happened
1667+
exception of type char const*
16681668
'''
16691669

16701670
self.do_run(src, expected)

0 commit comments

Comments
 (0)