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

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

Issue 9117015: Add functions to list library and script urls in an isolate. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 years, 11 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) 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 4026 matching lines...) Expand 10 before | Expand all | Expand 10 after
4037 Smi& used = Smi::Handle(); 4037 Smi& used = Smi::Handle();
4038 used ^= dict.At(dict_size); 4038 used ^= dict.At(dict_size);
4039 intptr_t used_elements = used.Value() + 1; // One more element added. 4039 intptr_t used_elements = used.Value() + 1; // One more element added.
4040 used = Smi::New(used_elements); 4040 used = Smi::New(used_elements);
4041 dict.SetAt(dict_size, used); // Update used count. 4041 dict.SetAt(dict_size, used); // Update used count.
4042 4042
4043 // Rehash if symbol_table is 75% full. 4043 // Rehash if symbol_table is 75% full.
4044 if (used_elements > ((dict_size / 4) * 3)) { 4044 if (used_elements > ((dict_size / 4) * 3)) {
4045 GrowDictionary(dict, dict_size); 4045 GrowDictionary(dict, dict_size);
4046 } 4046 }
4047
4048 // Invalidate the cache of loaded scripts.
4049 if (loaded_scripts() != Array::null()) {
4050 StorePointer(&raw_ptr()->loaded_scripts_, Array::null());
4051 }
siva 2012/01/24 18:37:14 Should this be an assertion that loaded_scripts sh
hausner 2012/01/24 21:34:42 See comment below.
4047 } 4052 }
4048 4053
4049 4054
4050 void Library::AddClass(const Class& cls) const { 4055 void Library::AddClass(const Class& cls) const {
4051 AddObject(cls, String::Handle(cls.Name())); 4056 AddObject(cls, String::Handle(cls.Name()));
4052 // Link class to this library. 4057 // Link class to this library.
4053 cls.set_library(*this); 4058 cls.set_library(*this);
4054 } 4059 }
4055 4060
4056 4061
4062 RawArray* Library::LoadedScripts() const {
4063 // We compute the list of loaded scripts lazily. The result is
4064 // cached in loaded_scripts_.
siva 2012/01/24 18:37:14 Should we assert that this should be called only a
hausner 2012/01/24 21:34:42 I had an assert at first that made sure this is on
4065 if (loaded_scripts() == Array::null()) {
4066 // Iterate over the library dictionary and collect all scripts.
4067 GrowableArray<Script*> scripts(8);
4068 Object& entry = Object::Handle();
4069 Function& func = Function::Handle();
4070 Field& field = Field::Handle();
4071 Class& cls = Class::Handle();
4072 Script& owner_script = Script::Handle();
4073 DictionaryIterator it(*this);
4074 while (it.HasNext()) {
4075 entry = it.GetNext();
4076 if (entry.IsClass()) {
4077 cls ^= entry.raw();
4078 } else if (entry.IsFunction()) {
4079 func ^= entry.raw();
4080 cls = func.owner();
4081 } else if (entry.IsField()) {
4082 field ^= entry.raw();
4083 cls = field.owner();
4084 } else {
4085 continue;
4086 }
4087 owner_script = cls.script();
4088 if (owner_script.IsNull()) {
4089 continue;
4090 }
4091 bool is_unique = true;
4092 for (int i = 0; i < scripts.length(); i++) {
4093 if (scripts[i]->raw() == owner_script.raw()) {
4094 // We already have a reference to this script.
4095 is_unique = false;
4096 break;
4097 }
4098 }
4099 if (is_unique) {
4100 // Create a unique script handle and add it to the list of scripts.
4101 Script& unique_script = Script::Handle(owner_script.raw());
4102 scripts.Add(&unique_script);
4103 }
4104 }
4105
4106 // Create the array of scripts and cache it in loaded_scripts_.
4107 const Array& loaded_scripts =
4108 Array::Handle(Array::New(scripts.length(), Heap::kOld));
4109 for (int i = 0; i < scripts.length(); i++) {
4110 loaded_scripts.SetAt(i, *scripts[i]);
4111 }
4112 StorePointer(&raw_ptr()->loaded_scripts_, loaded_scripts.raw());
4113 }
4114 return loaded_scripts();
4115 }
4116
4117
4057 // TODO(hausner): we might want to add a script dictionary to the 4118 // TODO(hausner): we might want to add a script dictionary to the
4058 // library class to make this lookup less cumbersome. 4119 // library class to make this lookup faster.
4059 RawScript* Library::LookupScript(const String& url) const { 4120 RawScript* Library::LookupScript(const String& url) const {
4060 Object& entry = Object::Handle(); 4121 const Array& scripts = Array::Handle(LoadedScripts());
4061 Class& cls = Class::Handle(); 4122 Script& script = Script::Handle();
4062 Function& func = Function::Handle(); 4123 String& script_url = String::Handle();
4063 Field& field = Field::Handle(); 4124 intptr_t num_scripts = scripts.Length();
4064 Script& owner_script = Script::Handle(); 4125 for (int i = 0; i < num_scripts; i++) {
4065 String& owner_url = String::Handle(); 4126 script ^= scripts.At(i);
4066 4127 script_url = script.url();
4067 DictionaryIterator it(*this); 4128 if (script_url.Equals(url)) {
4068 while (it.HasNext()) { 4129 return script.raw();
4069 entry = it.GetNext();
4070 if (entry.IsClass()) {
4071 cls ^= entry.raw();
4072 } else if (entry.IsFunction()) {
4073 func ^= entry.raw();
4074 cls = func.owner();
4075 } else if (entry.IsField()) {
4076 field ^= entry.raw();
4077 cls = field.owner();
4078 } else {
4079 continue;
4080 }
4081 owner_script = cls.script();
4082 if (owner_script.IsNull()) {
4083 continue;
4084 }
4085 owner_url = owner_script.url();
4086 if (owner_url.Equals(url)) {
4087 return owner_script.raw();
4088 } 4130 }
4089 } 4131 }
4090 return Script::null(); 4132 return Script::null();
4091 } 4133 }
4092 4134
4093 4135
4094 RawFunction* Library::LookupFunctionInSource(const String& script_url, 4136 RawFunction* Library::LookupFunctionInSource(const String& script_url,
4095 intptr_t line_number) const { 4137 intptr_t line_number) const {
4096 Script& script = Script::Handle(LookupScript(script_url)); 4138 Script& script = Script::Handle(LookupScript(script_url));
4097 if (script.IsNull()) { 4139 if (script.IsNull()) {
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
4428 bool import_core_lib) { 4470 bool import_core_lib) {
4429 const Library& result = Library::Handle(Library::New()); 4471 const Library& result = Library::Handle(Library::New());
4430 result.raw_ptr()->name_ = url.raw(); 4472 result.raw_ptr()->name_ = url.raw();
4431 result.raw_ptr()->url_ = url.raw(); 4473 result.raw_ptr()->url_ = url.raw();
4432 result.raw_ptr()->private_key_ = Scanner::AllocatePrivateKey(result); 4474 result.raw_ptr()->private_key_ = Scanner::AllocatePrivateKey(result);
4433 result.raw_ptr()->dictionary_ = Array::Empty(); 4475 result.raw_ptr()->dictionary_ = Array::Empty();
4434 result.raw_ptr()->anonymous_classes_ = Array::Empty(); 4476 result.raw_ptr()->anonymous_classes_ = Array::Empty();
4435 result.raw_ptr()->num_anonymous_ = 0; 4477 result.raw_ptr()->num_anonymous_ = 0;
4436 result.raw_ptr()->imports_ = Array::Empty(); 4478 result.raw_ptr()->imports_ = Array::Empty();
4437 result.raw_ptr()->next_registered_ = Library::null(); 4479 result.raw_ptr()->next_registered_ = Library::null();
4480 result.raw_ptr()->loaded_scripts_ = Array::null();
4438 result.set_native_entry_resolver(NULL); 4481 result.set_native_entry_resolver(NULL);
4439 result.raw_ptr()->corelib_imported_ = true; 4482 result.raw_ptr()->corelib_imported_ = true;
4440 result.raw_ptr()->load_state_ = RawLibrary::kAllocated; 4483 result.raw_ptr()->load_state_ = RawLibrary::kAllocated;
4441 result.InitClassDictionary(); 4484 result.InitClassDictionary();
4442 result.InitImportList(); 4485 result.InitImportList();
4443 result.InitImportedIntoList(); 4486 result.InitImportedIntoList();
4444 if (import_core_lib) { 4487 if (import_core_lib) {
4445 Library& core_lib = Library::Handle(Library::CoreLibrary()); 4488 Library& core_lib = Library::Handle(Library::CoreLibrary());
4446 ASSERT(!core_lib.IsNull()); 4489 ASSERT(!core_lib.IsNull());
4447 result.AddImport(core_lib); 4490 result.AddImport(core_lib);
(...skipping 3334 matching lines...) Expand 10 before | Expand all | Expand 10 after
7782 const String& str = String::Handle(pattern()); 7825 const String& str = String::Handle(pattern());
7783 const char* format = "JSRegExp: pattern=%s flags=%s"; 7826 const char* format = "JSRegExp: pattern=%s flags=%s";
7784 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags()); 7827 intptr_t len = OS::SNPrint(NULL, 0, format, str.ToCString(), Flags());
7785 char* chars = reinterpret_cast<char*>( 7828 char* chars = reinterpret_cast<char*>(
7786 Isolate::Current()->current_zone()->Allocate(len + 1)); 7829 Isolate::Current()->current_zone()->Allocate(len + 1));
7787 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags()); 7830 OS::SNPrint(chars, (len + 1), format, str.ToCString(), Flags());
7788 return chars; 7831 return chars;
7789 } 7832 }
7790 7833
7791 } // namespace dart 7834 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698