Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 867 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 878 for (intptr_t i = 0; i < 10; ++i) { | 878 for (intptr_t i = 0; i < 10; ++i) { |
| 879 Dart_Handle integer_obj = Dart_ListGetAt(byte_array1, i); | 879 Dart_Handle integer_obj = Dart_ListGetAt(byte_array1, i); |
| 880 EXPECT_VALID(integer_obj); | 880 EXPECT_VALID(integer_obj); |
| 881 int64_t int64_t_value = -1; | 881 int64_t int64_t_value = -1; |
| 882 EXPECT_VALID(Dart_IntegerToInt64(integer_obj, &int64_t_value)); | 882 EXPECT_VALID(Dart_IntegerToInt64(integer_obj, &int64_t_value)); |
| 883 EXPECT_EQ(10 - i, int64_t_value); | 883 EXPECT_EQ(10 - i, int64_t_value); |
| 884 } | 884 } |
| 885 } | 885 } |
| 886 | 886 |
| 887 | 887 |
| 888 static int kLength = 16; | |
| 889 | |
| 890 static void ByteDataNativeFunction(Dart_NativeArguments args) { | |
| 891 Dart_EnterScope(); | |
| 892 Dart_Handle byte_data = Dart_NewTypedData(kByteData, kLength); | |
| 893 EXPECT_VALID(byte_data); | |
| 894 // EXPECT_EQ(kByteData, Dart_GetTypeOfTypedData(byte_data)); | |
|
Mads Ager (google)
2013/03/22 08:29:09
Code in comment.
siva
2013/03/22 18:03:03
Removed comment.
On 2013/03/22 08:29:09, Mads Age
| |
| 895 Dart_SetReturnValue(args, byte_data); | |
| 896 Dart_ExitScope(); | |
| 897 } | |
| 898 | |
| 899 | |
| 900 static Dart_NativeFunction ByteDataNativeResolver(Dart_Handle name, | |
| 901 int arg_count) { | |
| 902 return &ByteDataNativeFunction; | |
| 903 } | |
| 904 | |
| 905 | |
| 906 TEST_CASE(ByteDataAccess) { | |
| 907 const char* kScriptChars = | |
| 908 "import 'dart:typeddata';\n" | |
| 909 "ByteData createByteData() native 'CreateByteData';" | |
| 910 "ByteData main() {" | |
| 911 " var length = 16;" | |
| 912 " var a = createByteData();" | |
| 913 " Expect.equals(length, a.lengthInBytes);" | |
| 914 " for (int i = 0; i < length; i+=1) {" | |
| 915 " a.setInt8(i, 0x42);" | |
| 916 " }" | |
| 917 " for (int i = 0; i < length; i+=2) {" | |
| 918 " Expect.equals(0x4242, a.getInt16(i));" | |
| 919 " }" | |
| 920 " return a;" | |
| 921 "}\n"; | |
| 922 // Create a test library and Load up a test script in it. | |
| 923 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 924 | |
| 925 Dart_Handle result = Dart_SetNativeResolver(lib, &ByteDataNativeResolver); | |
| 926 EXPECT_VALID(result); | |
| 927 | |
| 928 // Invoke 'main' function. | |
| 929 result = Dart_Invoke(lib, NewString("main"), 0, NULL); | |
| 930 EXPECT_VALID(result); | |
| 931 } | |
| 932 | |
| 933 | |
| 934 static const intptr_t kExtLength = 16; | |
| 935 static int8_t data[kExtLength] = { 0x41, 0x42, 0x41, 0x42, | |
| 936 0x41, 0x42, 0x41, 0x42, | |
| 937 0x41, 0x42, 0x41, 0x42, | |
| 938 0x41, 0x42, 0x41, 0x42, }; | |
| 939 | |
| 940 static void ExternalByteDataNativeFunction(Dart_NativeArguments args) { | |
| 941 Dart_EnterScope(); | |
| 942 Dart_Handle external_byte_data = Dart_NewExternalTypedData(kByteData, | |
| 943 data, | |
| 944 16, | |
| 945 NULL, NULL); | |
| 946 EXPECT_VALID(external_byte_data); | |
| 947 // EXPECT_EQ(kByteData, Dart_GetTypeOfTypedData(byte_data)); | |
|
Mads Ager (google)
2013/03/22 08:29:09
Code in comment.
siva
2013/03/22 18:03:03
Removed comment.
On 2013/03/22 08:29:09, Mads Age
| |
| 948 Dart_SetReturnValue(args, external_byte_data); | |
| 949 Dart_ExitScope(); | |
| 950 } | |
| 951 | |
| 952 | |
| 953 static Dart_NativeFunction ExternalByteDataNativeResolver(Dart_Handle name, | |
| 954 int arg_count) { | |
| 955 return &ExternalByteDataNativeFunction; | |
| 956 } | |
| 957 | |
| 958 | |
| 959 TEST_CASE(ExternalByteDataAccess) { | |
| 960 // TODO(asiva): Once we have getInt16LE and getInt16BE support use the | |
| 961 // appropriate getter instead of the host endian format used now. | |
| 962 const char* kScriptChars = | |
| 963 "import 'dart:typeddata';\n" | |
| 964 "ByteData createExternalByteData() native 'CreateExternalByteData';" | |
| 965 "ByteData main() {" | |
| 966 " var length = 16;" | |
| 967 " var a = createExternalByteData();" | |
| 968 " Expect.equals(length, a.lengthInBytes);" | |
| 969 " for (int i = 0; i < length; i+=2) {" | |
| 970 " Expect.equals(0x4241, a.getInt16(i));" | |
| 971 " }" | |
| 972 " for (int i = 0; i < length; i+=2) {" | |
| 973 " a.setInt8(i, 0x24);" | |
| 974 " a.setInt8(i + 1, 0x28);" | |
| 975 " }" | |
| 976 " for (int i = 0; i < length; i+=2) {" | |
| 977 " Expect.equals(0x2824, a.getInt16(i));" | |
| 978 " }" | |
| 979 " return a;" | |
| 980 "}\n"; | |
| 981 // Create a test library and Load up a test script in it. | |
| 982 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | |
| 983 | |
| 984 Dart_Handle result = Dart_SetNativeResolver(lib, | |
| 985 &ExternalByteDataNativeResolver); | |
| 986 EXPECT_VALID(result); | |
| 987 | |
| 988 // Invoke 'main' function. | |
| 989 result = Dart_Invoke(lib, NewString("main"), 0, NULL); | |
| 990 EXPECT_VALID(result); | |
| 991 | |
| 992 for (intptr_t i = 0; i < kExtLength; i+=2) { | |
| 993 EXPECT_EQ(0x24, data[i]); | |
| 994 EXPECT_EQ(0x28, data[i+1]); | |
| 995 } | |
| 996 } | |
| 997 | |
| 998 | |
| 888 TEST_CASE(TypedDataDirectAccess) { | 999 TEST_CASE(TypedDataDirectAccess) { |
| 889 Dart_Handle str = Dart_NewStringFromCString("junk"); | 1000 Dart_Handle str = Dart_NewStringFromCString("junk"); |
| 890 Dart_Handle byte_array = Dart_NewTypedData(kUint8, 10); | 1001 Dart_Handle byte_array = Dart_NewTypedData(kUint8, 10); |
| 891 EXPECT_VALID(byte_array); | 1002 EXPECT_VALID(byte_array); |
| 892 Dart_Handle result; | 1003 Dart_Handle result; |
| 893 result = Dart_TypedDataAcquireData(byte_array, NULL, NULL, NULL); | 1004 result = Dart_TypedDataAcquireData(byte_array, NULL, NULL, NULL); |
| 894 EXPECT_ERROR(result, "Dart_TypedDataAcquireData expects argument 'type'" | 1005 EXPECT_ERROR(result, "Dart_TypedDataAcquireData expects argument 'type'" |
| 895 " to be non-null."); | 1006 " to be non-null."); |
| 896 Dart_TypedData_Type type; | 1007 Dart_TypedData_Type type; |
| 897 result = Dart_TypedDataAcquireData(byte_array, &type, NULL, NULL); | 1008 result = Dart_TypedDataAcquireData(byte_array, &type, NULL, NULL); |
| (...skipping 6531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 7429 NULL); | 7540 NULL); |
| 7430 int64_t value = 0; | 7541 int64_t value = 0; |
| 7431 result = Dart_IntegerToInt64(result, &value); | 7542 result = Dart_IntegerToInt64(result, &value); |
| 7432 EXPECT_VALID(result); | 7543 EXPECT_VALID(result); |
| 7433 EXPECT_EQ(260, value); | 7544 EXPECT_EQ(260, value); |
| 7434 } | 7545 } |
| 7435 | 7546 |
| 7436 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 7547 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 7437 | 7548 |
| 7438 } // namespace dart | 7549 } // namespace dart |
| OLD | NEW |