Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: vm/object.cc

Issue 9363048: Implement capability to use the same library prefix for multiple library imports (currently this ... (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « vm/object.h ('k') | vm/parser.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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.
4776 Array& libs = Array::Handle(libraries());
4777 const intptr_t length = (libs.IsNull()) ? 0 : libs.Length();
4778 // Grow the list if it is full.
4779 if (num_current_libs >= length) {
4780 const intptr_t new_length = length + kIncrementSize;
4781 libs = Array::Grow(libs, new_length, Heap::kOld);
4782 set_libraries(libs);
4783 }
4784 libs.SetAt(num_current_libs, library);
4785 set_num_libs(num_current_libs + 1);
4786 }
4787
4788
4789 RawClass* LibraryPrefix::LookupLocalClass(const String& class_name) const {
4790 Array& libs = Array::Handle(libraries());
4791 Class& resolved_class = Class::Handle();
4792 Library& lib = Library::Handle();
4793 for (intptr_t i = 0; i < num_libs(); i++) {
4794 lib ^= libs.At(i);
4795 ASSERT(!lib.IsNull());
4796 resolved_class = lib.LookupLocalClass(class_name);
4797 if (!resolved_class.IsNull()) {
4798 return resolved_class.raw();
4799 }
4800 }
4801 return Class::null();
4802 }
4803
4804
4726 RawLibraryPrefix* LibraryPrefix::New() { 4805 RawLibraryPrefix* LibraryPrefix::New() {
4727 const Class& library_prefix_class = 4806 const Class& library_prefix_class =
4728 Class::Handle(Object::library_prefix_class()); 4807 Class::Handle(Object::library_prefix_class());
4729 RawObject* raw = Object::Allocate(library_prefix_class, 4808 RawObject* raw = Object::Allocate(library_prefix_class,
4730 LibraryPrefix::InstanceSize(), 4809 LibraryPrefix::InstanceSize(),
4731 Heap::kOld); 4810 Heap::kOld);
4732 return reinterpret_cast<RawLibraryPrefix*>(raw); 4811 return reinterpret_cast<RawLibraryPrefix*>(raw);
4733 } 4812 }
4734 4813
4735 4814
4736 RawLibraryPrefix* LibraryPrefix::New(const String& name, const Library& lib) { 4815 RawLibraryPrefix* LibraryPrefix::New(const String& name, const Library& lib) {
4737 const LibraryPrefix& result = LibraryPrefix::Handle(LibraryPrefix::New()); 4816 const LibraryPrefix& result = LibraryPrefix::Handle(LibraryPrefix::New());
4738 result.set_name(name); 4817 result.set_name(name);
4739 result.set_library(lib); 4818 result.set_num_libs(0);
4819 result.AddLibrary(lib);
4740 return result.raw(); 4820 return result.raw();
4741 } 4821 }
4742 4822
4743 4823
4744 const char* LibraryPrefix::ToCString() const { 4824 const char* LibraryPrefix::ToCString() const {
4745 const char* kFormat = "LibraryPrefix:'%s'"; 4825 const char* kFormat = "LibraryPrefix:'%s'";
4746 const String& prefix = String::Handle(name()); 4826 const String& prefix = String::Handle(name());
4747 intptr_t len = OS::SNPrint(NULL, 0, kFormat, prefix.ToCString()) + 1; 4827 intptr_t len = OS::SNPrint(NULL, 0, kFormat, prefix.ToCString()) + 1;
4748 char* chars = reinterpret_cast<char*>( 4828 char* chars = reinterpret_cast<char*>(
4749 Isolate::Current()->current_zone()->Allocate(len)); 4829 Isolate::Current()->current_zone()->Allocate(len));
4750 OS::SNPrint(chars, len, kFormat, prefix.ToCString()); 4830 OS::SNPrint(chars, len, kFormat, prefix.ToCString());
4751 return chars; 4831 return chars;
4752 } 4832 }
4753 4833
4754 4834
4835 RawString* LibraryPrefix::CheckForDuplicateDefinition() const {
4836 Library& lib = Library::Handle();
4837 Library& conflicting_lib = Library::Handle();
4838 Object& obj = Object::Handle();
4839 Class& cls = Class::Handle();
4840 Function& func = Function::Handle();
4841 Field& field = Field::Handle();
4842 String& entry_name = String::Handle();
4843
4844 for (intptr_t i = 0; i < num_libs(); i++) {
4845 lib = GetLibrary(i);
4846 ASSERT(!lib.IsNull());
4847 DictionaryIterator it(lib);
4848 while (it.HasNext()) {
4849 obj = it.GetNext();
4850 ASSERT(!obj.IsNull());
4851 if (obj.IsClass()) {
4852 cls ^= obj.raw();
4853 if (cls.IsCanonicalSignatureClass()) {
4854 continue;
4855 }
4856 entry_name = cls.Name();
4857 } else if (obj.IsFunction()) {
4858 func ^= obj.raw();
4859 entry_name = func.name();
4860 } else if (obj.IsField()) {
4861 field ^= obj.raw();
4862 entry_name = field.name();
4863 } else {
4864 // We don't check library prefixes defined in this library for
4865 // conflicts because they are not visible in the importing scope and
4866 // hence cannot cause any duplicate definitions.
4867 continue;
4868 }
4869 for (intptr_t j = i + 1; j < num_libs(); j++) {
4870 conflicting_lib = GetLibrary(j);
4871 ASSERT(!conflicting_lib.IsNull());
4872 // Check if name is found in the local scope of the library.
4873 obj = conflicting_lib.LookupLocalObject(entry_name);
4874 if (!obj.IsNull()) {
4875 return lib.DuplicateDefineErrorString(entry_name, conflicting_lib);
4876 }
4877 }
4878 }
4879 }
4880 return String::null();
4881 }
4882
4883
4755 void LibraryPrefix::set_name(const String& value) const { 4884 void LibraryPrefix::set_name(const String& value) const {
4756 ASSERT(value.IsSymbol()); 4885 ASSERT(value.IsSymbol());
4757 StorePointer(&raw_ptr()->name_, value.raw()); 4886 StorePointer(&raw_ptr()->name_, value.raw());
4758 } 4887 }
4759 4888
4760 4889
4761 void LibraryPrefix::set_library(const Library& value) const { 4890 void LibraryPrefix::set_libraries(const Array& value) const {
4762 StorePointer(&raw_ptr()->library_, value.raw()); 4891 StorePointer(&raw_ptr()->libraries_, value.raw());
4892 }
4893
4894
4895 void LibraryPrefix::set_num_libs(intptr_t value) const {
4896 raw_ptr()->num_libs_ = value;
4763 } 4897 }
4764 4898
4765 4899
4766 RawError* Library::CompileAll() { 4900 RawError* Library::CompileAll() {
4767 Error& error = Error::Handle(); 4901 Error& error = Error::Handle();
4768 Library& lib = Library::Handle( 4902 Library& lib = Library::Handle(
4769 Isolate::Current()->object_store()->registered_libraries()); 4903 Isolate::Current()->object_store()->registered_libraries());
4770 Class& cls = Class::Handle(); 4904 Class& cls = Class::Handle();
4771 while (!lib.IsNull()) { 4905 while (!lib.IsNull()) {
4772 ClassDictionaryIterator it(lib); 4906 ClassDictionaryIterator it(lib);
(...skipping 3287 matching lines...) Expand 10 before | Expand all | Expand 10 after
8060 const String& str = String::Handle(pattern()); 8194 const String& str = String::Handle(pattern());
8061 const char* format = "JSRegExp: pattern=%s flags=%s"; 8195 const char* format = "JSRegExp: pattern=%s flags=%s";
8062 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); 8196 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags());
8063 char* chars = reinterpret_cast<char*>( 8197 char* chars = reinterpret_cast<char*>(
8064 Isolate::Current()->current_zone()->Allocate(len + 1)); 8198 Isolate::Current()->current_zone()->Allocate(len + 1));
8065 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); 8199 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags());
8066 return chars; 8200 return chars;
8067 } 8201 }
8068 8202
8069 } // namespace dart 8203 } // namespace dart
OLDNEW
« no previous file with comments | « vm/object.h ('k') | vm/parser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698