@@ -90,10 +90,34 @@ operator new(std::size_t size) _THROW_BAD_ALLOC
90
90
return p;
91
91
}
92
92
93
+ #if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
94
+ void * _new_nothrow (size_t size) noexcept
95
+ {
96
+ // / We cannot call ::operator new(size) here because it would abort
97
+ // / when malloc returns 0 and exceptions are disabled.
98
+ // / Expected behaviour of std::nothrow is to return 0 in that case.
99
+ void * p = nullptr ;
100
+ if (size == 0 )
101
+ size = 1 ;
102
+ while ((p = ::malloc (size)) == nullptr )
103
+ {
104
+ std::new_handler nh = std::get_new_handler ();
105
+ if (nh)
106
+ nh ();
107
+ else
108
+ break ;
109
+ }
110
+ return p;
111
+ }
112
+ #endif
113
+
93
114
_LIBCPP_WEAK
94
115
void *
95
116
operator new (size_t size, const std::nothrow_t &) noexcept
96
117
{
118
+ #if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
119
+ return _new_nothrow (size);
120
+ #else
97
121
void * p = nullptr ;
98
122
#ifndef _LIBCPP_NO_EXCEPTIONS
99
123
try
@@ -107,6 +131,7 @@ operator new(size_t size, const std::nothrow_t&) noexcept
107
131
}
108
132
#endif // _LIBCPP_NO_EXCEPTIONS
109
133
return p;
134
+ #endif // __EMSCRIPTEN__ && _LIBCPP_NO_EXCEPTIONS
110
135
}
111
136
112
137
_LIBCPP_WEAK
@@ -120,6 +145,9 @@ _LIBCPP_WEAK
120
145
void *
121
146
operator new [](size_t size, const std::nothrow_t &) noexcept
122
147
{
148
+ #if defined(__EMSCRIPTEN__) && defined(_LIBCPP_NO_EXCEPTIONS)
149
+ return _new_nothrow (size);
150
+ #else
123
151
void * p = nullptr ;
124
152
#ifndef _LIBCPP_NO_EXCEPTIONS
125
153
try
@@ -133,6 +161,7 @@ operator new[](size_t size, const std::nothrow_t&) noexcept
133
161
}
134
162
#endif // _LIBCPP_NO_EXCEPTIONS
135
163
return p;
164
+ #endif // __EMSCRIPTEN__ && _LIBCPP_NO_EXCEPTIONS
136
165
}
137
166
138
167
_LIBCPP_WEAK
0 commit comments