Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: runtime/vm/code_generator.cc

Issue 1214723009: Make List constructor give better error messages for non-int arguments (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Fix test to also pass on dart2js. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | runtime/vm/exceptions.h » ('j') | sdk/lib/_internal/js_runtime/lib/js_array.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 "vm/code_generator.h" 5 #include "vm/code_generator.h"
6 6
7 #include "vm/assembler.h" 7 #include "vm/assembler.h"
8 #include "vm/ast.h" 8 #include "vm/ast.h"
9 #include "vm/code_patcher.h" 9 #include "vm/code_patcher.h"
10 #include "vm/compiler.h" 10 #include "vm/compiler.h"
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 98
99 99
100 // Allocation of a fixed length array of given element type. 100 // Allocation of a fixed length array of given element type.
101 // This runtime entry is never called for allocating a List of a generic type, 101 // This runtime entry is never called for allocating a List of a generic type,
102 // because a prior run time call instantiates the element type if necessary. 102 // because a prior run time call instantiates the element type if necessary.
103 // Arg0: array length. 103 // Arg0: array length.
104 // Arg1: array type arguments, i.e. vector of 1 type, the element type. 104 // Arg1: array type arguments, i.e. vector of 1 type, the element type.
105 // Return value: newly allocated array of length arg0. 105 // Return value: newly allocated array of length arg0.
106 DEFINE_RUNTIME_ENTRY(AllocateArray, 2) { 106 DEFINE_RUNTIME_ENTRY(AllocateArray, 2) {
107 const Instance& length = Instance::CheckedHandle(arguments.ArgAt(0)); 107 const Instance& length = Instance::CheckedHandle(arguments.ArgAt(0));
108 if (!length.IsSmi()) { 108 if (!length.IsInteger()) {
109 const String& error = String::Handle(String::NewFormatted( 109 // Throw: new ArgumentError.value(length, "length", "is not an integer");
110 "Length must be an integer in the range [0..%" Pd "].", 110 const Array& args = Array::Handle(Array::New(3));
111 Array::kMaxElements)); 111 args.SetAt(0, length);
112 Exceptions::ThrowArgumentError(error); 112 args.SetAt(1, Symbols::Length());
113 args.SetAt(2, String::Handle(String::New("is not an integer")));
114 Exceptions::ThrowByType(Exceptions::kArgumentValue, args);
113 } 115 }
114 const intptr_t len = Smi::Cast(length).Value(); 116 if (length.IsSmi()) {
115 if ((len < 0) || (len > Array::kMaxElements)) { 117 const intptr_t len = Smi::Cast(length).Value();
116 const String& error = String::Handle(String::NewFormatted( 118 if ((len >= 0) && (len <= Array::kMaxElements)) {
117 "Length (%" Pd ") must be an integer in the range [0..%" Pd "].", 119 Heap::Space space = isolate->heap()->SpaceForAllocation(kArrayCid);
118 len, Array::kMaxElements)); 120 const Array& array = Array::Handle(Array::New(len, space));
119 Exceptions::ThrowArgumentError(error); 121 arguments.SetReturn(array);
122 TypeArguments& element_type =
123 TypeArguments::CheckedHandle(arguments.ArgAt(1));
124 // An Array is raw or takes one type argument. However, its type argument
125 // vector may be longer than 1 due to a type optimization reusing the type
126 // argument vector of the instantiator.
127 ASSERT(element_type.IsNull() ||
128 ((element_type.Length() >= 1) && element_type.IsInstantiated()));
129 array.SetTypeArguments(element_type); // May be null.
130 return;
131 }
120 } 132 }
121 133 // Throw: new RangeError.range(length, 0, Array::kMaxElements, "length");
122 Heap::Space space = isolate->heap()->SpaceForAllocation(kArrayCid); 134 const Array& args = Array::Handle(Array::New(4));
123 const Array& array = Array::Handle(Array::New(len, space)); 135 args.SetAt(0, length);
124 arguments.SetReturn(array); 136 args.SetAt(1, Integer::Handle(Integer::New(0)));
125 TypeArguments& element_type = 137 args.SetAt(2, Integer::Handle(Integer::New(Array::kMaxElements)));
126 TypeArguments::CheckedHandle(arguments.ArgAt(1)); 138 args.SetAt(3, Symbols::Length());
127 // An Array is raw or takes one type argument. However, its type argument 139 Exceptions::ThrowByType(Exceptions::kRangeRange, args);
128 // vector may be longer than 1 due to a type optimization reusing the type
129 // argument vector of the instantiator.
130 ASSERT(element_type.IsNull() ||
131 ((element_type.Length() >= 1) && element_type.IsInstantiated()));
132 array.SetTypeArguments(element_type); // May be null.
133 } 140 }
134 141
135 142
136 // Helper returning the token position of the Dart caller. 143 // Helper returning the token position of the Dart caller.
137 static intptr_t GetCallerLocation() { 144 static intptr_t GetCallerLocation() {
138 DartFrameIterator iterator; 145 DartFrameIterator iterator;
139 StackFrame* caller_frame = iterator.NextFrame(); 146 StackFrame* caller_frame = iterator.NextFrame();
140 ASSERT(caller_frame != NULL); 147 ASSERT(caller_frame != NULL);
141 return caller_frame->GetTokenPos(); 148 return caller_frame->GetTokenPos();
142 } 149 }
(...skipping 1647 matching lines...) Expand 10 before | Expand all | Expand 10 after
1790 const intptr_t elm_size = old_data.ElementSizeInBytes(); 1797 const intptr_t elm_size = old_data.ElementSizeInBytes();
1791 const TypedData& new_data = 1798 const TypedData& new_data =
1792 TypedData::Handle(TypedData::New(cid, new_size, Heap::kOld)); 1799 TypedData::Handle(TypedData::New(cid, new_size, Heap::kOld));
1793 TypedData::Copy(new_data, 0, old_data, 0, old_size * elm_size); 1800 TypedData::Copy(new_data, 0, old_data, 0, old_size * elm_size);
1794 typed_data_cell.SetAt(0, new_data); 1801 typed_data_cell.SetAt(0, new_data);
1795 arguments.SetReturn(new_data); 1802 arguments.SetReturn(new_data);
1796 } 1803 }
1797 1804
1798 1805
1799 } // namespace dart 1806 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/exceptions.h » ('j') | sdk/lib/_internal/js_runtime/lib/js_array.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698