literal-buffer.cc 2.88 KB
Newer Older
1 2 3 4 5 6
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/parsing/literal-buffer.h"

7
#include "src/base/strings.h"
8
#include "src/execution/isolate.h"
9
#include "src/execution/local-isolate.h"
10
#include "src/heap/factory.h"
11
#include "src/utils/memcopy.h"
12 13 14 15

namespace v8 {
namespace internal {

16 17
template <typename IsolateT>
Handle<String> LiteralBuffer::Internalize(IsolateT* isolate) const {
18
  if (is_one_byte()) {
19
    return isolate->factory()->InternalizeString(one_byte_literal());
20
  }
21
  return isolate->factory()->InternalizeString(two_byte_literal());
22 23
}

24
template Handle<String> LiteralBuffer::Internalize(Isolate* isolate) const;
25
template Handle<String> LiteralBuffer::Internalize(LocalIsolate* isolate) const;
26

27 28 29 30 31 32 33
int LiteralBuffer::NewCapacity(int min_capacity) {
  return min_capacity < (kMaxGrowth / (kGrowthFactor - 1))
             ? min_capacity * kGrowthFactor
             : min_capacity + kMaxGrowth;
}

void LiteralBuffer::ExpandBuffer() {
34
  int min_capacity = std::max({kInitialCapacity, backing_store_.length()});
35 36
  base::Vector<byte> new_store =
      base::Vector<byte>::New(NewCapacity(min_capacity));
37 38 39 40 41 42 43 44 45
  if (position_ > 0) {
    MemCopy(new_store.begin(), backing_store_.begin(), position_);
  }
  backing_store_.Dispose();
  backing_store_ = new_store;
}

void LiteralBuffer::ConvertToTwoByte() {
  DCHECK(is_one_byte());
46
  base::Vector<byte> new_store;
47
  int new_content_size = position_ * base::kUC16Size;
48 49 50
  if (new_content_size >= backing_store_.length()) {
    // Ensure room for all currently read code units as UC16 as well
    // as the code unit about to be stored.
51
    new_store = base::Vector<byte>::New(NewCapacity(new_content_size));
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  } else {
    new_store = backing_store_;
  }
  uint8_t* src = backing_store_.begin();
  uint16_t* dst = reinterpret_cast<uint16_t*>(new_store.begin());
  for (int i = position_ - 1; i >= 0; i--) {
    dst[i] = src[i];
  }
  if (new_store.begin() != backing_store_.begin()) {
    backing_store_.Dispose();
    backing_store_ = new_store;
  }
  position_ = new_content_size;
  is_one_byte_ = false;
}

68
void LiteralBuffer::AddTwoByteChar(base::uc32 code_unit) {
69 70 71
  DCHECK(!is_one_byte());
  if (position_ >= backing_store_.length()) ExpandBuffer();
  if (code_unit <=
72
      static_cast<base::uc32>(unibrow::Utf16::kMaxNonSurrogateCharCode)) {
73
    *reinterpret_cast<uint16_t*>(&backing_store_[position_]) = code_unit;
74
    position_ += base::kUC16Size;
75 76 77
  } else {
    *reinterpret_cast<uint16_t*>(&backing_store_[position_]) =
        unibrow::Utf16::LeadSurrogate(code_unit);
78
    position_ += base::kUC16Size;
79 80 81
    if (position_ >= backing_store_.length()) ExpandBuffer();
    *reinterpret_cast<uint16_t*>(&backing_store_[position_]) =
        unibrow::Utf16::TrailSurrogate(code_unit);
82
    position_ += base::kUC16Size;
83 84 85 86 87
  }
}

}  // namespace internal
}  // namespace v8