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 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 * Serializes a V8 object into the buffer. | 52 * Serializes a V8 object into the buffer. |
53 */ | 53 */ |
54 Maybe<bool> WriteObject(Handle<Object> object) WARN_UNUSED_RESULT; | 54 Maybe<bool> WriteObject(Handle<Object> object) WARN_UNUSED_RESULT; |
55 | 55 |
56 /* | 56 /* |
57 * Returns the stored data. This serializer should not be used once the buffer | 57 * Returns the stored data. This serializer should not be used once the buffer |
58 * is released. The contents are undefined if a previous write has failed. | 58 * is released. The contents are undefined if a previous write has failed. |
59 */ | 59 */ |
60 std::vector<uint8_t> ReleaseBuffer() { return std::move(buffer_); } | 60 std::vector<uint8_t> ReleaseBuffer() { return std::move(buffer_); } |
61 | 61 |
| 62 /* |
| 63 * Marks an ArrayBuffer as havings its contents transferred out of band. |
| 64 * Pass the corresponding JSArrayBuffer in the deserializing context to |
| 65 * ValueDeserializer::TransferArrayBuffer. |
| 66 */ |
| 67 void TransferArrayBuffer(uint32_t transfer_id, |
| 68 Handle<JSArrayBuffer> array_buffer); |
| 69 |
62 private: | 70 private: |
63 // Writing the wire format. | 71 // Writing the wire format. |
64 void WriteTag(SerializationTag tag); | 72 void WriteTag(SerializationTag tag); |
65 template <typename T> | 73 template <typename T> |
66 void WriteVarint(T value); | 74 void WriteVarint(T value); |
67 template <typename T> | 75 template <typename T> |
68 void WriteZigZag(T value); | 76 void WriteZigZag(T value); |
69 void WriteDouble(double value); | 77 void WriteDouble(double value); |
70 void WriteOneByteString(Vector<const uint8_t> chars); | 78 void WriteOneByteString(Vector<const uint8_t> chars); |
71 void WriteTwoByteString(Vector<const uc16> chars); | 79 void WriteTwoByteString(Vector<const uc16> chars); |
(...skipping 26 matching lines...) Expand all Loading... |
98 Isolate* const isolate_; | 106 Isolate* const isolate_; |
99 std::vector<uint8_t> buffer_; | 107 std::vector<uint8_t> buffer_; |
100 Zone zone_; | 108 Zone zone_; |
101 | 109 |
102 // To avoid extra lookups in the identity map, ID+1 is actually stored in the | 110 // To avoid extra lookups in the identity map, ID+1 is actually stored in the |
103 // map (checking if the used identity is zero is the fast way of checking if | 111 // map (checking if the used identity is zero is the fast way of checking if |
104 // the entry is new). | 112 // the entry is new). |
105 IdentityMap<uint32_t> id_map_; | 113 IdentityMap<uint32_t> id_map_; |
106 uint32_t next_id_ = 0; | 114 uint32_t next_id_ = 0; |
107 | 115 |
| 116 // A similar map, for transferred array buffers. |
| 117 IdentityMap<uint32_t> array_buffer_transfer_map_; |
| 118 |
108 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); | 119 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); |
109 }; | 120 }; |
110 | 121 |
111 /* | 122 /* |
112 * Deserializes values from data written with ValueSerializer, or a compatible | 123 * Deserializes values from data written with ValueSerializer, or a compatible |
113 * implementation. | 124 * implementation. |
114 */ | 125 */ |
115 class ValueDeserializer { | 126 class ValueDeserializer { |
116 public: | 127 public: |
117 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data); | 128 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data); |
(...skipping 19 matching lines...) Expand all Loading... |
137 /* | 148 /* |
138 * Reads an object, consuming the entire buffer. | 149 * Reads an object, consuming the entire buffer. |
139 * | 150 * |
140 * This is required for the legacy "version 0" format, which did not allow | 151 * This is required for the legacy "version 0" format, which did not allow |
141 * reference deduplication, and instead relied on a "stack" model for | 152 * reference deduplication, and instead relied on a "stack" model for |
142 * deserializing, with the contents of objects and arrays provided first. | 153 * deserializing, with the contents of objects and arrays provided first. |
143 */ | 154 */ |
144 MaybeHandle<Object> ReadObjectUsingEntireBufferForLegacyFormat() | 155 MaybeHandle<Object> ReadObjectUsingEntireBufferForLegacyFormat() |
145 WARN_UNUSED_RESULT; | 156 WARN_UNUSED_RESULT; |
146 | 157 |
| 158 /* |
| 159 * Accepts the array buffer corresponding to the one passed previously to |
| 160 * ValueSerializer::TransferArrayBuffer. |
| 161 */ |
| 162 void TransferArrayBuffer(uint32_t transfer_id, |
| 163 Handle<JSArrayBuffer> array_buffer); |
| 164 |
147 private: | 165 private: |
148 // Reading the wire format. | 166 // Reading the wire format. |
149 Maybe<SerializationTag> PeekTag() const WARN_UNUSED_RESULT; | 167 Maybe<SerializationTag> PeekTag() const WARN_UNUSED_RESULT; |
150 void ConsumeTag(SerializationTag peeked_tag); | 168 void ConsumeTag(SerializationTag peeked_tag); |
151 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; | 169 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; |
152 template <typename T> | 170 template <typename T> |
153 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; | 171 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; |
154 template <typename T> | 172 template <typename T> |
155 Maybe<T> ReadZigZag() WARN_UNUSED_RESULT; | 173 Maybe<T> ReadZigZag() WARN_UNUSED_RESULT; |
156 Maybe<double> ReadDouble() WARN_UNUSED_RESULT; | 174 Maybe<double> ReadDouble() WARN_UNUSED_RESULT; |
157 Maybe<Vector<const uint8_t>> ReadRawBytes(int size) WARN_UNUSED_RESULT; | 175 Maybe<Vector<const uint8_t>> ReadRawBytes(int size) WARN_UNUSED_RESULT; |
158 | 176 |
159 // Reading V8 objects of specific kinds. | 177 // Reading V8 objects of specific kinds. |
160 // The tag is assumed to have already been read. | 178 // The tag is assumed to have already been read. |
161 MaybeHandle<String> ReadUtf8String() WARN_UNUSED_RESULT; | 179 MaybeHandle<String> ReadUtf8String() WARN_UNUSED_RESULT; |
162 MaybeHandle<String> ReadTwoByteString() WARN_UNUSED_RESULT; | 180 MaybeHandle<String> ReadTwoByteString() WARN_UNUSED_RESULT; |
163 MaybeHandle<JSObject> ReadJSObject() WARN_UNUSED_RESULT; | 181 MaybeHandle<JSObject> ReadJSObject() WARN_UNUSED_RESULT; |
164 MaybeHandle<JSArray> ReadSparseJSArray() WARN_UNUSED_RESULT; | 182 MaybeHandle<JSArray> ReadSparseJSArray() WARN_UNUSED_RESULT; |
165 MaybeHandle<JSArray> ReadDenseJSArray() WARN_UNUSED_RESULT; | 183 MaybeHandle<JSArray> ReadDenseJSArray() WARN_UNUSED_RESULT; |
166 MaybeHandle<JSDate> ReadJSDate() WARN_UNUSED_RESULT; | 184 MaybeHandle<JSDate> ReadJSDate() WARN_UNUSED_RESULT; |
167 MaybeHandle<JSValue> ReadJSValue(SerializationTag tag) WARN_UNUSED_RESULT; | 185 MaybeHandle<JSValue> ReadJSValue(SerializationTag tag) WARN_UNUSED_RESULT; |
168 MaybeHandle<JSRegExp> ReadJSRegExp() WARN_UNUSED_RESULT; | 186 MaybeHandle<JSRegExp> ReadJSRegExp() WARN_UNUSED_RESULT; |
169 MaybeHandle<JSMap> ReadJSMap() WARN_UNUSED_RESULT; | 187 MaybeHandle<JSMap> ReadJSMap() WARN_UNUSED_RESULT; |
170 MaybeHandle<JSSet> ReadJSSet() WARN_UNUSED_RESULT; | 188 MaybeHandle<JSSet> ReadJSSet() WARN_UNUSED_RESULT; |
171 MaybeHandle<JSArrayBuffer> ReadJSArrayBuffer() WARN_UNUSED_RESULT; | 189 MaybeHandle<JSArrayBuffer> ReadJSArrayBuffer() WARN_UNUSED_RESULT; |
| 190 MaybeHandle<JSArrayBuffer> ReadTransferredJSArrayBuffer() WARN_UNUSED_RESULT; |
172 | 191 |
173 /* | 192 /* |
174 * Reads key-value pairs into the object until the specified end tag is | 193 * Reads key-value pairs into the object until the specified end tag is |
175 * encountered. If successful, returns the number of properties read. | 194 * encountered. If successful, returns the number of properties read. |
176 */ | 195 */ |
177 Maybe<uint32_t> ReadJSObjectProperties(Handle<JSObject> object, | 196 Maybe<uint32_t> ReadJSObjectProperties(Handle<JSObject> object, |
178 SerializationTag end_tag); | 197 SerializationTag end_tag); |
179 | 198 |
180 // Manipulating the map from IDs to reified objects. | 199 // Manipulating the map from IDs to reified objects. |
181 bool HasObjectWithID(uint32_t id); | 200 bool HasObjectWithID(uint32_t id); |
182 MaybeHandle<JSReceiver> GetObjectWithID(uint32_t id); | 201 MaybeHandle<JSReceiver> GetObjectWithID(uint32_t id); |
183 void AddObjectWithID(uint32_t id, Handle<JSReceiver> object); | 202 void AddObjectWithID(uint32_t id, Handle<JSReceiver> object); |
184 | 203 |
185 Isolate* const isolate_; | 204 Isolate* const isolate_; |
186 const uint8_t* position_; | 205 const uint8_t* position_; |
187 const uint8_t* const end_; | 206 const uint8_t* const end_; |
188 uint32_t version_ = 0; | 207 uint32_t version_ = 0; |
189 Handle<SeededNumberDictionary> id_map_; // Always a global handle. | |
190 uint32_t next_id_ = 0; | 208 uint32_t next_id_ = 0; |
191 | 209 |
| 210 // Always global handles. |
| 211 Handle<SeededNumberDictionary> id_map_; |
| 212 MaybeHandle<SeededNumberDictionary> array_buffer_transfer_map_; |
| 213 |
192 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); | 214 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); |
193 }; | 215 }; |
194 | 216 |
195 } // namespace internal | 217 } // namespace internal |
196 } // namespace v8 | 218 } // namespace v8 |
197 | 219 |
198 #endif // V8_VALUE_SERIALIZER_H_ | 220 #endif // V8_VALUE_SERIALIZER_H_ |
OLD | NEW |