|
1 | 1 | #include "cxa_exception.h"
|
2 |
| -#include "cxxabi.h" |
| 2 | +#include "private_typeinfo.h" |
3 | 3 | #include <stdio.h>
|
4 |
| -#include <typeinfo> |
5 | 4 |
|
6 | 5 | #ifdef __USING_EMSCRIPTEN_EXCEPTIONS__
|
7 | 6 |
|
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 | +} |
9 | 14 |
|
10 |
| -int __cxa_can_catch(const std::type_info* catchType, |
11 |
| - const std::type_info* excpType, |
12 |
| - void** thrown); |
| 15 | +extern "C" { |
13 | 16 |
|
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(); |
19 | 23 |
|
20 | 24 | 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); |
22 | 26 | if (status == 0 && demangled_buf) {
|
23 |
| - exc_name = demangled_buf; |
| 27 | + type_name = demangled_buf; |
24 | 28 | }
|
25 | 29 |
|
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); |
27 | 33 | char* result = NULL;
|
28 | 34 | 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); |
31 | 38 | } else {
|
32 | 39 | asprintf(&result,
|
33 | 40 | "Cpp Exception: The exception is an object of type '%s' at "
|
34 | 41 | "address %p which does not inherit from std::exception",
|
35 |
| - exc_name, |
36 |
| - exc_ptr); |
| 42 | + type_name, |
| 43 | + thrown_object); |
37 | 44 | }
|
38 | 45 |
|
39 | 46 | if (demangled_buf) {
|
|
0 commit comments