Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef V8_VALUE_SERIALIZER_H_ | |
| 6 #define V8_VALUE_SERIALIZER_H_ | |
| 7 | |
| 8 #include <cstdint> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "include/v8.h" | |
| 12 #include "src/base/compiler-specific.h" | |
| 13 #include "src/base/macros.h" | |
| 14 #include "src/vector.h" | |
| 15 | |
| 16 namespace v8 { | |
| 17 namespace internal { | |
| 18 | |
| 19 class Isolate; | |
| 20 class Object; | |
| 21 class Oddball; | |
| 22 | |
| 23 enum class SerializationTag : uint8_t; | |
| 24 | |
| 25 /** | |
| 26 * Writes V8 objects in a binary format that allows the objects to be cloned | |
| 27 * according to the HTML structured clone algorithm. | |
| 28 * | |
| 29 * Format is based on Blink's previous serialization logic. | |
| 30 */ | |
|
Camillo Bruni
2016/08/12 09:54:44
I wish all V8 classes were that nicely documented
| |
| 31 class ValueSerializer { | |
| 32 public: | |
| 33 ValueSerializer(); | |
| 34 ~ValueSerializer(); | |
| 35 | |
| 36 /* | |
| 37 * Writes out a header, which includes the format version. | |
| 38 */ | |
| 39 void WriteHeader(); | |
| 40 | |
| 41 /* | |
| 42 * Serializes a V8 object into the buffer. | |
| 43 */ | |
| 44 Maybe<bool> WriteObject(Handle<Object> object) WARN_UNUSED_RESULT; | |
| 45 | |
| 46 /* | |
| 47 * 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. | |
| 49 */ | |
| 50 std::vector<uint8_t> ReleaseBuffer() { return std::move(buffer_); } | |
| 51 | |
| 52 private: | |
| 53 // Writing the wire format. | |
| 54 void WriteTag(SerializationTag tag); | |
| 55 template <typename T> | |
|
Camillo Bruni
2016/08/12 09:54:44
nit: Could you add a comment what WriteVarint does
jbroman
2016/08/12 15:16:34
Added comments inline, with a link to the protobuf
jbroman
2016/08/12 15:16:34
Added comments inline, with a link to the protobuf
| |
| 56 void WriteVarint(T value); | |
| 57 | |
| 58 // Writing V8 objects of various kinds. | |
| 59 void WriteOddball(Oddball* oddball); | |
| 60 | |
| 61 std::vector<uint8_t> buffer_; | |
| 62 | |
| 63 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); | |
| 64 }; | |
| 65 | |
| 66 /* | |
| 67 * Deserializes values from data written with ValueSerializer, or a compatible | |
| 68 * implementation. | |
| 69 */ | |
| 70 class ValueDeserializer { | |
| 71 public: | |
| 72 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data); | |
| 73 ~ValueDeserializer(); | |
| 74 | |
| 75 /* | |
| 76 * Runs version detection logic, which may fail if the format is invalid. | |
| 77 */ | |
| 78 Maybe<bool> ReadHeader() WARN_UNUSED_RESULT; | |
| 79 | |
| 80 /* | |
| 81 * Deserializes a V8 object from the buffer. | |
| 82 */ | |
| 83 MaybeHandle<Object> ReadObject() WARN_UNUSED_RESULT; | |
| 84 | |
| 85 private: | |
| 86 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; | |
| 87 template <typename T> | |
| 88 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; | |
| 89 | |
| 90 Isolate* const isolate_; | |
| 91 const uint8_t* position_; | |
| 92 const uint8_t* const end_; | |
| 93 uint32_t version_ = 0; | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); | |
| 96 }; | |
| 97 | |
| 98 } // namespace internal | |
| 99 } // namespace v8 | |
| 100 | |
| 101 #endif // V8_VALUE_SERIALIZER_H_ | |
| OLD | NEW |