summaryrefslogtreecommitdiff
path: root/chromium/v8/src/web-snapshot/web-snapshot.h
blob: 1de4efa46b3844868342af0504ccb19440a6ae6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
// Copyright 2021 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.

#ifndef V8_WEB_SNAPSHOT_WEB_SNAPSHOT_H_
#define V8_WEB_SNAPSHOT_WEB_SNAPSHOT_H_

#include <queue>

#include "src/handles/handles.h"
#include "src/objects/bigint.h"
#include "src/objects/value-serializer.h"
#include "src/snapshot/serializer.h"  // For ObjectCacheIndexMap

namespace v8 {

class Context;
class Isolate;

template <typename T>
class Local;

namespace internal {

class Context;
class Map;
class Object;
class String;

struct WebSnapshotData : public std::enable_shared_from_this<WebSnapshotData> {
  uint8_t* buffer = nullptr;
  size_t buffer_size = 0;
  WebSnapshotData() = default;
  WebSnapshotData(const WebSnapshotData&) = delete;
  WebSnapshotData& operator=(const WebSnapshotData&) = delete;
  ~WebSnapshotData() { free(buffer); }
};

class WebSnapshotSerializerDeserializer {
 public:
  inline bool has_error() const { return error_message_ != nullptr; }
  const char* error_message() const { return error_message_; }

  enum ValueType : uint8_t {
    FALSE_CONSTANT,
    TRUE_CONSTANT,
    NULL_CONSTANT,
    UNDEFINED_CONSTANT,
    // It corresponds to the hole value.
    NO_ELEMENT_CONSTANT,
    INTEGER,
    DOUBLE,
    REGEXP,
    STRING_ID,
    ARRAY_ID,
    OBJECT_ID,
    FUNCTION_ID,
    CLASS_ID,
    SYMBOL_ID,
    EXTERNAL_ID,
    BUILTIN_OBJECT_ID,
    IN_PLACE_STRING_ID,
    ARRAY_BUFFER_ID,
    TYPED_ARRAY_ID,
    DATA_VIEW_ID,
    BIGINT_ID
  };

  enum SymbolType : uint8_t {
    kNonGlobalNoDesription = 0,
    kNonGlobal = 1,
    kGlobal = 2
  };

  enum ElementsType : uint8_t { kDense = 0, kSparse = 1 };

  enum TypedArrayType : uint8_t {
    kInt8Array,
    kUint8Array,
    kUint8ClampedArray,
    kInt16Array,
    kUint16Array,
    kInt32Array,
    kUint32Array,
    kFloat32Array,
    kFloat64Array,
    kBigInt64Array,
    kBigUint64Array,
  };

  static inline ExternalArrayType TypedArrayTypeToExternalArrayType(
      TypedArrayType type);
  static inline TypedArrayType ExternalArrayTypeToTypedArrayType(
      ExternalArrayType type);

  static constexpr uint8_t kMagicNumber[4] = {'+', '+', '+', ';'};

  enum ContextType : uint8_t { FUNCTION, BLOCK };

  enum PropertyAttributesType : uint8_t { DEFAULT, CUSTOM };

  uint8_t FunctionKindToFunctionFlags(FunctionKind kind);
  FunctionKind FunctionFlagsToFunctionKind(uint8_t flags);
  bool IsFunctionOrMethod(uint8_t flags);
  bool IsConstructor(uint8_t flags);

  uint8_t GetDefaultAttributeFlags();
  uint8_t AttributesToFlags(PropertyDetails details);
  PropertyAttributes FlagsToAttributes(uint8_t flags);

  uint8_t ArrayBufferViewKindToFlags(
      Handle<JSArrayBufferView> array_buffer_view);

  uint8_t ArrayBufferKindToFlags(Handle<JSArrayBuffer> array_buffer);

  uint32_t BigIntSignAndLengthToFlags(Handle<BigInt> bigint);
  uint32_t BigIntFlagsToBitField(uint32_t flags);
  // The maximum count of items for each value type (strings, objects etc.)
  static constexpr uint32_t kMaxItemCount =
      static_cast<uint32_t>(FixedArray::kMaxLength - 1);
  // This ensures indices and lengths can be converted between uint32_t and int
  // without problems:
  static_assert(kMaxItemCount <
                static_cast<uint32_t>(std::numeric_limits<int32_t>::max()));

 protected:
  explicit WebSnapshotSerializerDeserializer(Isolate* isolate)
      : isolate_(isolate) {}
  // Not virtual, on purpose (because it doesn't need to be).
  void Throw(const char* message);

  void IterateBuiltinObjects(
      std::function<void(Handle<String>, Handle<HeapObject>)> func);

  static constexpr int kBuiltinObjectCount = 12;

  inline Factory* factory() const { return isolate_->factory(); }

  Isolate* isolate_;
  const char* error_message_ = nullptr;

  // Encode JSArrayBufferFlags, including was_detached, is_shared, is_resizable.
  // DetachedBitField indicates whether the ArrayBuffer was detached.
  using DetachedBitField = base::BitField<bool, 0, 1, uint8_t>;
  // SharedBitField indicates whether the ArrayBuffer is SharedArrayBuffer.
  using SharedBitField = DetachedBitField::Next<bool, 1>;
  // ResizableBitField indicates whether the ArrayBuffer is ResizableArrayBuffer
  // or GrowableSharedArrayBuffer.
  using ResizableBitField = SharedBitField::Next<bool, 1>;

  // Encode JSArrayBufferViewFlags, including is_length_tracking, see
  // https://github.com/tc39/proposal-resizablearraybuffer.
  // LengthTrackingBitField indicates whether the ArrayBufferView should track
  // the length of the backing buffer, that is whether the ArrayBufferView is
  // constructed without the specified length argument.
  using LengthTrackingBitField = base::BitField<bool, 0, 1, uint8_t>;

  // Encode BigInt's sign and digits length.
  using BigIntSignBitField = base::BitField<bool, 0, 1>;
  using BigIntLengthBitField =
      BigIntSignBitField::Next<int, BigInt::kLengthFieldBits>;
  static_assert(BigIntLengthBitField::kSize == BigInt::LengthBits::kSize);

 private:
  WebSnapshotSerializerDeserializer(const WebSnapshotSerializerDeserializer&) =
      delete;
  WebSnapshotSerializerDeserializer& operator=(
      const WebSnapshotSerializerDeserializer&) = delete;

  using AsyncFunctionBitField = base::BitField<bool, 0, 1, uint8_t>;
  using GeneratorFunctionBitField = AsyncFunctionBitField::Next<bool, 1>;
  using ArrowFunctionBitField = GeneratorFunctionBitField::Next<bool, 1>;
  using MethodBitField = ArrowFunctionBitField::Next<bool, 1>;
  using StaticBitField = MethodBitField::Next<bool, 1>;
  using ClassConstructorBitField = StaticBitField::Next<bool, 1>;
  using DefaultConstructorBitField = ClassConstructorBitField::Next<bool, 1>;
  using DerivedConstructorBitField = DefaultConstructorBitField::Next<bool, 1>;

  using ReadOnlyBitField = base::BitField<bool, 0, 1, uint8_t>;
  using ConfigurableBitField = ReadOnlyBitField::Next<bool, 1>;
  using EnumerableBitField = ConfigurableBitField::Next<bool, 1>;
};

class V8_EXPORT WebSnapshotSerializer
    : public WebSnapshotSerializerDeserializer {
 public:
  explicit WebSnapshotSerializer(v8::Isolate* isolate);
  explicit WebSnapshotSerializer(Isolate* isolate);

  ~WebSnapshotSerializer();

  bool TakeSnapshot(v8::Local<v8::Context> context,
                    v8::Local<v8::PrimitiveArray> exports,
                    WebSnapshotData& data_out);
  bool TakeSnapshot(Handle<Object> object, MaybeHandle<FixedArray> block_list,
                    WebSnapshotData& data_out);

  // For inspecting the state after taking a snapshot.
  uint32_t string_count() const {
    return static_cast<uint32_t>(string_ids_.size());
  }

  uint32_t symbol_count() const {
    return static_cast<uint32_t>(symbol_ids_.size());
  }

  uint32_t bigint_count() const {
    return static_cast<uint32_t>(bigint_ids_.size());
  }

  uint32_t map_count() const { return static_cast<uint32_t>(map_ids_.size()); }

  uint32_t builtin_object_count() const {
    return static_cast<uint32_t>(builtin_object_ids_.size());
  }

  uint32_t context_count() const {
    return static_cast<uint32_t>(context_ids_.size());
  }

  uint32_t function_count() const {
    return static_cast<uint32_t>(function_ids_.size());
  }

  uint32_t class_count() const {
    return static_cast<uint32_t>(class_ids_.size());
  }

  uint32_t array_count() const {
    return static_cast<uint32_t>(array_ids_.size());
  }

  uint32_t array_buffer_count() const {
    return static_cast<uint32_t>(array_buffer_ids_.size());
  }

  uint32_t typed_array_count() const {
    return static_cast<uint32_t>(typed_array_ids_.size());
  }

  uint32_t data_view_count() const {
    return static_cast<uint32_t>(data_view_ids_.size());
  }

  uint32_t object_count() const {
    return static_cast<uint32_t>(object_ids_.size());
  }

  uint32_t external_object_count() const {
    return static_cast<uint32_t>(external_object_ids_.size());
  }

  Handle<FixedArray> GetExternals();

 private:
  WebSnapshotSerializer(const WebSnapshotSerializer&) = delete;
  WebSnapshotSerializer& operator=(const WebSnapshotSerializer&) = delete;

  enum class AllowInPlace {
    No,   // This reference cannot be replace with an in-place item.
    Yes,  // This reference can be replaced with an in-place item.
  };

  void SerializePendingItems();
  void WriteSnapshot(uint8_t*& buffer, size_t& buffer_size);
  void WriteObjects(ValueSerializer& destination, size_t count,
                    ValueSerializer& source, const char* name);

  // Returns true if the object was already in the map, false if it was added.
  bool InsertIntoIndexMap(ObjectCacheIndexMap& map, HeapObject heap_object,
                          uint32_t& id);

  void ShallowDiscoverExternals(FixedArray externals);
  void ShallowDiscoverBuiltinObjects(v8::Local<v8::Context> context);
  void Discover(Handle<HeapObject> object);
  void DiscoverString(Handle<String> string,
                      AllowInPlace can_be_in_place = AllowInPlace::No);
  void DiscoverSymbol(Handle<Symbol> symbol);
  void DiscoverBigInt(Handle<BigInt> bigint);
  void DiscoverMap(Handle<Map> map, bool allow_property_in_descriptor = false);
  void DiscoverPropertyKey(Handle<Name> key);
  void DiscoverMapForFunction(Handle<JSFunction> function);
  void DiscoverFunction(Handle<JSFunction> function);
  void DiscoverClass(Handle<JSFunction> function);
  void DiscoverContextAndPrototype(Handle<JSFunction> function);
  void DiscoverContext(Handle<Context> context);
  void DiscoverArray(Handle<JSArray> array);
  void DiscoverTypedArray(Handle<JSTypedArray> typed_array);
  void DiscoverDataView(Handle<JSDataView> data_view);
  void DiscoverArrayBuffer(Handle<JSArrayBuffer> array_buffer);
  void DiscoverElements(Handle<JSObject> object);
  void DiscoverObject(Handle<JSObject> object);
  bool DiscoverIfBuiltinObject(Handle<HeapObject> object);
  void DiscoverSource(Handle<JSFunction> function);
  template <typename T>
  void DiscoverObjectPropertiesWithDictionaryMap(T dict);
  bool ShouldBeSerialized(Handle<Name> key);
  void ConstructSource();

  void SerializeFunctionInfo(Handle<JSFunction> function,
                             ValueSerializer& serializer);
  void SerializeFunctionProperties(Handle<JSFunction> function,
                                   ValueSerializer& serializer);
  void SerializeString(Handle<String> string, ValueSerializer& serializer);
  void SerializeSymbol(Handle<Symbol> symbol);
  void SerializeBigInt(Handle<BigInt> bigint);
  void SerializeMap(Handle<Map> map);
  void SerializeBuiltinObject(uint32_t name_id);
  void SerializeObjectPrototype(Handle<Map> map, ValueSerializer& serializer);

  template <typename T>
  void SerializeObjectPropertiesWithDictionaryMap(T dict);
  void SerializeFunction(Handle<JSFunction> function);
  void SerializeClass(Handle<JSFunction> function);
  void SerializeContext(Handle<Context> context, uint32_t id);
  void SerializeArray(Handle<JSArray> array);
  void SerializeElements(Handle<JSObject> object, ValueSerializer& serializer,
                         Maybe<uint32_t> length);
  void SerializeObject(Handle<JSObject> object);
  void SerializeArrayBufferView(Handle<JSArrayBufferView> array_buffer_view,
                                ValueSerializer& serializer);
  void SerializeArrayBuffer(Handle<JSArrayBuffer> array_buffer);
  void SerializeTypedArray(Handle<JSTypedArray> typed_array);
  void SerializeDataView(Handle<JSDataView> data_view);

  void SerializeExport(Handle<Object> object, Handle<String> export_name);
  void WriteValue(Handle<Object> object, ValueSerializer& serializer);
  void WriteStringMaybeInPlace(Handle<String> string,
                               ValueSerializer& serializer);
  void WriteStringId(Handle<String> string, ValueSerializer& serializer);

  uint32_t GetStringId(Handle<String> string, bool& in_place);
  uint32_t GetSymbolId(Symbol symbol);
  uint32_t GetBigIntId(BigInt bigint);
  uint32_t GetMapId(Map map);
  uint32_t GetFunctionId(JSFunction function);
  uint32_t GetClassId(JSFunction function);
  uint32_t GetContextId(Context context);
  uint32_t GetArrayId(JSArray array);
  uint32_t GetTypedArrayId(JSTypedArray typed_array);
  uint32_t GetDataViewId(JSDataView data_view);
  uint32_t GetArrayBufferId(JSArrayBuffer array_buffer);
  uint32_t GetObjectId(JSObject object);
  bool GetExternalId(HeapObject object, uint32_t* id = nullptr);
  // Returns index into builtin_object_name_strings_.
  bool GetBuiltinObjectNameIndex(HeapObject object, uint32_t& index);
  bool GetBuiltinObjectId(HeapObject object, uint32_t& id);

  ValueSerializer string_serializer_;
  ValueSerializer symbol_serializer_;
  ValueSerializer bigint_serializer_;
  ValueSerializer map_serializer_;
  ValueSerializer builtin_object_serializer_;
  ValueSerializer context_serializer_;
  ValueSerializer function_serializer_;
  ValueSerializer class_serializer_;
  ValueSerializer array_serializer_;
  ValueSerializer typed_array_serializer_;
  ValueSerializer array_buffer_serializer_;
  ValueSerializer data_view_serializer_;
  ValueSerializer object_serializer_;
  ValueSerializer export_serializer_;

  // These are needed for being able to serialize items in order.
  Handle<ArrayList> strings_;
  Handle<ArrayList> symbols_;
  Handle<ArrayList> bigints_;
  Handle<ArrayList> maps_;
  Handle<ArrayList> contexts_;
  Handle<ArrayList> functions_;
  Handle<ArrayList> classes_;
  Handle<ArrayList> arrays_;
  Handle<ArrayList> typed_arrays_;
  Handle<ArrayList> array_buffers_;
  Handle<ArrayList> data_views_;
  Handle<ArrayList> objects_;

  // IndexMap to keep track of explicitly blocked external objects and
  // non-serializable/not-supported objects (e.g. API Objects).
  ObjectCacheIndexMap external_object_ids_;

  // ObjectCacheIndexMap implements fast lookup item -> id. Some items (context,
  // function, class, array, object) can point to other items and we serialize
  // them in the reverse order. This ensures that the items this item points to
  // have a lower ID and will be deserialized first.
  ObjectCacheIndexMap string_ids_;
  ObjectCacheIndexMap symbol_ids_;
  ObjectCacheIndexMap bigint_ids_;
  ObjectCacheIndexMap map_ids_;
  ObjectCacheIndexMap context_ids_;
  ObjectCacheIndexMap function_ids_;
  ObjectCacheIndexMap class_ids_;
  ObjectCacheIndexMap array_ids_;
  ObjectCacheIndexMap typed_array_ids_;
  ObjectCacheIndexMap array_buffer_ids_;
  ObjectCacheIndexMap data_view_ids_;
  ObjectCacheIndexMap object_ids_;
  uint32_t export_count_ = 0;

  // For handling references to builtin objects:
  // --------------------------------
  // String objects for the names of all known builtins.
  Handle<FixedArray> builtin_object_name_strings_;

  // Map object -> index in builtin_name_strings_ for all known builtins.
  ObjectCacheIndexMap builtin_object_to_name_;

  // Map object -> index in builtins_. Includes only builtins which will be
  // incluced in the snapshot.
  ObjectCacheIndexMap builtin_object_ids_;

  // For creating the Builtin wrappers in the snapshot. Includes only builtins
  // which will be incluced in the snapshot. Each element is the id of the
  // builtin name string in the snapshot.
  std::vector<uint32_t> builtin_objects_;
  // --------------------------------

  std::queue<Handle<HeapObject>> discovery_queue_;

  // For keeping track of which strings have exactly one reference. Strings are
  // inserted here when the first reference is discovered, and never removed.
  // Strings which have more than one reference get an ID and are inserted to
  // strings_.
  IdentityMap<int, base::DefaultAllocationPolicy> all_strings_;

  // For constructing the minimal, "compacted", source string to cover all
  // function bodies.
  // --------------------------------
  // Script id -> offset of the script source code in full_source_.
  std::map<int, int> script_offsets_;
  Handle<String> full_source_;
  uint32_t source_id_;
  // Ordered set of (start, end) pairs of all functions we've discovered.
  std::set<std::pair<int, int>> source_intervals_;
  // Maps function positions in the real source code into the function positions
  // in the constructed source code (which we'll include in the web snapshot).
  std::unordered_map<int, int> source_offset_to_compacted_source_offset_;
  // --------------------------------
};

class V8_EXPORT WebSnapshotDeserializer
    : public WebSnapshotSerializerDeserializer {
 public:
  WebSnapshotDeserializer(v8::Isolate* v8_isolate, const uint8_t* data,
                          size_t buffer_size);
  WebSnapshotDeserializer(Isolate* isolate, Handle<Script> snapshot_as_script);
  ~WebSnapshotDeserializer();
  bool Deserialize(MaybeHandle<FixedArray> external_references = {},
                   bool skip_exports = false);

  // For inspecting the state after deserializing a snapshot.
  uint32_t string_count() const { return string_count_; }
  uint32_t symbol_count() const { return symbol_count_; }
  uint32_t map_count() const { return map_count_; }
  uint32_t builtin_object_count() const { return builtin_object_count_; }
  uint32_t context_count() const { return context_count_; }
  uint32_t function_count() const { return function_count_; }
  uint32_t class_count() const { return class_count_; }
  uint32_t array_count() const { return array_count_; }
  uint32_t object_count() const { return object_count_; }

  static void UpdatePointersCallback(v8::Isolate* isolate, v8::GCType type,
                                     v8::GCCallbackFlags flags,
                                     void* deserializer) {
    reinterpret_cast<WebSnapshotDeserializer*>(deserializer)->UpdatePointers();
  }

  void UpdatePointers();

  MaybeHandle<Object> value() const { return return_value_; }

 private:
  enum class InternalizeStrings {
    kNo,
    kYes,
  };

  WebSnapshotDeserializer(Isolate* isolate, Handle<Object> script_name,
                          base::Vector<const uint8_t> buffer);
  // Return value: {data, length, data_owned}.
  std::tuple<const uint8_t*, uint32_t, bool> ExtractScriptBuffer(
      Isolate* isolate, Handle<Script> snapshot_as_script);
  bool DeserializeSnapshot(bool skip_exports);
  void CollectBuiltinObjects();
  bool DeserializeScript();

  WebSnapshotDeserializer(const WebSnapshotDeserializer&) = delete;
  WebSnapshotDeserializer& operator=(const WebSnapshotDeserializer&) = delete;

  void DeserializeStrings();
  void DeserializeSymbols();
  void DeserializeBigInts();
  void DeserializeMaps();
  void DeserializeBuiltinObjects();
  void DeserializeContexts();
  Handle<ScopeInfo> CreateScopeInfo(uint32_t variable_count, bool has_parent,
                                    ContextType context_type,
                                    bool has_inlined_local_names);
  Handle<JSFunction> CreateJSFunction(int index, uint32_t start,
                                      uint32_t length, uint32_t parameter_count,
                                      uint8_t flags, uint32_t context_id);
  void DeserializeFunctionData(uint32_t count, uint32_t current_count);
  void DeserializeFunctions();
  void DeserializeClasses();
  void DeserializeArrays();
  void DeserializeArrayBuffers();
  void DeserializeTypedArrays();
  void DeserializeDataViews();
  void DeserializeObjects();
  void DeserializeObjectElements(Handle<JSObject> object,
                                 bool map_from_snapshot);
  void DeserializeExports(bool skip_exports);
  void DeserializeObjectPrototype(Handle<Map> map);
  Handle<Map> DeserializeObjectPrototypeAndCreateEmptyMap();
  void DeserializeObjectPrototypeForFunction(Handle<JSFunction> function);
  void SetPrototype(Handle<Map> map, Handle<Object> prototype);
  void DeserializeFunctionProperties(Handle<JSFunction> function);
  bool ReadCount(uint32_t& count);

  bool IsInitialFunctionPrototype(Object prototype);

  template <typename T>
  void DeserializeObjectPropertiesWithDictionaryMap(
      T dict, uint32_t property_count, bool has_custom_property_attributes);

  Handle<PropertyArray> DeserializePropertyArray(
      Handle<DescriptorArray> descriptors, int no_properties);

  // Return value: (object, was_deferred)
  std::tuple<Object, bool> ReadValue(
      Handle<HeapObject> object_for_deferred_reference = Handle<HeapObject>(),
      uint32_t index_for_deferred_reference = 0,
      InternalizeStrings internalize_strings = InternalizeStrings::kNo);

  Object ReadInteger();
  Object ReadNumber();
  String ReadString(
      InternalizeStrings internalize_strings = InternalizeStrings::kNo);
  String ReadInPlaceString(
      InternalizeStrings internalize_strings = InternalizeStrings::kNo);
  Object ReadSymbol();
  Object ReadBigInt();
  std::tuple<Object, bool> ReadArray(Handle<HeapObject> container,
                                     uint32_t container_index);
  std::tuple<Object, bool> ReadArrayBuffer(Handle<HeapObject> container,
                                           uint32_t container_index);
  std::tuple<Object, bool> ReadTypedArray(Handle<HeapObject> container,
                                          uint32_t container_index);
  std::tuple<Object, bool> ReadDataView(Handle<HeapObject> container,
                                        uint32_t container_index);
  std::tuple<Object, bool> ReadObject(Handle<HeapObject> container,
                                      uint32_t container_index);
  std::tuple<Object, bool> ReadFunction(Handle<HeapObject> container,
                                        uint32_t container_index);
  std::tuple<Object, bool> ReadClass(Handle<HeapObject> container,
                                     uint32_t container_index);
  Object ReadRegexp();
  Object ReadBuiltinObjectReference();
  Object ReadExternalReference();
  bool ReadMapType();
  std::tuple<Handle<FixedArrayBase>, ElementsKind, uint32_t>
  DeserializeElements();
  ElementsType ReadElementsType();
  std::tuple<Handle<FixedArrayBase>, ElementsKind, uint32_t> ReadDenseElements(
      uint32_t length);
  std::tuple<Handle<FixedArrayBase>, ElementsKind, uint32_t> ReadSparseElements(
      uint32_t length);

  void ReadFunctionPrototype(Handle<JSFunction> function);
  bool SetFunctionPrototype(JSFunction function, JSReceiver prototype);

  HeapObject AddDeferredReference(Handle<HeapObject> container, uint32_t index,
                                  ValueType target_type,
                                  uint32_t target_object_index);
  void ProcessDeferredReferences();
  // Not virtual, on purpose (because it doesn't need to be).
  void Throw(const char* message);
  void VerifyObjects();

  Handle<FixedArray> strings_handle_;
  FixedArray strings_;

  Handle<FixedArray> symbols_handle_;
  FixedArray symbols_;

  Handle<FixedArray> bigints_handle_;
  FixedArray bigints_;

  Handle<FixedArray> builtin_objects_handle_;
  FixedArray builtin_objects_;

  Handle<FixedArray> maps_handle_;
  FixedArray maps_;
  std::map<int, Handle<Map>> deserialized_function_maps_;

  Handle<FixedArray> contexts_handle_;
  FixedArray contexts_;

  Handle<FixedArray> functions_handle_;
  FixedArray functions_;

  Handle<FixedArray> classes_handle_;
  FixedArray classes_;

  Handle<FixedArray> arrays_handle_;
  FixedArray arrays_;

  Handle<FixedArray> array_buffers_handle_;
  FixedArray array_buffers_;

  Handle<FixedArray> typed_arrays_handle_;
  FixedArray typed_arrays_;

  Handle<FixedArray> data_views_handle_;
  FixedArray data_views_;

  Handle<FixedArray> objects_handle_;
  FixedArray objects_;

  Handle<FixedArray> external_references_handle_;
  FixedArray external_references_;

  // Map: String -> builtin object.
  Handle<ObjectHashTable> builtin_object_name_to_object_;

  Handle<ArrayList> deferred_references_;

  Handle<WeakFixedArray> shared_function_infos_handle_;
  WeakFixedArray shared_function_infos_;

  Handle<ObjectHashTable> shared_function_info_table_;

  Handle<Script> script_;
  Handle<Object> script_name_;

  Handle<Object> return_value_;

  uint32_t string_count_ = 0;
  uint32_t symbol_count_ = 0;
  uint32_t bigint_count_ = 0;
  uint32_t map_count_ = 0;
  uint32_t builtin_object_count_ = 0;
  uint32_t context_count_ = 0;
  uint32_t function_count_ = 0;
  uint32_t current_function_count_ = 0;
  uint32_t class_count_ = 0;
  uint32_t current_class_count_ = 0;
  uint32_t array_count_ = 0;
  uint32_t current_array_count_ = 0;
  uint32_t array_buffer_count_ = 0;
  uint32_t current_array_buffer_count_ = 0;
  uint32_t typed_array_count_ = 0;
  uint32_t current_typed_array_count_ = 0;
  uint32_t data_view_count_ = 0;
  uint32_t current_data_view_count_ = 0;
  uint32_t object_count_ = 0;
  uint32_t current_object_count_ = 0;

  std::unique_ptr<ValueDeserializer> deserializer_;
  std::unique_ptr<const uint8_t[]> owned_data_;
  ReadOnlyRoots roots_;

  bool deserialized_ = false;
};

}  // namespace internal
}  // namespace v8

#endif  // V8_WEB_SNAPSHOT_WEB_SNAPSHOT_H_