Line data Source code
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 607138 : : start_(TimestampMs()), result_(result) {}
22 :
23 607463 : ~TimedScope() { *result_ = TimestampMs() - start_; }
24 :
25 : private:
26 1213888 : static inline double TimestampMs() {
27 1213888 : return V8::GetCurrentPlatform()->MonotonicallyIncreasingTime() *
28 1213155 : 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 67096500 : if (!IsDecimalDigit(c)) return false;
38 15887326 : int d = c - '0';
39 15887326 : if (*index > 429496729U - ((d + 3) >> 3)) return false;
40 15883630 : *index = (*index) * 10 + d;
41 : return true;
42 : }
43 :
44 : template <typename Stream>
45 944149 : bool StringToArrayIndex(Stream* stream, uint32_t* index) {
46 943994 : 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 944149 : if (ch == '0') {
51 107447 : *index = 0;
52 107447 : return !stream->HasMore();
53 : }
54 :
55 : // Convert string to uint32 array index; character by character.
56 836702 : if (!IsDecimalDigit(ch)) return false;
57 4285 : int d = ch - '0';
58 4285 : uint32_t result = d;
59 31600 : while (stream->HasMore()) {
60 49007 : if (!TryAddIndexChar(&result, stream->GetNext())) return false;
61 : }
62 :
63 2119 : *index = result;
64 2119 : return true;
65 : }
66 :
67 : } // namespace internal
68 : } // namespace v8
69 :
70 : #endif // V8_UTILS_INL_H_
|