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

Side by Side Diff: runtime/lib/string.cc

Issue 25566002: Factor out throwing of argument error. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « runtime/lib/mirrors.cc ('k') | runtime/lib/typed_data.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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/bootstrap_natives.h" 5 #include "vm/bootstrap_natives.h"
6 6
7 #include "vm/exceptions.h" 7 #include "vm/exceptions.h"
8 #include "vm/native_entry.h" 8 #include "vm/native_entry.h"
9 #include "vm/object.h" 9 #include "vm/object.h"
10 #include "vm/symbols.h" 10 #include "vm/symbols.h"
11 #include "vm/unicode.h" 11 #include "vm/unicode.h"
12 12
13 namespace dart { 13 namespace dart {
14 14
15 DEFINE_NATIVE_ENTRY(StringBase_createFromCodePoints, 1) { 15 DEFINE_NATIVE_ENTRY(StringBase_createFromCodePoints, 1) {
16 GET_NON_NULL_NATIVE_ARGUMENT(Instance, list, arguments->NativeArgAt(0)); 16 GET_NON_NULL_NATIVE_ARGUMENT(Instance, list, arguments->NativeArgAt(0));
17 if (!list.IsGrowableObjectArray() && !list.IsArray()) { 17 if (!list.IsGrowableObjectArray() && !list.IsArray()) {
18 const Array& args = Array::Handle(Array::New(1)); 18 Exceptions::ThrowArgumentError(list);
19 args.SetAt(0, list);
20 Exceptions::ThrowByType(Exceptions::kArgument, args);
21 } 19 }
22 20
23 Array& a = Array::Handle(); 21 Array& a = Array::Handle();
24 intptr_t array_len; 22 intptr_t array_len;
25 if (list.IsGrowableObjectArray()) { 23 if (list.IsGrowableObjectArray()) {
26 const GrowableObjectArray& growableArray = GrowableObjectArray::Cast(list); 24 const GrowableObjectArray& growableArray = GrowableObjectArray::Cast(list);
27 a ^= growableArray.data(); 25 a ^= growableArray.data();
28 array_len = growableArray.Length(); 26 array_len = growableArray.Length();
29 } else { 27 } else {
30 a ^= Array::Cast(list).raw(); 28 a ^= Array::Cast(list).raw();
31 array_len = a.Length(); 29 array_len = a.Length();
32 } 30 }
33 31
34 Zone* zone = isolate->current_zone(); 32 Zone* zone = isolate->current_zone();
35 33
36 // Unbox the array and determine the maximum element width. 34 // Unbox the array and determine the maximum element width.
37 bool is_one_byte_string = true; 35 bool is_one_byte_string = true;
38 intptr_t utf16_len = array_len; 36 intptr_t utf16_len = array_len;
39 int32_t* utf32_array = zone->Alloc<int32_t>(array_len); 37 int32_t* utf32_array = zone->Alloc<int32_t>(array_len);
40 Object& index_object = Object::Handle(isolate); 38 Instance& index_object = Instance::Handle(isolate);
41 for (intptr_t i = 0; i < array_len; i++) { 39 for (intptr_t i = 0; i < array_len; i++) {
42 index_object = a.At(i); 40 index_object ^= a.At(i);
43 if (!index_object.IsSmi()) { 41 if (!index_object.IsSmi()) {
44 const Array& args = Array::Handle(Array::New(1)); 42 Exceptions::ThrowArgumentError(index_object);
45 args.SetAt(0, index_object);
46 Exceptions::ThrowByType(Exceptions::kArgument, args);
47 } 43 }
48 intptr_t value = Smi::Cast(index_object).Value(); 44 intptr_t value = Smi::Cast(index_object).Value();
49 if (Utf::IsOutOfRange(value)) { 45 if (Utf::IsOutOfRange(value)) {
50 Exceptions::ThrowByType(Exceptions::kArgument, Object::empty_array()); 46 Exceptions::ThrowByType(Exceptions::kArgument, Object::empty_array());
51 } else { 47 } else {
52 if (!Utf::IsLatin1(value)) { 48 if (!Utf::IsLatin1(value)) {
53 is_one_byte_string = false; 49 is_one_byte_string = false;
54 if (Utf::IsSupplementary(value)) { 50 if (Utf::IsSupplementary(value)) {
55 utf16_len += 1; 51 utf16_len += 1;
56 } 52 }
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 } 210 }
215 211
216 212
217 DEFINE_NATIVE_ENTRY(Strings_concatAll, 3) { 213 DEFINE_NATIVE_ENTRY(Strings_concatAll, 3) {
218 GET_NON_NULL_NATIVE_ARGUMENT(Instance, argument, arguments->NativeArgAt(0)); 214 GET_NON_NULL_NATIVE_ARGUMENT(Instance, argument, arguments->NativeArgAt(0));
219 GET_NON_NULL_NATIVE_ARGUMENT(Smi, start, arguments->NativeArgAt(1)); 215 GET_NON_NULL_NATIVE_ARGUMENT(Smi, start, arguments->NativeArgAt(1));
220 GET_NON_NULL_NATIVE_ARGUMENT(Smi, end, arguments->NativeArgAt(2)); 216 GET_NON_NULL_NATIVE_ARGUMENT(Smi, end, arguments->NativeArgAt(2));
221 const intptr_t start_ix = start.Value(); 217 const intptr_t start_ix = start.Value();
222 const intptr_t end_ix = end.Value(); 218 const intptr_t end_ix = end.Value();
223 if (start_ix < 0) { 219 if (start_ix < 0) {
224 const Array& args = Array::Handle(Array::New(1)); 220 Exceptions::ThrowArgumentError(start);
225 args.SetAt(0, start);
226 Exceptions::ThrowByType(Exceptions::kArgument, args);
227 } 221 }
228 Array& strings = Array::Handle(); 222 Array& strings = Array::Handle();
229 intptr_t length = -1; 223 intptr_t length = -1;
230 if (argument.IsArray()) { 224 if (argument.IsArray()) {
231 strings ^= argument.raw(); 225 strings ^= argument.raw();
232 length = strings.Length(); 226 length = strings.Length();
233 } else if (argument.IsGrowableObjectArray()) { 227 } else if (argument.IsGrowableObjectArray()) {
234 const GrowableObjectArray& g_array = GrowableObjectArray::Cast(argument); 228 const GrowableObjectArray& g_array = GrowableObjectArray::Cast(argument);
235 strings = g_array.data(); 229 strings = g_array.data();
236 length = g_array.Length(); 230 length = g_array.Length();
237 } else { 231 } else {
238 const Array& args = Array::Handle(Array::New(1)); 232 Exceptions::ThrowArgumentError(argument);
239 args.SetAt(0, argument);
240 Exceptions::ThrowByType(Exceptions::kArgument, args);
241 } 233 }
242 if (end_ix > length) { 234 if (end_ix > length) {
243 const Array& args = Array::Handle(Array::New(1)); 235 Exceptions::ThrowArgumentError(end);
244 args.SetAt(0, end);
245 Exceptions::ThrowByType(Exceptions::kArgument, args);
246 } 236 }
247 #if defined(DEBUG) 237 #if defined(DEBUG)
248 // Check that the array contains strings. 238 // Check that the array contains strings.
249 Instance& elem = Instance::Handle(); 239 Instance& elem = Instance::Handle();
250 for (intptr_t i = start_ix; i < end_ix; i++) { 240 for (intptr_t i = start_ix; i < end_ix; i++) {
251 elem ^= strings.At(i); 241 elem ^= strings.At(i);
252 ASSERT(elem.IsString()); 242 ASSERT(elem.IsString());
253 } 243 }
254 #endif 244 #endif
255 return String::ConcatAllRange(strings, start_ix, end_ix, Heap::kNew); 245 return String::ConcatAllRange(strings, start_ix, end_ix, Heap::kNew);
(...skipping 15 matching lines...) Expand all
271 ? String::Handle(OneByteString::New(length_value, Heap::kNew)) 261 ? String::Handle(OneByteString::New(length_value, Heap::kNew))
272 : String::Handle(TwoByteString::New(length_value, Heap::kNew)); 262 : String::Handle(TwoByteString::New(length_value, Heap::kNew));
273 NoGCScope no_gc; 263 NoGCScope no_gc;
274 264
275 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0)); 265 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0));
276 String::Copy(result, 0, data_position, length_value); 266 String::Copy(result, 0, data_position, length_value);
277 return result.raw(); 267 return result.raw();
278 } 268 }
279 269
280 } // namespace dart 270 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/lib/mirrors.cc ('k') | runtime/lib/typed_data.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698