| 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 RawString* Symbols::New(const char* cstr) { | 100 RawString* Symbols::New(const char* cstr) { |
| 101 ASSERT(cstr != NULL); | 101 ASSERT(cstr != NULL); |
| 102 intptr_t array_len = strlen(cstr); | 102 intptr_t array_len = strlen(cstr); |
| 103 const uint8_t* utf8_array = reinterpret_cast<const uint8_t*>(cstr); | 103 const uint8_t* utf8_array = reinterpret_cast<const uint8_t*>(cstr); |
| 104 return Symbols::FromUTF8(utf8_array, array_len); | 104 return Symbols::FromUTF8(utf8_array, array_len); |
| 105 } | 105 } |
| 106 | 106 |
| 107 | 107 |
| 108 RawString* Symbols::FromUTF8(const uint8_t* utf8_array, intptr_t array_len) { | 108 RawString* Symbols::FromUTF8(const uint8_t* utf8_array, intptr_t array_len) { |
| 109 if (array_len == 0 || utf8_array == NULL) { | 109 if (array_len == 0 || utf8_array == NULL) { |
| 110 return NewSymbol(reinterpret_cast<uint8_t*>(NULL), 0); | 110 return FromLatin1(reinterpret_cast<uint8_t*>(NULL), 0); |
| 111 } | 111 } |
| 112 Utf8::Type type; | 112 Utf8::Type type; |
| 113 intptr_t len = Utf8::CodeUnitCount(utf8_array, array_len, &type); | 113 intptr_t len = Utf8::CodeUnitCount(utf8_array, array_len, &type); |
| 114 ASSERT(len != 0); | 114 ASSERT(len != 0); |
| 115 Zone* zone = Isolate::Current()->current_zone(); | 115 Zone* zone = Isolate::Current()->current_zone(); |
| 116 if (type == Utf8::kLatin1) { | 116 if (type == Utf8::kLatin1) { |
| 117 uint8_t* characters = zone->Alloc<uint8_t>(len); | 117 uint8_t* characters = zone->Alloc<uint8_t>(len); |
| 118 Utf8::DecodeToLatin1(utf8_array, array_len, characters, len); | 118 Utf8::DecodeToLatin1(utf8_array, array_len, characters, len); |
| 119 return NewSymbol(characters, len); | 119 return FromLatin1(characters, len); |
| 120 } | 120 } |
| 121 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSupplementary)); | 121 ASSERT((type == Utf8::kBMP) || (type == Utf8::kSupplementary)); |
| 122 uint16_t* characters = zone->Alloc<uint16_t>(len); | 122 uint16_t* characters = zone->Alloc<uint16_t>(len); |
| 123 Utf8::DecodeToUTF16(utf8_array, array_len, characters, len); | 123 Utf8::DecodeToUTF16(utf8_array, array_len, characters, len); |
| 124 return NewSymbol(characters, len); | 124 return FromUTF16(characters, len); |
| 125 } | 125 } |
| 126 | 126 |
| 127 | 127 |
| 128 RawString* Symbols::FromLatin1(const uint8_t* latin1_array, intptr_t len) { | 128 RawString* Symbols::FromLatin1(const uint8_t* latin1_array, intptr_t len) { |
| 129 return NewSymbol(latin1_array, len); | 129 return NewSymbol(latin1_array, len, String::FromLatin1); |
| 130 } | 130 } |
| 131 | 131 |
| 132 | 132 |
| 133 RawString* Symbols::FromUTF16(const uint16_t* utf16_array, intptr_t len) { | 133 RawString* Symbols::FromUTF16(const uint16_t* utf16_array, intptr_t len) { |
| 134 return NewSymbol(utf16_array, len); | 134 return NewSymbol(utf16_array, len, String::FromUTF16); |
| 135 } | 135 } |
| 136 | 136 |
| 137 | 137 |
| 138 RawString* Symbols::FromUTF32(const int32_t* utf32_array, intptr_t len) { | 138 RawString* Symbols::FromUTF32(const int32_t* utf32_array, intptr_t len) { |
| 139 return NewSymbol(utf32_array, len); | 139 return NewSymbol(utf32_array, len, String::FromUTF32); |
| 140 } | 140 } |
| 141 | 141 |
| 142 | 142 |
| 143 template<typename T> | 143 template<typename CharacterType, typename CallbackType> |
| 144 RawString* Symbols::NewSymbol(const T* characters, intptr_t len) { | 144 RawString* Symbols::NewSymbol(const CharacterType* characters, |
| 145 intptr_t len, |
| 146 CallbackType new_string) { |
| 145 Isolate* isolate = Isolate::Current(); | 147 Isolate* isolate = Isolate::Current(); |
| 146 String& symbol = String::Handle(isolate, String::null()); | 148 String& symbol = String::Handle(isolate, String::null()); |
| 147 Array& symbol_table = Array::Handle(isolate, Array::null()); | 149 Array& symbol_table = Array::Handle(isolate, Array::null()); |
| 148 | 150 |
| 149 // Calculate the String hash for this sequence of characters. | 151 // Calculate the String hash for this sequence of characters. |
| 150 intptr_t hash = String::Hash(characters, len); | 152 intptr_t hash = String::Hash(characters, len); |
| 151 | 153 |
| 152 // First check if a symbol exists in the vm isolate for these characters. | 154 // First check if a symbol exists in the vm isolate for these characters. |
| 153 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); | 155 symbol_table = Dart::vm_isolate()->object_store()->symbol_table(); |
| 154 intptr_t index = FindIndex(symbol_table, characters, len, hash); | 156 intptr_t index = FindIndex(symbol_table, characters, len, hash); |
| 155 symbol ^= symbol_table.At(index); | 157 symbol ^= symbol_table.At(index); |
| 156 if (symbol.IsNull()) { | 158 if (symbol.IsNull()) { |
| 157 // Now try in the symbol table of the current isolate. | 159 // Now try in the symbol table of the current isolate. |
| 158 symbol_table = isolate->object_store()->symbol_table(); | 160 symbol_table = isolate->object_store()->symbol_table(); |
| 159 index = FindIndex(symbol_table, characters, len, hash); | 161 index = FindIndex(symbol_table, characters, len, hash); |
| 160 // Since we leave enough room in the table to guarantee, that we find an | 162 // Since we leave enough room in the table to guarantee, that we find an |
| 161 // empty spot, index is the insertion point if symbol is null. | 163 // empty spot, index is the insertion point if symbol is null. |
| 162 symbol ^= symbol_table.At(index); | 164 symbol ^= symbol_table.At(index); |
| 163 if (symbol.IsNull()) { | 165 if (symbol.IsNull()) { |
| 164 // Allocate new result string. | 166 // Allocate new result string. |
| 165 symbol = String::New(characters, len, Heap::kOld); | 167 symbol = (*new_string)(characters, len, Heap::kOld); |
| 166 symbol.SetHash(hash); // Remember the calculated hash value. | 168 symbol.SetHash(hash); // Remember the calculated hash value. |
| 167 InsertIntoSymbolTable(symbol_table, symbol, index); | 169 InsertIntoSymbolTable(symbol_table, symbol, index); |
| 168 } | 170 } |
| 169 } | 171 } |
| 170 ASSERT(symbol.IsSymbol()); | 172 ASSERT(symbol.IsSymbol()); |
| 171 return symbol.raw(); | 173 return symbol.raw(); |
| 172 } | 174 } |
| 173 | 175 |
| 174 | 176 |
| 175 template RawString* Symbols::NewSymbol(const uint8_t* characters, | 177 template RawString* Symbols::NewSymbol(const uint8_t* characters, |
| 176 intptr_t len); | 178 intptr_t len, |
| 179 RawString* (*new_string)(const uint8_t*, |
| 180 intptr_t, |
| 181 Heap::Space)); |
| 177 template RawString* Symbols::NewSymbol(const uint16_t* characters, | 182 template RawString* Symbols::NewSymbol(const uint16_t* characters, |
| 178 intptr_t len); | 183 intptr_t len, |
| 184 RawString* (*new_string)(const uint16_t*, |
| 185 intptr_t, |
| 186 Heap::Space)); |
| 179 template RawString* Symbols::NewSymbol(const int32_t* characters, | 187 template RawString* Symbols::NewSymbol(const int32_t* characters, |
| 180 intptr_t len); | 188 intptr_t len, |
| 189 RawString* (*new_string)(const int32_t*, |
| 190 intptr_t, |
| 191 Heap::Space)); |
| 181 | 192 |
| 182 | 193 |
| 183 RawString* Symbols::New(const String& str) { | 194 RawString* Symbols::New(const String& str) { |
| 184 if (str.IsSymbol()) { | 195 if (str.IsSymbol()) { |
| 185 return str.raw(); | 196 return str.raw(); |
| 186 } | 197 } |
| 187 return New(str, 0, str.Length()); | 198 return New(str, 0, str.Length()); |
| 188 } | 199 } |
| 189 | 200 |
| 190 | 201 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 } | 359 } |
| 349 | 360 |
| 350 | 361 |
| 351 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 362 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 352 ASSERT(IsVMSymbolId(object_id)); | 363 ASSERT(IsVMSymbolId(object_id)); |
| 353 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 364 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 354 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); | 365 return (i > 0 && i < Symbols::kMaxId) ? predefined_[i] : Object::null(); |
| 355 } | 366 } |
| 356 | 367 |
| 357 } // namespace dart | 368 } // namespace dart |
| OLD | NEW |