v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
literal-buffer.cc
Go to the documentation of this file.
1// Copyright 2019 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
6
7#include "src/base/strings.h"
10#include "src/heap/factory.h"
11#include "src/utils/memcopy.h"
12
13namespace v8 {
14namespace internal {
15
16template <typename IsolateT>
18 if (is_one_byte()) {
19 return isolate->factory()->InternalizeString(one_byte_literal());
20 }
21 return isolate->factory()->InternalizeString(two_byte_literal());
22}
23
25 Isolate* isolate) const;
27 LocalIsolate* isolate) const;
28
29int LiteralBuffer::NewCapacity(int min_capacity) {
30 return min_capacity < (kMaxGrowth / (kGrowthFactor - 1))
31 ? min_capacity * kGrowthFactor
32 : min_capacity + kMaxGrowth;
33}
34
36 int min_capacity = std::max({kInitialCapacity, backing_store_.length()});
37 base::Vector<uint8_t> new_store =
39 if (position_ > 0) {
40 MemCopy(new_store.begin(), backing_store_.begin(), position_);
41 }
43 backing_store_ = new_store;
44}
45
48 base::Vector<uint8_t> new_store;
49 int new_content_size = position_ * base::kUC16Size;
50 if (new_content_size >= backing_store_.length()) {
51 // Ensure room for all currently read code units as UC16 as well
52 // as the code unit about to be stored.
53 new_store = base::Vector<uint8_t>::New(NewCapacity(new_content_size));
54 } else {
55 new_store = backing_store_;
56 }
57 uint8_t* src = backing_store_.begin();
58 uint16_t* dst = reinterpret_cast<uint16_t*>(new_store.begin());
59 for (int i = position_ - 1; i >= 0; i--) {
60 dst[i] = src[i];
61 }
62 if (new_store.begin() != backing_store_.begin()) {
64 backing_store_ = new_store;
65 }
66 position_ = new_content_size;
67 is_one_byte_ = false;
68}
69
73 if (code_unit <=
75 *reinterpret_cast<uint16_t*>(&backing_store_[position_]) = code_unit;
77 } else {
78 *reinterpret_cast<uint16_t*>(&backing_store_[position_]) =
82 *reinterpret_cast<uint16_t*>(&backing_store_[position_]) =
85 }
86}
87
88} // namespace internal
89} // namespace v8
static uint16_t LeadSurrogate(uint32_t char_code)
Definition unicode.h:126
static const uchar kMaxNonSurrogateCharCode
Definition unicode.h:116
static uint16_t TrailSurrogate(uint32_t char_code)
Definition unicode.h:129
int length() const
Definition vector.h:64
void Dispose()
Definition vector.h:127
static Vector< T > New(size_t length)
Definition vector.h:35
constexpr T * begin() const
Definition vector.h:96
base::Vector< uint8_t > backing_store_
static constexpr int kGrowthFactor
V8_NOINLINE V8_PRESERVE_MOST void ExpandBuffer()
static constexpr int kMaxGrowth
base::Vector< const uint16_t > two_byte_literal() const
DirectHandle< String > Internalize(IsolateT *isolate) const
static constexpr int kInitialCapacity
void AddTwoByteChar(base::uc32 code_unit)
base::Vector< const uint8_t > one_byte_literal() const
int NewCapacity(int min_capacity)
uint32_t uc32
Definition strings.h:19
constexpr int kUC16Size
Definition strings.h:20
void MemCopy(void *dest, const void *src, size_t size)
Definition memcopy.h:124
#define DCHECK(condition)
Definition logging.h:482