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

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
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 3388 matching lines...) Expand 10 before | Expand all | Expand 10 after
3399 result.set_kind(kind); 3399 result.set_kind(kind);
3400 return result.raw(); 3400 return result.raw();
3401 } 3401 }
3402 3402
3403 3403
3404 const char* Script::ToCString() const { 3404 const char* Script::ToCString() const {
3405 return "Script"; 3405 return "Script";
3406 } 3406 }
3407 3407
3408 3408
3409 DictionaryIterator::DictionaryIterator(const Library& library)
3410 : array_(Array::Handle(library.dictionary())),
3411 // Last element in array is a Smi.
3412 size_(Array::Handle(library.dictionary()).Length() - 1),
3413 next_ix_(0) {
3414 MoveToNextObject();
3415 }
3416
3417
3418 RawObject* DictionaryIterator::GetNext() {
3419 ASSERT(HasNext());
3420 int ix = next_ix_++;
3421 MoveToNextObject();
3422 ASSERT(array_.At(ix) != Object::null());
3423 return array_.At(ix);
3424 }
3425
3426
3427 void DictionaryIterator::MoveToNextObject() {
3428 Object& obj = Object::Handle(array_.At(next_ix_));
3429 while (obj.IsNull() && HasNext()) {
3430 next_ix_++;
3431 obj = array_.At(next_ix_);
3432 }
3433 }
3434
3435
3409 ClassDictionaryIterator::ClassDictionaryIterator(const Library& library) 3436 ClassDictionaryIterator::ClassDictionaryIterator(const Library& library)
3410 : array_(Array::Handle(library.dictionary())), 3437 : DictionaryIterator(library) {
3411 // Last element in array is a Smi.
3412 size_(Array::Handle(library.dictionary()).Length() - 1),
3413 next_ix_(0) {
3414 MoveToNextClass(); 3438 MoveToNextClass();
3415 } 3439 }
3416 3440
3417 3441
3418 RawClass* ClassDictionaryIterator::GetNext() { 3442 RawClass* ClassDictionaryIterator::GetNextClass() {
3419 ASSERT(HasNext()); 3443 ASSERT(HasNext());
3420 int ix = next_ix_++; 3444 int ix = next_ix_++;
3445 Object& obj = Object::Handle(array_.At(ix));
3421 MoveToNextClass(); 3446 MoveToNextClass();
3422 ASSERT(array_.At(ix) != Object::null());
3423 Class& cls = Class::Handle(); 3447 Class& cls = Class::Handle();
3424 cls ^= array_.At(ix); 3448 cls ^= obj.raw();
3425 return cls.raw(); 3449 return cls.raw();
3426 } 3450 }
3427 3451
3428 3452
3429 void ClassDictionaryIterator::MoveToNextClass() { 3453 void ClassDictionaryIterator::MoveToNextClass() {
3430 Object& obj = Object::Handle(array_.At(next_ix_)); 3454 Object& obj = Object::Handle(array_.At(next_ix_));
3431 while (!obj.IsClass() && HasNext()) { 3455 while (!obj.IsClass() && HasNext()) {
3432 next_ix_++; 3456 next_ix_++;
3433 obj = array_.At(next_ix_); 3457 obj = array_.At(next_ix_);
3434 } 3458 }
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
3571 return entry.raw(); 3595 return entry.raw();
3572 } 3596 }
3573 index = (index + 1) % dict_size; 3597 index = (index + 1) % dict_size;
3574 entry = dict.At(index); 3598 entry = dict.At(index);
3575 } 3599 }
3576 return Class::null(); 3600 return Class::null();
3577 } 3601 }
3578 3602
3579 3603
3580 RawObject* Library::LookupObject(const String& name) const { 3604 RawObject* Library::LookupObject(const String& name) const {
3605 // First check if the name is found in the local scope.
3581 Object& obj = Object::Handle(LookupLocalObject(name)); 3606 Object& obj = Object::Handle(LookupLocalObject(name));
3582 if (!obj.IsNull()) { 3607 if (!obj.IsNull()) {
3583 return obj.raw(); 3608 return obj.raw();
3584 } 3609 }
3610 // Now check if the name is found in the local scope of any imported libs.
3585 Library& import = Library::Handle(); 3611 Library& import = Library::Handle();
3586 Array& imports = Array::Handle(this->imports()); 3612 Array& imports = Array::Handle(this->imports());
3587 for (intptr_t i = 0; i < num_imports(); i++) { 3613 for (intptr_t i = 0; i < num_imports(); i++) {
3588 import ^= imports.At(i); 3614 import ^= imports.At(i);
3589 obj = import.LookupLocalObject(name); 3615 obj = import.LookupLocalObject(name);
3590 if (!obj.IsNull()) { 3616 if (!obj.IsNull()) {
3591 return obj.raw(); 3617 return obj.raw();
3592 } 3618 }
3593 } 3619 }
3594 return Object::null(); 3620 return Object::null();
3595 } 3621 }
3596 3622
3597 3623
3624 RawObject* Library::LookupObjectInImporter(const String& name) const {
3625 const Array& imported_into_libs = Array::Handle(this->imported_into());
3626 Library& importer_lib = Library::Handle();
3627 Object& obj = Object::Handle();
3628 for (intptr_t i = 0; i < this->num_imported_into(); i++) {
3629 importer_lib ^= imported_into_libs.At(i);
3630 // Check if name is found in the top level scope of the importer.
3631 ASSERT(importer_lib.raw() != raw());
3632 // First check if name is found in the local scope of the library into
Ivan Posva 2011/10/20 08:46:04 Please factor this out as it is essentially Lookup
siva 2011/10/20 23:08:47 Done.
3633 // which this is imported into.
3634 obj = importer_lib.LookupLocalObject(name);
3635 if (!obj.IsNull()) {
3636 return obj.raw();
3637 }
3638 // Now check if name is found in the top level scope of libs imported
3639 // by the importer.
3640 const Array& imports = Array::Handle(importer_lib.imports());
3641 Library& import_lib = Library::Handle();
3642 for (intptr_t j = 0; j < importer_lib.num_imports(); j++) {
3643 import_lib ^= imports.At(j);
3644 if (import_lib.raw() == raw()) {
3645 continue;
3646 }
3647 obj = import_lib.LookupLocalObject(name);
3648 if (!obj.IsNull()) {
3649 return obj.raw();
3650 }
3651 }
3652 }
3653 return Object::null();
3654 }
3655
3656
3657 RawString* Library::DuplicateDefineErrorString(const Object& obj) const {
3658 Class& cls = Class::Handle();
3659 Function& func = Function::Handle();
3660 Field& field = Field::Handle();
3661 String& errstr = String::Handle();
3662 if (obj.IsClass()) {
3663 cls ^= obj.raw();
3664 errstr = cls.Name();
3665 } else if (obj.IsFunction()) {
3666 func ^= obj.raw();
3667 cls ^= func.owner();
3668 errstr = func.name();
3669 } else if (obj.IsField()) {
3670 field ^= obj.raw();
3671 cls ^= field.owner();
3672 errstr = field.name();
3673 } else {
3674 UNREACHABLE();
3675 }
3676 const Library& lib = Library::Handle(cls.library());
3677 Array& array = Array::Handle(Array::New(5));
3678 array.SetAt(0, errstr);
3679 errstr = String::New(" is defined in libraries ");
3680 array.SetAt(1, errstr);
3681 errstr = url();
3682 array.SetAt(2, errstr);
3683 errstr = String::New(" and ");
3684 array.SetAt(3, errstr);
3685 errstr = lib.url();
3686 array.SetAt(4, errstr);
3687 errstr = String::ConcatAll(array);
3688 return errstr.raw();
3689 }
3690
3691
3692 RawObject* Library::FindDuplicateDefinition() const {
3693 DictionaryIterator it(*this);
3694 Object& obj = Object::Handle();
3695 Class& cls = Class::Handle();
3696 Function& func = Function::Handle();
3697 Field& field = Field::Handle();
3698 String& entry_name = String::Handle();
3699 while (it.HasNext()) {
3700 obj = it.GetNext();
3701 if (obj.IsNull()) {
Ivan Posva 2011/10/20 08:46:04 I don't think the iterator is ever handing out nul
siva 2011/10/20 23:08:47 True, I changed this to an assertion. On 2011/10/
3702 continue;
3703 } else if (obj.IsClass()) {
3704 cls ^= obj.raw();
3705 entry_name = cls.Name();
3706 } else if (obj.IsFunction()) {
3707 func ^= obj.raw();
3708 entry_name = func.name();
3709 } else if (obj.IsField()) {
3710 field ^= obj.raw();
3711 entry_name = field.name();
3712 } else {
Ivan Posva 2011/10/20 08:46:04 Please add a comment explaining why you can skip l
siva 2011/10/20 23:08:47 Added comment stating that library prefixes in thi
3713 continue;
3714 }
3715 obj = LookupObjectInImporter(entry_name);
3716 if (!obj.IsNull()) {
3717 return obj.raw();
3718 }
3719 }
3720 return Object::null();
3721 }
3722
3723
3598 RawClass* Library::LookupClass(const String& name) const { 3724 RawClass* Library::LookupClass(const String& name) const {
3599 Object& obj = Object::Handle(LookupObject(name)); 3725 Object& obj = Object::Handle(LookupObject(name));
3600 if (!obj.IsNull() && obj.IsClass()) { 3726 if (!obj.IsNull() && obj.IsClass()) {
3601 return Class::CheckedHandle(obj.raw()).raw(); 3727 return Class::CheckedHandle(obj.raw()).raw();
3602 } 3728 }
3603 return Class::null(); 3729 return Class::null();
3604 } 3730 }
3605 3731
3606 3732
3607 RawClass* Library::LookupLocalClass(const String& name) const { 3733 RawClass* Library::LookupLocalClass(const String& name) const {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
3647 Array& imports = Array::Handle(this->imports()); 3773 Array& imports = Array::Handle(this->imports());
3648 intptr_t capacity = imports.Length(); 3774 intptr_t capacity = imports.Length();
3649 if (num_imports() == capacity) { 3775 if (num_imports() == capacity) {
3650 capacity = capacity + kImportsCapacityIncrement; 3776 capacity = capacity + kImportsCapacityIncrement;
3651 imports = Array::Grow(imports, capacity); 3777 imports = Array::Grow(imports, capacity);
3652 StorePointer(&raw_ptr()->imports_, imports.raw()); 3778 StorePointer(&raw_ptr()->imports_, imports.raw());
3653 } 3779 }
3654 intptr_t index = num_imports(); 3780 intptr_t index = num_imports();
3655 imports.SetAt(index, library); 3781 imports.SetAt(index, library);
3656 set_num_imports(index + 1); 3782 set_num_imports(index + 1);
3783 library.AddImportedInto(*this);
3784 }
3785
3786
3787 void Library::AddImportedInto(const Library& library) const {
3788 Array& imported_into = Array::Handle(this->imported_into());
3789 intptr_t capacity = imported_into.Length();
3790 if (num_imported_into() == capacity) {
3791 capacity = capacity + kImportedIntoCapacityIncrement;
3792 imported_into = Array::Grow(imported_into, capacity);
3793 StorePointer(&raw_ptr()->imported_into_, imported_into.raw());
3794 }
3795 intptr_t index = num_imported_into();
3796 imported_into.SetAt(index, library);
3797 set_num_imported_into(index + 1);
3657 } 3798 }
3658 3799
3659 3800
3660 void Library::InitClassDictionary() const { 3801 void Library::InitClassDictionary() const {
3661 // The last element of the dictionary specifies the number of in use slots. 3802 // The last element of the dictionary specifies the number of in use slots.
3662 // TODO(iposva): Find reasonable initial size. 3803 // TODO(iposva): Find reasonable initial size.
3663 const int kInitialElementCount = 16; 3804 const int kInitialElementCount = 16;
3664 3805
3665 const Array& dictionary = 3806 const Array& dictionary =
3666 Array::Handle(Array::New(kInitialElementCount + 1, Heap::kOld)); 3807 Array::Handle(Array::New(kInitialElementCount + 1, Heap::kOld));
3667 dictionary.SetAt(kInitialElementCount, Smi::Handle(Smi::New(0))); 3808 dictionary.SetAt(kInitialElementCount, Smi::Handle(Smi::New(0)));
3668 StorePointer(&raw_ptr()->dictionary_, dictionary.raw()); 3809 StorePointer(&raw_ptr()->dictionary_, dictionary.raw());
3669 } 3810 }
3670 3811
3671 3812
3672 void Library::InitImportList() const { 3813 void Library::InitImportList() const {
3673 const Array& imports = 3814 const Array& imports =
3674 Array::Handle(Array::New(kInitialImportsCapacity, Heap::kOld)); 3815 Array::Handle(Array::New(kInitialImportsCapacity, Heap::kOld));
3675 StorePointer(&raw_ptr()->imports_, imports.raw()); 3816 StorePointer(&raw_ptr()->imports_, imports.raw());
3676 raw_ptr()->num_imports_ = 0; 3817 raw_ptr()->num_imports_ = 0;
3677 } 3818 }
3678 3819
3679 3820
3821 void Library::InitImportedIntoList() const {
3822 const Array& imported_into =
3823 Array::Handle(Array::New(kInitialImportedIntoCapacity, Heap::kOld));
3824 StorePointer(&raw_ptr()->imported_into_, imported_into.raw());
3825 raw_ptr()->num_imported_into_ = 0;
3826 }
3827
3828
3680 RawLibrary* Library::New() { 3829 RawLibrary* Library::New() {
3681 const Class& library_class = Class::Handle(Object::library_class()); 3830 const Class& library_class = Class::Handle(Object::library_class());
3682 RawObject* raw = Object::Allocate(library_class, 3831 RawObject* raw = Object::Allocate(library_class,
3683 Library::InstanceSize(), 3832 Library::InstanceSize(),
3684 Heap::kOld); 3833 Heap::kOld);
3685 return reinterpret_cast<RawLibrary*>(raw); 3834 return reinterpret_cast<RawLibrary*>(raw);
3686 } 3835 }
3687 3836
3688 3837
3689 RawLibrary* Library::NewLibraryHelper(const String& url, 3838 RawLibrary* Library::NewLibraryHelper(const String& url,
3690 bool import_core_lib) { 3839 bool import_core_lib) {
3691 const Library& result = Library::Handle(Library::New()); 3840 const Library& result = Library::Handle(Library::New());
3692 result.raw_ptr()->name_ = url.raw(); 3841 result.raw_ptr()->name_ = url.raw();
3693 result.raw_ptr()->url_ = url.raw(); 3842 result.raw_ptr()->url_ = url.raw();
3694 result.raw_ptr()->private_key_ = Scanner::AllocatePrivateKey(result); 3843 result.raw_ptr()->private_key_ = Scanner::AllocatePrivateKey(result);
3695 result.raw_ptr()->dictionary_ = Array::Empty(); 3844 result.raw_ptr()->dictionary_ = Array::Empty();
3696 result.raw_ptr()->anonymous_classes_ = Array::Empty(); 3845 result.raw_ptr()->anonymous_classes_ = Array::Empty();
3697 result.raw_ptr()->num_anonymous_ = 0; 3846 result.raw_ptr()->num_anonymous_ = 0;
3698 result.raw_ptr()->imports_ = Array::Empty(); 3847 result.raw_ptr()->imports_ = Array::Empty();
3699 result.raw_ptr()->next_registered_ = Library::null(); 3848 result.raw_ptr()->next_registered_ = Library::null();
3700 result.set_native_entry_resolver(NULL); 3849 result.set_native_entry_resolver(NULL);
3701 result.raw_ptr()->corelib_imported_ = true; 3850 result.raw_ptr()->corelib_imported_ = true;
3702 result.raw_ptr()->loaded_ = false; 3851 result.raw_ptr()->loaded_ = false;
3703 result.InitClassDictionary(); 3852 result.InitClassDictionary();
3704 result.InitImportList(); 3853 result.InitImportList();
3854 result.InitImportedIntoList();
3705 if (import_core_lib) { 3855 if (import_core_lib) {
3706 Library& core_lib = Library::Handle(Library::CoreLibrary()); 3856 Library& core_lib = Library::Handle(Library::CoreLibrary());
3707 ASSERT(!core_lib.IsNull()); 3857 ASSERT(!core_lib.IsNull());
3708 result.AddImport(core_lib); 3858 result.AddImport(core_lib);
3709 if (FLAG_expose_core_impl) { 3859 if (FLAG_expose_core_impl) {
3710 // Make implementation corelib visible to Dart code. 3860 // Make implementation corelib visible to Dart code.
3711 result.AddImport(Library::Handle(Library::CoreImplLibrary())); 3861 result.AddImport(Library::Handle(Library::CoreImplLibrary()));
3712 } 3862 }
3713 } 3863 }
3714 return result.raw(); 3864 return result.raw();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
3746 lib_url = lib.url(); 3896 lib_url = lib.url();
3747 if (lib_url.Equals(url)) { 3897 if (lib_url.Equals(url)) {
3748 return lib.raw(); 3898 return lib.raw();
3749 } 3899 }
3750 lib = lib.next_registered(); 3900 lib = lib.next_registered();
3751 } 3901 }
3752 return Library::null(); 3902 return Library::null();
3753 } 3903 }
3754 3904
3755 3905
3906 RawString* Library::CheckForDuplicateDefinition() {
3907 Library& lib = Library::Handle();
3908 Isolate* isolate = Isolate::Current();
3909 ASSERT(isolate != NULL);
3910 ObjectStore* object_store = isolate->object_store();
3911 ASSERT(object_store != NULL);
3912 lib ^= object_store->registered_libraries();
3913 Object& obj = Object::Handle();
3914 while (!lib.IsNull()) {
3915 obj = lib.FindDuplicateDefinition();
3916 if (!obj.IsNull()) {
3917 return lib.DuplicateDefineErrorString(obj);
3918 }
3919 lib ^= lib.next_registered();
3920 }
3921 return String::null();
3922 }
3923
3924
3756 bool Library::IsKeyUsed(intptr_t key) { 3925 bool Library::IsKeyUsed(intptr_t key) {
3757 intptr_t lib_key; 3926 intptr_t lib_key;
3758 Library& lib = Library::Handle(); 3927 Library& lib = Library::Handle();
3759 lib = Isolate::Current()->object_store()->registered_libraries(); 3928 lib = Isolate::Current()->object_store()->registered_libraries();
3760 String& lib_url = String::Handle(); 3929 String& lib_url = String::Handle();
3761 while (!lib.IsNull()) { 3930 while (!lib.IsNull()) {
3762 lib_url ^= lib.url(); 3931 lib_url ^= lib.url();
3763 lib_key = lib_url.Hash(); 3932 lib_key = lib_url.Hash();
3764 if (lib_key == key) { 3933 if (lib_key == key) {
3765 return true; 3934 return true;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
3839 } 4008 }
3840 4009
3841 4010
3842 void Library::CompileAll() { 4011 void Library::CompileAll() {
3843 Library& lib = Library::Handle( 4012 Library& lib = Library::Handle(
3844 Isolate::Current()->object_store()->registered_libraries()); 4013 Isolate::Current()->object_store()->registered_libraries());
3845 Class& cls = Class::Handle(); 4014 Class& cls = Class::Handle();
3846 while (!lib.IsNull()) { 4015 while (!lib.IsNull()) {
3847 ClassDictionaryIterator it(lib); 4016 ClassDictionaryIterator it(lib);
3848 while (it.HasNext()) { 4017 while (it.HasNext()) {
3849 cls ^= it.GetNext(); 4018 cls ^= it.GetNextClass();
3850 if (!cls.is_interface()) { 4019 if (!cls.is_interface()) {
3851 Compiler::CompileAllFunctions(cls); 4020 Compiler::CompileAllFunctions(cls);
3852 } 4021 }
3853 } 4022 }
3854 Array& anon_classes = Array::Handle(lib.raw_ptr()->anonymous_classes_); 4023 Array& anon_classes = Array::Handle(lib.raw_ptr()->anonymous_classes_);
3855 for (int i = 0; i < lib.raw_ptr()->num_anonymous_; i++) { 4024 for (int i = 0; i < lib.raw_ptr()->num_anonymous_; i++) {
3856 cls ^= anon_classes.At(i); 4025 cls ^= anon_classes.At(i);
3857 ASSERT(!cls.is_interface()); 4026 ASSERT(!cls.is_interface());
3858 Compiler::CompileAllFunctions(cls); 4027 Compiler::CompileAllFunctions(cls);
3859 } 4028 }
(...skipping 2623 matching lines...) Expand 10 before | Expand all | Expand 10 after
6483 const String& str = String::Handle(pattern()); 6652 const String& str = String::Handle(pattern());
6484 const char* format = "JSRegExp: pattern=%s flags=%s"; 6653 const char* format = "JSRegExp: pattern=%s flags=%s";
6485 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); 6654 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags());
6486 char* chars = reinterpret_cast<char*>( 6655 char* chars = reinterpret_cast<char*>(
6487 Isolate::Current()->current_zone()->Allocate(len + 1)); 6656 Isolate::Current()->current_zone()->Allocate(len + 1));
6488 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); 6657 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags());
6489 return chars; 6658 return chars;
6490 } 6659 }
6491 6660
6492 } // namespace dart 6661 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698