| 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 1658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1669 return Api::NewError("Unable to allocate memory"); | 1669 return Api::NewError("Unable to allocate memory"); |
| 1670 } | 1670 } |
| 1671 const char* string_value = str_obj.ToCString(); | 1671 const char* string_value = str_obj.ToCString(); |
| 1672 memmove(res, string_value, string_length + 1); | 1672 memmove(res, string_value, string_length + 1); |
| 1673 ASSERT(res[string_length] == '\0'); | 1673 ASSERT(res[string_length] == '\0'); |
| 1674 *cstr = res; | 1674 *cstr = res; |
| 1675 return Api::Success(isolate); | 1675 return Api::Success(isolate); |
| 1676 } | 1676 } |
| 1677 | 1677 |
| 1678 | 1678 |
| 1679 DART_EXPORT Dart_Handle Dart_StringToLatin1(Dart_Handle str, |
| 1680 uint8_t** latin1_array, |
| 1681 intptr_t* length) { |
| 1682 Isolate* isolate = Isolate::Current(); |
| 1683 DARTSCOPE(isolate); |
| 1684 if (latin1_array == NULL) { |
| 1685 RETURN_NULL_ERROR(latin1_array); |
| 1686 } |
| 1687 if (length == NULL) { |
| 1688 RETURN_NULL_ERROR(length); |
| 1689 } |
| 1690 const String& str_obj = Api::UnwrapStringHandle(isolate, str); |
| 1691 if (str_obj.IsNull() || !Dart_IsStringLatin1(str)) { |
| 1692 RETURN_TYPE_ERROR(isolate, str, String); |
| 1693 } |
| 1694 intptr_t str_len = str_obj.Length(); |
| 1695 uint8_t* res = Api::TopScope(isolate)->zone()->Alloc<uint8_t>(str_len); |
| 1696 if (res == NULL) { |
| 1697 return Api::NewError("Unable to allocate memory"); |
| 1698 } |
| 1699 // We have already asserted that the string object is a Latin-1 string |
| 1700 // so we can copy the characters over using a simple loop. |
| 1701 for (intptr_t i = 0; i < str_len; i++) { |
| 1702 res[i] = str_obj.CharAt(i); |
| 1703 } |
| 1704 *latin1_array = res; |
| 1705 *length = str_len; |
| 1706 return Api::Success(isolate); |
| 1707 } |
| 1708 |
| 1709 |
| 1679 DART_EXPORT Dart_Handle Dart_StringToUTF8(Dart_Handle str, | 1710 DART_EXPORT Dart_Handle Dart_StringToUTF8(Dart_Handle str, |
| 1680 uint8_t** utf8_array, | 1711 uint8_t** utf8_array, |
| 1681 intptr_t* length) { | 1712 intptr_t* length) { |
| 1682 Isolate* isolate = Isolate::Current(); | 1713 Isolate* isolate = Isolate::Current(); |
| 1683 DARTSCOPE(isolate); | 1714 DARTSCOPE(isolate); |
| 1684 if (utf8_array == NULL) { | 1715 if (utf8_array == NULL) { |
| 1685 RETURN_NULL_ERROR(utf8_array); | 1716 RETURN_NULL_ERROR(utf8_array); |
| 1686 } | 1717 } |
| 1687 if (length == NULL) { | 1718 if (length == NULL) { |
| 1688 RETURN_NULL_ERROR(length); | 1719 RETURN_NULL_ERROR(length); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1747 const String& str_obj = Api::UnwrapStringHandle(isolate, str); | 1778 const String& str_obj = Api::UnwrapStringHandle(isolate, str); |
| 1748 if (str_obj.IsExternal()) { | 1779 if (str_obj.IsExternal()) { |
| 1749 return str; // String is already an external string. | 1780 return str; // String is already an external string. |
| 1750 } | 1781 } |
| 1751 if (str_obj.IsNull()) { | 1782 if (str_obj.IsNull()) { |
| 1752 RETURN_TYPE_ERROR(isolate, str, String); | 1783 RETURN_TYPE_ERROR(isolate, str, String); |
| 1753 } | 1784 } |
| 1754 if (array == NULL) { | 1785 if (array == NULL) { |
| 1755 RETURN_NULL_ERROR(array); | 1786 RETURN_NULL_ERROR(array); |
| 1756 } | 1787 } |
| 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()); | 1788 intptr_t str_size = (str_obj.Length() * str_obj.CharSize()); |
| 1762 if ((length < str_size) || (length > String::kMaxElements)) { | 1789 if ((length < str_size) || (length > String::kMaxElements)) { |
| 1763 return Api::NewError("Dart_MakeExternalString " | 1790 return Api::NewError("Dart_MakeExternalString " |
| 1764 "expects argument length to be in the range" | 1791 "expects argument length to be in the range" |
| 1765 "[%"Pd"..%"Pd"].", | 1792 "[%"Pd"..%"Pd"].", |
| 1766 str_size, String::kMaxElements); | 1793 str_size, String::kMaxElements); |
| 1767 } | 1794 } |
| 1795 if (str_obj.IsCanonical()) { |
| 1796 // Since the string object is read only we do not externalize |
| 1797 // the string but instead copy the contents of the string into the |
| 1798 // specified buffer and return a Null object. |
| 1799 // This ensures that the embedder does not have to call again |
| 1800 // to get at the contents. |
| 1801 intptr_t copy_len = str_obj.Length(); |
| 1802 if (str_obj.IsOneByteString()) { |
| 1803 ASSERT(length >= copy_len); |
| 1804 uint8_t* latin1_array = reinterpret_cast<uint8_t*>(array); |
| 1805 for (intptr_t i = 0; i < copy_len; i++) { |
| 1806 latin1_array[i] = static_cast<uint8_t>(str_obj.CharAt(i)); |
| 1807 } |
| 1808 } else { |
| 1809 ASSERT(str_obj.IsTwoByteString()); |
| 1810 ASSERT(length >= (copy_len * sizeof(uint16_t))); |
| 1811 uint16_t* utf16_array = reinterpret_cast<uint16_t*>(array); |
| 1812 for (intptr_t i = 0; i < copy_len; i++) { |
| 1813 utf16_array[i] = static_cast<uint16_t>(str_obj.CharAt(i)); |
| 1814 } |
| 1815 } |
| 1816 return Api::Null(isolate); |
| 1817 } |
| 1768 return Api::NewHandle(isolate, | 1818 return Api::NewHandle(isolate, |
| 1769 str_obj.MakeExternal(array, length, peer, cback)); | 1819 str_obj.MakeExternal(array, length, peer, cback)); |
| 1770 } | 1820 } |
| 1771 | 1821 |
| 1772 | 1822 |
| 1773 // --- Lists --- | 1823 // --- Lists --- |
| 1774 | 1824 |
| 1775 | 1825 |
| 1776 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) { | 1826 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) { |
| 1777 if (obj.IsInstance()) { | 1827 if (obj.IsInstance()) { |
| (...skipping 2681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4459 } | 4509 } |
| 4460 { | 4510 { |
| 4461 NoGCScope no_gc; | 4511 NoGCScope no_gc; |
| 4462 RawObject* raw_obj = obj.raw(); | 4512 RawObject* raw_obj = obj.raw(); |
| 4463 isolate->heap()->SetPeer(raw_obj, peer); | 4513 isolate->heap()->SetPeer(raw_obj, peer); |
| 4464 } | 4514 } |
| 4465 return Api::Success(isolate); | 4515 return Api::Success(isolate); |
| 4466 } | 4516 } |
| 4467 | 4517 |
| 4468 } // namespace dart | 4518 } // namespace dart |
| OLD | NEW |