OLD | NEW |
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 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 | 508 |
509 static uint16_t StringValueAt(const String& str, const Integer& index) { | 509 static uint16_t StringValueAt(const String& str, const Integer& index) { |
510 if (index.IsSmi()) { | 510 if (index.IsSmi()) { |
511 const intptr_t index_value = Smi::Cast(index).Value(); | 511 const intptr_t index_value = Smi::Cast(index).Value(); |
512 if ((0 <= index_value) && (index_value < str.Length())) { | 512 if ((0 <= index_value) && (index_value < str.Length())) { |
513 return str.CharAt(index_value); | 513 return str.CharAt(index_value); |
514 } | 514 } |
515 } | 515 } |
516 | 516 |
517 // An index larger than Smi is always illegal. | 517 // An index larger than Smi is always illegal. |
518 Exceptions::ThrowRangeError("index", index, 0, str.Length()); | 518 Exceptions::ThrowRangeError("index", index, 0, str.Length() - 1); |
519 return 0; | 519 return 0; |
520 } | 520 } |
521 | 521 |
522 | 522 |
523 DEFINE_NATIVE_ENTRY(String_charAt, 2) { | 523 DEFINE_NATIVE_ENTRY(String_charAt, 2) { |
524 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); | 524 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); |
525 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); | 525 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); |
526 uint16_t value = StringValueAt(receiver, index); | 526 uint16_t value = StringValueAt(receiver, index); |
527 return Symbols::FromCharCode(static_cast<int32_t>(value)); | 527 return Symbols::FromCharCode(static_cast<int32_t>(value)); |
528 } | 528 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 } | 594 } |
595 | 595 |
596 | 596 |
597 DEFINE_NATIVE_ENTRY(StringBuffer_createStringFromUint16Array, 3) { | 597 DEFINE_NATIVE_ENTRY(StringBuffer_createStringFromUint16Array, 3) { |
598 GET_NON_NULL_NATIVE_ARGUMENT(TypedData, codeUnits, arguments->NativeArgAt(0)); | 598 GET_NON_NULL_NATIVE_ARGUMENT(TypedData, codeUnits, arguments->NativeArgAt(0)); |
599 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(1)); | 599 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(1)); |
600 GET_NON_NULL_NATIVE_ARGUMENT(Bool, isLatin1, arguments->NativeArgAt(2)); | 600 GET_NON_NULL_NATIVE_ARGUMENT(Bool, isLatin1, arguments->NativeArgAt(2)); |
601 intptr_t array_length = codeUnits.Length(); | 601 intptr_t array_length = codeUnits.Length(); |
602 intptr_t length_value = length.Value(); | 602 intptr_t length_value = length.Value(); |
603 if (length_value < 0 || length_value > array_length) { | 603 if (length_value < 0 || length_value > array_length) { |
604 Exceptions::ThrowRangeError("length", length, 0, array_length + 1); | 604 Exceptions::ThrowRangeError("length", length, 0, array_length); |
605 } | 605 } |
606 const String& result = isLatin1.value() | 606 const String& result = isLatin1.value() |
607 ? String::Handle(OneByteString::New(length_value, Heap::kNew)) | 607 ? String::Handle(OneByteString::New(length_value, Heap::kNew)) |
608 : String::Handle(TwoByteString::New(length_value, Heap::kNew)); | 608 : String::Handle(TwoByteString::New(length_value, Heap::kNew)); |
609 NoSafepointScope no_safepoint; | 609 NoSafepointScope no_safepoint; |
610 | 610 |
611 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0)); | 611 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0)); |
612 String::Copy(result, 0, data_position, length_value); | 612 String::Copy(result, 0, data_position, length_value); |
613 return result.raw(); | 613 return result.raw(); |
614 } | 614 } |
615 | 615 |
616 } // namespace dart | 616 } // namespace dart |
OLD | NEW |