| 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. Returns the number of keys actually written, which may be smaller |
| 79 * if some keys are not own properties when accessed. |
| 80 */ |
| 81 Maybe<uint32_t> WriteJSObjectProperties( |
| 82 Handle<JSObject> object, Handle<FixedArray> keys) WARN_UNUSED_RESULT; |
| 83 |
| 84 Isolate* const isolate_; |
| 72 std::vector<uint8_t> buffer_; | 85 std::vector<uint8_t> buffer_; |
| 86 Zone zone_; |
| 87 |
| 88 // To avoid extra lookups in the identity map, ID+1 is actually stored in the |
| 89 // map (checking if the used identity is zero is the fast way of checking if |
| 90 // the entry is new). |
| 91 IdentityMap<uint32_t> id_map_; |
| 92 uint32_t next_id_ = 0; |
| 73 | 93 |
| 74 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); | 94 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); |
| 75 }; | 95 }; |
| 76 | 96 |
| 77 /* | 97 /* |
| 78 * Deserializes values from data written with ValueSerializer, or a compatible | 98 * Deserializes values from data written with ValueSerializer, or a compatible |
| 79 * implementation. | 99 * implementation. |
| 80 */ | 100 */ |
| 81 class ValueDeserializer { | 101 class ValueDeserializer { |
| 82 public: | 102 public: |
| 83 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data); | 103 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data); |
| 84 ~ValueDeserializer(); | 104 ~ValueDeserializer(); |
| 85 | 105 |
| 86 /* | 106 /* |
| 87 * Runs version detection logic, which may fail if the format is invalid. | 107 * Runs version detection logic, which may fail if the format is invalid. |
| 88 */ | 108 */ |
| 89 Maybe<bool> ReadHeader() WARN_UNUSED_RESULT; | 109 Maybe<bool> ReadHeader() WARN_UNUSED_RESULT; |
| 90 | 110 |
| 91 /* | 111 /* |
| 92 * Deserializes a V8 object from the buffer. | 112 * Deserializes a V8 object from the buffer. |
| 93 */ | 113 */ |
| 94 MaybeHandle<Object> ReadObject() WARN_UNUSED_RESULT; | 114 MaybeHandle<Object> ReadObject() WARN_UNUSED_RESULT; |
| 95 | 115 |
| 96 private: | 116 private: |
| 97 // Reading the wire format. | 117 // Reading the wire format. |
| 118 Maybe<SerializationTag> PeekTag() const WARN_UNUSED_RESULT; |
| 98 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; | 119 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; |
| 99 template <typename T> | 120 template <typename T> |
| 100 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; | 121 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; |
| 101 template <typename T> | 122 template <typename T> |
| 102 Maybe<T> ReadZigZag() WARN_UNUSED_RESULT; | 123 Maybe<T> ReadZigZag() WARN_UNUSED_RESULT; |
| 103 Maybe<double> ReadDouble() WARN_UNUSED_RESULT; | 124 Maybe<double> ReadDouble() WARN_UNUSED_RESULT; |
| 104 Maybe<Vector<const uint8_t>> ReadRawBytes(int size) WARN_UNUSED_RESULT; | 125 Maybe<Vector<const uint8_t>> ReadRawBytes(int size) WARN_UNUSED_RESULT; |
| 105 | 126 |
| 106 // Reading V8 objects of specific kinds. | 127 // Reading V8 objects of specific kinds. |
| 107 // The tag is assumed to have already been read. | 128 // The tag is assumed to have already been read. |
| 108 MaybeHandle<String> ReadUtf8String() WARN_UNUSED_RESULT; | 129 MaybeHandle<String> ReadUtf8String() WARN_UNUSED_RESULT; |
| 109 MaybeHandle<String> ReadTwoByteString() WARN_UNUSED_RESULT; | 130 MaybeHandle<String> ReadTwoByteString() WARN_UNUSED_RESULT; |
| 131 MaybeHandle<JSObject> ReadJSObject() WARN_UNUSED_RESULT; |
| 132 |
| 133 /* |
| 134 * Reads key-value pairs into the object until the specified end tag is |
| 135 * encountered. If successful, returns the number of properties read. |
| 136 */ |
| 137 Maybe<uint32_t> ReadJSObjectProperties(Handle<JSObject> object, |
| 138 SerializationTag end_tag); |
| 139 |
| 140 // Manipulating the map from IDs to reified objects. |
| 141 bool HasObjectWithID(uint32_t id); |
| 142 MaybeHandle<JSReceiver> GetObjectWithID(uint32_t id); |
| 143 void AddObjectWithID(uint32_t id, Handle<JSReceiver> object); |
| 110 | 144 |
| 111 Isolate* const isolate_; | 145 Isolate* const isolate_; |
| 112 const uint8_t* position_; | 146 const uint8_t* position_; |
| 113 const uint8_t* const end_; | 147 const uint8_t* const end_; |
| 114 uint32_t version_ = 0; | 148 uint32_t version_ = 0; |
| 149 Handle<SeededNumberDictionary> id_map_; // Always a global handle. |
| 150 uint32_t next_id_ = 0; |
| 115 | 151 |
| 116 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); | 152 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); |
| 117 }; | 153 }; |
| 118 | 154 |
| 119 } // namespace internal | 155 } // namespace internal |
| 120 } // namespace v8 | 156 } // namespace v8 |
| 121 | 157 |
| 122 #endif // V8_VALUE_SERIALIZER_H_ | 158 #endif // V8_VALUE_SERIALIZER_H_ |
| OLD | NEW |