Skip to content

Commit 4ea7592

Browse files
authored
[Emscripten EH] Fix EXCEPTION_STACK_TRACES for resumeException case (#19099)
1 parent 2282236 commit 4ea7592

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

src/library_exceptions.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ var LibraryExceptions = {
188188
var info = new ExceptionInfo(ptr);
189189
// Initialize ExceptionInfo content after it was allocated in __cxa_allocate_exception.
190190
info.init(type, destructor);
191-
exceptionLast = ptr;
191+
{{{ storeException('exceptionLast', 'ptr') }}}
192192
uncaughtExceptionCount++;
193-
{{{ makeThrow('ptr') }}}
193+
{{{ makeThrow('exceptionLast') }}}
194194
},
195195

196196
// This exception will be caught twice, but while begin_catch runs twice,
@@ -215,8 +215,8 @@ var LibraryExceptions = {
215215
dbg('__cxa_rethrow, popped ' +
216216
[ptrToString(ptr), exceptionLast, 'stack', exceptionCaught]);
217217
#endif
218-
exceptionLast = ptr;
219-
{{{ makeThrow('ptr') }}}
218+
{{{ storeException('exceptionLast', 'ptr') }}}
219+
{{{ makeThrow('exceptionLast') }}}
220220
},
221221

222222
llvm_eh_typeid_for__sig: 'ip',
@@ -316,7 +316,12 @@ var LibraryExceptions = {
316316
__cxa_find_matching_catch__deps: ['$exceptionLast', '$ExceptionInfo', '__resumeException', '__cxa_can_catch', 'setTempRet0'],
317317
//__cxa_find_matching_catch__sig: 'p',
318318
__cxa_find_matching_catch: function() {
319-
var thrown = exceptionLast;
319+
var thrown =
320+
#if EXCEPTION_STACK_TRACES
321+
exceptionLast && exceptionLast.excPtr;
322+
#else
323+
exceptionLast;
324+
#endif
320325
if (!thrown) {
321326
// just pass through the null ptr
322327
setTempRet0(0);
@@ -364,8 +369,10 @@ var LibraryExceptions = {
364369
#if EXCEPTION_DEBUG
365370
dbg("__resumeException " + [ptrToString(ptr), exceptionLast]);
366371
#endif
367-
if (!exceptionLast) { exceptionLast = ptr; }
368-
{{{ makeThrow('ptr') }}}
372+
if (!exceptionLast) {
373+
{{{ storeException('exceptionLast', 'ptr') }}}
374+
}
375+
{{{ makeThrow('exceptionLast') }}}
369376
},
370377

371378
#endif

src/parseTools.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,14 @@ function makeThrow(excPtr) {
495495
}
496496
return `assert(false, '${assertInfo}');`;
497497
}
498-
if (EXCEPTION_STACK_TRACES) {
499-
return `throw new CppException(${excPtr});`;
500-
}
501498
return `throw ${excPtr};`;
502499
}
503500

501+
function storeException(varName, excPtr) {
502+
var exceptionToStore = EXCEPTION_STACK_TRACES ? `new CppException(${excPtr})` : `${excPtr}`;
503+
return `${varName} = ${exceptionToStore};`;
504+
}
505+
504506
function charCode(char) {
505507
return char.charCodeAt(0);
506508
}

src/runtime_exceptions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class EmscriptenSjLj extends EmscriptenEH {}
1515
class CppException extends EmscriptenEH {
1616
constructor(excPtr) {
1717
super(excPtr);
18+
this.excPtr = excPtr;
1819
#if !DISABLE_EXCEPTION_CATCHING
1920
const excInfo = getExceptionMessage(excPtr);
2021
this.name = excInfo[0];

test/test_other.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8296,7 +8296,9 @@ def test_exceptions_stack_trace_and_message(self, wasm_eh):
82968296
throw std::runtime_error("my message");
82978297
}
82988298
void foo() {
8299-
bar();
8299+
try {
8300+
bar();
8301+
} catch (const std::invalid_argument &err) {}
83008302
}
83018303
int main() {
83028304
foo();

0 commit comments

Comments
 (0)