Skip to content

Commit c233703

Browse files
authored
Remove a static allocation in C++ exceptions support code in JS (#12039)
This also fixes a potential issue with thread-safety (as all threads used to use that same location, previously, and now each thread gets its own allocation, kind of like TLS).
1 parent 0284a11 commit c233703

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/library_exceptions.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
var LibraryExceptions = {
88
$exceptionLast: '0',
99
$exceptionCaught: ' []',
10+
$exceptionThrowBuf: '0',
1011

1112
// Static fields for ExceptionInfo class.
1213
$ExceptionInfoAttrs: {
@@ -395,7 +396,7 @@ var LibraryExceptions = {
395396
// unwinding using 'if' blocks around each function, so the remaining
396397
// functionality boils down to picking a suitable 'catch' block.
397398
// We'll do that here, instead, to keep things simpler.
398-
__cxa_find_matching_catch__deps: ['$exceptionLast', '$ExceptionInfo', '$CatchInfo', '__resumeException'],
399+
__cxa_find_matching_catch__deps: ['$exceptionLast', '$ExceptionInfo', '$CatchInfo', '__resumeException', '$exceptionThrowBuf'],
399400
__cxa_find_matching_catch: function() {
400401
var thrown = exceptionLast;
401402
if (!thrown) {
@@ -416,12 +417,10 @@ var LibraryExceptions = {
416417
#if EXCEPTION_DEBUG
417418
out("can_catch on " + [thrown]);
418419
#endif
419-
#if DISABLE_EXCEPTION_CATCHING == 1
420-
var thrownBuf = 0;
421-
#else
422-
var thrownBuf = {{{ makeStaticAlloc(4) }}};
423-
#endif
424-
{{{ makeSetValue('thrownBuf', '0', 'thrown', '*') }}};
420+
if (!exceptionThrowBuf) {
421+
exceptionThrowBuf = _malloc(4);
422+
}
423+
{{{ makeSetValue('exceptionThrowBuf', '0', 'thrown', '*') }}};
425424
// The different catch blocks are denoted by different types.
426425
// Due to inheritance, those types may not precisely match the
427426
// type of the thrown object. Find one which matches, and
@@ -432,8 +431,8 @@ var LibraryExceptions = {
432431
// Catch all clause matched or exactly the same type is caught
433432
break;
434433
}
435-
if ({{{ exportedAsmFunc('___cxa_can_catch') }}}(caughtType, thrownType, thrownBuf)) {
436-
var adjusted = {{{ makeGetValue('thrownBuf', '0', '*') }}};
434+
if ({{{ exportedAsmFunc('___cxa_can_catch') }}}(caughtType, thrownType, exceptionThrowBuf)) {
435+
var adjusted = {{{ makeGetValue('exceptionThrowBuf', '0', '*') }}};
437436
if (thrown !== adjusted) {
438437
catchInfo.set_adjusted_ptr(adjusted);
439438
}

0 commit comments

Comments
 (0)