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