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 "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/bootstrap_natives.h" | 6 #include "vm/bootstrap_natives.h" |
7 #include "vm/assembler.h" | 7 #include "vm/assembler.h" |
8 #include "vm/bigint_operations.h" | 8 #include "vm/bigint_operations.h" |
9 #include "vm/exceptions.h" | 9 #include "vm/exceptions.h" |
10 #include "vm/native_entry.h" | 10 #include "vm/native_entry.h" |
11 #include "vm/object.h" | 11 #include "vm/object.h" |
12 | 12 |
13 namespace dart { | 13 namespace dart { |
14 | 14 |
15 DEFINE_NATIVE_ENTRY(List_allocate, 2) { | 15 DEFINE_NATIVE_ENTRY(List_allocate, 2) { |
16 // Implemented in FlowGraphBuilder::VisitNativeBody. | 16 // Implemented in FlowGraphBuilder::VisitNativeBody. |
17 UNREACHABLE(); | 17 UNREACHABLE(); |
18 return Object::null(); | 18 return Object::null(); |
19 } | 19 } |
20 | 20 |
21 | 21 |
22 DEFINE_NATIVE_ENTRY(List_getIndexed, 2) { | 22 DEFINE_NATIVE_ENTRY(List_getIndexed, 2) { |
23 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 23 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
24 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); | 24 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); |
25 if ((index.Value() < 0) || (index.Value() >= array.Length())) { | 25 if ((index.Value() < 0) || (index.Value() >= array.Length())) { |
26 const Array& args = Array::Handle(Array::New(1)); | 26 Exceptions::ThrowRangeError("index", index, 0, array.Length()); |
27 args.SetAt(0, index); | |
28 Exceptions::ThrowByType(Exceptions::kRange, args); | |
29 } | 27 } |
30 return array.At(index.Value()); | 28 return array.At(index.Value()); |
31 } | 29 } |
32 | 30 |
33 | 31 |
34 DEFINE_NATIVE_ENTRY(List_setIndexed, 3) { | 32 DEFINE_NATIVE_ENTRY(List_setIndexed, 3) { |
35 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 33 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
36 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); | 34 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); |
37 const Instance& value = Instance::CheckedHandle(arguments->NativeArgAt(2)); | 35 const Instance& value = Instance::CheckedHandle(arguments->NativeArgAt(2)); |
38 if ((index.Value() < 0) || (index.Value() >= array.Length())) { | 36 if ((index.Value() < 0) || (index.Value() >= array.Length())) { |
39 const Array& args = Array::Handle(Array::New(1)); | 37 Exceptions::ThrowRangeError("index", index, 0, array.Length()); |
40 args.SetAt(0, index); | |
41 Exceptions::ThrowByType(Exceptions::kRange, args); | |
42 } | 38 } |
43 array.SetAt(index.Value(), value); | 39 array.SetAt(index.Value(), value); |
44 return Object::null(); | 40 return Object::null(); |
45 } | 41 } |
46 | 42 |
47 | 43 |
48 DEFINE_NATIVE_ENTRY(List_getLength, 1) { | 44 DEFINE_NATIVE_ENTRY(List_getLength, 1) { |
49 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 45 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
50 return Smi::New(array.Length()); | 46 return Smi::New(array.Length()); |
51 } | 47 } |
52 | 48 |
53 | 49 |
| 50 static void CheckRange(const char* name, |
| 51 intptr_t start, |
| 52 intptr_t count, |
| 53 intptr_t length) { |
| 54 if ((start < 0) || ((start + count) > length)) { |
| 55 if (count > length) { |
| 56 Exceptions::ThrowRangeError( |
| 57 "count", |
| 58 Smi::Handle(Smi::New(count)), |
| 59 0, |
| 60 length + 1); |
| 61 } else { |
| 62 Exceptions::ThrowRangeError( |
| 63 name, |
| 64 Smi::Handle(Smi::New(start)), |
| 65 0, |
| 66 length - count); |
| 67 } |
| 68 } |
| 69 } |
| 70 |
| 71 |
54 // ObjectArray src, int srcStart, int dstStart, int count. | 72 // ObjectArray src, int srcStart, int dstStart, int count. |
55 DEFINE_NATIVE_ENTRY(List_copyFromObjectArray, 5) { | 73 DEFINE_NATIVE_ENTRY(List_copyFromObjectArray, 5) { |
56 const Array& dest = Array::CheckedHandle(arguments->NativeArgAt(0)); | 74 const Array& dest = Array::CheckedHandle(arguments->NativeArgAt(0)); |
57 GET_NON_NULL_NATIVE_ARGUMENT(Array, source, arguments->NativeArgAt(1)); | 75 GET_NON_NULL_NATIVE_ARGUMENT(Array, source, arguments->NativeArgAt(1)); |
58 GET_NON_NULL_NATIVE_ARGUMENT(Smi, src_start, arguments->NativeArgAt(2)); | 76 GET_NON_NULL_NATIVE_ARGUMENT(Smi, src_start, arguments->NativeArgAt(2)); |
59 GET_NON_NULL_NATIVE_ARGUMENT(Smi, dst_start, arguments->NativeArgAt(3)); | 77 GET_NON_NULL_NATIVE_ARGUMENT(Smi, dst_start, arguments->NativeArgAt(3)); |
60 GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(4)); | 78 GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(4)); |
61 intptr_t icount = count.Value(); | 79 intptr_t icount = count.Value(); |
62 if (icount < 0) { | 80 if (icount < 0) { |
63 Exceptions::ThrowByType(Exceptions::kArgument, Object::empty_array()); | 81 Exceptions::ThrowByType(Exceptions::kArgument, Object::empty_array()); |
64 } | 82 } |
65 if (icount == 0) { | 83 if (icount == 0) { |
66 return Object::null(); | 84 return Object::null(); |
67 } | 85 } |
68 intptr_t isrc_start = src_start.Value(); | 86 intptr_t isrc_start = src_start.Value(); |
69 intptr_t idst_start = dst_start.Value(); | 87 intptr_t idst_start = dst_start.Value(); |
70 if ((isrc_start < 0) || ((isrc_start + icount) > source.Length())) { | 88 CheckRange("source", isrc_start, icount, source.Length()); |
71 const Array& args = Array::Handle(Array::New(1)); | 89 CheckRange("destination", idst_start, icount, dest.Length()); |
72 args.SetAt(0, src_start); | |
73 Exceptions::ThrowByType(Exceptions::kRange, args); | |
74 } | |
75 if ((idst_start < 0) || ((idst_start + icount) > dest.Length())) { | |
76 const Array& args = Array::Handle(Array::New(1)); | |
77 args.SetAt(0, dst_start); | |
78 Exceptions::ThrowByType(Exceptions::kRange, args); | |
79 } | |
80 | 90 |
81 Object& src_obj = Object::Handle(); | 91 PassiveObject& src_obj = PassiveObject::Handle(); |
82 if (isrc_start < idst_start) { | 92 if (isrc_start < idst_start) { |
83 for (intptr_t i = icount - 1; i >= 0; i--) { | 93 for (intptr_t i = icount - 1; i >= 0; i--) { |
84 src_obj = source.At(isrc_start + i); | 94 src_obj = source.At(isrc_start + i); |
85 dest.SetAt(idst_start + i, src_obj); | 95 dest.SetAt(idst_start + i, src_obj); |
86 } | 96 } |
87 } else { | 97 } else { |
88 for (intptr_t i = 0; i < icount; i++) { | 98 for (intptr_t i = 0; i < icount; i++) { |
89 src_obj = source.At(isrc_start + i); | 99 src_obj = source.At(isrc_start + i); |
90 dest.SetAt(idst_start + i, src_obj); | 100 dest.SetAt(idst_start + i, src_obj); |
91 } | 101 } |
92 } | 102 } |
| 103 |
93 return Object::null(); | 104 return Object::null(); |
94 } | 105 } |
95 | 106 |
96 | 107 |
97 // Private factory, expects correct arguments. | 108 // Private factory, expects correct arguments. |
98 DEFINE_NATIVE_ENTRY(ImmutableList_from, 4) { | 109 DEFINE_NATIVE_ENTRY(ImmutableList_from, 4) { |
99 // Ignore first argument of a thsi factory (type argument). | 110 // Ignore first argument of a thsi factory (type argument). |
100 const Array& from_array = Array::CheckedHandle(arguments->NativeArgAt(1)); | 111 const Array& from_array = Array::CheckedHandle(arguments->NativeArgAt(1)); |
101 const Smi& smi_offset = Smi::CheckedHandle(arguments->NativeArgAt(2)); | 112 const Smi& smi_offset = Smi::CheckedHandle(arguments->NativeArgAt(2)); |
102 const Smi& smi_length = Smi::CheckedHandle(arguments->NativeArgAt(3)); | 113 const Smi& smi_length = Smi::CheckedHandle(arguments->NativeArgAt(3)); |
103 const intptr_t length = smi_length.Value(); | 114 const intptr_t length = smi_length.Value(); |
104 const intptr_t offset = smi_offset.Value(); | 115 const intptr_t offset = smi_offset.Value(); |
105 const Array& result = Array::Handle(Array::New(length)); | 116 const Array& result = Array::Handle(Array::New(length)); |
106 Object& temp = Object::Handle(); | 117 Object& temp = Object::Handle(); |
107 for (intptr_t i = 0; i < length; i++) { | 118 for (intptr_t i = 0; i < length; i++) { |
108 temp = from_array.At(i + offset); | 119 temp = from_array.At(i + offset); |
109 result.SetAt(i, temp); | 120 result.SetAt(i, temp); |
110 } | 121 } |
111 result.MakeImmutable(); | 122 result.MakeImmutable(); |
112 return result.raw(); | 123 return result.raw(); |
113 } | 124 } |
114 | 125 |
115 } // namespace dart | 126 } // namespace dart |
OLD | NEW |