1 | // Copyright 2016 the V8 project authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | #ifndef V8_UTILS_INL_H_ |
6 | #define V8_UTILS_INL_H_ |
7 | |
8 | #include "src/utils.h" |
9 | |
10 | #include "include/v8-platform.h" |
11 | #include "src/base/platform/time.h" |
12 | #include "src/char-predicates-inl.h" |
13 | #include "src/v8.h" |
14 | |
15 | namespace v8 { |
16 | namespace internal { |
17 | |
18 | class TimedScope { |
19 | public: |
20 | explicit TimedScope(double* result) |
21 | : start_(TimestampMs()), result_(result) {} |
22 | |
23 | ~TimedScope() { *result_ = TimestampMs() - start_; } |
24 | |
25 | private: |
26 | static inline double TimestampMs() { |
27 | return V8::GetCurrentPlatform()->MonotonicallyIncreasingTime() * |
28 | static_cast<double>(base::Time::kMillisecondsPerSecond); |
29 | } |
30 | |
31 | double start_; |
32 | double* result_; |
33 | }; |
34 | |
35 | template <typename Char> |
36 | bool TryAddIndexChar(uint32_t* index, Char c) { |
37 | if (!IsDecimalDigit(c)) return false; |
38 | int d = c - '0'; |
39 | if (*index > 429496729U - ((d + 3) >> 3)) return false; |
40 | *index = (*index) * 10 + d; |
41 | return true; |
42 | } |
43 | |
44 | template <typename Stream> |
45 | bool StringToArrayIndex(Stream* stream, uint32_t* index) { |
46 | uint16_t ch = stream->GetNext(); |
47 | |
48 | // If the string begins with a '0' character, it must only consist |
49 | // of it to be a legal array index. |
50 | if (ch == '0') { |
51 | *index = 0; |
52 | return !stream->HasMore(); |
53 | } |
54 | |
55 | // Convert string to uint32 array index; character by character. |
56 | if (!IsDecimalDigit(ch)) return false; |
57 | int d = ch - '0'; |
58 | uint32_t result = d; |
59 | while (stream->HasMore()) { |
60 | if (!TryAddIndexChar(&result, stream->GetNext())) return false; |
61 | } |
62 | |
63 | *index = result; |
64 | return true; |
65 | } |
66 | |
67 | } // namespace internal |
68 | } // namespace v8 |
69 | |
70 | #endif // V8_UTILS_INL_H_ |
71 | |