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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 | 227 |
228 | 228 |
229 DEFINE_NATIVE_ENTRY(String_getLength, 1) { | 229 DEFINE_NATIVE_ENTRY(String_getLength, 1) { |
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 intptr_t index_value = Smi::Cast(index).Value(); |
238 intptr_t index = smi.Value(); | 238 if ((0 <= index_value) && (index_value < str.Length())) { |
239 if ((index < 0) || (index >= str.Length())) { | 239 return str.CharAt(index_value); |
240 const Array& args = Array::Handle(Array::New(1)); | |
241 args.SetAt(0, smi); | |
242 Exceptions::ThrowByType(Exceptions::kRange, args); | |
243 } | 240 } |
244 return str.CharAt(index); | |
245 } else { | |
246 // An index larger than Smi is always illegal. | |
247 const Array& args = Array::Handle(Array::New(1)); | |
248 args.SetAt(0, index); | |
249 Exceptions::ThrowByType(Exceptions::kRange, args); | |
250 return 0; | |
251 } | 241 } |
| 242 |
| 243 // An index larger than Smi is always illegal. |
| 244 Exceptions::ThrowRangeError("index", index, 0, str.Length()); |
| 245 return 0; |
252 } | 246 } |
253 | 247 |
254 | 248 |
255 DEFINE_NATIVE_ENTRY(String_charAt, 2) { | 249 DEFINE_NATIVE_ENTRY(String_charAt, 2) { |
256 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); | 250 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); |
257 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); | 251 GET_NON_NULL_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); |
258 uint32_t value = StringValueAt(receiver, index); | 252 uint32_t value = StringValueAt(receiver, index); |
259 ASSERT(value <= 0x10FFFF); | 253 ASSERT(value <= 0x10FFFF); |
260 return Symbols::FromCharCode(value); | 254 return Symbols::FromCharCode(value); |
261 } | 255 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 } | 323 } |
330 | 324 |
331 | 325 |
332 DEFINE_NATIVE_ENTRY(StringBuffer_createStringFromUint16Array, 3) { | 326 DEFINE_NATIVE_ENTRY(StringBuffer_createStringFromUint16Array, 3) { |
333 GET_NON_NULL_NATIVE_ARGUMENT(TypedData, codeUnits, arguments->NativeArgAt(0)); | 327 GET_NON_NULL_NATIVE_ARGUMENT(TypedData, codeUnits, arguments->NativeArgAt(0)); |
334 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(1)); | 328 GET_NON_NULL_NATIVE_ARGUMENT(Smi, length, arguments->NativeArgAt(1)); |
335 GET_NON_NULL_NATIVE_ARGUMENT(Bool, isLatin1, arguments->NativeArgAt(2)); | 329 GET_NON_NULL_NATIVE_ARGUMENT(Bool, isLatin1, arguments->NativeArgAt(2)); |
336 intptr_t array_length = codeUnits.Length(); | 330 intptr_t array_length = codeUnits.Length(); |
337 intptr_t length_value = length.Value(); | 331 intptr_t length_value = length.Value(); |
338 if (length_value < 0 || length_value > array_length) { | 332 if (length_value < 0 || length_value > array_length) { |
339 const Array& args = Array::Handle(Array::New(1)); | 333 Exceptions::ThrowRangeError("length", length, 0, array_length + 1); |
340 args.SetAt(0, length); | |
341 Exceptions::ThrowByType(Exceptions::kRange, args); | |
342 } | 334 } |
343 const String& result = isLatin1.value() | 335 const String& result = isLatin1.value() |
344 ? String::Handle(OneByteString::New(length_value, Heap::kNew)) | 336 ? String::Handle(OneByteString::New(length_value, Heap::kNew)) |
345 : String::Handle(TwoByteString::New(length_value, Heap::kNew)); | 337 : String::Handle(TwoByteString::New(length_value, Heap::kNew)); |
346 NoGCScope no_gc; | 338 NoGCScope no_gc; |
347 | 339 |
348 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0)); | 340 uint16_t* data_position = reinterpret_cast<uint16_t*>(codeUnits.DataAddr(0)); |
349 String::Copy(result, 0, data_position, length_value); | 341 String::Copy(result, 0, data_position, length_value); |
350 return result.raw(); | 342 return result.raw(); |
351 } | 343 } |
352 | 344 |
353 } // namespace dart | 345 } // namespace dart |
OLD | NEW |