Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Side by Side Diff: runtime/vm/dart_api_impl.cc

Issue 11318018: - Represent strings internally in UTF-16 format, this makes it (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/custom_isolate_test.cc ('k') | runtime/vm/dart_api_impl_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 *peer = data->peer(); 266 *peer = data->peer();
267 return true; 267 return true;
268 } 268 }
269 case kExternalTwoByteStringCid: { 269 case kExternalTwoByteStringCid: {
270 RawExternalTwoByteString* raw_string = 270 RawExternalTwoByteString* raw_string =
271 reinterpret_cast<RawExternalTwoByteString*>(raw_obj)->ptr(); 271 reinterpret_cast<RawExternalTwoByteString*>(raw_obj)->ptr();
272 ExternalStringData<uint16_t>* data = raw_string->external_data_; 272 ExternalStringData<uint16_t>* data = raw_string->external_data_;
273 *peer = data->peer(); 273 *peer = data->peer();
274 return true; 274 return true;
275 } 275 }
276 case kExternalFourByteStringCid: {
277 RawExternalFourByteString* raw_string =
278 reinterpret_cast<RawExternalFourByteString*>(raw_obj)->ptr();
279 ExternalStringData<uint32_t>* data = raw_string->external_data_;
280 *peer = data->peer();
281 return true;
282 }
283 } 276 }
284 return false; 277 return false;
285 } 278 }
286 279
287 280
288 // When we want to return a handle to a type to the user, we handle 281 // When we want to return a handle to a type to the user, we handle
289 // class-types differently than some other types. 282 // class-types differently than some other types.
290 static Dart_Handle TypeToHandle(Isolate* isolate, 283 static Dart_Handle TypeToHandle(Isolate* isolate,
291 const char* function_name, 284 const char* function_name,
292 const AbstractType& type) { 285 const AbstractType& type) {
(...skipping 1222 matching lines...) Expand 10 before | Expand all | Expand 10 after
1515 1508
1516 1509
1517 // --- Strings --- 1510 // --- Strings ---
1518 1511
1519 1512
1520 DART_EXPORT bool Dart_IsString(Dart_Handle object) { 1513 DART_EXPORT bool Dart_IsString(Dart_Handle object) {
1521 return RawObject::IsStringClassId(Api::ClassId(object)); 1514 return RawObject::IsStringClassId(Api::ClassId(object));
1522 } 1515 }
1523 1516
1524 1517
1525 DART_EXPORT bool Dart_IsString8(Dart_Handle object) { 1518 DART_EXPORT bool Dart_IsAsciiString(Dart_Handle object) {
1526 return RawObject::IsOneByteStringClassId(Api::ClassId(object)); 1519 return RawObject::IsOneByteStringClassId(Api::ClassId(object));
1527 } 1520 }
1528 1521
1529 1522
1530 DART_EXPORT bool Dart_IsString16(Dart_Handle object) {
1531 return RawObject::IsTwoByteStringClassId(Api::ClassId(object));
1532 }
1533
1534
1535 DART_EXPORT Dart_Handle Dart_StringLength(Dart_Handle str, intptr_t* len) { 1523 DART_EXPORT Dart_Handle Dart_StringLength(Dart_Handle str, intptr_t* len) {
1536 Isolate* isolate = Isolate::Current(); 1524 Isolate* isolate = Isolate::Current();
1537 DARTSCOPE(isolate); 1525 DARTSCOPE(isolate);
1538 const String& str_obj = Api::UnwrapStringHandle(isolate, str); 1526 const String& str_obj = Api::UnwrapStringHandle(isolate, str);
1539 if (str_obj.IsNull()) { 1527 if (str_obj.IsNull()) {
1540 RETURN_TYPE_ERROR(isolate, str, String); 1528 RETURN_TYPE_ERROR(isolate, str, String);
1541 } 1529 }
1542 *len = str_obj.Length(); 1530 *len = str_obj.Length();
1543 return Api::Success(isolate); 1531 return Api::Success(isolate);
1544 } 1532 }
1545 1533
1546 1534
1547 DART_EXPORT Dart_Handle Dart_NewString(const char* str) { 1535 DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char* str) {
1548 Isolate* isolate = Isolate::Current(); 1536 Isolate* isolate = Isolate::Current();
1549 DARTSCOPE(isolate); 1537 DARTSCOPE(isolate);
1550 if (str == NULL) { 1538 if (str == NULL) {
1551 RETURN_NULL_ERROR(str); 1539 RETURN_NULL_ERROR(str);
1552 } 1540 }
1553 if (!Utf8::IsValid(str)) {
1554 return Api::NewError("%s expects argument 'str' to be valid UTF-8.",
1555 CURRENT_FUNC);
1556 }
1557 return Api::NewHandle(isolate, String::New(str)); 1541 return Api::NewHandle(isolate, String::New(str));
1558 } 1542 }
1559 1543
1560 1544
1561 DART_EXPORT Dart_Handle Dart_NewString8(const uint8_t* codepoints, 1545 DART_EXPORT Dart_Handle Dart_NewStringFromUTF8(const uint8_t* utf8_array,
1562 intptr_t length) { 1546 intptr_t length) {
1563 Isolate* isolate = Isolate::Current(); 1547 Isolate* isolate = Isolate::Current();
1564 DARTSCOPE(isolate); 1548 DARTSCOPE(isolate);
1565 if (codepoints == NULL && length != 0) { 1549 if (utf8_array == NULL && length != 0) {
1566 RETURN_NULL_ERROR(codepoints); 1550 RETURN_NULL_ERROR(utf8_array);
1567 } 1551 }
1568 CHECK_LENGTH(length, String::kMaxElements); 1552 CHECK_LENGTH(length, String::kMaxElements);
1569 return Api::NewHandle(isolate, String::New(codepoints, length)); 1553 if (!Utf8::IsValid(utf8_array, length)) {
1554 return Api::NewError("%s expects argument 'str' to be valid UTF-8.",
1555 CURRENT_FUNC);
1556 }
1557 return Api::NewHandle(isolate, String::New(utf8_array, length));
1570 } 1558 }
1571 1559
1572 1560
1573 DART_EXPORT Dart_Handle Dart_NewString16(const uint16_t* codepoints, 1561 DART_EXPORT Dart_Handle Dart_NewStringFromUTF16(const uint16_t* utf16_array,
1574 intptr_t length) { 1562 intptr_t length) {
1575 Isolate* isolate = Isolate::Current(); 1563 Isolate* isolate = Isolate::Current();
1576 DARTSCOPE(isolate); 1564 DARTSCOPE(isolate);
1577 if (codepoints == NULL && length != 0) { 1565 if (utf16_array == NULL && length != 0) {
1578 RETURN_NULL_ERROR(codepoints); 1566 RETURN_NULL_ERROR(utf16_array);
1579 } 1567 }
1580 CHECK_LENGTH(length, String::kMaxElements); 1568 CHECK_LENGTH(length, String::kMaxElements);
1581 return Api::NewHandle(isolate, String::New(codepoints, length)); 1569 return Api::NewHandle(isolate, String::New(utf16_array, length));
1582 } 1570 }
1583 1571
1584 1572
1585 DART_EXPORT Dart_Handle Dart_NewString32(const uint32_t* codepoints, 1573 DART_EXPORT Dart_Handle Dart_NewStringFromUTF32(const uint32_t* utf32_array,
1586 intptr_t length) { 1574 intptr_t length) {
1587 Isolate* isolate = Isolate::Current(); 1575 Isolate* isolate = Isolate::Current();
1588 DARTSCOPE(isolate); 1576 DARTSCOPE(isolate);
1589 if (codepoints == NULL && length != 0) { 1577 if (utf32_array == NULL && length != 0) {
1590 RETURN_NULL_ERROR(codepoints); 1578 RETURN_NULL_ERROR(utf32_array);
1591 } 1579 }
1592 CHECK_LENGTH(length, String::kMaxElements); 1580 CHECK_LENGTH(length, String::kMaxElements);
1593 return Api::NewHandle(isolate, String::New(codepoints, length)); 1581 return Api::NewHandle(isolate, String::New(utf32_array, length));
1594 } 1582 }
1595 1583
1596 1584
1597 DART_EXPORT bool Dart_IsExternalString(Dart_Handle object) { 1585 DART_EXPORT bool Dart_IsExternalString(Dart_Handle object) {
1598 return RawObject::IsExternalStringClassId(Api::ClassId(object)); 1586 return RawObject::IsExternalStringClassId(Api::ClassId(object));
1599 } 1587 }
1600 1588
1601 1589
1602 DART_EXPORT Dart_Handle Dart_ExternalStringGetPeer(Dart_Handle object, 1590 DART_EXPORT Dart_Handle Dart_ExternalStringGetPeer(Dart_Handle object,
1603 void** peer) { 1591 void** peer) {
(...skipping 12 matching lines...) Expand all
1616 RETURN_TYPE_ERROR(Isolate::Current(), object, String); 1604 RETURN_TYPE_ERROR(Isolate::Current(), object, String);
1617 } else { 1605 } else {
1618 return 1606 return
1619 Api::NewError( 1607 Api::NewError(
1620 "%s expects argument 'object' to be an external String.", 1608 "%s expects argument 'object' to be an external String.",
1621 CURRENT_FUNC); 1609 CURRENT_FUNC);
1622 } 1610 }
1623 } 1611 }
1624 1612
1625 1613
1626 DART_EXPORT Dart_Handle Dart_NewExternalString8(const uint8_t* codepoints, 1614 DART_EXPORT Dart_Handle Dart_NewExternalUTF8String(const uint8_t* utf8_array,
1627 intptr_t length, 1615 intptr_t length,
1628 void* peer, 1616 void* peer,
1629 Dart_PeerFinalizer callback) { 1617 Dart_PeerFinalizer cback) {
1630 Isolate* isolate = Isolate::Current(); 1618 Isolate* isolate = Isolate::Current();
1631 DARTSCOPE(isolate); 1619 DARTSCOPE(isolate);
1632 if (codepoints == NULL && length != 0) { 1620 if (utf8_array == NULL && length != 0) {
1633 RETURN_NULL_ERROR(codepoints); 1621 RETURN_NULL_ERROR(utf8_array);
1634 } 1622 }
1635 CHECK_LENGTH(length, String::kMaxElements); 1623 CHECK_LENGTH(length, String::kMaxElements);
1636 return Api::NewHandle( 1624 return Api::NewHandle(isolate,
1637 isolate, String::NewExternal(codepoints, length, peer, callback)); 1625 String::NewExternal(utf8_array, length, peer, cback));
1638 } 1626 }
1639 1627
1640 1628
1641 DART_EXPORT Dart_Handle Dart_NewExternalString16(const uint16_t* codepoints, 1629 DART_EXPORT Dart_Handle Dart_NewExternalUTF16String(const uint16_t* utf16_array,
1642 intptr_t length, 1630 intptr_t length,
1643 void* peer, 1631 void* peer,
1644 Dart_PeerFinalizer callback) { 1632 Dart_PeerFinalizer cback) {
1645 Isolate* isolate = Isolate::Current(); 1633 Isolate* isolate = Isolate::Current();
1646 DARTSCOPE(isolate); 1634 DARTSCOPE(isolate);
1647 if (codepoints == NULL && length != 0) { 1635 if (utf16_array == NULL && length != 0) {
1648 RETURN_NULL_ERROR(codepoints); 1636 RETURN_NULL_ERROR(utf16_array);
1649 } 1637 }
1650 CHECK_LENGTH(length, String::kMaxElements); 1638 CHECK_LENGTH(length, String::kMaxElements);
1651 return Api::NewHandle( 1639 return Api::NewHandle(isolate,
1652 isolate, String::NewExternal(codepoints, length, peer, callback)); 1640 String::NewExternal(utf16_array, length, peer, cback));
1653 } 1641 }
1654 1642
1655 1643
1656 DART_EXPORT Dart_Handle Dart_NewExternalString32(const uint32_t* codepoints, 1644 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object,
1657 intptr_t length, 1645 const char** cstr) {
1658 void* peer,
1659 Dart_PeerFinalizer callback) {
1660 Isolate* isolate = Isolate::Current(); 1646 Isolate* isolate = Isolate::Current();
1661 DARTSCOPE(isolate); 1647 DARTSCOPE(isolate);
1662 if (codepoints == NULL && length != 0) { 1648 const String& str_obj = Api::UnwrapStringHandle(isolate, object);
1663 RETURN_NULL_ERROR(codepoints); 1649 if (str_obj.IsNull()) {
1650 RETURN_TYPE_ERROR(isolate, object, String);
1664 } 1651 }
1665 CHECK_LENGTH(length, String::kMaxElements); 1652 intptr_t string_length = Utf8::Length(str_obj);
1666 return Api::NewHandle( 1653 char* res = Api::TopScope(isolate)->zone()->Alloc<char>(string_length + 1);
1667 isolate, String::NewExternal(codepoints, length, peer, callback)); 1654 if (res == NULL) {
1668 } 1655 return Api::NewError("Unable to allocate memory");
1669
1670
1671 DART_EXPORT Dart_Handle Dart_StringGet8(Dart_Handle str,
1672 uint8_t* codepoints,
1673 intptr_t* length) {
1674 Isolate* isolate = Isolate::Current();
1675 DARTSCOPE(isolate);
1676 const OneByteString& str_obj = Api::UnwrapOneByteStringHandle(isolate, str);
1677 if (str_obj.IsNull()) {
1678 RETURN_TYPE_ERROR(isolate, str, String8);
1679 } 1656 }
1680 intptr_t str_len = str_obj.Length(); 1657 const char* string_value = str_obj.ToCString();
1681 intptr_t copy_len = (str_len > *length) ? *length : str_len; 1658 memmove(res, string_value, string_length + 1);
1682 for (intptr_t i = 0; i < copy_len; i++) { 1659 ASSERT(res[string_length] == '\0');
1683 codepoints[i] = static_cast<uint8_t>(str_obj.CharAt(i)); 1660 *cstr = res;
1684 }
1685 *length= copy_len;
1686 return Api::Success(isolate); 1661 return Api::Success(isolate);
1687 } 1662 }
1688 1663
1689 1664
1690 DART_EXPORT Dart_Handle Dart_StringGet16(Dart_Handle str, 1665 DART_EXPORT Dart_Handle Dart_StringToUTF8(Dart_Handle str,
1691 uint16_t* codepoints, 1666 uint8_t* utf8_array,
1692 intptr_t* length) { 1667 intptr_t* length) {
1693 Isolate* isolate = Isolate::Current(); 1668 Isolate* isolate = Isolate::Current();
1694 DARTSCOPE(isolate); 1669 DARTSCOPE(isolate);
1695 const String& str_obj = Api::UnwrapStringHandle(isolate, str); 1670 const String& str_obj = Api::UnwrapStringHandle(isolate, str);
1696 if (str_obj.IsNull()) { 1671 if (str_obj.IsNull()) {
1697 RETURN_TYPE_ERROR(isolate, str, String); 1672 RETURN_TYPE_ERROR(isolate, str, String);
1698 } 1673 }
1699 if (str_obj.CharSize() > String::kTwoByteChar) { 1674 intptr_t str_len = str_obj.Length();
Bill Hesse 2012/11/01 11:35:02 This seems totally wrong, unless we restrict the f
siva 2012/11/01 20:04:11 Yes that should be intptr_t str_len = Utf8::Length
1700 return Api::NewError("Object is not a String16 or String8"); 1675 if (str_len > *length) {
1676 return Api::NewError("Input array is not large enough to hold the result");
1701 } 1677 }
1702 intptr_t str_len = str_obj.Length(); 1678 str_obj.ToUTF8(utf8_array, str_len);
1703 intptr_t copy_len = (str_len > *length) ? *length : str_len; 1679 *length= str_len;
1704 for (intptr_t i = 0; i < copy_len; i++) {
1705 codepoints[i] = static_cast<uint16_t>(str_obj.CharAt(i));
1706 }
1707 *length = copy_len;
1708 return Api::Success(isolate); 1680 return Api::Success(isolate);
1709 } 1681 }
1710 1682
1711 1683
1712 DART_EXPORT Dart_Handle Dart_StringGet32(Dart_Handle str, 1684 DART_EXPORT Dart_Handle Dart_StringToUTF16(Dart_Handle str,
1713 uint32_t* codepoints, 1685 uint16_t* utf16_array,
1714 intptr_t* length) { 1686 intptr_t* length) {
1715 Isolate* isolate = Isolate::Current(); 1687 Isolate* isolate = Isolate::Current();
1716 DARTSCOPE(isolate); 1688 DARTSCOPE(isolate);
1717 const String& str_obj = Api::UnwrapStringHandle(isolate, str); 1689 const String& str_obj = Api::UnwrapStringHandle(isolate, str);
1718 if (str_obj.IsNull()) { 1690 if (str_obj.IsNull()) {
1719 RETURN_TYPE_ERROR(isolate, str, String); 1691 RETURN_TYPE_ERROR(isolate, str, String);
1720 } 1692 }
1721 intptr_t str_len = str_obj.Length(); 1693 intptr_t str_len = str_obj.Length();
1722 intptr_t copy_len = (str_len > *length) ? *length : str_len; 1694 intptr_t copy_len = (str_len > *length) ? *length : str_len;
1723 for (intptr_t i = 0; i < copy_len; i++) { 1695 for (intptr_t i = 0; i < copy_len; i++) {
1724 codepoints[i] = static_cast<uint32_t>(str_obj.CharAt(i)); 1696 utf16_array[i] = static_cast<uint16_t>(str_obj.CharAt(i));
1725 } 1697 }
1726 *length = copy_len; 1698 *length = copy_len;
1727 return Api::Success(isolate); 1699 return Api::Success(isolate);
1728 } 1700 }
1729 1701
1730 1702
1731 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object,
1732 const char** result) {
1733 Isolate* isolate = Isolate::Current();
1734 DARTSCOPE(isolate);
1735 const String& str_obj = Api::UnwrapStringHandle(isolate, object);
1736 if (str_obj.IsNull()) {
1737 RETURN_TYPE_ERROR(isolate, object, String);
1738 }
1739 intptr_t string_length = Utf8::Length(str_obj);
1740 char* res = Api::TopScope(isolate)->zone()->Alloc<char>(string_length + 1);
1741 if (res == NULL) {
1742 return Api::NewError("Unable to allocate memory");
1743 }
1744 const char* string_value = str_obj.ToCString();
1745 memmove(res, string_value, string_length + 1);
1746 ASSERT(res[string_length] == '\0');
1747 *result = res;
1748 return Api::Success(isolate);
1749 }
1750
1751
1752 DART_EXPORT Dart_Handle Dart_StringToBytes(Dart_Handle object,
1753 const uint8_t** bytes,
1754 intptr_t *length) {
1755 Isolate* isolate = Isolate::Current();
1756 DARTSCOPE(isolate);
1757 const String& str = Api::UnwrapStringHandle(isolate, object);
1758 if (str.IsNull()) {
1759 RETURN_TYPE_ERROR(isolate, object, String);
1760 }
1761 if (bytes == NULL) {
1762 RETURN_NULL_ERROR(bytes);
1763 }
1764 if (length == NULL) {
1765 RETURN_NULL_ERROR(length);
1766 }
1767 const char* cstring = str.ToCString();
1768 *length = Utf8::Length(str);
1769 uint8_t* result = Api::TopScope(isolate)->zone()->Alloc<uint8_t>(*length);
1770 if (result == NULL) {
1771 return Api::NewError("Unable to allocate memory");
1772 }
1773 memmove(result, cstring, *length);
1774 *bytes = result;
1775 return Api::Success(isolate);
1776 }
1777
1778
1779 // --- Lists --- 1703 // --- Lists ---
1780 1704
1781 1705
1782 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) { 1706 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) {
1783 if (obj.IsInstance()) { 1707 if (obj.IsInstance()) {
1784 const Instance& instance = Instance::Cast(obj); 1708 const Instance& instance = Instance::Cast(obj);
1785 const Type& type = 1709 const Type& type =
1786 Type::Handle(isolate, isolate->object_store()->list_interface()); 1710 Type::Handle(isolate, isolate->object_store()->list_interface());
1787 Error& malformed_type_error = Error::Handle(isolate); 1711 Error& malformed_type_error = Error::Handle(isolate);
1788 if (instance.IsInstanceOf(type, 1712 if (instance.IsInstanceOf(type,
(...skipping 2748 matching lines...) Expand 10 before | Expand all | Expand 10 after
4537 } 4461 }
4538 { 4462 {
4539 NoGCScope no_gc; 4463 NoGCScope no_gc;
4540 RawObject* raw_obj = obj.raw(); 4464 RawObject* raw_obj = obj.raw();
4541 isolate->heap()->SetPeer(raw_obj, peer); 4465 isolate->heap()->SetPeer(raw_obj, peer);
4542 } 4466 }
4543 return Api::Success(isolate); 4467 return Api::Success(isolate);
4544 } 4468 }
4545 4469
4546 } // namespace dart 4470 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/custom_isolate_test.cc ('k') | runtime/vm/dart_api_impl_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698