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

Side by Side Diff: vm/dart_api_impl.cc

Issue 10411038: Fix some dart api functions to use the proper unwrap patterns. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 7 months 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 | « no previous file | 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 1330 matching lines...) Expand 10 before | Expand all | Expand 10 after
1341 1341
1342 1342
1343 DART_EXPORT bool Dart_IsString16(Dart_Handle object) { 1343 DART_EXPORT bool Dart_IsString16(Dart_Handle object) {
1344 return RawObject::IsTwoByteStringClassIndex(Api::ClassIndex(object)); 1344 return RawObject::IsTwoByteStringClassIndex(Api::ClassIndex(object));
1345 } 1345 }
1346 1346
1347 1347
1348 DART_EXPORT Dart_Handle Dart_StringLength(Dart_Handle str, intptr_t* len) { 1348 DART_EXPORT Dart_Handle Dart_StringLength(Dart_Handle str, intptr_t* len) {
1349 Isolate* isolate = Isolate::Current(); 1349 Isolate* isolate = Isolate::Current();
1350 DARTSCOPE(isolate); 1350 DARTSCOPE(isolate);
1351 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(str)); 1351 const String& str_obj = Api::UnwrapStringHandle(isolate, str);
1352 if (obj.IsString()) { 1352 if (str_obj.IsNull()) {
1353 String& string_obj = String::Handle(isolate); 1353 RETURN_TYPE_ERROR(isolate, str, String);
1354 string_obj ^= obj.raw();
1355 *len = string_obj.Length();
1356 return Api::Success(isolate);
1357 } 1354 }
1358 return Api::NewError("Object is not a String"); 1355 *len = str_obj.Length();
1356 return Api::Success(isolate);
1359 } 1357 }
1360 1358
1361 1359
1362 DART_EXPORT Dart_Handle Dart_NewString(const char* str) { 1360 DART_EXPORT Dart_Handle Dart_NewString(const char* str) {
1363 Isolate* isolate = Isolate::Current(); 1361 Isolate* isolate = Isolate::Current();
1364 DARTSCOPE(isolate); 1362 DARTSCOPE(isolate);
1365 return Api::NewHandle(isolate, String::New(str)); 1363 return Api::NewHandle(isolate, String::New(str));
1366 } 1364 }
1367 1365
1368 1366
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
1472 return Api::NewHandle( 1470 return Api::NewHandle(
1473 isolate, String::NewExternal(codepoints, length, peer, callback)); 1471 isolate, String::NewExternal(codepoints, length, peer, callback));
1474 } 1472 }
1475 1473
1476 1474
1477 DART_EXPORT Dart_Handle Dart_StringGet8(Dart_Handle str, 1475 DART_EXPORT Dart_Handle Dart_StringGet8(Dart_Handle str,
1478 uint8_t* codepoints, 1476 uint8_t* codepoints,
1479 intptr_t* length) { 1477 intptr_t* length) {
1480 Isolate* isolate = Isolate::Current(); 1478 Isolate* isolate = Isolate::Current();
1481 DARTSCOPE(isolate); 1479 DARTSCOPE(isolate);
1482 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(str)); 1480 const OneByteString& str_obj = Api::UnwrapOneByteStringHandle(isolate, str);
1483 if (obj.IsString()) { 1481 if (str_obj.IsNull()) {
1484 String& string_obj = String::Handle(isolate); 1482 RETURN_TYPE_ERROR(isolate, str, String8);
1485 string_obj ^= obj.raw();
1486 if (string_obj.CharSize() == String::kOneByteChar) {
1487 intptr_t str_len = string_obj.Length();
1488 intptr_t copy_len = (str_len > *length) ? *length : str_len;
1489 for (intptr_t i = 0; i < copy_len; i++) {
1490 codepoints[i] = static_cast<uint8_t>(string_obj.CharAt(i));
1491 }
1492 *length= copy_len;
1493 return Api::Success(isolate);
1494 }
1495 } 1483 }
1496 return Api::NewError(obj.IsString() 1484 intptr_t str_len = str_obj.Length();
1497 ? "Object is not a String8" 1485 intptr_t copy_len = (str_len > *length) ? *length : str_len;
1498 : "Object is not a String"); 1486 for (intptr_t i = 0; i < copy_len; i++) {
1487 codepoints[i] = static_cast<uint8_t>(str_obj.CharAt(i));
1488 }
1489 *length= copy_len;
1490 return Api::Success(isolate);
1499 } 1491 }
1500 1492
1501 1493
1502 DART_EXPORT Dart_Handle Dart_StringGet16(Dart_Handle str, 1494 DART_EXPORT Dart_Handle Dart_StringGet16(Dart_Handle str,
1503 uint16_t* codepoints, 1495 uint16_t* codepoints,
1504 intptr_t* length) { 1496 intptr_t* length) {
1505 Isolate* isolate = Isolate::Current(); 1497 Isolate* isolate = Isolate::Current();
1506 DARTSCOPE(isolate); 1498 DARTSCOPE(isolate);
1507 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(str)); 1499 const String& str_obj = Api::UnwrapStringHandle(isolate, str);
1508 if (obj.IsString()) { 1500 if (str_obj.IsNull()) {
1509 String& string_obj = String::Handle(isolate); 1501 RETURN_TYPE_ERROR(isolate, str, String);
1510 string_obj ^= obj.raw();
1511 if (string_obj.CharSize() <= String::kTwoByteChar) {
1512 intptr_t str_len = string_obj.Length();
1513 intptr_t copy_len = (str_len > *length) ? *length : str_len;
1514 for (intptr_t i = 0; i < copy_len; i++) {
1515 codepoints[i] = static_cast<uint16_t>(string_obj.CharAt(i));
1516 }
1517 *length = copy_len;
1518 return Api::Success(isolate);
1519 }
1520 } 1502 }
1521 return Api::NewError(obj.IsString() 1503 if (str_obj.CharSize() > String::kTwoByteChar) {
1522 ? "Object is not a String16" 1504 return Api::NewError("Object is not a String16 or String8");
1523 : "Object is not a String"); 1505 }
1506 intptr_t str_len = str_obj.Length();
1507 intptr_t copy_len = (str_len > *length) ? *length : str_len;
1508 for (intptr_t i = 0; i < copy_len; i++) {
1509 codepoints[i] = static_cast<uint16_t>(str_obj.CharAt(i));
1510 }
1511 *length = copy_len;
1512 return Api::Success(isolate);
1524 } 1513 }
1525 1514
1526 1515
1527 DART_EXPORT Dart_Handle Dart_StringGet32(Dart_Handle str, 1516 DART_EXPORT Dart_Handle Dart_StringGet32(Dart_Handle str,
1528 uint32_t* codepoints, 1517 uint32_t* codepoints,
1529 intptr_t* length) { 1518 intptr_t* length) {
1530 Isolate* isolate = Isolate::Current(); 1519 Isolate* isolate = Isolate::Current();
1531 DARTSCOPE(isolate); 1520 DARTSCOPE(isolate);
1532 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(str)); 1521 const String& str_obj = Api::UnwrapStringHandle(isolate, str);
1533 if (obj.IsString()) { 1522 if (str_obj.IsNull()) {
1534 String& string_obj = String::Handle(isolate); 1523 RETURN_TYPE_ERROR(isolate, str, String);
1535 string_obj ^= obj.raw();
1536 intptr_t str_len = string_obj.Length();
1537 intptr_t copy_len = (str_len > *length) ? *length : str_len;
1538 for (intptr_t i = 0; i < copy_len; i++) {
1539 codepoints[i] = static_cast<uint32_t>(string_obj.CharAt(i));
1540 }
1541 *length = copy_len;
1542 return Api::Success(isolate);
1543 } 1524 }
1544 return Api::NewError("Object is not a String"); 1525 intptr_t str_len = str_obj.Length();
1526 intptr_t copy_len = (str_len > *length) ? *length : str_len;
1527 for (intptr_t i = 0; i < copy_len; i++) {
1528 codepoints[i] = static_cast<uint32_t>(str_obj.CharAt(i));
1529 }
1530 *length = copy_len;
1531 return Api::Success(isolate);
1545 } 1532 }
1546 1533
1547 1534
1548 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object, 1535 DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object,
1549 const char** result) { 1536 const char** result) {
1550 Isolate* isolate = Isolate::Current(); 1537 Isolate* isolate = Isolate::Current();
1551 DARTSCOPE(isolate); 1538 DARTSCOPE(isolate);
1552 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object)); 1539 const String& str_obj = Api::UnwrapStringHandle(isolate, object);
1553 if (obj.IsString()) { 1540 if (str_obj.IsNull()) {
1554 const char* string_value = obj.ToCString(); 1541 RETURN_TYPE_ERROR(isolate, object, String);
1555 intptr_t string_length = strlen(string_value);
1556 char* res =
1557 reinterpret_cast<char*>(Api::Allocate(isolate, string_length + 1));
1558 if (res == NULL) {
1559 return Api::NewError("Unable to allocate memory");
1560 }
1561 strncpy(res, string_value, string_length + 1);
1562 ASSERT(res[string_length] == '\0');
1563 *result = res;
1564 return Api::Success(isolate);
1565 } 1542 }
1566 return Api::NewError("Object is not a String"); 1543 const char* string_value = str_obj.ToCString();
1544 intptr_t string_length = strlen(string_value);
cshapiro 2012/05/19 01:57:00 Maybe call Utf8::Length here instead?
siva 2012/05/21 21:41:27 Done.
1545 char* res =
1546 reinterpret_cast<char*>(Api::Allocate(isolate, string_length + 1));
1547 if (res == NULL) {
1548 return Api::NewError("Unable to allocate memory");
1549 }
1550 strncpy(res, string_value, string_length + 1);
cshapiro 2012/05/19 01:57:00 Maybe use memmove instead of strncpy?
siva 2012/05/21 21:41:27 Done.
1551 ASSERT(res[string_length] == '\0');
1552 *result = res;
1553 return Api::Success(isolate);
1567 } 1554 }
1568 1555
1569 1556
1570 DART_EXPORT Dart_Handle Dart_StringToBytes(Dart_Handle object, 1557 DART_EXPORT Dart_Handle Dart_StringToBytes(Dart_Handle object,
1571 const uint8_t** bytes, 1558 const uint8_t** bytes,
1572 intptr_t *length) { 1559 intptr_t *length) {
1573 Isolate* isolate = Isolate::Current(); 1560 Isolate* isolate = Isolate::Current();
1574 DARTSCOPE(isolate); 1561 DARTSCOPE(isolate);
1575 const String& str = Api::UnwrapStringHandle(isolate, object); 1562 const String& str = Api::UnwrapStringHandle(isolate, object);
1576 if (str.IsNull()) { 1563 if (str.IsNull()) {
(...skipping 1248 matching lines...) Expand 10 before | Expand all | Expand 10 after
2825 } 2812 }
2826 return Api::NewHandle(isolate, cls.raw()); 2813 return Api::NewHandle(isolate, cls.raw());
2827 } 2814 }
2828 2815
2829 2816
2830 DART_EXPORT Dart_Handle Dart_GetNativeInstanceField(Dart_Handle obj, 2817 DART_EXPORT Dart_Handle Dart_GetNativeInstanceField(Dart_Handle obj,
2831 int index, 2818 int index,
2832 intptr_t* value) { 2819 intptr_t* value) {
2833 Isolate* isolate = Isolate::Current(); 2820 Isolate* isolate = Isolate::Current();
2834 DARTSCOPE(isolate); 2821 DARTSCOPE(isolate);
2835 const Object& param = Object::Handle(isolate, Api::UnwrapHandle(obj)); 2822 const Instance& object = Api::UnwrapInstanceHandle(isolate, obj);
2836 if (param.IsNull() || !param.IsInstance()) { 2823 if (object.IsNull()) {
2837 return Api::NewError( 2824 RETURN_TYPE_ERROR(isolate, obj, Instance);
2838 "Invalid object passed in to access native instance field");
2839 } 2825 }
2840 Instance& object = Instance::Handle(isolate);
2841 object ^= param.raw();
2842 if (!object.IsValidNativeIndex(index)) { 2826 if (!object.IsValidNativeIndex(index)) {
2843 return Api::NewError( 2827 return Api::NewError(
2844 "Invalid index passed in to access native instance field"); 2828 "Invalid index passed in to access native instance field");
2845 } 2829 }
2846 *value = object.GetNativeField(index); 2830 *value = object.GetNativeField(index);
2847 return Api::Success(isolate); 2831 return Api::Success(isolate);
2848 } 2832 }
2849 2833
2850 2834
2851 DART_EXPORT Dart_Handle Dart_SetNativeInstanceField(Dart_Handle obj, 2835 DART_EXPORT Dart_Handle Dart_SetNativeInstanceField(Dart_Handle obj,
2852 int index, 2836 int index,
2853 intptr_t value) { 2837 intptr_t value) {
2854 Isolate* isolate = Isolate::Current(); 2838 Isolate* isolate = Isolate::Current();
2855 DARTSCOPE(isolate); 2839 DARTSCOPE(isolate);
2856 const Object& param = Object::Handle(isolate, Api::UnwrapHandle(obj)); 2840 const Instance& object = Api::UnwrapInstanceHandle(isolate, obj);
2857 if (param.IsNull() || !param.IsInstance()) { 2841 if (object.IsNull()) {
2858 return Api::NewError( 2842 RETURN_TYPE_ERROR(isolate, obj, Instance);
2859 "Invalid object passed in to set native instance field");
2860 } 2843 }
2861 Instance& object = Instance::Handle(isolate);
2862 object ^= param.raw();
2863 if (!object.IsValidNativeIndex(index)) { 2844 if (!object.IsValidNativeIndex(index)) {
2864 return Api::NewError( 2845 return Api::NewError(
2865 "Invalid index passed in to set native instance field"); 2846 "Invalid index passed in to set native instance field");
2866 } 2847 }
2867 object.SetNativeField(index, value); 2848 object.SetNativeField(index, value);
2868 return Api::Success(isolate); 2849 return Api::Success(isolate);
2869 } 2850 }
2870 2851
2871 2852
2872 // --- Exceptions ---- 2853 // --- Exceptions ----
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after
3283 *buffer = NULL; 3264 *buffer = NULL;
3284 } 3265 }
3285 delete debug_region; 3266 delete debug_region;
3286 } else { 3267 } else {
3287 *buffer = NULL; 3268 *buffer = NULL;
3288 *buffer_size = 0; 3269 *buffer_size = 0;
3289 } 3270 }
3290 } 3271 }
3291 3272
3292 } // namespace dart 3273 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | vm/dart_api_impl_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698