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/assembler.h" |
6 #include "vm/bootstrap_natives.h" | 7 #include "vm/bootstrap_natives.h" |
7 #include "vm/assembler.h" | |
8 #include "vm/exceptions.h" | 8 #include "vm/exceptions.h" |
9 #include "vm/native_entry.h" | 9 #include "vm/native_entry.h" |
10 #include "vm/object.h" | 10 #include "vm/object.h" |
11 | 11 |
12 namespace dart { | 12 namespace dart { |
13 | 13 |
14 DEFINE_NATIVE_ENTRY(List_allocate, 2) { | 14 DEFINE_NATIVE_ENTRY(List_allocate, 2) { |
15 // Implemented in FlowGraphBuilder::VisitNativeBody. | 15 // Implemented in FlowGraphBuilder::VisitNativeBody. |
16 UNREACHABLE(); | 16 UNREACHABLE(); |
17 return Object::null(); | 17 return Object::null(); |
18 } | 18 } |
19 | 19 |
20 | |
21 DEFINE_NATIVE_ENTRY(List_getIndexed, 2) { | 20 DEFINE_NATIVE_ENTRY(List_getIndexed, 2) { |
22 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 21 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
23 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); | 22 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); |
24 if ((index.Value() < 0) || (index.Value() >= array.Length())) { | 23 if ((index.Value() < 0) || (index.Value() >= array.Length())) { |
25 Exceptions::ThrowRangeError("index", index, 0, array.Length() - 1); | 24 Exceptions::ThrowRangeError("index", index, 0, array.Length() - 1); |
26 } | 25 } |
27 return array.At(index.Value()); | 26 return array.At(index.Value()); |
28 } | 27 } |
29 | 28 |
30 | |
31 DEFINE_NATIVE_ENTRY(List_setIndexed, 3) { | 29 DEFINE_NATIVE_ENTRY(List_setIndexed, 3) { |
32 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 30 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
33 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); | 31 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); |
34 const Instance& value = Instance::CheckedHandle(arguments->NativeArgAt(2)); | 32 const Instance& value = Instance::CheckedHandle(arguments->NativeArgAt(2)); |
35 if ((index.Value() < 0) || (index.Value() >= array.Length())) { | 33 if ((index.Value() < 0) || (index.Value() >= array.Length())) { |
36 Exceptions::ThrowRangeError("index", index, 0, array.Length() - 1); | 34 Exceptions::ThrowRangeError("index", index, 0, array.Length() - 1); |
37 } | 35 } |
38 array.SetAt(index.Value(), value); | 36 array.SetAt(index.Value(), value); |
39 return Object::null(); | 37 return Object::null(); |
40 } | 38 } |
41 | 39 |
42 | |
43 DEFINE_NATIVE_ENTRY(List_getLength, 1) { | 40 DEFINE_NATIVE_ENTRY(List_getLength, 1) { |
44 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 41 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
45 return Smi::New(array.Length()); | 42 return Smi::New(array.Length()); |
46 } | 43 } |
47 | 44 |
48 | |
49 // ObjectArray src, int start, int count, bool needTypeArgument. | 45 // ObjectArray src, int start, int count, bool needTypeArgument. |
50 DEFINE_NATIVE_ENTRY(List_slice, 4) { | 46 DEFINE_NATIVE_ENTRY(List_slice, 4) { |
51 const Array& src = Array::CheckedHandle(arguments->NativeArgAt(0)); | 47 const Array& src = Array::CheckedHandle(arguments->NativeArgAt(0)); |
52 GET_NON_NULL_NATIVE_ARGUMENT(Smi, start, arguments->NativeArgAt(1)); | 48 GET_NON_NULL_NATIVE_ARGUMENT(Smi, start, arguments->NativeArgAt(1)); |
53 GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(2)); | 49 GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(2)); |
54 GET_NON_NULL_NATIVE_ARGUMENT(Bool, needs_type_arg, arguments->NativeArgAt(3)); | 50 GET_NON_NULL_NATIVE_ARGUMENT(Bool, needs_type_arg, arguments->NativeArgAt(3)); |
55 intptr_t istart = start.Value(); | 51 intptr_t istart = start.Value(); |
56 if ((istart < 0) || (istart > src.Length())) { | 52 if ((istart < 0) || (istart > src.Length())) { |
57 Exceptions::ThrowRangeError("start", start, 0, src.Length()); | 53 Exceptions::ThrowRangeError("start", start, 0, src.Length()); |
58 } | 54 } |
59 intptr_t icount = count.Value(); | 55 intptr_t icount = count.Value(); |
60 // Zero count should be handled outside already. | 56 // Zero count should be handled outside already. |
61 if ((icount <= 0) || (icount > src.Length())) { | 57 if ((icount <= 0) || (icount > src.Length())) { |
62 Exceptions::ThrowRangeError("count", count, | 58 Exceptions::ThrowRangeError("count", count, |
63 0, // This is the limit the user sees. | 59 0, // This is the limit the user sees. |
64 src.Length() - istart); | 60 src.Length() - istart); |
65 } | 61 } |
66 | 62 |
67 return src.Slice(istart, icount, needs_type_arg.value()); | 63 return src.Slice(istart, icount, needs_type_arg.value()); |
68 } | 64 } |
69 | 65 |
70 | |
71 // Private factory, expects correct arguments. | 66 // Private factory, expects correct arguments. |
72 DEFINE_NATIVE_ENTRY(ImmutableList_from, 4) { | 67 DEFINE_NATIVE_ENTRY(ImmutableList_from, 4) { |
73 // Ignore first argument of a thsi factory (type argument). | 68 // Ignore first argument of a thsi factory (type argument). |
74 const Array& from_array = Array::CheckedHandle(arguments->NativeArgAt(1)); | 69 const Array& from_array = Array::CheckedHandle(arguments->NativeArgAt(1)); |
75 const Smi& smi_offset = Smi::CheckedHandle(arguments->NativeArgAt(2)); | 70 const Smi& smi_offset = Smi::CheckedHandle(arguments->NativeArgAt(2)); |
76 const Smi& smi_length = Smi::CheckedHandle(arguments->NativeArgAt(3)); | 71 const Smi& smi_length = Smi::CheckedHandle(arguments->NativeArgAt(3)); |
77 const intptr_t length = smi_length.Value(); | 72 const intptr_t length = smi_length.Value(); |
78 const intptr_t offset = smi_offset.Value(); | 73 const intptr_t offset = smi_offset.Value(); |
79 const Array& result = Array::Handle(Array::New(length)); | 74 const Array& result = Array::Handle(Array::New(length)); |
80 Object& temp = Object::Handle(); | 75 Object& temp = Object::Handle(); |
81 for (intptr_t i = 0; i < length; i++) { | 76 for (intptr_t i = 0; i < length; i++) { |
82 temp = from_array.At(i + offset); | 77 temp = from_array.At(i + offset); |
83 result.SetAt(i, temp); | 78 result.SetAt(i, temp); |
84 } | 79 } |
85 result.MakeImmutable(); | 80 result.MakeImmutable(); |
86 return result.raw(); | 81 return result.raw(); |
87 } | 82 } |
88 | 83 |
89 } // namespace dart | 84 } // namespace dart |
OLD | NEW |