| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 "vm/bootstrap_natives.h" | 5 #include "vm/bootstrap_natives.h" |
| 6 | 6 |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 | 8 |
| 9 #include "vm/bigint_operations.h" | 9 #include "vm/bigint_operations.h" |
| 10 #include "vm/exceptions.h" | 10 #include "vm/exceptions.h" |
| 11 #include "vm/native_entry.h" | 11 #include "vm/native_entry.h" |
| 12 #include "vm/object.h" | 12 #include "vm/object.h" |
| 13 | 13 |
| 14 namespace dart { | 14 namespace dart { |
| 15 | 15 |
| 16 // TypedData. | 16 // TypedData. |
| 17 | 17 |
| 18 // Checks to see if offsetInBytes is in the range. | 18 // Checks to see if offset_in_bytes is in the range. |
| 19 static bool RangeCheck(intptr_t offsetInBytes, intptr_t lengthInBytes) { | 19 static bool RangeCheck(intptr_t offset_in_bytes, intptr_t length_in_bytes) { |
| 20 return ((offsetInBytes >= 0) && | 20 return ((offset_in_bytes >= 0) && |
| 21 (lengthInBytes > 0) && | 21 (length_in_bytes > 0) && |
| 22 (offsetInBytes < lengthInBytes)); | 22 (offset_in_bytes < length_in_bytes)); |
| 23 } |
| 24 |
| 25 |
| 26 // Checks to see if offsetInBytes + num_bytes is in the range. |
| 27 static void SetRangeCheck(intptr_t offset_in_bytes, |
| 28 intptr_t num_bytes, |
| 29 intptr_t length_in_bytes, |
| 30 intptr_t element_size_in_bytes) { |
| 31 if (!Utils::RangeCheck(offset_in_bytes, num_bytes, length_in_bytes)) { |
| 32 const String& error = String::Handle(String::NewFormatted( |
| 33 "index (%"Pd") must be in the range [0..%"Pd")", |
| 34 (offset_in_bytes / element_size_in_bytes), |
| 35 (length_in_bytes / element_size_in_bytes))); |
| 36 const Array& args = Array::Handle(Array::New(1)); |
| 37 args.SetAt(0, error); |
| 38 Exceptions::ThrowByType(Exceptions::kRange, args); |
| 39 } |
| 23 } | 40 } |
| 24 | 41 |
| 25 | 42 |
| 26 // Checks to see if a length will not result in an OOM error. | 43 // Checks to see if a length will not result in an OOM error. |
| 27 static void LengthCheck(intptr_t len, intptr_t max) { | 44 static void LengthCheck(intptr_t len, intptr_t max) { |
| 28 ASSERT(len >= 0); | 45 ASSERT(len >= 0); |
| 29 if (len > max) { | 46 if (len > max) { |
| 30 const String& error = String::Handle(String::NewFormatted( | 47 const String& error = String::Handle(String::NewFormatted( |
| 31 "insufficient memory to allocate a TypedData object of length (%"Pd")", | 48 "insufficient memory to allocate a TypedData object of length (%"Pd")", |
| 32 len)); | 49 len)); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 55 } | 72 } |
| 56 const String& error = String::Handle(String::NewFormatted( | 73 const String& error = String::Handle(String::NewFormatted( |
| 57 "Expected a TypedData object but found %s", instance.ToCString())); | 74 "Expected a TypedData object but found %s", instance.ToCString())); |
| 58 const Array& args = Array::Handle(Array::New(1)); | 75 const Array& args = Array::Handle(Array::New(1)); |
| 59 args.SetAt(0, error); | 76 args.SetAt(0, error); |
| 60 Exceptions::ThrowByType(Exceptions::kArgument, args); | 77 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 61 return Integer::null(); | 78 return Integer::null(); |
| 62 } | 79 } |
| 63 | 80 |
| 64 | 81 |
| 82 #define COPY_DATA(type, dst, src) \ |
| 83 const type& dst_array = type::Cast(dst); \ |
| 84 const type& src_array = type::Cast(src); \ |
| 85 intptr_t element_size_in_bytes = dst_array.ElementSizeInBytes(); \ |
| 86 intptr_t length_in_bytes = length.Value() * element_size_in_bytes; \ |
| 87 intptr_t src_offset_in_bytes = src_start.Value() * element_size_in_bytes; \ |
| 88 intptr_t dst_offset_in_bytes = dst_start.Value() * element_size_in_bytes; \ |
| 89 SetRangeCheck(src_offset_in_bytes, \ |
| 90 length_in_bytes, \ |
| 91 src_array.LengthInBytes(), \ |
| 92 element_size_in_bytes); \ |
| 93 SetRangeCheck(dst_offset_in_bytes, \ |
| 94 length_in_bytes, \ |
| 95 dst_array.LengthInBytes(), \ |
| 96 element_size_in_bytes); \ |
| 97 type::Copy(dst_array, dst_offset_in_bytes, \ |
| 98 src_array, src_offset_in_bytes, \ |
| 99 length_in_bytes); |
| 100 |
| 101 DEFINE_NATIVE_ENTRY(TypedData_setRange, 5) { |
| 102 GET_NON_NULL_NATIVE_ARGUMENT(Instance, dst, arguments->NativeArgAt(0)); |
| 103 GET_NON_NULL_NATIVE_ARGUMENT(Smi, dst_start, arguments->NativeArgAt(1)); |
| 104 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(2)); |
| 105 GET_NON_NULL_NATIVE_ARGUMENT(Instance, src, arguments->NativeArgAt(3)); |
| 106 GET_NON_NULL_NATIVE_ARGUMENT(Smi, src_start, arguments->NativeArgAt(4)); |
| 107 |
| 108 if (length.Value() < 0) { |
| 109 const String& error = String::Handle(String::NewFormatted( |
| 110 "length (%"Pd") must be non-negative", length.Value())); |
| 111 const Array& args = Array::Handle(Array::New(1)); |
| 112 args.SetAt(0, error); |
| 113 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 114 } |
| 115 if ((dst.IsTypedData() || dst.IsExternalTypedData()) && |
| 116 (dst.clazz() == src.clazz())) { |
| 117 if (dst.IsTypedData()) { |
| 118 ASSERT(src.IsTypedData()); |
| 119 COPY_DATA(TypedData, dst, src); |
| 120 } else { |
| 121 ASSERT(src.IsExternalTypedData()); |
| 122 ASSERT(dst.IsExternalTypedData()); |
| 123 COPY_DATA(ExternalTypedData, dst, src); |
| 124 } |
| 125 return Bool::True().raw(); |
| 126 } |
| 127 return Bool::False().raw(); |
| 128 } |
| 129 |
| 130 |
| 65 // We check the length parameter against a possible maximum length for the | 131 // We check the length parameter against a possible maximum length for the |
| 66 // array based on available physical addressable memory on the system. The | 132 // array based on available physical addressable memory on the system. The |
| 67 // maximum possible length is a scaled value of kSmiMax which is set up based | 133 // maximum possible length is a scaled value of kSmiMax which is set up based |
| 68 // on whether the underlying architecture is 32-bit or 64-bit. | 134 // on whether the underlying architecture is 32-bit or 64-bit. |
| 69 #define TYPED_DATA_NEW(name) \ | 135 #define TYPED_DATA_NEW(name) \ |
| 70 DEFINE_NATIVE_ENTRY(TypedData_##name##_new, 1) { \ | 136 DEFINE_NATIVE_ENTRY(TypedData_##name##_new, 1) { \ |
| 71 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(0)); \ | 137 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(0)); \ |
| 72 intptr_t cid = kTypedData##name##Cid; \ | 138 intptr_t cid = kTypedData##name##Cid; \ |
| 73 intptr_t len = length.Value(); \ | 139 intptr_t len = length.Value(); \ |
| 74 intptr_t max = TypedData::MaxElements(cid); \ | 140 intptr_t max = TypedData::MaxElements(cid); \ |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 TYPED_DATA_NATIVES(Int16Array, GetInt16, SetInt16, Smi, Value) | 301 TYPED_DATA_NATIVES(Int16Array, GetInt16, SetInt16, Smi, Value) |
| 236 TYPED_DATA_NATIVES(Uint16Array, GetUint16, SetUint16, Smi, Value) | 302 TYPED_DATA_NATIVES(Uint16Array, GetUint16, SetUint16, Smi, Value) |
| 237 TYPED_DATA_NATIVES(Int32Array, GetInt32, SetInt32, Integer, AsInt64Value) | 303 TYPED_DATA_NATIVES(Int32Array, GetInt32, SetInt32, Integer, AsInt64Value) |
| 238 TYPED_DATA_NATIVES(Uint32Array, GetUint32, SetUint32, Integer, AsInt64Value) | 304 TYPED_DATA_NATIVES(Uint32Array, GetUint32, SetUint32, Integer, AsInt64Value) |
| 239 TYPED_DATA_NATIVES(Int64Array, GetInt64, SetInt64, Integer, AsInt64Value) | 305 TYPED_DATA_NATIVES(Int64Array, GetInt64, SetInt64, Integer, AsInt64Value) |
| 240 TYPED_DATA_UINT64_NATIVES(Uint64Array, GetUint64, SetUint64, Integer) | 306 TYPED_DATA_UINT64_NATIVES(Uint64Array, GetUint64, SetUint64, Integer) |
| 241 TYPED_DATA_NATIVES(Float32Array, GetFloat32, SetFloat32, Double, value) | 307 TYPED_DATA_NATIVES(Float32Array, GetFloat32, SetFloat32, Double, value) |
| 242 TYPED_DATA_NATIVES(Float64Array, GetFloat64, SetFloat64, Double, value) | 308 TYPED_DATA_NATIVES(Float64Array, GetFloat64, SetFloat64, Double, value) |
| 243 | 309 |
| 244 } // namespace dart | 310 } // namespace dart |
| OLD | NEW |