Chromium Code Reviews| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Variant for the common case where a script is used to build the original | 45 // Variant for the common case where a script is used to build the original |
| 46 // value. | 46 // value. |
| 47 template <typename OutputFunctor> | 47 template <typename OutputFunctor> |
| 48 void RoundTripTest(const char* source, const OutputFunctor& output_functor) { | 48 void RoundTripTest(const char* source, const OutputFunctor& output_functor) { |
| 49 RoundTripTest([this, source]() { return EvaluateScriptForInput(source); }, | 49 RoundTripTest([this, source]() { return EvaluateScriptForInput(source); }, |
| 50 output_functor); | 50 output_functor); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // Variant which uses JSON.parse/stringify to check the result. | |
| 54 void RoundTripJSON(const char* source) { | |
| 55 RoundTripTest( | |
| 56 [this, source]() { | |
| 57 return JSON::Parse(serialization_context_, StringFromUtf8(source)) | |
| 58 .ToLocalChecked(); | |
| 59 }, | |
| 60 [this, source](Local<Value> value) { | |
| 61 ASSERT_TRUE(value->IsObject()); | |
| 62 EXPECT_EQ(source, Utf8Value(JSON::Stringify(deserialization_context_, | |
| 63 value.As<Object>()) | |
| 64 .ToLocalChecked())); | |
| 65 }); | |
| 66 } | |
| 67 | |
| 53 Maybe<std::vector<uint8_t>> DoEncode(Local<Value> value) { | 68 Maybe<std::vector<uint8_t>> DoEncode(Local<Value> value) { |
| 54 Local<Context> context = serialization_context(); | 69 Local<Context> context = serialization_context(); |
| 55 ValueSerializer serializer(isolate()); | 70 ValueSerializer serializer(isolate()); |
| 56 BeforeEncode(&serializer); | 71 BeforeEncode(&serializer); |
| 57 serializer.WriteHeader(); | 72 serializer.WriteHeader(); |
| 58 if (!serializer.WriteValue(context, value).FromMaybe(false)) { | 73 if (!serializer.WriteValue(context, value).FromMaybe(false)) { |
| 59 return Nothing<std::vector<uint8_t>>(); | 74 return Nothing<std::vector<uint8_t>>(); |
| 60 } | 75 } |
| 61 return Just(serializer.ReleaseBuffer()); | 76 return Just(serializer.ReleaseBuffer()); |
| 62 } | 77 } |
| (...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 649 // If an exception is thrown by script, encoding must fail and the exception | 664 // If an exception is thrown by script, encoding must fail and the exception |
| 650 // must be thrown. | 665 // must be thrown. |
| 651 InvalidEncodeTest("({ get a() { throw new Error('sentinel'); } })", | 666 InvalidEncodeTest("({ get a() { throw new Error('sentinel'); } })", |
| 652 [](Local<Message> message) { | 667 [](Local<Message> message) { |
| 653 ASSERT_FALSE(message.IsEmpty()); | 668 ASSERT_FALSE(message.IsEmpty()); |
| 654 EXPECT_NE(std::string::npos, | 669 EXPECT_NE(std::string::npos, |
| 655 Utf8Value(message->Get()).find("sentinel")); | 670 Utf8Value(message->Get()).find("sentinel")); |
| 656 }); | 671 }); |
| 657 } | 672 } |
| 658 | 673 |
| 674 TEST_F(ValueSerializerTest, RoundTripDictionaryObjectForTransitions) { | |
| 675 // A case which should run on the fast path, and should reach all of the | |
| 676 // different cases: | |
| 677 // - expected transition matched | |
| 678 // - expected transition failed to match | |
| 679 // - transition found (of multiple possible) | |
| 680 // - no known transition | |
|
Camillo Bruni
2016/09/14 16:26:20
nit: can you update the order of the comment, so i
jbroman
2016/09/14 19:07:16
Done.
| |
| 681 RoundTripJSON( | |
| 682 "[{\"x\":1,\"y\":2,\"z\":3},{\"x\":4,\"y\":5,\"z\":6},{\"x\":5,\"y\":6," | |
| 683 "\"w\":7},{\"x\":6,\"y\":7,\"z\":8},{\"x\":0,\"y\":0,\"w\":0}]"); | |
|
Camillo Bruni
2016/09/14 16:26:20
late question: single quotes doesn't work for the
jbroman
2016/09/14 19:07:16
This uses JSON::Parse/Stringify to compare the obj
| |
| 684 // A slightly simpler case that uses two-byte strings. | |
| 685 RoundTripJSON( | |
| 686 "[{\"\xF0\x9F\x91\x8A\":1,\"\xF0\x9F\x91\x8B\":2},{\"\xF0\x9F\x91\x8A\":" | |
| 687 "3,\"\xF0\x9F\x91\x8C\":4},{\"\xF0\x9F\x91\x8A\":5,\"\xF0\x9F\x91\x9B\":" | |
| 688 "6}]"); | |
| 689 } | |
| 690 | |
| 659 TEST_F(ValueSerializerTest, DecodeDictionaryObjectVersion0) { | 691 TEST_F(ValueSerializerTest, DecodeDictionaryObjectVersion0) { |
| 660 // Empty object. | 692 // Empty object. |
| 661 DecodeTestForVersion0( | 693 DecodeTestForVersion0( |
| 662 {0x7b, 0x00}, | 694 {0x7b, 0x00}, |
| 663 [this](Local<Value> value) { | 695 [this](Local<Value> value) { |
| 664 ASSERT_TRUE(value->IsObject()); | 696 ASSERT_TRUE(value->IsObject()); |
| 665 EXPECT_TRUE(EvaluateScriptForResultBool( | 697 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 666 "Object.getPrototypeOf(result) === Object.prototype")); | 698 "Object.getPrototypeOf(result) === Object.prototype")); |
| 667 EXPECT_TRUE(EvaluateScriptForResultBool( | 699 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 668 "Object.getOwnPropertyNames(result).length === 0")); | 700 "Object.getOwnPropertyNames(result).length === 0")); |
| (...skipping 1350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2019 } | 2051 } |
| 2020 | 2052 |
| 2021 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer, | 2053 TEST_F(ValueSerializerTestWithSharedArrayBufferTransfer, |
| 2022 SharedArrayBufferMustBeTransferred) { | 2054 SharedArrayBufferMustBeTransferred) { |
| 2023 // A SharedArrayBuffer which was not marked for transfer should fail encoding. | 2055 // A SharedArrayBuffer which was not marked for transfer should fail encoding. |
| 2024 InvalidEncodeTest("new SharedArrayBuffer(32)"); | 2056 InvalidEncodeTest("new SharedArrayBuffer(32)"); |
| 2025 } | 2057 } |
| 2026 | 2058 |
| 2027 } // namespace | 2059 } // namespace |
| 2028 } // namespace v8 | 2060 } // namespace v8 |
| OLD | NEW |