Skip to content
34 changes: 21 additions & 13 deletions src/lib/libexceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,18 @@ var LibraryExceptions = {
#endif
],
__cxa_rethrow: () => {
var info = exceptionCaught.pop();
if (!info) {
if (!exceptionCaught.length) {
abort('no exception to throw');
}
var info = exceptionCaught.at(-1);
var ptr = info.excPtr;
if (!info.get_rethrown()) {
// Only pop if the corresponding push was through rethrow_primary_exception
exceptionCaught.push(info);
info.set_rethrown(true);
info.set_caught(false);
uncaughtExceptionCount++;
}
info.set_rethrown(true);
info.set_caught(false);
uncaughtExceptionCount++;
#if !DISABLE_EXCEPTION_CATCHING
___cxa_increment_exception_refcount(ptr);
#if EXCEPTION_DEBUG
dbg('__cxa_rethrow, popped ' +
dbg('__cxa_rethrow: ' +
[ptrToString(ptr), exceptionLast, 'stack', exceptionCaught]);
#endif
exceptionLast = new CppException(ptr);
Expand Down Expand Up @@ -216,13 +212,25 @@ var LibraryExceptions = {
return info.get_type();
},

__cxa_rethrow_primary_exception__deps: ['$ExceptionInfo', '$exceptionCaught', '__cxa_rethrow'],
__cxa_rethrow_primary_exception__deps: ['$ExceptionInfo', '$uncaughtExceptionCount',
#if !DISABLE_EXCEPTION_CATCHING
'$exceptionLast',
'__cxa_increment_exception_refcount',
#endif
],
__cxa_rethrow_primary_exception: (ptr) => {
if (!ptr) return;
#if EXCEPTION_DEBUG
dbg('__cxa_rethrow_primary_exception: ' + ptrToString(ptr));
#endif
var info = new ExceptionInfo(ptr);
exceptionCaught.push(info);
info.set_rethrown(true);
___cxa_rethrow();
info.set_caught(false);
#if !DISABLE_EXCEPTION_CATCHING
___cxa_increment_exception_refcount(ptr);
exceptionLast = new CppException(ptr);
#endif
{{{ makeThrow('exceptionLast') }}}
},

// Finds a suitable catch clause for when an exception is thrown.
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_except.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 23195,
"a.out.js.gz": 8968,
"a.out.js": 23173,
"a.out.js.gz": 8962,
"a.out.nodebug.wasm": 172516,
"a.out.nodebug.wasm.gz": 57438,
"total": 195711,
"total_gz": 66406,
"total": 195689,
"total_gz": 66400,
"sent": [
"__cxa_begin_catch",
"__cxa_end_catch",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_mangle.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 23245,
"a.out.js.gz": 8990,
"a.out.js": 23223,
"a.out.js.gz": 8984,
"a.out.nodebug.wasm": 238957,
"a.out.nodebug.wasm.gz": 79847,
"total": 262202,
"total_gz": 88837,
"total": 262180,
"total_gz": 88831,
"sent": [
"__cxa_begin_catch",
"__cxa_end_catch",
Expand Down
Loading