| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 "platform/assert.h" | 5 #include "platform/assert.h" |
| 6 #include "vm/assembler.h" | 6 #include "vm/assembler.h" |
| 7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
| 8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
| 9 #include "vm/isolate.h" | 9 #include "vm/isolate.h" |
| 10 #include "vm/object.h" | 10 #include "vm/object.h" |
| 11 #include "vm/object_store.h" | 11 #include "vm/object_store.h" |
| 12 #include "vm/simulator.h" | 12 #include "vm/simulator.h" |
| 13 #include "vm/symbols.h" | 13 #include "vm/symbols.h" |
| 14 #include "vm/unit_test.h" | 14 #include "vm/unit_test.h" |
| 15 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| 17 | 17 |
| 18 static RawClass* CreateDummyClass(const String& class_name, |
| 19 const Script& script) { |
| 20 const Class& cls = Class::Handle( |
| 21 Class::New(class_name, script, Scanner::kDummyTokenIndex)); |
| 22 cls.set_is_parsed(); // Dummy class for testing, mark as parsed. |
| 23 return cls.raw(); |
| 24 } |
| 25 |
| 26 |
| 18 TEST_CASE(Class) { | 27 TEST_CASE(Class) { |
| 19 // Allocate the class first. | 28 // Allocate the class first. |
| 20 const String& class_name = String::Handle(Symbols::New("MyClass")); | 29 const String& class_name = String::Handle(Symbols::New("MyClass")); |
| 21 const Script& script = Script::Handle(); | 30 const Script& script = Script::Handle(); |
| 22 const Class& cls = Class::Handle( | 31 const Class& cls = Class::Handle(CreateDummyClass(class_name, script)); |
| 23 Class::New(class_name, script, Scanner::kDummyTokenIndex)); | |
| 24 | 32 |
| 25 // Class has no fields. | 33 // Class has no fields. |
| 26 cls.SetFields(Object::empty_array()); | 34 cls.SetFields(Object::empty_array()); |
| 27 | 35 |
| 28 // Create and populate the function arrays. | 36 // Create and populate the function arrays. |
| 29 const Array& functions = Array::Handle(Array::New(6)); | 37 const Array& functions = Array::Handle(Array::New(6)); |
| 30 Function& function = Function::Handle(); | 38 Function& function = Function::Handle(); |
| 31 String& function_name = String::Handle(); | 39 String& function_name = String::Handle(); |
| 32 function_name = Symbols::New("foo"); | 40 function_name = Symbols::New("foo"); |
| 33 function = Function::New( | 41 function = Function::New( |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 function_name = String::New("bar"); | 107 function_name = String::New("bar"); |
| 100 function = cls.LookupDynamicFunction(function_name); | 108 function = cls.LookupDynamicFunction(function_name); |
| 101 EXPECT(!function.IsNull()); | 109 EXPECT(!function.IsNull()); |
| 102 EXPECT_EQ(kNumFixedParameters, function.num_fixed_parameters()); | 110 EXPECT_EQ(kNumFixedParameters, function.num_fixed_parameters()); |
| 103 EXPECT_EQ(kNumOptionalParameters, function.NumOptionalParameters()); | 111 EXPECT_EQ(kNumOptionalParameters, function.NumOptionalParameters()); |
| 104 | 112 |
| 105 const Array& interfaces = Array::Handle(Array::New(2)); | 113 const Array& interfaces = Array::Handle(Array::New(2)); |
| 106 Class& interface = Class::Handle(); | 114 Class& interface = Class::Handle(); |
| 107 String& interface_name = String::Handle(); | 115 String& interface_name = String::Handle(); |
| 108 interface_name = Symbols::New("Harley"); | 116 interface_name = Symbols::New("Harley"); |
| 109 interface = Class::New(interface_name, script, Scanner::kDummyTokenIndex); | 117 interface = CreateDummyClass(interface_name, script); |
| 110 interfaces.SetAt(0, Type::Handle(Type::NewNonParameterizedType(interface))); | 118 interfaces.SetAt(0, Type::Handle(Type::NewNonParameterizedType(interface))); |
| 111 interface_name = Symbols::New("Norton"); | 119 interface_name = Symbols::New("Norton"); |
| 112 interface = Class::New(interface_name, script, Scanner::kDummyTokenIndex); | 120 interface = CreateDummyClass(interface_name, script); |
| 113 interfaces.SetAt(1, Type::Handle(Type::NewNonParameterizedType(interface))); | 121 interfaces.SetAt(1, Type::Handle(Type::NewNonParameterizedType(interface))); |
| 114 cls.set_interfaces(interfaces); | 122 cls.set_interfaces(interfaces); |
| 115 cls.Finalize(); | 123 cls.Finalize(); |
| 116 } | 124 } |
| 117 | 125 |
| 118 | 126 |
| 119 TEST_CASE(TypeArguments) { | 127 TEST_CASE(TypeArguments) { |
| 120 const Type& type1 = Type::Handle(Type::Double()); | 128 const Type& type1 = Type::Handle(Type::Double()); |
| 121 const Type& type2 = Type::Handle(Type::StringType()); | 129 const Type& type2 = Type::Handle(Type::StringType()); |
| 122 const TypeArguments& type_arguments1 = TypeArguments::Handle( | 130 const TypeArguments& type_arguments1 = TypeArguments::Handle( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 iterator.Advance(); // Advance to end of stream. | 166 iterator.Advance(); // Advance to end of stream. |
| 159 EXPECT_EQ(Token::kEOS, iterator.CurrentTokenKind()); | 167 EXPECT_EQ(Token::kEOS, iterator.CurrentTokenKind()); |
| 160 } | 168 } |
| 161 | 169 |
| 162 | 170 |
| 163 TEST_CASE(InstanceClass) { | 171 TEST_CASE(InstanceClass) { |
| 164 // Allocate the class first. | 172 // Allocate the class first. |
| 165 String& class_name = String::Handle(Symbols::New("EmptyClass")); | 173 String& class_name = String::Handle(Symbols::New("EmptyClass")); |
| 166 Script& script = Script::Handle(); | 174 Script& script = Script::Handle(); |
| 167 const Class& empty_class = | 175 const Class& empty_class = |
| 168 Class::Handle(Class::New(class_name, script, Scanner::kDummyTokenIndex)); | 176 Class::Handle(CreateDummyClass(class_name, script)); |
| 169 | 177 |
| 170 // No functions and no super class for the EmptyClass. | 178 // No functions and no super class for the EmptyClass. |
| 171 empty_class.SetFields(Object::empty_array()); | 179 empty_class.SetFields(Object::empty_array()); |
| 172 empty_class.Finalize(); | 180 empty_class.Finalize(); |
| 173 EXPECT_EQ(kObjectAlignment, empty_class.instance_size()); | 181 EXPECT_EQ(kObjectAlignment, empty_class.instance_size()); |
| 174 Instance& instance = Instance::Handle(Instance::New(empty_class)); | 182 Instance& instance = Instance::Handle(Instance::New(empty_class)); |
| 175 EXPECT_EQ(empty_class.raw(), instance.clazz()); | 183 EXPECT_EQ(empty_class.raw(), instance.clazz()); |
| 176 | 184 |
| 177 class_name = Symbols::New("OneFieldClass"); | 185 class_name = Symbols::New("OneFieldClass"); |
| 178 const Class& one_field_class = | 186 const Class& one_field_class = |
| 179 Class::Handle(Class::New(class_name, script, Scanner::kDummyTokenIndex)); | 187 Class::Handle(CreateDummyClass(class_name, script)); |
| 180 | 188 |
| 181 // No functions and no super class for the OneFieldClass. | 189 // No functions and no super class for the OneFieldClass. |
| 182 const Array& one_fields = Array::Handle(Array::New(1)); | 190 const Array& one_fields = Array::Handle(Array::New(1)); |
| 183 const String& field_name = String::Handle(Symbols::New("the_field")); | 191 const String& field_name = String::Handle(Symbols::New("the_field")); |
| 184 const Field& field = Field::Handle( | 192 const Field& field = Field::Handle( |
| 185 Field::New(field_name, false, false, false, one_field_class, 0)); | 193 Field::New(field_name, false, false, false, one_field_class, 0)); |
| 186 one_fields.SetAt(0, field); | 194 one_fields.SetAt(0, field); |
| 187 one_field_class.SetFields(one_fields); | 195 one_field_class.SetFields(one_fields); |
| 188 one_field_class.Finalize(); | 196 one_field_class.Finalize(); |
| 189 intptr_t header_size = sizeof(RawObject); | 197 intptr_t header_size = sizeof(RawObject); |
| (...skipping 1976 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2166 EXPECT_EQ(1, var_c->index()); | 2174 EXPECT_EQ(1, var_c->index()); |
| 2167 EXPECT_EQ(parent_scope_context_level - local_scope_context_level, | 2175 EXPECT_EQ(parent_scope_context_level - local_scope_context_level, |
| 2168 var_c->owner()->context_level()); // Adjusted context level. | 2176 var_c->owner()->context_level()); // Adjusted context level. |
| 2169 } | 2177 } |
| 2170 | 2178 |
| 2171 | 2179 |
| 2172 TEST_CASE(Closure) { | 2180 TEST_CASE(Closure) { |
| 2173 // Allocate the class first. | 2181 // Allocate the class first. |
| 2174 const String& class_name = String::Handle(Symbols::New("MyClass")); | 2182 const String& class_name = String::Handle(Symbols::New("MyClass")); |
| 2175 const Script& script = Script::Handle(); | 2183 const Script& script = Script::Handle(); |
| 2176 const Class& cls = | 2184 const Class& cls = Class::Handle(CreateDummyClass(class_name, script)); |
| 2177 Class::Handle(Class::New(class_name, script, Scanner::kDummyTokenIndex)); | |
| 2178 const Array& functions = Array::Handle(Array::New(1)); | 2185 const Array& functions = Array::Handle(Array::New(1)); |
| 2179 | 2186 |
| 2180 const Context& context = Context::Handle(Context::New(0)); | 2187 const Context& context = Context::Handle(Context::New(0)); |
| 2181 Function& parent = Function::Handle(); | 2188 Function& parent = Function::Handle(); |
| 2182 const String& parent_name = String::Handle(Symbols::New("foo_papa")); | 2189 const String& parent_name = String::Handle(Symbols::New("foo_papa")); |
| 2183 parent = Function::New(parent_name, RawFunction::kRegularFunction, | 2190 parent = Function::New(parent_name, RawFunction::kRegularFunction, |
| 2184 false, false, false, false, cls, 0); | 2191 false, false, false, false, cls, 0); |
| 2185 functions.SetAt(0, parent); | 2192 functions.SetAt(0, parent); |
| 2186 cls.SetFunctions(functions); | 2193 cls.SetFunctions(functions); |
| 2187 | 2194 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2241 str3 = OneByteString::null(); | 2248 str3 = OneByteString::null(); |
| 2242 EXPECT(str3.IsString()); | 2249 EXPECT(str3.IsString()); |
| 2243 EXPECT(!str3.IsOneByteString()); | 2250 EXPECT(!str3.IsOneByteString()); |
| 2244 } | 2251 } |
| 2245 | 2252 |
| 2246 | 2253 |
| 2247 static Function* CreateFunction(const char* name) { | 2254 static Function* CreateFunction(const char* name) { |
| 2248 const String& class_name = String::Handle(Symbols::New("ownerClass")); | 2255 const String& class_name = String::Handle(Symbols::New("ownerClass")); |
| 2249 const Script& script = Script::Handle(); | 2256 const Script& script = Script::Handle(); |
| 2250 const Class& owner_class = | 2257 const Class& owner_class = |
| 2251 Class::Handle(Class::New(class_name, script, Scanner::kDummyTokenIndex)); | 2258 Class::Handle(CreateDummyClass(class_name, script)); |
| 2252 const String& function_name = String::ZoneHandle(Symbols::New(name)); | 2259 const String& function_name = String::ZoneHandle(Symbols::New(name)); |
| 2253 Function& function = Function::ZoneHandle( | 2260 Function& function = Function::ZoneHandle( |
| 2254 Function::New(function_name, RawFunction::kRegularFunction, | 2261 Function::New(function_name, RawFunction::kRegularFunction, |
| 2255 true, false, false, false, owner_class, 0)); | 2262 true, false, false, false, owner_class, 0)); |
| 2256 return &function; | 2263 return &function; |
| 2257 } | 2264 } |
| 2258 | 2265 |
| 2259 | 2266 |
| 2260 // Test for Code and Instruction object creation. | 2267 // Test for Code and Instruction object creation. |
| 2261 TEST_CASE(Code) { | 2268 TEST_CASE(Code) { |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2413 EXPECT_EQ(static_cast<uword>(80), pc_descs.PC(5)); | 2420 EXPECT_EQ(static_cast<uword>(80), pc_descs.PC(5)); |
| 2414 EXPECT_EQ(150, pc_descs.TokenPos(5)); | 2421 EXPECT_EQ(150, pc_descs.TokenPos(5)); |
| 2415 EXPECT_EQ(PcDescriptors::kOther, pc_descs.DescriptorKind(0)); | 2422 EXPECT_EQ(PcDescriptors::kOther, pc_descs.DescriptorKind(0)); |
| 2416 EXPECT_EQ(PcDescriptors::kDeopt, pc_descs.DescriptorKind(1)); | 2423 EXPECT_EQ(PcDescriptors::kDeopt, pc_descs.DescriptorKind(1)); |
| 2417 } | 2424 } |
| 2418 | 2425 |
| 2419 | 2426 |
| 2420 static RawClass* CreateTestClass(const char* name) { | 2427 static RawClass* CreateTestClass(const char* name) { |
| 2421 const String& class_name = String::Handle(Symbols::New(name)); | 2428 const String& class_name = String::Handle(Symbols::New(name)); |
| 2422 const Class& cls = Class::Handle( | 2429 const Class& cls = Class::Handle( |
| 2423 Class::New(class_name, Script::Handle(), Scanner::kDummyTokenIndex)); | 2430 CreateDummyClass(class_name, Script::Handle())); |
| 2424 return cls.raw(); | 2431 return cls.raw(); |
| 2425 } | 2432 } |
| 2426 | 2433 |
| 2427 | 2434 |
| 2428 static RawField* CreateTestField(const char* name) { | 2435 static RawField* CreateTestField(const char* name) { |
| 2429 const Class& cls = Class::Handle(CreateTestClass("global:")); | 2436 const Class& cls = Class::Handle(CreateTestClass("global:")); |
| 2430 const String& field_name = String::Handle(Symbols::New(name)); | 2437 const String& field_name = String::Handle(Symbols::New(name)); |
| 2431 const Field& field = | 2438 const Field& field = |
| 2432 Field::Handle(Field::New(field_name, true, false, false, cls, 0)); | 2439 Field::Handle(Field::New(field_name, true, false, false, cls, 0)); |
| 2433 return field.raw(); | 2440 return field.raw(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 2453 ASSERT((cls.raw() == ae66.raw()) || (cls.raw() == re44.raw())); | 2460 ASSERT((cls.raw() == ae66.raw()) || (cls.raw() == re44.raw())); |
| 2454 count++; | 2461 count++; |
| 2455 } | 2462 } |
| 2456 ASSERT(count == 2); | 2463 ASSERT(count == 2); |
| 2457 } | 2464 } |
| 2458 | 2465 |
| 2459 | 2466 |
| 2460 static RawFunction* GetDummyTarget(const char* name) { | 2467 static RawFunction* GetDummyTarget(const char* name) { |
| 2461 const String& function_name = String::Handle(Symbols::New(name)); | 2468 const String& function_name = String::Handle(Symbols::New(name)); |
| 2462 const Class& cls = Class::Handle( | 2469 const Class& cls = Class::Handle( |
| 2463 Class::New(function_name, Script::Handle(), Scanner::kDummyTokenIndex)); | 2470 CreateDummyClass(function_name, Script::Handle())); |
| 2464 const bool is_static = false; | 2471 const bool is_static = false; |
| 2465 const bool is_const = false; | 2472 const bool is_const = false; |
| 2466 const bool is_abstract = false; | 2473 const bool is_abstract = false; |
| 2467 const bool is_external = false; | 2474 const bool is_external = false; |
| 2468 return Function::New(function_name, | 2475 return Function::New(function_name, |
| 2469 RawFunction::kRegularFunction, | 2476 RawFunction::kRegularFunction, |
| 2470 is_static, | 2477 is_static, |
| 2471 is_const, | 2478 is_const, |
| 2472 is_abstract, | 2479 is_abstract, |
| 2473 is_external, | 2480 is_external, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2526 EXPECT_EQ(kSmiCid, test_class_ids[0]); | 2533 EXPECT_EQ(kSmiCid, test_class_ids[0]); |
| 2527 EXPECT_EQ(kSmiCid, test_class_ids[1]); | 2534 EXPECT_EQ(kSmiCid, test_class_ids[1]); |
| 2528 EXPECT_EQ(target1.raw(), test_target.raw()); | 2535 EXPECT_EQ(target1.raw(), test_target.raw()); |
| 2529 } | 2536 } |
| 2530 | 2537 |
| 2531 | 2538 |
| 2532 TEST_CASE(SubtypeTestCache) { | 2539 TEST_CASE(SubtypeTestCache) { |
| 2533 String& class_name = String::Handle(Symbols::New("EmptyClass")); | 2540 String& class_name = String::Handle(Symbols::New("EmptyClass")); |
| 2534 Script& script = Script::Handle(); | 2541 Script& script = Script::Handle(); |
| 2535 const Class& empty_class = | 2542 const Class& empty_class = |
| 2536 Class::Handle(Class::New(class_name, script, Scanner::kDummyTokenIndex)); | 2543 Class::Handle(CreateDummyClass(class_name, script)); |
| 2537 SubtypeTestCache& cache = SubtypeTestCache::Handle(SubtypeTestCache::New()); | 2544 SubtypeTestCache& cache = SubtypeTestCache::Handle(SubtypeTestCache::New()); |
| 2538 ASSERT(!cache.IsNull()); | 2545 ASSERT(!cache.IsNull()); |
| 2539 EXPECT_EQ(0, cache.NumberOfChecks()); | 2546 EXPECT_EQ(0, cache.NumberOfChecks()); |
| 2540 const TypeArguments& targ_0 = TypeArguments::Handle(TypeArguments::New(2)); | 2547 const TypeArguments& targ_0 = TypeArguments::Handle(TypeArguments::New(2)); |
| 2541 const TypeArguments& targ_1 = TypeArguments::Handle(TypeArguments::New(3)); | 2548 const TypeArguments& targ_1 = TypeArguments::Handle(TypeArguments::New(3)); |
| 2542 cache.AddCheck(empty_class.id(), targ_0, targ_1, Bool::True()); | 2549 cache.AddCheck(empty_class.id(), targ_0, targ_1, Bool::True()); |
| 2543 EXPECT_EQ(1, cache.NumberOfChecks()); | 2550 EXPECT_EQ(1, cache.NumberOfChecks()); |
| 2544 intptr_t test_class_id = -1; | 2551 intptr_t test_class_id = -1; |
| 2545 AbstractTypeArguments& test_targ_0 = AbstractTypeArguments::Handle(); | 2552 AbstractTypeArguments& test_targ_0 = AbstractTypeArguments::Handle(); |
| 2546 AbstractTypeArguments& test_targ_1 = AbstractTypeArguments::Handle(); | 2553 AbstractTypeArguments& test_targ_1 = AbstractTypeArguments::Handle(); |
| (...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3205 const Function& test7 = Function::Handle(GetFunction(class_a, "test7")); | 3212 const Function& test7 = Function::Handle(GetFunction(class_a, "test7")); |
| 3206 EXPECT_EQ(test1.SourceFingerprint(), test2.SourceFingerprint()); | 3213 EXPECT_EQ(test1.SourceFingerprint(), test2.SourceFingerprint()); |
| 3207 EXPECT_NE(test1.SourceFingerprint(), test3.SourceFingerprint()); | 3214 EXPECT_NE(test1.SourceFingerprint(), test3.SourceFingerprint()); |
| 3208 EXPECT_NE(test3.SourceFingerprint(), test4.SourceFingerprint()); | 3215 EXPECT_NE(test3.SourceFingerprint(), test4.SourceFingerprint()); |
| 3209 EXPECT_NE(test4.SourceFingerprint(), test5.SourceFingerprint()); | 3216 EXPECT_NE(test4.SourceFingerprint(), test5.SourceFingerprint()); |
| 3210 EXPECT_NE(test5.SourceFingerprint(), test6.SourceFingerprint()); | 3217 EXPECT_NE(test5.SourceFingerprint(), test6.SourceFingerprint()); |
| 3211 EXPECT_EQ(test6.SourceFingerprint(), test7.SourceFingerprint()); | 3218 EXPECT_EQ(test6.SourceFingerprint(), test7.SourceFingerprint()); |
| 3212 } | 3219 } |
| 3213 | 3220 |
| 3214 } // namespace dart | 3221 } // namespace dart |
| OLD | NEW |