1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 9//===----------------------------------------------------------------------===// 10 11#include <cstdio> 12 13namespace std { 14 15_LIBCPP_SAFE_STATIC static std::terminate_handler __terminate_handler; 16_LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler; 17 18 19// libcxxrt provides implementations of these functions itself. 20unexpected_handler 21set_unexpected(unexpected_handler func) _NOEXCEPT 22{ 23 return __sync_lock_test_and_set(&__unexpected_handler, func); 24} 25 26unexpected_handler 27get_unexpected() _NOEXCEPT 28{ 29 return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0); 30 31} 32 33_LIBCPP_NORETURN 34void unexpected() 35{ 36 (*get_unexpected())(); 37 // unexpected handler should not return 38 terminate(); 39} 40 41terminate_handler 42set_terminate(terminate_handler func) _NOEXCEPT 43{ 44 return __sync_lock_test_and_set(&__terminate_handler, func); 45} 46 47terminate_handler 48get_terminate() _NOEXCEPT 49{ 50 return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0); 51 52} 53 54#ifndef __EMSCRIPTEN__ // We provide this in JS 55_LIBCPP_NORETURN 56void 57terminate() _NOEXCEPT 58{ 59#ifndef _LIBCPP_NO_EXCEPTIONS 60 try 61 { 62#endif // _LIBCPP_NO_EXCEPTIONS 63 (*get_terminate())(); 64 // handler should not return 65 fprintf(stderr, "terminate_handler unexpectedly returned\n"); 66 ::abort(); 67#ifndef _LIBCPP_NO_EXCEPTIONS 68 } 69 catch (...) 70 { 71 // handler should not throw exception 72 fprintf(stderr, "terminate_handler unexpectedly threw an exception\n"); 73 ::abort(); 74 } 75#endif // _LIBCPP_NO_EXCEPTIONS 76} 77#endif // !__EMSCRIPTEN__ 78 79#if !defined(__EMSCRIPTEN__) 80bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; } 81 82int uncaught_exceptions() _NOEXCEPT 83{ 84#warning uncaught_exception not yet implemented 85 fprintf(stderr, "uncaught_exceptions not yet implemented\n"); 86 ::abort(); 87} 88#endif // !__EMSCRIPTEN__ 89 90 91exception::~exception() _NOEXCEPT 92{ 93} 94 95const char* exception::what() const _NOEXCEPT 96{ 97 return "std::exception"; 98} 99 100bad_exception::~bad_exception() _NOEXCEPT 101{ 102} 103 104const char* bad_exception::what() const _NOEXCEPT 105{ 106 return "std::bad_exception"; 107} 108 109 110bad_alloc::bad_alloc() _NOEXCEPT 111{ 112} 113 114bad_alloc::~bad_alloc() _NOEXCEPT 115{ 116} 117 118const char* 119bad_alloc::what() const _NOEXCEPT 120{ 121 return "std::bad_alloc"; 122} 123 124bad_array_new_length::bad_array_new_length() _NOEXCEPT 125{ 126} 127 128bad_array_new_length::~bad_array_new_length() _NOEXCEPT 129{ 130} 131 132const char* 133bad_array_new_length::what() const _NOEXCEPT 134{ 135 return "bad_array_new_length"; 136} 137 138 139bad_array_length::bad_array_length() _NOEXCEPT 140{ 141} 142 143bad_array_length::~bad_array_length() _NOEXCEPT 144{ 145} 146 147const char* 148bad_array_length::what() const _NOEXCEPT 149{ 150 return "bad_array_length"; 151} 152 153 154bad_cast::bad_cast() _NOEXCEPT 155{ 156} 157 158bad_typeid::bad_typeid() _NOEXCEPT 159{ 160} 161 162bad_cast::~bad_cast() _NOEXCEPT 163{ 164} 165 166const char* 167bad_cast::what() const _NOEXCEPT 168{ 169 return "std::bad_cast"; 170} 171 172bad_typeid::~bad_typeid() _NOEXCEPT 173{ 174} 175 176const char* 177bad_typeid::what() const _NOEXCEPT 178{ 179 return "std::bad_typeid"; 180} 181 182} // namespace std 183