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

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

Issue 533483003: Cleanup throwing of the RangeError in the runtime to remove duplicated code. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 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
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 "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "vm/exceptions.h" 8 #include "vm/exceptions.h"
9 #include "vm/dart_api_impl.h" 9 #include "vm/dart_api_impl.h"
10 #include "vm/isolate.h" 10 #include "vm/isolate.h"
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 230 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
231 return Smi::New(receiver.Length()); 231 return Smi::New(receiver.Length());
232 } 232 }
233 233
234 234
235 static int32_t StringValueAt(const String& str, const Integer& index) { 235 static int32_t StringValueAt(const String& str, const Integer& index) {
236 if (index.IsSmi()) { 236 if (index.IsSmi()) {
237 const Smi& smi = Smi::Cast(index); 237 const Smi& smi = Smi::Cast(index);
238 intptr_t index = smi.Value(); 238 intptr_t index = smi.Value();
239 if ((index < 0) || (index >= str.Length())) { 239 if ((index < 0) || (index >= str.Length())) {
240 const Array& args = Array::Handle(Array::New(1)); 240 Exceptions::ThrowRangeError(smi);
241 args.SetAt(0, smi);
242 Exceptions::ThrowByType(Exceptions::kRange, args);
243 } 241 }
244 return str.CharAt(index); 242 return str.CharAt(index);
245 } else { 243 } else {
246 // An index larger than Smi is always illegal. 244 // An index larger than Smi is always illegal.
247 const Array& args = Array::Handle(Array::New(1)); 245 Exceptions::ThrowRangeError(index);
248 args.SetAt(0, index);
249 Exceptions::ThrowByType(Exceptions::kRange, args);
250 return 0; 246 return 0;
251 } 247 }
252 } 248 }
253 249
254 250
255 DEFINE_NATIVE_ENTRY(String_charAt, 2) { 251 DEFINE_NATIVE_ENTRY(String_charAt, 2) {
256 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 252 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
257 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); 253 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1));
258 uint32_t value = StringValueAt(receiver, index); 254 uint32_t value = StringValueAt(receiver, index);
259 ASSERT(value <= 0x10FFFF); 255 ASSERT(value <= 0x10FFFF);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 } 325 }
330 326
331 327
332 DEFINE_NATIVE_ENTRY(StringBuffer_createStringFromUint16Array, 3) { 328 DEFINE_NATIVE_ENTRY(StringBuffer_createStringFromUint16Array, 3) {
333 GET_NON_NULL_NATIVE_ARGUMENT(TypedData, codeUnits, arguments->NativeArgAt(0)); 329 GET_NON_NULL_NATIVE_ARGUMENT(TypedData, codeUnits, arguments->NativeArgAt(0));
334 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(1)); 330 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(1));
335 GET_NON_NULL_NATIVE_ARGUMENT(Bool, isLatin1, arguments->NativeArgAt(2)); 331 GET_NON_NULL_NATIVE_ARGUMENT(Bool, isLatin1, arguments->NativeArgAt(2));
336 intptr_t array_length = codeUnits.Length(); 332 intptr_t array_length = codeUnits.Length();
337 intptr_t length_value = length.Value(); 333 intptr_t length_value = length.Value();
338 if (length_value < 0 || length_value > array_length) { 334 if (length_value < 0 || length_value > array_length) {
339 const Array& args = Array::Handle(Array::New(1)); 335 Exceptions::ThrowRangeError(length);
340 args.SetAt(0, length);
341 Exceptions::ThrowByType(Exceptions::kRange, args);
342 } 336 }
343 const String& result = isLatin1.value() 337 const String& result = isLatin1.value()
344 ? String::Handle(OneByteString::New(length_value, Heap::kNew)) 338 ? String::Handle(OneByteString::New(length_value, Heap::kNew))
345 : String::Handle(TwoByteString::New(length_value, Heap::kNew)); 339 : String::Handle(TwoByteString::New(length_value, Heap::kNew));
346 NoGCScope no_gc; 340 NoGCScope no_gc;
347 341
348 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0)); 342 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0));
349 String::Copy(result, 0, data_position, length_value); 343 String::Copy(result, 0, data_position, length_value);
350 return result.raw(); 344 return result.raw();
351 } 345 }
352 346
353 } // namespace dart 347 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698