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/vector.h" | 14 #include "src/vector.h" |
15 | 15 |
16 namespace v8 { | 16 namespace v8 { |
17 namespace internal { | 17 namespace internal { |
18 | 18 |
| 19 class HeapNumber; |
19 class Isolate; | 20 class Isolate; |
20 class Object; | 21 class Object; |
21 class Oddball; | 22 class Oddball; |
| 23 class Smi; |
22 | 24 |
23 enum class SerializationTag : uint8_t; | 25 enum class SerializationTag : uint8_t; |
24 | 26 |
25 /** | 27 /** |
26 * Writes V8 objects in a binary format that allows the objects to be cloned | 28 * Writes V8 objects in a binary format that allows the objects to be cloned |
27 * according to the HTML structured clone algorithm. | 29 * according to the HTML structured clone algorithm. |
28 * | 30 * |
29 * Format is based on Blink's previous serialization logic. | 31 * Format is based on Blink's previous serialization logic. |
30 */ | 32 */ |
31 class ValueSerializer { | 33 class ValueSerializer { |
(...skipping 15 matching lines...) Expand all Loading... |
47 * Returns the stored data. This serializer should not be used once the buffer | 49 * Returns the stored data. This serializer should not be used once the buffer |
48 * is released. The contents are undefined if a previous write has failed. | 50 * is released. The contents are undefined if a previous write has failed. |
49 */ | 51 */ |
50 std::vector<uint8_t> ReleaseBuffer() { return std::move(buffer_); } | 52 std::vector<uint8_t> ReleaseBuffer() { return std::move(buffer_); } |
51 | 53 |
52 private: | 54 private: |
53 // Writing the wire format. | 55 // Writing the wire format. |
54 void WriteTag(SerializationTag tag); | 56 void WriteTag(SerializationTag tag); |
55 template <typename T> | 57 template <typename T> |
56 void WriteVarint(T value); | 58 void WriteVarint(T value); |
| 59 template <typename T> |
| 60 void WriteZigZag(T value); |
| 61 void WriteDouble(double value); |
57 | 62 |
58 // Writing V8 objects of various kinds. | 63 // Writing V8 objects of various kinds. |
59 void WriteOddball(Oddball* oddball); | 64 void WriteOddball(Oddball* oddball); |
| 65 void WriteSmi(Smi* smi); |
| 66 void WriteHeapNumber(HeapNumber* number); |
60 | 67 |
61 std::vector<uint8_t> buffer_; | 68 std::vector<uint8_t> buffer_; |
62 | 69 |
63 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); | 70 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); |
64 }; | 71 }; |
65 | 72 |
66 /* | 73 /* |
67 * Deserializes values from data written with ValueSerializer, or a compatible | 74 * Deserializes values from data written with ValueSerializer, or a compatible |
68 * implementation. | 75 * implementation. |
69 */ | 76 */ |
70 class ValueDeserializer { | 77 class ValueDeserializer { |
71 public: | 78 public: |
72 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data); | 79 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data); |
73 ~ValueDeserializer(); | 80 ~ValueDeserializer(); |
74 | 81 |
75 /* | 82 /* |
76 * Runs version detection logic, which may fail if the format is invalid. | 83 * Runs version detection logic, which may fail if the format is invalid. |
77 */ | 84 */ |
78 Maybe<bool> ReadHeader() WARN_UNUSED_RESULT; | 85 Maybe<bool> ReadHeader() WARN_UNUSED_RESULT; |
79 | 86 |
80 /* | 87 /* |
81 * Deserializes a V8 object from the buffer. | 88 * Deserializes a V8 object from the buffer. |
82 */ | 89 */ |
83 MaybeHandle<Object> ReadObject() WARN_UNUSED_RESULT; | 90 MaybeHandle<Object> ReadObject() WARN_UNUSED_RESULT; |
84 | 91 |
85 private: | 92 private: |
86 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; | 93 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; |
87 template <typename T> | 94 template <typename T> |
88 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; | 95 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; |
| 96 template <typename T> |
| 97 Maybe<T> ReadZigZag() WARN_UNUSED_RESULT; |
| 98 Maybe<double> ReadDouble() WARN_UNUSED_RESULT; |
89 | 99 |
90 Isolate* const isolate_; | 100 Isolate* const isolate_; |
91 const uint8_t* position_; | 101 const uint8_t* position_; |
92 const uint8_t* const end_; | 102 const uint8_t* const end_; |
93 uint32_t version_ = 0; | 103 uint32_t version_ = 0; |
94 | 104 |
95 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); | 105 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); |
96 }; | 106 }; |
97 | 107 |
98 } // namespace internal | 108 } // namespace internal |
99 } // namespace v8 | 109 } // namespace v8 |
100 | 110 |
101 #endif // V8_VALUE_SERIALIZER_H_ | 111 #endif // V8_VALUE_SERIALIZER_H_ |
OLD | NEW |