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