diff --git a/src/lib/libexceptions.js b/src/lib/libexceptions.js index 1799f96a9ec9c..c3a05a763e819 100644 --- a/src/lib/libexceptions.js +++ b/src/lib/libexceptions.js @@ -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); @@ -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. diff --git a/test/codesize/test_codesize_cxx_except.json b/test/codesize/test_codesize_cxx_except.json index f5beaf9c15fb0..65d5840622ee1 100644 --- a/test/codesize/test_codesize_cxx_except.json +++ b/test/codesize/test_codesize_cxx_except.json @@ -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", diff --git a/test/codesize/test_codesize_cxx_mangle.json b/test/codesize/test_codesize_cxx_mangle.json index 626d3a0d494a4..fa71038445551 100644 --- a/test/codesize/test_codesize_cxx_mangle.json +++ b/test/codesize/test_codesize_cxx_mangle.json @@ -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",