| 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/handles.h" | 7 #include "vm/handles.h" |
| 8 #include "vm/handles_impl.h" | 8 #include "vm/handles_impl.h" |
| 9 #include "vm/hash_table.h" | 9 #include "vm/hash_table.h" |
| 10 #include "vm/isolate.h" | 10 #include "vm/isolate.h" |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 const intptr_t keyword_id = Symbols::kKwTableStart + 1 + kw_index; | 190 const intptr_t keyword_id = Symbols::kKwTableStart + 1 + kw_index; |
| 191 ASSERT(symbol_handles_[keyword_id] != NULL); | 191 ASSERT(symbol_handles_[keyword_id] != NULL); |
| 192 return *symbol_handles_[keyword_id]; | 192 return *symbol_handles_[keyword_id]; |
| 193 } | 193 } |
| 194 | 194 |
| 195 | 195 |
| 196 void Symbols::InitOnce(Isolate* vm_isolate) { | 196 void Symbols::InitOnce(Isolate* vm_isolate) { |
| 197 // Should only be run by the vm isolate. | 197 // Should only be run by the vm isolate. |
| 198 ASSERT(Isolate::Current() == Dart::vm_isolate()); | 198 ASSERT(Isolate::Current() == Dart::vm_isolate()); |
| 199 ASSERT(vm_isolate == Dart::vm_isolate()); | 199 ASSERT(vm_isolate == Dart::vm_isolate()); |
| 200 Zone* zone = Thread::Current()->zone(); |
| 200 | 201 |
| 201 // Create and setup a symbol table in the vm isolate. | 202 // Create and setup a symbol table in the vm isolate. |
| 202 SetupSymbolTable(vm_isolate); | 203 SetupSymbolTable(vm_isolate); |
| 203 | 204 |
| 204 // Create all predefined symbols. | 205 // Create all predefined symbols. |
| 205 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kNullCharId); | 206 ASSERT((sizeof(names) / sizeof(const char*)) == Symbols::kNullCharId); |
| 206 | 207 |
| 207 SymbolTable table(vm_isolate, vm_isolate->object_store()->symbol_table()); | 208 SymbolTable table(zone, vm_isolate->object_store()->symbol_table()); |
| 208 | 209 |
| 209 // First set up all the predefined string symbols. | 210 // First set up all the predefined string symbols. |
| 210 // Create symbols for language keywords. Some keywords are equal to | 211 // Create symbols for language keywords. Some keywords are equal to |
| 211 // symbols we already created, so use New() instead of Add() to ensure | 212 // symbols we already created, so use New() instead of Add() to ensure |
| 212 // that the symbols are canonicalized. | 213 // that the symbols are canonicalized. |
| 213 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) { | 214 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) { |
| 214 String* str = String::ReadOnlyHandle(); | 215 String* str = String::ReadOnlyHandle(); |
| 215 *str = OneByteString::New(names[i], Heap::kOld); | 216 *str = OneByteString::New(names[i], Heap::kOld); |
| 216 str->Hash(); | 217 str->Hash(); |
| 217 str->SetCanonical(); | 218 str->SetCanonical(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 237 } | 238 } |
| 238 | 239 |
| 239 vm_isolate->object_store()->set_symbol_table(table.Release()); | 240 vm_isolate->object_store()->set_symbol_table(table.Release()); |
| 240 } | 241 } |
| 241 | 242 |
| 242 | 243 |
| 243 void Symbols::InitOnceFromSnapshot(Isolate* vm_isolate) { | 244 void Symbols::InitOnceFromSnapshot(Isolate* vm_isolate) { |
| 244 // Should only be run by the vm isolate. | 245 // Should only be run by the vm isolate. |
| 245 ASSERT(Isolate::Current() == Dart::vm_isolate()); | 246 ASSERT(Isolate::Current() == Dart::vm_isolate()); |
| 246 ASSERT(vm_isolate == Dart::vm_isolate()); | 247 ASSERT(vm_isolate == Dart::vm_isolate()); |
| 248 Zone* zone = Thread::Current()->zone(); |
| 247 | 249 |
| 248 SymbolTable table(vm_isolate, vm_isolate->object_store()->symbol_table()); | 250 SymbolTable table(zone, vm_isolate->object_store()->symbol_table()); |
| 249 | 251 |
| 250 // Lookup all the predefined string symbols and language keyword symbols | 252 // Lookup all the predefined string symbols and language keyword symbols |
| 251 // and cache them in the read only handles for fast access. | 253 // and cache them in the read only handles for fast access. |
| 252 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) { | 254 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) { |
| 253 String* str = String::ReadOnlyHandle(); | 255 String* str = String::ReadOnlyHandle(); |
| 254 const unsigned char* name = | 256 const unsigned char* name = |
| 255 reinterpret_cast<const unsigned char*>(names[i]); | 257 reinterpret_cast<const unsigned char*>(names[i]); |
| 256 *str ^= table.GetOrNull(Latin1Array(name, strlen(names[i]))); | 258 *str ^= table.GetOrNull(Latin1Array(name, strlen(names[i]))); |
| 257 ASSERT(!str->IsNull()); | 259 ASSERT(!str->IsNull()); |
| 258 ASSERT(str->HasHash()); | 260 ASSERT(str->HasHash()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 275 predefined_[c] = str->raw(); | 277 predefined_[c] = str->raw(); |
| 276 symbol_handles_[idx] = str; | 278 symbol_handles_[idx] = str; |
| 277 } | 279 } |
| 278 | 280 |
| 279 vm_isolate->object_store()->set_symbol_table(table.Release()); | 281 vm_isolate->object_store()->set_symbol_table(table.Release()); |
| 280 } | 282 } |
| 281 | 283 |
| 282 | 284 |
| 283 void Symbols::AddPredefinedSymbolsToIsolate() { | 285 void Symbols::AddPredefinedSymbolsToIsolate() { |
| 284 // Should only be run by regular Dart isolates. | 286 // Should only be run by regular Dart isolates. |
| 285 Isolate* isolate = Isolate::Current(); | 287 Thread* thread = Thread::Current(); |
| 288 Isolate* isolate = thread->isolate(); |
| 289 Zone* zone = thread->zone(); |
| 286 ASSERT(isolate != Dart::vm_isolate()); | 290 ASSERT(isolate != Dart::vm_isolate()); |
| 287 String& str = String::Handle(isolate); | 291 String& str = String::Handle(isolate); |
| 288 | 292 |
| 289 SymbolTable table(isolate, isolate->object_store()->symbol_table()); | 293 SymbolTable table(zone, isolate->object_store()->symbol_table()); |
| 290 | 294 |
| 291 // Set up all the predefined string symbols and create symbols for | 295 // Set up all the predefined string symbols and create symbols for |
| 292 // language keywords. | 296 // language keywords. |
| 293 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) { | 297 for (intptr_t i = 1; i < Symbols::kNullCharId; i++) { |
| 294 str = OneByteString::New(names[i], Heap::kOld); | 298 str = OneByteString::New(names[i], Heap::kOld); |
| 295 str.Hash(); | 299 str.Hash(); |
| 296 str.SetCanonical(); | 300 str.SetCanonical(); |
| 297 bool present = table.Insert(str); | 301 bool present = table.Insert(str); |
| 298 ASSERT(!present); | 302 ASSERT(!present); |
| 299 } | 303 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 322 const intptr_t initial_size = (isolate == Dart::vm_isolate()) ? | 326 const intptr_t initial_size = (isolate == Dart::vm_isolate()) ? |
| 323 kInitialVMIsolateSymtabSize : kInitialSymtabSize; | 327 kInitialVMIsolateSymtabSize : kInitialSymtabSize; |
| 324 Array& array = | 328 Array& array = |
| 325 Array::Handle(HashTables::New<SymbolTable>(initial_size, Heap::kOld)); | 329 Array::Handle(HashTables::New<SymbolTable>(initial_size, Heap::kOld)); |
| 326 isolate->object_store()->set_symbol_table(array); | 330 isolate->object_store()->set_symbol_table(array); |
| 327 } | 331 } |
| 328 | 332 |
| 329 | 333 |
| 330 void Symbols::GetStats(Isolate* isolate, intptr_t* size, intptr_t* capacity) { | 334 void Symbols::GetStats(Isolate* isolate, intptr_t* size, intptr_t* capacity) { |
| 331 ASSERT(isolate != NULL); | 335 ASSERT(isolate != NULL); |
| 332 SymbolTable table(isolate, isolate->object_store()->symbol_table()); | 336 SymbolTable table(isolate->object_store()->symbol_table()); |
| 333 *size = table.NumOccupied(); | 337 *size = table.NumOccupied(); |
| 334 *capacity = table.NumEntries(); | 338 *capacity = table.NumEntries(); |
| 335 table.Release(); | 339 table.Release(); |
| 336 } | 340 } |
| 337 | 341 |
| 338 | 342 |
| 339 RawString* Symbols::New(const char* cstr, intptr_t len) { | 343 RawString* Symbols::New(const char* cstr, intptr_t len) { |
| 340 ASSERT((cstr != NULL) && (len >= 0)); | 344 ASSERT((cstr != NULL) && (len >= 0)); |
| 341 const uint8_t* utf8_array = reinterpret_cast<const uint8_t*>(cstr); | 345 const uint8_t* utf8_array = reinterpret_cast<const uint8_t*>(cstr); |
| 342 return Symbols::FromUTF8(utf8_array, len); | 346 return Symbols::FromUTF8(utf8_array, len); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 | 383 |
| 380 | 384 |
| 381 RawString* Symbols::FromConcat(const String& str1, const String& str2) { | 385 RawString* Symbols::FromConcat(const String& str1, const String& str2) { |
| 382 return NewSymbol(ConcatString(str1, str2)); | 386 return NewSymbol(ConcatString(str1, str2)); |
| 383 } | 387 } |
| 384 | 388 |
| 385 | 389 |
| 386 // StringType can be StringSlice, ConcatString, or {Latin1,UTF16,UTF32}Array. | 390 // StringType can be StringSlice, ConcatString, or {Latin1,UTF16,UTF32}Array. |
| 387 template<typename StringType> | 391 template<typename StringType> |
| 388 RawString* Symbols::NewSymbol(const StringType& str) { | 392 RawString* Symbols::NewSymbol(const StringType& str) { |
| 389 Isolate* isolate = Isolate::Current(); | 393 Thread* thread = Thread::Current(); |
| 394 Isolate* isolate = thread->isolate(); |
| 395 Zone* zone = thread->zone(); |
| 390 String& symbol = String::Handle(isolate); | 396 String& symbol = String::Handle(isolate); |
| 391 { | 397 { |
| 392 Isolate* vm_isolate = Dart::vm_isolate(); | 398 Isolate* vm_isolate = Dart::vm_isolate(); |
| 393 SymbolTable table(isolate, vm_isolate->object_store()->symbol_table()); | 399 SymbolTable table(zone, vm_isolate->object_store()->symbol_table()); |
| 394 symbol ^= table.GetOrNull(str); | 400 symbol ^= table.GetOrNull(str); |
| 395 table.Release(); | 401 table.Release(); |
| 396 } | 402 } |
| 397 if (symbol.IsNull()) { | 403 if (symbol.IsNull()) { |
| 398 SymbolTable table(isolate, isolate->object_store()->symbol_table()); | 404 SymbolTable table(zone, isolate->object_store()->symbol_table()); |
| 399 symbol ^= table.InsertNewOrGet(str); | 405 symbol ^= table.InsertNewOrGet(str); |
| 400 isolate->object_store()->set_symbol_table(table.Release()); | 406 isolate->object_store()->set_symbol_table(table.Release()); |
| 401 } | 407 } |
| 402 ASSERT(symbol.IsSymbol()); | 408 ASSERT(symbol.IsSymbol()); |
| 403 ASSERT(symbol.HasHash()); | 409 ASSERT(symbol.HasHash()); |
| 404 return symbol.raw(); | 410 return symbol.raw(); |
| 405 } | 411 } |
| 406 | 412 |
| 407 | 413 |
| 408 RawString* Symbols::New(const String& str) { | 414 RawString* Symbols::New(const String& str) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { | 463 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { |
| 458 ASSERT(IsVMSymbolId(object_id)); | 464 ASSERT(IsVMSymbolId(object_id)); |
| 459 intptr_t i = (object_id - kMaxPredefinedObjectIds); | 465 intptr_t i = (object_id - kMaxPredefinedObjectIds); |
| 460 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { | 466 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { |
| 461 return symbol_handles_[i]->raw(); | 467 return symbol_handles_[i]->raw(); |
| 462 } | 468 } |
| 463 return Object::null(); | 469 return Object::null(); |
| 464 } | 470 } |
| 465 | 471 |
| 466 } // namespace dart | 472 } // namespace dart |
| OLD | NEW |