| 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 "include/dart_api.h" | 5 #include "include/dart_api.h" |
| 6 | 6 |
| 7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
| 8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
| 9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
| 10 #include "vm/dart.h" | 10 #include "vm/dart.h" |
| (...skipping 1684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1695 *utf8_array = Api::TopScope(isolate)->zone()->Alloc<uint8_t>(str_len); | 1695 *utf8_array = Api::TopScope(isolate)->zone()->Alloc<uint8_t>(str_len); |
| 1696 if (*utf8_array == NULL) { | 1696 if (*utf8_array == NULL) { |
| 1697 return Api::NewError("Unable to allocate memory"); | 1697 return Api::NewError("Unable to allocate memory"); |
| 1698 } | 1698 } |
| 1699 str_obj.ToUTF8(*utf8_array, str_len); | 1699 str_obj.ToUTF8(*utf8_array, str_len); |
| 1700 *length = str_len; | 1700 *length = str_len; |
| 1701 return Api::Success(isolate); | 1701 return Api::Success(isolate); |
| 1702 } | 1702 } |
| 1703 | 1703 |
| 1704 | 1704 |
| 1705 DART_EXPORT Dart_Handle Dart_StringToLatin1(Dart_Handle str, |
| 1706 uint8_t* latin1_array, |
| 1707 intptr_t* length) { |
| 1708 Isolate* isolate = Isolate::Current(); |
| 1709 DARTSCOPE(isolate); |
| 1710 if (latin1_array == NULL) { |
| 1711 RETURN_NULL_ERROR(latin1_array); |
| 1712 } |
| 1713 if (length == NULL) { |
| 1714 RETURN_NULL_ERROR(length); |
| 1715 } |
| 1716 const String& str_obj = Api::UnwrapStringHandle(isolate, str); |
| 1717 if (str_obj.IsNull() || !Dart_IsStringLatin1(str)) { |
| 1718 RETURN_TYPE_ERROR(isolate, str, String); |
| 1719 } |
| 1720 intptr_t str_len = str_obj.Length(); |
| 1721 intptr_t copy_len = (str_len > *length) ? *length : str_len; |
| 1722 |
| 1723 // We have already asserted that the string object is a Latin-1 string |
| 1724 // so we can copy the characters over using a simple loop. |
| 1725 for (intptr_t i = 0; i < copy_len; i++) { |
| 1726 latin1_array[i] = str_obj.CharAt(i); |
| 1727 } |
| 1728 *length = copy_len; |
| 1729 return Api::Success(isolate); |
| 1730 } |
| 1731 |
| 1732 |
| 1705 DART_EXPORT Dart_Handle Dart_StringToUTF16(Dart_Handle str, | 1733 DART_EXPORT Dart_Handle Dart_StringToUTF16(Dart_Handle str, |
| 1706 uint16_t* utf16_array, | 1734 uint16_t* utf16_array, |
| 1707 intptr_t* length) { | 1735 intptr_t* length) { |
| 1708 Isolate* isolate = Isolate::Current(); | 1736 Isolate* isolate = Isolate::Current(); |
| 1709 DARTSCOPE(isolate); | 1737 DARTSCOPE(isolate); |
| 1710 const String& str_obj = Api::UnwrapStringHandle(isolate, str); | 1738 const String& str_obj = Api::UnwrapStringHandle(isolate, str); |
| 1711 if (str_obj.IsNull()) { | 1739 if (str_obj.IsNull()) { |
| 1712 RETURN_TYPE_ERROR(isolate, str, String); | 1740 RETURN_TYPE_ERROR(isolate, str, String); |
| 1713 } | 1741 } |
| 1714 intptr_t str_len = str_obj.Length(); | 1742 intptr_t str_len = str_obj.Length(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1747 const String& str_obj = Api::UnwrapStringHandle(isolate, str); | 1775 const String& str_obj = Api::UnwrapStringHandle(isolate, str); |
| 1748 if (str_obj.IsExternal()) { | 1776 if (str_obj.IsExternal()) { |
| 1749 return str; // String is already an external string. | 1777 return str; // String is already an external string. |
| 1750 } | 1778 } |
| 1751 if (str_obj.IsNull()) { | 1779 if (str_obj.IsNull()) { |
| 1752 RETURN_TYPE_ERROR(isolate, str, String); | 1780 RETURN_TYPE_ERROR(isolate, str, String); |
| 1753 } | 1781 } |
| 1754 if (array == NULL) { | 1782 if (array == NULL) { |
| 1755 RETURN_NULL_ERROR(array); | 1783 RETURN_NULL_ERROR(array); |
| 1756 } | 1784 } |
| 1757 if (str_obj.IsCanonical()) { | |
| 1758 return Api::NewError("Dart_MakeExternalString " | |
| 1759 "cannot externalize a read-only string."); | |
| 1760 } | |
| 1761 intptr_t str_size = (str_obj.Length() * str_obj.CharSize()); | 1785 intptr_t str_size = (str_obj.Length() * str_obj.CharSize()); |
| 1762 if ((length < str_size) || (length > String::kMaxElements)) { | 1786 if ((length < str_size) || (length > String::kMaxElements)) { |
| 1763 return Api::NewError("Dart_MakeExternalString " | 1787 return Api::NewError("Dart_MakeExternalString " |
| 1764 "expects argument length to be in the range" | 1788 "expects argument length to be in the range" |
| 1765 "[%"Pd"..%"Pd"].", | 1789 "[%"Pd"..%"Pd"].", |
| 1766 str_size, String::kMaxElements); | 1790 str_size, String::kMaxElements); |
| 1767 } | 1791 } |
| 1792 if (str_obj.IsCanonical()) { |
| 1793 // Since the string object is read only we do not externalize |
| 1794 // the string but instead copy the contents of the string into the |
| 1795 // specified buffer and return a Null object. |
| 1796 // This ensures that the embedder does not have to call again |
| 1797 // to get at the contents. |
| 1798 intptr_t copy_len = str_obj.Length(); |
| 1799 if (str_obj.IsOneByteString()) { |
| 1800 ASSERT(length >= copy_len); |
| 1801 uint8_t* latin1_array = reinterpret_cast<uint8_t*>(array); |
| 1802 for (intptr_t i = 0; i < copy_len; i++) { |
| 1803 latin1_array[i] = static_cast<uint8_t>(str_obj.CharAt(i)); |
| 1804 } |
| 1805 } else { |
| 1806 ASSERT(str_obj.IsTwoByteString()); |
| 1807 ASSERT(length >= (copy_len * sizeof(uint16_t))); |
| 1808 uint16_t* utf16_array = reinterpret_cast<uint16_t*>(array); |
| 1809 for (intptr_t i = 0; i < copy_len; i++) { |
| 1810 utf16_array[i] = static_cast<uint16_t>(str_obj.CharAt(i)); |
| 1811 } |
| 1812 } |
| 1813 return Api::Null(isolate); |
| 1814 } |
| 1768 return Api::NewHandle(isolate, | 1815 return Api::NewHandle(isolate, |
| 1769 str_obj.MakeExternal(array, length, peer, cback)); | 1816 str_obj.MakeExternal(array, length, peer, cback)); |
| 1770 } | 1817 } |
| 1771 | 1818 |
| 1772 | 1819 |
| 1773 // --- Lists --- | 1820 // --- Lists --- |
| 1774 | 1821 |
| 1775 | 1822 |
| 1776 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) { | 1823 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) { |
| 1777 if (obj.IsInstance()) { | 1824 if (obj.IsInstance()) { |
| (...skipping 2681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4459 } | 4506 } |
| 4460 { | 4507 { |
| 4461 NoGCScope no_gc; | 4508 NoGCScope no_gc; |
| 4462 RawObject* raw_obj = obj.raw(); | 4509 RawObject* raw_obj = obj.raw(); |
| 4463 isolate->heap()->SetPeer(raw_obj, peer); | 4510 isolate->heap()->SetPeer(raw_obj, peer); |
| 4464 } | 4511 } |
| 4465 return Api::Success(isolate); | 4512 return Api::Success(isolate); |
| 4466 } | 4513 } |
| 4467 | 4514 |
| 4468 } // namespace dart | 4515 } // namespace dart |
| OLD | NEW |