v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
preparse-data-impl.h
Go to the documentation of this file.
1// Copyright 2018 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_PARSING_PREPARSE_DATA_IMPL_H_
6#define V8_PARSING_PREPARSE_DATA_IMPL_H_
7
8#include <memory>
9
12
13namespace v8 {
14namespace internal {
15
16// Classes which are internal to prepared-scope-data.cc, but are exposed in
17// a header for tests.
18
19// Wraps a ZoneVector<uint8_t> to have with functions named the same as
20// Tagged<PodArray<uint8_t>>.
22 public:
23 class Inner {
24 public:
25 Inner() = default;
26 explicit Inner(ZoneVector<uint8_t>* data) : data_(data) {}
27
28 int data_length() const { return static_cast<int>(data_->size()); }
29 uint8_t get(int index) const { return data_->at(index); }
30
31 private:
33 };
34
35 ZoneVectorWrapper() = default;
36 explicit ZoneVectorWrapper(ZoneVector<uint8_t>* data) : inner_(data) {}
37
38 const Inner* operator->() const { return &inner_; }
39
40 private:
42};
43
44template <class Data>
46 public:
48 public:
49 // Reading from the ByteData is only allowed when a ReadingScope is on the
50 // stack. This ensures that we have a DisallowGarbageCollection in place
51 // whenever ByteData holds a raw pointer into the heap.
53 public:
54 ReadingScope(ByteData* consumed_data, Data data)
55 : consumed_data_(consumed_data) {
56 consumed_data->data_ = data;
57#ifdef DEBUG
58 consumed_data->has_data_ = true;
59#endif
60 }
62 : ReadingScope(parent->scope_data_.get(), parent->GetScopeData()) {}
64#ifdef DEBUG
65 consumed_data_->has_data_ = false;
66#endif
67 }
68
69 private:
72 };
73
75 DCHECK_LE(position, data_->data_length());
77 }
78
79 size_t RemainingBytes() const {
80 DCHECK(has_data_);
81 DCHECK_LE(index_, data_->data_length());
82 return data_->data_length() - index_;
83 }
84
85 bool HasRemainingBytes(size_t bytes) const {
86 DCHECK(has_data_);
87 return index_ <= data_->data_length() && bytes <= RemainingBytes();
88 }
89
90 int32_t ReadUint32() {
91 DCHECK(has_data_);
93 // Check that there indeed is an integer following.
95 int32_t result = data_->get(index_) + (data_->get(index_ + 1) << 8) +
96 (data_->get(index_ + 2) << 16) +
97 (data_->get(index_ + 3) << 24);
98 index_ += 4;
100 return result;
101 }
102
103 int32_t ReadVarint32() {
106 int32_t value = 0;
107 bool has_another_byte;
108 unsigned shift = 0;
109 do {
110 uint8_t byte = data_->get(index_++);
111 value |= static_cast<int32_t>(byte & 0x7F) << shift;
112 shift += 7;
113 has_another_byte = byte & 0x80;
114 } while (has_another_byte);
115 DCHECK_EQ(data_->get(index_++), kVarint32EndMarker);
117 return value;
118 }
119
120 uint8_t ReadUint8() {
121 DCHECK(has_data_);
123 // Check that there indeed is a byte following.
126 return data_->get(index_++);
127 }
128
129 uint8_t ReadQuarter() {
130 DCHECK(has_data_);
131 if (stored_quarters_ == 0) {
133 // Check that there indeed are quarters following.
134 DCHECK_EQ(data_->get(index_++), kQuarterMarker);
135 stored_byte_ = data_->get(index_++);
137 }
138 // Read the first 2 bits from stored_byte_.
139 uint8_t result = (stored_byte_ >> 6) & 3;
140 DCHECK_LE(result, 3);
142 stored_byte_ <<= 2;
143 return result;
144 }
145
146 private:
148 int index_ = 0;
149 uint8_t stored_quarters_ = 0;
150 uint8_t stored_byte_ = 0;
151#ifdef DEBUG
152 bool has_data_ = false;
153#endif
154 };
155
159
160 virtual Data GetScopeData() = 0;
161
162 virtual ProducedPreparseData* GetChildData(Zone* zone, int child_index) = 0;
163
165 Zone* zone, int start_position, int* end_position, int* num_parameters,
166 int* function_length, int* num_inner_functions, bool* uses_super_property,
167 LanguageMode* language_mode) final;
168
170 AstValueFactory* ast_value_factory,
171 Zone* zone) final;
172
173#ifdef DEBUG
174 bool VerifyDataStart();
175#endif
176
177 private:
178 void RestoreDataForScope(Scope* scope, AstValueFactory* ast_value_factory,
179 Zone* zone);
182 AstValueFactory* ast_value_factory,
183 Zone* zone);
184
185 std::unique_ptr<ByteData> scope_data_;
186 // When consuming the data, these indexes point to the data we're going to
187 // consume next.
189};
190
191// Implementation of ConsumedPreparseData for on-heap data.
193 : public BaseConsumedPreparseData<Tagged<PreparseData>> {
194 public:
196
198 ProducedPreparseData* GetChildData(Zone* zone, int child_index) final;
199
200 private:
203};
204
205// A serialized PreparseData in zone memory (as apposed to being on-heap).
207 public:
210 int child_length);
211
214
217
218 int children_length() const { return static_cast<int>(children_.size()); }
219
220 ZonePreparseData* get_child(int index) { return children_[index]; }
221
222 void set_child(int index, ZonePreparseData* child) {
223 DCHECK_NOT_NULL(child);
224 children_[index] = child;
225 }
226
228
229 private:
232};
233
235 Zone* zone, int children_length) {
236 DCHECK(is_finalized_);
237 return zone->New<ZonePreparseData>(zone, &zone_byte_data_, children_length);
238}
239
240// Implementation of ConsumedPreparseData for PreparseData
241// serialized into zone memory.
243 : public BaseConsumedPreparseData<ZoneVectorWrapper> {
244 public:
246
247 ZoneVectorWrapper GetScopeData() final;
248 ProducedPreparseData* GetChildData(Zone* zone, int child_index) final;
249
250 private:
253};
254
255} // namespace internal
256} // namespace v8
257
258#endif // V8_PARSING_PREPARSE_DATA_IMPL_H_
#define DISALLOW_GARBAGE_COLLECTION(name)
union v8::internal::@341::BuiltinMetadata::KindSpecificData data
BaseConsumedPreparseData(const BaseConsumedPreparseData &)=delete
virtual ProducedPreparseData * GetChildData(Zone *zone, int child_index)=0
void RestoreDataForScope(Scope *scope, AstValueFactory *ast_value_factory, Zone *zone)
void RestoreDataForInnerScopes(Scope *scope, AstValueFactory *ast_value_factory, Zone *zone)
void RestoreScopeAllocationData(DeclarationScope *scope, AstValueFactory *ast_value_factory, Zone *zone) final
BaseConsumedPreparseData & operator=(const BaseConsumedPreparseData &)=delete
ProducedPreparseData * GetDataForSkippableFunction(Zone *zone, int start_position, int *end_position, int *num_parameters, int *function_length, int *num_inner_functions, bool *uses_super_property, LanguageMode *language_mode) final
std::unique_ptr< ByteData > scope_data_
Tagged< PreparseData > GetScopeData() final
OnHeapConsumedPreparseData(LocalIsolate *isolate, Handle< PreparseData > data)
ProducedPreparseData * GetChildData(Zone *zone, int child_index) final
ZonePreparseData * CopyToZone(Zone *zone, int children_length)
ZonePreparseData(const ZonePreparseData &)=delete
V8_EXPORT_PRIVATE ZonePreparseData(Zone *zone, base::Vector< uint8_t > *byte_data, int child_length)
void set_child(int index, ZonePreparseData *child)
ZoneVector< ZonePreparseData * > children_
ZonePreparseData & operator=(const ZonePreparseData &)=delete
ZoneVector< uint8_t > * byte_data()
Handle< PreparseData > Serialize(Isolate *isolate)
ZonePreparseData * get_child(int index)
ZoneVectorWrapper(ZoneVector< uint8_t > *data)
T * New(Args &&... args)
Definition zone.h:114
ZoneVector< RpoNumber > & result
int position
Definition liveedit.cc:290
return value
Definition map-inl.h:893
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK_NOT_NULL(val)
Definition logging.h:492
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460
static constexpr size_t kVarint32MinSize
#define V8_NODISCARD
Definition v8config.h:693