OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium 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 "content/child/v8_value_converter_impl.h" | 5 #include "content/child/v8_value_converter_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <cmath> | 10 #include <cmath> |
(...skipping 831 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 expected_dictionary.Set(key, base::Value::CreateNullValue()); | 842 expected_dictionary.Set(key, base::Value::CreateNullValue()); |
843 | 843 |
844 // The actual result. | 844 // The actual result. |
845 std::unique_ptr<base::Value> actual_dictionary( | 845 std::unique_ptr<base::Value> actual_dictionary( |
846 converter.FromV8Value(recursive_object, context)); | 846 converter.FromV8Value(recursive_object, context)); |
847 ASSERT_TRUE(actual_dictionary.get()); | 847 ASSERT_TRUE(actual_dictionary.get()); |
848 | 848 |
849 EXPECT_TRUE(expected_dictionary.Equals(actual_dictionary.get())); | 849 EXPECT_TRUE(expected_dictionary.Equals(actual_dictionary.get())); |
850 } | 850 } |
851 | 851 |
| 852 // Tests that reused object values with no cycles do not get nullified. |
| 853 TEST_F(V8ValueConverterImplTest, ReuseObjects) { |
| 854 v8::HandleScope handle_scope(isolate_); |
| 855 v8::Local<v8::Context> context = |
| 856 v8::Local<v8::Context>::New(isolate_, context_); |
| 857 v8::Context::Scope context_scope(context); |
| 858 v8::MicrotasksScope microtasks( |
| 859 isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks); |
| 860 V8ValueConverterImpl converter; |
| 861 |
| 862 // Object with reused values in different keys. |
| 863 { |
| 864 const char* source = "(function() {" |
| 865 "var objA = {key: 'another same value'};" |
| 866 "var obj = {one: objA, two: objA};" |
| 867 "return obj;" |
| 868 "})();"; |
| 869 v8::Local<v8::Script> script( |
| 870 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 871 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); |
| 872 ASSERT_FALSE(object.IsEmpty()); |
| 873 |
| 874 // The actual result. |
| 875 std::unique_ptr<base::DictionaryValue> result( |
| 876 static_cast<base::DictionaryValue*>( |
| 877 converter.FromV8Value(object, context))); |
| 878 ASSERT_TRUE(result.get()); |
| 879 EXPECT_EQ(2u, result->size()); |
| 880 |
| 881 { |
| 882 base::DictionaryValue* one_dict = nullptr; |
| 883 const char* key1 = "one"; |
| 884 ASSERT_TRUE(result->GetDictionary(key1, &one_dict)); |
| 885 EXPECT_EQ("another same value", GetString(one_dict, "key")); |
| 886 } |
| 887 { |
| 888 base::DictionaryValue* two_dict = nullptr; |
| 889 const char* key2 = "two"; |
| 890 ASSERT_TRUE(result->GetDictionary(key2, &two_dict)); |
| 891 EXPECT_EQ("another same value", GetString(two_dict, "key")); |
| 892 } |
| 893 } |
| 894 |
| 895 // Array with reused values. |
| 896 { |
| 897 const char* source = "(function() {" |
| 898 "var objA = {key: 'same value'};" |
| 899 "var arr = [objA, objA];" |
| 900 "return arr;" |
| 901 "})();"; |
| 902 v8::Local<v8::Script> script( |
| 903 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 904 v8::Local<v8::Array> array = script->Run().As<v8::Array>(); |
| 905 ASSERT_FALSE(array.IsEmpty()); |
| 906 |
| 907 // The actual result. |
| 908 std::unique_ptr<base::ListValue> list_result( |
| 909 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); |
| 910 ASSERT_TRUE(list_result.get()); |
| 911 ASSERT_EQ(2u, list_result->GetSize()); |
| 912 for (size_t i = 0; i < list_result->GetSize(); ++i) { |
| 913 ASSERT_FALSE(IsNull(list_result.get(), i)); |
| 914 base::DictionaryValue* dict_value = nullptr; |
| 915 ASSERT_TRUE(list_result->GetDictionary(0u, &dict_value)); |
| 916 EXPECT_EQ("same value", GetString(dict_value, "key")); |
| 917 } |
| 918 } |
| 919 } |
| 920 |
852 TEST_F(V8ValueConverterImplTest, MaxRecursionDepth) { | 921 TEST_F(V8ValueConverterImplTest, MaxRecursionDepth) { |
853 v8::HandleScope handle_scope(isolate_); | 922 v8::HandleScope handle_scope(isolate_); |
854 v8::Local<v8::Context> context = | 923 v8::Local<v8::Context> context = |
855 v8::Local<v8::Context>::New(isolate_, context_); | 924 v8::Local<v8::Context>::New(isolate_, context_); |
856 v8::Context::Scope context_scope(context); | 925 v8::Context::Scope context_scope(context); |
857 | 926 |
858 // Must larger than kMaxRecursionDepth in v8_value_converter_impl.cc. | 927 // Must larger than kMaxRecursionDepth in v8_value_converter_impl.cc. |
859 int kDepth = 1000; | 928 int kDepth = 1000; |
860 const char kKey[] = "key"; | 929 const char kKey[] = "key"; |
861 | 930 |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1054 EXPECT_TRUE( | 1123 EXPECT_TRUE( |
1055 base::Value::Equals(reference_number_value.get(), number_value.get())); | 1124 base::Value::Equals(reference_number_value.get(), number_value.get())); |
1056 | 1125 |
1057 v8::Local<v8::Primitive> undefined(v8::Undefined(isolate_)); | 1126 v8::Local<v8::Primitive> undefined(v8::Undefined(isolate_)); |
1058 std::unique_ptr<base::Value> undefined_value( | 1127 std::unique_ptr<base::Value> undefined_value( |
1059 converter.FromV8Value(undefined, context)); | 1128 converter.FromV8Value(undefined, context)); |
1060 EXPECT_FALSE(undefined_value); | 1129 EXPECT_FALSE(undefined_value); |
1061 } | 1130 } |
1062 | 1131 |
1063 } // namespace content | 1132 } // namespace content |
OLD | NEW |