OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_VALUE_SERIALIZER_H_ | 5 #ifndef V8_VALUE_SERIALIZER_H_ |
6 #define V8_VALUE_SERIALIZER_H_ | 6 #define V8_VALUE_SERIALIZER_H_ |
7 | 7 |
8 #include <cstdint> | 8 #include <cstdint> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "include/v8.h" | 11 #include "include/v8.h" |
12 #include "src/base/compiler-specific.h" | 12 #include "src/base/compiler-specific.h" |
13 #include "src/base/macros.h" | 13 #include "src/base/macros.h" |
| 14 #include "src/identity-map.h" |
14 #include "src/vector.h" | 15 #include "src/vector.h" |
| 16 #include "src/zone.h" |
15 | 17 |
16 namespace v8 { | 18 namespace v8 { |
17 namespace internal { | 19 namespace internal { |
18 | 20 |
19 class HeapNumber; | 21 class HeapNumber; |
20 class Isolate; | 22 class Isolate; |
21 class Object; | 23 class Object; |
22 class Oddball; | 24 class Oddball; |
23 class Smi; | 25 class Smi; |
24 | 26 |
25 enum class SerializationTag : uint8_t; | 27 enum class SerializationTag : uint8_t; |
26 | 28 |
27 /** | 29 /** |
28 * Writes V8 objects in a binary format that allows the objects to be cloned | 30 * Writes V8 objects in a binary format that allows the objects to be cloned |
29 * according to the HTML structured clone algorithm. | 31 * according to the HTML structured clone algorithm. |
30 * | 32 * |
31 * Format is based on Blink's previous serialization logic. | 33 * Format is based on Blink's previous serialization logic. |
32 */ | 34 */ |
33 class ValueSerializer { | 35 class ValueSerializer { |
34 public: | 36 public: |
35 ValueSerializer(); | 37 explicit ValueSerializer(Isolate* isolate); |
36 ~ValueSerializer(); | 38 ~ValueSerializer(); |
37 | 39 |
38 /* | 40 /* |
39 * Writes out a header, which includes the format version. | 41 * Writes out a header, which includes the format version. |
40 */ | 42 */ |
41 void WriteHeader(); | 43 void WriteHeader(); |
42 | 44 |
43 /* | 45 /* |
44 * Serializes a V8 object into the buffer. | 46 * Serializes a V8 object into the buffer. |
45 */ | 47 */ |
(...skipping 15 matching lines...) Expand all Loading... |
61 void WriteDouble(double value); | 63 void WriteDouble(double value); |
62 void WriteOneByteString(Vector<const uint8_t> chars); | 64 void WriteOneByteString(Vector<const uint8_t> chars); |
63 void WriteTwoByteString(Vector<const uc16> chars); | 65 void WriteTwoByteString(Vector<const uc16> chars); |
64 uint8_t* ReserveRawBytes(size_t bytes); | 66 uint8_t* ReserveRawBytes(size_t bytes); |
65 | 67 |
66 // Writing V8 objects of various kinds. | 68 // Writing V8 objects of various kinds. |
67 void WriteOddball(Oddball* oddball); | 69 void WriteOddball(Oddball* oddball); |
68 void WriteSmi(Smi* smi); | 70 void WriteSmi(Smi* smi); |
69 void WriteHeapNumber(HeapNumber* number); | 71 void WriteHeapNumber(HeapNumber* number); |
70 void WriteString(Handle<String> string); | 72 void WriteString(Handle<String> string); |
| 73 Maybe<bool> WriteJSReceiver(Handle<JSReceiver> receiver) WARN_UNUSED_RESULT; |
| 74 Maybe<bool> WriteJSObject(Handle<JSObject> object) WARN_UNUSED_RESULT; |
71 | 75 |
| 76 /* |
| 77 * Reads the specified keys from the object and writes key-value pairs to the |
| 78 * buffer. |
| 79 */ |
| 80 Maybe<bool> WriteJSObjectProperties( |
| 81 Handle<JSObject> object, Handle<FixedArray> keys) WARN_UNUSED_RESULT; |
| 82 |
| 83 Isolate* const isolate_; |
72 std::vector<uint8_t> buffer_; | 84 std::vector<uint8_t> buffer_; |
| 85 Zone zone_; |
| 86 |
| 87 // To avoid extra lookups in the identity map, ID+1 is actually stored in the |
| 88 // map (checking if the used identity is zero is the fast way of checking if |
| 89 // the entry is new). |
| 90 IdentityMap<uint32_t> id_map_; |
| 91 uint32_t next_id_ = 0; |
73 | 92 |
74 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); | 93 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); |
75 }; | 94 }; |
76 | 95 |
77 /* | 96 /* |
78 * Deserializes values from data written with ValueSerializer, or a compatible | 97 * Deserializes values from data written with ValueSerializer, or a compatible |
79 * implementation. | 98 * implementation. |
80 */ | 99 */ |
81 class ValueDeserializer { | 100 class ValueDeserializer { |
82 public: | 101 public: |
83 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data); | 102 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data); |
84 ~ValueDeserializer(); | 103 ~ValueDeserializer(); |
85 | 104 |
86 /* | 105 /* |
87 * Runs version detection logic, which may fail if the format is invalid. | 106 * Runs version detection logic, which may fail if the format is invalid. |
88 */ | 107 */ |
89 Maybe<bool> ReadHeader() WARN_UNUSED_RESULT; | 108 Maybe<bool> ReadHeader() WARN_UNUSED_RESULT; |
90 | 109 |
91 /* | 110 /* |
92 * Deserializes a V8 object from the buffer. | 111 * Deserializes a V8 object from the buffer. |
93 */ | 112 */ |
94 MaybeHandle<Object> ReadObject() WARN_UNUSED_RESULT; | 113 MaybeHandle<Object> ReadObject() WARN_UNUSED_RESULT; |
95 | 114 |
96 private: | 115 private: |
97 // Reading the wire format. | 116 // Reading the wire format. |
| 117 Maybe<SerializationTag> PeekTag() const WARN_UNUSED_RESULT; |
98 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; | 118 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; |
99 template <typename T> | 119 template <typename T> |
100 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; | 120 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; |
101 template <typename T> | 121 template <typename T> |
102 Maybe<T> ReadZigZag() WARN_UNUSED_RESULT; | 122 Maybe<T> ReadZigZag() WARN_UNUSED_RESULT; |
103 Maybe<double> ReadDouble() WARN_UNUSED_RESULT; | 123 Maybe<double> ReadDouble() WARN_UNUSED_RESULT; |
104 Maybe<Vector<const uint8_t>> ReadRawBytes(int size) WARN_UNUSED_RESULT; | 124 Maybe<Vector<const uint8_t>> ReadRawBytes(int size) WARN_UNUSED_RESULT; |
105 | 125 |
106 // Reading V8 objects of specific kinds. | 126 // Reading V8 objects of specific kinds. |
107 // The tag is assumed to have already been read. | 127 // The tag is assumed to have already been read. |
108 MaybeHandle<String> ReadUtf8String() WARN_UNUSED_RESULT; | 128 MaybeHandle<String> ReadUtf8String() WARN_UNUSED_RESULT; |
109 MaybeHandle<String> ReadTwoByteString() WARN_UNUSED_RESULT; | 129 MaybeHandle<String> ReadTwoByteString() WARN_UNUSED_RESULT; |
| 130 MaybeHandle<JSObject> ReadJSObject() WARN_UNUSED_RESULT; |
| 131 |
| 132 /* |
| 133 * Includes common logic for reading a receiver, which may be complex and may |
| 134 * recurse. Accepts a functor which does object-specific decoding given the ID |
| 135 * that the object should be stored with. The functor must store the object |
| 136 * before any additional references to it could be encountered. |
| 137 */ |
| 138 template <typename T, typename Functor> |
| 139 MaybeHandle<T> ReadJSReceiver(const Functor& functor) WARN_UNUSED_RESULT; |
| 140 |
| 141 /* |
| 142 * Reads key-value pairs into the object until the specified end tag is |
| 143 * encountered. If successful, returns the number of properties read. |
| 144 */ |
| 145 Maybe<uint32_t> ReadJSObjectProperties(Handle<JSObject> object, |
| 146 SerializationTag end_tag); |
| 147 |
| 148 // Manipulating the map from IDs to reified objects. |
| 149 bool HasObjectWithID(uint32_t id); |
| 150 MaybeHandle<JSReceiver> GetObjectWithID(uint32_t id); |
| 151 void AddObjectWithID(uint32_t id, Handle<JSReceiver> object); |
110 | 152 |
111 Isolate* const isolate_; | 153 Isolate* const isolate_; |
112 const uint8_t* position_; | 154 const uint8_t* position_; |
113 const uint8_t* const end_; | 155 const uint8_t* const end_; |
114 uint32_t version_ = 0; | 156 uint32_t version_ = 0; |
| 157 Handle<SeededNumberDictionary> id_map_; // always a global handle |
| 158 uint32_t next_id_ = 0; |
115 | 159 |
116 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); | 160 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); |
117 }; | 161 }; |
118 | 162 |
119 } // namespace internal | 163 } // namespace internal |
120 } // namespace v8 | 164 } // namespace v8 |
121 | 165 |
122 #endif // V8_VALUE_SERIALIZER_H_ | 166 #endif // V8_VALUE_SERIALIZER_H_ |
OLD | NEW |