Chromium Code Reviews| 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/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 | 20 |
| 21 DEFINE_NATIVE_ENTRY(List_getIndexed, 2) { | 21 DEFINE_NATIVE_ENTRY(List_getIndexed, 2) { |
| 22 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 22 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
| 23 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); | 23 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); |
| 24 if ((index.Value() < 0) || (index.Value() >= array.Length())) { | 24 if ((index.Value() < 0) || (index.Value() >= array.Length())) { |
| 25 Exceptions::ThrowRangeError("index", index, 0, array.Length()); | 25 Exceptions::ThrowRangeError("index", index, 0, array.Length() - 1); |
| 26 } | 26 } |
| 27 return array.At(index.Value()); | 27 return array.At(index.Value()); |
| 28 } | 28 } |
| 29 | 29 |
| 30 | 30 |
| 31 DEFINE_NATIVE_ENTRY(List_setIndexed, 3) { | 31 DEFINE_NATIVE_ENTRY(List_setIndexed, 3) { |
| 32 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 32 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
| 33 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); | 33 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); |
| 34 const Instance& value = Instance::CheckedHandle(arguments->NativeArgAt(2)); | 34 const Instance& value = Instance::CheckedHandle(arguments->NativeArgAt(2)); |
| 35 if ((index.Value() < 0) || (index.Value() >= array.Length())) { | 35 if ((index.Value() < 0) || (index.Value() >= array.Length())) { |
| 36 Exceptions::ThrowRangeError("index", index, 0, array.Length()); | 36 Exceptions::ThrowRangeError("index", index, 0, array.Length() - 1); |
| 37 } | 37 } |
| 38 array.SetAt(index.Value(), value); | 38 array.SetAt(index.Value(), value); |
| 39 return Object::null(); | 39 return Object::null(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 | 42 |
| 43 DEFINE_NATIVE_ENTRY(List_getLength, 1) { | 43 DEFINE_NATIVE_ENTRY(List_getLength, 1) { |
| 44 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 44 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
| 45 return Smi::New(array.Length()); | 45 return Smi::New(array.Length()); |
| 46 } | 46 } |
| 47 | 47 |
| 48 | 48 |
| 49 // ObjectArray src, int start, int count, bool needTypeArgument. | 49 // ObjectArray src, int start, int count, bool needTypeArgument. |
| 50 DEFINE_NATIVE_ENTRY(List_slice, 4) { | 50 DEFINE_NATIVE_ENTRY(List_slice, 4) { |
| 51 const Array& src = Array::CheckedHandle(arguments->NativeArgAt(0)); | 51 const Array& src = Array::CheckedHandle(arguments->NativeArgAt(0)); |
| 52 GET_NON_NULL_NATIVE_ARGUMENT(Smi, start, arguments->NativeArgAt(1)); | 52 GET_NON_NULL_NATIVE_ARGUMENT(Smi, start, arguments->NativeArgAt(1)); |
| 53 GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(2)); | 53 GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(2)); |
| 54 GET_NON_NULL_NATIVE_ARGUMENT(Bool, needs_type_arg, arguments->NativeArgAt(3)); | 54 GET_NON_NULL_NATIVE_ARGUMENT(Bool, needs_type_arg, arguments->NativeArgAt(3)); |
| 55 intptr_t istart = start.Value(); | |
| 56 if ((istart < 0) || (istart > src.Length())) { | |
| 57 Exceptions::ThrowRangeError( | |
| 58 "start", | |
| 59 Smi::Handle(Smi::New(istart)), | |
|
Ivan Posva
2015/09/11 09:12:50
start
| |
| 60 0, | |
| 61 src.Length()); | |
| 62 } | |
| 55 intptr_t icount = count.Value(); | 63 intptr_t icount = count.Value(); |
| 56 // Zero count should be handled outside already. | 64 // Zero count should be handled outside already. |
| 57 if ((icount <= 0) || (icount > src.Length())) { | 65 if ((icount <= 0) || (icount > src.Length())) { |
| 58 Exceptions::ThrowRangeError( | 66 Exceptions::ThrowRangeError( |
| 59 "count", | 67 "count", |
| 60 Smi::Handle(Smi::New(icount)), | 68 Smi::Handle(Smi::New(icount)), |
|
Ivan Posva
2015/09/11 09:12:50
count
| |
| 61 1, | 69 0, // This is the limit the user sees. |
| 62 src.Length() + 1); | 70 src.Length() - istart); |
|
srdjan
2015/09/25 20:32:17
Shouldn't this be "src.Length()" only.
Lasse Reichstein Nielsen
2015/09/25 23:28:22
Probably. Maybe it would be simpler to pass `end`
Lasse Reichstein Nielsen
2015/09/25 23:29:15
... but that would mean changing "count" above to
| |
| 63 } | |
| 64 intptr_t istart = start.Value(); | |
| 65 if ((istart < 0) || ((istart + icount) > src.Length())) { | |
| 66 Exceptions::ThrowRangeError( | |
| 67 "start", | |
| 68 Smi::Handle(Smi::New(istart)), | |
| 69 0, | |
| 70 src.Length() - icount + 1); | |
| 71 } | 71 } |
| 72 | 72 |
| 73 return src.Slice(istart, icount, needs_type_arg.value()); | 73 return src.Slice(istart, icount, needs_type_arg.value()); |
| 74 } | 74 } |
| 75 | 75 |
| 76 | 76 |
| 77 // Private factory, expects correct arguments. | 77 // Private factory, expects correct arguments. |
| 78 DEFINE_NATIVE_ENTRY(ImmutableList_from, 4) { | 78 DEFINE_NATIVE_ENTRY(ImmutableList_from, 4) { |
| 79 // Ignore first argument of a thsi factory (type argument). | 79 // Ignore first argument of a thsi factory (type argument). |
| 80 const Array& from_array = Array::CheckedHandle(arguments->NativeArgAt(1)); | 80 const Array& from_array = Array::CheckedHandle(arguments->NativeArgAt(1)); |
| 81 const Smi& smi_offset = Smi::CheckedHandle(arguments->NativeArgAt(2)); | 81 const Smi& smi_offset = Smi::CheckedHandle(arguments->NativeArgAt(2)); |
| 82 const Smi& smi_length = Smi::CheckedHandle(arguments->NativeArgAt(3)); | 82 const Smi& smi_length = Smi::CheckedHandle(arguments->NativeArgAt(3)); |
| 83 const intptr_t length = smi_length.Value(); | 83 const intptr_t length = smi_length.Value(); |
| 84 const intptr_t offset = smi_offset.Value(); | 84 const intptr_t offset = smi_offset.Value(); |
| 85 const Array& result = Array::Handle(Array::New(length)); | 85 const Array& result = Array::Handle(Array::New(length)); |
| 86 Object& temp = Object::Handle(); | 86 Object& temp = Object::Handle(); |
| 87 for (intptr_t i = 0; i < length; i++) { | 87 for (intptr_t i = 0; i < length; i++) { |
| 88 temp = from_array.At(i + offset); | 88 temp = from_array.At(i + offset); |
| 89 result.SetAt(i, temp); | 89 result.SetAt(i, temp); |
| 90 } | 90 } |
| 91 result.MakeImmutable(); | 91 result.MakeImmutable(); |
| 92 return result.raw(); | 92 return result.raw(); |
| 93 } | 93 } |
| 94 | 94 |
| 95 } // namespace dart | 95 } // namespace dart |
| OLD | NEW |