| 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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after 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::New(utf8_array, array_len); |
| 105 } |
| 106 |
| 107 |
| 108 RawString* Symbols::New(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::New(const uint16_t* characters, intptr_t len) { |
| 129 return NewSymbol(characters, len); |
| 130 } |
| 131 |
| 132 |
| 133 RawString* Symbols::New(const int32_t* characters, intptr_t len) { |
| 134 return NewSymbol(characters, len); |
| 119 } | 135 } |
| 120 | 136 |
| 121 | 137 |
| 122 template<typename T> | 138 template<typename T> |
| 123 RawString* Symbols::New(const T* characters, intptr_t len) { | 139 RawString* Symbols::NewSymbol(const T* characters, intptr_t len) { |
| 124 Isolate* isolate = Isolate::Current(); | 140 Isolate* isolate = Isolate::Current(); |
| 125 String& symbol = String::Handle(isolate, String::null()); | 141 String& symbol = String::Handle(isolate, String::null()); |
| 126 Array& symbol_table = Array::Handle(isolate, Array::null()); | 142 Array& symbol_table = Array::Handle(isolate, Array::null()); |
| 127 | 143 |
| 128 // Calculate the String hash for this sequence of characters. | 144 // Calculate the String hash for this sequence of characters. |
| 129 intptr_t hash = String::Hash(characters, len); | 145 intptr_t hash = String::Hash(characters, len); |
| 130 | 146 |
| 131 // First check if a symbol exists in the vm isolate for these characters. | 147 // First check if a symbol exists in the vm isolate for these characters. |
| 132 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); | 148 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); |
| 133 intptr_t index = FindIndex(symbol_table, characters, len, hash); | 149 intptr_t index = FindIndex(symbol_table, characters, len, hash); |
| 134 symbol ^= symbol_table.At(index); | 150 symbol ^= symbol_table.At(index); |
| 135 if (symbol.IsNull()) { | 151 if (symbol.IsNull()) { |
| 136 // Now try in the symbol table of the current isolate. | 152 // Now try in the symbol table of the current isolate. |
| 137 symbol_table = isolate->object_store()->symbol_table(); | 153 symbol_table = isolate->object_store()->symbol_table(); |
| 138 index = FindIndex(symbol_table, characters, len, hash); | 154 index = FindIndex(symbol_table, characters, len, hash); |
| 139 // Since we leave enough room in the table to guarantee, that we find an | 155 // 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. | 156 // empty spot, index is the insertion point if symbol is null. |
| 141 symbol ^= symbol_table.At(index); | 157 symbol ^= symbol_table.At(index); |
| 142 if (symbol.IsNull()) { | 158 if (symbol.IsNull()) { |
| 143 // Allocate new result string. | 159 // Allocate new result string. |
| 144 symbol = String::New(characters, len, Heap::kOld); | 160 symbol = String::New(characters, len, Heap::kOld); |
| 145 symbol.SetHash(hash); // Remember the calculated hash value. | 161 symbol.SetHash(hash); // Remember the calculated hash value. |
| 146 InsertIntoSymbolTable(symbol_table, symbol, index); | 162 InsertIntoSymbolTable(symbol_table, symbol, index); |
| 147 } | 163 } |
| 148 } | 164 } |
| 149 ASSERT(symbol.IsSymbol()); | 165 ASSERT(symbol.IsSymbol()); |
| 150 return symbol.raw(); | 166 return symbol.raw(); |
| 151 } | 167 } |
| 152 | 168 |
| 153 template RawString* Symbols::New(const uint8_t* characters, intptr_t len); | 169 |
| 154 template RawString* Symbols::New(const uint16_t* characters, intptr_t len); | 170 template RawString* Symbols::NewSymbol(const uint8_t* characters, |
| 155 template RawString* Symbols::New(const int32_t* characters, intptr_t len); | 171 intptr_t len); |
| 172 template RawString* Symbols::NewSymbol(const uint16_t* characters, |
| 173 intptr_t len); |
| 174 template RawString* Symbols::NewSymbol(const int32_t* characters, |
| 175 intptr_t len); |
| 156 | 176 |
| 157 | 177 |
| 158 RawString* Symbols::New(const String& str) { | 178 RawString* Symbols::New(const String& str) { |
| 159 if (str.IsSymbol()) { | 179 if (str.IsSymbol()) { |
| 160 return str.raw(); | 180 return str.raw(); |
| 161 } | 181 } |
| 162 return New(str, 0, str.Length()); | 182 return New(str, 0, str.Length()); |
| 163 } | 183 } |
| 164 | 184 |
| 165 | 185 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 } | 343 } |
| 324 | 344 |
| 325 | 345 |
| 326 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 346 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 327 ASSERT(IsVMSymbolId(object_id)); | 347 ASSERT(IsVMSymbolId(object_id)); |
| 328 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 348 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 329 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); | 349 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); |
| 330 } | 350 } |
| 331 | 351 |
| 332 } // namespace dart | 352 } // namespace dart |
| OLD | NEW |