Index: src/runtime/runtime-typedarray.cc |
diff --git a/src/runtime/runtime-typedarray.cc b/src/runtime/runtime-typedarray.cc |
index d5e394c3456caf6e06501bb5403a3e1ed00f34dd..8d474372fe7e3d7b59e261c3096ba43c15f5a274 100644 |
--- a/src/runtime/runtime-typedarray.cc |
+++ b/src/runtime/runtime-typedarray.cc |
@@ -367,6 +367,63 @@ RUNTIME_FUNCTION(Runtime_TypedArraySetFastCases) { |
} |
} |
+namespace { |
+ |
+#define TYPED_ARRAY_SORT_COMPAREFN(Type, type, TYPE, ctype, size) \ |
+ bool compare##Type(ctype x, ctype y) { \ |
+ if (x < y) { \ |
+ return true; \ |
+ } else if (x > y) { \ |
+ return false; \ |
+ } else if (x == 0 && x == y) { \ |
+ return std::signbit(x) ? true : false; \ |
+ } else if (std::isnan(x)) { \ |
+ return false; \ |
+ } \ |
+ return true; \ |
+ } |
+ |
+TYPED_ARRAYS(TYPED_ARRAY_SORT_COMPAREFN) |
+#undef TYPED_ARRAY_SORT_COMPAREFN |
+ |
+} // namespace |
+ |
+RUNTIME_FUNCTION(Runtime_TypedArraySortFast) { |
+ HandleScope scope(isolate); |
+ DCHECK_EQ(1, args.length()); |
+ |
+ if (!args[0]->IsJSTypedArray()) { |
+ THROW_NEW_ERROR_RETURN_FAILURE( |
+ isolate, NewTypeError(MessageTemplate::kNotTypedArray)); |
+ } |
+ |
+ CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, target_obj, 0); |
+ |
+ Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(target_obj); |
+ |
+ if (array->WasNeutered()) { |
+ THROW_NEW_ERROR_RETURN_FAILURE( |
caitp
2017/02/15 12:24:20
So, as Dan was saying, we probably want to jist ea
caitp
2017/02/15 13:27:22
To add to this, maybe move ValidateTypedArray() in
Choongwoo Han
2017/02/16 08:06:34
Done. I've moved it to static inline method of JST
|
+ isolate, NewTypeError(MessageTemplate::kNotTypedArray)); |
+ } |
+ |
+ size_t length = array->length_value(); |
+ if (length == 0) return *array; |
+ |
+ switch (array->type()) { |
+#define TYPED_ARRAY_SORT(Type, type, TYPE, ctype, size) \ |
+ case kExternal##Type##Array: { \ |
+ ctype* backing_store = \ |
+ static_cast<ctype*>(array->GetBuffer()->backing_store()); \ |
+ std::sort(backing_store, backing_store + length, compare##Type); \ |
+ break; \ |
+ } |
+ |
+ TYPED_ARRAYS(TYPED_ARRAY_SORT) |
+#undef TYPED_ARRAY_SORT |
+ } |
+ |
+ return *array; |
+} |
RUNTIME_FUNCTION(Runtime_TypedArrayMaxSizeInHeap) { |
DCHECK_EQ(0, args.length()); |