OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "tonic/dart_string_cache.h" |
| 6 |
| 7 #include "tonic/dart_state.h" |
| 8 #include "tonic/dart_string.h" |
| 9 |
| 10 namespace blink { |
| 11 |
| 12 DartStringCache::DartStringCache() : last_dart_string_(nullptr) { |
| 13 } |
| 14 |
| 15 DartStringCache::~DartStringCache() { |
| 16 } |
| 17 |
| 18 Dart_WeakPersistentHandle DartStringCache::GetSlow(StringImpl* string_impl, |
| 19 bool auto_scope) { |
| 20 if (Dart_WeakPersistentHandle string = cache_.get(string_impl)) { |
| 21 last_dart_string_ = string; |
| 22 last_string_impl_ = string_impl; |
| 23 return string; |
| 24 } |
| 25 |
| 26 if (!auto_scope) |
| 27 Dart_EnterScope(); |
| 28 |
| 29 Dart_Handle string = CreateDartString(string_impl); |
| 30 DCHECK(!Dart_IsError(string)); |
| 31 |
| 32 intptr_t size_in_bytes = string_impl->sizeInBytes(); |
| 33 Dart_WeakPersistentHandle wrapper = Dart_NewWeakPersistentHandle( |
| 34 string, string_impl, size_in_bytes, FinalizeCacheEntry); |
| 35 |
| 36 string_impl->ref(); // Balanced in FinalizeCacheEntry. |
| 37 cache_.set(string_impl, wrapper); |
| 38 |
| 39 last_dart_string_ = wrapper; |
| 40 last_string_impl_ = string_impl; |
| 41 |
| 42 if (!auto_scope) |
| 43 Dart_ExitScope(); |
| 44 |
| 45 return wrapper; |
| 46 } |
| 47 |
| 48 void DartStringCache::FinalizeCacheEntry(void* isolate_callback_data, |
| 49 Dart_WeakPersistentHandle handle, |
| 50 void* peer) { |
| 51 DartState* state = reinterpret_cast<DartState*>(isolate_callback_data); |
| 52 StringImpl* string_impl = reinterpret_cast<StringImpl*>(peer); |
| 53 DartStringCache& cache = state->string_cache(); |
| 54 |
| 55 Dart_WeakPersistentHandle cached_handle = cache.cache_.take(string_impl); |
| 56 ASSERT_UNUSED(cached_handle, handle == cached_handle); |
| 57 |
| 58 if (cache.last_dart_string_ == handle) { |
| 59 cache.last_dart_string_ = nullptr; |
| 60 cache.last_string_impl_ = nullptr; |
| 61 } |
| 62 |
| 63 string_impl->deref(); |
| 64 } |
| 65 |
| 66 } // namespace blink |
OLD | NEW |