| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 | 57 |
| 58 for (int32_t c = 0; c <= kMaxOneCharCodeSymbol; c++) { | 58 for (int32_t c = 0; c <= kMaxOneCharCodeSymbol; c++) { |
| 59 ASSERT(kMaxPredefinedId + c < kMaxId); | 59 ASSERT(kMaxPredefinedId + c < kMaxId); |
| 60 predefined_[kMaxPredefinedId + c] = New(&c, 1); | 60 predefined_[kMaxPredefinedId + c] = FromUTF32(&c, 1); |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 | 64 |
| 65 void Symbols::SetupSymbolTable(Isolate* isolate) { | 65 void Symbols::SetupSymbolTable(Isolate* isolate) { |
| 66 ASSERT(isolate != NULL); | 66 ASSERT(isolate != NULL); |
| 67 | 67 |
| 68 // Setup the symbol table used within the String class. | 68 // Setup the symbol table used within the String class. |
| 69 const int initial_size = (isolate == Dart::vm_isolate()) ? | 69 const int initial_size = (isolate == Dart::vm_isolate()) ? |
| 70 kInitialVMIsolateSymtabSize : kInitialSymtabSize; | 70 kInitialVMIsolateSymtabSize : kInitialSymtabSize; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 90 void Symbols::Add(const Array& symbol_table, const String& str) { | 90 void Symbols::Add(const Array& symbol_table, const String& str) { |
| 91 // Should only be run by the vm isolate. | 91 // Should only be run by the vm isolate. |
| 92 ASSERT(Isolate::Current() == Dart::vm_isolate()); | 92 ASSERT(Isolate::Current() == Dart::vm_isolate()); |
| 93 intptr_t hash = str.Hash(); | 93 intptr_t hash = str.Hash(); |
| 94 intptr_t index = FindIndex(symbol_table, str, 0, str.Length(), hash); | 94 intptr_t index = FindIndex(symbol_table, str, 0, str.Length(), hash); |
| 95 ASSERT(symbol_table.At(index) == String::null()); | 95 ASSERT(symbol_table.At(index) == String::null()); |
| 96 InsertIntoSymbolTable(symbol_table, str, index); | 96 InsertIntoSymbolTable(symbol_table, str, index); |
| 97 } | 97 } |
| 98 | 98 |
| 99 | 99 |
| 100 RawString* Symbols::New(const char* str) { | 100 RawString* Symbols::New(const char* cstr) { |
| 101 ASSERT(str != NULL); | 101 ASSERT(cstr != NULL); |
| 102 intptr_t array_len = strlen(cstr); |
| 103 const uint8_t* utf8_array = reinterpret_cast<const uint8_t*>(cstr); |
| 104 return Symbols::FromUTF8(utf8_array, array_len); |
| 105 } |
| 106 |
| 107 |
| 108 RawString* Symbols::FromUTF8(const uint8_t* utf8_array, intptr_t array_len) { |
| 109 if (array_len == 0 || utf8_array == NULL) { |
| 110 return NewSymbol(reinterpret_cast<uint8_t*>(NULL), 0); |
| 111 } |
| 102 Utf8::Type type; | 112 Utf8::Type type; |
| 103 intptr_t str_len = strlen(str); | 113 intptr_t len = Utf8::CodeUnitCount(utf8_array, array_len, &type); |
| 104 const uint8_t* utf8_array = reinterpret_cast<const uint8_t*>(str); | 114 ASSERT(len != 0); |
| 105 intptr_t len = Utf8::CodeUnitCount(utf8_array, str_len, &type); | |
| 106 Zone* zone = Isolate::Current()->current_zone(); | 115 Zone* zone = Isolate::Current()->current_zone(); |
| 107 if (len == 0) { | |
| 108 return Symbols::New(reinterpret_cast<uint8_t*>(NULL), 0); | |
| 109 } | |
| 110 if (type == Utf8::kLatin1) { | 116 if (type == Utf8::kLatin1) { |
| 111 uint8_t* characters = zone->Alloc<uint8_t>(len); | 117 uint8_t* characters = zone->Alloc<uint8_t>(len); |
| 112 Utf8::DecodeToLatin1(utf8_array, str_len, characters, len); | 118 Utf8::DecodeToLatin1(utf8_array, array_len, characters, len); |
| 113 return New(characters, len); | 119 return NewSymbol(characters, len); |
| 114 } | 120 } |
| 115 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSupplementary)); | 121 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSupplementary)); |
| 116 uint16_t* characters = zone->Alloc<uint16_t>(len); | 122 uint16_t* characters = zone->Alloc<uint16_t>(len); |
| 117 Utf8::DecodeToUTF16(utf8_array, str_len, characters, len); | 123 Utf8::DecodeToUTF16(utf8_array, array_len, characters, len); |
| 118 return New(characters, len); | 124 return NewSymbol(characters, len); |
| 125 } |
| 126 |
| 127 |
| 128 RawString* Symbols::FromLatin1(const uint8_t* latin1_array, intptr_t len) { |
| 129 return NewSymbol(latin1_array, len); |
| 130 } |
| 131 |
| 132 |
| 133 RawString* Symbols::FromUTF16(const uint16_t* utf16_array, intptr_t len) { |
| 134 return NewSymbol(utf16_array, len); |
| 135 } |
| 136 |
| 137 |
| 138 RawString* Symbols::FromUTF32(const int32_t* utf32_array, intptr_t len) { |
| 139 return NewSymbol(utf32_array, len); |
| 119 } | 140 } |
| 120 | 141 |
| 121 | 142 |
| 122 template<typename T> | 143 template<typename T> |
| 123 RawString* Symbols::New(const T* characters, intptr_t len) { | 144 RawString* Symbols::NewSymbol(const T* characters, intptr_t len) { |
| 124 Isolate* isolate = Isolate::Current(); | 145 Isolate* isolate = Isolate::Current(); |
| 125 String& symbol = String::Handle(isolate, String::null()); | 146 String& symbol = String::Handle(isolate, String::null()); |
| 126 Array& symbol_table = Array::Handle(isolate, Array::null()); | 147 Array& symbol_table = Array::Handle(isolate, Array::null()); |
| 127 | 148 |
| 128 // Calculate the String hash for this sequence of characters. | 149 // Calculate the String hash for this sequence of characters. |
| 129 intptr_t hash = String::Hash(characters, len); | 150 intptr_t hash = String::Hash(characters, len); |
| 130 | 151 |
| 131 // First check if a symbol exists in the vm isolate for these characters. | 152 // First check if a symbol exists in the vm isolate for these characters. |
| 132 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); | 153 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); |
| 133 intptr_t index = FindIndex(symbol_table, characters, len, hash); | 154 intptr_t index = FindIndex(symbol_table, characters, len, hash); |
| 134 symbol ^= symbol_table.At(index); | 155 symbol ^= symbol_table.At(index); |
| 135 if (symbol.IsNull()) { | 156 if (symbol.IsNull()) { |
| 136 // Now try in the symbol table of the current isolate. | 157 // Now try in the symbol table of the current isolate. |
| 137 symbol_table = isolate->object_store()->symbol_table(); | 158 symbol_table = isolate->object_store()->symbol_table(); |
| 138 index = FindIndex(symbol_table, characters, len, hash); | 159 index = FindIndex(symbol_table, characters, len, hash); |
| 139 // Since we leave enough room in the table to guarantee, that we find an | 160 // Since we leave enough room in the table to guarantee, that we find an |
| 140 // empty spot, index is the insertion point if symbol is null. | 161 // empty spot, index is the insertion point if symbol is null. |
| 141 symbol ^= symbol_table.At(index); | 162 symbol ^= symbol_table.At(index); |
| 142 if (symbol.IsNull()) { | 163 if (symbol.IsNull()) { |
| 143 // Allocate new result string. | 164 // Allocate new result string. |
| 144 symbol = String::New(characters, len, Heap::kOld); | 165 symbol = String::New(characters, len, Heap::kOld); |
| 145 symbol.SetHash(hash); // Remember the calculated hash value. | 166 symbol.SetHash(hash); // Remember the calculated hash value. |
| 146 InsertIntoSymbolTable(symbol_table, symbol, index); | 167 InsertIntoSymbolTable(symbol_table, symbol, index); |
| 147 } | 168 } |
| 148 } | 169 } |
| 149 ASSERT(symbol.IsSymbol()); | 170 ASSERT(symbol.IsSymbol()); |
| 150 return symbol.raw(); | 171 return symbol.raw(); |
| 151 } | 172 } |
| 152 | 173 |
| 153 template RawString* Symbols::New(const uint8_t* characters, intptr_t len); | 174 |
| 154 template RawString* Symbols::New(const uint16_t* characters, intptr_t len); | 175 template RawString* Symbols::NewSymbol(const uint8_t* characters, |
| 155 template RawString* Symbols::New(const int32_t* characters, intptr_t len); | 176 intptr_t len); |
| 177 template RawString* Symbols::NewSymbol(const uint16_t* characters, |
| 178 intptr_t len); |
| 179 template RawString* Symbols::NewSymbol(const int32_t* characters, |
| 180 intptr_t len); |
| 156 | 181 |
| 157 | 182 |
| 158 RawString* Symbols::New(const String& str) { | 183 RawString* Symbols::New(const String& str) { |
| 159 if (str.IsSymbol()) { | 184 if (str.IsSymbol()) { |
| 160 return str.raw(); | 185 return str.raw(); |
| 161 } | 186 } |
| 162 return New(str, 0, str.Length()); | 187 return New(str, 0, str.Length()); |
| 163 } | 188 } |
| 164 | 189 |
| 165 | 190 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 InsertIntoSymbolTable(symbol_table, symbol, index); | 224 InsertIntoSymbolTable(symbol_table, symbol, index); |
| 200 } | 225 } |
| 201 } | 226 } |
| 202 ASSERT(symbol.IsSymbol()); | 227 ASSERT(symbol.IsSymbol()); |
| 203 return symbol.raw(); | 228 return symbol.raw(); |
| 204 } | 229 } |
| 205 | 230 |
| 206 | 231 |
| 207 RawString* Symbols::FromCharCode(int32_t char_code) { | 232 RawString* Symbols::FromCharCode(int32_t char_code) { |
| 208 if (char_code > kMaxOneCharCodeSymbol) { | 233 if (char_code > kMaxOneCharCodeSymbol) { |
| 209 return New(&char_code, 1); | 234 return FromUTF32(&char_code, 1); |
| 210 } | 235 } |
| 211 return predefined_[kNullCharId + char_code]; | 236 return predefined_[kNullCharId + char_code]; |
| 212 } | 237 } |
| 213 | 238 |
| 214 | 239 |
| 215 void Symbols::GrowSymbolTable(const Array& symbol_table) { | 240 void Symbols::GrowSymbolTable(const Array& symbol_table) { |
| 216 // TODO(iposva): Avoid exponential growth. | 241 // TODO(iposva): Avoid exponential growth. |
| 217 intptr_t table_size = symbol_table.Length() - 1; | 242 intptr_t table_size = symbol_table.Length() - 1; |
| 218 intptr_t new_table_size = table_size * 2; | 243 intptr_t new_table_size = table_size * 2; |
| 219 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); | 244 Array& new_symbol_table = Array::Handle(Array::New(new_table_size + 1)); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 } | 348 } |
| 324 | 349 |
| 325 | 350 |
| 326 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 351 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 327 ASSERT(IsVMSymbolId(object_id)); | 352 ASSERT(IsVMSymbolId(object_id)); |
| 328 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 353 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 329 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); | 354 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); |
| 330 } | 355 } |
| 331 | 356 |
| 332 } // namespace dart | 357 } // namespace dart |
| OLD | NEW |