| 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" |
| 11 #include "vm/snapshot_ids.h" | 11 #include "vm/snapshot_ids.h" |
| 12 #include "vm/unicode.h" | 12 #include "vm/unicode.h" |
| 13 #include "vm/visitor.h" | 13 #include "vm/visitor.h" |
| 14 | 14 |
| 15 namespace dart { | 15 namespace dart { |
| 16 | 16 |
| 17 RawString* Symbols::predefined_[Symbols::kMaxId]; | 17 RawString* Symbols::predefined_[Symbols::kMaxId]; |
| 18 | 18 |
| 19 static const char* names[] = { | 19 static const char* names[] = { |
| 20 NULL, | 20 NULL, |
| 21 | 21 |
| 22 #define DEFINE_SYMBOL_LITERAL(symbol, literal) \ | 22 #define DEFINE_SYMBOL_LITERAL(symbol, literal) \ |
| 23 literal, | 23 literal, |
| 24 PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_LITERAL) | 24 PREDEFINED_SYMBOLS_LIST(DEFINE_SYMBOL_LITERAL) |
| 25 #undef DEFINE_SYMBOL_LITERAL | 25 #undef DEFINE_SYMBOL_LITERAL |
| 26 }; | 26 }; |
| 27 | 27 |
| 28 | 28 |
| 29 const char* Symbols::Name(intptr_t symbol) { | 29 const char* Symbols::Name(SymbolId symbol) { |
| 30 ASSERT((symbol > kIllegal) && (symbol < kMaxId)); | 30 ASSERT((symbol > kIllegal) && (symbol < kMaxPredefinedId)); |
| 31 return names[symbol]; | 31 return names[symbol]; |
| 32 } | 32 } |
| 33 | 33 |
| 34 | 34 |
| 35 void Symbols::InitOnce(Isolate* isolate) { | 35 void Symbols::InitOnce(Isolate* isolate) { |
| 36 // Should only be run by the vm isolate. | 36 // Should only be run by the vm isolate. |
| 37 ASSERT(isolate == Dart::vm_isolate()); | 37 ASSERT(isolate == Dart::vm_isolate()); |
| 38 | 38 |
| 39 // Create and setup a symbol table in the vm isolate. | 39 // Create and setup a symbol table in the vm isolate. |
| 40 SetupSymbolTable(isolate); | 40 SetupSymbolTable(isolate); |
| 41 | 41 |
| 42 // Create all predefined symbols. | 42 // Create all predefined symbols. |
| 43 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kMaxId); | 43 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kMaxPredefinedId); |
| 44 ObjectStore* object_store = isolate->object_store(); | 44 ObjectStore* object_store = isolate->object_store(); |
| 45 Array& symbol_table = Array::Handle(); | 45 Array& symbol_table = Array::Handle(); |
| 46 dart::String& str = String::Handle(); | 46 dart::String& str = String::Handle(); |
| 47 | 47 |
| 48 for (intptr_t i = 1; i < Symbols::kMaxId; i++) { | 48 for (intptr_t i = 1; i < Symbols::kMaxPredefinedId; i++) { |
| 49 // The symbol_table needs to be reloaded as it might have grown in the | 49 // The symbol_table needs to be reloaded as it might have grown in the |
| 50 // previous iteration. | 50 // previous iteration. |
| 51 symbol_table = object_store->symbol_table(); | 51 symbol_table = object_store->symbol_table(); |
| 52 str = OneByteString::New(names[i], Heap::kOld); | 52 str = OneByteString::New(names[i], Heap::kOld); |
| 53 Add(symbol_table, str); | 53 Add(symbol_table, str); |
| 54 predefined_[i] = str.raw(); | 54 predefined_[i] = str.raw(); |
| 55 } | 55 } |
| 56 Object::RegisterSingletonClassNames(); | 56 Object::RegisterSingletonClassNames(); |
| 57 |
| 58 for (uint32_t c = 0; c <= kMaxOneByteCharCode; c++) { |
| 59 ASSERT(kMaxPredefinedId + c < kMaxId); |
| 60 predefined_[kMaxPredefinedId + c] = New(&c, 1); |
| 61 } |
| 57 } | 62 } |
| 58 | 63 |
| 59 | 64 |
| 60 void Symbols::SetupSymbolTable(Isolate* isolate) { | 65 void Symbols::SetupSymbolTable(Isolate* isolate) { |
| 61 ASSERT(isolate != NULL); | 66 ASSERT(isolate != NULL); |
| 62 | 67 |
| 63 // Setup the symbol table used within the String class. | 68 // Setup the symbol table used within the String class. |
| 64 const int initial_size = (isolate == Dart::vm_isolate()) ? | 69 const int initial_size = (isolate == Dart::vm_isolate()) ? |
| 65 kInitialVMIsolateSymtabSize : kInitialSymtabSize; | 70 kInitialVMIsolateSymtabSize : kInitialSymtabSize; |
| 66 const Array& array = Array::Handle(Array::New(initial_size + 1)); | 71 const Array& array = Array::Handle(Array::New(initial_size + 1)); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSMP)); | 115 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSMP)); |
| 111 uint16_t* characters = zone->Alloc<uint16_t>(len); | 116 uint16_t* characters = zone->Alloc<uint16_t>(len); |
| 112 Utf8::DecodeToUTF16(utf8_array, str_len, characters, len); | 117 Utf8::DecodeToUTF16(utf8_array, str_len, characters, len); |
| 113 return New(characters, len); | 118 return New(characters, len); |
| 114 } | 119 } |
| 115 | 120 |
| 116 | 121 |
| 117 template<typename T> | 122 template<typename T> |
| 118 RawString* Symbols::New(const T* characters, intptr_t len) { | 123 RawString* Symbols::New(const T* characters, intptr_t len) { |
| 119 Isolate* isolate = Isolate::Current(); | 124 Isolate* isolate = Isolate::Current(); |
| 120 ASSERT(isolate != Dart::vm_isolate()); | |
| 121 String& symbol = String::Handle(isolate, String::null()); | 125 String& symbol = String::Handle(isolate, String::null()); |
| 122 Array& symbol_table = Array::Handle(isolate, Array::null()); | 126 Array& symbol_table = Array::Handle(isolate, Array::null()); |
| 123 | 127 |
| 124 // Calculate the String hash for this sequence of characters. | 128 // Calculate the String hash for this sequence of characters. |
| 125 intptr_t hash = String::Hash(characters, len); | 129 intptr_t hash = String::Hash(characters, len); |
| 126 | 130 |
| 127 // First check if a symbol exists in the vm isolate for these characters. | 131 // First check if a symbol exists in the vm isolate for these characters. |
| 128 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); | 132 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); |
| 129 intptr_t index = FindIndex(symbol_table, characters, len, hash); | 133 intptr_t index = FindIndex(symbol_table, characters, len, hash); |
| 130 symbol ^= symbol_table.At(index); | 134 symbol ^= symbol_table.At(index); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 symbol.SetHash(hash); | 197 symbol.SetHash(hash); |
| 194 } | 198 } |
| 195 InsertIntoSymbolTable(symbol_table, symbol, index); | 199 InsertIntoSymbolTable(symbol_table, symbol, index); |
| 196 } | 200 } |
| 197 } | 201 } |
| 198 ASSERT(symbol.IsSymbol()); | 202 ASSERT(symbol.IsSymbol()); |
| 199 return symbol.raw(); | 203 return symbol.raw(); |
| 200 } | 204 } |
| 201 | 205 |
| 202 | 206 |
| 207 RawString* Symbols::FromCharCode(uint32_t char_code) { |
| 208 if (char_code > kMaxOneByteCharCode) { |
| 209 return New(&char_code, 1); |
| 210 } |
| 211 return predefined_[kNullCharId + char_code]; |
| 212 } |
| 213 |
| 214 |
| 203 void Symbols::GrowSymbolTable(const Array& symbol_table) { | 215 void Symbols::GrowSymbolTable(const Array& symbol_table) { |
| 204 // TODO(iposva): Avoid exponential growth. | 216 // TODO(iposva): Avoid exponential growth. |
| 205 intptr_t table_size = symbol_table.Length() - 1; | 217 intptr_t table_size = symbol_table.Length() - 1; |
| 206 intptr_t new_table_size = table_size * 2; | 218 intptr_t new_table_size = table_size * 2; |
| 207 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); | 219 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); |
| 208 // Copy all elements from the original symbol table to the newly allocated | 220 // Copy all elements from the original symbol table to the newly allocated |
| 209 // array. | 221 // array. |
| 210 String& element = String::Handle(); | 222 String& element = String::Handle(); |
| 211 dart::Object& new_element = Object::Handle(); | 223 dart::Object& new_element = Object::Handle(); |
| 212 for (intptr_t i = 0; i < table_size; i++) { | 224 for (intptr_t i = 0; i < table_size; i++) { |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 } | 323 } |
| 312 | 324 |
| 313 | 325 |
| 314 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 326 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 315 ASSERT(IsVMSymbolId(object_id)); | 327 ASSERT(IsVMSymbolId(object_id)); |
| 316 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 328 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 317 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); | 329 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); |
| 318 } | 330 } |
| 319 | 331 |
| 320 } // namespace dart | 332 } // namespace dart |
| OLD | NEW |