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

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

Issue 10379018: Revert "Revert "Implement {Int,Uint}{8,16,32,64} and Float{32,64} typed arrays."" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
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
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 1845 matching lines...) Expand 10 before | Expand all | Expand 10 after
1856 return Api::NewError("Invalid length passed in to access array elements"); \ 1856 return Api::NewError("Invalid length passed in to access array elements"); \
1857 1857
1858 1858
1859 DART_EXPORT Dart_Handle Dart_ListGetAsBytes(Dart_Handle list, 1859 DART_EXPORT Dart_Handle Dart_ListGetAsBytes(Dart_Handle list,
1860 intptr_t offset, 1860 intptr_t offset,
1861 uint8_t* native_array, 1861 uint8_t* native_array,
1862 intptr_t length) { 1862 intptr_t length) {
1863 Isolate* isolate = Isolate::Current(); 1863 Isolate* isolate = Isolate::Current();
1864 DARTSCOPE(isolate); 1864 DARTSCOPE(isolate);
1865 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(list)); 1865 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(list));
1866 if (obj.IsByteArray()) { 1866 if (obj.IsUint8Array() || obj.IsExternalUint8Array()) {
1867 ByteArray& byte_array = ByteArray::Handle(isolate); 1867 ByteArray& byte_array = ByteArray::Handle(isolate);
1868 byte_array ^= obj.raw(); 1868 byte_array ^= obj.raw();
1869 if (Utils::RangeCheck(offset, length, byte_array.Length())) { 1869 if (Utils::RangeCheck(offset, length, byte_array.Length())) {
1870 ByteArray::Copy(native_array, byte_array, offset, length); 1870 ByteArray::Copy(native_array, byte_array, offset, length);
1871 return Api::Success(isolate); 1871 return Api::Success(isolate);
1872 } 1872 }
1873 return Api::NewError("Invalid length passed in to access list elements"); 1873 return Api::NewError("Invalid length passed in to access list elements");
1874 } 1874 }
1875 if (obj.IsArray()) { 1875 if (obj.IsArray()) {
1876 GET_LIST_ELEMENT_AS_BYTES(isolate, 1876 GET_LIST_ELEMENT_AS_BYTES(isolate,
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1940 return Api::NewError("Invalid length passed in to set array elements"); \ 1940 return Api::NewError("Invalid length passed in to set array elements"); \
1941 1941
1942 1942
1943 DART_EXPORT Dart_Handle Dart_ListSetAsBytes(Dart_Handle list, 1943 DART_EXPORT Dart_Handle Dart_ListSetAsBytes(Dart_Handle list,
1944 intptr_t offset, 1944 intptr_t offset,
1945 uint8_t* native_array, 1945 uint8_t* native_array,
1946 intptr_t length) { 1946 intptr_t length) {
1947 Isolate* isolate = Isolate::Current(); 1947 Isolate* isolate = Isolate::Current();
1948 DARTSCOPE(isolate); 1948 DARTSCOPE(isolate);
1949 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(list)); 1949 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(list));
1950 if (obj.IsByteArray()) { 1950 if (obj.IsUint8Array() || obj.IsExternalUint8Array()) {
1951 ByteArray& byte_array = ByteArray::Handle(isolate); 1951 ByteArray& byte_array = ByteArray::Handle(isolate);
1952 byte_array ^= obj.raw(); 1952 byte_array ^= obj.raw();
1953 if (Utils::RangeCheck(offset, length, byte_array.Length())) { 1953 if (Utils::RangeCheck(offset, length, byte_array.Length())) {
1954 ByteArray::Copy(byte_array, offset, native_array, length); 1954 ByteArray::Copy(byte_array, offset, native_array, length);
1955 return Api::Success(isolate); 1955 return Api::Success(isolate);
1956 } 1956 }
1957 return Api::NewError("Invalid length passed in to set list elements"); 1957 return Api::NewError("Invalid length passed in to set list elements");
1958 } 1958 }
1959 if (obj.IsArray()) { 1959 if (obj.IsArray()) {
1960 if (obj.IsImmutableArray()) { 1960 if (obj.IsImmutableArray()) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
2015 Isolate* isolate = Isolate::Current(); 2015 Isolate* isolate = Isolate::Current();
2016 DARTSCOPE(isolate); 2016 DARTSCOPE(isolate);
2017 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object)); 2017 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
2018 return obj.IsByteArray(); 2018 return obj.IsByteArray();
2019 } 2019 }
2020 2020
2021 2021
2022 DART_EXPORT Dart_Handle Dart_NewByteArray(intptr_t length) { 2022 DART_EXPORT Dart_Handle Dart_NewByteArray(intptr_t length) {
2023 Isolate* isolate = Isolate::Current(); 2023 Isolate* isolate = Isolate::Current();
2024 DARTSCOPE(isolate); 2024 DARTSCOPE(isolate);
2025 return Api::NewHandle(isolate, InternalByteArray::New(length)); 2025 return Api::NewHandle(isolate, Uint8Array::New(length));
2026 } 2026 }
2027 2027
2028 2028
2029 DART_EXPORT Dart_Handle Dart_NewExternalByteArray(uint8_t* data, 2029 DART_EXPORT Dart_Handle Dart_NewExternalByteArray(uint8_t* data,
2030 intptr_t length, 2030 intptr_t length,
2031 void* peer, 2031 void* peer,
2032 Dart_PeerFinalizer callback) { 2032 Dart_PeerFinalizer callback) {
2033 Isolate* isolate = Isolate::Current(); 2033 Isolate* isolate = Isolate::Current();
2034 DARTSCOPE(isolate); 2034 DARTSCOPE(isolate);
2035 if (data == NULL && length != 0) { 2035 if (data == NULL && length != 0) {
2036 return Api::NewError("%s expects argument 'data' to be non-null.", 2036 return Api::NewError("%s expects argument 'data' to be non-null.",
2037 CURRENT_FUNC); 2037 CURRENT_FUNC);
2038 } 2038 }
2039 if (length < 0) { 2039 if (length < 0) {
2040 return Api::NewError("%s expects argument 'length' to be greater than 0.", 2040 return Api::NewError("%s expects argument 'length' to be greater than 0.",
2041 CURRENT_FUNC); 2041 CURRENT_FUNC);
2042 } 2042 }
2043 return Api::NewHandle( 2043 return Api::NewHandle(
2044 isolate, ExternalByteArray::New(data, length, peer, callback)); 2044 isolate, ExternalUint8Array::New(data, length, peer, callback));
2045 } 2045 }
2046 2046
2047 2047
2048 DART_EXPORT Dart_Handle Dart_ExternalByteArrayGetPeer(Dart_Handle object, 2048 DART_EXPORT Dart_Handle Dart_ExternalByteArrayGetPeer(Dart_Handle object,
2049 void** peer) { 2049 void** peer) {
2050 Isolate* isolate = Isolate::Current(); 2050 Isolate* isolate = Isolate::Current();
2051 DARTSCOPE(isolate); 2051 DARTSCOPE(isolate);
2052 const ExternalByteArray& array = 2052 const ExternalUint8Array& array =
2053 Api::UnwrapExternalByteArrayHandle(isolate, object); 2053 Api::UnwrapExternalUint8ArrayHandle(isolate, object);
2054 if (array.IsNull()) { 2054 if (array.IsNull()) {
2055 RETURN_TYPE_ERROR(isolate, object, ExternalByteArray); 2055 RETURN_TYPE_ERROR(isolate, object, ExternalUint8Array);
2056 } 2056 }
2057 if (peer == NULL) { 2057 if (peer == NULL) {
2058 return Api::NewError("%s expects argument 'peer' to be non-null.", 2058 return Api::NewError("%s expects argument 'peer' to be non-null.",
2059 CURRENT_FUNC); 2059 CURRENT_FUNC);
2060 } 2060 }
2061 *peer = array.GetPeer(); 2061 *peer = array.GetPeer();
2062 return Api::Success(isolate); 2062 return Api::Success(isolate);
2063 } 2063 }
2064 2064
2065 2065
2066 template<typename T> 2066 template<typename T>
2067 Dart_Handle ByteArrayGetAt(T* value, Dart_Handle array, intptr_t offset) { 2067 Dart_Handle ByteArrayGetAt(T* value, Dart_Handle array, intptr_t offset) {
2068 Isolate* isolate = Isolate::Current(); 2068 Isolate* isolate = Isolate::Current();
2069 CHECK_ISOLATE(isolate); 2069 CHECK_ISOLATE(isolate);
2070 const ByteArray& array_obj = Api::UnwrapByteArrayHandle(isolate, array); 2070 const ByteArray& array_obj = Api::UnwrapByteArrayHandle(isolate, array);
2071 if (array_obj.IsNull()) { 2071 if (array_obj.IsNull()) {
2072 RETURN_TYPE_ERROR(isolate, array, ByteArray); 2072 RETURN_TYPE_ERROR(isolate, array, ByteArray);
2073 } 2073 }
2074 intptr_t length = sizeof(T); 2074 intptr_t length = sizeof(T);
2075 if (!Utils::RangeCheck(offset, length, array_obj.Length())) { 2075 if (!Utils::RangeCheck(offset, length, array_obj.ByteLength())) {
2076 return Api::NewError("Invalid index passed in to get byte array element"); 2076 return Api::NewError("Invalid index passed in to get byte array element");
2077 } 2077 }
2078 uint8_t* dst = reinterpret_cast<uint8_t*>(value); 2078 uint8_t* dst = reinterpret_cast<uint8_t*>(value);
2079 ByteArray::Copy(dst, array_obj, offset, length); 2079 ByteArray::Copy(dst, array_obj, offset, length);
2080 return Api::Success(isolate); 2080 return Api::Success(isolate);
2081 } 2081 }
2082 2082
2083 2083
2084 template<typename T> 2084 template<typename T>
2085 Dart_Handle ByteArraySetAt(Dart_Handle array, intptr_t offset, T value) { 2085 Dart_Handle ByteArraySetAt(Dart_Handle array, intptr_t offset, T value) {
2086 Isolate* isolate = Isolate::Current(); 2086 Isolate* isolate = Isolate::Current();
2087 CHECK_ISOLATE(isolate); 2087 CHECK_ISOLATE(isolate);
2088 const ByteArray& array_obj = Api::UnwrapByteArrayHandle(isolate, array); 2088 const ByteArray& array_obj = Api::UnwrapByteArrayHandle(isolate, array);
2089 if (array_obj.IsNull()) { 2089 if (array_obj.IsNull()) {
2090 RETURN_TYPE_ERROR(isolate, array, ByteArray); 2090 RETURN_TYPE_ERROR(isolate, array, ByteArray);
2091 } 2091 }
2092 intptr_t length = sizeof(T); 2092 intptr_t length = sizeof(T);
2093 if (!Utils::RangeCheck(offset, length, array_obj.Length())) { 2093 if (!Utils::RangeCheck(offset, length, array_obj.ByteLength())) {
2094 return Api::NewError("Invalid index passed in to get byte array element"); 2094 return Api::NewError("Invalid index passed in to get byte array element");
2095 } 2095 }
2096 const uint8_t* src = reinterpret_cast<uint8_t*>(&value); 2096 const uint8_t* src = reinterpret_cast<uint8_t*>(&value);
2097 ByteArray::Copy(array_obj, offset, src, length); 2097 ByteArray::Copy(array_obj, offset, src, length);
2098 return Api::Success(isolate); 2098 return Api::Success(isolate);
2099 } 2099 }
2100 2100
2101 2101
2102 DART_EXPORT Dart_Handle Dart_ByteArrayGetInt8At(Dart_Handle array, 2102 DART_EXPORT Dart_Handle Dart_ByteArrayGetInt8At(Dart_Handle array,
2103 intptr_t offset, 2103 intptr_t byte_offset,
2104 int8_t* value) { 2104 int8_t* value) {
2105 return ByteArrayGetAt(value, array, offset); 2105 return ByteArrayGetAt(value, array, byte_offset);
2106 } 2106 }
2107 2107
2108 2108
2109 DART_EXPORT Dart_Handle Dart_ByteArraySetInt8At(Dart_Handle array, 2109 DART_EXPORT Dart_Handle Dart_ByteArraySetInt8At(Dart_Handle array,
2110 intptr_t offset, 2110 intptr_t byte_offset,
2111 int8_t value) { 2111 int8_t value) {
2112 return ByteArraySetAt(array, offset, value); 2112 return ByteArraySetAt(array, byte_offset, value);
2113 } 2113 }
2114 2114
2115 2115
2116 DART_EXPORT Dart_Handle Dart_ByteArrayGetUint8At(Dart_Handle array, 2116 DART_EXPORT Dart_Handle Dart_ByteArrayGetUint8At(Dart_Handle array,
2117 intptr_t offset, 2117 intptr_t byte_offset,
2118 uint8_t* value) { 2118 uint8_t* value) {
2119 return ByteArrayGetAt(value, array, offset); 2119 return ByteArrayGetAt(value, array, byte_offset);
2120 } 2120 }
2121 2121
2122 2122
2123 DART_EXPORT Dart_Handle Dart_ByteArraySetUint8At(Dart_Handle array, 2123 DART_EXPORT Dart_Handle Dart_ByteArraySetUint8At(Dart_Handle array,
2124 intptr_t offset, 2124 intptr_t byte_offset,
2125 uint8_t value) { 2125 uint8_t value) {
2126 return ByteArraySetAt(array, offset, value); 2126 return ByteArraySetAt(array, byte_offset, value);
2127 } 2127 }
2128 2128
2129 2129
2130 DART_EXPORT Dart_Handle Dart_ByteArrayGetInt16At(Dart_Handle array, 2130 DART_EXPORT Dart_Handle Dart_ByteArrayGetInt16At(Dart_Handle array,
2131 intptr_t offset, 2131 intptr_t byte_offset,
2132 int16_t* value) { 2132 int16_t* value) {
2133 return ByteArrayGetAt(value, array, offset); 2133 return ByteArrayGetAt(value, array, byte_offset);
2134 } 2134 }
2135 2135
2136 2136
2137 DART_EXPORT Dart_Handle Dart_ByteArraySetInt16At(Dart_Handle array, 2137 DART_EXPORT Dart_Handle Dart_ByteArraySetInt16At(Dart_Handle array,
2138 intptr_t offset, 2138 intptr_t byte_offset,
2139 int16_t value) { 2139 int16_t value) {
2140 return ByteArraySetAt(array, offset, value); 2140 return ByteArraySetAt(array, byte_offset, value);
2141 } 2141 }
2142 2142
2143 2143
2144 DART_EXPORT Dart_Handle Dart_ByteArrayGetUint16At(Dart_Handle array, 2144 DART_EXPORT Dart_Handle Dart_ByteArrayGetUint16At(Dart_Handle array,
2145 intptr_t offset, 2145 intptr_t byte_offset,
2146 uint16_t* value) { 2146 uint16_t* value) {
2147 return ByteArrayGetAt(value, array, offset); 2147 return ByteArrayGetAt(value, array, byte_offset);
2148 } 2148 }
2149 2149
2150 2150
2151 DART_EXPORT Dart_Handle Dart_ByteArraySetUint16At(Dart_Handle array, 2151 DART_EXPORT Dart_Handle Dart_ByteArraySetUint16At(Dart_Handle array,
2152 intptr_t offset, 2152 intptr_t byte_offset,
2153 uint16_t value) { 2153 uint16_t value) {
2154 return ByteArraySetAt(array, offset, value); 2154 return ByteArraySetAt(array, byte_offset, value);
2155 } 2155 }
2156 2156
2157 2157
2158 DART_EXPORT Dart_Handle Dart_ByteArrayGetInt32At(Dart_Handle array, 2158 DART_EXPORT Dart_Handle Dart_ByteArrayGetInt32At(Dart_Handle array,
2159 intptr_t offset, 2159 intptr_t byte_offset,
2160 int32_t* value) { 2160 int32_t* value) {
2161 return ByteArrayGetAt(value, array, offset); 2161 return ByteArrayGetAt(value, array, byte_offset);
2162 } 2162 }
2163 2163
2164 2164
2165 DART_EXPORT Dart_Handle Dart_ByteArraySetInt32At(Dart_Handle array, 2165 DART_EXPORT Dart_Handle Dart_ByteArraySetInt32At(Dart_Handle array,
2166 intptr_t offset, 2166 intptr_t byte_offset,
2167 int32_t value) { 2167 int32_t value) {
2168 return ByteArraySetAt(array, offset, value); 2168 return ByteArraySetAt(array, byte_offset, value);
2169 } 2169 }
2170 2170
2171 2171
2172 DART_EXPORT Dart_Handle Dart_ByteArrayGetUint32At(Dart_Handle array, 2172 DART_EXPORT Dart_Handle Dart_ByteArrayGetUint32At(Dart_Handle array,
2173 intptr_t offset, 2173 intptr_t byte_offset,
2174 uint32_t* value) { 2174 uint32_t* value) {
2175 return ByteArrayGetAt(value, array, offset); 2175 return ByteArrayGetAt(value, array, byte_offset);
2176 } 2176 }
2177 2177
2178 2178
2179 DART_EXPORT Dart_Handle Dart_ByteArraySetUint32At(Dart_Handle array, 2179 DART_EXPORT Dart_Handle Dart_ByteArraySetUint32At(Dart_Handle array,
2180 intptr_t offset, 2180 intptr_t byte_offset,
2181 uint32_t value) { 2181 uint32_t value) {
2182 return ByteArraySetAt(array, offset, value); 2182 return ByteArraySetAt(array, byte_offset, value);
2183 } 2183 }
2184 2184
2185 2185
2186 DART_EXPORT Dart_Handle Dart_ByteArrayGetInt64At(Dart_Handle array, 2186 DART_EXPORT Dart_Handle Dart_ByteArrayGetInt64At(Dart_Handle array,
2187 intptr_t offset, 2187 intptr_t byte_offset,
2188 int64_t* value) { 2188 int64_t* value) {
2189 return ByteArrayGetAt(value, array, offset); 2189 return ByteArrayGetAt(value, array, byte_offset);
2190 } 2190 }
2191 2191
2192 2192
2193 DART_EXPORT Dart_Handle Dart_ByteArraySetInt64At(Dart_Handle array, 2193 DART_EXPORT Dart_Handle Dart_ByteArraySetInt64At(Dart_Handle array,
2194 intptr_t offset, 2194 intptr_t byte_offset,
2195 int64_t value) { 2195 int64_t value) {
2196 return ByteArraySetAt(array, offset, value); 2196 return ByteArraySetAt(array, byte_offset, value);
2197 } 2197 }
2198 2198
2199 2199
2200 DART_EXPORT Dart_Handle Dart_ByteArrayGetUint64At(Dart_Handle array, 2200 DART_EXPORT Dart_Handle Dart_ByteArrayGetUint64At(Dart_Handle array,
2201 intptr_t offset, 2201 intptr_t byte_offset,
2202 uint64_t* value) { 2202 uint64_t* value) {
2203 return ByteArrayGetAt(value, array, offset); 2203 return ByteArrayGetAt(value, array, byte_offset);
2204 } 2204 }
2205 2205
2206 2206
2207 DART_EXPORT Dart_Handle Dart_ByteArraySetUint64At(Dart_Handle array, 2207 DART_EXPORT Dart_Handle Dart_ByteArraySetUint64At(Dart_Handle array,
2208 intptr_t offset, 2208 intptr_t byte_offset,
2209 uint64_t value) { 2209 uint64_t value) {
2210 return ByteArraySetAt(array, offset, value); 2210 return ByteArraySetAt(array, byte_offset, value);
2211 } 2211 }
2212 2212
2213 2213
2214 DART_EXPORT Dart_Handle Dart_ByteArrayGetFloat32At(Dart_Handle array, 2214 DART_EXPORT Dart_Handle Dart_ByteArrayGetFloat32At(Dart_Handle array,
2215 intptr_t offset, 2215 intptr_t byte_offset,
2216 float* value) { 2216 float* value) {
2217 return ByteArrayGetAt(value, array, offset); 2217 return ByteArrayGetAt(value, array, byte_offset);
2218 } 2218 }
2219 2219
2220 2220
2221 DART_EXPORT Dart_Handle Dart_ByteArraySetFloat32At(Dart_Handle array, 2221 DART_EXPORT Dart_Handle Dart_ByteArraySetFloat32At(Dart_Handle array,
2222 intptr_t offset, 2222 intptr_t byte_offset,
2223 float value) { 2223 float value) {
2224 return ByteArraySetAt(array, offset, value); 2224 return ByteArraySetAt(array, byte_offset, value);
2225 } 2225 }
2226 2226
2227 2227
2228 DART_EXPORT Dart_Handle Dart_ByteArrayGetFloat64At(Dart_Handle array, 2228 DART_EXPORT Dart_Handle Dart_ByteArrayGetFloat64At(Dart_Handle array,
2229 intptr_t offset, 2229 intptr_t byte_offset,
2230 double* value) { 2230 double* value) {
2231 return ByteArrayGetAt(value, array, offset); 2231 return ByteArrayGetAt(value, array, byte_offset);
2232 } 2232 }
2233 2233
2234 2234
2235 DART_EXPORT Dart_Handle Dart_ByteArraySetFloat64At(Dart_Handle array, 2235 DART_EXPORT Dart_Handle Dart_ByteArraySetFloat64At(Dart_Handle array,
2236 intptr_t offset, 2236 intptr_t byte_offset,
2237 double value) { 2237 double value) {
2238 return ByteArraySetAt(array, offset, value); 2238 return ByteArraySetAt(array, byte_offset, value);
2239 } 2239 }
2240 2240
2241 2241
2242 // --- Closures --- 2242 // --- Closures ---
2243 2243
2244 2244
2245 DART_EXPORT bool Dart_IsClosure(Dart_Handle object) { 2245 DART_EXPORT bool Dart_IsClosure(Dart_Handle object) {
2246 Isolate* isolate = Isolate::Current(); 2246 Isolate* isolate = Isolate::Current();
2247 DARTSCOPE(isolate); 2247 DARTSCOPE(isolate);
2248 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object)); 2248 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
(...skipping 1073 matching lines...) Expand 10 before | Expand all | Expand 10 after
3322 *buffer = NULL; 3322 *buffer = NULL;
3323 } 3323 }
3324 delete debug_region; 3324 delete debug_region;
3325 } else { 3325 } else {
3326 *buffer = NULL; 3326 *buffer = NULL;
3327 *buffer_size = 0; 3327 *buffer_size = 0;
3328 } 3328 }
3329 } 3329 }
3330 3330
3331 } // namespace dart 3331 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698