Chromium Code Reviews| 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" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 return Smi::New(array.Length()); | 71 return Smi::New(array.Length()); |
| 72 } | 72 } |
| 73 const String& error = String::Handle(String::NewFormatted( | 73 const String& error = String::Handle(String::NewFormatted( |
| 74 "Expected a TypedData object but found %s", instance.ToCString())); | 74 "Expected a TypedData object but found %s", instance.ToCString())); |
| 75 const Array& args = Array::Handle(Array::New(1)); | 75 const Array& args = Array::Handle(Array::New(1)); |
| 76 args.SetAt(0, error); | 76 args.SetAt(0, error); |
| 77 Exceptions::ThrowByType(Exceptions::kArgument, args); | 77 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 78 return Integer::null(); | 78 return Integer::null(); |
| 79 } | 79 } |
| 80 | 80 |
| 81 | 81 template <typename DstType, typename SrcType> |
| 82 #define COPY_DATA(type, dst, src) \ | 82 static RawBool* CopyData(const Instance& dst, const Instance& src, |
| 83 const type& dst_array = type::Cast(dst); \ | 83 const Smi& dst_start, const Smi& src_start, |
| 84 const type& src_array = type::Cast(src); \ | 84 const Smi& length) { |
| 85 intptr_t element_size_in_bytes = dst_array.ElementSizeInBytes(); \ | 85 const DstType& dst_array = DstType::Cast(dst); |
| 86 intptr_t length_in_bytes = length.Value() * element_size_in_bytes; \ | 86 const SrcType& src_array = SrcType::Cast(src); |
| 87 intptr_t src_offset_in_bytes = src_start.Value() * element_size_in_bytes; \ | 87 intptr_t dst_element_size_in_bytes = dst_array.ElementSizeInBytes(); |
| 88 intptr_t dst_offset_in_bytes = dst_start.Value() * element_size_in_bytes; \ | 88 intptr_t src_element_size_in_bytes = src_array.ElementSizeInBytes(); |
| 89 SetRangeCheck(src_offset_in_bytes, \ | 89 intptr_t dst_offset_in_bytes = dst_start.Value() * dst_element_size_in_bytes; |
| 90 length_in_bytes, \ | 90 intptr_t src_offset_in_bytes = src_start.Value() * src_element_size_in_bytes; |
| 91 src_array.LengthInBytes(), \ | 91 intptr_t length_in_bytes = length.Value() * src_element_size_in_bytes; |
| 92 element_size_in_bytes); \ | 92 if (src_element_size_in_bytes != dst_element_size_in_bytes) { |
|
Vyacheslav Egorov (Google)
2013/04/16 16:23:06
You need to compare element types.
kustermann
2013/04/16 17:26:04
Done.
| |
| 93 SetRangeCheck(dst_offset_in_bytes, \ | 93 return Bool::False().raw(); |
| 94 length_in_bytes, \ | 94 } |
|
siva
2013/04/16 16:36:48
I don't believe this check is sufficient, if dst i
kustermann
2013/04/16 17:26:04
I added now an ElementType function to TypedData/E
| |
| 95 dst_array.LengthInBytes(), \ | 95 SetRangeCheck(src_offset_in_bytes, |
| 96 element_size_in_bytes); \ | 96 length_in_bytes, |
| 97 type::Copy(dst_array, dst_offset_in_bytes, \ | 97 src_array.LengthInBytes(), |
| 98 src_array, src_offset_in_bytes, \ | 98 src_element_size_in_bytes); |
| 99 length_in_bytes); | 99 SetRangeCheck(dst_offset_in_bytes, |
| 100 length_in_bytes, | |
| 101 dst_array.LengthInBytes(), | |
| 102 dst_element_size_in_bytes); | |
| 103 { | |
| 104 NoGCScope no_gc; | |
| 105 if (length_in_bytes > 0) { | |
| 106 memmove(dst_array.DataAddr(dst_offset_in_bytes), | |
| 107 src_array.DataAddr(src_offset_in_bytes), | |
| 108 length_in_bytes); | |
| 109 } | |
|
siva
2013/04/16 16:36:48
Why does this have to be explicitly listed here in
kustermann
2013/04/16 17:26:04
The reason for this is that the appropriate Copy f
| |
| 110 return Bool::True().raw(); | |
| 111 } | |
| 112 } | |
| 100 | 113 |
| 101 DEFINE_NATIVE_ENTRY(TypedData_setRange, 5) { | 114 DEFINE_NATIVE_ENTRY(TypedData_setRange, 5) { |
| 102 GET_NON_NULL_NATIVE_ARGUMENT(Instance, dst, arguments->NativeArgAt(0)); | 115 GET_NON_NULL_NATIVE_ARGUMENT(Instance, dst, arguments->NativeArgAt(0)); |
| 103 GET_NON_NULL_NATIVE_ARGUMENT(Smi, dst_start, arguments->NativeArgAt(1)); | 116 GET_NON_NULL_NATIVE_ARGUMENT(Smi, dst_start, arguments->NativeArgAt(1)); |
| 104 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(2)); | 117 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(2)); |
| 105 GET_NON_NULL_NATIVE_ARGUMENT(Instance, src, arguments->NativeArgAt(3)); | 118 GET_NON_NULL_NATIVE_ARGUMENT(Instance, src, arguments->NativeArgAt(3)); |
| 106 GET_NON_NULL_NATIVE_ARGUMENT(Smi, src_start, arguments->NativeArgAt(4)); | 119 GET_NON_NULL_NATIVE_ARGUMENT(Smi, src_start, arguments->NativeArgAt(4)); |
| 107 | 120 |
| 108 if (length.Value() < 0) { | 121 if (length.Value() < 0) { |
| 109 const String& error = String::Handle(String::NewFormatted( | 122 const String& error = String::Handle(String::NewFormatted( |
| 110 "length (%"Pd") must be non-negative", length.Value())); | 123 "length (%"Pd") must be non-negative", length.Value())); |
| 111 const Array& args = Array::Handle(Array::New(1)); | 124 const Array& args = Array::Handle(Array::New(1)); |
| 112 args.SetAt(0, error); | 125 args.SetAt(0, error); |
| 113 Exceptions::ThrowByType(Exceptions::kArgument, args); | 126 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 114 } | 127 } |
| 115 if ((dst.IsTypedData() || dst.IsExternalTypedData()) && | 128 if (dst.IsTypedData()) { |
| 116 (dst.clazz() == src.clazz())) { | 129 if (src.IsTypedData()) { |
| 117 if (dst.IsTypedData()) { | 130 return CopyData<TypedData, TypedData>( |
| 118 ASSERT(src.IsTypedData()); | 131 dst, src, dst_start, src_start, length); |
| 119 COPY_DATA(TypedData, dst, src); | 132 } else if (src.IsExternalTypedData()) { |
| 120 } else { | 133 return CopyData<TypedData, ExternalTypedData>( |
| 121 ASSERT(src.IsExternalTypedData()); | 134 dst, src, dst_start, src_start, length); |
| 122 ASSERT(dst.IsExternalTypedData()); | |
| 123 COPY_DATA(ExternalTypedData, dst, src); | |
| 124 } | 135 } |
| 125 return Bool::True().raw(); | 136 } else if (dst.IsExternalTypedData()) { |
| 137 if (src.IsTypedData()) { | |
| 138 return CopyData<ExternalTypedData, TypedData>( | |
| 139 dst, src, dst_start, src_start, length); | |
| 140 } else if (src.IsExternalTypedData()) { | |
| 141 return CopyData<ExternalTypedData, ExternalTypedData>( | |
| 142 dst, src, dst_start, src_start, length); | |
| 143 } | |
| 126 } | 144 } |
| 127 return Bool::False().raw(); | 145 return Bool::False().raw(); |
| 128 } | 146 } |
| 129 | 147 |
| 130 | 148 |
| 131 // We check the length parameter against a possible maximum length for the | 149 // We check the length parameter against a possible maximum length for the |
| 132 // array based on available physical addressable memory on the system. The | 150 // array based on available physical addressable memory on the system. The |
| 133 // maximum possible length is a scaled value of kSmiMax which is set up based | 151 // maximum possible length is a scaled value of kSmiMax which is set up based |
| 134 // on whether the underlying architecture is 32-bit or 64-bit. | 152 // on whether the underlying architecture is 32-bit or 64-bit. |
| 135 #define TYPED_DATA_NEW(name) \ | 153 #define TYPED_DATA_NEW(name) \ |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 TYPED_DATA_NATIVES(Uint16Array, GetUint16, SetUint16, Smi, Value) | 319 TYPED_DATA_NATIVES(Uint16Array, GetUint16, SetUint16, Smi, Value) |
| 302 TYPED_DATA_NATIVES(Int32Array, GetInt32, SetInt32, Integer, AsInt64Value) | 320 TYPED_DATA_NATIVES(Int32Array, GetInt32, SetInt32, Integer, AsInt64Value) |
| 303 TYPED_DATA_NATIVES(Uint32Array, GetUint32, SetUint32, Integer, AsInt64Value) | 321 TYPED_DATA_NATIVES(Uint32Array, GetUint32, SetUint32, Integer, AsInt64Value) |
| 304 TYPED_DATA_NATIVES(Int64Array, GetInt64, SetInt64, Integer, AsInt64Value) | 322 TYPED_DATA_NATIVES(Int64Array, GetInt64, SetInt64, Integer, AsInt64Value) |
| 305 TYPED_DATA_UINT64_NATIVES(Uint64Array, GetUint64, SetUint64, Integer) | 323 TYPED_DATA_UINT64_NATIVES(Uint64Array, GetUint64, SetUint64, Integer) |
| 306 TYPED_DATA_NATIVES(Float32Array, GetFloat32, SetFloat32, Double, value) | 324 TYPED_DATA_NATIVES(Float32Array, GetFloat32, SetFloat32, Double, value) |
| 307 TYPED_DATA_NATIVES(Float64Array, GetFloat64, SetFloat64, Double, value) | 325 TYPED_DATA_NATIVES(Float64Array, GetFloat64, SetFloat64, Double, value) |
| 308 TYPED_DATA_NATIVES(Float32x4Array, GetFloat32x4, SetFloat32x4, Float32x4, value) | 326 TYPED_DATA_NATIVES(Float32x4Array, GetFloat32x4, SetFloat32x4, Float32x4, value) |
| 309 | 327 |
| 310 } // namespace dart | 328 } // namespace dart |
| OLD | NEW |