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

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

Issue 12937010: Handle TypedDataView objects in Dart_TypedDataAcquireData and (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 9 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/json.h" 7 #include "platform/json.h"
8 #include "platform/utils.h" 8 #include "platform/utils.h"
9 #include "vm/class_finalizer.h" 9 #include "vm/class_finalizer.h"
10 #include "vm/dart_api_impl.h" 10 #include "vm/dart_api_impl.h"
(...skipping 903 matching lines...) Expand 10 before | Expand all | Expand 10 after
914 " to be non-null."); 914 " to be non-null.");
915 result = Dart_TypedDataReleaseData(str); 915 result = Dart_TypedDataReleaseData(str);
916 EXPECT_ERROR(result, "Dart_TypedDataReleaseData expects argument 'object'" 916 EXPECT_ERROR(result, "Dart_TypedDataReleaseData expects argument 'object'"
917 " to be of type 'TypedData'."); 917 " to be of type 'TypedData'.");
918 } 918 }
919 919
920 920
921 static void TestDirectAccess(Dart_Handle lib, 921 static void TestDirectAccess(Dart_Handle lib,
922 Dart_Handle array, 922 Dart_Handle array,
923 Dart_TypedData_Type expected_type) { 923 Dart_TypedData_Type expected_type) {
924 Dart_Handle result;
925
924 // Invoke the dart function that sets initial values. 926 // Invoke the dart function that sets initial values.
925 Dart_Handle dart_args[1]; 927 Dart_Handle dart_args[1];
926 dart_args[0] = array; 928 dart_args[0] = array;
927 Dart_Invoke(lib, NewString("setMain"), 1, dart_args); 929 result = Dart_Invoke(lib, NewString("setMain"), 1, dart_args);
930 EXPECT_VALID(result);
928 931
929 // Now Get a direct access to this typed data object and check it's contents. 932 // Now Get a direct access to this typed data object and check it's contents.
930 const int kLength = 10; 933 const int kLength = 10;
931 Dart_Handle result;
932 Dart_TypedData_Type type; 934 Dart_TypedData_Type type;
933 void* data; 935 void* data;
934 intptr_t len; 936 intptr_t len;
935 result = Dart_TypedDataAcquireData(array, &type, &data, &len); 937 result = Dart_TypedDataAcquireData(array, &type, &data, &len);
936 EXPECT_VALID(result); 938 EXPECT_VALID(result);
937 EXPECT_EQ(expected_type, type); 939 EXPECT_EQ(expected_type, type);
938 EXPECT_EQ(kLength, len); 940 EXPECT_EQ(kLength, len);
939 int8_t* dataP = reinterpret_cast<int8_t*>(data); 941 int8_t* dataP = reinterpret_cast<int8_t*>(data);
940 for (int i = 0; i < kLength; i++) { 942 for (int i = 0; i < kLength; i++) {
941 EXPECT_EQ(i, dataP[i]); 943 EXPECT_EQ(i, dataP[i]);
942 } 944 }
943 945
944 // Now modify the values in the directly accessible array and then check 946 // Now modify the values in the directly accessible array and then check
945 // it we see the changes back in dart. 947 // it we see the changes back in dart.
946 for (int i = 0; i < kLength; i++) { 948 for (int i = 0; i < kLength; i++) {
947 dataP[i] += 10; 949 dataP[i] += 10;
948 } 950 }
949 951
950 // Release direct accesss to the typed data object. 952 // Release direct accesss to the typed data object.
951 result = Dart_TypedDataReleaseData(array); 953 result = Dart_TypedDataReleaseData(array);
952 EXPECT_VALID(result); 954 EXPECT_VALID(result);
953 955
954 // Invoke the dart function in order to check the modified values. 956 // Invoke the dart function in order to check the modified values.
955 Dart_Invoke(lib, NewString("testMain"), 1, dart_args); 957 result = Dart_Invoke(lib, NewString("testMain"), 1, dart_args);
958 EXPECT_VALID(result);
956 } 959 }
957 960
958 961
959 TEST_CASE(TypedDataDirectAccess1) { 962 TEST_CASE(TypedDataDirectAccess1) {
960 const char* kScriptChars = 963 const char* kScriptChars =
961 "import 'dart:typeddata';\n" 964 "import 'dart:typeddata';\n"
962 "void setMain(var a) {" 965 "void setMain(var a) {"
963 " for (var i = 0; i < 10; i++) {" 966 " for (var i = 0; i < 10; i++) {"
964 " a[i] = i;" 967 " a[i] = i;"
965 " }" 968 " }"
966 "}\n" 969 "}\n"
967 "void testMain(var list) {" 970 "bool testMain(var list) {"
968 " for (var i = 0; i < 10; i++) {" 971 " for (var i = 0; i < 10; i++) {"
969 " Expect.equals((10 + i), list[i]);" 972 " Expect.equals((10 + i), list[i]);"
970 " }\n" 973 " }\n"
974 " return true;"
971 "}\n" 975 "}\n"
972 "List main() {" 976 "List main() {"
973 " var a = new Int8List(10);" 977 " var a = new Int8List(10);"
974 " return a;" 978 " return a;"
975 "}\n"; 979 "}\n";
976 // Create a test library and Load up a test script in it. 980 // Create a test library and Load up a test script in it.
977 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 981 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
978 982
979 // Test with an regular typed data object. 983 // Test with an regular typed data object.
980 Dart_Handle list_access_test_obj; 984 Dart_Handle list_access_test_obj;
981 list_access_test_obj = Dart_Invoke(lib, NewString("main"), 0, NULL); 985 list_access_test_obj = Dart_Invoke(lib, NewString("main"), 0, NULL);
982 EXPECT_VALID(list_access_test_obj); 986 EXPECT_VALID(list_access_test_obj);
983 TestDirectAccess(lib, list_access_test_obj, kInt8); 987 TestDirectAccess(lib, list_access_test_obj, kInt8);
984 988
985 // Test with an external typed data object. 989 // Test with an external typed data object.
986 uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 990 uint8_t data[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
987 intptr_t data_length = ARRAY_SIZE(data); 991 intptr_t data_length = ARRAY_SIZE(data);
988 Dart_Handle ext_list_access_test_obj; 992 Dart_Handle ext_list_access_test_obj;
989 ext_list_access_test_obj = Dart_NewExternalTypedData(kUint8, 993 ext_list_access_test_obj = Dart_NewExternalTypedData(kUint8,
990 data, 994 data,
991 data_length, 995 data_length,
992 NULL, NULL); 996 NULL, NULL);
993 EXPECT_VALID(ext_list_access_test_obj); 997 EXPECT_VALID(ext_list_access_test_obj);
994 TestDirectAccess(lib, ext_list_access_test_obj, kUint8); 998 TestDirectAccess(lib, ext_list_access_test_obj, kUint8);
995 } 999 }
996 1000
997 1001
1002 TEST_CASE(TypedDataViewDirectAccess) {
1003 const char* kScriptChars =
1004 "import 'dart:typeddata';\n"
1005 "void setMain(var list) {"
1006 " Expect.equals(10, list.length);"
1007 " for (var i = 0; i < 10; i++) {"
1008 " list[i] = i;"
1009 " }"
1010 "}\n"
1011 "bool testMain(var list) {"
1012 " Expect.equals(10, list.length);"
1013 " for (var i = 0; i < 10; i++) {"
1014 " Expect.equals((10 + i), list[i]);"
1015 " }"
1016 " return true;"
1017 "}\n"
1018 "List main() {"
1019 " var a = new Int8List(100);"
1020 " var view = new Int8List.view(a.buffer, 50, 10);"
1021 " return view;"
1022 "}\n";
1023 // Create a test library and Load up a test script in it.
1024 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1025
1026 // Test with a typed data view object.
1027 Dart_Handle list_access_test_obj;
1028 list_access_test_obj = Dart_Invoke(lib, NewString("main"), 0, NULL);
1029 EXPECT_VALID(list_access_test_obj);
1030 TestDirectAccess(lib, list_access_test_obj, kInt8);
1031 }
1032
1033
998 static void ExternalTypedDataAccessTests(Dart_Handle obj, 1034 static void ExternalTypedDataAccessTests(Dart_Handle obj,
999 Dart_TypedData_Type expected_type, 1035 Dart_TypedData_Type expected_type,
1000 uint8_t data[], 1036 uint8_t data[],
1001 intptr_t data_length) { 1037 intptr_t data_length) {
1002 EXPECT_VALID(obj); 1038 EXPECT_VALID(obj);
1003 EXPECT_EQ(expected_type, Dart_GetTypeOfExternalTypedData(obj)); 1039 EXPECT_EQ(expected_type, Dart_GetTypeOfExternalTypedData(obj));
1004 EXPECT(Dart_IsList(obj)); 1040 EXPECT(Dart_IsList(obj));
1005 1041
1006 void* raw_data = NULL; 1042 void* raw_data = NULL;
1007 intptr_t len; 1043 intptr_t len;
(...skipping 6421 matching lines...) Expand 10 before | Expand all | Expand 10 after
7429 NULL); 7465 NULL);
7430 int64_t value = 0; 7466 int64_t value = 0;
7431 result = Dart_IntegerToInt64(result, &value); 7467 result = Dart_IntegerToInt64(result, &value);
7432 EXPECT_VALID(result); 7468 EXPECT_VALID(result);
7433 EXPECT_EQ(260, value); 7469 EXPECT_EQ(260, value);
7434 } 7470 }
7435 7471
7436 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 7472 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
7437 7473
7438 } // namespace dart 7474 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698