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 | 52 |
53 /* | 53 /* |
54 * Serializes a V8 object into the buffer. | 54 * Serializes a V8 object into the buffer. |
55 */ | 55 */ |
56 Maybe<bool> WriteObject(Handle<Object> object) WARN_UNUSED_RESULT; | 56 Maybe<bool> WriteObject(Handle<Object> object) WARN_UNUSED_RESULT; |
57 | 57 |
58 /* | 58 /* |
59 * Returns the stored data. This serializer should not be used once the buffer | 59 * Returns the stored data. This serializer should not be used once the buffer |
60 * is released. The contents are undefined if a previous write has failed. | 60 * is released. The contents are undefined if a previous write has failed. |
61 */ | 61 */ |
62 std::vector<uint8_t> ReleaseBuffer() { return std::move(buffer_); } | 62 std::vector<uint8_t> ReleaseBuffer(); |
| 63 |
| 64 /* |
| 65 * Returns the buffer, allocated via the delegate, and its size. |
| 66 * Caller assumes ownership of the buffer. |
| 67 */ |
| 68 std::pair<uint8_t*, size_t> Release(); |
63 | 69 |
64 /* | 70 /* |
65 * Marks an ArrayBuffer as havings its contents transferred out of band. | 71 * Marks an ArrayBuffer as havings its contents transferred out of band. |
66 * Pass the corresponding JSArrayBuffer in the deserializing context to | 72 * Pass the corresponding JSArrayBuffer in the deserializing context to |
67 * ValueDeserializer::TransferArrayBuffer. | 73 * ValueDeserializer::TransferArrayBuffer. |
68 */ | 74 */ |
69 void TransferArrayBuffer(uint32_t transfer_id, | 75 void TransferArrayBuffer(uint32_t transfer_id, |
70 Handle<JSArrayBuffer> array_buffer); | 76 Handle<JSArrayBuffer> array_buffer); |
71 | 77 |
72 /* | 78 /* |
73 * Publicly exposed wire format writing methods. | 79 * Publicly exposed wire format writing methods. |
74 * These are intended for use within the delegate's WriteHostObject method. | 80 * These are intended for use within the delegate's WriteHostObject method. |
75 */ | 81 */ |
76 void WriteUint32(uint32_t value); | 82 void WriteUint32(uint32_t value); |
77 void WriteUint64(uint64_t value); | 83 void WriteUint64(uint64_t value); |
78 void WriteRawBytes(const void* source, size_t length); | 84 void WriteRawBytes(const void* source, size_t length); |
79 void WriteDouble(double value); | 85 void WriteDouble(double value); |
80 | 86 |
81 private: | 87 private: |
| 88 // Managing allocations of the internal buffer. |
| 89 void ExpandBuffer(size_t required_capacity); |
| 90 |
82 // Writing the wire format. | 91 // Writing the wire format. |
83 void WriteTag(SerializationTag tag); | 92 void WriteTag(SerializationTag tag); |
84 template <typename T> | 93 template <typename T> |
85 void WriteVarint(T value); | 94 void WriteVarint(T value); |
86 template <typename T> | 95 template <typename T> |
87 void WriteZigZag(T value); | 96 void WriteZigZag(T value); |
88 void WriteOneByteString(Vector<const uint8_t> chars); | 97 void WriteOneByteString(Vector<const uint8_t> chars); |
89 void WriteTwoByteString(Vector<const uc16> chars); | 98 void WriteTwoByteString(Vector<const uc16> chars); |
90 uint8_t* ReserveRawBytes(size_t bytes); | 99 uint8_t* ReserveRawBytes(size_t bytes); |
91 | 100 |
(...skipping 27 matching lines...) Expand all Loading... |
119 /* | 128 /* |
120 * Asks the delegate to handle an error that occurred during data cloning, by | 129 * Asks the delegate to handle an error that occurred during data cloning, by |
121 * throwing an exception appropriate for the host. | 130 * throwing an exception appropriate for the host. |
122 */ | 131 */ |
123 void ThrowDataCloneError(MessageTemplate::Template template_index); | 132 void ThrowDataCloneError(MessageTemplate::Template template_index); |
124 V8_NOINLINE void ThrowDataCloneError(MessageTemplate::Template template_index, | 133 V8_NOINLINE void ThrowDataCloneError(MessageTemplate::Template template_index, |
125 Handle<Object> arg0); | 134 Handle<Object> arg0); |
126 | 135 |
127 Isolate* const isolate_; | 136 Isolate* const isolate_; |
128 v8::ValueSerializer::Delegate* const delegate_; | 137 v8::ValueSerializer::Delegate* const delegate_; |
129 std::vector<uint8_t> buffer_; | 138 uint8_t* buffer_ = nullptr; |
| 139 size_t buffer_size_ = 0; |
| 140 size_t buffer_capacity_ = 0; |
130 Zone zone_; | 141 Zone zone_; |
131 | 142 |
132 // To avoid extra lookups in the identity map, ID+1 is actually stored in the | 143 // To avoid extra lookups in the identity map, ID+1 is actually stored in the |
133 // map (checking if the used identity is zero is the fast way of checking if | 144 // map (checking if the used identity is zero is the fast way of checking if |
134 // the entry is new). | 145 // the entry is new). |
135 IdentityMap<uint32_t> id_map_; | 146 IdentityMap<uint32_t> id_map_; |
136 uint32_t next_id_ = 0; | 147 uint32_t next_id_ = 0; |
137 | 148 |
138 // A similar map, for transferred array buffers. | 149 // A similar map, for transferred array buffers. |
139 IdentityMap<uint32_t> array_buffer_transfer_map_; | 150 IdentityMap<uint32_t> array_buffer_transfer_map_; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 Handle<FixedArray> id_map_; | 270 Handle<FixedArray> id_map_; |
260 MaybeHandle<SeededNumberDictionary> array_buffer_transfer_map_; | 271 MaybeHandle<SeededNumberDictionary> array_buffer_transfer_map_; |
261 | 272 |
262 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); | 273 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); |
263 }; | 274 }; |
264 | 275 |
265 } // namespace internal | 276 } // namespace internal |
266 } // namespace v8 | 277 } // namespace v8 |
267 | 278 |
268 #endif // V8_VALUE_SERIALIZER_H_ | 279 #endif // V8_VALUE_SERIALIZER_H_ |
OLD | NEW |