Skip to content

Commit 2b45f62

Browse files
authored
[EH] Tidy up format_exception.cpp (NFC) (#16995)
This makes small cosmetic changes that do not affect the functionality. - Use `using namespace __cxxabiv1`. This namespace is used many times and I think it's OK to put this at the top. Other libc++abi cpp files also do this. - Use `static_cast` instead of the old cast. This is also in line with the rest of libc++abi. - Change some variable names. This tries to make variable names in line with libc++abi's other files; personally I don't particularly like their long names such as `exception_header`, but I think keeping them in line with the rest of the code is generally good and possibly helpful when we upstream the changes. - Add `cxa_exception_from_thrown_object` and use it. The reason I copied this function from cxa_exception.cpp is I'm planning to use all these three when I add Wasm EH support for this function, and it will be easier to read if we use these than just doing the arthmetic ourselves. https://github.com/emscripten-core/emscripten/blob/d5ef6937fe395488e23a82c1e582a7ea5c2dab83/system/lib/libcxxabi/src/cxa_exception.cpp#L39-L67 The reason I copied them is they are static functions in cxa_exception.cpp. We possibly can move them to a header file and include it both places, but that means further changes to libc++abi codebase, which I think will make this harder to upstream. - Use `__shim_type_info::can_catch` directly instead of `__cxa_catch`, which is a wrapper created to be called from JS. This removes the need to include the declaration of `__cxa_catch` in this file.
1 parent 75f5315 commit 2b45f62

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

system/lib/libcxxabi/src/format_exception.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
11
#include "cxa_exception.h"
2-
#include "cxxabi.h"
2+
#include "private_typeinfo.h"
33
#include <stdio.h>
4-
#include <typeinfo>
54

65
#ifdef __USING_EMSCRIPTEN_EXCEPTIONS__
76

8-
extern "C" {
7+
using namespace __cxxabiv1;
8+
9+
// Utility routines copied from cxa_exception.cpp
10+
static inline __cxa_exception*
11+
cxa_exception_from_thrown_object(void* thrown_object) {
12+
return static_cast<__cxa_exception*>(thrown_object) - 1;
13+
}
914

10-
int __cxa_can_catch(const std::type_info* catchType,
11-
const std::type_info* excpType,
12-
void** thrown);
15+
extern "C" {
1316

14-
char* emscripten_format_exception(void* exc_ptr) {
15-
__cxxabiv1::__cxa_exception* exc_info =
16-
(__cxxabiv1::__cxa_exception*)exc_ptr - 1;
17-
std::type_info* exc_type = exc_info->exceptionType;
18-
const char* exc_name = exc_type->name();
17+
char* emscripten_format_exception(void* thrown_object) {
18+
__cxa_exception* exception_header =
19+
cxa_exception_from_thrown_object(thrown_object);
20+
const __shim_type_info* thrown_type =
21+
static_cast<const __shim_type_info*>(exception_header->exceptionType);
22+
const char* type_name = thrown_type->name();
1923

2024
int status = 0;
21-
char* demangled_buf = __cxxabiv1::__cxa_demangle(exc_name, 0, 0, &status);
25+
char* demangled_buf = __cxa_demangle(type_name, 0, 0, &status);
2226
if (status == 0 && demangled_buf) {
23-
exc_name = demangled_buf;
27+
type_name = demangled_buf;
2428
}
2529

26-
int can_catch = __cxa_can_catch(&typeid(std::exception), exc_type, &exc_ptr);
30+
const __shim_type_info* catch_type =
31+
static_cast<const __shim_type_info*>(&typeid(std::exception));
32+
int can_catch = catch_type->can_catch(thrown_type, thrown_object);
2733
char* result = NULL;
2834
if (can_catch) {
29-
const char* exc_what = ((std::exception*)exc_ptr)->what();
30-
asprintf(&result, "Cpp Exception %s: %s", exc_name, exc_what);
35+
const char* what =
36+
static_cast<const std::exception*>(thrown_object)->what();
37+
asprintf(&result, "Cpp Exception %s: %s", type_name, what);
3138
} else {
3239
asprintf(&result,
3340
"Cpp Exception: The exception is an object of type '%s' at "
3441
"address %p which does not inherit from std::exception",
35-
exc_name,
36-
exc_ptr);
42+
type_name,
43+
thrown_object);
3744
}
3845

3946
if (demangled_buf) {

0 commit comments

Comments
 (0)