| 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/identity-map.h" |
| 15 #include "src/vector.h" | 15 #include "src/vector.h" |
| 16 #include "src/zone-containers.h" |
| 16 #include "src/zone.h" | 17 #include "src/zone.h" |
| 17 | 18 |
| 18 namespace v8 { | 19 namespace v8 { |
| 19 namespace internal { | 20 namespace internal { |
| 20 | 21 |
| 21 class HeapNumber; | 22 class HeapNumber; |
| 22 class Isolate; | 23 class Isolate; |
| 23 class JSArrayBuffer; | 24 class JSArrayBuffer; |
| 24 class JSArrayBufferView; | 25 class JSArrayBufferView; |
| 25 class JSDate; | 26 class JSDate; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 44 explicit ValueSerializer(Isolate* isolate); | 45 explicit ValueSerializer(Isolate* isolate); |
| 45 ~ValueSerializer(); | 46 ~ValueSerializer(); |
| 46 | 47 |
| 47 /* | 48 /* |
| 48 * Writes out a header, which includes the format version. | 49 * Writes out a header, which includes the format version. |
| 49 */ | 50 */ |
| 50 void WriteHeader(); | 51 void WriteHeader(); |
| 51 | 52 |
| 52 /* | 53 /* |
| 53 * Serializes a V8 object into the buffer. | 54 * Serializes a V8 object into the buffer. |
| 55 * |
| 56 * Code internal to this class should use WriteObjectInternal instead, which |
| 57 * will use the same states stack to avoid recursion on the call stack. |
| 54 */ | 58 */ |
| 55 Maybe<bool> WriteObject(Handle<Object> object) WARN_UNUSED_RESULT; | 59 Maybe<bool> WriteObject(Handle<Object> object) WARN_UNUSED_RESULT; |
| 56 | 60 |
| 57 /* | 61 /* |
| 58 * Returns the stored data. This serializer should not be used once the buffer | 62 * Returns the stored data. This serializer should not be used once the buffer |
| 59 * is released. The contents are undefined if a previous write has failed. | 63 * is released. The contents are undefined if a previous write has failed. |
| 60 */ | 64 */ |
| 61 std::vector<uint8_t> ReleaseBuffer() { return std::move(buffer_); } | 65 std::vector<uint8_t> ReleaseBuffer() { return std::move(buffer_); } |
| 62 | 66 |
| 63 /* | 67 /* |
| (...skipping 11 matching lines...) Expand all Loading... |
| 75 void WriteVarint(T value); | 79 void WriteVarint(T value); |
| 76 template <typename T> | 80 template <typename T> |
| 77 void WriteZigZag(T value); | 81 void WriteZigZag(T value); |
| 78 void WriteDouble(double value); | 82 void WriteDouble(double value); |
| 79 void WriteOneByteString(Vector<const uint8_t> chars); | 83 void WriteOneByteString(Vector<const uint8_t> chars); |
| 80 void WriteTwoByteString(Vector<const uc16> chars); | 84 void WriteTwoByteString(Vector<const uc16> chars); |
| 81 void WriteRawBytes(const void* source, size_t length); | 85 void WriteRawBytes(const void* source, size_t length); |
| 82 uint8_t* ReserveRawBytes(size_t bytes); | 86 uint8_t* ReserveRawBytes(size_t bytes); |
| 83 | 87 |
| 84 // Writing V8 objects of various kinds. | 88 // Writing V8 objects of various kinds. |
| 89 // These are expected to be used internally only. For objects which require |
| 90 // recursive serialization, a state is pushed before returning. |
| 91 // WriteObject will iterate on the states until the object is fully |
| 92 // serialized. |
| 93 Maybe<bool> WriteObjectInternal(Handle<Object> object) WARN_UNUSED_RESULT; |
| 85 void WriteOddball(Oddball* oddball); | 94 void WriteOddball(Oddball* oddball); |
| 86 void WriteSmi(Smi* smi); | 95 void WriteSmi(Smi* smi); |
| 87 void WriteHeapNumber(HeapNumber* number); | 96 void WriteHeapNumber(HeapNumber* number); |
| 88 void WriteString(Handle<String> string); | 97 void WriteString(Handle<String> string); |
| 89 Maybe<bool> WriteJSReceiver(Handle<JSReceiver> receiver) WARN_UNUSED_RESULT; | 98 Maybe<bool> WriteJSReceiver(Handle<JSReceiver> receiver) WARN_UNUSED_RESULT; |
| 90 Maybe<bool> WriteJSObject(Handle<JSObject> object) WARN_UNUSED_RESULT; | 99 Maybe<bool> WriteJSObject(Handle<JSObject> object) WARN_UNUSED_RESULT; |
| 91 Maybe<bool> WriteJSArray(Handle<JSArray> array) WARN_UNUSED_RESULT; | 100 Maybe<bool> WriteJSArray(Handle<JSArray> array) WARN_UNUSED_RESULT; |
| 92 void WriteJSDate(JSDate* date); | 101 void WriteJSDate(JSDate* date); |
| 93 Maybe<bool> WriteJSValue(Handle<JSValue> value) WARN_UNUSED_RESULT; | 102 Maybe<bool> WriteJSValue(Handle<JSValue> value) WARN_UNUSED_RESULT; |
| 94 void WriteJSRegExp(JSRegExp* regexp); | 103 void WriteJSRegExp(JSRegExp* regexp); |
| 95 Maybe<bool> WriteJSMap(Handle<JSMap> map) WARN_UNUSED_RESULT; | 104 Maybe<bool> WriteJSMap(Handle<JSMap> map) WARN_UNUSED_RESULT; |
| 96 Maybe<bool> WriteJSSet(Handle<JSSet> map) WARN_UNUSED_RESULT; | 105 Maybe<bool> WriteJSSet(Handle<JSSet> map) WARN_UNUSED_RESULT; |
| 97 Maybe<bool> WriteJSArrayBuffer(JSArrayBuffer* array_buffer); | 106 Maybe<bool> WriteJSArrayBuffer(JSArrayBuffer* array_buffer); |
| 98 Maybe<bool> WriteJSArrayBufferView(JSArrayBufferView* array_buffer); | 107 Maybe<bool> WriteJSArrayBufferView(JSArrayBufferView* array_buffer); |
| 99 | 108 |
| 100 /* | |
| 101 * Reads the specified keys from the object and writes key-value pairs to the | |
| 102 * buffer. Returns the number of keys actually written, which may be smaller | |
| 103 * if some keys are not own properties when accessed. | |
| 104 */ | |
| 105 Maybe<uint32_t> WriteJSObjectProperties( | |
| 106 Handle<JSObject> object, Handle<FixedArray> keys) WARN_UNUSED_RESULT; | |
| 107 | |
| 108 Isolate* const isolate_; | 109 Isolate* const isolate_; |
| 109 std::vector<uint8_t> buffer_; | 110 std::vector<uint8_t> buffer_; |
| 110 Zone zone_; | 111 Zone zone_; |
| 111 | 112 |
| 112 // To avoid extra lookups in the identity map, ID+1 is actually stored in the | 113 // To avoid extra lookups in the identity map, ID+1 is actually stored in the |
| 113 // map (checking if the used identity is zero is the fast way of checking if | 114 // map (checking if the used identity is zero is the fast way of checking if |
| 114 // the entry is new). | 115 // the entry is new). |
| 115 IdentityMap<uint32_t> id_map_; | 116 IdentityMap<uint32_t> id_map_; |
| 116 uint32_t next_id_ = 0; | 117 uint32_t next_id_ = 0; |
| 117 | 118 |
| 118 // A similar map, for transferred array buffers. | 119 // A similar map, for transferred array buffers. |
| 119 IdentityMap<uint32_t> array_buffer_transfer_map_; | 120 IdentityMap<uint32_t> array_buffer_transfer_map_; |
| 120 | 121 |
| 122 // A stack of states. |
| 123 struct State; |
| 124 ZoneVector<State> states_; |
| 125 |
| 121 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); | 126 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); |
| 122 }; | 127 }; |
| 123 | 128 |
| 124 /* | 129 /* |
| 125 * Deserializes values from data written with ValueSerializer, or a compatible | 130 * Deserializes values from data written with ValueSerializer, or a compatible |
| 126 * implementation. | 131 * implementation. |
| 127 */ | 132 */ |
| 128 class ValueDeserializer { | 133 class ValueDeserializer { |
| 129 public: | 134 public: |
| 130 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data); | 135 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 Maybe<SerializationTag> PeekTag() const WARN_UNUSED_RESULT; | 174 Maybe<SerializationTag> PeekTag() const WARN_UNUSED_RESULT; |
| 170 void ConsumeTag(SerializationTag peeked_tag); | 175 void ConsumeTag(SerializationTag peeked_tag); |
| 171 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; | 176 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; |
| 172 template <typename T> | 177 template <typename T> |
| 173 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; | 178 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; |
| 174 template <typename T> | 179 template <typename T> |
| 175 Maybe<T> ReadZigZag() WARN_UNUSED_RESULT; | 180 Maybe<T> ReadZigZag() WARN_UNUSED_RESULT; |
| 176 Maybe<double> ReadDouble() WARN_UNUSED_RESULT; | 181 Maybe<double> ReadDouble() WARN_UNUSED_RESULT; |
| 177 Maybe<Vector<const uint8_t>> ReadRawBytes(int size) WARN_UNUSED_RESULT; | 182 Maybe<Vector<const uint8_t>> ReadRawBytes(int size) WARN_UNUSED_RESULT; |
| 178 | 183 |
| 179 // Like ReadObject, but skips logic for special cases in simulating the | 184 // Reading V8 objects of specific kinds. |
| 180 // "stack machine". | 185 // These are expected to be used internally only. For objects which require |
| 186 // recursive deserialization, a state is pushed before returning. |
| 187 // ReadObject will iterate on the states until the object is fully |
| 188 // serialized. |
| 181 MaybeHandle<Object> ReadObjectInternal() WARN_UNUSED_RESULT; | 189 MaybeHandle<Object> ReadObjectInternal() WARN_UNUSED_RESULT; |
| 182 | |
| 183 // Reading V8 objects of specific kinds. | |
| 184 // The tag is assumed to have already been read. | |
| 185 MaybeHandle<String> ReadUtf8String() WARN_UNUSED_RESULT; | 190 MaybeHandle<String> ReadUtf8String() WARN_UNUSED_RESULT; |
| 186 MaybeHandle<String> ReadTwoByteString() WARN_UNUSED_RESULT; | 191 MaybeHandle<String> ReadTwoByteString() WARN_UNUSED_RESULT; |
| 187 MaybeHandle<JSObject> ReadJSObject() WARN_UNUSED_RESULT; | 192 MaybeHandle<JSObject> ReadJSObject() WARN_UNUSED_RESULT; |
| 188 MaybeHandle<JSArray> ReadSparseJSArray() WARN_UNUSED_RESULT; | 193 MaybeHandle<JSArray> ReadSparseJSArray() WARN_UNUSED_RESULT; |
| 189 MaybeHandle<JSArray> ReadDenseJSArray() WARN_UNUSED_RESULT; | 194 MaybeHandle<JSArray> ReadDenseJSArray() WARN_UNUSED_RESULT; |
| 190 MaybeHandle<JSDate> ReadJSDate() WARN_UNUSED_RESULT; | 195 MaybeHandle<JSDate> ReadJSDate() WARN_UNUSED_RESULT; |
| 191 MaybeHandle<JSValue> ReadJSValue(SerializationTag tag) WARN_UNUSED_RESULT; | 196 MaybeHandle<JSValue> ReadJSValue(SerializationTag tag) WARN_UNUSED_RESULT; |
| 192 MaybeHandle<JSRegExp> ReadJSRegExp() WARN_UNUSED_RESULT; | 197 MaybeHandle<JSRegExp> ReadJSRegExp() WARN_UNUSED_RESULT; |
| 193 MaybeHandle<JSMap> ReadJSMap() WARN_UNUSED_RESULT; | 198 MaybeHandle<JSMap> ReadJSMap() WARN_UNUSED_RESULT; |
| 194 MaybeHandle<JSSet> ReadJSSet() WARN_UNUSED_RESULT; | 199 MaybeHandle<JSSet> ReadJSSet() WARN_UNUSED_RESULT; |
| 195 MaybeHandle<JSArrayBuffer> ReadJSArrayBuffer() WARN_UNUSED_RESULT; | 200 MaybeHandle<JSArrayBuffer> ReadJSArrayBuffer() WARN_UNUSED_RESULT; |
| 196 MaybeHandle<JSArrayBuffer> ReadTransferredJSArrayBuffer(bool is_shared) | 201 MaybeHandle<JSArrayBuffer> ReadTransferredJSArrayBuffer(bool is_shared) |
| 197 WARN_UNUSED_RESULT; | 202 WARN_UNUSED_RESULT; |
| 198 MaybeHandle<JSArrayBufferView> ReadJSArrayBufferView( | 203 MaybeHandle<JSArrayBufferView> ReadJSArrayBufferView( |
| 199 Handle<JSArrayBuffer> buffer) WARN_UNUSED_RESULT; | 204 Handle<JSArrayBuffer> buffer) WARN_UNUSED_RESULT; |
| 200 | 205 |
| 201 /* | |
| 202 * Reads key-value pairs into the object until the specified end tag is | |
| 203 * encountered. If successful, returns the number of properties read. | |
| 204 */ | |
| 205 Maybe<uint32_t> ReadJSObjectProperties(Handle<JSObject> object, | |
| 206 SerializationTag end_tag); | |
| 207 | |
| 208 // Manipulating the map from IDs to reified objects. | 206 // Manipulating the map from IDs to reified objects. |
| 209 bool HasObjectWithID(uint32_t id); | 207 bool HasObjectWithID(uint32_t id); |
| 210 MaybeHandle<JSReceiver> GetObjectWithID(uint32_t id); | 208 MaybeHandle<JSReceiver> GetObjectWithID(uint32_t id); |
| 211 void AddObjectWithID(uint32_t id, Handle<JSReceiver> object); | 209 void AddObjectWithID(uint32_t id, Handle<JSReceiver> object); |
| 212 | 210 |
| 213 Isolate* const isolate_; | 211 Isolate* const isolate_; |
| 214 const uint8_t* position_; | 212 const uint8_t* position_; |
| 215 const uint8_t* const end_; | 213 const uint8_t* const end_; |
| 216 uint32_t version_ = 0; | 214 uint32_t version_ = 0; |
| 217 uint32_t next_id_ = 0; | 215 uint32_t next_id_ = 0; |
| 218 | 216 |
| 219 // Always global handles. | 217 // Always global handles. |
| 220 Handle<SeededNumberDictionary> id_map_; | 218 Handle<SeededNumberDictionary> id_map_; |
| 221 MaybeHandle<SeededNumberDictionary> array_buffer_transfer_map_; | 219 MaybeHandle<SeededNumberDictionary> array_buffer_transfer_map_; |
| 222 | 220 |
| 221 // A stack of states. |
| 222 struct State; |
| 223 std::vector<State> states_; |
| 224 |
| 223 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); | 225 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); |
| 224 }; | 226 }; |
| 225 | 227 |
| 226 } // namespace internal | 228 } // namespace internal |
| 227 } // namespace v8 | 229 } // namespace v8 |
| 228 | 230 |
| 229 #endif // V8_VALUE_SERIALIZER_H_ | 231 #endif // V8_VALUE_SERIALIZER_H_ |
| OLD | NEW |