Commit 1bf8becc authored by cwhan.tunz's avatar cwhan.tunz Committed by Commit bot

[typedarrays] Implement %TypedArray%.prototype.reverse in C++

- Implement %TypedArray%prototype.reverse in builtins and
  ElementsAccessor to use std::reverse
- Remove TypedArrayReverse in src/js/typedarray.js
- Fix typo in comments

BUG=v8:5929

Review-Url: https://codereview.chromium.org/2761453002
Cr-Commit-Position: refs/heads/master@{#44132}
parent 56dc3121
......@@ -2631,6 +2631,8 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
Builtins::kTypedArrayPrototypeIndexOf, 1, false);
SimpleInstallFunction(prototype, "lastIndexOf",
Builtins::kTypedArrayPrototypeLastIndexOf, 1, false);
SimpleInstallFunction(prototype, "reverse",
Builtins::kTypedArrayPrototypeReverse, 0, false);
}
{ // -- T y p e d A r r a y s
......
......@@ -255,5 +255,20 @@ BUILTIN(TypedArrayPrototypeLastIndexOf) {
return *isolate->factory()->NewNumberFromInt64(result.FromJust());
}
BUILTIN(TypedArrayPrototypeReverse) {
HandleScope scope(isolate);
Handle<JSTypedArray> array;
const char* method = "%TypedArray%.prototype.reverse";
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method));
if (V8_UNLIKELY(array->WasNeutered())) return *array;
ElementsAccessor* elements = array->GetElementsAccessor();
elements->Reverse(*array);
return *array;
}
} // namespace internal
} // namespace v8
......@@ -915,8 +915,10 @@ class Isolate;
CPP(TypedArrayPrototypeIncludes) \
/* ES6 #sec-%typedarray%.prototype.indexof */ \
CPP(TypedArrayPrototypeIndexOf) \
/* ES6 #sec-%typedarray%.prototype.indexof */ \
/* ES6 #sec-%typedarray%.prototype.lastindexof */ \
CPP(TypedArrayPrototypeLastIndexOf) \
/* ES6 #sec-%typedarray%.prototype.reverse */ \
CPP(TypedArrayPrototypeReverse) \
\
/* Wasm */ \
ASM(WasmCompileLazy) \
......
......@@ -1261,6 +1261,10 @@ class ElementsAccessorBase : public ElementsAccessor {
return Subclass::LastIndexOfValueImpl(isolate, receiver, value, start_from);
}
static void ReverseImpl(JSObject* receiver) { UNREACHABLE(); }
void Reverse(JSObject* receiver) final { Subclass::ReverseImpl(receiver); }
static uint32_t GetIndexForEntryImpl(FixedArrayBase* backing_store,
uint32_t entry) {
return entry;
......@@ -3035,6 +3039,19 @@ class TypedElementsAccessor
} while (k-- != 0);
return Just<int64_t>(-1);
}
static void ReverseImpl(JSObject* receiver) {
DisallowHeapAllocation no_gc;
DCHECK(!WasNeutered(receiver));
BackingStore* elements = BackingStore::cast(receiver->elements());
uint32_t len = elements->length();
if (len == 0) return;
ctype* data = static_cast<ctype*>(elements->DataPtr());
std::reverse(data, data + len);
}
};
#define FIXED_ELEMENTS_ACCESSOR(Type, type, TYPE, ctype, size) \
......
......@@ -180,6 +180,8 @@ class ElementsAccessor {
Handle<Object> value,
uint32_t start) = 0;
virtual void Reverse(JSObject* receiver) = 0;
virtual void CopyElements(Handle<FixedArrayBase> source,
ElementsKind source_kind,
Handle<FixedArrayBase> destination, int size) = 0;
......
......@@ -1446,7 +1446,6 @@ utils.Export(function(to) {
to.InnerArrayJoin = InnerArrayJoin;
to.InnerArraySort = InnerArraySort;
to.InnerArrayToLocaleString = InnerArrayToLocaleString;
to.PackedArrayReverse = PackedArrayReverse;
});
%InstallToContext([
......
......@@ -29,7 +29,6 @@ var InnerArrayToLocaleString;
var InternalArray = utils.InternalArray;
var MaxSimple;
var MinSimple;
var PackedArrayReverse;
var SpeciesConstructor;
var ToPositiveInteger;
var ToIndex;
......@@ -69,7 +68,6 @@ utils.Import(function(from) {
InnerArrayToLocaleString = from.InnerArrayToLocaleString;
MaxSimple = from.MaxSimple;
MinSimple = from.MinSimple;
PackedArrayReverse = from.PackedArrayReverse;
SpeciesConstructor = from.SpeciesConstructor;
ToPositiveInteger = from.ToPositiveInteger;
ToIndex = from.ToIndex;
......@@ -432,15 +430,6 @@ function TypedArrayFindIndex(predicate, thisArg) {
%FunctionSetLength(TypedArrayFindIndex, 1);
// ES6 draft 05-18-15, section 22.2.3.21
function TypedArrayReverse() {
if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray);
var length = %_TypedArrayGetLength(this);
return PackedArrayReverse(this, length);
}
// ES6 draft 05-18-15, section 22.2.3.25
function TypedArraySort(comparefn) {
if (!IS_TYPEDARRAY(this)) throw %make_type_error(kNotTypedArray);
......@@ -712,7 +701,6 @@ utils.InstallFunctions(GlobalTypedArray.prototype, DONT_ENUM, [
"map", TypedArrayMap,
"reduce", TypedArrayReduce,
"reduceRight", TypedArrayReduceRight,
"reverse", TypedArrayReverse,
"slice", TypedArraySlice,
"some", TypedArraySome,
"sort", TypedArraySort,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment