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 | |
7 | #include "vm/bootstrap_natives.h" |
8 | #include "vm/exceptions.h" |
9 | #include "vm/native_entry.h" |
10 | #include "vm/object.h" |
11 | |
12 | namespace dart { |
13 | |
14 | DEFINE_NATIVE_ENTRY(GrowableList_allocate, 0, 2) { |
15 | const TypeArguments& type_arguments = |
16 | TypeArguments::CheckedHandle(zone, arguments->NativeArgAt(0)); |
17 | GET_NON_NULL_NATIVE_ARGUMENT(Array, data, arguments->NativeArgAt(1)); |
18 | if (data.Length() < 0) { |
19 | Exceptions::ThrowRangeError("length" , |
20 | Integer::Handle(Integer::New(data.Length())), |
21 | 0, // This is the limit the user sees. |
22 | Array::kMaxElements); |
23 | } |
24 | const GrowableObjectArray& new_array = |
25 | GrowableObjectArray::Handle(GrowableObjectArray::New(data)); |
26 | new_array.SetTypeArguments(type_arguments); |
27 | return new_array.raw(); |
28 | } |
29 | |
30 | DEFINE_NATIVE_ENTRY(GrowableList_getIndexed, 0, 2) { |
31 | const GrowableObjectArray& array = |
32 | GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0)); |
33 | GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); |
34 | if ((index.Value() < 0) || (index.Value() >= array.Length())) { |
35 | Exceptions::ThrowRangeError("index" , index, 0, array.Length() - 1); |
36 | } |
37 | const Instance& obj = Instance::CheckedHandle(zone, array.At(index.Value())); |
38 | return obj.raw(); |
39 | } |
40 | |
41 | DEFINE_NATIVE_ENTRY(GrowableList_setIndexed, 0, 3) { |
42 | const GrowableObjectArray& array = |
43 | GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0)); |
44 | GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); |
45 | if ((index.Value() < 0) || (index.Value() >= array.Length())) { |
46 | Exceptions::ThrowRangeError("index" , index, 0, array.Length() - 1); |
47 | } |
48 | GET_NON_NULL_NATIVE_ARGUMENT(Instance, value, arguments->NativeArgAt(2)); |
49 | array.SetAt(index.Value(), value); |
50 | return Object::null(); |
51 | } |
52 | |
53 | DEFINE_NATIVE_ENTRY(GrowableList_getLength, 0, 1) { |
54 | const GrowableObjectArray& array = |
55 | GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0)); |
56 | return Smi::New(array.Length()); |
57 | } |
58 | |
59 | DEFINE_NATIVE_ENTRY(GrowableList_getCapacity, 0, 1) { |
60 | const GrowableObjectArray& array = |
61 | GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0)); |
62 | return Smi::New(array.Capacity()); |
63 | } |
64 | |
65 | DEFINE_NATIVE_ENTRY(GrowableList_setLength, 0, 2) { |
66 | const GrowableObjectArray& array = |
67 | GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0)); |
68 | GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(1)); |
69 | ASSERT((length.Value() >= 0) && (length.Value() <= array.Capacity())); |
70 | array.SetLength(length.Value()); |
71 | return Object::null(); |
72 | } |
73 | |
74 | DEFINE_NATIVE_ENTRY(GrowableList_setData, 0, 2) { |
75 | const GrowableObjectArray& array = |
76 | GrowableObjectArray::CheckedHandle(zone, arguments->NativeArgAt(0)); |
77 | GET_NON_NULL_NATIVE_ARGUMENT(Array, data, arguments->NativeArgAt(1)); |
78 | ASSERT(data.Length() >= 0); |
79 | array.SetData(data); |
80 | return Object::null(); |
81 | } |
82 | |
83 | DEFINE_NATIVE_ENTRY(Internal_makeListFixedLength, 0, 1) { |
84 | GET_NON_NULL_NATIVE_ARGUMENT(GrowableObjectArray, array, |
85 | arguments->NativeArgAt(0)); |
86 | return Array::MakeFixedLength(array, /* unique = */ true); |
87 | } |
88 | |
89 | DEFINE_NATIVE_ENTRY(Internal_makeFixedListUnmodifiable, 0, 1) { |
90 | GET_NON_NULL_NATIVE_ARGUMENT(Array, array, arguments->NativeArgAt(0)); |
91 | array.MakeImmutable(); |
92 | return array.raw(); |
93 | } |
94 | |
95 | } // namespace dart |
96 | |