Skip to content

Commit c3a1875

Browse files
authored
Move __cxa_get_exception_ptr to native code. NFC (#22421)
The removes the need to for the `__cxa_is_pointer_type` export which is now just part of `__cxa_get_exception_ptr`.
1 parent 5991425 commit c3a1875

27 files changed

+53
-66
lines changed

src/library_exceptions.js

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ var LibraryExceptions = {
2020
// reference counter) is not protected from that. Also protection is not enough, separate state
2121
// should be allocated. libcxxabi has concept of dependent exception which is used for that
2222
// purpose, it references the primary exception.
23-
$ExceptionInfo__deps: [
24-
'__cxa_is_pointer_type',
2523
#if EXCEPTION_DEBUG
26-
'$ptrToString'
24+
$ExceptionInfo__deps: ['$ptrToString'],
2725
#endif
28-
],
2926
$ExceptionInfo: class {
3027
// excPtr - Thrown object pointer to wrap. Metadata pointer is calculated from it.
3128
constructor(excPtr) {
@@ -84,22 +81,6 @@ var LibraryExceptions = {
8481
get_adjusted_ptr() {
8582
return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.adjustedPtr, '*') }}};
8683
}
87-
88-
// Get pointer which is expected to be received by catch clause in C++ code. It may be adjusted
89-
// when the pointer is casted to some of the exception object base classes (e.g. when virtual
90-
// inheritance is used). When a pointer is thrown this method should return the thrown pointer
91-
// itself.
92-
get_exception_ptr() {
93-
// Work around a fastcomp bug, this code is still included for some reason in a build without
94-
// exceptions support.
95-
var isPointer = ___cxa_is_pointer_type(this.get_type());
96-
if (isPointer) {
97-
return {{{ makeGetValue('this.excPtr', '0', '*') }}};
98-
}
99-
var adjusted = this.get_adjusted_ptr();
100-
if (adjusted !== 0) return adjusted;
101-
return this.excPtr;
102-
}
10384
},
10485

10586
// Here, we throw an exception after recording a couple of values that we need to remember
@@ -146,6 +127,7 @@ var LibraryExceptions = {
146127

147128
#if !DISABLE_EXCEPTION_CATCHING
148129
__cxa_begin_catch__deps: ['$exceptionCaught', '__cxa_increment_exception_refcount',
130+
'__cxa_get_exception_ptr',
149131
'$uncaughtExceptionCount'],
150132
__cxa_begin_catch: (ptr) => {
151133
var info = new ExceptionInfo(ptr);
@@ -158,8 +140,8 @@ var LibraryExceptions = {
158140
#if EXCEPTION_DEBUG
159141
dbg('__cxa_begin_catch ' + [ptrToString(ptr), 'stack', exceptionCaught]);
160142
#endif
161-
___cxa_increment_exception_refcount(info.excPtr);
162-
return info.get_exception_ptr();
143+
___cxa_increment_exception_refcount(ptr);
144+
return ___cxa_get_exception_ptr(ptr);
163145
},
164146

165147
// We're done with a catch. Now, we can run the destructor if there is one
@@ -183,15 +165,6 @@ var LibraryExceptions = {
183165
exceptionLast = 0; // XXX in decRef?
184166
},
185167

186-
__cxa_get_exception_ptr__deps: ['$ExceptionInfo'],
187-
__cxa_get_exception_ptr: (ptr) => {
188-
var rtn = new ExceptionInfo(ptr).get_exception_ptr();
189-
#if EXCEPTION_DEBUG
190-
dbg('__cxa_get_exception_ptr ' + ptrToString(ptr) + ' -> ' + ptrToString(rtn));
191-
#endif
192-
return rtn;
193-
},
194-
195168
__cxa_uncaught_exceptions__deps: ['$uncaughtExceptionCount'],
196169
__cxa_uncaught_exceptions: () => uncaughtExceptionCount,
197170

src/library_sigs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ sigs = {
220220
__cxa_call_unexpected__sig: 'vp',
221221
__cxa_current_primary_exception__sig: 'p',
222222
__cxa_end_catch__sig: 'v',
223-
__cxa_get_exception_ptr__sig: 'pp',
224223
__cxa_rethrow__sig: 'v',
225224
__cxa_rethrow_primary_exception__sig: 'vp',
226225
__cxa_throw__sig: 'vppp',

system/lib/libcxxabi/src/cxa_exception_emscripten.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
#include "cxa_exception.h"
1818
#include "include/atomic_support.h"
1919
#include "fallback_malloc.h"
20+
#include "private_typeinfo.h"
2021
#include "stdio.h"
2122
#include "assert.h"
2223

24+
#ifdef __WASM_EXCEPTIONS__
25+
#error "This file should only be included when building with emscripten exceptions"
26+
#endif
27+
2328
// Define to enable extra debugging on stderr.
2429
#if EXCEPTIONS_DEBUG
2530
#include "emscripten/console.h"
@@ -109,6 +114,26 @@ void __cxa_free_exception(void *thrown_object) throw() {
109114
__aligned_free_with_fallback((void *)raw_buffer);
110115
}
111116

117+
void *__cxa_get_exception_ptr(void *thrown_object) throw() {
118+
// Get pointer which is expected to be received by catch clause in C++ code.
119+
// It may be adjusted when the pointer is casted to some of the exception
120+
// object base classes (e.g. when virtual inheritance is used). When a pointer
121+
// is thrown this method should return the thrown pointer itself.
122+
// Work around a fastcomp bug, this code is still included for some reason in
123+
// a build without exceptions support.
124+
__cxa_exception* ex = cxa_exception_from_thrown_object(thrown_object);
125+
bool is_pointer = !!dynamic_cast<__pointer_type_info*>(ex->exceptionType);
126+
void* rtn;
127+
if (is_pointer)
128+
rtn = *(void**)thrown_object;
129+
else if (ex->adjustedPtr)
130+
rtn = ex->adjustedPtr;
131+
else
132+
rtn = ex;
133+
DEBUG("__cxa_get_exception_ptr %p -> %p\n", thrown_object, rtn);
134+
return rtn;
135+
}
136+
112137
/*
113138
If thrown_object is not null, atomically increment the referenceCount field
114139
of the __cxa_exception header associated with the thrown object referred to

system/lib/libcxxabi/src/private_typeinfo.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,10 +1555,6 @@ int __cxa_can_catch(__shim_type_info* catchType, __shim_type_info* excpType, voi
15551555
return ret;
15561556
}
15571557

1558-
int __cxa_is_pointer_type(__shim_type_info* type) {
1559-
return !!dynamic_cast<__pointer_type_info*>(type);
1560-
}
1561-
15621558
}
15631559
#endif // __EMSCRIPTEN_EXCEPTIONS__
15641560

test/code_size/embind_hello_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"a.html.gz": 380,
44
"a.js": 9961,
55
"a.js.gz": 4350,
6-
"a.wasm": 7820,
7-
"a.wasm.gz": 3526,
8-
"total": 18333,
9-
"total_gz": 8256
6+
"a.wasm": 7715,
7+
"a.wasm.gz": 3508,
8+
"total": 18228,
9+
"total_gz": 8238
1010
}

test/code_size/embind_val_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"a.html.gz": 380,
44
"a.js": 6849,
55
"a.js.gz": 2947,
6-
"a.wasm": 11586,
7-
"a.wasm.gz": 5782,
8-
"total": 18987,
9-
"total_gz": 9109
6+
"a.wasm": 9563,
7+
"a.wasm.gz": 4917,
8+
"total": 16964,
9+
"total_gz": 8244
1010
}

test/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,8 @@ def metafunc(self, mode, *args, **kwargs):
577577
else:
578578
self.set_setting('DISABLE_EXCEPTION_CATCHING', 0)
579579
self.set_setting('SUPPORT_LONGJMP', 'emscripten')
580-
# DISABLE_EXCEPTION_CATCHING=0 exports __cxa_can_catch and
581-
# __cxa_is_pointer_type, so if we don't build in C++ mode, wasm-ld will
580+
# DISABLE_EXCEPTION_CATCHING=0 exports __cxa_can_catch,
581+
# so if we don't build in C++ mode, wasm-ld will
582582
# error out because libc++abi is not included. See
583583
# https://github.com/emscripten-core/emscripten/pull/14192 for details.
584584
self.set_setting('DEFAULT_TO_CXX')

test/other/metadce/test_metadce_cxx_ctors1.exports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
__cxa_is_pointer_type
21
__indirect_function_table
32
__wasm_call_ctors
43
_emscripten_stack_alloc
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
129474
1+
129316

test/other/metadce/test_metadce_cxx_ctors2.exports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
__cxa_is_pointer_type
21
__indirect_function_table
32
_emscripten_stack_alloc
43
_emscripten_stack_restore

0 commit comments

Comments
 (0)