Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/symbols.h" | 5 #include "vm/symbols.h" |
| 6 | 6 |
| 7 #include "vm/isolate.h" | 7 #include "vm/isolate.h" |
| 8 #include "vm/object.h" | 8 #include "vm/object.h" |
| 9 #include "vm/object_store.h" | 9 #include "vm/object_store.h" |
| 10 #include "vm/raw_object.h" | 10 #include "vm/raw_object.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 static const char* names[] = { | 21 static const char* names[] = { |
| 22 NULL, | 22 NULL, |
| 23 | 23 |
| 24 #define DEFINE_SYMBOL_LITERAL(symbol, literal) \ | 24 #define DEFINE_SYMBOL_LITERAL(symbol, literal) \ |
| 25 literal, | 25 literal, |
| 26 PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_LITERAL) | 26 PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_LITERAL) |
| 27 #undef DEFINE_SYMBOL_LITERAL | 27 #undef DEFINE_SYMBOL_LITERAL |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 | 30 |
| 31 const char* Symbols::Name(intptr_t symbol) { | 31 const char* Symbols::Name(SymbolId symbol) { |
|
Ivan Posva
2012/11/15 09:07:10
I find it unfortunate that we cannot access the na
Florian Schneider
2012/11/15 21:54:42
Yes, but not sure if the names are actually needed
| |
| 32 ASSERT((symbol > kIllegal) && (symbol < kMaxId)); | 32 ASSERT((symbol > kIllegal) && (symbol < kMaxPredefinedId)); |
|
Ivan Posva
2012/11/15 09:07:10
I find it unfortunate that we cannot access the na
| |
| 33 return names[symbol]; | 33 return names[symbol]; |
| 34 } | 34 } |
| 35 | 35 |
| 36 | 36 |
| 37 void Symbols::InitOnce(Isolate* isolate) { | 37 void Symbols::InitOnce(Isolate* isolate) { |
| 38 // Should only be run by the vm isolate. | 38 // Should only be run by the vm isolate. |
| 39 ASSERT(isolate == Dart::vm_isolate()); | 39 ASSERT(isolate == Dart::vm_isolate()); |
| 40 | 40 |
| 41 // Create and setup a symbol table in the vm isolate. | 41 // Create and setup a symbol table in the vm isolate. |
| 42 SetupSymbolTable(isolate); | 42 SetupSymbolTable(isolate); |
| 43 | 43 |
| 44 // Turn off population of symbols in the VM symbol table, so that we | 44 // Turn off population of symbols in the VM symbol table, so that we |
| 45 // don't find these symbols while doing a Symbols::New(...). | 45 // don't find these symbols while doing a Symbols::New(...). |
| 46 // Create all predefined symbols. | 46 // Create all predefined symbols. |
| 47 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kMaxId); | 47 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kMaxPredefinedId); |
| 48 const Array& symbol_table = | 48 const Array& symbol_table = |
| 49 Array::Handle(isolate->object_store()->symbol_table()); | 49 Array::Handle(isolate->object_store()->symbol_table()); |
| 50 dart::String& str = String::Handle(); | 50 dart::String& str = String::Handle(); |
| 51 | 51 |
| 52 for (intptr_t i = 1; i < Symbols::kMaxId; i++) { | 52 for (intptr_t i = 1; i < Symbols::kMaxPredefinedId; i++) { |
|
Ivan Posva
2012/11/15 09:07:10
if the single char strings would be part of the na
| |
| 53 str = OneByteString::New(names[i], Heap::kOld); | 53 str = OneByteString::New(names[i], Heap::kOld); |
| 54 Add(symbol_table, str); | 54 Add(symbol_table, str); |
|
Ivan Posva
2012/11/15 09:07:10
The naked Add into the symbol table seems rather s
| |
| 55 predefined_[i] = str.raw(); | 55 predefined_[i] = str.raw(); |
| 56 } | 56 } |
| 57 Object::RegisterSingletonClassNames(); | 57 Object::RegisterSingletonClassNames(); |
| 58 | |
| 59 for (uint32_t c = 0; c <= kMaxOneByteCharCode; c++) { | |
| 60 ASSERT(kMaxPredefinedId + c < kMaxId); | |
| 61 predefined_[kMaxPredefinedId + c] = New(&c, 1); | |
| 62 } | |
| 58 } | 63 } |
| 59 | 64 |
| 60 | 65 |
| 61 void Symbols::SetupSymbolTable(Isolate* isolate) { | 66 void Symbols::SetupSymbolTable(Isolate* isolate) { |
| 62 ASSERT(isolate != NULL); | 67 ASSERT(isolate != NULL); |
| 63 | 68 |
| 64 // Setup the symbol table used within the String class. | 69 // Setup the symbol table used within the String class. |
| 65 const int initial_size = (isolate == Dart::vm_isolate()) ? | 70 const int initial_size = (isolate == Dart::vm_isolate()) ? |
| 66 kInitialVMIsolateSymtabSize : kInitialSymtabSize; | 71 kInitialVMIsolateSymtabSize : kInitialSymtabSize; |
| 67 const Array& array = Array::Handle(Array::New(initial_size + 1)); | 72 const Array& array = Array::Handle(Array::New(initial_size + 1)); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSMP)); | 116 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSMP)); |
| 112 uint16_t* characters = zone->Alloc<uint16_t>(len); | 117 uint16_t* characters = zone->Alloc<uint16_t>(len); |
| 113 Utf8::DecodeToUTF16(utf8_array, str_len, characters, len); | 118 Utf8::DecodeToUTF16(utf8_array, str_len, characters, len); |
| 114 return New(characters, len); | 119 return New(characters, len); |
| 115 } | 120 } |
| 116 | 121 |
| 117 | 122 |
| 118 template<typename T> | 123 template<typename T> |
| 119 RawString* Symbols::New(const T* characters, intptr_t len) { | 124 RawString* Symbols::New(const T* characters, intptr_t len) { |
| 120 Isolate* isolate = Isolate::Current(); | 125 Isolate* isolate = Isolate::Current(); |
| 121 ASSERT(isolate != Dart::vm_isolate()); | |
|
Ivan Posva
2012/11/15 09:07:10
If single char was handled in names, you would not
Florian Schneider
2012/11/15 21:54:42
It is a bit unfortunate, but I also want to avoid
| |
| 122 String& symbol = String::Handle(isolate, String::null()); | 126 String& symbol = String::Handle(isolate, String::null()); |
| 123 Array& symbol_table = Array::Handle(isolate, Array::null()); | 127 Array& symbol_table = Array::Handle(isolate, Array::null()); |
| 124 | 128 |
| 125 // Calculate the String hash for this sequence of characters. | 129 // Calculate the String hash for this sequence of characters. |
| 126 intptr_t hash = String::Hash(characters, len); | 130 intptr_t hash = String::Hash(characters, len); |
| 127 | 131 |
| 128 // First check if a symbol exists in the vm isolate for these characters. | 132 // First check if a symbol exists in the vm isolate for these characters. |
| 129 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); | 133 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); |
| 130 intptr_t index = FindIndex(symbol_table, characters, len, hash); | 134 intptr_t index = FindIndex(symbol_table, characters, len, hash); |
| 131 symbol ^= symbol_table.At(index); | 135 symbol ^= symbol_table.At(index); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 symbol.SetHash(hash); | 198 symbol.SetHash(hash); |
| 195 } | 199 } |
| 196 InsertIntoSymbolTable(symbol_table, symbol, index); | 200 InsertIntoSymbolTable(symbol_table, symbol, index); |
| 197 } | 201 } |
| 198 } | 202 } |
| 199 ASSERT(symbol.IsSymbol()); | 203 ASSERT(symbol.IsSymbol()); |
| 200 return symbol.raw(); | 204 return symbol.raw(); |
| 201 } | 205 } |
| 202 | 206 |
| 203 | 207 |
| 208 RawString* Symbols::FromCharCode(uint32_t char_code) { | |
| 209 if (char_code > kMaxOneByteCharCode) { | |
| 210 return New(&char_code, 1); | |
| 211 } | |
| 212 return predefined_[kMaxPredefinedId + char_code]; | |
| 213 } | |
| 214 | |
| 215 | |
| 204 void Symbols::GrowSymbolTable(const Array& symbol_table) { | 216 void Symbols::GrowSymbolTable(const Array& symbol_table) { |
| 205 // TODO(iposva): Avoid exponential growth. | 217 // TODO(iposva): Avoid exponential growth. |
| 206 intptr_t table_size = symbol_table.Length() - 1; | 218 intptr_t table_size = symbol_table.Length() - 1; |
| 207 intptr_t new_table_size = table_size * 2; | 219 intptr_t new_table_size = table_size * 2; |
| 208 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); | 220 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); |
| 209 // Copy all elements from the original symbol table to the newly allocated | 221 // Copy all elements from the original symbol table to the newly allocated |
| 210 // array. | 222 // array. |
| 211 String& element = String::Handle(); | 223 String& element = String::Handle(); |
| 212 dart::Object& new_element = Object::Handle(); | 224 dart::Object& new_element = Object::Handle(); |
| 213 for (intptr_t i = 0; i < table_size; i++) { | 225 for (intptr_t i = 0; i < table_size; i++) { |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 312 } | 324 } |
| 313 | 325 |
| 314 | 326 |
| 315 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 327 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 316 ASSERT(IsVMSymbolId(object_id)); | 328 ASSERT(IsVMSymbolId(object_id)); |
| 317 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 329 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 318 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); | 330 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); |
| 319 } | 331 } |
| 320 | 332 |
| 321 } // namespace dart | 333 } // namespace dart |
| OLD | NEW |