| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/dart_api_impl.h" | 5 #include "vm/dart_api_impl.h" |
| 6 #include "vm/dart_api_state.h" | 6 #include "vm/dart_api_state.h" |
| 7 #include "vm/object.h" | 7 #include "vm/object.h" |
| 8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
| 9 | 9 |
| 10 namespace dart { | 10 namespace dart { |
| 11 | 11 |
| 12 RawField* LookupField(Dart_Handle library, | 12 RawField* LookupField(Dart_Handle library, |
| 13 const char* class_name, | 13 const char* class_name, |
| 14 const char* field_name) { | 14 const char* field_name) { |
| 15 RawLibrary* raw_library = Library::RawCast(Api::UnwrapHandle(library)); | 15 RawLibrary* raw_library = Library::RawCast(Api::UnwrapHandle(library)); |
| 16 Library& lib = Library::ZoneHandle(raw_library); | 16 Library& lib = Library::ZoneHandle(raw_library); |
| 17 const String& classname = | 17 const String& classname = |
| 18 String::Handle(Symbols::New(Thread::Current(), class_name)); | 18 String::Handle(Symbols::New(Thread::Current(), class_name)); |
| 19 Class& cls = Class::Handle(lib.LookupClass(classname)); | 19 Class& cls = Class::Handle(lib.LookupClass(classname)); |
| 20 EXPECT(!cls.IsNull()); // No ambiguity error expected. | 20 EXPECT(!cls.IsNull()); // No ambiguity error expected. |
| 21 | 21 |
| 22 String& fieldname = String::Handle(String::New(field_name)); | 22 String& fieldname = String::Handle(String::New(field_name)); |
| 23 Field& field = | 23 Field& field = |
| 24 Field::ZoneHandle(cls.LookupInstanceFieldAllowPrivate(fieldname)); | 24 Field::ZoneHandle(cls.LookupInstanceFieldAllowPrivate(fieldname)); |
| 25 EXPECT(!field.IsNull()); | 25 EXPECT(!field.IsNull()); |
| 26 return field.raw(); | 26 return field.raw(); |
| 27 } | 27 } |
| 28 | 28 |
| 29 | |
| 30 TEST_CASE(GuardFieldSimpleTest) { | 29 TEST_CASE(GuardFieldSimpleTest) { |
| 31 const char* script_chars = | 30 const char* script_chars = |
| 32 "class A {\n" | 31 "class A {\n" |
| 33 " var f1 = 3.0;\n" | 32 " var f1 = 3.0;\n" |
| 34 " var f2 = 3;\n" | 33 " var f2 = 3;\n" |
| 35 " var f3 = new List(4);\n" | 34 " var f3 = new List(4);\n" |
| 36 " foo() {\n" | 35 " foo() {\n" |
| 37 " f1 = f1 + f1;\n" | 36 " f1 = f1 + f1;\n" |
| 38 " }\n" | 37 " }\n" |
| 39 " bar() {\n" | 38 " bar() {\n" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 70 const intptr_t no_length = Field::kNoFixedLength; | 69 const intptr_t no_length = Field::kNoFixedLength; |
| 71 EXPECT_EQ(no_length, f1.guarded_list_length()); | 70 EXPECT_EQ(no_length, f1.guarded_list_length()); |
| 72 EXPECT_EQ(kDoubleCid, f1.guarded_cid()); | 71 EXPECT_EQ(kDoubleCid, f1.guarded_cid()); |
| 73 EXPECT_EQ(false, f1.is_nullable()); | 72 EXPECT_EQ(false, f1.is_nullable()); |
| 74 EXPECT_EQ(no_length, f2.guarded_list_length()); | 73 EXPECT_EQ(no_length, f2.guarded_list_length()); |
| 75 EXPECT_EQ(kDynamicCid, f2.guarded_cid()); | 74 EXPECT_EQ(kDynamicCid, f2.guarded_cid()); |
| 76 EXPECT_EQ(true, f2.is_nullable()); | 75 EXPECT_EQ(true, f2.is_nullable()); |
| 77 EXPECT_EQ(no_length, f3.guarded_list_length()); | 76 EXPECT_EQ(no_length, f3.guarded_list_length()); |
| 78 } | 77 } |
| 79 | 78 |
| 80 | |
| 81 TEST_CASE(GuardFieldFinalListTest) { | 79 TEST_CASE(GuardFieldFinalListTest) { |
| 82 const char* script_chars = | 80 const char* script_chars = |
| 83 "class A {\n" | 81 "class A {\n" |
| 84 " var f1 = 3.0;\n" | 82 " var f1 = 3.0;\n" |
| 85 " var f2 = 3;\n" | 83 " var f2 = 3;\n" |
| 86 " final f3 = new List(4);\n" | 84 " final f3 = new List(4);\n" |
| 87 " foo() {\n" | 85 " foo() {\n" |
| 88 " f1 = f1 + f1;\n" | 86 " f1 = f1 + f1;\n" |
| 89 " }\n" | 87 " }\n" |
| 90 " bar() {\n" | 88 " bar() {\n" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 EXPECT_EQ(kDoubleCid, f1.guarded_cid()); | 121 EXPECT_EQ(kDoubleCid, f1.guarded_cid()); |
| 124 EXPECT_EQ(false, f1.is_nullable()); | 122 EXPECT_EQ(false, f1.is_nullable()); |
| 125 EXPECT_EQ(no_length, f2.guarded_list_length()); | 123 EXPECT_EQ(no_length, f2.guarded_list_length()); |
| 126 EXPECT_EQ(kDynamicCid, f2.guarded_cid()); | 124 EXPECT_EQ(kDynamicCid, f2.guarded_cid()); |
| 127 EXPECT_EQ(true, f2.is_nullable()); | 125 EXPECT_EQ(true, f2.is_nullable()); |
| 128 EXPECT_EQ(4, f3.guarded_list_length()); | 126 EXPECT_EQ(4, f3.guarded_list_length()); |
| 129 EXPECT_EQ(kArrayCid, f3.guarded_cid()); | 127 EXPECT_EQ(kArrayCid, f3.guarded_cid()); |
| 130 EXPECT_EQ(false, f3.is_nullable()); | 128 EXPECT_EQ(false, f3.is_nullable()); |
| 131 } | 129 } |
| 132 | 130 |
| 133 | |
| 134 TEST_CASE(GuardFieldFinalVariableLengthListTest) { | 131 TEST_CASE(GuardFieldFinalVariableLengthListTest) { |
| 135 const char* script_chars = | 132 const char* script_chars = |
| 136 "class A {\n" | 133 "class A {\n" |
| 137 " var f1 = 3.0;\n" | 134 " var f1 = 3.0;\n" |
| 138 " var f2 = 3;\n" | 135 " var f2 = 3;\n" |
| 139 " final f3 = new List();\n" | 136 " final f3 = new List();\n" |
| 140 " foo() {\n" | 137 " foo() {\n" |
| 141 " f1 = f1 + f1;\n" | 138 " f1 = f1 + f1;\n" |
| 142 " }\n" | 139 " }\n" |
| 143 " bar() {\n" | 140 " bar() {\n" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 EXPECT_EQ(kDoubleCid, f1.guarded_cid()); | 173 EXPECT_EQ(kDoubleCid, f1.guarded_cid()); |
| 177 EXPECT_EQ(false, f1.is_nullable()); | 174 EXPECT_EQ(false, f1.is_nullable()); |
| 178 EXPECT_EQ(no_length, f2.guarded_list_length()); | 175 EXPECT_EQ(no_length, f2.guarded_list_length()); |
| 179 EXPECT_EQ(kDynamicCid, f2.guarded_cid()); | 176 EXPECT_EQ(kDynamicCid, f2.guarded_cid()); |
| 180 EXPECT_EQ(true, f2.is_nullable()); | 177 EXPECT_EQ(true, f2.is_nullable()); |
| 181 EXPECT_EQ(no_length, f3.guarded_list_length()); | 178 EXPECT_EQ(no_length, f3.guarded_list_length()); |
| 182 EXPECT_EQ(kGrowableObjectArrayCid, f3.guarded_cid()); | 179 EXPECT_EQ(kGrowableObjectArrayCid, f3.guarded_cid()); |
| 183 EXPECT_EQ(false, f3.is_nullable()); | 180 EXPECT_EQ(false, f3.is_nullable()); |
| 184 } | 181 } |
| 185 | 182 |
| 186 | |
| 187 TEST_CASE(GuardFieldConstructorTest) { | 183 TEST_CASE(GuardFieldConstructorTest) { |
| 188 const char* script_chars = | 184 const char* script_chars = |
| 189 "import 'dart:typed_data';\n" | 185 "import 'dart:typed_data';\n" |
| 190 "class A {\n" | 186 "class A {\n" |
| 191 " var f1 = 3.0;\n" | 187 " var f1 = 3.0;\n" |
| 192 " var f2 = 3;\n" | 188 " var f2 = 3;\n" |
| 193 " final f3;\n" | 189 " final f3;\n" |
| 194 " A(x) : f3 = x;\n" | 190 " A(x) : f3 = x;\n" |
| 195 " foo() {\n" | 191 " foo() {\n" |
| 196 " f1 = f1 + f1;\n" | 192 " f1 = f1 + f1;\n" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 EXPECT_EQ(false, f1.is_nullable()); | 230 EXPECT_EQ(false, f1.is_nullable()); |
| 235 EXPECT_EQ(no_length, f2.guarded_list_length()); | 231 EXPECT_EQ(no_length, f2.guarded_list_length()); |
| 236 EXPECT_EQ(kDynamicCid, f2.guarded_cid()); | 232 EXPECT_EQ(kDynamicCid, f2.guarded_cid()); |
| 237 EXPECT_EQ(true, f2.is_nullable()); | 233 EXPECT_EQ(true, f2.is_nullable()); |
| 238 const intptr_t length = 5; | 234 const intptr_t length = 5; |
| 239 EXPECT_EQ(length, f3.guarded_list_length()); | 235 EXPECT_EQ(length, f3.guarded_list_length()); |
| 240 EXPECT_EQ(kTypedDataFloat32ArrayCid, f3.guarded_cid()); | 236 EXPECT_EQ(kTypedDataFloat32ArrayCid, f3.guarded_cid()); |
| 241 EXPECT_EQ(false, f3.is_nullable()); | 237 EXPECT_EQ(false, f3.is_nullable()); |
| 242 } | 238 } |
| 243 | 239 |
| 244 | |
| 245 TEST_CASE(GuardFieldConstructor2Test) { | 240 TEST_CASE(GuardFieldConstructor2Test) { |
| 246 const char* script_chars = | 241 const char* script_chars = |
| 247 "import 'dart:typed_data';\n" | 242 "import 'dart:typed_data';\n" |
| 248 "class A {\n" | 243 "class A {\n" |
| 249 " final f3;\n" | 244 " final f3;\n" |
| 250 " A(x) : f3 = x;\n" | 245 " A(x) : f3 = x;\n" |
| 251 " foo() {\n" | 246 " foo() {\n" |
| 252 " }\n" | 247 " }\n" |
| 253 " bar() {\n" | 248 " bar() {\n" |
| 254 " }\n" | 249 " }\n" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 275 Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, NULL); | 270 Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, NULL); |
| 276 EXPECT_VALID(result); | 271 EXPECT_VALID(result); |
| 277 Field& f3 = Field::ZoneHandle(LookupField(lib, "A", "f3")); | 272 Field& f3 = Field::ZoneHandle(LookupField(lib, "A", "f3")); |
| 278 const intptr_t no_length = Field::kNoFixedLength; | 273 const intptr_t no_length = Field::kNoFixedLength; |
| 279 EXPECT_EQ(no_length, f3.guarded_list_length()); | 274 EXPECT_EQ(no_length, f3.guarded_list_length()); |
| 280 EXPECT_EQ(kTypedDataFloat32ArrayCid, f3.guarded_cid()); | 275 EXPECT_EQ(kTypedDataFloat32ArrayCid, f3.guarded_cid()); |
| 281 EXPECT_EQ(false, f3.is_nullable()); | 276 EXPECT_EQ(false, f3.is_nullable()); |
| 282 } | 277 } |
| 283 | 278 |
| 284 } // namespace dart | 279 } // namespace dart |
| OLD | NEW |