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 "vm/bootstrap_natives.h" | 5 #include "vm/bootstrap_natives.h" |
6 | 6 |
7 #include "vm/exceptions.h" | 7 #include "vm/exceptions.h" |
8 #include "vm/native_entry.h" | 8 #include "vm/native_entry.h" |
9 #include "vm/object.h" | 9 #include "vm/object.h" |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 GrowableArray<const Object*> args; | 23 GrowableArray<const Object*> args; |
24 args.Add(&length); | 24 args.Add(&length); |
25 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); | 25 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
26 } | 26 } |
27 const InternalByteArray& new_array = | 27 const InternalByteArray& new_array = |
28 InternalByteArray::Handle(InternalByteArray::New(length.Value())); | 28 InternalByteArray::Handle(InternalByteArray::New(length.Value())); |
29 arguments->SetReturn(new_array); | 29 arguments->SetReturn(new_array); |
30 } | 30 } |
31 | 31 |
32 | 32 |
33 static void RangeCheck(const ByteArray& array, const Smi& index, | 33 static void RangeCheck(const ByteArray& array, |
| 34 intptr_t index, |
34 intptr_t num_bytes) { | 35 intptr_t num_bytes) { |
35 if ((index.Value() < 0) || ((index.Value() + num_bytes) > array.Length())) { | 36 if (!Utils::RangeCheck(index, num_bytes, array.Length())) { |
36 GrowableArray<const Object*> arguments; | 37 GrowableArray<const Object*> arguments; |
37 arguments.Add(&index); | 38 const Smi &index_object = Smi::Handle(Smi::New(index)); |
| 39 arguments.Add(&index_object); |
38 Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments); | 40 Exceptions::ThrowByType(Exceptions::kIndexOutOfRange, arguments); |
39 } | 41 } |
40 } | 42 } |
41 | 43 |
42 | 44 |
| 45 DEFINE_NATIVE_ENTRY(ByteArray_setRange, 5) { |
| 46 ByteArray& dst = ByteArray::CheckedHandle(arguments->At(0)); |
| 47 GET_NATIVE_ARGUMENT(Smi, dst_start, arguments->At(1)); |
| 48 GET_NATIVE_ARGUMENT(Smi, length, arguments->At(2)); |
| 49 GET_NATIVE_ARGUMENT(ByteArray, src, arguments->At(3)); |
| 50 GET_NATIVE_ARGUMENT(Smi, src_start, arguments->At(4)); |
| 51 intptr_t length_value = length.Value(); |
| 52 intptr_t src_start_value = src_start.Value(); |
| 53 intptr_t dst_start_value = dst_start.Value(); |
| 54 if (length_value < 0) { |
| 55 GrowableArray<const Object*> args; |
| 56 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
| 57 } |
| 58 RangeCheck(src, src_start_value, length_value); |
| 59 RangeCheck(dst, dst_start_value, length_value); |
| 60 ByteArray::Copy(dst, dst_start_value, src, src_start_value, length_value); |
| 61 } |
| 62 |
| 63 |
43 #define GETTER(ArrayT, ObjectT, ValueT) \ | 64 #define GETTER(ArrayT, ObjectT, ValueT) \ |
44 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->At(0)); \ | 65 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->At(0)); \ |
45 GET_NATIVE_ARGUMENT(Smi, index, arguments->At(1)); \ | 66 GET_NATIVE_ARGUMENT(Smi, index, arguments->At(1)); \ |
46 RangeCheck(array, index, sizeof(ValueT)); \ | 67 RangeCheck(array, index.Value(), sizeof(ValueT)); \ |
47 ValueT result = array.UnalignedAt<ValueT>(index.Value()); \ | 68 ValueT result = array.UnalignedAt<ValueT>(index.Value()); \ |
48 arguments->SetReturn(ObjectT::Handle(ObjectT::New(result))); | 69 arguments->SetReturn(ObjectT::Handle(ObjectT::New(result))); |
49 | 70 |
50 | 71 |
51 #define SETTER(ArrayT, ObjectT, Getter, ValueT) \ | 72 #define SETTER(ArrayT, ObjectT, Getter, ValueT) \ |
52 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->At(0)); \ | 73 GET_NATIVE_ARGUMENT(ArrayT, array, arguments->At(0)); \ |
53 GET_NATIVE_ARGUMENT(Smi, index, arguments->At(1)); \ | 74 GET_NATIVE_ARGUMENT(Smi, index, arguments->At(1)); \ |
54 RangeCheck(array, index, sizeof(ValueT)); \ | 75 RangeCheck(array, index.Value(), sizeof(ValueT)); \ |
55 GET_NATIVE_ARGUMENT(ObjectT, value, arguments->At(2)); \ | 76 GET_NATIVE_ARGUMENT(ObjectT, value, arguments->At(2)); \ |
56 array.SetUnalignedAt<ValueT>(index.Value(), value.Getter()); | 77 array.SetUnalignedAt<ValueT>(index.Value(), value.Getter()); |
57 | 78 |
58 | 79 |
59 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt8, 2) { | 80 DEFINE_NATIVE_ENTRY(InternalByteArray_getInt8, 2) { |
60 GETTER(InternalByteArray, Smi, int8_t); | 81 GETTER(InternalByteArray, Smi, int8_t); |
61 } | 82 } |
62 | 83 |
63 | 84 |
64 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt8, 3) { | 85 DEFINE_NATIVE_ENTRY(InternalByteArray_setInt8, 3) { |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 DEFINE_NATIVE_ENTRY(ExternalByteArray_getFloat64, 2) { | 270 DEFINE_NATIVE_ENTRY(ExternalByteArray_getFloat64, 2) { |
250 GETTER(ExternalByteArray, Double, double); | 271 GETTER(ExternalByteArray, Double, double); |
251 } | 272 } |
252 | 273 |
253 | 274 |
254 DEFINE_NATIVE_ENTRY(ExternalByteArray_setFloat64, 3) { | 275 DEFINE_NATIVE_ENTRY(ExternalByteArray_setFloat64, 3) { |
255 SETTER(ExternalByteArray, Double, value, double); | 276 SETTER(ExternalByteArray, Double, value, double); |
256 } | 277 } |
257 | 278 |
258 } // namespace dart | 279 } // namespace dart |
OLD | NEW |