Skip to content

Commit ef01122

Browse files
authored
Remove dep between __cxa_rethrow_primary_exception and __cxa_rethrow (NFC) (#26518)
Currently `__cxa_rethrow_primary_exception` calls `__cxa_rethrow`, and `__cxa_rethrow` is unnecessarily complicated to split logic when it is called from `__cxa_rethrow_primary_exception` and when it is not. This just decouples the two and duplicates the necessary logic in `__cxa_rethrow_primary_exception`. This makes `__cxa_rethrow` simple and easy to follow.
1 parent 5967d73 commit ef01122

3 files changed

Lines changed: 29 additions & 21 deletions

File tree

src/lib/libexceptions.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,18 @@ var LibraryExceptions = {
125125
#endif
126126
],
127127
__cxa_rethrow: () => {
128-
var info = exceptionCaught.pop();
129-
if (!info) {
128+
if (!exceptionCaught.length) {
130129
abort('no exception to throw');
131130
}
131+
var info = exceptionCaught.at(-1);
132132
var ptr = info.excPtr;
133-
if (!info.get_rethrown()) {
134-
// Only pop if the corresponding push was through rethrow_primary_exception
135-
exceptionCaught.push(info);
136-
info.set_rethrown(true);
137-
info.set_caught(false);
138-
uncaughtExceptionCount++;
139-
}
133+
info.set_rethrown(true);
134+
info.set_caught(false);
135+
uncaughtExceptionCount++;
140136
#if !DISABLE_EXCEPTION_CATCHING
141137
___cxa_increment_exception_refcount(ptr);
142138
#if EXCEPTION_DEBUG
143-
dbg('__cxa_rethrow, popped ' +
139+
dbg('__cxa_rethrow: ' +
144140
[ptrToString(ptr), exceptionLast, 'stack', exceptionCaught]);
145141
#endif
146142
exceptionLast = new CppException(ptr);
@@ -216,13 +212,25 @@ var LibraryExceptions = {
216212
return info.get_type();
217213
},
218214

219-
__cxa_rethrow_primary_exception__deps: ['$ExceptionInfo', '$exceptionCaught', '__cxa_rethrow'],
215+
__cxa_rethrow_primary_exception__deps: ['$ExceptionInfo', '$uncaughtExceptionCount',
216+
#if !DISABLE_EXCEPTION_CATCHING
217+
'$exceptionLast',
218+
'__cxa_increment_exception_refcount',
219+
#endif
220+
],
220221
__cxa_rethrow_primary_exception: (ptr) => {
221222
if (!ptr) return;
223+
#if EXCEPTION_DEBUG
224+
dbg('__cxa_rethrow_primary_exception: ' + ptrToString(ptr));
225+
#endif
222226
var info = new ExceptionInfo(ptr);
223-
exceptionCaught.push(info);
224227
info.set_rethrown(true);
225-
___cxa_rethrow();
228+
info.set_caught(false);
229+
#if !DISABLE_EXCEPTION_CATCHING
230+
___cxa_increment_exception_refcount(ptr);
231+
exceptionLast = new CppException(ptr);
232+
#endif
233+
{{{ makeThrow('exceptionLast') }}}
226234
},
227235

228236
// Finds a suitable catch clause for when an exception is thrown.

test/codesize/test_codesize_cxx_except.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 23195,
3-
"a.out.js.gz": 8968,
2+
"a.out.js": 23173,
3+
"a.out.js.gz": 8962,
44
"a.out.nodebug.wasm": 172516,
55
"a.out.nodebug.wasm.gz": 57438,
6-
"total": 195711,
7-
"total_gz": 66406,
6+
"total": 195689,
7+
"total_gz": 66400,
88
"sent": [
99
"__cxa_begin_catch",
1010
"__cxa_end_catch",

test/codesize/test_codesize_cxx_mangle.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 23245,
3-
"a.out.js.gz": 8990,
2+
"a.out.js": 23223,
3+
"a.out.js.gz": 8984,
44
"a.out.nodebug.wasm": 238957,
55
"a.out.nodebug.wasm.gz": 79847,
6-
"total": 262202,
7-
"total_gz": 88837,
6+
"total": 262180,
7+
"total_gz": 88831,
88
"sent": [
99
"__cxa_begin_catch",
1010
"__cxa_end_catch",

0 commit comments

Comments
 (0)