GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
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 "util.h" |
||
30 |
|||
31 |
// These are defined by <sys/byteorder.h> or <netinet/in.h> on some systems. |
||
32 |
// To avoid warnings, undefine them before redefining them. |
||
33 |
#ifdef BSWAP_2 |
||
34 |
# undef BSWAP_2 |
||
35 |
#endif |
||
36 |
#ifdef BSWAP_4 |
||
37 |
# undef BSWAP_4 |
||
38 |
#endif |
||
39 |
#ifdef BSWAP_8 |
||
40 |
# undef BSWAP_8 |
||
41 |
#endif |
||
42 |
|||
43 |
#if defined(_MSC_VER) |
||
44 |
#include <intrin.h> |
||
45 |
#define BSWAP_2(x) _byteswap_ushort(x) |
||
46 |
#define BSWAP_4(x) _byteswap_ulong(x) |
||
47 |
#define BSWAP_8(x) _byteswap_uint64(x) |
||
48 |
#else |
||
49 |
#define BSWAP_2(x) ((x) << 8) | ((x) >> 8) |
||
50 |
#define BSWAP_4(x) \ |
||
51 |
(((x) & 0xFF) << 24) | \ |
||
52 |
(((x) & 0xFF00) << 8) | \ |
||
53 |
(((x) >> 8) & 0xFF00) | \ |
||
54 |
(((x) >> 24) & 0xFF) |
||
55 |
#define BSWAP_8(x) \ |
||
56 |
(((x) & 0xFF00000000000000ull) >> 56) | \ |
||
57 |
(((x) & 0x00FF000000000000ull) >> 40) | \ |
||
58 |
(((x) & 0x0000FF0000000000ull) >> 24) | \ |
||
59 |
(((x) & 0x000000FF00000000ull) >> 8) | \ |
||
60 |
(((x) & 0x00000000FF000000ull) << 8) | \ |
||
61 |
(((x) & 0x0000000000FF0000ull) << 24) | \ |
||
62 |
(((x) & 0x000000000000FF00ull) << 40) | \ |
||
63 |
(((x) & 0x00000000000000FFull) << 56) |
||
64 |
#endif |
||
65 |
|||
66 |
namespace node { |
||
67 |
|||
68 |
template <typename T> |
||
69 |
201762 |
ListNode<T>::ListNode() : prev_(this), next_(this) {} |
|
70 |
|||
71 |
template <typename T> |
||
72 |
200283 |
ListNode<T>::~ListNode() { |
|
73 |
200283 |
Remove(); |
|
74 |
200284 |
} |
|
75 |
|||
76 |
template <typename T> |
||
77 |
202520 |
void ListNode<T>::Remove() { |
|
78 |
202520 |
prev_->next_ = next_; |
|
79 |
202520 |
next_->prev_ = prev_; |
|
80 |
202520 |
prev_ = this; |
|
81 |
202520 |
next_ = this; |
|
82 |
202520 |
} |
|
83 |
|||
84 |
template <typename T> |
||
85 |
29298 |
bool ListNode<T>::IsEmpty() const { |
|
86 |
29298 |
return prev_ == this; |
|
87 |
} |
||
88 |
|||
89 |
template <typename T, ListNode<T> (T::*M)> |
||
90 |
37584 |
ListHead<T, M>::Iterator::Iterator(ListNode<T>* node) : node_(node) {} |
|
91 |
|||
92 |
template <typename T, ListNode<T> (T::*M)> |
||
93 |
2883 |
T* ListHead<T, M>::Iterator::operator*() const { |
|
94 |
2883 |
return ContainerOf(M, node_); |
|
95 |
} |
||
96 |
|||
97 |
template <typename T, ListNode<T> (T::*M)> |
||
98 |
const typename ListHead<T, M>::Iterator& |
||
99 |
2883 |
ListHead<T, M>::Iterator::operator++() { |
|
100 |
2883 |
node_ = node_->next_; |
|
101 |
2883 |
return *this; |
|
102 |
} |
||
103 |
|||
104 |
template <typename T, ListNode<T> (T::*M)> |
||
105 |
21676 |
bool ListHead<T, M>::Iterator::operator!=(const Iterator& that) const { |
|
106 |
21676 |
return node_ != that.node_; |
|
107 |
} |
||
108 |
|||
109 |
template <typename T, ListNode<T> (T::*M)> |
||
110 |
9553 |
ListHead<T, M>::~ListHead() { |
|
111 |
✗✓✗✓ |
19106 |
while (IsEmpty() == false) |
112 |
head_.next_->Remove(); |
||
113 |
9553 |
} |
|
114 |
|||
115 |
template <typename T, ListNode<T> (T::*M)> |
||
116 |
193603 |
void ListHead<T, M>::PushBack(T* element) { |
|
117 |
193603 |
ListNode<T>* that = &(element->*M); |
|
118 |
193603 |
head_.prev_->next_ = that; |
|
119 |
193603 |
that->prev_ = head_.prev_; |
|
120 |
193603 |
that->next_ = &head_; |
|
121 |
193603 |
head_.prev_ = that; |
|
122 |
193603 |
} |
|
123 |
|||
124 |
template <typename T, ListNode<T> (T::*M)> |
||
125 |
void ListHead<T, M>::PushFront(T* element) { |
||
126 |
ListNode<T>* that = &(element->*M); |
||
127 |
head_.next_->prev_ = that; |
||
128 |
that->prev_ = &head_; |
||
129 |
that->next_ = head_.next_; |
||
130 |
head_.next_ = that; |
||
131 |
} |
||
132 |
|||
133 |
template <typename T, ListNode<T> (T::*M)> |
||
134 |
29297 |
bool ListHead<T, M>::IsEmpty() const { |
|
135 |
29297 |
return head_.IsEmpty(); |
|
136 |
} |
||
137 |
|||
138 |
template <typename T, ListNode<T> (T::*M)> |
||
139 |
T* ListHead<T, M>::PopFront() { |
||
140 |
if (IsEmpty()) |
||
141 |
return nullptr; |
||
142 |
ListNode<T>* node = head_.next_; |
||
143 |
node->Remove(); |
||
144 |
return ContainerOf(M, node); |
||
145 |
} |
||
146 |
|||
147 |
template <typename T, ListNode<T> (T::*M)> |
||
148 |
18791 |
typename ListHead<T, M>::Iterator ListHead<T, M>::begin() const { |
|
149 |
18791 |
return Iterator(head_.next_); |
|
150 |
} |
||
151 |
|||
152 |
template <typename T, ListNode<T> (T::*M)> |
||
153 |
18792 |
typename ListHead<T, M>::Iterator ListHead<T, M>::end() const { |
|
154 |
18792 |
return Iterator(const_cast<ListNode<T>*>(&head_)); |
|
155 |
} |
||
156 |
|||
157 |
template <typename Inner, typename Outer> |
||
158 |
2536530 |
constexpr uintptr_t OffsetOf(Inner Outer::*field) { |
|
159 |
2536530 |
return reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(nullptr)->*field)); |
|
160 |
} |
||
161 |
|||
162 |
template <typename Inner, typename Outer> |
||
163 |
2491243 |
ContainerOfHelper<Inner, Outer>::ContainerOfHelper(Inner Outer::*field, |
|
164 |
Inner* pointer) |
||
165 |
: pointer_( |
||
166 |
reinterpret_cast<Outer*>( |
||
167 |
2491243 |
reinterpret_cast<uintptr_t>(pointer) - OffsetOf(field))) {} |
|
168 |
|||
169 |
template <typename Inner, typename Outer> |
||
170 |
template <typename TypeName> |
||
171 |
2491222 |
ContainerOfHelper<Inner, Outer>::operator TypeName*() const { |
|
172 |
2491222 |
return static_cast<TypeName*>(pointer_); |
|
173 |
} |
||
174 |
|||
175 |
template <typename Inner, typename Outer> |
||
176 |
2491234 |
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field, |
|
177 |
Inner* pointer) { |
||
178 |
2491234 |
return ContainerOfHelper<Inner, Outer>(field, pointer); |
|
179 |
} |
||
180 |
|||
181 |
5392928 |
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate, |
|
182 |
const char* data, |
||
183 |
int length) { |
||
184 |
return v8::String::NewFromOneByte(isolate, |
||
185 |
reinterpret_cast<const uint8_t*>(data), |
||
186 |
v8::NewStringType::kNormal, |
||
187 |
10785863 |
length).ToLocalChecked(); |
|
188 |
} |
||
189 |
|||
190 |
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate, |
||
191 |
const signed char* data, |
||
192 |
int length) { |
||
193 |
return v8::String::NewFromOneByte(isolate, |
||
194 |
reinterpret_cast<const uint8_t*>(data), |
||
195 |
v8::NewStringType::kNormal, |
||
196 |
length).ToLocalChecked(); |
||
197 |
} |
||
198 |
|||
199 |
76 |
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate, |
|
200 |
const unsigned char* data, |
||
201 |
int length) { |
||
202 |
return v8::String::NewFromOneByte( |
||
203 |
isolate, data, v8::NewStringType::kNormal, length) |
||
204 |
152 |
.ToLocalChecked(); |
|
205 |
} |
||
206 |
|||
207 |
6 |
void SwapBytes16(char* data, size_t nbytes) { |
|
208 |
✗✓ | 6 |
CHECK_EQ(nbytes % 2, 0); |
209 |
|||
210 |
#if defined(_MSC_VER) |
||
211 |
int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint16_t); |
||
212 |
if (align == 0) { |
||
213 |
// MSVC has no strict aliasing, and is able to highly optimize this case. |
||
214 |
uint16_t* data16 = reinterpret_cast<uint16_t*>(data); |
||
215 |
size_t len16 = nbytes / sizeof(*data16); |
||
216 |
for (size_t i = 0; i < len16; i++) { |
||
217 |
data16[i] = BSWAP_2(data16[i]); |
||
218 |
} |
||
219 |
return; |
||
220 |
} |
||
221 |
#endif |
||
222 |
|||
223 |
uint16_t temp; |
||
224 |
✓✓ | 2053 |
for (size_t i = 0; i < nbytes; i += sizeof(temp)) { |
225 |
2047 |
memcpy(&temp, &data[i], sizeof(temp)); |
|
226 |
2047 |
temp = BSWAP_2(temp); |
|
227 |
2047 |
memcpy(&data[i], &temp, sizeof(temp)); |
|
228 |
} |
||
229 |
6 |
} |
|
230 |
|||
231 |
2 |
void SwapBytes32(char* data, size_t nbytes) { |
|
232 |
✗✓ | 2 |
CHECK_EQ(nbytes % 4, 0); |
233 |
|||
234 |
#if defined(_MSC_VER) |
||
235 |
int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint32_t); |
||
236 |
// MSVC has no strict aliasing, and is able to highly optimize this case. |
||
237 |
if (align == 0) { |
||
238 |
uint32_t* data32 = reinterpret_cast<uint32_t*>(data); |
||
239 |
size_t len32 = nbytes / sizeof(*data32); |
||
240 |
for (size_t i = 0; i < len32; i++) { |
||
241 |
data32[i] = BSWAP_4(data32[i]); |
||
242 |
} |
||
243 |
return; |
||
244 |
} |
||
245 |
#endif |
||
246 |
|||
247 |
uint32_t temp; |
||
248 |
✓✓ | 769 |
for (size_t i = 0; i < nbytes; i += sizeof(temp)) { |
249 |
767 |
memcpy(&temp, &data[i], sizeof(temp)); |
|
250 |
767 |
temp = BSWAP_4(temp); |
|
251 |
767 |
memcpy(&data[i], &temp, sizeof(temp)); |
|
252 |
} |
||
253 |
2 |
} |
|
254 |
|||
255 |
2 |
void SwapBytes64(char* data, size_t nbytes) { |
|
256 |
✗✓ | 2 |
CHECK_EQ(nbytes % 8, 0); |
257 |
|||
258 |
#if defined(_MSC_VER) |
||
259 |
int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint64_t); |
||
260 |
if (align == 0) { |
||
261 |
// MSVC has no strict aliasing, and is able to highly optimize this case. |
||
262 |
uint64_t* data64 = reinterpret_cast<uint64_t*>(data); |
||
263 |
size_t len64 = nbytes / sizeof(*data64); |
||
264 |
for (size_t i = 0; i < len64; i++) { |
||
265 |
data64[i] = BSWAP_8(data64[i]); |
||
266 |
} |
||
267 |
return; |
||
268 |
} |
||
269 |
#endif |
||
270 |
|||
271 |
uint64_t temp; |
||
272 |
✓✓ | 513 |
for (size_t i = 0; i < nbytes; i += sizeof(temp)) { |
273 |
511 |
memcpy(&temp, &data[i], sizeof(temp)); |
|
274 |
511 |
temp = BSWAP_8(temp); |
|
275 |
511 |
memcpy(&data[i], &temp, sizeof(temp)); |
|
276 |
} |
||
277 |
2 |
} |
|
278 |
|||
279 |
12698257 |
char ToLower(char c) { |
|
280 |
✓✓✓✓ |
12698257 |
return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; |
281 |
} |
||
282 |
|||
283 |
896 |
std::string ToLower(const std::string& in) { |
|
284 |
896 |
std::string out(in.size(), 0); |
|
285 |
✓✓ | 11563 |
for (size_t i = 0; i < in.size(); ++i) |
286 |
10667 |
out[i] = ToLower(in[i]); |
|
287 |
896 |
return out; |
|
288 |
} |
||
289 |
|||
290 |
12256 |
char ToUpper(char c) { |
|
291 |
✓✓✓✗ |
12256 |
return c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c; |
292 |
} |
||
293 |
|||
294 |
std::string ToUpper(const std::string& in) { |
||
295 |
std::string out(in.size(), 0); |
||
296 |
for (size_t i = 0; i < in.size(); ++i) |
||
297 |
out[i] = ToUpper(in[i]); |
||
298 |
return out; |
||
299 |
} |
||
300 |
|||
301 |
6845132 |
bool StringEqualNoCase(const char* a, const char* b) { |
|
302 |
✓✓ | 6342658 |
do { |
303 |
✓✓ | 6845132 |
if (*a == '\0') |
304 |
502460 |
return *b == '\0'; |
|
305 |
✓✓ | 6342672 |
if (*b == '\0') |
306 |
14 |
return *a == '\0'; |
|
307 |
6342658 |
} while (ToLower(*a++) == ToLower(*b++)); |
|
308 |
4635978 |
return false; |
|
309 |
} |
||
310 |
|||
311 |
332 |
bool StringEqualNoCaseN(const char* a, const char* b, size_t length) { |
|
312 |
✓✓ | 1243 |
for (size_t i = 0; i < length; i++) { |
313 |
✓✓ | 1137 |
if (ToLower(a[i]) != ToLower(b[i])) |
314 |
226 |
return false; |
|
315 |
✗✓ | 911 |
if (a[i] == '\0') |
316 |
return true; |
||
317 |
} |
||
318 |
106 |
return true; |
|
319 |
} |
||
320 |
|||
321 |
template <typename T> |
||
322 |
3695886 |
inline T MultiplyWithOverflowCheck(T a, T b) { |
|
323 |
3695886 |
auto ret = a * b; |
|
324 |
✓✓ | 3695886 |
if (a != 0) |
325 |
✗✓ | 3695885 |
CHECK_EQ(b, ret / a); |
326 |
|||
327 |
3695886 |
return ret; |
|
328 |
} |
||
329 |
|||
330 |
// These should be used in our code as opposed to the native |
||
331 |
// versions as they abstract out some platform and or |
||
332 |
// compiler version specific functionality. |
||
333 |
// malloc(0) and realloc(ptr, 0) have implementation-defined behavior in |
||
334 |
// that the standard allows them to either return a unique pointer or a |
||
335 |
// nullptr for zero-sized allocation requests. Normalize by always using |
||
336 |
// a nullptr. |
||
337 |
template <typename T> |
||
338 |
3160493 |
T* UncheckedRealloc(T* pointer, size_t n) { |
|
339 |
3160493 |
size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n); |
|
340 |
|||
341 |
✓✓✗✓ ✗✓✗✗ |
3160497 |
if (full_size == 0) { |
342 |
102132 |
free(pointer); |
|
343 |
102132 |
return nullptr; |
|
344 |
} |
||
345 |
|||
346 |
3058365 |
void* allocated = realloc(pointer, full_size); |
|
347 |
|||
348 |
✗✓✗✓ ✗✓✗✗ |
3058365 |
if (UNLIKELY(allocated == nullptr)) { |
349 |
// Tell V8 that memory is low and retry. |
||
350 |
LowMemoryNotification(); |
||
351 |
allocated = realloc(pointer, full_size); |
||
352 |
} |
||
353 |
|||
354 |
3058365 |
return static_cast<T*>(allocated); |
|
355 |
} |
||
356 |
|||
357 |
// As per spec realloc behaves like malloc if passed nullptr. |
||
358 |
template <typename T> |
||
359 |
2214316 |
inline T* UncheckedMalloc(size_t n) { |
|
360 |
✓✓✗✓ ✗✓✗✗ |
2214316 |
if (n == 0) n = 1; |
361 |
2214316 |
return UncheckedRealloc<T>(nullptr, n); |
|
362 |
} |
||
363 |
|||
364 |
template <typename T> |
||
365 |
62586 |
inline T* UncheckedCalloc(size_t n) { |
|
366 |
✗✓ | 62586 |
if (n == 0) n = 1; |
367 |
62586 |
MultiplyWithOverflowCheck(sizeof(T), n); |
|
368 |
62586 |
return static_cast<T*>(calloc(n, sizeof(T))); |
|
369 |
} |
||
370 |
|||
371 |
template <typename T> |
||
372 |
413008 |
inline T* Realloc(T* pointer, size_t n) { |
|
373 |
413008 |
T* ret = UncheckedRealloc(pointer, n); |
|
374 |
✓✗✗✓ ✗✓✓✗ ✗✓✗✓ ✗✗✗✗ ✗✗ |
413008 |
CHECK_IMPLIES(n > 0, ret != nullptr); |
375 |
413008 |
return ret; |
|
376 |
} |
||
377 |
|||
378 |
template <typename T> |
||
379 |
114 |
inline T* Malloc(size_t n) { |
|
380 |
114 |
T* ret = UncheckedMalloc<T>(n); |
|
381 |
✓✗✗✓ ✗✓✓✗ ✗✓✗✓ ✓✗✗✓ ✗✓✓✓ ✗✓✗✓ |
114 |
CHECK_IMPLIES(n > 0, ret != nullptr); |
382 |
114 |
return ret; |
|
383 |
} |
||
384 |
|||
385 |
template <typename T> |
||
386 |
inline T* Calloc(size_t n) { |
||
387 |
T* ret = UncheckedCalloc<T>(n); |
||
388 |
CHECK_IMPLIES(n > 0, ret != nullptr); |
||
389 |
return ret; |
||
390 |
} |
||
391 |
|||
392 |
// Shortcuts for char*. |
||
393 |
12 |
inline char* Malloc(size_t n) { return Malloc<char>(n); } |
|
394 |
inline char* Calloc(size_t n) { return Calloc<char>(n); } |
||
395 |
2214177 |
inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc<char>(n); } |
|
396 |
62586 |
inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc<char>(n); } |
|
397 |
|||
398 |
// This is a helper in the .cc file so including util-inl.h doesn't include more |
||
399 |
// headers than we really need to. |
||
400 |
void ThrowErrStringTooLong(v8::Isolate* isolate); |
||
401 |
|||
402 |
2977042 |
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
|
403 |
const std::string& str, |
||
404 |
v8::Isolate* isolate) { |
||
405 |
✓✓ | 4173385 |
if (isolate == nullptr) isolate = context->GetIsolate(); |
406 |
✗✓ | 2977045 |
if (UNLIKELY(str.size() >= static_cast<size_t>(v8::String::kMaxLength))) { |
407 |
// V8 only has a TODO comment about adding an exception when the maximum |
||
408 |
// string size is exceeded. |
||
409 |
ThrowErrStringTooLong(isolate); |
||
410 |
return v8::MaybeLocal<v8::Value>(); |
||
411 |
} |
||
412 |
|||
413 |
return v8::String::NewFromUtf8( |
||
414 |
2977052 |
isolate, str.data(), v8::NewStringType::kNormal, str.size()) |
|
415 |
5954106 |
.FromMaybe(v8::Local<v8::String>()); |
|
416 |
} |
||
417 |
|||
418 |
template <typename T> |
||
419 |
166927 |
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
|
420 |
const std::vector<T>& vec, |
||
421 |
v8::Isolate* isolate) { |
||
422 |
✓✓ | 240686 |
if (isolate == nullptr) isolate = context->GetIsolate(); |
423 |
166927 |
v8::EscapableHandleScope handle_scope(isolate); |
|
424 |
|||
425 |
333854 |
MaybeStackBuffer<v8::Local<v8::Value>, 128> arr(vec.size()); |
|
426 |
166927 |
arr.SetLength(vec.size()); |
|
427 |
✓✓ | 1854470 |
for (size_t i = 0; i < vec.size(); ++i) { |
428 |
✗✓ | 3375086 |
if (!ToV8Value(context, vec[i], isolate).ToLocal(&arr[i])) |
429 |
return v8::MaybeLocal<v8::Value>(); |
||
430 |
} |
||
431 |
|||
432 |
333854 |
return handle_scope.Escape(v8::Array::New(isolate, arr.out(), arr.length())); |
|
433 |
} |
||
434 |
|||
435 |
template <typename T, typename U> |
||
436 |
5176 |
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
|
437 |
const std::unordered_map<T, U>& map, |
||
438 |
v8::Isolate* isolate) { |
||
439 |
✓✗ | 10352 |
if (isolate == nullptr) isolate = context->GetIsolate(); |
440 |
5176 |
v8::EscapableHandleScope handle_scope(isolate); |
|
441 |
|||
442 |
5176 |
v8::Local<v8::Map> ret = v8::Map::New(isolate); |
|
443 |
✓✓ | 98344 |
for (const auto& item : map) { |
444 |
v8::Local<v8::Value> first, second; |
||
445 |
✓✗✓✗ ✗✓✓✗ ✗✓ |
652176 |
if (!ToV8Value(context, item.first, isolate).ToLocal(&first) || |
446 |
✓✗ | 279504 |
!ToV8Value(context, item.second, isolate).ToLocal(&second) || |
447 |
✓✗ | 279504 |
ret->Set(context, first, second).IsEmpty()) { |
448 |
return v8::MaybeLocal<v8::Value>(); |
||
449 |
} |
||
450 |
} |
||
451 |
|||
452 |
5176 |
return handle_scope.Escape(ret); |
|
453 |
} |
||
454 |
|||
455 |
template <typename T, typename > |
||
456 |
2 |
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, |
|
457 |
const T& number, |
||
458 |
v8::Isolate* isolate) { |
||
459 |
✗✓ | 2 |
if (isolate == nullptr) isolate = context->GetIsolate(); |
460 |
|||
461 |
using Limits = std::numeric_limits<T>; |
||
462 |
// Choose Uint32, Int32, or Double depending on range checks. |
||
463 |
// These checks should all collapse at compile time. |
||
464 |
✓✗✓✗ ✓✗ |
6 |
if (static_cast<uint32_t>(Limits::max()) <= |
465 |
2 |
std::numeric_limits<uint32_t>::max() && |
|
466 |
2 |
static_cast<uint32_t>(Limits::min()) >= |
|
467 |
2 |
std::numeric_limits<uint32_t>::min() && Limits::is_exact) { |
|
468 |
4 |
return v8::Integer::NewFromUnsigned(isolate, static_cast<uint32_t>(number)); |
|
469 |
} |
||
470 |
|||
471 |
if (static_cast<int32_t>(Limits::max()) <= |
||
472 |
std::numeric_limits<int32_t>::max() && |
||
473 |
static_cast<int32_t>(Limits::min()) >= |
||
474 |
std::numeric_limits<int32_t>::min() && Limits::is_exact) { |
||
475 |
return v8::Integer::New(isolate, static_cast<int32_t>(number)); |
||
476 |
} |
||
477 |
|||
478 |
return v8::Number::New(isolate, static_cast<double>(number)); |
||
479 |
} |
||
480 |
|||
481 |
234777 |
SlicedArguments::SlicedArguments( |
|
482 |
234777 |
const v8::FunctionCallbackInfo<v8::Value>& args, size_t start) { |
|
483 |
234777 |
const size_t length = static_cast<size_t>(args.Length()); |
|
484 |
✓✓ | 469554 |
if (start >= length) return; |
485 |
234527 |
const size_t size = length - start; |
|
486 |
|||
487 |
234527 |
AllocateSufficientStorage(size); |
|
488 |
✓✓ | 680088 |
for (size_t i = 0; i < size; ++i) |
489 |
891122 |
(*this)[i] = args[i + start]; |
|
490 |
} |
||
491 |
|||
492 |
template <typename T, size_t S> |
||
493 |
854119 |
ArrayBufferViewContents<T, S>::ArrayBufferViewContents( |
|
494 |
854119 |
v8::Local<v8::Value> value) { |
|
495 |
✗✓✗✓ |
854119 |
CHECK(value->IsArrayBufferView()); |
496 |
854119 |
Read(value.As<v8::ArrayBufferView>()); |
|
497 |
854119 |
} |
|
498 |
|||
499 |
template <typename T, size_t S> |
||
500 |
535270 |
ArrayBufferViewContents<T, S>::ArrayBufferViewContents( |
|
501 |
535270 |
v8::Local<v8::Object> value) { |
|
502 |
✗✓ | 535270 |
CHECK(value->IsArrayBufferView()); |
503 |
535270 |
Read(value.As<v8::ArrayBufferView>()); |
|
504 |
535270 |
} |
|
505 |
|||
506 |
template <typename T, size_t S> |
||
507 |
146263 |
ArrayBufferViewContents<T, S>::ArrayBufferViewContents( |
|
508 |
146263 |
v8::Local<v8::ArrayBufferView> abv) { |
|
509 |
146263 |
Read(abv); |
|
510 |
146263 |
} |
|
511 |
|||
512 |
template <typename T, size_t S> |
||
513 |
1536822 |
void ArrayBufferViewContents<T, S>::Read(v8::Local<v8::ArrayBufferView> abv) { |
|
514 |
static_assert(sizeof(T) == 1, "Only supports one-byte data at the moment"); |
||
515 |
1536822 |
length_ = abv->ByteLength(); |
|
516 |
✓✓✓✓ ✓✓✓✓ ✓✓✓✓ |
2148244 |
if (length_ > sizeof(stack_storage_) || abv->HasBuffer()) { |
517 |
4598616 |
data_ = static_cast<T*>(abv->Buffer()->GetContents().Data()) + |
|
518 |
1532872 |
abv->ByteOffset(); |
|
519 |
} else { |
||
520 |
7900 |
abv->CopyContents(stack_storage_, sizeof(stack_storage_)); |
|
521 |
3950 |
data_ = stack_storage_; |
|
522 |
} |
||
523 |
1536822 |
} |
|
524 |
|||
525 |
// ECMA262 20.1.2.5 |
||
526 |
444867 |
inline bool IsSafeJsInt(v8::Local<v8::Value> v) { |
|
527 |
✓✓ | 444867 |
if (!v->IsNumber()) return false; |
528 |
614772 |
double v_d = v.As<v8::Number>()->Value(); |
|
529 |
✗✓ | 307386 |
if (std::isnan(v_d)) return false; |
530 |
✗✓ | 307386 |
if (std::isinf(v_d)) return false; |
531 |
✓✓ | 307386 |
if (std::trunc(v_d) != v_d) return false; // not int |
532 |
✓✗ | 307385 |
if (std::abs(v_d) <= static_cast<double>(kMaxSafeJsInteger)) return true; |
533 |
return false; |
||
534 |
} |
||
535 |
|||
536 |
} // namespace node |
||
537 |
|||
538 |
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
||
539 |
|||
540 |
#endif // SRC_UTIL_INL_H_ |
Generated by: GCOVR (Version 3.4) |