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

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

Issue 9368049: Add external byte array API and finalize external strings and byte arrays. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fix simarm and other minor changes Created 8 years, 10 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
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 #include "platform/assert.h" 6 #include "platform/assert.h"
7 #include "platform/utils.h" 7 #include "platform/utils.h"
8 #include "vm/dart_api_impl.h" 8 #include "vm/dart_api_impl.h"
9 #include "vm/dart_api_state.h" 9 #include "vm/dart_api_state.h"
10 #include "vm/thread.h" 10 #include "vm/thread.h"
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 EXPECT(Dart_IsError(result)); 550 EXPECT(Dart_IsError(result));
551 EXPECT_STREQ("Dart_ExternalStringGetPeer expects argument 'object' to be " 551 EXPECT_STREQ("Dart_ExternalStringGetPeer expects argument 'object' to be "
552 "of type String.", Dart_GetError(result)); 552 "of type String.", Dart_GetError(result));
553 EXPECT(peer == NULL); 553 EXPECT(peer == NULL);
554 } 554 }
555 555
556 556
557 // Only ia32 and x64 can run execution tests. 557 // Only ia32 and x64 can run execution tests.
558 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) 558 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64)
559 559
560 static void ExternalStringCallbackFinalizer(Dart_Handle handle, void* peer) {
561 Dart_DeletePersistentHandle(handle);
562 *static_cast<int*>(peer) *= 2;
563 }
564
565
566 TEST_CASE(ExternalStringCallback) {
567 int peer8 = 40;
568 int peer16 = 41;
569 int peer32 = 42;
570
571 {
572 Dart_EnterScope();
573
574 uint8_t data8[] = { 'h', 'e', 'l', 'l', 'o' };
575 Dart_Handle obj8 = Dart_NewExternalString8(
576 data8,
577 ARRAY_SIZE(data8),
578 &peer8,
579 ExternalStringCallbackFinalizer);
580 EXPECT_VALID(obj8);
581 void* api_peer8 = NULL;
582 EXPECT_VALID(Dart_ExternalStringGetPeer(obj8, &api_peer8));
583 EXPECT_EQ(api_peer8, &peer8);
584
585 uint16_t data16[] = { 'h', 'e', 'l', 'l', 'o' };
586 Dart_Handle obj16 = Dart_NewExternalString16(
587 data16,
588 ARRAY_SIZE(data16),
589 &peer16,
590 ExternalStringCallbackFinalizer);
591 EXPECT_VALID(obj16);
592 void* api_peer16 = NULL;
593 EXPECT_VALID(Dart_ExternalStringGetPeer(obj16, &api_peer16));
594 EXPECT_EQ(api_peer16, &peer16);
595
596 uint32_t data32[] = { 'h', 'e', 'l', 'l', 'o' };
597 Dart_Handle obj32 = Dart_NewExternalString32(
598 data32,
599 ARRAY_SIZE(data32),
600 &peer32,
601 ExternalStringCallbackFinalizer);
602 EXPECT_VALID(obj32);
603 void* api_peer32 = NULL;
604 EXPECT_VALID(Dart_ExternalStringGetPeer(obj32, &api_peer32));
605 EXPECT_EQ(api_peer32, &peer32);
606
607 Dart_ExitScope();
608 }
609
610 EXPECT_EQ(peer8, 40);
611 EXPECT_EQ(peer16, 41);
612 EXPECT_EQ(peer32, 42);
613 Isolate::Current()->heap()->CollectGarbage(Heap::kOld);
614 EXPECT_EQ(peer8, 40);
615 EXPECT_EQ(peer16, 41);
616 EXPECT_EQ(peer32, 42);
617 Isolate::Current()->heap()->CollectGarbage(Heap::kNew);
618 EXPECT_EQ(peer8, 80);
619 EXPECT_EQ(peer16, 82);
620 EXPECT_EQ(peer32, 84);
621 }
622
623
560 TEST_CASE(ListAccess) { 624 TEST_CASE(ListAccess) {
561 const char* kScriptChars = 625 const char* kScriptChars =
562 "class ListAccessTest {" 626 "class ListAccessTest {"
563 " ListAccessTest() {}" 627 " ListAccessTest() {}"
564 " static List testMain() {" 628 " static List testMain() {"
565 " List a = new List();" 629 " List a = new List();"
566 " a.add(10);" 630 " a.add(10);"
567 " a.add(20);" 631 " a.add(20);"
568 " a.add(30);" 632 " a.add(30);"
569 " return a;" 633 " return a;"
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 result = Dart_IntegerToInt64(result, &value); 732 result = Dart_IntegerToInt64(result, &value);
669 EXPECT_VALID(result); 733 EXPECT_VALID(result);
670 EXPECT_EQ(30, value); 734 EXPECT_EQ(30, value);
671 735
672 // Check if we get an exception when accessing beyond limit. 736 // Check if we get an exception when accessing beyond limit.
673 result = Dart_ListGetAt(ListAccessTestObj, 4); 737 result = Dart_ListGetAt(ListAccessTestObj, 4);
674 EXPECT(Dart_IsError(result)); 738 EXPECT(Dart_IsError(result));
675 } 739 }
676 740
677 741
678 TEST_CASE(ByteArray) { 742 TEST_CASE(ByteArrayAccess) {
679 Dart_Handle byte_array1 = Dart_NewByteArray(10); 743 Dart_Handle byte_array1 = Dart_NewByteArray(10);
680 EXPECT_VALID(byte_array1); 744 EXPECT_VALID(byte_array1);
681 EXPECT(Dart_IsByteArray(byte_array1)); 745 EXPECT(Dart_IsByteArray(byte_array1));
682 EXPECT(Dart_IsList(byte_array1)); 746 EXPECT(Dart_IsList(byte_array1));
683 747
684 intptr_t length = 0; 748 intptr_t length = 0;
685 Dart_Handle result = Dart_ListLength(byte_array1, &length); 749 Dart_Handle result = Dart_ListLength(byte_array1, &length);
686 EXPECT_VALID(result); 750 EXPECT_VALID(result);
687 EXPECT_EQ(10, length); 751 EXPECT_EQ(10, length);
688 752
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 for (intptr_t i = 0; i < 10; ++i) { 808 for (intptr_t i = 0; i < 10; ++i) {
745 Dart_Handle e = Dart_ListGetAt(byte_array1, i); 809 Dart_Handle e = Dart_ListGetAt(byte_array1, i);
746 EXPECT_VALID(e); 810 EXPECT_VALID(e);
747 int64_t value; 811 int64_t value;
748 result = Dart_IntegerToInt64(e, &value); 812 result = Dart_IntegerToInt64(e, &value);
749 EXPECT_VALID(result); 813 EXPECT_VALID(result);
750 EXPECT_EQ(10 - i, value); 814 EXPECT_EQ(10 - i, value);
751 } 815 }
752 } 816 }
753 817
818
819 TEST_CASE(ExternalByteArrayAccess) {
820 uint8_t data[] = { 0, 11, 22, 33, 44, 55, 66, 77 };
821 intptr_t data_length = ARRAY_SIZE(data);
822
823 Dart_Handle obj = Dart_NewExternalByteArray(data, data_length, NULL, NULL);
824 EXPECT_VALID(obj);
825 EXPECT(Dart_IsByteArray(obj));
826 EXPECT(Dart_IsList(obj));
827
828 void* peer = &data; // just a non-NULL value
829 EXPECT_VALID(Dart_ExternalByteArrayGetPeer(obj, &peer));
830 EXPECT(peer == NULL);
831
832 intptr_t list_length = 0;
833 EXPECT_VALID(Dart_ListLength(obj, &list_length));
834 EXPECT_EQ(data_length, list_length);
835
836 // Load and check values from underlying array and API.
837 for (intptr_t i = 0; i < list_length; ++i) {
838 EXPECT_EQ(11 * i, data[i]);
839 Dart_Handle elt = Dart_ListGetAt(obj, i);
840 EXPECT_VALID(elt);
841 int64_t value = 0;
842 EXPECT_VALID(Dart_IntegerToInt64(elt, &value));
843 EXPECT_EQ(data[i], value);
844 }
845
846 // Write values through the underlying array.
847 for (intptr_t i = 0; i < data_length; ++i) {
848 data[i] *= 2;
849 }
850 // Read them back through the API.
851 for (intptr_t i = 0; i < list_length; ++i) {
852 Dart_Handle elt = Dart_ListGetAt(obj, i);
853 EXPECT_VALID(elt);
854 int64_t value = 0;
855 EXPECT_VALID(Dart_IntegerToInt64(elt, &value));
856 EXPECT_EQ(22 * i, value);
857 }
858
859 // Write values through the API.
860 for (intptr_t i = 0; i < list_length; ++i) {
861 Dart_Handle value = Dart_NewInteger(33 * i);
862 EXPECT_VALID(value);
863 EXPECT_VALID(Dart_ListSetAt(obj, i, value));
864 }
865 // Read them back through the underlying array.
866 for (intptr_t i = 0; i < data_length; ++i) {
867 EXPECT_EQ(33 * i, data[i]);
868 }
869 }
870
871
872 static void ExternalByteArrayCallbackFinalizer(Dart_Handle handle, void* peer) {
873 Dart_DeletePersistentHandle(handle);
874 *static_cast<int*>(peer) = 42;
875 }
876
877
878 TEST_CASE(ExternalByteArrayCallback) {
879 int peer = 0;
880 {
881 Dart_EnterScope();
882 uint8_t data[] = { 1, 2, 3, 4 };
883 Dart_Handle obj = Dart_NewExternalByteArray(
884 data,
885 ARRAY_SIZE(data),
886 &peer,
887 ExternalByteArrayCallbackFinalizer);
888 EXPECT_VALID(obj);
889 void* api_peer = NULL;
890 EXPECT_VALID(Dart_ExternalByteArrayGetPeer(obj, &api_peer));
891 EXPECT_EQ(api_peer, &peer);
892 Dart_ExitScope();
893 }
894 EXPECT(peer == 0);
895 Isolate::Current()->heap()->CollectGarbage(Heap::kOld);
896 EXPECT(peer == 0);
897 Isolate::Current()->heap()->CollectGarbage(Heap::kNew);
898 EXPECT(peer == 42);
899 }
900
754 #endif 901 #endif
755 902
756 903
757 // Unit test for entering a scope, creating a local handle and exiting 904 // Unit test for entering a scope, creating a local handle and exiting
758 // the scope. 905 // the scope.
759 UNIT_TEST_CASE(EnterExitScope) { 906 UNIT_TEST_CASE(EnterExitScope) {
760 TestIsolateScope __test_isolate__; 907 TestIsolateScope __test_isolate__;
761 908
762 Isolate* isolate = Isolate::Current(); 909 Isolate* isolate = Isolate::Current();
763 EXPECT(isolate != NULL); 910 EXPECT(isolate != NULL);
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
974 EXPECT_VALID(weak_new_ref); 1121 EXPECT_VALID(weak_new_ref);
975 EXPECT(Dart_IsNull(weak_new_ref)); 1122 EXPECT(Dart_IsNull(weak_new_ref));
976 EXPECT_VALID(weak_old_ref); 1123 EXPECT_VALID(weak_old_ref);
977 EXPECT(Dart_IsNull(weak_old_ref)); 1124 EXPECT(Dart_IsNull(weak_old_ref));
978 1125
979 Dart_DeletePersistentHandle(weak_new_ref); 1126 Dart_DeletePersistentHandle(weak_new_ref);
980 Dart_DeletePersistentHandle(weak_old_ref); 1127 Dart_DeletePersistentHandle(weak_old_ref);
981 } 1128 }
982 1129
983 1130
984 static void WeakPersistentHandlePeerFinalizer(void* peer) { 1131 static void WeakPersistentHandlePeerFinalizer(Dart_Handle handle, void* peer) {
985 *static_cast<int*>(peer) = 42; 1132 *static_cast<int*>(peer) = 42;
986 } 1133 }
987 1134
988 1135
989 TEST_CASE(WeakPersistentHandleCallback) { 1136 TEST_CASE(WeakPersistentHandleCallback) {
990 Dart_Handle weak_ref = Dart_Null(); 1137 Dart_Handle weak_ref = Dart_Null();
991 EXPECT(Dart_IsNull(weak_ref)); 1138 EXPECT(Dart_IsNull(weak_ref));
992 int* peer = new int(); 1139 int* peer = new int();
993 { 1140 {
994 Dart_EnterScope(); 1141 Dart_EnterScope();
(...skipping 2229 matching lines...) Expand 10 before | Expand all | Expand 10 after
3224 // We should have received the expected number of interrupts. 3371 // We should have received the expected number of interrupts.
3225 EXPECT_EQ(kInterruptCount, interrupt_count); 3372 EXPECT_EQ(kInterruptCount, interrupt_count);
3226 3373
3227 // Give the spawned thread enough time to properly exit. 3374 // Give the spawned thread enough time to properly exit.
3228 Isolate::SetInterruptCallback(saved); 3375 Isolate::SetInterruptCallback(saved);
3229 } 3376 }
3230 3377
3231 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 3378 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
3232 3379
3233 } // namespace dart 3380 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698