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

Side by Side Diff: runtime/vm/object.cc

Issue 10829431: Eliminate imported_into pointers in Library (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 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 | « runtime/vm/object.h ('k') | runtime/vm/raw_object.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 "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/bigint_operations.h" 10 #include "vm/bigint_operations.h"
(...skipping 5919 matching lines...) Expand 10 before | Expand all | Expand 10 after
5930 } 5930 }
5931 if (obj.IsFunction()) { 5931 if (obj.IsFunction()) {
5932 return Function::Cast(obj).raw(); 5932 return Function::Cast(obj).raw();
5933 } 5933 }
5934 5934
5935 // No function found. 5935 // No function found.
5936 return Function::null(); 5936 return Function::null();
5937 } 5937 }
5938 5938
5939 5939
5940 RawObject* Library::LookupObjectFiltered(const String& name, 5940 RawObject* Library::LookupObject(const String& name) const {
5941 const Library& filter_lib) const {
5942 // First check if name is found in the local scope of the library. 5941 // First check if name is found in the local scope of the library.
5943 Object& obj = Object::Handle(LookupLocalObject(name)); 5942 Object& obj = Object::Handle(LookupLocalObject(name));
5944 if (!obj.IsNull()) { 5943 if (!obj.IsNull()) {
5945 return obj.raw(); 5944 return obj.raw();
5946 } 5945 }
5947 // Now check if name is found in the top level scope of any imported libs. 5946 // Now check if name is found in the top level scope of any imported libs.
5948 const Array& imports = Array::Handle(this->imports()); 5947 const Array& imports = Array::Handle(this->imports());
5949 Library& import_lib = Library::Handle(); 5948 Library& import_lib = Library::Handle();
5950 for (intptr_t j = 0; j < this->num_imports(); j++) { 5949 for (intptr_t j = 0; j < this->num_imports(); j++) {
5951 import_lib ^= imports.At(j); 5950 import_lib ^= imports.At(j);
5952 // Skip over the library that we need to filter out.
5953 if (!filter_lib.IsNull() && import_lib.raw() == filter_lib.raw()) {
5954 continue;
5955 }
5956 obj = import_lib.LookupLocalObject(name); 5951 obj = import_lib.LookupLocalObject(name);
5957 if (!obj.IsNull()) { 5952 if (!obj.IsNull()) {
5958 return obj.raw(); 5953 return obj.raw();
5959 } 5954 }
5960 } 5955 }
5961 return Object::null(); 5956 return Object::null();
5962 } 5957 }
5963 5958
5964 5959
5965 RawObject* Library::LookupObject(const String& name) const {
5966 return LookupObjectFiltered(name, Library::Handle());
5967 }
5968
5969
5970 RawLibrary* Library::LookupObjectInImporter(const String& name) const {
5971 Isolate* isolate = Isolate::Current();
5972 const Array& imported_into_libs = Array::Handle(isolate,
5973 this->imported_into());
5974 Library& lib = Library::Handle(isolate, Library::null());
5975 Object& obj = Object::Handle(isolate, Object::null());
5976 for (intptr_t i = 0; i < this->num_imported_into(); i++) {
5977 lib ^= imported_into_libs.At(i);
5978 obj = lib.LookupObjectFiltered(name, *this);
5979 if (!obj.IsNull()) {
5980 // If the object found is a class, field or function extract the
5981 // library in which it is defined as it might be defined in one of
5982 // the imported libraries.
5983 Class& cls = Class::Handle(isolate, Class::null());
5984 Function& func = Function::Handle(isolate, Function::null());
5985 Field& field = Field::Handle(isolate, Field::null());
5986 if (obj.IsClass()) {
5987 cls ^= obj.raw();
5988 lib ^= cls.library();
5989 } else if (obj.IsFunction()) {
5990 func ^= obj.raw();
5991 cls ^= func.Owner();
5992 lib ^= cls.library();
5993 } else if (obj.IsField()) {
5994 field ^= obj.raw();
5995 cls ^= field.owner();
5996 lib ^= cls.library();
5997 }
5998 return lib.raw();
5999 }
6000 }
6001 return Library::null();
6002 }
6003
6004
6005 RawString* Library::DuplicateDefineErrorString(const String& entry_name,
6006 const Library& conflict) const {
6007 String& errstr = String::Handle();
6008 Array& array = Array::Handle(Array::New(7));
6009 errstr = String::New("'");
6010 array.SetAt(0, errstr);
6011 array.SetAt(1, entry_name);
6012 errstr = String::New("' is defined in '");
6013 array.SetAt(2, errstr);
6014 errstr = url();
6015 array.SetAt(3, errstr);
6016 errstr = String::New("' and '");
6017 array.SetAt(4, errstr);
6018 errstr = conflict.url();
6019 array.SetAt(5, errstr);
6020 errstr = String::New("'");
6021 array.SetAt(6, errstr);
6022 errstr = String::ConcatAll(array);
6023 return errstr.raw();
6024 }
6025
6026
6027 RawString* Library::FindDuplicateDefinition() const {
6028 DictionaryIterator it(*this);
6029 Object& obj = Object::Handle();
6030 Class& cls = Class::Handle();
6031 Function& func = Function::Handle();
6032 Field& field = Field::Handle();
6033 String& entry_name = String::Handle();
6034 String& error_message = String::Handle();
6035 Library& conflicting_lib = Library::Handle();
6036 LibraryPrefix& lib_prefix = LibraryPrefix::Handle();
6037 while (it.HasNext()) {
6038 obj = it.GetNext();
6039 ASSERT(!obj.IsNull());
6040 if (obj.IsClass()) {
6041 cls ^= obj.raw();
6042 if (cls.IsCanonicalSignatureClass()) {
6043 continue;
6044 }
6045 entry_name = cls.Name();
6046 } else if (obj.IsFunction()) {
6047 func ^= obj.raw();
6048 entry_name = func.name();
6049 } else if (obj.IsField()) {
6050 field ^= obj.raw();
6051 entry_name = field.name();
6052 } else if (obj.IsLibraryPrefix()) {
6053 // For library prefix objects we check to make sure there are no
6054 // duplicate definitions within the libraries imported using this
6055 // prefix.
6056 lib_prefix ^= obj.raw();
6057 error_message = lib_prefix.CheckForDuplicateDefinition();
6058 if (!error_message.IsNull()) {
6059 return error_message.raw();
6060 }
6061 // We don't check library prefixes defined in this library for
6062 // conflicts because they are not visible in the importing scope and
6063 // hence cannot cause any duplicate definitions.
6064 continue;
6065 } else {
6066 UNREACHABLE();
6067 }
6068 conflicting_lib = LookupObjectInImporter(entry_name);
6069 if (!conflicting_lib.IsNull()) {
6070 return this->DuplicateDefineErrorString(entry_name, conflicting_lib);
6071 }
6072 }
6073 return String::null();
6074 }
6075
6076
6077 RawClass* Library::LookupClass(const String& name) const { 5960 RawClass* Library::LookupClass(const String& name) const {
6078 Object& obj = Object::Handle(LookupObject(name)); 5961 Object& obj = Object::Handle(LookupObject(name));
6079 if (!obj.IsNull() && obj.IsClass()) { 5962 if (!obj.IsNull() && obj.IsClass()) {
6080 return Class::CheckedHandle(obj.raw()).raw(); 5963 return Class::CheckedHandle(obj.raw()).raw();
6081 } 5964 }
6082 return Class::null(); 5965 return Class::null();
6083 } 5966 }
6084 5967
6085 5968
6086 RawClass* Library::LookupLocalClass(const String& name) const { 5969 RawClass* Library::LookupLocalClass(const String& name) const {
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
6190 Array& imports = Array::Handle(this->imports()); 6073 Array& imports = Array::Handle(this->imports());
6191 intptr_t capacity = imports.Length(); 6074 intptr_t capacity = imports.Length();
6192 if (num_imports() == capacity) { 6075 if (num_imports() == capacity) {
6193 capacity = capacity + kImportsCapacityIncrement; 6076 capacity = capacity + kImportsCapacityIncrement;
6194 imports = Array::Grow(imports, capacity); 6077 imports = Array::Grow(imports, capacity);
6195 StorePointer(&raw_ptr()->imports_, imports.raw()); 6078 StorePointer(&raw_ptr()->imports_, imports.raw());
6196 } 6079 }
6197 intptr_t index = num_imports(); 6080 intptr_t index = num_imports();
6198 imports.SetAt(index, library); 6081 imports.SetAt(index, library);
6199 set_num_imports(index + 1); 6082 set_num_imports(index + 1);
6200 library.AddImportedInto(*this);
6201 }
6202
6203
6204 void Library::AddImportedInto(const Library& library) const {
6205 Array& imported_into = Array::Handle(this->imported_into());
6206 intptr_t capacity = imported_into.Length();
6207 if (num_imported_into() == capacity) {
6208 capacity = capacity + kImportedIntoCapacityIncrement;
6209 imported_into = Array::Grow(imported_into, capacity);
6210 StorePointer(&raw_ptr()->imported_into_, imported_into.raw());
6211 }
6212 intptr_t index = num_imported_into();
6213 imported_into.SetAt(index, library);
6214 set_num_imported_into(index + 1);
6215 } 6083 }
6216 6084
6217 6085
6218 void Library::InitClassDictionary() const { 6086 void Library::InitClassDictionary() const {
6219 // The last element of the dictionary specifies the number of in use slots. 6087 // The last element of the dictionary specifies the number of in use slots.
6220 // TODO(iposva): Find reasonable initial size. 6088 // TODO(iposva): Find reasonable initial size.
6221 const int kInitialElementCount = 16; 6089 const int kInitialElementCount = 16;
6222 6090
6223 const Array& dictionary = 6091 const Array& dictionary =
6224 Array::Handle(Array::New(kInitialElementCount + 1, Heap::kOld)); 6092 Array::Handle(Array::New(kInitialElementCount + 1, Heap::kOld));
6225 dictionary.SetAt(kInitialElementCount, Smi::Handle(Smi::New(0))); 6093 dictionary.SetAt(kInitialElementCount, Smi::Handle(Smi::New(0)));
6226 StorePointer(&raw_ptr()->dictionary_, dictionary.raw()); 6094 StorePointer(&raw_ptr()->dictionary_, dictionary.raw());
6227 } 6095 }
6228 6096
6229 6097
6230 void Library::InitImportList() const { 6098 void Library::InitImportList() const {
6231 const Array& imports = 6099 const Array& imports =
6232 Array::Handle(Array::New(kInitialImportsCapacity, Heap::kOld)); 6100 Array::Handle(Array::New(kInitialImportsCapacity, Heap::kOld));
6233 StorePointer(&raw_ptr()->imports_, imports.raw()); 6101 StorePointer(&raw_ptr()->imports_, imports.raw());
6234 raw_ptr()->num_imports_ = 0; 6102 raw_ptr()->num_imports_ = 0;
6235 } 6103 }
6236 6104
6237 6105
6238 void Library::InitImportedIntoList() const {
6239 const Array& imported_into =
6240 Array::Handle(Array::New(kInitialImportedIntoCapacity, Heap::kOld));
6241 StorePointer(&raw_ptr()->imported_into_, imported_into.raw());
6242 raw_ptr()->num_imported_into_ = 0;
6243 }
6244
6245
6246 RawLibrary* Library::New() { 6106 RawLibrary* Library::New() {
6247 ASSERT(Object::library_class() != Class::null()); 6107 ASSERT(Object::library_class() != Class::null());
6248 RawObject* raw = Object::Allocate(Library::kClassId, 6108 RawObject* raw = Object::Allocate(Library::kClassId,
6249 Library::InstanceSize(), 6109 Library::InstanceSize(),
6250 Heap::kOld); 6110 Heap::kOld);
6251 return reinterpret_cast<RawLibrary*>(raw); 6111 return reinterpret_cast<RawLibrary*>(raw);
6252 } 6112 }
6253 6113
6254 6114
6255 RawLibrary* Library::NewLibraryHelper(const String& url, 6115 RawLibrary* Library::NewLibraryHelper(const String& url,
6256 bool import_core_lib) { 6116 bool import_core_lib) {
6257 const Library& result = Library::Handle(Library::New()); 6117 const Library& result = Library::Handle(Library::New());
6258 result.StorePointer(&result.raw_ptr()->name_, url.raw()); 6118 result.StorePointer(&result.raw_ptr()->name_, url.raw());
6259 result.StorePointer(&result.raw_ptr()->url_, url.raw()); 6119 result.StorePointer(&result.raw_ptr()->url_, url.raw());
6260 result.raw_ptr()->private_key_ = Scanner::AllocatePrivateKey(result); 6120 result.raw_ptr()->private_key_ = Scanner::AllocatePrivateKey(result);
6261 result.raw_ptr()->dictionary_ = Object::empty_array(); 6121 result.raw_ptr()->dictionary_ = Object::empty_array();
6262 result.raw_ptr()->anonymous_classes_ = Object::empty_array(); 6122 result.raw_ptr()->anonymous_classes_ = Object::empty_array();
6263 result.raw_ptr()->num_anonymous_ = 0; 6123 result.raw_ptr()->num_anonymous_ = 0;
6264 result.raw_ptr()->imports_ = Object::empty_array(); 6124 result.raw_ptr()->imports_ = Object::empty_array();
6265 result.raw_ptr()->loaded_scripts_ = Array::null(); 6125 result.raw_ptr()->loaded_scripts_ = Array::null();
6266 result.set_native_entry_resolver(NULL); 6126 result.set_native_entry_resolver(NULL);
6267 result.raw_ptr()->corelib_imported_ = true; 6127 result.raw_ptr()->corelib_imported_ = true;
6268 result.set_debuggable(false); 6128 result.set_debuggable(false);
6269 result.raw_ptr()->load_state_ = RawLibrary::kAllocated; 6129 result.raw_ptr()->load_state_ = RawLibrary::kAllocated;
6270 result.raw_ptr()->index_ = -1; 6130 result.raw_ptr()->index_ = -1;
6271 result.InitClassDictionary(); 6131 result.InitClassDictionary();
6272 result.InitImportList(); 6132 result.InitImportList();
6273 result.InitImportedIntoList();
6274 if (import_core_lib) { 6133 if (import_core_lib) {
6275 Library& core_lib = Library::Handle(Library::CoreLibrary()); 6134 Library& core_lib = Library::Handle(Library::CoreLibrary());
6276 ASSERT(!core_lib.IsNull()); 6135 ASSERT(!core_lib.IsNull());
6277 result.AddImport(core_lib); 6136 result.AddImport(core_lib);
6278 } 6137 }
6279 return result.raw(); 6138 return result.raw();
6280 } 6139 }
6281 6140
6282 6141
6283 RawLibrary* Library::New(const String& url) { 6142 RawLibrary* Library::New(const String& url) {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
6376 lib ^= libs.At(i); 6235 lib ^= libs.At(i);
6377 lib_url = lib.url(); 6236 lib_url = lib.url();
6378 if (lib_url.Equals(url)) { 6237 if (lib_url.Equals(url)) {
6379 return lib.raw(); 6238 return lib.raw();
6380 } 6239 }
6381 } 6240 }
6382 return Library::null(); 6241 return Library::null();
6383 } 6242 }
6384 6243
6385 6244
6386 RawString* Library::CheckForDuplicateDefinition() {
6387 Isolate* isolate = Isolate::Current();
6388 ASSERT(isolate != NULL);
6389 ObjectStore* object_store = isolate->object_store();
6390 ASSERT(object_store != NULL);
6391 const GrowableObjectArray& libs =
6392 GrowableObjectArray::Handle(object_store->libraries());
6393 Library& lib = Library::Handle();
6394 String& error_message = String::Handle();
6395 for (int i = 0; i < libs.Length(); i++) {
6396 lib ^= libs.At(i);
6397 error_message = lib.FindDuplicateDefinition();
6398 if (!error_message.IsNull()) {
6399 return error_message.raw();
6400 }
6401 }
6402 return String::null();
6403 }
6404
6405
6406 RawError* Library::Patch(const Script& script) const { 6245 RawError* Library::Patch(const Script& script) const {
6407 ASSERT(script.kind() == RawScript::kPatchTag); 6246 ASSERT(script.kind() == RawScript::kPatchTag);
6408 return Compiler::Compile(*this, script); 6247 return Compiler::Compile(*this, script);
6409 } 6248 }
6410 6249
6411 6250
6412 bool Library::IsKeyUsed(intptr_t key) { 6251 bool Library::IsKeyUsed(intptr_t key) {
6413 intptr_t lib_key; 6252 intptr_t lib_key;
6414 const GrowableObjectArray& libs = GrowableObjectArray::Handle( 6253 const GrowableObjectArray& libs = GrowableObjectArray::Handle(
6415 Isolate::Current()->object_store()->libraries()); 6254 Isolate::Current()->object_store()->libraries());
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
6590 const char* LibraryPrefix::ToCString() const { 6429 const char* LibraryPrefix::ToCString() const {
6591 const char* kFormat = "LibraryPrefix:'%s'"; 6430 const char* kFormat = "LibraryPrefix:'%s'";
6592 const String& prefix = String::Handle(name()); 6431 const String& prefix = String::Handle(name());
6593 intptr_t len = OS::SNPrint(NULL, 0, kFormat, prefix.ToCString()) + 1; 6432 intptr_t len = OS::SNPrint(NULL, 0, kFormat, prefix.ToCString()) + 1;
6594 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len); 6433 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len);
6595 OS::SNPrint(chars, len, kFormat, prefix.ToCString()); 6434 OS::SNPrint(chars, len, kFormat, prefix.ToCString());
6596 return chars; 6435 return chars;
6597 } 6436 }
6598 6437
6599 6438
6600 RawString* LibraryPrefix::CheckForDuplicateDefinition() const {
6601 Library& lib = Library::Handle();
6602 Library& conflicting_lib = Library::Handle();
6603 Object& obj = Object::Handle();
6604 Class& cls = Class::Handle();
6605 Function& func = Function::Handle();
6606 Field& field = Field::Handle();
6607 String& entry_name = String::Handle();
6608
6609 for (intptr_t i = 0; i < num_libs(); i++) {
6610 lib = GetLibrary(i);
6611 ASSERT(!lib.IsNull());
6612 DictionaryIterator it(lib);
6613 while (it.HasNext()) {
6614 obj = it.GetNext();
6615 ASSERT(!obj.IsNull());
6616 if (obj.IsClass()) {
6617 cls ^= obj.raw();
6618 if (cls.IsCanonicalSignatureClass()) {
6619 continue;
6620 }
6621 entry_name = cls.Name();
6622 } else if (obj.IsFunction()) {
6623 func ^= obj.raw();
6624 entry_name = func.name();
6625 } else if (obj.IsField()) {
6626 field ^= obj.raw();
6627 entry_name = field.name();
6628 } else {
6629 // We don't check library prefixes defined in this library for
6630 // conflicts because they are not visible in the importing scope and
6631 // hence cannot cause any duplicate definitions.
6632 continue;
6633 }
6634 for (intptr_t j = i + 1; j < num_libs(); j++) {
6635 conflicting_lib = GetLibrary(j);
6636 ASSERT(!conflicting_lib.IsNull());
6637 // Check if name is found in the local scope of the library.
6638 obj = conflicting_lib.LookupLocalObject(entry_name);
6639 if (!obj.IsNull()) {
6640 return lib.DuplicateDefineErrorString(entry_name, conflicting_lib);
6641 }
6642 }
6643 }
6644 }
6645 return String::null();
6646 }
6647
6648
6649 void LibraryPrefix::set_name(const String& value) const { 6439 void LibraryPrefix::set_name(const String& value) const {
6650 ASSERT(value.IsSymbol()); 6440 ASSERT(value.IsSymbol());
6651 StorePointer(&raw_ptr()->name_, value.raw()); 6441 StorePointer(&raw_ptr()->name_, value.raw());
6652 } 6442 }
6653 6443
6654 6444
6655 void LibraryPrefix::set_libraries(const Array& value) const { 6445 void LibraryPrefix::set_libraries(const Array& value) const {
6656 StorePointer(&raw_ptr()->libraries_, value.raw()); 6446 StorePointer(&raw_ptr()->libraries_, value.raw());
6657 } 6447 }
6658 6448
(...skipping 4672 matching lines...) Expand 10 before | Expand all | Expand 10 after
11331 } 11121 }
11332 return result.raw(); 11122 return result.raw();
11333 } 11123 }
11334 11124
11335 11125
11336 const char* WeakProperty::ToCString() const { 11126 const char* WeakProperty::ToCString() const {
11337 return "WeakProperty"; 11127 return "WeakProperty";
11338 } 11128 }
11339 11129
11340 } // namespace dart 11130 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/raw_object.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698