Commit a1f2239e authored by loorongjie's avatar loorongjie Committed by Commit bot

Move Oddball/String to %Typearray%.prototype.fill fast path

ToNumber for Oddball/String has no side-effect, no need to go
through %Typearray%.prototype.fill slow path.

BUG=v8:5929,chromium:702902

Review-Url: https://codereview.chromium.org/2769673002
Cr-Commit-Position: refs/heads/master@{#44129}
parent 2beb5613
...@@ -2863,16 +2863,23 @@ class TypedElementsAccessor ...@@ -2863,16 +2863,23 @@ class TypedElementsAccessor
Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(receiver); Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(receiver);
DCHECK(!array->WasNeutered()); DCHECK(!array->WasNeutered());
if (!obj_value->IsNumber()) { ctype value;
return FillNumberSlowPath(isolate, array, obj_value, start, end);
}
ctype value = 0;
if (obj_value->IsSmi()) { if (obj_value->IsSmi()) {
value = BackingStore::from_int(Smi::cast(*obj_value)->value()); value = BackingStore::from_int(Smi::cast(*obj_value)->value());
} else { } else {
DCHECK(obj_value->IsHeapNumber()); Handle<HeapObject> heap_obj_value = Handle<HeapObject>::cast(obj_value);
value = BackingStore::from_double(HeapNumber::cast(*obj_value)->value()); if (heap_obj_value->IsHeapNumber()) {
value = BackingStore::from_double(
HeapNumber::cast(*heap_obj_value)->value());
} else if (heap_obj_value->IsOddball()) {
value = BackingStore::from_double(
Oddball::ToNumber(Handle<Oddball>::cast(heap_obj_value))->Number());
} else if (heap_obj_value->IsString()) {
value = BackingStore::from_double(
String::ToNumber(Handle<String>::cast(heap_obj_value))->Number());
} else {
return FillNumberSlowPath(isolate, array, obj_value, start, end);
}
} }
// Ensure indexes are within array bounds // Ensure indexes are within array bounds
......
...@@ -2,16 +2,22 @@ ...@@ -2,16 +2,22 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var typedArrayConstructors = [ var intArrayConstructors = [
Uint8Array, Uint8Array,
Int8Array, Int8Array,
Uint16Array, Uint16Array,
Int16Array, Int16Array,
Uint32Array, Uint32Array,
Int32Array, Int32Array,
Uint8ClampedArray, Uint8ClampedArray
];
var floatArrayConstructors = [
Float32Array, Float32Array,
Float64Array]; Float64Array
];
var typedArrayConstructors = [...intArrayConstructors, ...floatArrayConstructors];
for (var constructor of typedArrayConstructors) { for (var constructor of typedArrayConstructors) {
assertEquals(1, constructor.prototype.fill.length); assertEquals(1, constructor.prototype.fill.length);
...@@ -40,6 +46,11 @@ for (var constructor of typedArrayConstructors) { ...@@ -40,6 +46,11 @@ for (var constructor of typedArrayConstructors) {
assertThrows('constructor.prototype.fill.call(undefined)', TypeError); assertThrows('constructor.prototype.fill.call(undefined)', TypeError);
assertThrows('constructor.prototype.fill.call([])', TypeError); assertThrows('constructor.prototype.fill.call([])', TypeError);
assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(false));
assertArrayEquals([1, 1, 1, 1, 1], new constructor([0, 0, 0, 0, 0]).fill(true));
assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(null));
assertArrayEquals([8, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill("8"));
// Test ToNumber // Test ToNumber
var s = ""; var s = "";
var p = new Proxy({}, {get(t,k) { s += k.toString() + '\n'; return Reflect.get(t, k)}}) var p = new Proxy({}, {get(t,k) { s += k.toString() + '\n'; return Reflect.get(t, k)}})
...@@ -67,9 +78,17 @@ Symbol(Symbol.toStringTag) ...@@ -67,9 +78,17 @@ Symbol(Symbol.toStringTag)
assertArrayEquals([4, 3], [a[0], a[1]]); assertArrayEquals([4, 3], [a[0], a[1]]);
} }
// Empty args for (var constructor of intArrayConstructors) {
assertArrayEquals([0], new Uint8Array([0]).fill()); assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(undefined));
assertArrayEquals([NaN], new Float32Array([0]).fill()); assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill());
assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill("abcd"));
}
for (var constructor of floatArrayConstructors) {
assertArrayEquals([NaN, NaN, NaN, NaN, NaN], new constructor([0, 0, 0, 0, 0]).fill(undefined));
assertArrayEquals([NaN, NaN, NaN, NaN, NaN], new constructor([0, 0, 0, 0, 0]).fill());
assertArrayEquals([NaN, NaN, NaN, NaN, NaN], new constructor([0, 0, 0, 0, 0]).fill("abcd"));
}
// Clamping // Clamping
assertArrayEquals([0, 0, 0, 0, 0], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(-10)); assertArrayEquals([0, 0, 0, 0, 0], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(-10));
......
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