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

Side by Side Diff: vm/object.cc

Issue 8342046: Add an imported into list for libraries so that it is possible to lookup (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: '' Created 9 years, 2 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/object_test.cc » ('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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 "vm/assembler.h" 7 #include "vm/assembler.h"
8 #include "vm/assert.h" 8 #include "vm/assert.h"
9 #include "vm/bigint_operations.h" 9 #include "vm/bigint_operations.h"
10 #include "vm/bootstrap.h" 10 #include "vm/bootstrap.h"
(...skipping 3396 matching lines...) Expand 10 before | Expand all | Expand 10 after
3407 result.set_kind(kind); 3407 result.set_kind(kind);
3408 return result.raw(); 3408 return result.raw();
3409 } 3409 }
3410 3410
3411 3411
3412 const char* Script::ToCString() const { 3412 const char* Script::ToCString() const {
3413 return "Script"; 3413 return "Script";
3414 } 3414 }
3415 3415
3416 3416
3417 DictionaryIterator::DictionaryIterator(const Library& library)
3418 : array_(Array::Handle(library.dictionary())),
3419 // Last element in array is a Smi.
3420 size_(Array::Handle(library.dictionary()).Length() - 1),
3421 next_ix_(0) {
3422 MoveToNextObject();
3423 }
3424
3425
3426 RawObject* DictionaryIterator::GetNext() {
3427 ASSERT(HasNext());
3428 int ix = next_ix_++;
3429 MoveToNextObject();
3430 ASSERT(array_.At(ix) != Object::null());
3431 return array_.At(ix);
3432 }
3433
3434
3435 void DictionaryIterator::MoveToNextObject() {
3436 Object& obj = Object::Handle(array_.At(next_ix_));
3437 while (obj.IsNull() && HasNext()) {
3438 next_ix_++;
3439 obj = array_.At(next_ix_);
3440 }
3441 }
3442
3443
3417 ClassDictionaryIterator::ClassDictionaryIterator(const Library& library) 3444 ClassDictionaryIterator::ClassDictionaryIterator(const Library& library)
3418 : array_(Array::Handle(library.dictionary())), 3445 : DictionaryIterator(library) {
3419 // Last element in array is a Smi.
3420 size_(Array::Handle(library.dictionary()).Length() - 1),
3421 next_ix_(0) {
3422 MoveToNextClass(); 3446 MoveToNextClass();
3423 } 3447 }
3424 3448
3425 3449
3426 RawClass* ClassDictionaryIterator::GetNext() { 3450 RawClass* ClassDictionaryIterator::GetNextClass() {
3427 ASSERT(HasNext()); 3451 ASSERT(HasNext());
3428 int ix = next_ix_++; 3452 int ix = next_ix_++;
3453 Object& obj = Object::Handle(array_.At(ix));
3429 MoveToNextClass(); 3454 MoveToNextClass();
3430 ASSERT(array_.At(ix) != Object::null());
3431 Class& cls = Class::Handle(); 3455 Class& cls = Class::Handle();
3432 cls ^= array_.At(ix); 3456 cls ^= obj.raw();
3433 return cls.raw(); 3457 return cls.raw();
3434 } 3458 }
3435 3459
3436 3460
3437 void ClassDictionaryIterator::MoveToNextClass() { 3461 void ClassDictionaryIterator::MoveToNextClass() {
3438 Object& obj = Object::Handle(array_.At(next_ix_)); 3462 Object& obj = Object::Handle(array_.At(next_ix_));
3439 while (!obj.IsClass() && HasNext()) { 3463 while (!obj.IsClass() && HasNext()) {
3440 next_ix_++; 3464 next_ix_++;
3441 obj = array_.At(next_ix_); 3465 obj = array_.At(next_ix_);
3442 } 3466 }
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
3578 if (entry_name.Equals(name)) { 3602 if (entry_name.Equals(name)) {
3579 return entry.raw(); 3603 return entry.raw();
3580 } 3604 }
3581 index = (index + 1) % dict_size; 3605 index = (index + 1) % dict_size;
3582 entry = dict.At(index); 3606 entry = dict.At(index);
3583 } 3607 }
3584 return Class::null(); 3608 return Class::null();
3585 } 3609 }
3586 3610
3587 3611
3588 RawObject* Library::LookupObject(const String& name) const { 3612 RawObject* Library::LookupObjectFiltered(const String& name,
3613 const Library& filter_lib) const {
3614 // First check if name is found in the local scope of the library.
3589 Object& obj = Object::Handle(LookupLocalObject(name)); 3615 Object& obj = Object::Handle(LookupLocalObject(name));
3590 if (!obj.IsNull()) { 3616 if (!obj.IsNull()) {
3591 return obj.raw(); 3617 return obj.raw();
3592 } 3618 }
3593 Library& import = Library::Handle(); 3619 // Now check if name is found in the top level scope of any imported libs.
3594 Array& imports = Array::Handle(this->imports()); 3620 const Array& imports = Array::Handle(this->imports());
3595 for (intptr_t i = 0; i < num_imports(); i++) { 3621 Library& import_lib = Library::Handle();
3596 import ^= imports.At(i); 3622 for (intptr_t j = 0; j < this->num_imports(); j++) {
3597 obj = import.LookupLocalObject(name); 3623 import_lib ^= imports.At(j);
3624 // Skip over the library that we need to filter out.
3625 if (!filter_lib.IsNull() && import_lib.raw() == filter_lib.raw()) {
3626 continue;
3627 }
3628 obj = import_lib.LookupLocalObject(name);
3598 if (!obj.IsNull()) { 3629 if (!obj.IsNull()) {
3599 return obj.raw(); 3630 return obj.raw();
3600 } 3631 }
3601 } 3632 }
3602 return Object::null(); 3633 return Object::null();
3603 } 3634 }
3604 3635
3605 3636
3637 RawObject* Library::LookupObject(const String& name) const {
3638 return LookupObjectFiltered(name, Library::Handle());
3639 }
3640
3641
3642 RawLibrary* Library::LookupObjectInImporter(const String& name) const {
3643 const Array& imported_into_libs = Array::Handle(this->imported_into());
3644 Library& lib = Library::Handle();
3645 Object& obj = Object::Handle();
3646 for (intptr_t i = 0; i < this->num_imported_into(); i++) {
3647 lib ^= imported_into_libs.At(i);
3648 obj = lib.LookupObjectFiltered(name, *this);
3649 if (!obj.IsNull()) {
3650 // If the object found is a class, field or function extract the
3651 // library in which it is defined as it might be defined in one of
3652 // the imported libraries.
3653 Class& cls = Class::Handle();
3654 Function& func = Function::Handle();
3655 Field& field = Field::Handle();
3656 if (obj.IsClass()) {
3657 cls ^= obj.raw();
3658 lib ^= cls.library();
3659 } else if (obj.IsFunction()) {
3660 func ^= obj.raw();
3661 cls ^= func.owner();
3662 lib ^= cls.library();
3663 } else if (obj.IsField()) {
3664 field ^= obj.raw();
3665 cls ^= field.owner();
3666 lib ^= cls.library();
3667 }
3668 return lib.raw();
3669 }
3670 }
3671 return Library::null();
3672 }
3673
3674
3675 RawString* Library::DuplicateDefineErrorString(const String& entry_name,
3676 const Library& conflict) const {
3677 String& errstr = String::Handle();
3678 Array& array = Array::Handle(Array::New(7));
3679 errstr = String::New("'");
3680 array.SetAt(0, errstr);
3681 array.SetAt(1, entry_name);
3682 errstr = String::New("' is defined in '");
3683 array.SetAt(2, errstr);
3684 errstr = url();
3685 array.SetAt(3, errstr);
3686 errstr = String::New("' and '");
3687 array.SetAt(4, errstr);
3688 errstr = conflict.url();
3689 array.SetAt(5, errstr);
3690 errstr = String::New("'");
3691 array.SetAt(6, errstr);
3692 errstr = String::ConcatAll(array);
3693 return errstr.raw();
3694 }
3695
3696
3697 RawString* Library::FindDuplicateDefinition(Library* conflicting_lib) const {
3698 DictionaryIterator it(*this);
3699 Object& obj = Object::Handle();
3700 Class& cls = Class::Handle();
3701 Function& func = Function::Handle();
3702 Field& field = Field::Handle();
3703 String& entry_name = String::Handle();
3704 while (it.HasNext()) {
3705 obj = it.GetNext();
3706 ASSERT(!obj.IsNull());
3707 if (obj.IsClass()) {
3708 cls ^= obj.raw();
3709 entry_name = cls.Name();
3710 } else if (obj.IsFunction()) {
3711 func ^= obj.raw();
3712 entry_name = func.name();
3713 } else if (obj.IsField()) {
3714 field ^= obj.raw();
3715 entry_name = field.name();
3716 } else {
3717 // We don't check for library prefixes defined in this library because
3718 // they are not visible in the importing scope and hence cannot
3719 // cause any duplicate definitions.
3720 continue;
3721 }
3722 *conflicting_lib = LookupObjectInImporter(entry_name);
3723 if (!conflicting_lib->IsNull()) {
3724 return entry_name.raw();
3725 }
3726 }
3727 return String::null();
3728 }
3729
3730
3606 RawClass* Library::LookupClass(const String& name) const { 3731 RawClass* Library::LookupClass(const String& name) const {
3607 Object& obj = Object::Handle(LookupObject(name)); 3732 Object& obj = Object::Handle(LookupObject(name));
3608 if (!obj.IsNull() && obj.IsClass()) { 3733 if (!obj.IsNull() && obj.IsClass()) {
3609 return Class::CheckedHandle(obj.raw()).raw(); 3734 return Class::CheckedHandle(obj.raw()).raw();
3610 } 3735 }
3611 return Class::null(); 3736 return Class::null();
3612 } 3737 }
3613 3738
3614 3739
3615 RawClass* Library::LookupLocalClass(const String& name) const { 3740 RawClass* Library::LookupLocalClass(const String& name) const {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
3655 Array& imports = Array::Handle(this->imports()); 3780 Array& imports = Array::Handle(this->imports());
3656 intptr_t capacity = imports.Length(); 3781 intptr_t capacity = imports.Length();
3657 if (num_imports() == capacity) { 3782 if (num_imports() == capacity) {
3658 capacity = capacity + kImportsCapacityIncrement; 3783 capacity = capacity + kImportsCapacityIncrement;
3659 imports = Array::Grow(imports, capacity); 3784 imports = Array::Grow(imports, capacity);
3660 StorePointer(&raw_ptr()->imports_, imports.raw()); 3785 StorePointer(&raw_ptr()->imports_, imports.raw());
3661 } 3786 }
3662 intptr_t index = num_imports(); 3787 intptr_t index = num_imports();
3663 imports.SetAt(index, library); 3788 imports.SetAt(index, library);
3664 set_num_imports(index + 1); 3789 set_num_imports(index + 1);
3790 library.AddImportedInto(*this);
3791 }
3792
3793
3794 void Library::AddImportedInto(const Library& library) const {
3795 Array& imported_into = Array::Handle(this->imported_into());
3796 intptr_t capacity = imported_into.Length();
3797 if (num_imported_into() == capacity) {
3798 capacity = capacity + kImportedIntoCapacityIncrement;
3799 imported_into = Array::Grow(imported_into, capacity);
3800 StorePointer(&raw_ptr()->imported_into_, imported_into.raw());
3801 }
3802 intptr_t index = num_imported_into();
3803 imported_into.SetAt(index, library);
3804 set_num_imported_into(index + 1);
3665 } 3805 }
3666 3806
3667 3807
3668 void Library::InitClassDictionary() const { 3808 void Library::InitClassDictionary() const {
3669 // The last element of the dictionary specifies the number of in use slots. 3809 // The last element of the dictionary specifies the number of in use slots.
3670 // TODO(iposva): Find reasonable initial size. 3810 // TODO(iposva): Find reasonable initial size.
3671 const int kInitialElementCount = 16; 3811 const int kInitialElementCount = 16;
3672 3812
3673 const Array& dictionary = 3813 const Array& dictionary =
3674 Array::Handle(Array::New(kInitialElementCount + 1, Heap::kOld)); 3814 Array::Handle(Array::New(kInitialElementCount + 1, Heap::kOld));
3675 dictionary.SetAt(kInitialElementCount, Smi::Handle(Smi::New(0))); 3815 dictionary.SetAt(kInitialElementCount, Smi::Handle(Smi::New(0)));
3676 StorePointer(&raw_ptr()->dictionary_, dictionary.raw()); 3816 StorePointer(&raw_ptr()->dictionary_, dictionary.raw());
3677 } 3817 }
3678 3818
3679 3819
3680 void Library::InitImportList() const { 3820 void Library::InitImportList() const {
3681 const Array& imports = 3821 const Array& imports =
3682 Array::Handle(Array::New(kInitialImportsCapacity, Heap::kOld)); 3822 Array::Handle(Array::New(kInitialImportsCapacity, Heap::kOld));
3683 StorePointer(&raw_ptr()->imports_, imports.raw()); 3823 StorePointer(&raw_ptr()->imports_, imports.raw());
3684 raw_ptr()->num_imports_ = 0; 3824 raw_ptr()->num_imports_ = 0;
3685 } 3825 }
3686 3826
3687 3827
3828 void Library::InitImportedIntoList() const {
3829 const Array& imported_into =
3830 Array::Handle(Array::New(kInitialImportedIntoCapacity, Heap::kOld));
3831 StorePointer(&raw_ptr()->imported_into_, imported_into.raw());
3832 raw_ptr()->num_imported_into_ = 0;
3833 }
3834
3835
3688 RawLibrary* Library::New() { 3836 RawLibrary* Library::New() {
3689 const Class& library_class = Class::Handle(Object::library_class()); 3837 const Class& library_class = Class::Handle(Object::library_class());
3690 RawObject* raw = Object::Allocate(library_class, 3838 RawObject* raw = Object::Allocate(library_class,
3691 Library::InstanceSize(), 3839 Library::InstanceSize(),
3692 Heap::kOld); 3840 Heap::kOld);
3693 return reinterpret_cast<RawLibrary*>(raw); 3841 return reinterpret_cast<RawLibrary*>(raw);
3694 } 3842 }
3695 3843
3696 3844
3697 RawLibrary* Library::NewLibraryHelper(const String& url, 3845 RawLibrary* Library::NewLibraryHelper(const String& url,
3698 bool import_core_lib) { 3846 bool import_core_lib) {
3699 const Library& result = Library::Handle(Library::New()); 3847 const Library& result = Library::Handle(Library::New());
3700 result.raw_ptr()->name_ = url.raw(); 3848 result.raw_ptr()->name_ = url.raw();
3701 result.raw_ptr()->url_ = url.raw(); 3849 result.raw_ptr()->url_ = url.raw();
3702 result.raw_ptr()->private_key_ = Scanner::AllocatePrivateKey(result); 3850 result.raw_ptr()->private_key_ = Scanner::AllocatePrivateKey(result);
3703 result.raw_ptr()->dictionary_ = Array::Empty(); 3851 result.raw_ptr()->dictionary_ = Array::Empty();
3704 result.raw_ptr()->anonymous_classes_ = Array::Empty(); 3852 result.raw_ptr()->anonymous_classes_ = Array::Empty();
3705 result.raw_ptr()->num_anonymous_ = 0; 3853 result.raw_ptr()->num_anonymous_ = 0;
3706 result.raw_ptr()->imports_ = Array::Empty(); 3854 result.raw_ptr()->imports_ = Array::Empty();
3707 result.raw_ptr()->next_registered_ = Library::null(); 3855 result.raw_ptr()->next_registered_ = Library::null();
3708 result.set_native_entry_resolver(NULL); 3856 result.set_native_entry_resolver(NULL);
3709 result.raw_ptr()->corelib_imported_ = true; 3857 result.raw_ptr()->corelib_imported_ = true;
3710 result.raw_ptr()->loaded_ = false; 3858 result.raw_ptr()->loaded_ = false;
3711 result.InitClassDictionary(); 3859 result.InitClassDictionary();
3712 result.InitImportList(); 3860 result.InitImportList();
3861 result.InitImportedIntoList();
3713 if (import_core_lib) { 3862 if (import_core_lib) {
3714 Library& core_lib = Library::Handle(Library::CoreLibrary()); 3863 Library& core_lib = Library::Handle(Library::CoreLibrary());
3715 ASSERT(!core_lib.IsNull()); 3864 ASSERT(!core_lib.IsNull());
3716 result.AddImport(core_lib); 3865 result.AddImport(core_lib);
3717 if (FLAG_expose_core_impl) { 3866 if (FLAG_expose_core_impl) {
3718 // Make implementation corelib visible to Dart code. 3867 // Make implementation corelib visible to Dart code.
3719 result.AddImport(Library::Handle(Library::CoreImplLibrary())); 3868 result.AddImport(Library::Handle(Library::CoreImplLibrary()));
3720 } 3869 }
3721 } 3870 }
3722 return result.raw(); 3871 return result.raw();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
3754 lib_url = lib.url(); 3903 lib_url = lib.url();
3755 if (lib_url.Equals(url)) { 3904 if (lib_url.Equals(url)) {
3756 return lib.raw(); 3905 return lib.raw();
3757 } 3906 }
3758 lib = lib.next_registered(); 3907 lib = lib.next_registered();
3759 } 3908 }
3760 return Library::null(); 3909 return Library::null();
3761 } 3910 }
3762 3911
3763 3912
3913 RawString* Library::CheckForDuplicateDefinition() {
3914 Library& lib = Library::Handle();
3915 Isolate* isolate = Isolate::Current();
3916 ASSERT(isolate != NULL);
3917 ObjectStore* object_store = isolate->object_store();
3918 ASSERT(object_store != NULL);
3919 lib ^= object_store->registered_libraries();
3920 String& entry_name = String::Handle();
3921 Library& conflicting_lib = Library::Handle();
3922 while (!lib.IsNull()) {
3923 entry_name = lib.FindDuplicateDefinition(&conflicting_lib);
3924 if (!entry_name.IsNull()) {
3925 return lib.DuplicateDefineErrorString(entry_name, conflicting_lib);
3926 }
3927 lib ^= lib.next_registered();
3928 }
3929 return String::null();
3930 }
3931
3932
3764 bool Library::IsKeyUsed(intptr_t key) { 3933 bool Library::IsKeyUsed(intptr_t key) {
3765 intptr_t lib_key; 3934 intptr_t lib_key;
3766 Library& lib = Library::Handle(); 3935 Library& lib = Library::Handle();
3767 lib = Isolate::Current()->object_store()->registered_libraries(); 3936 lib = Isolate::Current()->object_store()->registered_libraries();
3768 String& lib_url = String::Handle(); 3937 String& lib_url = String::Handle();
3769 while (!lib.IsNull()) { 3938 while (!lib.IsNull()) {
3770 lib_url ^= lib.url(); 3939 lib_url ^= lib.url();
3771 lib_key = lib_url.Hash(); 3940 lib_key = lib_url.Hash();
3772 if (lib_key == key) { 3941 if (lib_key == key) {
3773 return true; 3942 return true;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
3847 } 4016 }
3848 4017
3849 4018
3850 void Library::CompileAll() { 4019 void Library::CompileAll() {
3851 Library& lib = Library::Handle( 4020 Library& lib = Library::Handle(
3852 Isolate::Current()->object_store()->registered_libraries()); 4021 Isolate::Current()->object_store()->registered_libraries());
3853 Class& cls = Class::Handle(); 4022 Class& cls = Class::Handle();
3854 while (!lib.IsNull()) { 4023 while (!lib.IsNull()) {
3855 ClassDictionaryIterator it(lib); 4024 ClassDictionaryIterator it(lib);
3856 while (it.HasNext()) { 4025 while (it.HasNext()) {
3857 cls ^= it.GetNext(); 4026 cls ^= it.GetNextClass();
3858 if (!cls.is_interface()) { 4027 if (!cls.is_interface()) {
3859 Compiler::CompileAllFunctions(cls); 4028 Compiler::CompileAllFunctions(cls);
3860 } 4029 }
3861 } 4030 }
3862 Array& anon_classes = Array::Handle(lib.raw_ptr()->anonymous_classes_); 4031 Array& anon_classes = Array::Handle(lib.raw_ptr()->anonymous_classes_);
3863 for (int i = 0; i < lib.raw_ptr()->num_anonymous_; i++) { 4032 for (int i = 0; i < lib.raw_ptr()->num_anonymous_; i++) {
3864 cls ^= anon_classes.At(i); 4033 cls ^= anon_classes.At(i);
3865 ASSERT(!cls.is_interface()); 4034 ASSERT(!cls.is_interface());
3866 Compiler::CompileAllFunctions(cls); 4035 Compiler::CompileAllFunctions(cls);
3867 } 4036 }
(...skipping 2648 matching lines...) Expand 10 before | Expand all | Expand 10 after
6516 const String& str = String::Handle(pattern()); 6685 const String& str = String::Handle(pattern());
6517 const char* format = "JSRegExp: pattern=%s flags=%s"; 6686 const char* format = "JSRegExp: pattern=%s flags=%s";
6518 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); 6687 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags());
6519 char* chars = reinterpret_cast<char*>( 6688 char* chars = reinterpret_cast<char*>(
6520 Isolate::Current()->current_zone()->Allocate(len + 1)); 6689 Isolate::Current()->current_zone()->Allocate(len + 1));
6521 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); 6690 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags());
6522 return chars; 6691 return chars;
6523 } 6692 }
6524 6693
6525 } // namespace dart 6694 } // namespace dart
OLDNEW
« no previous file with comments | « vm/object.h ('k') | vm/object_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698