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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 ASSERT_FALSE(try_catch.HasCaught()); | 73 ASSERT_FALSE(try_catch.HasCaught()); |
74 ASSERT_TRUE(deserialization_context() | 74 ASSERT_TRUE(deserialization_context() |
75 ->Global() | 75 ->Global() |
76 ->CreateDataProperty(deserialization_context_, | 76 ->CreateDataProperty(deserialization_context_, |
77 StringFromUtf8("result"), result) | 77 StringFromUtf8("result"), result) |
78 .FromMaybe(false)); | 78 .FromMaybe(false)); |
79 output_functor(result); | 79 output_functor(result); |
80 ASSERT_FALSE(try_catch.HasCaught()); | 80 ASSERT_FALSE(try_catch.HasCaught()); |
81 } | 81 } |
82 | 82 |
83 template <typename OutputFunctor> | |
84 void DecodeTestForVersion0(const std::vector<uint8_t>& data, | |
85 const OutputFunctor& output_functor) { | |
86 Context::Scope scope(deserialization_context()); | |
87 TryCatch try_catch(isolate()); | |
88 // TODO(jbroman): Use the public API once it exists. | |
89 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate()); | |
90 i::HandleScope handle_scope(internal_isolate); | |
91 i::ValueDeserializer deserializer( | |
92 internal_isolate, | |
93 i::Vector<const uint8_t>(&data[0], static_cast<int>(data.size()))); | |
94 // TODO(jbroman): Enable legacy support. | |
95 ASSERT_TRUE(deserializer.ReadHeader().FromMaybe(false)); | |
96 // TODO(jbroman): Check version 0. | |
97 Local<Value> result; | |
98 ASSERT_TRUE(ToLocal<Value>( | |
99 deserializer.ReadObjectUsingEntireBufferForLegacyFormat(), &result)); | |
100 ASSERT_FALSE(result.IsEmpty()); | |
101 ASSERT_FALSE(try_catch.HasCaught()); | |
102 ASSERT_TRUE(deserialization_context() | |
103 ->Global() | |
104 ->CreateDataProperty(deserialization_context_, | |
105 StringFromUtf8("result"), result) | |
106 .FromMaybe(false)); | |
107 output_functor(result); | |
108 ASSERT_FALSE(try_catch.HasCaught()); | |
109 } | |
110 | |
83 void InvalidDecodeTest(const std::vector<uint8_t>& data) { | 111 void InvalidDecodeTest(const std::vector<uint8_t>& data) { |
84 Context::Scope scope(deserialization_context()); | 112 Context::Scope scope(deserialization_context()); |
85 TryCatch try_catch(isolate()); | 113 TryCatch try_catch(isolate()); |
86 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate()); | 114 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate()); |
87 i::HandleScope handle_scope(internal_isolate); | 115 i::HandleScope handle_scope(internal_isolate); |
88 i::ValueDeserializer deserializer( | 116 i::ValueDeserializer deserializer( |
89 internal_isolate, | 117 internal_isolate, |
90 i::Vector<const uint8_t>(&data[0], static_cast<int>(data.size()))); | 118 i::Vector<const uint8_t>(&data[0], static_cast<int>(data.size()))); |
91 Maybe<bool> header_result = deserializer.ReadHeader(); | 119 Maybe<bool> header_result = deserializer.ReadHeader(); |
92 if (header_result.IsNothing()) return; | 120 if (header_result.IsNothing()) return; |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
485 // resolved. | 513 // resolved. |
486 DecodeTest( | 514 DecodeTest( |
487 {0xff, 0x09, 0x3f, 0x00, 0x6f, 0x3f, 0x01, 0x53, 0x04, 0x73, | 515 {0xff, 0x09, 0x3f, 0x00, 0x6f, 0x3f, 0x01, 0x53, 0x04, 0x73, |
488 0x65, 0x6c, 0x66, 0x3f, 0x01, 0x5e, 0x00, 0x7b, 0x01, 0x00}, | 516 0x65, 0x6c, 0x66, 0x3f, 0x01, 0x5e, 0x00, 0x7b, 0x01, 0x00}, |
489 [this](Local<Value> value) { | 517 [this](Local<Value> value) { |
490 ASSERT_TRUE(value->IsObject()); | 518 ASSERT_TRUE(value->IsObject()); |
491 EXPECT_TRUE(EvaluateScriptForResultBool("result === result.self")); | 519 EXPECT_TRUE(EvaluateScriptForResultBool("result === result.self")); |
492 }); | 520 }); |
493 } | 521 } |
494 | 522 |
523 TEST_F(ValueSerializerTest, DecodeDictionaryObjectVersion0) { | |
524 // Empty object. | |
525 DecodeTestForVersion0( | |
526 {0x7b, 0x00}, | |
527 [this](Local<Value> value) { | |
528 ASSERT_TRUE(value->IsObject()); | |
529 EXPECT_TRUE(EvaluateScriptForResultBool( | |
530 "Object.getPrototypeOf(result) === Object.prototype")); | |
531 EXPECT_TRUE(EvaluateScriptForResultBool( | |
532 "Object.getOwnPropertyNames(result).length === 0")); | |
533 }); | |
534 // String key. | |
535 DecodeTestForVersion0( | |
536 {0x53, 0x01, 0x61, 0x49, 0x54, 0x7b, 0x01, 0x00}, | |
537 [this](Local<Value> value) { | |
538 ASSERT_TRUE(value->IsObject()); | |
Camillo Bruni
2016/08/16 09:04:07
nit: Can you add the prototype check from above as
jbroman
2016/08/16 21:34:14
Done here, though in general I'd prefer to avoid d
| |
539 EXPECT_TRUE(EvaluateScriptForResultBool("result.hasOwnProperty('a')")); | |
540 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === 42")); | |
541 EXPECT_TRUE(EvaluateScriptForResultBool( | |
542 "Object.getOwnPropertyNames(result).length === 1")); | |
543 }); | |
544 // Integer key (treated as a string, but may be encoded differently). | |
545 DecodeTestForVersion0( | |
546 {0x49, 0x54, 0x53, 0x01, 0x61, 0x7b, 0x01, 0x00}, | |
547 [this](Local<Value> value) { | |
548 ASSERT_TRUE(value->IsObject()); | |
549 EXPECT_TRUE(EvaluateScriptForResultBool("result.hasOwnProperty('42')")); | |
550 EXPECT_TRUE(EvaluateScriptForResultBool("result[42] === 'a'")); | |
551 EXPECT_TRUE(EvaluateScriptForResultBool( | |
552 "Object.getOwnPropertyNames(result).length === 1")); | |
553 }); | |
554 // Key order must be preserved. | |
555 DecodeTestForVersion0( | |
556 {0x53, 0x01, 0x78, 0x49, 0x02, 0x53, 0x01, 0x79, 0x49, 0x04, 0x53, 0x01, | |
557 0x61, 0x49, 0x06, 0x7b, 0x03, 0x00}, | |
558 [this](Local<Value> value) { | |
559 EXPECT_TRUE(EvaluateScriptForResultBool( | |
560 "Object.getOwnPropertyNames(result).toString() === 'x,y,a'")); | |
561 }); | |
562 } | |
Camillo Bruni
2016/08/16 09:04:07
nit: could you add a simple test with properties a
jbroman
2016/08/16 21:34:14
OK, I'll do one with one property and one element.
| |
563 | |
495 } // namespace | 564 } // namespace |
496 } // namespace v8 | 565 } // namespace v8 |
OLD | NEW |