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

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

Powered by Google App Engine
This is Rietveld 408576698