| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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 "sky/engine/config.h" |
| 6 #include "sky/engine/tonic/dart_string.h" |
| 7 |
| 8 #include "base/logging.h" |
| 9 |
| 10 namespace blink { |
| 11 namespace { |
| 12 |
| 13 void FinalizeString(void* string_impl) { |
| 14 DCHECK(string_impl); |
| 15 reinterpret_cast<StringImpl*>(string_impl)->deref(); |
| 16 } |
| 17 |
| 18 template <typename CharType> |
| 19 String Externalize(Dart_Handle handle, intptr_t length) { |
| 20 if (!length) |
| 21 return StringImpl::empty(); |
| 22 CharType* buffer = nullptr; |
| 23 RefPtr<StringImpl> string_impl = |
| 24 StringImpl::createUninitialized(length, buffer); |
| 25 |
| 26 string_impl->ref(); // Balanced in FinalizeString. |
| 27 |
| 28 Dart_Handle result = |
| 29 Dart_MakeExternalString(handle, buffer, length * sizeof(CharType), |
| 30 string_impl.get(), FinalizeString); |
| 31 DCHECK(!Dart_IsError(result)); |
| 32 return String(string_impl.release()); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 Dart_Handle CreateDartString(StringImpl* string_impl) { |
| 38 if (!string_impl) |
| 39 return Dart_EmptyString(); |
| 40 |
| 41 string_impl->ref(); // Balanced in FinalizeString. |
| 42 |
| 43 if (string_impl->is8Bit()) { |
| 44 return Dart_NewExternalLatin1String(string_impl->characters8(), |
| 45 string_impl->length(), string_impl, |
| 46 FinalizeString); |
| 47 } else { |
| 48 return Dart_NewExternalUTF16String(string_impl->characters16(), |
| 49 string_impl->length(), string_impl, |
| 50 FinalizeString); |
| 51 } |
| 52 } |
| 53 |
| 54 String ExternalizeDartString(Dart_Handle handle) { |
| 55 DCHECK(Dart_IsString(handle)); |
| 56 DCHECK(!Dart_IsExternalString(handle)); |
| 57 bool is_latin1 = Dart_IsStringLatin1(handle); |
| 58 intptr_t length; |
| 59 Dart_StringLength(handle, &length); |
| 60 if (is_latin1) |
| 61 return Externalize<LChar>(handle, length); |
| 62 return Externalize<UChar>(handle, length); |
| 63 } |
| 64 |
| 65 } // namespace blink |
| OLD | NEW |