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