1 |
|
|
// Copyright Joyent, Inc. and other Node contributors. |
2 |
|
|
// |
3 |
|
|
// Permission is hereby granted, free of charge, to any person obtaining a |
4 |
|
|
// copy of this software and associated documentation files (the |
5 |
|
|
// "Software"), to deal in the Software without restriction, including |
6 |
|
|
// without limitation the rights to use, copy, modify, merge, publish, |
7 |
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit |
8 |
|
|
// persons to whom the Software is furnished to do so, subject to the |
9 |
|
|
// following conditions: |
10 |
|
|
// |
11 |
|
|
// The above copyright notice and this permission notice shall be included |
12 |
|
|
// in all copies or substantial portions of the Software. |
13 |
|
|
// |
14 |
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
15 |
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
16 |
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
17 |
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
18 |
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
19 |
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
20 |
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE. |
21 |
|
|
|
22 |
|
|
#ifndef SRC_UTIL_INL_H_ |
23 |
|
|
#define SRC_UTIL_INL_H_ |
24 |
|
|
|
25 |
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
26 |
|
|
|
27 |
|
|
#include <cmath> |
28 |
|
|
#include <cstring> |
29 |
|
|
#include <locale> |
30 |
|
|
#include "util.h" |
31 |
|
|
|
32 |
|
|
// These are defined by <sys/byteorder.h> or <netinet/in.h> on some systems. |
33 |
|
|
// To avoid warnings, undefine them before redefining them. |
34 |
|
|
#ifdef BSWAP_2 |
35 |
|
|
# undef BSWAP_2 |
36 |
|
|
#endif |
37 |
|
|
#ifdef BSWAP_4 |
38 |
|
|
# undef BSWAP_4 |
39 |
|
|
#endif |
40 |
|
|
#ifdef BSWAP_8 |
41 |
|
|
# undef BSWAP_8 |
42 |
|
|
#endif |
43 |
|
|
|
44 |
|
|
#if defined(_MSC_VER) |
45 |
|
|
#include <intrin.h> |
46 |
|
|
#define BSWAP_2(x) _byteswap_ushort(x) |
47 |
|
|
#define BSWAP_4(x) _byteswap_ulong(x) |
48 |
|
|
#define BSWAP_8(x) _byteswap_uint64(x) |
49 |
|
|
#else |
50 |
|
|
#define BSWAP_2(x) ((x) << 8) | ((x) >> 8) |
51 |
|
|
#define BSWAP_4(x) \ |
52 |
|
|
(((x) & 0xFF) << 24) | \ |
53 |
|
|
(((x) & 0xFF00) << 8) | \ |
54 |
|
|
(((x) >> 8) & 0xFF00) | \ |
55 |
|
|
(((x) >> 24) & 0xFF) |
56 |
|
|
#define BSWAP_8(x) \ |
57 |
|
|
(((x) & 0xFF00000000000000ull) >> 56) | \ |
58 |
|
|
(((x) & 0x00FF000000000000ull) >> 40) | \ |
59 |
|
|
(((x) & 0x0000FF0000000000ull) >> 24) | \ |
60 |
|
|
(((x) & 0x000000FF00000000ull) >> 8) | \ |
61 |
|
|
(((x) & 0x00000000FF000000ull) << 8) | \ |
62 |
|
|
(((x) & 0x0000000000FF0000ull) << 24) | \ |
63 |
|
|
(((x) & 0x000000000000FF00ull) << 40) | \ |
64 |
|
|
(((x) & 0x00000000000000FFull) << 56) |
65 |
|
|
#endif |
66 |
|
|
|
67 |
|
|
#define CHAR_TEST(bits, name, expr) \ |
68 |
|
|
template <typename T> \ |
69 |
|
|
bool name(const T ch) { \ |
70 |
|
|
static_assert(sizeof(ch) >= (bits) / 8, \ |
71 |
|
|
"Character must be wider than " #bits " bits"); \ |
72 |
|
|
return (expr); \ |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
namespace node { |
76 |
|
|
|
77 |
|
|
template <typename T> |
78 |
|
210510 |
ListNode<T>::ListNode() : prev_(this), next_(this) {} |
79 |
|
|
|
80 |
|
|
template <typename T> |
81 |
|
207019 |
ListNode<T>::~ListNode() { |
82 |
|
207019 |
Remove(); |
83 |
|
207019 |
} |
84 |
|
|
|
85 |
|
|
template <typename T> |
86 |
|
323513 |
void ListNode<T>::Remove() { |
87 |
|
323513 |
prev_->next_ = next_; |
88 |
|
323513 |
next_->prev_ = prev_; |
89 |
|
323513 |
prev_ = this; |
90 |
|
323513 |
next_ = this; |
91 |
|
323513 |
} |
92 |
|
|
|
93 |
|
|
template <typename T> |
94 |
|
70945 |
bool ListNode<T>::IsEmpty() const { |
95 |
|
70945 |
return prev_ == this; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
template <typename T, ListNode<T> (T::*M)> |
99 |
|
91532 |
ListHead<T, M>::Iterator::Iterator(ListNode<T>* node) : node_(node) {} |
100 |
|
|
|
101 |
|
|
template <typename T, ListNode<T> (T::*M)> |
102 |
|
9801 |
T* ListHead<T, M>::Iterator::operator*() const { |
103 |
|
9801 |
return ContainerOf(M, node_); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
template <typename T, ListNode<T> (T::*M)> |
107 |
|
|
const typename ListHead<T, M>::Iterator& |
108 |
|
9801 |
ListHead<T, M>::Iterator::operator++() { |
109 |
|
9801 |
node_ = node_->next_; |
110 |
|
9801 |
return *this; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
template <typename T, ListNode<T> (T::*M)> |
114 |
|
55565 |
bool ListHead<T, M>::Iterator::operator!=(const Iterator& that) const { |
115 |
|
55565 |
return node_ != that.node_; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
template <typename T, ListNode<T> (T::*M)> |
119 |
|
22877 |
ListHead<T, M>::~ListHead() { |
120 |
✗✓ |
22877 |
while (IsEmpty() == false) |
121 |
|
|
head_.next_->Remove(); |
122 |
|
22877 |
} |
123 |
|
|
|
124 |
|
|
template <typename T, ListNode<T> (T::*M)> |
125 |
|
136705 |
void ListHead<T, M>::PushBack(T* element) { |
126 |
|
136705 |
ListNode<T>* that = &(element->*M); |
127 |
|
136705 |
head_.prev_->next_ = that; |
128 |
|
136705 |
that->prev_ = head_.prev_; |
129 |
|
136705 |
that->next_ = &head_; |
130 |
|
136705 |
head_.prev_ = that; |
131 |
|
136705 |
} |
132 |
|
|
|
133 |
|
|
template <typename T, ListNode<T> (T::*M)> |
134 |
|
|
void ListHead<T, M>::PushFront(T* element) { |
135 |
|
|
ListNode<T>* that = &(element->*M); |
136 |
|
|
head_.next_->prev_ = that; |
137 |
|
|
that->prev_ = &head_; |
138 |
|
|
that->next_ = head_.next_; |
139 |
|
|
head_.next_ = that; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
template <typename T, ListNode<T> (T::*M)> |
143 |
|
70941 |
bool ListHead<T, M>::IsEmpty() const { |
144 |
|
70941 |
return head_.IsEmpty(); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
template <typename T, ListNode<T> (T::*M)> |
148 |
|
2 |
T* ListHead<T, M>::PopFront() { |
149 |
✗✓ |
2 |
if (IsEmpty()) |
150 |
|
|
return nullptr; |
151 |
|
2 |
ListNode<T>* node = head_.next_; |
152 |
|
2 |
node->Remove(); |
153 |
|
2 |
return ContainerOf(M, node); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
template <typename T, ListNode<T> (T::*M)> |
157 |
|
45764 |
typename ListHead<T, M>::Iterator ListHead<T, M>::begin() const { |
158 |
|
45764 |
return Iterator(head_.next_); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
template <typename T, ListNode<T> (T::*M)> |
162 |
|
45768 |
typename ListHead<T, M>::Iterator ListHead<T, M>::end() const { |
163 |
|
45768 |
return Iterator(const_cast<ListNode<T>*>(&head_)); |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
template <typename Inner, typename Outer> |
167 |
|
2249981 |
constexpr uintptr_t OffsetOf(Inner Outer::*field) { |
168 |
|
2249981 |
return reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(nullptr)->*field)); |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
template <typename Inner, typename Outer> |
172 |
|
2183129 |
ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field, |
173 |
|
|
Inner* pointer) |
174 |
|
|
: pointer_( |
175 |
|
|
reinterpret_cast<Outer*>( |
176 |
|
2183129 |
reinterpret_cast<uintptr_t>(pointer) - OffsetOf(field))) {} |
177 |
|
|
|
178 |
|
|
template <typename Inner, typename Outer> |
179 |
|
|
template <typename TypeName> |
180 |
|
2183129 |
ContainerOfHelper<Inner, Outer>::operator TypeName*() const { |
181 |
|
2183129 |
return static_cast<TypeName*>(pointer_); |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
template <typename Inner, typename Outer> |
185 |
|
2175797 |
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field, |
186 |
|
|
Inner* pointer) { |
187 |
|
2175797 |
return ContainerOfHelper<Inner, Outer>(field, pointer); |
188 |
|
|
} |
189 |
|
|
|
190 |
|
3932261 |
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate, |
191 |
|
|
const char* data, |
192 |
|
|
int length) { |
193 |
|
3932261 |
return v8::String::NewFromOneByte(isolate, |
194 |
|
|
reinterpret_cast<const uint8_t*>(data), |
195 |
|
|
v8::NewStringType::kNormal, |
196 |
|
3932261 |
length).ToLocalChecked(); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate, |
200 |
|
|
const signed char* data, |
201 |
|
|
int length) { |
202 |
|
|
return v8::String::NewFromOneByte(isolate, |
203 |
|
|
reinterpret_cast<const uint8_t*>(data), |
204 |
|
|
v8::NewStringType::kNormal, |
205 |
|
|
length).ToLocalChecked(); |
206 |
|
|
} |
207 |
|
|
|
208 |
|
1352 |
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate, |
209 |
|
|
const unsigned char* data, |
210 |
|
|
int length) { |
211 |
|
1352 |
return v8::String::NewFromOneByte( |
212 |
|
1352 |
isolate, data, v8::NewStringType::kNormal, length) |
213 |
|
1352 |
.ToLocalChecked(); |
214 |
|
|
} |
215 |
|
|
|
216 |
|
2 |
void SwapBytes16(char* data, size_t nbytes) { |
217 |
✗✓ |
2 |
CHECK_EQ(nbytes % 2, 0); |
218 |
|
|
|
219 |
|
|
#if defined(_MSC_VER) |
220 |
|
|
if (AlignUp(data, sizeof(uint16_t)) == data) { |
221 |
|
|
// MSVC has no strict aliasing, and is able to highly optimize this case. |
222 |
|
|
uint16_t* data16 = reinterpret_cast<uint16_t*>(data); |
223 |
|
|
size_t len16 = nbytes / sizeof(*data16); |
224 |
|
|
for (size_t i = 0; i < len16; i++) { |
225 |
|
|
data16[i] = BSWAP_2(data16[i]); |
226 |
|
|
} |
227 |
|
|
return; |
228 |
|
|
} |
229 |
|
|
#endif |
230 |
|
|
|
231 |
|
|
uint16_t temp; |
232 |
✓✓ |
1537 |
for (size_t i = 0; i < nbytes; i += sizeof(temp)) { |
233 |
|
1535 |
memcpy(&temp, &data[i], sizeof(temp)); |
234 |
|
1535 |
temp = BSWAP_2(temp); |
235 |
|
1535 |
memcpy(&data[i], &temp, sizeof(temp)); |
236 |
|
|
} |
237 |
|
2 |
} |
238 |
|
|
|
239 |
|
2 |
void SwapBytes32(char* data, size_t nbytes) { |
240 |
✗✓ |
2 |
CHECK_EQ(nbytes % 4, 0); |
241 |
|
|
|
242 |
|
|
#if defined(_MSC_VER) |
243 |
|
|
// MSVC has no strict aliasing, and is able to highly optimize this case. |
244 |
|
|
if (AlignUp(data, sizeof(uint32_t)) == data) { |
245 |
|
|
uint32_t* data32 = reinterpret_cast<uint32_t*>(data); |
246 |
|
|
size_t len32 = nbytes / sizeof(*data32); |
247 |
|
|
for (size_t i = 0; i < len32; i++) { |
248 |
|
|
data32[i] = BSWAP_4(data32[i]); |
249 |
|
|
} |
250 |
|
|
return; |
251 |
|
|
} |
252 |
|
|
#endif |
253 |
|
|
|
254 |
|
|
uint32_t temp; |
255 |
✓✓ |
769 |
for (size_t i = 0; i < nbytes; i += sizeof(temp)) { |
256 |
|
767 |
memcpy(&temp, &data[i], sizeof(temp)); |
257 |
|
767 |
temp = BSWAP_4(temp); |
258 |
|
767 |
memcpy(&data[i], &temp, sizeof(temp)); |
259 |
|
|
} |
260 |
|
2 |
} |
261 |
|
|
|
262 |
|
2 |
void SwapBytes64(char* data, size_t nbytes) { |
263 |
✗✓ |
2 |
CHECK_EQ(nbytes % 8, 0); |
264 |
|
|
|
265 |
|
|
#if defined(_MSC_VER) |
266 |
|
|
if (AlignUp(data, sizeof(uint64_t)) == data) { |
267 |
|
|
// MSVC has no strict aliasing, and is able to highly optimize this case. |
268 |
|
|
uint64_t* data64 = reinterpret_cast<uint64_t*>(data); |
269 |
|
|
size_t len64 = nbytes / sizeof(*data64); |
270 |
|
|
for (size_t i = 0; i < len64; i++) { |
271 |
|
|
data64[i] = BSWAP_8(data64[i]); |
272 |
|
|
} |
273 |
|
|
return; |
274 |
|
|
} |
275 |
|
|
#endif |
276 |
|
|
|
277 |
|
|
uint64_t temp; |
278 |
✓✓ |
513 |
for (size_t i = 0; i < nbytes; i += sizeof(temp)) { |
279 |
|
511 |
memcpy(&temp, &data[i], sizeof(temp)); |
280 |
|
511 |
temp = BSWAP_8(temp); |
281 |
|
511 |
memcpy(&data[i], &temp, sizeof(temp)); |
282 |
|
|
} |
283 |
|
2 |
} |
284 |
|
|
|
285 |
|
55715 |
char ToLower(char c) { |
286 |
|
55715 |
return std::tolower(c, std::locale::classic()); |
287 |
|
|
} |
288 |
|
|
|
289 |
|
4011 |
std::string ToLower(const std::string& in) { |
290 |
|
4011 |
std::string out(in.size(), 0); |
291 |
✓✓ |
52199 |
for (size_t i = 0; i < in.size(); ++i) |
292 |
|
48188 |
out[i] = ToLower(in[i]); |
293 |
|
4011 |
return out; |
294 |
|
|
} |
295 |
|
|
|
296 |
|
13362 |
char ToUpper(char c) { |
297 |
|
13362 |
return std::toupper(c, std::locale::classic()); |
298 |
|
|
} |
299 |
|
|
|
300 |
|
2 |
std::string ToUpper(const std::string& in) { |
301 |
|
2 |
std::string out(in.size(), 0); |
302 |
✓✓ |
5 |
for (size_t i = 0; i < in.size(); ++i) |
303 |
|
3 |
out[i] = ToUpper(in[i]); |
304 |
|
2 |
return out; |
305 |
|
|
} |
306 |
|
|
|
307 |
|
1334 |
bool StringEqualNoCase(const char* a, const char* b) { |
308 |
✓✓ |
1334 |
while (ToLower(*a) == ToLower(*b++)) { |
309 |
✓✓ |
1221 |
if (*a++ == '\0') |
310 |
|
108 |
return true; |
311 |
|
|
} |
312 |
|
113 |
return false; |
313 |
|
|
} |
314 |
|
|
|
315 |
|
643 |
bool StringEqualNoCaseN(const char* a, const char* b, size_t length) { |
316 |
✓✓ |
2623 |
for (size_t i = 0; i < length; i++) { |
317 |
✓✓ |
2428 |
if (ToLower(a[i]) != ToLower(b[i])) |
318 |
|
447 |
return false; |
319 |
✓✓ |
1981 |
if (a[i] == '\0') |
320 |
|
1 |
return true; |
321 |
|
|
} |
322 |
|
195 |
return true; |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
template <typename T> |
326 |
|
463484 |
inline T MultiplyWithOverflowCheck(T a, T b) { |
327 |
|
463484 |
auto ret = a * b; |
328 |
✓✓ |
463484 |
if (a != 0) |
329 |
✗✓ |
463482 |
CHECK_EQ(b, ret / a); |
330 |
|
|
|
331 |
|
463484 |
return ret; |
332 |
|
|
} |
333 |
|
|
|
334 |
|
|
// These should be used in our code as opposed to the native |
335 |
|
|
// versions as they abstract out some platform and or |
336 |
|
|
// compiler version specific functionality. |
337 |
|
|
// malloc(0) and realloc(ptr, 0) have implementation-defined behavior in |
338 |
|
|
// that the standard allows them to either return a unique pointer or a |
339 |
|
|
// nullptr for zero-sized allocation requests. Normalize by always using |
340 |
|
|
// a nullptr. |
341 |
|
|
template <typename T> |
342 |
|
771835 |
T* UncheckedRealloc(T* pointer, size_t n) { |
343 |
|
771835 |
size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n); |
344 |
|
|
|
345 |
✓✓ |
771835 |
if (full_size == 0) { |
346 |
|
211628 |
free(pointer); |
347 |
|
211628 |
return nullptr; |
348 |
|
|
} |
349 |
|
|
|
350 |
|
560207 |
void* allocated = realloc(pointer, full_size); |
351 |
|
|
|
352 |
✗✓ |
560207 |
if (UNLIKELY(allocated == nullptr)) { |
353 |
|
|
// Tell V8 that memory is low and retry. |
354 |
|
|
LowMemoryNotification(); |
355 |
|
|
allocated = realloc(pointer, full_size); |
356 |
|
|
} |
357 |
|
|
|
358 |
|
560207 |
return static_cast<T*>(allocated); |
359 |
|
|
} |
360 |
|
|
|
361 |
|
|
// As per spec realloc behaves like malloc if passed nullptr. |
362 |
|
|
template <typename T> |
363 |
|
49870 |
inline T* UncheckedMalloc(size_t n) { |
364 |
✓✓ |
49870 |
if (n == 0) n = 1; |
365 |
|
49870 |
return UncheckedRealloc<T>(nullptr, n); |
366 |
|
|
} |
367 |
|
|
|
368 |
|
|
template <typename T> |
369 |
|
54 |
inline T* UncheckedCalloc(size_t n) { |
370 |
✓✓ |
54 |
if (n == 0) n = 1; |
371 |
|
54 |
MultiplyWithOverflowCheck(sizeof(T), n); |
372 |
|
54 |
return static_cast<T*>(calloc(n, sizeof(T))); |
373 |
|
|
} |
374 |
|
|
|
375 |
|
|
template <typename T> |
376 |
|
150078 |
inline T* Realloc(T* pointer, size_t n) { |
377 |
|
150078 |
T* ret = UncheckedRealloc(pointer, n); |
378 |
✓✗✗✓
|
150078 |
CHECK_IMPLIES(n > 0, ret != nullptr); |
379 |
|
150078 |
return ret; |
380 |
|
|
} |
381 |
|
|
|
382 |
|
|
template <typename T> |
383 |
|
2014 |
inline T* Malloc(size_t n) { |
384 |
|
2014 |
T* ret = UncheckedMalloc<T>(n); |
385 |
✓✓✗✓
|
2014 |
CHECK_IMPLIES(n > 0, ret != nullptr); |
386 |
|
2014 |
return ret; |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
template <typename T> |
390 |
|
50 |
inline T* Calloc(size_t n) { |
391 |
|
50 |
T* ret = UncheckedCalloc<T>(n); |
392 |
✓✓✗✓
|
50 |
CHECK_IMPLIES(n > 0, ret != nullptr); |
393 |
|
50 |
return ret; |
394 |
|
|
} |
395 |
|
|
|
396 |
|
|
// Shortcuts for char*. |
397 |
|
4 |
inline char* Malloc(size_t n) { return Malloc<char>(n); } |
398 |
|
2 |
inline char* Calloc(size_t n) { return Calloc<char>(n); } |
399 |
|
23902 |
inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc<char>(n); } |
400 |
|
2 |
inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc<char>(n); } |
401 |
|
|
|
402 |
|
|
// This is a helper in the .cc file so including util-inl.h doesn't include more |
403 |
|
|
// headers than we really need to. |
404 |
|
|
void ThrowErrStringTooLong(v8::Isolate* isolate); |
405 |
|
|
|
406 |
|
3880641 |
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
407 |
|
|
std::string_view str, |
408 |
|
|
v8::Isolate* isolate) { |
409 |
✓✓ |
6097592 |
if (isolate == nullptr) isolate = context->GetIsolate(); |
410 |
✗✓ |
3880641 |
if (UNLIKELY(str.size() >= static_cast<size_t>(v8::String::kMaxLength))) { |
411 |
|
|
// V8 only has a TODO comment about adding an exception when the maximum |
412 |
|
|
// string size is exceeded. |
413 |
|
|
ThrowErrStringTooLong(isolate); |
414 |
|
|
return v8::MaybeLocal<v8::Value>(); |
415 |
|
|
} |
416 |
|
|
|
417 |
|
3880641 |
return v8::String::NewFromUtf8( |
418 |
|
3880641 |
isolate, str.data(), v8::NewStringType::kNormal, str.size()) |
419 |
|
3880641 |
.FromMaybe(v8::Local<v8::String>()); |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
template <typename T> |
423 |
|
370354 |
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
424 |
|
|
const std::vector<T>& vec, |
425 |
|
|
v8::Isolate* isolate) { |
426 |
✓✓ |
588776 |
if (isolate == nullptr) isolate = context->GetIsolate(); |
427 |
|
370354 |
v8::EscapableHandleScope handle_scope(isolate); |
428 |
|
|
|
429 |
|
740708 |
MaybeStackBuffer<v8::Local<v8::Value>, 128> arr(vec.size()); |
430 |
|
370354 |
arr.SetLength(vec.size()); |
431 |
✓✓ |
1881654 |
for (size_t i = 0; i < vec.size(); ++i) { |
432 |
✗✓✓ |
3022600 |
if (!ToV8Value(context, vec[i], isolate).ToLocal(&arr[i])) |
433 |
|
|
return v8::MaybeLocal<v8::Value>(); |
434 |
|
|
} |
435 |
|
|
|
436 |
|
740708 |
return handle_scope.Escape(v8::Array::New(isolate, arr.out(), arr.length())); |
437 |
|
|
} |
438 |
|
|
|
439 |
|
|
template <typename T> |
440 |
|
4 |
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
441 |
|
|
const std::set<T>& set, |
442 |
|
|
v8::Isolate* isolate) { |
443 |
✓✗ |
8 |
if (isolate == nullptr) isolate = context->GetIsolate(); |
444 |
|
4 |
v8::Local<v8::Set> set_js = v8::Set::New(isolate); |
445 |
|
8 |
v8::HandleScope handle_scope(isolate); |
446 |
|
|
|
447 |
✓✓ |
464 |
for (const T& entry : set) { |
448 |
|
|
v8::Local<v8::Value> value; |
449 |
✗✓ |
920 |
if (!ToV8Value(context, entry, isolate).ToLocal(&value)) |
450 |
|
|
return {}; |
451 |
✗✓ |
920 |
if (set_js->Add(context, value).IsEmpty()) |
452 |
|
|
return {}; |
453 |
|
|
} |
454 |
|
|
|
455 |
|
4 |
return set_js; |
456 |
|
|
} |
457 |
|
|
|
458 |
|
|
template <typename T, typename U> |
459 |
|
6906 |
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
460 |
|
|
const std::unordered_map<T, U>& map, |
461 |
|
|
v8::Isolate* isolate) { |
462 |
✓✗ |
13812 |
if (isolate == nullptr) isolate = context->GetIsolate(); |
463 |
|
6906 |
v8::EscapableHandleScope handle_scope(isolate); |
464 |
|
|
|
465 |
|
6906 |
v8::Local<v8::Map> ret = v8::Map::New(isolate); |
466 |
✓✓ |
158838 |
for (const auto& item : map) { |
467 |
|
|
v8::Local<v8::Value> first, second; |
468 |
|
151932 |
if (!ToV8Value(context, item.first, isolate).ToLocal(&first) || |
469 |
✓✗✓✗
|
455796 |
!ToV8Value(context, item.second, isolate).ToLocal(&second) || |
470 |
✗✓✗✓
|
455796 |
ret->Set(context, first, second).IsEmpty()) { |
471 |
|
|
return v8::MaybeLocal<v8::Value>(); |
472 |
|
|
} |
473 |
|
|
} |
474 |
|
|
|
475 |
|
6906 |
return handle_scope.Escape(ret); |
476 |
|
|
} |
477 |
|
|
|
478 |
|
|
template <typename T, typename > |
479 |
|
2 |
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
480 |
|
|
const T& number, |
481 |
|
|
v8::Isolate* isolate) { |
482 |
✗✓ |
2 |
if (isolate == nullptr) isolate = context->GetIsolate(); |
483 |
|
|
|
484 |
|
|
using Limits = std::numeric_limits<T>; |
485 |
|
|
// Choose Uint32, Int32, or Double depending on range checks. |
486 |
|
|
// These checks should all collapse at compile time. |
487 |
|
2 |
if (static_cast<uint32_t>(Limits::max()) <= |
488 |
✓✗ |
4 |
std::numeric_limits<uint32_t>::max() && |
489 |
|
2 |
static_cast<uint32_t>(Limits::min()) >= |
490 |
✓✗✓✗
|
6 |
std::numeric_limits<uint32_t>::min() && Limits::is_exact) { |
491 |
|
4 |
return v8::Integer::NewFromUnsigned(isolate, static_cast<uint32_t>(number)); |
492 |
|
|
} |
493 |
|
|
|
494 |
|
|
if (static_cast<int32_t>(Limits::max()) <= |
495 |
|
|
std::numeric_limits<int32_t>::max() && |
496 |
|
|
static_cast<int32_t>(Limits::min()) >= |
497 |
|
|
std::numeric_limits<int32_t>::min() && Limits::is_exact) { |
498 |
|
|
return v8::Integer::New(isolate, static_cast<int32_t>(number)); |
499 |
|
|
} |
500 |
|
|
|
501 |
|
|
return v8::Number::New(isolate, static_cast<double>(number)); |
502 |
|
|
} |
503 |
|
|
|
504 |
|
70766 |
SlicedArguments::SlicedArguments( |
505 |
|
70766 |
const v8::FunctionCallbackInfo<v8::Value>& args, size_t start) { |
506 |
|
70766 |
const size_t length = static_cast<size_t>(args.Length()); |
507 |
✓✓ |
70766 |
if (start >= length) return; |
508 |
|
70748 |
const size_t size = length - start; |
509 |
|
|
|
510 |
|
70748 |
AllocateSufficientStorage(size); |
511 |
✓✓ |
154065 |
for (size_t i = 0; i < size; ++i) |
512 |
✓✗ |
166634 |
(*this)[i] = args[i + start]; |
513 |
|
|
} |
514 |
|
|
|
515 |
|
|
template <typename T, size_t S> |
516 |
|
17563 |
ArrayBufferViewContents<T, S>::ArrayBufferViewContents( |
517 |
|
17563 |
v8::Local<v8::Value> value) { |
518 |
✗✓ |
17563 |
CHECK(value->IsArrayBufferView()); |
519 |
|
17563 |
Read(value.As<v8::ArrayBufferView>()); |
520 |
|
17563 |
} |
521 |
|
|
|
522 |
|
|
template <typename T, size_t S> |
523 |
|
140175 |
ArrayBufferViewContents<T, S>::ArrayBufferViewContents( |
524 |
|
140175 |
v8::Local<v8::Object> value) { |
525 |
✗✓ |
140175 |
CHECK(value->IsArrayBufferView()); |
526 |
|
140175 |
Read(value.As<v8::ArrayBufferView>()); |
527 |
|
140175 |
} |
528 |
|
|
|
529 |
|
|
template <typename T, size_t S> |
530 |
|
28243 |
ArrayBufferViewContents<T, S>::ArrayBufferViewContents( |
531 |
|
28243 |
v8::Local<v8::ArrayBufferView> abv) { |
532 |
|
28243 |
Read(abv); |
533 |
|
28243 |
} |
534 |
|
|
|
535 |
|
|
template <typename T, size_t S> |
536 |
|
186154 |
void ArrayBufferViewContents<T, S>::Read(v8::Local<v8::ArrayBufferView> abv) { |
537 |
|
|
static_assert(sizeof(T) == 1, "Only supports one-byte data at the moment"); |
538 |
|
186154 |
length_ = abv->ByteLength(); |
539 |
✓✓✓✓ ✓✓ |
276893 |
if (length_ > sizeof(stack_storage_) || abv->HasBuffer()) { |
540 |
|
555087 |
data_ = static_cast<T*>(abv->Buffer()->Data()) + abv->ByteOffset(); |
541 |
|
|
} else { |
542 |
|
1125 |
abv->CopyContents(stack_storage_, sizeof(stack_storage_)); |
543 |
|
1125 |
data_ = stack_storage_; |
544 |
|
|
} |
545 |
|
186154 |
} |
546 |
|
|
|
547 |
|
|
// ECMA262 20.1.2.5 |
548 |
|
743936 |
inline bool IsSafeJsInt(v8::Local<v8::Value> v) { |
549 |
✓✓ |
743936 |
if (!v->IsNumber()) return false; |
550 |
|
522433 |
double v_d = v.As<v8::Number>()->Value(); |
551 |
✗✓ |
522433 |
if (std::isnan(v_d)) return false; |
552 |
✗✓ |
522433 |
if (std::isinf(v_d)) return false; |
553 |
✗✓ |
522433 |
if (std::trunc(v_d) != v_d) return false; // not int |
554 |
✓✗ |
522433 |
if (std::abs(v_d) <= static_cast<double>(kMaxSafeJsInteger)) return true; |
555 |
|
|
return false; |
556 |
|
|
} |
557 |
|
|
|
558 |
|
|
constexpr size_t FastStringKey::HashImpl(const char* str) { |
559 |
|
|
// Low-quality hash (djb2), but just fine for current use cases. |
560 |
|
|
size_t h = 5381; |
561 |
|
|
while (*str != '\0') { |
562 |
|
|
h = h * 33 + *(str++); // NOLINT(readability/pointer_notation) |
563 |
|
|
} |
564 |
|
|
return h; |
565 |
|
|
} |
566 |
|
|
|
567 |
|
1047107 |
constexpr size_t FastStringKey::Hash::operator()( |
568 |
|
|
const FastStringKey& key) const { |
569 |
|
1047107 |
return key.cached_hash_; |
570 |
|
|
} |
571 |
|
|
|
572 |
|
1020745 |
constexpr bool FastStringKey::operator==(const FastStringKey& other) const { |
573 |
|
1020745 |
const char* p1 = name_; |
574 |
|
1020745 |
const char* p2 = other.name_; |
575 |
✓✗ |
1020745 |
if (p1 == p2) return true; |
576 |
|
|
do { |
577 |
|
|
if (*(p1++) != *(p2++)) return false; |
578 |
|
|
} while (*p1 != '\0'); |
579 |
|
|
return *p2 == '\0'; |
580 |
|
|
} |
581 |
|
|
|
582 |
|
|
constexpr FastStringKey::FastStringKey(const char* name) |
583 |
|
|
: name_(name), cached_hash_(HashImpl(name)) {} |
584 |
|
|
|
585 |
|
22076 |
constexpr const char* FastStringKey::c_str() const { |
586 |
|
22076 |
return name_; |
587 |
|
|
} |
588 |
|
|
|
589 |
|
|
} // namespace node |
590 |
|
|
|
591 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
592 |
|
|
|
593 |
|
|
#endif // SRC_UTIL_INL_H_ |