v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
utils-inl.h
Go to the documentation of this file.
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_UTILS_INL_H_
6#define V8_UTILS_UTILS_INL_H_
7
8#include "src/utils/utils.h"
9// Include the non-inl header before the rest of the headers.
10
11#include "include/v8-platform.h"
13#include "src/base/strings.h"
14#include "src/init/v8.h"
16
17namespace v8 {
18namespace internal {
19
21 public:
22 explicit TimedScope(double* result)
23 : start_(TimestampMs()), result_(result) {}
24
25 ~TimedScope() { *result_ = TimestampMs() - start_; }
26
27 private:
28 static inline double TimestampMs() {
29 return V8::GetCurrentPlatform()->MonotonicallyIncreasingTime() *
30 static_cast<double>(base::Time::kMillisecondsPerSecond);
31 }
32
33 double start_;
34 double* result_;
35};
36
37template <typename Char>
38bool TryAddArrayIndexChar(uint32_t* index, Char c) {
40 int d = c - '0';
41 // The maximum index is 4294967294; for the computation below to not
42 // exceed that, the previous index value must be <= 429496729 if d <= 4,
43 // or <= 429496728 if d >= 5. The (d+3)>>3 computation is a branch-free
44 // way to express that.
45 if (*index > 429496729U - ((d + 3) >> 3)) return false;
46 *index = (*index) * 10 + d;
47 return true;
48}
49
50template <typename Char>
51bool TryAddIntegerIndexChar(uint64_t* index, Char c) {
53 int d = c - '0';
54 *index = (*index) * 10 + d;
55 return (*index <= kMaxSafeIntegerUint64);
56}
57
58template <typename Stream, typename index_t, enum ToIndexMode mode>
59bool StringToIndex(Stream* stream, index_t* index) {
60 uint16_t ch = stream->GetNext();
61
62 // If the string begins with a '0' character, it must only consist
63 // of it to be a legal array index.
64 if (ch == '0') {
65 *index = 0;
66 return !stream->HasMore();
67 }
68
69 // Convert string to uint32 array index; character by character.
70 if (!IsDecimalDigit(ch)) return false;
71 int d = ch - '0';
72 index_t result = d;
73 while (stream->HasMore()) {
74 // Clang on Mac doesn't think that size_t and uint*_t should be
75 // implicitly convertible.
76 base::uc32 c = stream->GetNext();
77 if (!IsDecimalDigit(c)) return false;
78 if (sizeof(result) == 8) {
80 if (!TryAddIntegerIndexChar(reinterpret_cast<uint64_t*>(&result), c)) {
81 return false;
82 }
83 } else {
84 // Either mode is fine here.
85 if (!TryAddArrayIndexChar(reinterpret_cast<uint32_t*>(&result), c))
86 return false;
87 }
88 }
89
90 *index = result;
91 return true;
92}
93
94} // namespace internal
95} // namespace v8
96
97#endif // V8_UTILS_UTILS_INL_H_
static double TimestampMs()
Definition utils-inl.h:28
TimedScope(double *result)
Definition utils-inl.h:22
T const result_
uint8_t *const start_
Definition assembler.cc:131
ZoneVector< RpoNumber > & result
uint32_t uc32
Definition strings.h:19
bool TryAddArrayIndexChar(uint32_t *index, Char c)
Definition utils-inl.h:38
constexpr uint64_t kMaxSafeIntegerUint64
Definition globals.h:1983
@ kToIntegerIndex
Definition utils.h:749
bool TryAddIntegerIndexChar(uint64_t *index, Char c)
Definition utils-inl.h:51
bool StringToIndex(Stream *stream, index_t *index)
Definition utils-inl.h:59
constexpr bool IsDecimalDigit(base::uc32 c)
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_NODISCARD
Definition v8config.h:693