OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/factory.h" | 8 #include "src/factory.h" |
9 #include "src/messages.h" | 9 #include "src/messages.h" |
10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 target_base + target_byte_length > source_base)) { | 360 target_base + target_byte_length > source_base)) { |
361 // We do not support overlapping ArrayBuffers | 361 // We do not support overlapping ArrayBuffers |
362 DCHECK(target->GetBuffer()->backing_store() == | 362 DCHECK(target->GetBuffer()->backing_store() == |
363 source->GetBuffer()->backing_store()); | 363 source->GetBuffer()->backing_store()); |
364 return Smi::FromInt(TYPED_ARRAY_SET_TYPED_ARRAY_OVERLAPPING); | 364 return Smi::FromInt(TYPED_ARRAY_SET_TYPED_ARRAY_OVERLAPPING); |
365 } else { // Non-overlapping typed arrays | 365 } else { // Non-overlapping typed arrays |
366 return Smi::FromInt(TYPED_ARRAY_SET_TYPED_ARRAY_NONOVERLAPPING); | 366 return Smi::FromInt(TYPED_ARRAY_SET_TYPED_ARRAY_NONOVERLAPPING); |
367 } | 367 } |
368 } | 368 } |
369 | 369 |
| 370 namespace { |
| 371 |
| 372 #define TYPED_ARRAY_SORT_COMPAREFN(Type, type, TYPE, ctype, size) \ |
| 373 bool compare##Type(ctype x, ctype y) { \ |
| 374 if (x < y) { \ |
| 375 return true; \ |
| 376 } else if (x > y) { \ |
| 377 return false; \ |
| 378 } else if (x == 0 && x == y) { \ |
| 379 return std::signbit(static_cast<double>(x)) ? true : false; \ |
| 380 } else if (std::isnan(static_cast<double>(x))) { \ |
| 381 return false; \ |
| 382 } \ |
| 383 return true; \ |
| 384 } |
| 385 |
| 386 TYPED_ARRAYS(TYPED_ARRAY_SORT_COMPAREFN) |
| 387 #undef TYPED_ARRAY_SORT_COMPAREFN |
| 388 |
| 389 } // namespace |
| 390 |
| 391 RUNTIME_FUNCTION(Runtime_TypedArraySortFast) { |
| 392 HandleScope scope(isolate); |
| 393 DCHECK_EQ(1, args.length()); |
| 394 |
| 395 CONVERT_ARG_HANDLE_CHECKED(Object, target_obj, 0); |
| 396 |
| 397 Handle<JSTypedArray> array; |
| 398 const char* method = "%TypedArray%.prototype.sort"; |
| 399 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 400 isolate, array, JSTypedArray::Validate(isolate, target_obj, method)); |
| 401 |
| 402 // This line can be remove when JSTypedArray::Validate throws |
| 403 // if array.[[ViewedArrayBuffer]] is neutered(v8:4648) |
| 404 if (V8_UNLIKELY(array->WasNeutered())) return *array; |
| 405 |
| 406 size_t length = array->length_value(); |
| 407 if (length == 0) return *array; |
| 408 |
| 409 switch (array->type()) { |
| 410 #define TYPED_ARRAY_SORT(Type, type, TYPE, ctype, size) \ |
| 411 case kExternal##Type##Array: { \ |
| 412 ctype* backing_store = \ |
| 413 static_cast<ctype*>(array->GetBuffer()->backing_store()); \ |
| 414 std::sort(backing_store, backing_store + length, compare##Type); \ |
| 415 break; \ |
| 416 } |
| 417 |
| 418 TYPED_ARRAYS(TYPED_ARRAY_SORT) |
| 419 #undef TYPED_ARRAY_SORT |
| 420 } |
| 421 |
| 422 return *array; |
| 423 } |
370 | 424 |
371 RUNTIME_FUNCTION(Runtime_TypedArrayMaxSizeInHeap) { | 425 RUNTIME_FUNCTION(Runtime_TypedArrayMaxSizeInHeap) { |
372 DCHECK_EQ(0, args.length()); | 426 DCHECK_EQ(0, args.length()); |
373 DCHECK_OBJECT_SIZE(FLAG_typed_array_max_size_in_heap + | 427 DCHECK_OBJECT_SIZE(FLAG_typed_array_max_size_in_heap + |
374 FixedTypedArrayBase::kDataOffset); | 428 FixedTypedArrayBase::kDataOffset); |
375 return Smi::FromInt(FLAG_typed_array_max_size_in_heap); | 429 return Smi::FromInt(FLAG_typed_array_max_size_in_heap); |
376 } | 430 } |
377 | 431 |
378 | 432 |
379 RUNTIME_FUNCTION(Runtime_IsTypedArray) { | 433 RUNTIME_FUNCTION(Runtime_IsTypedArray) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 return isolate->heap()->false_value(); | 468 return isolate->heap()->false_value(); |
415 } | 469 } |
416 | 470 |
417 Handle<JSTypedArray> obj(JSTypedArray::cast(args[0])); | 471 Handle<JSTypedArray> obj(JSTypedArray::cast(args[0])); |
418 return isolate->heap()->ToBoolean(obj->GetBuffer()->is_shared() && | 472 return isolate->heap()->ToBoolean(obj->GetBuffer()->is_shared() && |
419 obj->type() == kExternalInt32Array); | 473 obj->type() == kExternalInt32Array); |
420 } | 474 } |
421 | 475 |
422 } // namespace internal | 476 } // namespace internal |
423 } // namespace v8 | 477 } // namespace v8 |
OLD | NEW |