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/bigint_operations.h" | 8 #include "vm/bigint_operations.h" |
9 #include "vm/exceptions.h" | 9 #include "vm/exceptions.h" |
10 #include "vm/native_entry.h" | 10 #include "vm/native_entry.h" |
11 #include "vm/object.h" | 11 #include "vm/object.h" |
12 | 12 |
13 namespace dart { | 13 namespace dart { |
14 | 14 |
15 DEFINE_NATIVE_ENTRY(List_allocate, 2) { | 15 DEFINE_NATIVE_ENTRY(List_allocate, 2) { |
16 // Implemented in FlowGraphBuilder::VisitNativeBody. | 16 // Implemented in FlowGraphBuilder::VisitNativeBody. |
17 UNREACHABLE(); | 17 UNREACHABLE(); |
18 return Object::null(); | 18 return Object::null(); |
19 } | 19 } |
20 | 20 |
21 | 21 |
22 DEFINE_NATIVE_ENTRY(List_getIndexed, 2) { | 22 DEFINE_NATIVE_ENTRY(List_getIndexed, 2) { |
23 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 23 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
24 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); | 24 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); |
25 if ((index.Value() < 0) || (index.Value() >= array.Length())) { | 25 if ((index.Value() < 0) || (index.Value() >= array.Length())) { |
26 const Array& args = Array::Handle(Array::New(1)); | 26 Exceptions::ThrowRangeError(index); |
27 args.SetAt(0, index); | |
28 Exceptions::ThrowByType(Exceptions::kRange, args); | |
29 } | 27 } |
30 return array.At(index.Value()); | 28 return array.At(index.Value()); |
31 } | 29 } |
32 | 30 |
33 | 31 |
34 DEFINE_NATIVE_ENTRY(List_setIndexed, 3) { | 32 DEFINE_NATIVE_ENTRY(List_setIndexed, 3) { |
35 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 33 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
36 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); | 34 GET_NON_NULL_NATIVE_ARGUMENT(Smi, index, arguments->NativeArgAt(1)); |
37 const Instance& value = Instance::CheckedHandle(arguments->NativeArgAt(2)); | 35 const Instance& value = Instance::CheckedHandle(arguments->NativeArgAt(2)); |
38 if ((index.Value() < 0) || (index.Value() >= array.Length())) { | 36 if ((index.Value() < 0) || (index.Value() >= array.Length())) { |
39 const Array& args = Array::Handle(Array::New(1)); | 37 Exceptions::ThrowRangeError(index); |
40 args.SetAt(0, index); | |
41 Exceptions::ThrowByType(Exceptions::kRange, args); | |
42 } | 38 } |
43 array.SetAt(index.Value(), value); | 39 array.SetAt(index.Value(), value); |
44 return Object::null(); | 40 return Object::null(); |
45 } | 41 } |
46 | 42 |
47 | 43 |
48 DEFINE_NATIVE_ENTRY(List_getLength, 1) { | 44 DEFINE_NATIVE_ENTRY(List_getLength, 1) { |
49 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); | 45 const Array& array = Array::CheckedHandle(arguments->NativeArgAt(0)); |
50 return Smi::New(array.Length()); | 46 return Smi::New(array.Length()); |
51 } | 47 } |
52 | 48 |
53 | 49 |
54 // ObjectArray src, int srcStart, int dstStart, int count. | 50 // ObjectArray src, int srcStart, int dstStart, int count. |
55 DEFINE_NATIVE_ENTRY(List_copyFromObjectArray, 5) { | 51 DEFINE_NATIVE_ENTRY(List_copyFromObjectArray, 5) { |
56 const Array& dest = Array::CheckedHandle(arguments->NativeArgAt(0)); | 52 const Array& dest = Array::CheckedHandle(arguments->NativeArgAt(0)); |
57 GET_NON_NULL_NATIVE_ARGUMENT(Array, source, arguments->NativeArgAt(1)); | 53 GET_NON_NULL_NATIVE_ARGUMENT(Array, source, arguments->NativeArgAt(1)); |
58 GET_NON_NULL_NATIVE_ARGUMENT(Smi, src_start, arguments->NativeArgAt(2)); | 54 GET_NON_NULL_NATIVE_ARGUMENT(Smi, src_start, arguments->NativeArgAt(2)); |
59 GET_NON_NULL_NATIVE_ARGUMENT(Smi, dst_start, arguments->NativeArgAt(3)); | 55 GET_NON_NULL_NATIVE_ARGUMENT(Smi, dst_start, arguments->NativeArgAt(3)); |
60 GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(4)); | 56 GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(4)); |
61 intptr_t icount = count.Value(); | 57 dest.CopyFrom(dst_start.Value(), source, src_start.Value(), count.Value()); |
62 if (icount < 0) { | |
63 Exceptions::ThrowByType(Exceptions::kArgument, Object::empty_array()); | |
64 } | |
65 if (icount == 0) { | |
66 return Object::null(); | |
67 } | |
68 intptr_t isrc_start = src_start.Value(); | |
69 intptr_t idst_start = dst_start.Value(); | |
70 if ((isrc_start < 0) || ((isrc_start + icount) > source.Length())) { | |
71 const Array& args = Array::Handle(Array::New(1)); | |
72 args.SetAt(0, src_start); | |
73 Exceptions::ThrowByType(Exceptions::kRange, args); | |
74 } | |
75 if ((idst_start < 0) || ((idst_start + icount) > dest.Length())) { | |
76 const Array& args = Array::Handle(Array::New(1)); | |
77 args.SetAt(0, dst_start); | |
78 Exceptions::ThrowByType(Exceptions::kRange, args); | |
79 } | |
80 | |
81 Object& src_obj = Object::Handle(); | |
Ivan Posva
2014/09/02 15:27:07
PassiveObject& src_obj = ...
Since we do not need
Vyacheslav Egorov (Google)
2014/09/02 16:02:53
PassiveObject& brings around 1.5x-2x improvement.
| |
82 if (isrc_start < idst_start) { | |
83 for (intptr_t i = icount - 1; i >= 0; i--) { | |
84 src_obj = source.At(isrc_start + i); | |
85 dest.SetAt(idst_start + i, src_obj); | |
86 } | |
87 } else { | |
88 for (intptr_t i = 0; i < icount; i++) { | |
89 src_obj = source.At(isrc_start + i); | |
90 dest.SetAt(idst_start + i, src_obj); | |
91 } | |
92 } | |
93 return Object::null(); | 58 return Object::null(); |
94 } | 59 } |
95 | 60 |
96 | 61 |
97 // Private factory, expects correct arguments. | 62 // Private factory, expects correct arguments. |
98 DEFINE_NATIVE_ENTRY(ImmutableList_from, 4) { | 63 DEFINE_NATIVE_ENTRY(ImmutableList_from, 4) { |
99 // Ignore first argument of a thsi factory (type argument). | 64 // Ignore first argument of a thsi factory (type argument). |
100 const Array& from_array = Array::CheckedHandle(arguments->NativeArgAt(1)); | 65 const Array& from_array = Array::CheckedHandle(arguments->NativeArgAt(1)); |
101 const Smi& smi_offset = Smi::CheckedHandle(arguments->NativeArgAt(2)); | 66 const Smi& smi_offset = Smi::CheckedHandle(arguments->NativeArgAt(2)); |
102 const Smi& smi_length = Smi::CheckedHandle(arguments->NativeArgAt(3)); | 67 const Smi& smi_length = Smi::CheckedHandle(arguments->NativeArgAt(3)); |
103 const intptr_t length = smi_length.Value(); | 68 const intptr_t length = smi_length.Value(); |
104 const intptr_t offset = smi_offset.Value(); | 69 const intptr_t offset = smi_offset.Value(); |
105 const Array& result = Array::Handle(Array::New(length)); | 70 const Array& result = Array::Handle(Array::New(length)); |
106 Object& temp = Object::Handle(); | 71 Object& temp = Object::Handle(); |
107 for (intptr_t i = 0; i < length; i++) { | 72 for (intptr_t i = 0; i < length; i++) { |
108 temp = from_array.At(i + offset); | 73 temp = from_array.At(i + offset); |
109 result.SetAt(i, temp); | 74 result.SetAt(i, temp); |
110 } | 75 } |
111 result.MakeImmutable(); | 76 result.MakeImmutable(); |
112 return result.raw(); | 77 return result.raw(); |
113 } | 78 } |
114 | 79 |
115 } // namespace dart | 80 } // namespace dart |
OLD | NEW |