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

Side by Side Diff: vm/dart_api_impl.cc

Issue 11275008: - Represent strings internally in UTF-16 format, this makes it (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
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
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) {
1526 return RawObject::IsOneByteStringClassId(Api::ClassId(object));
1527 }
1528
1529
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) { 1518 DART_EXPORT Dart_Handle Dart_StringLength(Dart_Handle str, intptr_t* len) {
1536 Isolate* isolate = Isolate::Current(); 1519 Isolate* isolate = Isolate::Current();
1537 DARTSCOPE(isolate); 1520 DARTSCOPE(isolate);
1538 const String& str_obj = Api::UnwrapStringHandle(isolate, str); 1521 const String& str_obj = Api::UnwrapStringHandle(isolate, str);
1539 if (str_obj.IsNull()) { 1522 if (str_obj.IsNull()) {
1540 RETURN_TYPE_ERROR(isolate, str, String); 1523 RETURN_TYPE_ERROR(isolate, str, String);
1541 } 1524 }
1542 *len = str_obj.Length(); 1525 *len = str_obj.Length();
1543 return Api::Success(isolate); 1526 return Api::Success(isolate);
1544 } 1527 }
1545 1528
1546 1529
1547 DART_EXPORT Dart_Handle Dart_NewString(const char* str) { 1530 DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char* str) {
1548 Isolate* isolate = Isolate::Current(); 1531 Isolate* isolate = Isolate::Current();
1549 DARTSCOPE(isolate); 1532 DARTSCOPE(isolate);
1550 if (str == NULL) { 1533 if (str == NULL) {
1551 RETURN_NULL_ERROR(str); 1534 RETURN_NULL_ERROR(str);
1552 } 1535 }
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)); 1536 return Api::NewHandle(isolate, String::New(str));
1558 } 1537 }
1559 1538
1560 1539
1561 DART_EXPORT Dart_Handle Dart_NewString8(const uint8_t* codepoints, 1540 DART_EXPORT Dart_Handle Dart_NewStringFromUTF8(const uint8_t* utf8_array,
1562 intptr_t length) { 1541 intptr_t length) {
1563 Isolate* isolate = Isolate::Current(); 1542 Isolate* isolate = Isolate::Current();
1564 DARTSCOPE(isolate); 1543 DARTSCOPE(isolate);
1565 if (codepoints == NULL && length != 0) { 1544 if (utf8_array == NULL && length != 0) {
1566 RETURN_NULL_ERROR(codepoints); 1545 RETURN_NULL_ERROR(utf8_array);
1567 } 1546 }
1568 CHECK_LENGTH(length, String::kMaxElements); 1547 CHECK_LENGTH(length, String::kMaxElements);
1569 return Api::NewHandle(isolate, String::New(codepoints, length)); 1548 if (!Utf8::IsValid(utf8_array, length)) {
1549 return Api::NewError("%s expects argument 'str' to be valid UTF-8.",
1550 CURRENT_FUNC);
1551 }
1552 return Api::NewHandle(isolate, String::New(utf8_array, length));
1570 } 1553 }
1571 1554
1572 1555
1573 DART_EXPORT Dart_Handle Dart_NewString16(const uint16_t* codepoints, 1556 DART_EXPORT Dart_Handle Dart_NewStringFromUTF16(const uint16_t* utf16_array,
1574 intptr_t length) { 1557 intptr_t length) {
1575 Isolate* isolate = Isolate::Current(); 1558 Isolate* isolate = Isolate::Current();
1576 DARTSCOPE(isolate); 1559 DARTSCOPE(isolate);
1577 if (codepoints == NULL && length != 0) { 1560 if (utf16_array == NULL && length != 0) {
1578 RETURN_NULL_ERROR(codepoints); 1561 RETURN_NULL_ERROR(utf16_array);
1579 } 1562 }
1580 CHECK_LENGTH(length, String::kMaxElements); 1563 CHECK_LENGTH(length, String::kMaxElements);
1581 return Api::NewHandle(isolate, String::New(codepoints, length)); 1564 return Api::NewHandle(isolate, String::New(utf16_array, length));
1582 } 1565 }
1583 1566
1584 1567
1585 DART_EXPORT Dart_Handle Dart_NewString32(const uint32_t* codepoints, 1568 DART_EXPORT Dart_Handle Dart_NewStringFromUTF32(const uint32_t* utf32_array,
1586 intptr_t length) { 1569 intptr_t length) {
1587 Isolate* isolate = Isolate::Current(); 1570 Isolate* isolate = Isolate::Current();
1588 DARTSCOPE(isolate); 1571 DARTSCOPE(isolate);
1589 if (codepoints == NULL && length != 0) { 1572 if (utf32_array == NULL && length != 0) {
1590 RETURN_NULL_ERROR(codepoints); 1573 RETURN_NULL_ERROR(utf32_array);
1591 } 1574 }
1592 CHECK_LENGTH(length, String::kMaxElements); 1575 CHECK_LENGTH(length, String::kMaxElements);
1593 return Api::NewHandle(isolate, String::New(codepoints, length)); 1576 return Api::NewHandle(isolate, String::New(utf32_array, length));
1594 } 1577 }
1595 1578
1596 1579
1597 DART_EXPORT bool Dart_IsExternalString(Dart_Handle object) { 1580 DART_EXPORT bool Dart_IsExternalString(Dart_Handle object) {
1598 return RawObject::IsExternalStringClassId(Api::ClassId(object)); 1581 return RawObject::IsExternalStringClassId(Api::ClassId(object));
1599 } 1582 }
1600 1583
1601 1584
1602 DART_EXPORT Dart_Handle Dart_ExternalStringGetPeer(Dart_Handle object, 1585 DART_EXPORT Dart_Handle Dart_ExternalStringGetPeer(Dart_Handle object,
1603 void** peer) { 1586 void** peer) {
(...skipping 12 matching lines...) Expand all
1616 RETURN_TYPE_ERROR(Isolate::Current(), object, String); 1599 RETURN_TYPE_ERROR(Isolate::Current(), object, String);
1617 } else { 1600 } else {
1618 return 1601 return
1619 Api::NewError( 1602 Api::NewError(
1620 "%s expects argument 'object' to be an external String.", 1603 "%s expects argument 'object' to be an external String.",
1621 CURRENT_FUNC); 1604 CURRENT_FUNC);
1622 } 1605 }
1623 } 1606 }
1624 1607
1625 1608
1626 DART_EXPORT Dart_Handle Dart_NewExternalString8(const uint8_t* codepoints, 1609 DART_EXPORT Dart_Handle Dart_NewExternalUTF8String(const uint8_t* utf8_array,
1627 intptr_t length, 1610 intptr_t length,
1628 void* peer, 1611 void* peer,
1629 Dart_PeerFinalizer callback) { 1612 Dart_PeerFinalizer cback) {
1630 Isolate* isolate = Isolate::Current(); 1613 Isolate* isolate = Isolate::Current();
1631 DARTSCOPE(isolate); 1614 DARTSCOPE(isolate);
1632 if (codepoints == NULL && length != 0) { 1615 if (utf8_array == NULL && length != 0) {
1633 RETURN_NULL_ERROR(codepoints); 1616 RETURN_NULL_ERROR(utf8_array);
1634 } 1617 }
1635 CHECK_LENGTH(length, String::kMaxElements); 1618 CHECK_LENGTH(length, String::kMaxElements);
1636 return Api::NewHandle( 1619 return Api::NewHandle(isolate,
1637 isolate, String::NewExternal(codepoints, length, peer, callback)); 1620 String::NewExternal(utf8_array, length, peer, cback));
1638 } 1621 }
1639 1622
1640 1623
1641 DART_EXPORT Dart_Handle Dart_NewExternalString16(const uint16_t* codepoints, 1624 DART_EXPORT Dart_Handle Dart_NewExternalUTF16String(const uint16_t* utf16_array,
1642 intptr_t length, 1625 intptr_t length,
1643 void* peer, 1626 void* peer,
1644 Dart_PeerFinalizer callback) { 1627 Dart_PeerFinalizer cback) {
1645 Isolate* isolate = Isolate::Current(); 1628 Isolate* isolate = Isolate::Current();
1646 DARTSCOPE(isolate); 1629 DARTSCOPE(isolate);
1647 if (codepoints == NULL && length != 0) { 1630 if (utf16_array == NULL && length != 0) {
1648 RETURN_NULL_ERROR(codepoints); 1631 RETURN_NULL_ERROR(utf16_array);
1649 } 1632 }
1650 CHECK_LENGTH(length, String::kMaxElements); 1633 CHECK_LENGTH(length, String::kMaxElements);
1651 return Api::NewHandle( 1634 return Api::NewHandle(isolate,
1652 isolate, String::NewExternal(codepoints, length, peer, callback)); 1635 String::NewExternal(utf16_array, length, peer, cback));
1653 } 1636 }
1654 1637
1655 1638
1656 DART_EXPORT Dart_Handle Dart_NewExternalString32(const uint32_t* codepoints, 1639 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object,
1657 intptr_t length, 1640 const char** cstr) {
1658 void* peer,
1659 Dart_PeerFinalizer callback) {
1660 Isolate* isolate = Isolate::Current(); 1641 Isolate* isolate = Isolate::Current();
1661 DARTSCOPE(isolate); 1642 DARTSCOPE(isolate);
1662 if (codepoints == NULL && length != 0) { 1643 const String& str_obj = Api::UnwrapStringHandle(isolate, object);
1663 RETURN_NULL_ERROR(codepoints); 1644 if (str_obj.IsNull()) {
1645 RETURN_TYPE_ERROR(isolate, object, String);
1664 } 1646 }
1665 CHECK_LENGTH(length, String::kMaxElements); 1647 intptr_t string_length = Utf8::Length(str_obj);
1666 return Api::NewHandle( 1648 char* res = Api::TopScope(isolate)->zone()->Alloc<char>(string_length + 1);
1667 isolate, String::NewExternal(codepoints, length, peer, callback)); 1649 if (res == NULL) {
1668 } 1650 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 } 1651 }
1680 intptr_t str_len = str_obj.Length(); 1652 const char* string_value = str_obj.ToCString();
1681 intptr_t copy_len = (str_len > *length) ? *length : str_len; 1653 memmove(res, string_value, string_length + 1);
1682 for (intptr_t i = 0; i < copy_len; i++) { 1654 ASSERT(res[string_length] == '\0');
1683 codepoints[i] = static_cast<uint8_t>(str_obj.CharAt(i)); 1655 *cstr = res;
1684 }
1685 *length= copy_len;
1686 return Api::Success(isolate); 1656 return Api::Success(isolate);
1687 } 1657 }
1688 1658
1689 1659
1690 DART_EXPORT Dart_Handle Dart_StringGet16(Dart_Handle str, 1660 DART_EXPORT Dart_Handle Dart_StringToUTF8(Dart_Handle str,
1691 uint16_t* codepoints, 1661 uint8_t* utf8_array,
1692 intptr_t* length) { 1662 intptr_t* length) {
1693 Isolate* isolate = Isolate::Current(); 1663 Isolate* isolate = Isolate::Current();
1694 DARTSCOPE(isolate); 1664 DARTSCOPE(isolate);
1695 const String& str_obj = Api::UnwrapStringHandle(isolate, str); 1665 const String& str_obj = Api::UnwrapStringHandle(isolate, str);
1696 if (str_obj.IsNull()) { 1666 if (str_obj.IsNull()) {
1697 RETURN_TYPE_ERROR(isolate, str, String); 1667 RETURN_TYPE_ERROR(isolate, str, String);
1698 } 1668 }
1699 if (str_obj.CharSize() > String::kTwoByteChar) { 1669 intptr_t str_len = str_obj.Length();
1700 return Api::NewError("Object is not a String16 or String8"); 1670 if (str_len > *length) {
1671 return Api::NewError("Input array is not large enough to hold the result");
1701 } 1672 }
1702 intptr_t str_len = str_obj.Length(); 1673 str_obj.ToUTF8(utf8_array, str_len);
1703 intptr_t copy_len = (str_len > *length) ? *length : str_len; 1674 *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); 1675 return Api::Success(isolate);
1709 } 1676 }
1710 1677
1711 1678
1712 DART_EXPORT Dart_Handle Dart_StringGet32(Dart_Handle str, 1679 DART_EXPORT Dart_Handle Dart_StringToUTF16(Dart_Handle str,
1713 uint32_t* codepoints, 1680 uint16_t* utf16_array,
1714 intptr_t* length) { 1681 intptr_t* length) {
1715 Isolate* isolate = Isolate::Current(); 1682 Isolate* isolate = Isolate::Current();
1716 DARTSCOPE(isolate); 1683 DARTSCOPE(isolate);
1717 const String& str_obj = Api::UnwrapStringHandle(isolate, str); 1684 const String& str_obj = Api::UnwrapStringHandle(isolate, str);
1718 if (str_obj.IsNull()) { 1685 if (str_obj.IsNull()) {
1719 RETURN_TYPE_ERROR(isolate, str, String); 1686 RETURN_TYPE_ERROR(isolate, str, String);
1720 } 1687 }
1721 intptr_t str_len = str_obj.Length(); 1688 intptr_t str_len = str_obj.Length();
1722 intptr_t copy_len = (str_len > *length) ? *length : str_len; 1689 intptr_t copy_len = (str_len > *length) ? *length : str_len;
1723 for (intptr_t i = 0; i < copy_len; i++) { 1690 for (intptr_t i = 0; i < copy_len; i++) {
1724 codepoints[i] = static_cast<uint32_t>(str_obj.CharAt(i)); 1691 utf16_array[i] = static_cast<uint16_t>(str_obj.CharAt(i));
1725 } 1692 }
1726 *length = copy_len; 1693 *length = copy_len;
1727 return Api::Success(isolate); 1694 return Api::Success(isolate);
1728 } 1695 }
1729 1696
1730 1697
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 --- 1698 // --- Lists ---
1780 1699
1781 1700
1782 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) { 1701 static RawInstance* GetListInstance(Isolate* isolate, const Object& obj) {
1783 if (obj.IsInstance()) { 1702 if (obj.IsInstance()) {
1784 const Instance& instance = Instance::Cast(obj); 1703 const Instance& instance = Instance::Cast(obj);
1785 const Type& type = 1704 const Type& type =
1786 Type::Handle(isolate, isolate->object_store()->list_interface()); 1705 Type::Handle(isolate, isolate->object_store()->list_interface());
1787 Error& malformed_type_error = Error::Handle(isolate); 1706 Error& malformed_type_error = Error::Handle(isolate);
1788 if (instance.IsInstanceOf(type, 1707 if (instance.IsInstanceOf(type,
(...skipping 2748 matching lines...) Expand 10 before | Expand all | Expand 10 after
4537 } 4456 }
4538 { 4457 {
4539 NoGCScope no_gc; 4458 NoGCScope no_gc;
4540 RawObject* raw_obj = obj.raw(); 4459 RawObject* raw_obj = obj.raw();
4541 isolate->heap()->SetPeer(raw_obj, peer); 4460 isolate->heap()->SetPeer(raw_obj, peer);
4542 } 4461 }
4543 return Api::Success(isolate); 4462 return Api::Success(isolate);
4544 } 4463 }
4545 4464
4546 } // namespace dart 4465 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698