| 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/isolate.h" | 8 #include "vm/isolate.h" |
| 9 #include "vm/object.h" | 9 #include "vm/object.h" |
| 10 #include "vm/object_store.h" | 10 #include "vm/object_store.h" |
| 11 #include "vm/unit_test.h" | 11 #include "vm/unit_test.h" |
| 12 | 12 |
| 13 namespace dart { | 13 namespace dart { |
| 14 | 14 |
| 15 TEST_CASE(Class) { | 15 TEST_CASE(Class) { |
| 16 // Allocate the class first. | 16 // Allocate the class first. |
| 17 const String& class_name = String::Handle(String::NewSymbol("MyClass")); | 17 const String& class_name = String::Handle(String::NewSymbol("MyClass")); |
| 18 const Script& script = Script::Handle(); | 18 const Script& script = Script::Handle(); |
| 19 const Class& cls = Class::Handle(Class::New(class_name, script)); | 19 const intptr_t kPos = 1; // Dummy token index in non-existing source. |
| 20 const Class& cls = Class::Handle(Class::New(class_name, script, kPos)); |
| 20 | 21 |
| 21 // Class has no fields. | 22 // Class has no fields. |
| 22 const Array& no_fields = Array::Handle(Array::Empty()); | 23 const Array& no_fields = Array::Handle(Array::Empty()); |
| 23 cls.SetFields(no_fields); | 24 cls.SetFields(no_fields); |
| 24 | 25 |
| 25 // Create and populate the function arrays. | 26 // Create and populate the function arrays. |
| 26 const Array& functions = Array::Handle(Array::New(6)); | 27 const Array& functions = Array::Handle(Array::New(6)); |
| 27 Function& function = Function::Handle(); | 28 Function& function = Function::Handle(); |
| 28 String& function_name = String::Handle(); | 29 String& function_name = String::Handle(); |
| 29 function_name = String::NewSymbol("foo"); | 30 function_name = String::NewSymbol("foo"); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 function_name = String::New("bar"); | 88 function_name = String::New("bar"); |
| 88 function = cls.LookupDynamicFunction(function_name); | 89 function = cls.LookupDynamicFunction(function_name); |
| 89 EXPECT(!function.IsNull()); | 90 EXPECT(!function.IsNull()); |
| 90 EXPECT_EQ(kNumFixedParameters, function.num_fixed_parameters()); | 91 EXPECT_EQ(kNumFixedParameters, function.num_fixed_parameters()); |
| 91 EXPECT_EQ(kNumOptionalParameters, function.num_optional_parameters()); | 92 EXPECT_EQ(kNumOptionalParameters, function.num_optional_parameters()); |
| 92 | 93 |
| 93 const Array& interfaces = Array::Handle(Array::New(2)); | 94 const Array& interfaces = Array::Handle(Array::New(2)); |
| 94 Class& interface = Class::Handle(); | 95 Class& interface = Class::Handle(); |
| 95 String& interface_name = String::Handle(); | 96 String& interface_name = String::Handle(); |
| 96 interface_name = String::NewSymbol("Harley"); | 97 interface_name = String::NewSymbol("Harley"); |
| 97 interface = Class::New(interface_name, script); | 98 interface = Class::New(interface_name, script, kPos); |
| 98 interfaces.SetAt(0, Type::Handle(Type::NewNonParameterizedType(interface))); | 99 interfaces.SetAt(0, Type::Handle(Type::NewNonParameterizedType(interface))); |
| 99 interface_name = String::NewSymbol("Norton"); | 100 interface_name = String::NewSymbol("Norton"); |
| 100 interface = Class::New(interface_name, script); | 101 interface = Class::New(interface_name, script, kPos); |
| 101 interfaces.SetAt(1, Type::Handle(Type::NewNonParameterizedType(interface))); | 102 interfaces.SetAt(1, Type::Handle(Type::NewNonParameterizedType(interface))); |
| 102 cls.set_interfaces(interfaces); | 103 cls.set_interfaces(interfaces); |
| 103 cls.Finalize(); | 104 cls.Finalize(); |
| 104 | 105 |
| 105 const Array& array = Array::Handle(cls.functions_cache()); | 106 const Array& array = Array::Handle(cls.functions_cache()); |
| 106 array.SetAt(0, function_name); | 107 array.SetAt(0, function_name); |
| 107 cls.set_functions_cache(array); | 108 cls.set_functions_cache(array); |
| 108 String& test_name = String::Handle(); | 109 String& test_name = String::Handle(); |
| 109 test_name ^= Array::Handle(cls.functions_cache()).At(0); | 110 test_name ^= Array::Handle(cls.functions_cache()).At(0); |
| 110 EXPECT(test_name.Equals(function_name)); | 111 EXPECT(test_name.Equals(function_name)); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 EXPECT_EQ(Token::kLPAREN, token_stream.KindAt(1)); | 146 EXPECT_EQ(Token::kLPAREN, token_stream.KindAt(1)); |
| 146 EXPECT_EQ(Token::kPERIOD, token_stream.KindAt(4)); | 147 EXPECT_EQ(Token::kPERIOD, token_stream.KindAt(4)); |
| 147 EXPECT_EQ(Token::kEOS, token_stream.KindAt(5)); | 148 EXPECT_EQ(Token::kEOS, token_stream.KindAt(5)); |
| 148 } | 149 } |
| 149 | 150 |
| 150 | 151 |
| 151 TEST_CASE(InstanceClass) { | 152 TEST_CASE(InstanceClass) { |
| 152 // Allocate the class first. | 153 // Allocate the class first. |
| 153 String& class_name = String::Handle(String::NewSymbol("EmptyClass")); | 154 String& class_name = String::Handle(String::NewSymbol("EmptyClass")); |
| 154 Script& script = Script::Handle(); | 155 Script& script = Script::Handle(); |
| 155 const Class& empty_class = Class::Handle(Class::New(class_name, script)); | 156 const intptr_t kPos = 1; // Dummy token index in non-existing source. |
| 157 const Class& empty_class = |
| 158 Class::Handle(Class::New(class_name, script, kPos)); |
| 156 | 159 |
| 157 // No functions and no super class for the EmptyClass. | 160 // No functions and no super class for the EmptyClass. |
| 158 const Array& no_fields = Array::Handle(Array::Empty()); | 161 const Array& no_fields = Array::Handle(Array::Empty()); |
| 159 empty_class.SetFields(no_fields); | 162 empty_class.SetFields(no_fields); |
| 160 empty_class.Finalize(); | 163 empty_class.Finalize(); |
| 161 EXPECT_EQ(kObjectAlignment, empty_class.instance_size()); | 164 EXPECT_EQ(kObjectAlignment, empty_class.instance_size()); |
| 162 Instance& instance = Instance::Handle(Instance::New(empty_class)); | 165 Instance& instance = Instance::Handle(Instance::New(empty_class)); |
| 163 EXPECT_EQ(empty_class.raw(), instance.clazz()); | 166 EXPECT_EQ(empty_class.raw(), instance.clazz()); |
| 164 | 167 |
| 165 class_name = String::NewSymbol("OneFieldClass"); | 168 class_name = String::NewSymbol("OneFieldClass"); |
| 166 const Class& one_field_class = Class::Handle(Class::New(class_name, script)); | 169 const Class& one_field_class = |
| 170 Class::Handle(Class::New(class_name, script, kPos)); |
| 167 | 171 |
| 168 // No functions and no super class for the OneFieldClass. | 172 // No functions and no super class for the OneFieldClass. |
| 169 const Array& one_fields = Array::Handle(Array::New(1)); | 173 const Array& one_fields = Array::Handle(Array::New(1)); |
| 170 const String& field_name = String::Handle(String::NewSymbol("the_field")); | 174 const String& field_name = String::Handle(String::NewSymbol("the_field")); |
| 171 const Field& field = Field::Handle(Field::New(field_name, false, false, 0)); | 175 const Field& field = Field::Handle(Field::New(field_name, false, false, 0)); |
| 172 one_fields.SetAt(0, field); | 176 one_fields.SetAt(0, field); |
| 173 one_field_class.SetFields(one_fields); | 177 one_field_class.SetFields(one_fields); |
| 174 one_field_class.Finalize(); | 178 one_field_class.Finalize(); |
| 175 intptr_t header_size = sizeof(RawObject); | 179 intptr_t header_size = sizeof(RawObject); |
| 176 EXPECT_EQ(Utils::RoundUp((header_size + (1 * kWordSize)), kObjectAlignment), | 180 EXPECT_EQ(Utils::RoundUp((header_size + (1 * kWordSize)), kObjectAlignment), |
| 177 one_field_class.instance_size()); | 181 one_field_class.instance_size()); |
| 178 EXPECT_EQ(header_size, field.Offset()); | 182 EXPECT_EQ(header_size, field.Offset()); |
| 179 } | 183 } |
| 180 | 184 |
| 181 | 185 |
| 182 TEST_CASE(Interface) { | 186 TEST_CASE(Interface) { |
| 183 String& class_name = String::Handle(String::NewSymbol("EmptyClass")); | 187 String& class_name = String::Handle(String::NewSymbol("EmptyClass")); |
| 184 Script& script = Script::Handle(); | 188 Script& script = Script::Handle(); |
| 185 const Class& factory_class = Class::Handle(Class::New(class_name, script)); | 189 const intptr_t kPos = 1; // Dummy token index in non-existing source. |
| 190 const Class& factory_class = |
| 191 Class::Handle(Class::New(class_name, script, kPos)); |
| 186 const Array& no_fields = Array::Handle(Array::Empty()); | 192 const Array& no_fields = Array::Handle(Array::Empty()); |
| 187 // Finalizes the class. | 193 // Finalizes the class. |
| 188 factory_class.SetFields(no_fields); | 194 factory_class.SetFields(no_fields); |
| 189 | 195 |
| 190 String& interface_name = String::Handle(String::NewSymbol("MyInterface")); | 196 String& interface_name = String::Handle(String::NewSymbol("MyInterface")); |
| 191 const Class& interface = | 197 const Class& interface = |
| 192 Class::Handle(Class::NewInterface(interface_name, script)); | 198 Class::Handle(Class::NewInterface(interface_name, script, kPos)); |
| 193 EXPECT(interface.is_interface()); | 199 EXPECT(interface.is_interface()); |
| 194 EXPECT(!factory_class.is_interface()); | 200 EXPECT(!factory_class.is_interface()); |
| 195 EXPECT(!interface.HasFactoryClass()); | 201 EXPECT(!interface.HasFactoryClass()); |
| 196 interface.set_factory_class(factory_class); | 202 interface.set_factory_class(factory_class); |
| 197 EXPECT_EQ(factory_class.raw(), interface.FactoryClass()); | 203 EXPECT_EQ(factory_class.raw(), interface.FactoryClass()); |
| 198 } | 204 } |
| 199 | 205 |
| 200 | 206 |
| 201 TEST_CASE(Smi) { | 207 TEST_CASE(Smi) { |
| 202 const Smi& smi = Smi::Handle(Smi::New(5)); | 208 const Smi& smi = Smi::Handle(Smi::New(5)); |
| (...skipping 1934 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2137 EXPECT_EQ(1, var_c->index()); | 2143 EXPECT_EQ(1, var_c->index()); |
| 2138 EXPECT_EQ(parent_scope_context_level - local_scope_context_level, | 2144 EXPECT_EQ(parent_scope_context_level - local_scope_context_level, |
| 2139 var_c->owner()->context_level()); // Adjusted context level. | 2145 var_c->owner()->context_level()); // Adjusted context level. |
| 2140 } | 2146 } |
| 2141 | 2147 |
| 2142 | 2148 |
| 2143 TEST_CASE(Closure) { | 2149 TEST_CASE(Closure) { |
| 2144 // Allocate the class first. | 2150 // Allocate the class first. |
| 2145 const String& class_name = String::Handle(String::NewSymbol("MyClass")); | 2151 const String& class_name = String::Handle(String::NewSymbol("MyClass")); |
| 2146 const Script& script = Script::Handle(); | 2152 const Script& script = Script::Handle(); |
| 2147 const Class& cls = Class::Handle(Class::New(class_name, script)); | 2153 const intptr_t kPos = 1; // Dummy token index in non-existing source. |
| 2154 const Class& cls = Class::Handle(Class::New(class_name, script, kPos)); |
| 2148 const Array& functions = Array::Handle(Array::New(1)); | 2155 const Array& functions = Array::Handle(Array::New(1)); |
| 2149 | 2156 |
| 2150 const Context& context = Context::Handle(Context::New(0)); | 2157 const Context& context = Context::Handle(Context::New(0)); |
| 2151 Function& parent = Function::Handle(); | 2158 Function& parent = Function::Handle(); |
| 2152 const String& parent_name = String::Handle(String::NewSymbol("foo_papa")); | 2159 const String& parent_name = String::Handle(String::NewSymbol("foo_papa")); |
| 2153 parent = Function::New(parent_name, RawFunction::kFunction, false, false, 0); | 2160 parent = Function::New(parent_name, RawFunction::kFunction, false, false, 0); |
| 2154 functions.SetAt(0, parent); | 2161 functions.SetAt(0, parent); |
| 2155 cls.SetFunctions(functions); | 2162 cls.SetFunctions(functions); |
| 2156 | 2163 |
| 2157 Function& function = Function::Handle(); | 2164 Function& function = Function::Handle(); |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2342 EXPECT_EQ(3, pc_descs.TryIndex(5)); | 2349 EXPECT_EQ(3, pc_descs.TryIndex(5)); |
| 2343 EXPECT_EQ(static_cast<uword>(80), pc_descs.PC(5)); | 2350 EXPECT_EQ(static_cast<uword>(80), pc_descs.PC(5)); |
| 2344 EXPECT_EQ(150, pc_descs.TokenIndex(5)); | 2351 EXPECT_EQ(150, pc_descs.TokenIndex(5)); |
| 2345 EXPECT_EQ(PcDescriptors::kOther, pc_descs.DescriptorKind(0)); | 2352 EXPECT_EQ(PcDescriptors::kOther, pc_descs.DescriptorKind(0)); |
| 2346 EXPECT_EQ(PcDescriptors::kDeopt, pc_descs.DescriptorKind(1)); | 2353 EXPECT_EQ(PcDescriptors::kDeopt, pc_descs.DescriptorKind(1)); |
| 2347 } | 2354 } |
| 2348 | 2355 |
| 2349 | 2356 |
| 2350 static RawClass* CreateTestClass(const char* name) { | 2357 static RawClass* CreateTestClass(const char* name) { |
| 2351 const String& class_name = String::Handle(String::NewSymbol(name)); | 2358 const String& class_name = String::Handle(String::NewSymbol(name)); |
| 2352 const Class& cls = Class::Handle(Class::New(class_name, Script::Handle())); | 2359 const intptr_t kPos = 1; // Dummy token index in non-existing source. |
| 2360 const Class& cls = |
| 2361 Class::Handle(Class::New(class_name, Script::Handle(), kPos)); |
| 2353 return cls.raw(); | 2362 return cls.raw(); |
| 2354 } | 2363 } |
| 2355 | 2364 |
| 2356 | 2365 |
| 2357 static RawField* CreateTestField(const char* name) { | 2366 static RawField* CreateTestField(const char* name) { |
| 2358 const Class& cls = Class::Handle(CreateTestClass("global:")); | 2367 const Class& cls = Class::Handle(CreateTestClass("global:")); |
| 2359 const String& field_name = String::Handle(String::NewSymbol(name)); | 2368 const String& field_name = String::Handle(String::NewSymbol(name)); |
| 2360 const Field& field = Field::Handle(Field::New(field_name, true, false, 0)); | 2369 const Field& field = Field::Handle(Field::New(field_name, true, false, 0)); |
| 2361 field.set_owner(cls); | 2370 field.set_owner(cls); |
| 2362 return field.raw(); | 2371 return field.raw(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 2381 cls = iterator.GetNextClass(); | 2390 cls = iterator.GetNextClass(); |
| 2382 ASSERT((cls.raw() == ae66.raw()) || (cls.raw() == re44.raw())); | 2391 ASSERT((cls.raw() == ae66.raw()) || (cls.raw() == re44.raw())); |
| 2383 count++; | 2392 count++; |
| 2384 } | 2393 } |
| 2385 ASSERT(count == 2); | 2394 ASSERT(count == 2); |
| 2386 } | 2395 } |
| 2387 | 2396 |
| 2388 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 2397 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 2389 | 2398 |
| 2390 } // namespace dart | 2399 } // namespace dart |
| OLD | NEW |