Chromium Code Reviews| 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 "vm/object.h" | 5 #include "vm/object.h" |
| 6 | 6 |
| 7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
| 8 #include "vm/assembler.h" | 8 #include "vm/assembler.h" |
| 9 #include "vm/bigint_operations.h" | 9 #include "vm/bigint_operations.h" |
| 10 #include "vm/bootstrap.h" | 10 #include "vm/bootstrap.h" |
| (...skipping 4361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4372 array.SetAt(4, errstr); | 4372 array.SetAt(4, errstr); |
| 4373 errstr = conflict.url(); | 4373 errstr = conflict.url(); |
| 4374 array.SetAt(5, errstr); | 4374 array.SetAt(5, errstr); |
| 4375 errstr = String::New("'"); | 4375 errstr = String::New("'"); |
| 4376 array.SetAt(6, errstr); | 4376 array.SetAt(6, errstr); |
| 4377 errstr = String::ConcatAll(array); | 4377 errstr = String::ConcatAll(array); |
| 4378 return errstr.raw(); | 4378 return errstr.raw(); |
| 4379 } | 4379 } |
| 4380 | 4380 |
| 4381 | 4381 |
| 4382 RawString* Library::FindDuplicateDefinition(Library* conflicting_lib) const { | 4382 RawString* Library::FindDuplicateDefinition() const { |
| 4383 DictionaryIterator it(*this); | 4383 DictionaryIterator it(*this); |
| 4384 Object& obj = Object::Handle(); | 4384 Object& obj = Object::Handle(); |
| 4385 Class& cls = Class::Handle(); | 4385 Class& cls = Class::Handle(); |
| 4386 Function& func = Function::Handle(); | 4386 Function& func = Function::Handle(); |
| 4387 Field& field = Field::Handle(); | 4387 Field& field = Field::Handle(); |
| 4388 String& entry_name = String::Handle(); | 4388 String& entry_name = String::Handle(); |
| 4389 String& error_message = String::Handle(); | |
| 4390 Library& conflicting_lib = Library::Handle(); | |
| 4391 LibraryPrefix& lib_prefix = LibraryPrefix::Handle(); | |
| 4389 while (it.HasNext()) { | 4392 while (it.HasNext()) { |
| 4390 obj = it.GetNext(); | 4393 obj = it.GetNext(); |
| 4391 ASSERT(!obj.IsNull()); | 4394 ASSERT(!obj.IsNull()); |
| 4392 if (obj.IsClass()) { | 4395 if (obj.IsClass()) { |
| 4393 cls ^= obj.raw(); | 4396 cls ^= obj.raw(); |
| 4394 if (cls.IsCanonicalSignatureClass()) { | 4397 if (cls.IsCanonicalSignatureClass()) { |
| 4395 continue; | 4398 continue; |
| 4396 } | 4399 } |
| 4397 entry_name = cls.Name(); | 4400 entry_name = cls.Name(); |
| 4398 } else if (obj.IsFunction()) { | 4401 } else if (obj.IsFunction()) { |
| 4399 func ^= obj.raw(); | 4402 func ^= obj.raw(); |
| 4400 entry_name = func.name(); | 4403 entry_name = func.name(); |
| 4401 } else if (obj.IsField()) { | 4404 } else if (obj.IsField()) { |
| 4402 field ^= obj.raw(); | 4405 field ^= obj.raw(); |
| 4403 entry_name = field.name(); | 4406 entry_name = field.name(); |
| 4407 } else if (obj.IsLibraryPrefix()) { | |
| 4408 // For library prefix objects we check to make sure there are no | |
| 4409 // duplicate definitions within the libraries imported using this | |
| 4410 // prefix. | |
| 4411 lib_prefix ^= obj.raw(); | |
| 4412 error_message = lib_prefix.CheckForDuplicateDefinition(); | |
| 4413 if (!error_message.IsNull()) { | |
| 4414 return error_message.raw(); | |
| 4415 } | |
| 4416 // We don't check library prefixes defined in this library for | |
| 4417 // conflicts because they are not visible in the importing scope and | |
| 4418 // hence cannot cause any duplicate definitions. | |
| 4419 continue; | |
| 4404 } else { | 4420 } else { |
| 4405 // We don't check for library prefixes defined in this library because | 4421 UNREACHABLE(); |
| 4406 // they are not visible in the importing scope and hence cannot | |
| 4407 // cause any duplicate definitions. | |
| 4408 continue; | |
| 4409 } | 4422 } |
| 4410 *conflicting_lib = LookupObjectInImporter(entry_name); | 4423 conflicting_lib = LookupObjectInImporter(entry_name); |
| 4411 if (!conflicting_lib->IsNull()) { | 4424 if (!conflicting_lib.IsNull()) { |
| 4412 return entry_name.raw(); | 4425 return this->DuplicateDefineErrorString(entry_name, conflicting_lib); |
| 4413 } | 4426 } |
| 4414 } | 4427 } |
| 4415 return String::null(); | 4428 return String::null(); |
| 4416 } | 4429 } |
| 4417 | 4430 |
| 4418 | 4431 |
| 4419 RawClass* Library::LookupClass(const String& name) const { | 4432 RawClass* Library::LookupClass(const String& name) const { |
| 4420 Object& obj = Object::Handle(LookupObject(name)); | 4433 Object& obj = Object::Handle(LookupObject(name)); |
| 4421 if (!obj.IsNull() && obj.IsClass()) { | 4434 if (!obj.IsNull() && obj.IsClass()) { |
| 4422 return Class::CheckedHandle(obj.raw()).raw(); | 4435 return Class::CheckedHandle(obj.raw()).raw(); |
| 4423 } | 4436 } |
| 4424 return Class::null(); | 4437 return Class::null(); |
| 4425 } | 4438 } |
| 4426 | 4439 |
| 4427 | 4440 |
| 4428 RawClass* Library::LookupLocalClass(const String& name) const { | 4441 RawClass* Library::LookupLocalClass(const String& name) const { |
| 4429 Object& obj = Object::Handle(LookupLocalObject(name)); | 4442 Object& obj = Object::Handle(LookupLocalObject(name)); |
| 4430 if (!obj.IsNull() && obj.IsClass()) { | 4443 if (!obj.IsNull() && obj.IsClass()) { |
| 4431 return Class::CheckedHandle(obj.raw()).raw(); | 4444 return Class::CheckedHandle(obj.raw()).raw(); |
| 4432 } | 4445 } |
| 4433 return Class::null(); | 4446 return Class::null(); |
| 4434 } | 4447 } |
| 4435 | 4448 |
| 4436 | 4449 |
| 4450 RawLibraryPrefix* Library::LookupLocalLibraryPrefix(const String& name) const { | |
| 4451 Object& obj = Object::Handle(LookupLocalObject(name)); | |
| 4452 if (!obj.IsNull() && obj.IsLibraryPrefix()) { | |
| 4453 return LibraryPrefix::CheckedHandle(obj.raw()).raw(); | |
| 4454 } | |
| 4455 return LibraryPrefix::null(); | |
| 4456 } | |
| 4457 | |
| 4458 | |
| 4437 void Library::AddAnonymousClass(const Class& cls) const { | 4459 void Library::AddAnonymousClass(const Class& cls) const { |
| 4438 intptr_t num_anonymous = this->raw_ptr()->num_anonymous_; | 4460 intptr_t num_anonymous = this->raw_ptr()->num_anonymous_; |
| 4439 Array& anon_array = Array::Handle(this->raw_ptr()->anonymous_classes_); | 4461 Array& anon_array = Array::Handle(this->raw_ptr()->anonymous_classes_); |
| 4440 if (num_anonymous == anon_array.Length()) { | 4462 if (num_anonymous == anon_array.Length()) { |
| 4441 intptr_t new_len = (num_anonymous == 0) ? 4 : num_anonymous * 2; | 4463 intptr_t new_len = (num_anonymous == 0) ? 4 : num_anonymous * 2; |
| 4442 anon_array = Array::Grow(anon_array, new_len); | 4464 anon_array = Array::Grow(anon_array, new_len); |
| 4443 StorePointer(&raw_ptr()->anonymous_classes_, anon_array.raw()); | 4465 StorePointer(&raw_ptr()->anonymous_classes_, anon_array.raw()); |
| 4444 } | 4466 } |
| 4445 anon_array.SetAt(num_anonymous, cls); | 4467 anon_array.SetAt(num_anonymous, cls); |
| 4446 num_anonymous++; | 4468 num_anonymous++; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4641 } | 4663 } |
| 4642 | 4664 |
| 4643 | 4665 |
| 4644 RawString* Library::CheckForDuplicateDefinition() { | 4666 RawString* Library::CheckForDuplicateDefinition() { |
| 4645 Library& lib = Library::Handle(); | 4667 Library& lib = Library::Handle(); |
| 4646 Isolate* isolate = Isolate::Current(); | 4668 Isolate* isolate = Isolate::Current(); |
| 4647 ASSERT(isolate != NULL); | 4669 ASSERT(isolate != NULL); |
| 4648 ObjectStore* object_store = isolate->object_store(); | 4670 ObjectStore* object_store = isolate->object_store(); |
| 4649 ASSERT(object_store != NULL); | 4671 ASSERT(object_store != NULL); |
| 4650 lib ^= object_store->registered_libraries(); | 4672 lib ^= object_store->registered_libraries(); |
| 4651 String& entry_name = String::Handle(); | 4673 String& error_message = String::Handle(); |
| 4652 Library& conflicting_lib = Library::Handle(); | |
| 4653 while (!lib.IsNull()) { | 4674 while (!lib.IsNull()) { |
| 4654 entry_name = lib.FindDuplicateDefinition(&conflicting_lib); | 4675 error_message = lib.FindDuplicateDefinition(); |
| 4655 if (!entry_name.IsNull()) { | 4676 if (!error_message.IsNull()) { |
| 4656 return lib.DuplicateDefineErrorString(entry_name, conflicting_lib); | 4677 return error_message.raw(); |
| 4657 } | 4678 } |
| 4658 lib ^= lib.next_registered(); | 4679 lib ^= lib.next_registered(); |
| 4659 } | 4680 } |
| 4660 return String::null(); | 4681 return String::null(); |
| 4661 } | 4682 } |
| 4662 | 4683 |
| 4663 | 4684 |
| 4664 bool Library::IsKeyUsed(intptr_t key) { | 4685 bool Library::IsKeyUsed(intptr_t key) { |
| 4665 intptr_t lib_key; | 4686 intptr_t lib_key; |
| 4666 Library& lib = Library::Handle(); | 4687 Library& lib = Library::Handle(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4716 const char* kFormat = "Library:'%s'"; | 4737 const char* kFormat = "Library:'%s'"; |
| 4717 const String& name = String::Handle(url()); | 4738 const String& name = String::Handle(url()); |
| 4718 intptr_t len = OS::SNPrint(NULL, 0, kFormat, name.ToCString()) + 1; | 4739 intptr_t len = OS::SNPrint(NULL, 0, kFormat, name.ToCString()) + 1; |
| 4719 char* chars = reinterpret_cast<char*>( | 4740 char* chars = reinterpret_cast<char*>( |
| 4720 Isolate::Current()->current_zone()->Allocate(len)); | 4741 Isolate::Current()->current_zone()->Allocate(len)); |
| 4721 OS::SNPrint(chars, len, kFormat, name.ToCString()); | 4742 OS::SNPrint(chars, len, kFormat, name.ToCString()); |
| 4722 return chars; | 4743 return chars; |
| 4723 } | 4744 } |
| 4724 | 4745 |
| 4725 | 4746 |
| 4747 RawLibrary* LibraryPrefix::GetLibrary(int index) const { | |
| 4748 Library& lib = Library::Handle(); | |
| 4749 if ((index >= 0) || (index < num_libs())) { | |
| 4750 Array& libs = Array::Handle(libraries()); | |
| 4751 lib ^= libs.At(index); | |
| 4752 } | |
| 4753 return lib.raw(); | |
| 4754 } | |
| 4755 | |
| 4756 | |
| 4757 void LibraryPrefix::AddLibrary(const Library& library) const { | |
| 4758 Library& lib = Library::Handle(); | |
| 4759 intptr_t num_current_libs = num_libs(); | |
| 4760 | |
| 4761 // First check if the library is already in the list of libraries imported. | |
| 4762 if (num_current_libs > 0) { | |
| 4763 const String& url = String::Handle(library.url()); | |
| 4764 String& lib_url = String::Handle(); | |
| 4765 for (intptr_t i = 0; i < num_current_libs; i++) { | |
| 4766 lib = GetLibrary(i); | |
| 4767 ASSERT(!lib.IsNull()); | |
| 4768 lib_url = lib.url(); | |
| 4769 if (url.Equals(lib_url)) { | |
| 4770 return; // Library already imported with same prefix. | |
| 4771 } | |
| 4772 } | |
| 4773 } | |
| 4774 | |
| 4775 // The library needs to be added to the list. Grow the list if it is full. | |
| 4776 Array& libs = Array::Handle(libraries()); | |
| 4777 const intptr_t length = (libs.IsNull()) ? 0 : libs.Length(); | |
| 4778 if (num_current_libs < length) { | |
| 4779 libs.SetAt(num_current_libs, library); | |
|
hausner
2012/02/15 18:04:27
If you handle the case of growing the array first,
siva
2012/02/15 20:52:47
Good point.
Restructured as suggested.
On 2012/0
| |
| 4780 } else { | |
| 4781 ASSERT(num_current_libs == length); | |
| 4782 const intptr_t new_length = length + kIncrementSize; | |
| 4783 const Array& new_libs = | |
| 4784 Array::Handle(Array::Grow(libs, new_length, Heap::kOld)); | |
| 4785 new_libs.SetAt(num_current_libs, library); | |
| 4786 set_libraries(new_libs); | |
| 4787 } | |
| 4788 set_num_libs(num_current_libs + 1); | |
| 4789 } | |
| 4790 | |
| 4791 | |
| 4792 RawClass* LibraryPrefix::LookupLocalClass(const String& class_name) const { | |
| 4793 Array& libs = Array::Handle(libraries()); | |
| 4794 Class& resolved_class = Class::Handle(); | |
| 4795 Library& lib = Library::Handle(); | |
| 4796 for (intptr_t i = 0; i < num_libs(); i++) { | |
| 4797 lib ^= libs.At(i); | |
| 4798 ASSERT(!lib.IsNull()); | |
| 4799 resolved_class = lib.LookupLocalClass(class_name); | |
| 4800 if (!resolved_class.IsNull()) { | |
| 4801 return resolved_class.raw(); | |
| 4802 } | |
| 4803 } | |
| 4804 return Class::null(); | |
| 4805 } | |
| 4806 | |
| 4807 | |
| 4726 RawLibraryPrefix* LibraryPrefix::New() { | 4808 RawLibraryPrefix* LibraryPrefix::New() { |
| 4727 const Class& library_prefix_class = | 4809 const Class& library_prefix_class = |
| 4728 Class::Handle(Object::library_prefix_class()); | 4810 Class::Handle(Object::library_prefix_class()); |
| 4729 RawObject* raw = Object::Allocate(library_prefix_class, | 4811 RawObject* raw = Object::Allocate(library_prefix_class, |
| 4730 LibraryPrefix::InstanceSize(), | 4812 LibraryPrefix::InstanceSize(), |
| 4731 Heap::kOld); | 4813 Heap::kOld); |
| 4732 return reinterpret_cast<RawLibraryPrefix*>(raw); | 4814 return reinterpret_cast<RawLibraryPrefix*>(raw); |
| 4733 } | 4815 } |
| 4734 | 4816 |
| 4735 | 4817 |
| 4736 RawLibraryPrefix* LibraryPrefix::New(const String& name, const Library& lib) { | 4818 RawLibraryPrefix* LibraryPrefix::New(const String& name, const Library& lib) { |
| 4737 const LibraryPrefix& result = LibraryPrefix::Handle(LibraryPrefix::New()); | 4819 const LibraryPrefix& result = LibraryPrefix::Handle(LibraryPrefix::New()); |
| 4738 result.set_name(name); | 4820 result.set_name(name); |
| 4739 result.set_library(lib); | 4821 result.set_num_libs(0); |
| 4822 result.AddLibrary(lib); | |
| 4740 return result.raw(); | 4823 return result.raw(); |
| 4741 } | 4824 } |
| 4742 | 4825 |
| 4743 | 4826 |
| 4744 const char* LibraryPrefix::ToCString() const { | 4827 const char* LibraryPrefix::ToCString() const { |
| 4745 const char* kFormat = "LibraryPrefix:'%s'"; | 4828 const char* kFormat = "LibraryPrefix:'%s'"; |
| 4746 const String& prefix = String::Handle(name()); | 4829 const String& prefix = String::Handle(name()); |
| 4747 intptr_t len = OS::SNPrint(NULL, 0, kFormat, prefix.ToCString()) + 1; | 4830 intptr_t len = OS::SNPrint(NULL, 0, kFormat, prefix.ToCString()) + 1; |
| 4748 char* chars = reinterpret_cast<char*>( | 4831 char* chars = reinterpret_cast<char*>( |
| 4749 Isolate::Current()->current_zone()->Allocate(len)); | 4832 Isolate::Current()->current_zone()->Allocate(len)); |
| 4750 OS::SNPrint(chars, len, kFormat, prefix.ToCString()); | 4833 OS::SNPrint(chars, len, kFormat, prefix.ToCString()); |
| 4751 return chars; | 4834 return chars; |
| 4752 } | 4835 } |
| 4753 | 4836 |
| 4754 | 4837 |
| 4838 RawString* LibraryPrefix::CheckForDuplicateDefinition() const { | |
| 4839 Library& lib = Library::Handle(); | |
| 4840 Library& conflicting_lib = Library::Handle(); | |
| 4841 Object& obj = Object::Handle(); | |
| 4842 Class& cls = Class::Handle(); | |
| 4843 Function& func = Function::Handle(); | |
| 4844 Field& field = Field::Handle(); | |
| 4845 String& entry_name = String::Handle(); | |
| 4846 | |
| 4847 for (intptr_t i = 0; i < num_libs(); i++) { | |
| 4848 lib = GetLibrary(i); | |
| 4849 ASSERT(!lib.IsNull()); | |
| 4850 DictionaryIterator it(lib); | |
| 4851 while (it.HasNext()) { | |
| 4852 obj = it.GetNext(); | |
| 4853 ASSERT(!obj.IsNull()); | |
| 4854 if (obj.IsClass()) { | |
| 4855 cls ^= obj.raw(); | |
| 4856 if (cls.IsCanonicalSignatureClass()) { | |
| 4857 continue; | |
| 4858 } | |
| 4859 entry_name = cls.Name(); | |
| 4860 } else if (obj.IsFunction()) { | |
| 4861 func ^= obj.raw(); | |
| 4862 entry_name = func.name(); | |
| 4863 } else if (obj.IsField()) { | |
| 4864 field ^= obj.raw(); | |
| 4865 entry_name = field.name(); | |
| 4866 } else { | |
| 4867 // We don't check library prefixes defined in this library for | |
| 4868 // conflicts because they are not visible in the importing scope and | |
| 4869 // hence cannot cause any duplicate definitions. | |
| 4870 continue; | |
| 4871 } | |
| 4872 for (intptr_t j = i + 1; j < num_libs(); j++) { | |
| 4873 conflicting_lib = GetLibrary(j); | |
| 4874 ASSERT(!conflicting_lib.IsNull()); | |
| 4875 // Check if name is found in the local scope of the library. | |
| 4876 obj = conflicting_lib.LookupLocalObject(entry_name); | |
| 4877 if (!obj.IsNull()) { | |
| 4878 return lib.DuplicateDefineErrorString(entry_name, conflicting_lib); | |
| 4879 } | |
| 4880 } | |
| 4881 } | |
| 4882 } | |
| 4883 return String::null(); | |
| 4884 } | |
| 4885 | |
| 4886 | |
| 4755 void LibraryPrefix::set_name(const String& value) const { | 4887 void LibraryPrefix::set_name(const String& value) const { |
| 4756 ASSERT(value.IsSymbol()); | 4888 ASSERT(value.IsSymbol()); |
| 4757 StorePointer(&raw_ptr()->name_, value.raw()); | 4889 StorePointer(&raw_ptr()->name_, value.raw()); |
| 4758 } | 4890 } |
| 4759 | 4891 |
| 4760 | 4892 |
| 4761 void LibraryPrefix::set_library(const Library& value) const { | 4893 void LibraryPrefix::set_libraries(const Array& value) const { |
| 4762 StorePointer(&raw_ptr()->library_, value.raw()); | 4894 StorePointer(&raw_ptr()->libraries_, value.raw()); |
| 4895 } | |
| 4896 | |
| 4897 | |
| 4898 void LibraryPrefix::set_num_libs(intptr_t value) const { | |
| 4899 raw_ptr()->num_libs_ = value; | |
| 4763 } | 4900 } |
| 4764 | 4901 |
| 4765 | 4902 |
| 4766 RawError* Library::CompileAll() { | 4903 RawError* Library::CompileAll() { |
| 4767 Error& error = Error::Handle(); | 4904 Error& error = Error::Handle(); |
| 4768 Library& lib = Library::Handle( | 4905 Library& lib = Library::Handle( |
| 4769 Isolate::Current()->object_store()->registered_libraries()); | 4906 Isolate::Current()->object_store()->registered_libraries()); |
| 4770 Class& cls = Class::Handle(); | 4907 Class& cls = Class::Handle(); |
| 4771 while (!lib.IsNull()) { | 4908 while (!lib.IsNull()) { |
| 4772 ClassDictionaryIterator it(lib); | 4909 ClassDictionaryIterator it(lib); |
| (...skipping 3287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 8060 const String& str = String::Handle(pattern()); | 8197 const String& str = String::Handle(pattern()); |
| 8061 const char* format = "JSRegExp: pattern=%s flags=%s"; | 8198 const char* format = "JSRegExp: pattern=%s flags=%s"; |
| 8062 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); | 8199 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); |
| 8063 char* chars = reinterpret_cast<char*>( | 8200 char* chars = reinterpret_cast<char*>( |
| 8064 Isolate::Current()->current_zone()->Allocate(len + 1)); | 8201 Isolate::Current()->current_zone()->Allocate(len + 1)); |
| 8065 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); | 8202 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); |
| 8066 return chars; | 8203 return chars; |
| 8067 } | 8204 } |
| 8068 | 8205 |
| 8069 } // namespace dart | 8206 } // namespace dart |
| OLD | NEW |