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 #include "src/value-serializer.h" | 5 #include "src/value-serializer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "include/v8.h" | 10 #include "include/v8.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 const Local<Context>& serialization_context() { | 57 const Local<Context>& serialization_context() { |
58 return serialization_context_; | 58 return serialization_context_; |
59 } | 59 } |
60 const Local<Context>& deserialization_context() { | 60 const Local<Context>& deserialization_context() { |
61 return deserialization_context_; | 61 return deserialization_context_; |
62 } | 62 } |
63 | 63 |
64 // Overridden in more specific fixtures. | 64 // Overridden in more specific fixtures. |
65 virtual ValueSerializer::Delegate* GetSerializerDelegate() { return nullptr; } | 65 virtual ValueSerializer::Delegate* GetSerializerDelegate() { return nullptr; } |
66 virtual void BeforeEncode(ValueSerializer*) {} | 66 virtual void BeforeEncode(ValueSerializer*) {} |
| 67 virtual void AfterEncode() {} |
67 virtual ValueDeserializer::Delegate* GetDeserializerDelegate() { | 68 virtual ValueDeserializer::Delegate* GetDeserializerDelegate() { |
68 return nullptr; | 69 return nullptr; |
69 } | 70 } |
70 virtual void BeforeDecode(ValueDeserializer*) {} | 71 virtual void BeforeDecode(ValueDeserializer*) {} |
71 | 72 |
72 template <typename InputFunctor, typename OutputFunctor> | 73 template <typename InputFunctor, typename OutputFunctor> |
73 void RoundTripTest(const InputFunctor& input_functor, | 74 void RoundTripTest(const InputFunctor& input_functor, |
74 const OutputFunctor& output_functor) { | 75 const OutputFunctor& output_functor) { |
75 EncodeTest(input_functor, | 76 EncodeTest(input_functor, |
76 [this, &output_functor](const std::vector<uint8_t>& data) { | 77 [this, &output_functor](const std::vector<uint8_t>& data) { |
(...skipping 25 matching lines...) Expand all Loading... |
102 } | 103 } |
103 | 104 |
104 Maybe<std::vector<uint8_t>> DoEncode(Local<Value> value) { | 105 Maybe<std::vector<uint8_t>> DoEncode(Local<Value> value) { |
105 Local<Context> context = serialization_context(); | 106 Local<Context> context = serialization_context(); |
106 ValueSerializer serializer(isolate(), GetSerializerDelegate()); | 107 ValueSerializer serializer(isolate(), GetSerializerDelegate()); |
107 BeforeEncode(&serializer); | 108 BeforeEncode(&serializer); |
108 serializer.WriteHeader(); | 109 serializer.WriteHeader(); |
109 if (!serializer.WriteValue(context, value).FromMaybe(false)) { | 110 if (!serializer.WriteValue(context, value).FromMaybe(false)) { |
110 return Nothing<std::vector<uint8_t>>(); | 111 return Nothing<std::vector<uint8_t>>(); |
111 } | 112 } |
| 113 AfterEncode(); |
112 return Just(serializer.ReleaseBuffer()); | 114 return Just(serializer.ReleaseBuffer()); |
113 } | 115 } |
114 | 116 |
115 template <typename InputFunctor, typename EncodedDataFunctor> | 117 template <typename InputFunctor, typename EncodedDataFunctor> |
116 void EncodeTest(const InputFunctor& input_functor, | 118 void EncodeTest(const InputFunctor& input_functor, |
117 const EncodedDataFunctor& encoded_data_functor) { | 119 const EncodedDataFunctor& encoded_data_functor) { |
118 Context::Scope scope(serialization_context()); | 120 Context::Scope scope(serialization_context()); |
119 TryCatch try_catch(isolate()); | 121 TryCatch try_catch(isolate()); |
120 Local<Value> input_value = input_functor(); | 122 Local<Value> input_value = input_functor(); |
121 std::vector<uint8_t> buffer; | 123 std::vector<uint8_t> buffer; |
(...skipping 1611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1733 // Includes an ArrayBuffer wrapper marked for transfer from the serialization | 1735 // Includes an ArrayBuffer wrapper marked for transfer from the serialization |
1734 // context to the deserialization context. | 1736 // context to the deserialization context. |
1735 class ValueSerializerTestWithArrayBufferTransfer : public ValueSerializerTest { | 1737 class ValueSerializerTestWithArrayBufferTransfer : public ValueSerializerTest { |
1736 protected: | 1738 protected: |
1737 static const size_t kTestByteLength = 4; | 1739 static const size_t kTestByteLength = 4; |
1738 | 1740 |
1739 ValueSerializerTestWithArrayBufferTransfer() { | 1741 ValueSerializerTestWithArrayBufferTransfer() { |
1740 { | 1742 { |
1741 Context::Scope scope(serialization_context()); | 1743 Context::Scope scope(serialization_context()); |
1742 input_buffer_ = ArrayBuffer::New(isolate(), nullptr, 0); | 1744 input_buffer_ = ArrayBuffer::New(isolate(), nullptr, 0); |
1743 input_buffer_->Neuter(); | |
1744 } | 1745 } |
1745 { | 1746 { |
1746 Context::Scope scope(deserialization_context()); | 1747 Context::Scope scope(deserialization_context()); |
1747 output_buffer_ = ArrayBuffer::New(isolate(), kTestByteLength); | 1748 output_buffer_ = ArrayBuffer::New(isolate(), kTestByteLength); |
1748 const uint8_t data[kTestByteLength] = {0x00, 0x01, 0x80, 0xff}; | 1749 const uint8_t data[kTestByteLength] = {0x00, 0x01, 0x80, 0xff}; |
1749 memcpy(output_buffer_->GetContents().Data(), data, kTestByteLength); | 1750 memcpy(output_buffer_->GetContents().Data(), data, kTestByteLength); |
1750 } | 1751 } |
1751 } | 1752 } |
1752 | 1753 |
1753 const Local<ArrayBuffer>& input_buffer() { return input_buffer_; } | 1754 const Local<ArrayBuffer>& input_buffer() { return input_buffer_; } |
1754 const Local<ArrayBuffer>& output_buffer() { return output_buffer_; } | 1755 const Local<ArrayBuffer>& output_buffer() { return output_buffer_; } |
1755 | 1756 |
1756 void BeforeEncode(ValueSerializer* serializer) override { | 1757 void BeforeEncode(ValueSerializer* serializer) override { |
1757 serializer->TransferArrayBuffer(0, input_buffer_); | 1758 serializer->TransferArrayBuffer(0, input_buffer_); |
1758 } | 1759 } |
1759 | 1760 |
| 1761 void AfterEncode() override { input_buffer_->Neuter(); } |
| 1762 |
1760 void BeforeDecode(ValueDeserializer* deserializer) override { | 1763 void BeforeDecode(ValueDeserializer* deserializer) override { |
1761 deserializer->TransferArrayBuffer(0, output_buffer_); | 1764 deserializer->TransferArrayBuffer(0, output_buffer_); |
1762 } | 1765 } |
1763 | 1766 |
1764 private: | 1767 private: |
1765 Local<ArrayBuffer> input_buffer_; | 1768 Local<ArrayBuffer> input_buffer_; |
1766 Local<ArrayBuffer> output_buffer_; | 1769 Local<ArrayBuffer> output_buffer_; |
1767 }; | 1770 }; |
1768 | 1771 |
1769 TEST_F(ValueSerializerTestWithArrayBufferTransfer, | 1772 TEST_F(ValueSerializerTestWithArrayBufferTransfer, |
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2364 "({ a: new ExampleHostObject(), get b() { return this.a; }})", | 2367 "({ a: new ExampleHostObject(), get b() { return this.a; }})", |
2365 [this](Local<Value> value) { | 2368 [this](Local<Value> value) { |
2366 EXPECT_TRUE(EvaluateScriptForResultBool( | 2369 EXPECT_TRUE(EvaluateScriptForResultBool( |
2367 "result.a instanceof ExampleHostObject")); | 2370 "result.a instanceof ExampleHostObject")); |
2368 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); | 2371 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
2369 }); | 2372 }); |
2370 } | 2373 } |
2371 | 2374 |
2372 } // namespace | 2375 } // namespace |
2373 } // namespace v8 | 2376 } // namespace v8 |
OLD | NEW |