Home | History | Annotate | Download | only in src
      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 Stream>
     36 bool StringToArrayIndex(Stream* stream, uint32_t* index) {
     37   uint16_t ch = stream->GetNext();
     38 
     39   // If the string begins with a '0' character, it must only consist
     40   // of it to be a legal array index.
     41   if (ch == '0') {
     42     *index = 0;
     43     return !stream->HasMore();
     44   }
     45 
     46   // Convert string to uint32 array index; character by character.
     47   if (!IsDecimalDigit(ch)) return false;
     48   int d = ch - '0';
     49   uint32_t result = d;
     50   while (stream->HasMore()) {
     51     ch = stream->GetNext();
     52     if (!IsDecimalDigit(ch)) return false;
     53     d = ch - '0';
     54     // Check that the new result is below the 32 bit limit.
     55     if (result > 429496729U - ((d + 3) >> 3)) return false;
     56     result = (result * 10) + d;
     57   }
     58 
     59   *index = result;
     60   return true;
     61 }
     62 
     63 }  // namespace internal
     64 }  // namespace v8
     65 
     66 #endif  // V8_UTILS_INL_H_
     67