Skip to content

Commit

Permalink
[WebAssembly] Upstream misc. EH changes
Browse files Browse the repository at this point in the history
This upstreams more recent, mostly EH changes from libcxx and libcxxabi:
- `__cxa_init_primary_exception`-related changes made when updating to
  LLVM 18.1.2 (emscripten-core/emscripten#21638)
- Removes ctype macros
  (emscripten-core/emscripten#20960)
- Guard destructor changes with `__wasm__`
  (emscripten-core/emscripten#21974)
  • Loading branch information
aheejin committed May 22, 2024
1 parent d53c6cd commit e9b94cd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
17 changes: 14 additions & 3 deletions libcxx/include/__exception/exception_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ struct __cxa_exception;
_LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception(
void*,
std::type_info*,
void(
# if defined(_WIN32)
__thiscall
void(__thiscall*)(void*)) throw();
# elif defined(__wasm__)
// In Wasm, a destructor returns its argument
void* (*)(void*)) throw();
# else
void (*)(void*)) throw();
# endif
*)(void*)) throw();
}

} // namespace __cxxabiv1
Expand Down Expand Up @@ -92,8 +95,16 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep __e) _NOEXCEPT {
using _Ep2 = __decay_t<_Ep>;

void* __ex = __cxxabiv1::__cxa_allocate_exception(sizeof(_Ep));
# ifdef __wasm__
// In Wasm, a destructor returns its argument
(void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) -> void* {
# else
(void)__cxxabiv1::__cxa_init_primary_exception(__ex, const_cast<std::type_info*>(&typeid(_Ep)), [](void* __p) {
# endif
std::__destroy_at(static_cast<_Ep2*>(__p));
# ifdef __wasm__
return __p;
# endif
});

try {
Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/__locale
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,12 @@ public:
static const mask __regex_word = 0x4000; // 0x8000 and 0x0100 and 0x00ff are used
# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
# ifdef __APPLE__
typedef __uint32_t mask;
# elif defined(__FreeBSD__)
typedef unsigned long mask;
# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
# elif defined(__NetBSD__)
typedef unsigned short mask;
# endif
static const mask space = _CTYPE_S;
Expand Down
8 changes: 6 additions & 2 deletions libcxxabi/include/cxxabi.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ extern _LIBCXXABI_FUNC_VIS void
__cxa_free_exception(void *thrown_exception) throw();
// This function is an LLVM extension, which mirrors the same extension in libsupc++ and libcxxrt
extern _LIBCXXABI_FUNC_VIS __cxa_exception*
#ifdef __wasm__
// In Wasm, a destructor returns its argument
__cxa_init_primary_exception(void* object, std::type_info* tinfo, void*(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw();
#else
__cxa_init_primary_exception(void* object, std::type_info* tinfo, void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw();
#endif

// 2.4.3 Throwing the Exception Object
extern _LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void
__cxa_throw(void *thrown_exception, std::type_info *tinfo,
#ifdef __WASM_EXCEPTIONS__
// In Wasm, a destructor returns its argument
#ifdef __wasm__
void *(_LIBCXXABI_DTOR_FUNC *dest)(void *));
#else
void (_LIBCXXABI_DTOR_FUNC *dest)(void *));
Expand Down
7 changes: 6 additions & 1 deletion libcxxabi/src/cxa_exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ void __cxa_free_exception(void *thrown_object) throw() {
}

__cxa_exception* __cxa_init_primary_exception(void* object, std::type_info* tinfo,
#ifdef __wasm__
// In Wasm, a destructor returns its argument
void *(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() {
#else
void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() {
#endif
__cxa_exception* exception_header = cxa_exception_from_thrown_object(object);
exception_header->referenceCount = 0;
exception_header->unexpectedHandler = std::get_unexpected();
Expand Down Expand Up @@ -267,7 +272,7 @@ will call terminate, assuming that there was no handler for the
exception.
*/
void
#ifdef __WASM_EXCEPTIONS__
#ifdef __wasm__
// In Wasm, a destructor returns its argument
__cxa_throw(void *thrown_object, std::type_info *tinfo, void *(_LIBCXXABI_DTOR_FUNC *dest)(void *)) {
#else
Expand Down
2 changes: 1 addition & 1 deletion libcxxabi/src/cxa_exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct _LIBCXXABI_HIDDEN __cxa_exception {

// Manage the exception object itself.
std::type_info *exceptionType;
#ifdef __WASM_EXCEPTIONS__
#ifdef __wasm__
// In Wasm, a destructor returns its argument
void *(_LIBCXXABI_DTOR_FUNC *exceptionDestructor)(void *);
#else
Expand Down

0 comments on commit e9b94cd

Please sign in to comment.