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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 |
| 82 #define COPY_DATA(type, dst, src) \ | 82 #define COPY_DATA_AND_RETURN(dsttype, dst, srctype, src) \ |
|
Vyacheslav Egorov (Google)
2013/04/16 14:22:23
I'd prefer good old template to this huge macro.
| |
| 83 const type& dst_array = type::Cast(dst); \ | 83 const dsttype& dst_array = dsttype::Cast(dst); \ |
| 84 const type& src_array = type::Cast(src); \ | 84 const srctype& src_array = srctype::Cast(src); \ |
| 85 intptr_t element_size_in_bytes = dst_array.ElementSizeInBytes(); \ | 85 intptr_t dst_element_size_in_bytes = dst_array.ElementSizeInBytes(); \ |
| 86 intptr_t length_in_bytes = length.Value() * element_size_in_bytes; \ | 86 intptr_t src_element_size_in_bytes = src_array.ElementSizeInBytes(); \ |
| 87 intptr_t src_offset_in_bytes = src_start.Value() * element_size_in_bytes; \ | 87 intptr_t dst_offset_in_bytes = dst_start.Value() * dst_element_size_in_bytes;\ |
| 88 intptr_t dst_offset_in_bytes = dst_start.Value() * element_size_in_bytes; \ | 88 intptr_t src_offset_in_bytes = src_start.Value() * src_element_size_in_bytes;\ |
| 89 intptr_t length_in_bytes = length.Value() * src_element_size_in_bytes; \ | |
| 90 if (src_element_size_in_bytes != dst_element_size_in_bytes) { \ | |
| 91 return Bool::False().raw(); \ | |
| 92 } \ | |
| 89 SetRangeCheck(src_offset_in_bytes, \ | 93 SetRangeCheck(src_offset_in_bytes, \ |
| 90 length_in_bytes, \ | 94 length_in_bytes, \ |
| 91 src_array.LengthInBytes(), \ | 95 src_array.LengthInBytes(), \ |
| 92 element_size_in_bytes); \ | 96 src_element_size_in_bytes); \ |
| 93 SetRangeCheck(dst_offset_in_bytes, \ | 97 SetRangeCheck(dst_offset_in_bytes, \ |
| 94 length_in_bytes, \ | 98 length_in_bytes, \ |
| 95 dst_array.LengthInBytes(), \ | 99 dst_array.LengthInBytes(), \ |
| 96 element_size_in_bytes); \ | 100 dst_element_size_in_bytes); \ |
| 97 type::Copy(dst_array, dst_offset_in_bytes, \ | 101 { \ |
| 98 src_array, src_offset_in_bytes, \ | 102 NoGCScope no_gc; \ |
| 99 length_in_bytes); | 103 if (length_in_bytes > 0) { \ |
| 104 memmove(dst_array.DataAddr(dst_offset_in_bytes), \ | |
| 105 src_array.DataAddr(src_offset_in_bytes), \ | |
| 106 length_in_bytes); \ | |
| 107 } \ | |
| 108 return Bool::True().raw(); \ | |
| 109 } | |
| 100 | 110 |
| 101 DEFINE_NATIVE_ENTRY(TypedData_setRange, 5) { | 111 DEFINE_NATIVE_ENTRY(TypedData_setRange, 5) { |
| 102 GET_NON_NULL_NATIVE_ARGUMENT(Instance, dst, arguments->NativeArgAt(0)); | 112 GET_NON_NULL_NATIVE_ARGUMENT(Instance, dst, arguments->NativeArgAt(0)); |
| 103 GET_NON_NULL_NATIVE_ARGUMENT(Smi, dst_start, arguments->NativeArgAt(1)); | 113 GET_NON_NULL_NATIVE_ARGUMENT(Smi, dst_start, arguments->NativeArgAt(1)); |
| 104 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(2)); | 114 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(2)); |
| 105 GET_NON_NULL_NATIVE_ARGUMENT(Instance, src, arguments->NativeArgAt(3)); | 115 GET_NON_NULL_NATIVE_ARGUMENT(Instance, src, arguments->NativeArgAt(3)); |
| 106 GET_NON_NULL_NATIVE_ARGUMENT(Smi, src_start, arguments->NativeArgAt(4)); | 116 GET_NON_NULL_NATIVE_ARGUMENT(Smi, src_start, arguments->NativeArgAt(4)); |
| 107 | 117 |
| 108 if (length.Value() < 0) { | 118 if (length.Value() < 0) { |
| 109 const String& error = String::Handle(String::NewFormatted( | 119 const String& error = String::Handle(String::NewFormatted( |
| 110 "length (%"Pd") must be non-negative", length.Value())); | 120 "length (%"Pd") must be non-negative", length.Value())); |
| 111 const Array& args = Array::Handle(Array::New(1)); | 121 const Array& args = Array::Handle(Array::New(1)); |
| 112 args.SetAt(0, error); | 122 args.SetAt(0, error); |
| 113 Exceptions::ThrowByType(Exceptions::kArgument, args); | 123 Exceptions::ThrowByType(Exceptions::kArgument, args); |
| 114 } | 124 } |
| 115 if ((dst.IsTypedData() || dst.IsExternalTypedData()) && | 125 if (dst.IsTypedData()) { |
| 116 (dst.clazz() == src.clazz())) { | 126 if (src.IsTypedData()) { |
| 117 if (dst.IsTypedData()) { | 127 COPY_DATA_AND_RETURN(TypedData, dst, TypedData, src); |
| 118 ASSERT(src.IsTypedData()); | 128 } else if (src.IsExternalTypedData()) { |
| 119 COPY_DATA(TypedData, dst, src); | 129 COPY_DATA_AND_RETURN(TypedData, dst, ExternalTypedData, src); |
| 120 } else { | |
| 121 ASSERT(src.IsExternalTypedData()); | |
| 122 ASSERT(dst.IsExternalTypedData()); | |
| 123 COPY_DATA(ExternalTypedData, dst, src); | |
| 124 } | 130 } |
| 125 return Bool::True().raw(); | 131 } else if (dst.IsExternalTypedData()) { |
| 132 if (src.IsTypedData()) { | |
| 133 COPY_DATA_AND_RETURN(ExternalTypedData, dst, TypedData, src); | |
| 134 } else if (src.IsExternalTypedData()) { | |
| 135 COPY_DATA_AND_RETURN(ExternalTypedData, dst, ExternalTypedData, src); | |
| 136 } | |
| 126 } | 137 } |
| 127 return Bool::False().raw(); | 138 return Bool::False().raw(); |
| 128 } | 139 } |
| 129 | 140 |
| 130 | 141 |
| 131 // We check the length parameter against a possible maximum length for the | 142 // We check the length parameter against a possible maximum length for the |
| 132 // array based on available physical addressable memory on the system. The | 143 // 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 | 144 // 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. | 145 // on whether the underlying architecture is 32-bit or 64-bit. |
| 135 #define TYPED_DATA_NEW(name) \ | 146 #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) | 312 TYPED_DATA_NATIVES(Uint16Array, GetUint16, SetUint16, Smi, Value) |
| 302 TYPED_DATA_NATIVES(Int32Array, GetInt32, SetInt32, Integer, AsInt64Value) | 313 TYPED_DATA_NATIVES(Int32Array, GetInt32, SetInt32, Integer, AsInt64Value) |
| 303 TYPED_DATA_NATIVES(Uint32Array, GetUint32, SetUint32, Integer, AsInt64Value) | 314 TYPED_DATA_NATIVES(Uint32Array, GetUint32, SetUint32, Integer, AsInt64Value) |
| 304 TYPED_DATA_NATIVES(Int64Array, GetInt64, SetInt64, Integer, AsInt64Value) | 315 TYPED_DATA_NATIVES(Int64Array, GetInt64, SetInt64, Integer, AsInt64Value) |
| 305 TYPED_DATA_UINT64_NATIVES(Uint64Array, GetUint64, SetUint64, Integer) | 316 TYPED_DATA_UINT64_NATIVES(Uint64Array, GetUint64, SetUint64, Integer) |
| 306 TYPED_DATA_NATIVES(Float32Array, GetFloat32, SetFloat32, Double, value) | 317 TYPED_DATA_NATIVES(Float32Array, GetFloat32, SetFloat32, Double, value) |
| 307 TYPED_DATA_NATIVES(Float64Array, GetFloat64, SetFloat64, Double, value) | 318 TYPED_DATA_NATIVES(Float64Array, GetFloat64, SetFloat64, Double, value) |
| 308 TYPED_DATA_NATIVES(Float32x4Array, GetFloat32x4, SetFloat32x4, Float32x4, value) | 319 TYPED_DATA_NATIVES(Float32x4Array, GetFloat32x4, SetFloat32x4, Float32x4, value) |
| 309 | 320 |
| 310 } // namespace dart | 321 } // namespace dart |
| OLD | NEW |