| 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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 87 |
| 88 /* | 88 /* |
| 89 * Indicate whether to treat ArrayBufferView objects as host objects, | 89 * Indicate whether to treat ArrayBufferView objects as host objects, |
| 90 * i.e. pass them to Delegate::WriteHostObject. This should not be | 90 * i.e. pass them to Delegate::WriteHostObject. This should not be |
| 91 * called when no Delegate was passed. | 91 * called when no Delegate was passed. |
| 92 * | 92 * |
| 93 * The default is not to treat ArrayBufferViews as host objects. | 93 * The default is not to treat ArrayBufferViews as host objects. |
| 94 */ | 94 */ |
| 95 void SetTreatArrayBufferViewsAsHostObjects(bool mode); | 95 void SetTreatArrayBufferViewsAsHostObjects(bool mode); |
| 96 | 96 |
| 97 /* |
| 98 * If a positive values is assigned, strings of at least that length will be |
| 99 * passed to the delegate instead of serialized inline into the buffer. |
| 100 * This option is recommended only if the delegate has a more efficient means |
| 101 * of delivering those strings. By default, this feature is disabled. |
| 102 */ |
| 103 void SetMinimumLongStringLength(int length) { |
| 104 long_string_threshold_ = length; |
| 105 } |
| 106 |
| 97 private: | 107 private: |
| 98 // Managing allocations of the internal buffer. | 108 // Managing allocations of the internal buffer. |
| 99 Maybe<bool> ExpandBuffer(size_t required_capacity); | 109 Maybe<bool> ExpandBuffer(size_t required_capacity); |
| 100 | 110 |
| 101 // Writing the wire format. | 111 // Writing the wire format. |
| 102 void WriteTag(SerializationTag tag); | 112 void WriteTag(SerializationTag tag); |
| 103 template <typename T> | 113 template <typename T> |
| 104 void WriteVarint(T value); | 114 void WriteVarint(T value); |
| 105 template <typename T> | 115 template <typename T> |
| 106 void WriteZigZag(T value); | 116 void WriteZigZag(T value); |
| 107 void WriteOneByteString(Vector<const uint8_t> chars); | 117 void WriteOneByteString(Vector<const uint8_t> chars); |
| 108 void WriteTwoByteString(Vector<const uc16> chars); | 118 void WriteTwoByteString(Vector<const uc16> chars); |
| 109 Maybe<uint8_t*> ReserveRawBytes(size_t bytes); | 119 Maybe<uint8_t*> ReserveRawBytes(size_t bytes); |
| 110 | 120 |
| 111 // Writing V8 objects of various kinds. | 121 // Writing V8 objects of various kinds. |
| 112 void WriteOddball(Oddball* oddball); | 122 void WriteOddball(Oddball* oddball); |
| 113 void WriteSmi(Smi* smi); | 123 void WriteSmi(Smi* smi); |
| 114 void WriteHeapNumber(HeapNumber* number); | 124 void WriteHeapNumber(HeapNumber* number); |
| 115 void WriteString(Handle<String> string); | 125 Maybe<bool> WriteString(Handle<String> string); |
| 116 Maybe<bool> WriteJSReceiver(Handle<JSReceiver> receiver) WARN_UNUSED_RESULT; | 126 Maybe<bool> WriteJSReceiver(Handle<JSReceiver> receiver) WARN_UNUSED_RESULT; |
| 117 Maybe<bool> WriteJSObject(Handle<JSObject> object) WARN_UNUSED_RESULT; | 127 Maybe<bool> WriteJSObject(Handle<JSObject> object) WARN_UNUSED_RESULT; |
| 118 Maybe<bool> WriteJSObjectSlow(Handle<JSObject> object) WARN_UNUSED_RESULT; | 128 Maybe<bool> WriteJSObjectSlow(Handle<JSObject> object) WARN_UNUSED_RESULT; |
| 119 Maybe<bool> WriteJSArray(Handle<JSArray> array) WARN_UNUSED_RESULT; | 129 Maybe<bool> WriteJSArray(Handle<JSArray> array) WARN_UNUSED_RESULT; |
| 120 void WriteJSDate(JSDate* date); | 130 void WriteJSDate(JSDate* date); |
| 121 Maybe<bool> WriteJSValue(Handle<JSValue> value) WARN_UNUSED_RESULT; | 131 Maybe<bool> WriteJSValue(Handle<JSValue> value) WARN_UNUSED_RESULT; |
| 122 void WriteJSRegExp(JSRegExp* regexp); | 132 void WriteJSRegExp(JSRegExp* regexp); |
| 123 Maybe<bool> WriteJSMap(Handle<JSMap> map) WARN_UNUSED_RESULT; | 133 Maybe<bool> WriteJSMap(Handle<JSMap> map) WARN_UNUSED_RESULT; |
| 124 Maybe<bool> WriteJSSet(Handle<JSSet> map) WARN_UNUSED_RESULT; | 134 Maybe<bool> WriteJSSet(Handle<JSSet> map) WARN_UNUSED_RESULT; |
| 125 Maybe<bool> WriteJSArrayBuffer(Handle<JSArrayBuffer> array_buffer) | 135 Maybe<bool> WriteJSArrayBuffer(Handle<JSArrayBuffer> array_buffer) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 | 167 |
| 158 // To avoid extra lookups in the identity map, ID+1 is actually stored in the | 168 // To avoid extra lookups in the identity map, ID+1 is actually stored in the |
| 159 // map (checking if the used identity is zero is the fast way of checking if | 169 // map (checking if the used identity is zero is the fast way of checking if |
| 160 // the entry is new). | 170 // the entry is new). |
| 161 IdentityMap<uint32_t, ZoneAllocationPolicy> id_map_; | 171 IdentityMap<uint32_t, ZoneAllocationPolicy> id_map_; |
| 162 uint32_t next_id_ = 0; | 172 uint32_t next_id_ = 0; |
| 163 | 173 |
| 164 // A similar map, for transferred array buffers. | 174 // A similar map, for transferred array buffers. |
| 165 IdentityMap<uint32_t, ZoneAllocationPolicy> array_buffer_transfer_map_; | 175 IdentityMap<uint32_t, ZoneAllocationPolicy> array_buffer_transfer_map_; |
| 166 | 176 |
| 177 // Used for tracking long strings. |
| 178 int long_string_threshold_ = -1; |
| 179 IdentityMap<uint32_t, ZoneAllocationPolicy> long_string_map_; |
| 180 |
| 167 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); | 181 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); |
| 168 }; | 182 }; |
| 169 | 183 |
| 170 /* | 184 /* |
| 171 * Deserializes values from data written with ValueSerializer, or a compatible | 185 * Deserializes values from data written with ValueSerializer, or a compatible |
| 172 * implementation. | 186 * implementation. |
| 173 */ | 187 */ |
| 174 class ValueDeserializer { | 188 class ValueDeserializer { |
| 175 public: | 189 public: |
| 176 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data, | 190 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 205 WARN_UNUSED_RESULT; | 219 WARN_UNUSED_RESULT; |
| 206 | 220 |
| 207 /* | 221 /* |
| 208 * Accepts the array buffer corresponding to the one passed previously to | 222 * Accepts the array buffer corresponding to the one passed previously to |
| 209 * ValueSerializer::TransferArrayBuffer. | 223 * ValueSerializer::TransferArrayBuffer. |
| 210 */ | 224 */ |
| 211 void TransferArrayBuffer(uint32_t transfer_id, | 225 void TransferArrayBuffer(uint32_t transfer_id, |
| 212 Handle<JSArrayBuffer> array_buffer); | 226 Handle<JSArrayBuffer> array_buffer); |
| 213 | 227 |
| 214 /* | 228 /* |
| 229 * Accepts a long string corresponding to one passed previously to |
| 230 * ValueSerializer::Delegate::GetLongStringId. |
| 231 */ |
| 232 void TransferLongString(uint32_t transfer_id, Handle<String> string); |
| 233 |
| 234 /* |
| 215 * Publicly exposed wire format writing methods. | 235 * Publicly exposed wire format writing methods. |
| 216 * These are intended for use within the delegate's WriteHostObject method. | 236 * These are intended for use within the delegate's WriteHostObject method. |
| 217 */ | 237 */ |
| 218 bool ReadUint32(uint32_t* value) WARN_UNUSED_RESULT; | 238 bool ReadUint32(uint32_t* value) WARN_UNUSED_RESULT; |
| 219 bool ReadUint64(uint64_t* value) WARN_UNUSED_RESULT; | 239 bool ReadUint64(uint64_t* value) WARN_UNUSED_RESULT; |
| 220 bool ReadDouble(double* value) WARN_UNUSED_RESULT; | 240 bool ReadDouble(double* value) WARN_UNUSED_RESULT; |
| 221 bool ReadRawBytes(size_t length, const void** data) WARN_UNUSED_RESULT; | 241 bool ReadRawBytes(size_t length, const void** data) WARN_UNUSED_RESULT; |
| 222 void set_expect_inline_wasm(bool expect_inline_wasm) { | 242 void set_expect_inline_wasm(bool expect_inline_wasm) { |
| 223 expect_inline_wasm_ = expect_inline_wasm; | 243 expect_inline_wasm_ = expect_inline_wasm; |
| 224 } | 244 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 247 // Reads a string intended to be part of a more complicated object. | 267 // Reads a string intended to be part of a more complicated object. |
| 248 // Before v12, these are UTF-8 strings. After, they can be any encoding | 268 // Before v12, these are UTF-8 strings. After, they can be any encoding |
| 249 // permissible for a string (with the relevant tag). | 269 // permissible for a string (with the relevant tag). |
| 250 MaybeHandle<String> ReadString() WARN_UNUSED_RESULT; | 270 MaybeHandle<String> ReadString() WARN_UNUSED_RESULT; |
| 251 | 271 |
| 252 // Reading V8 objects of specific kinds. | 272 // Reading V8 objects of specific kinds. |
| 253 // The tag is assumed to have already been read. | 273 // The tag is assumed to have already been read. |
| 254 MaybeHandle<String> ReadUtf8String() WARN_UNUSED_RESULT; | 274 MaybeHandle<String> ReadUtf8String() WARN_UNUSED_RESULT; |
| 255 MaybeHandle<String> ReadOneByteString() WARN_UNUSED_RESULT; | 275 MaybeHandle<String> ReadOneByteString() WARN_UNUSED_RESULT; |
| 256 MaybeHandle<String> ReadTwoByteString() WARN_UNUSED_RESULT; | 276 MaybeHandle<String> ReadTwoByteString() WARN_UNUSED_RESULT; |
| 277 MaybeHandle<String> ReadLongString() WARN_UNUSED_RESULT; |
| 257 MaybeHandle<JSObject> ReadJSObject() WARN_UNUSED_RESULT; | 278 MaybeHandle<JSObject> ReadJSObject() WARN_UNUSED_RESULT; |
| 258 MaybeHandle<JSArray> ReadSparseJSArray() WARN_UNUSED_RESULT; | 279 MaybeHandle<JSArray> ReadSparseJSArray() WARN_UNUSED_RESULT; |
| 259 MaybeHandle<JSArray> ReadDenseJSArray() WARN_UNUSED_RESULT; | 280 MaybeHandle<JSArray> ReadDenseJSArray() WARN_UNUSED_RESULT; |
| 260 MaybeHandle<JSDate> ReadJSDate() WARN_UNUSED_RESULT; | 281 MaybeHandle<JSDate> ReadJSDate() WARN_UNUSED_RESULT; |
| 261 MaybeHandle<JSValue> ReadJSValue(SerializationTag tag) WARN_UNUSED_RESULT; | 282 MaybeHandle<JSValue> ReadJSValue(SerializationTag tag) WARN_UNUSED_RESULT; |
| 262 MaybeHandle<JSRegExp> ReadJSRegExp() WARN_UNUSED_RESULT; | 283 MaybeHandle<JSRegExp> ReadJSRegExp() WARN_UNUSED_RESULT; |
| 263 MaybeHandle<JSMap> ReadJSMap() WARN_UNUSED_RESULT; | 284 MaybeHandle<JSMap> ReadJSMap() WARN_UNUSED_RESULT; |
| 264 MaybeHandle<JSSet> ReadJSSet() WARN_UNUSED_RESULT; | 285 MaybeHandle<JSSet> ReadJSSet() WARN_UNUSED_RESULT; |
| 265 MaybeHandle<JSArrayBuffer> ReadJSArrayBuffer() WARN_UNUSED_RESULT; | 286 MaybeHandle<JSArrayBuffer> ReadJSArrayBuffer() WARN_UNUSED_RESULT; |
| 266 MaybeHandle<JSArrayBuffer> ReadTransferredJSArrayBuffer(bool is_shared) | 287 MaybeHandle<JSArrayBuffer> ReadTransferredJSArrayBuffer(bool is_shared) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 289 const uint8_t* position_; | 310 const uint8_t* position_; |
| 290 const uint8_t* const end_; | 311 const uint8_t* const end_; |
| 291 PretenureFlag pretenure_; | 312 PretenureFlag pretenure_; |
| 292 uint32_t version_ = 0; | 313 uint32_t version_ = 0; |
| 293 uint32_t next_id_ = 0; | 314 uint32_t next_id_ = 0; |
| 294 bool expect_inline_wasm_ = false; | 315 bool expect_inline_wasm_ = false; |
| 295 | 316 |
| 296 // Always global handles. | 317 // Always global handles. |
| 297 Handle<FixedArray> id_map_; | 318 Handle<FixedArray> id_map_; |
| 298 MaybeHandle<SeededNumberDictionary> array_buffer_transfer_map_; | 319 MaybeHandle<SeededNumberDictionary> array_buffer_transfer_map_; |
| 320 MaybeHandle<SeededNumberDictionary> long_string_map_; |
| 299 | 321 |
| 300 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); | 322 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); |
| 301 }; | 323 }; |
| 302 | 324 |
| 303 } // namespace internal | 325 } // namespace internal |
| 304 } // namespace v8 | 326 } // namespace v8 |
| 305 | 327 |
| 306 #endif // V8_VALUE_SERIALIZER_H_ | 328 #endif // V8_VALUE_SERIALIZER_H_ |
| OLD | NEW |