Commit 033ca1ba authored by Mike Stanton's avatar Mike Stanton Committed by Commit Bot

[Builtins] Cleanup of Array.prototype.forEach()

Making it into more "idiomatic" Torque code (we are still defining what that means).
Template specialization on double and fast fixed arrays allowed me to cut
down on the boilerplate.

Bug: v8:7672
Change-Id: Ia35706993a9e2ea087ecc3ef93b3a5864ec97827
Reviewed-on: https://chromium-review.googlesource.com/1064054
Commit-Queue: Michael Stanton <mvstanton@chromium.org>
Reviewed-by: 's avatarDaniel Clifford <danno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53246}
parent 973285f5
......@@ -345,20 +345,19 @@ module array {
array: Object, object: Object, initialK: Object, length: Object,
to: Object): Object {
try {
let callbackfn: Callable = cast<Callable>(callback) otherwise TypeError;
let k: Smi = cast<Smi>(initialK) otherwise TypeError;
let number_length: Number = cast<Number>(length) otherwise TypeError;
let callbackfn: Callable = cast<Callable>(callback) otherwise Unexpected;
let k: Smi = cast<Smi>(initialK) otherwise Unexpected;
let number_length: Number = cast<Number>(length) otherwise Unexpected;
return ArrayForEachTorqueContinuation(
context, object, number_length, callbackfn, thisArg, k);
}
label TypeError {
ThrowTypeError(context, kCalledNonCallable, callback);
// TODO(mvstanton): this should be unreachable.
label Unexpected {
unreachable;
}
}
macro VisitAllDoubleElements(
macro VisitAllElements<FixedArrayType : type>(
context: Context, a: JSArray, len: Smi, callbackfn: Callable,
thisArg: Object): void labels
Bailout(Smi) {
......@@ -369,18 +368,16 @@ module array {
// Build a fast loop over the smi array.
for (; k < len; k = k + 1) {
// Ensure that the map didn't change.
if (map != a.map) goto slow;
if (map != a.map) goto Slow;
// Ensure that we haven't walked beyond a possibly updated length.
if (k >= a.length) goto slow;
let elements: FixedDoubleArray = cast<FixedDoubleArray>(a.elements)
otherwise slow;
if (k >= a.length) goto Slow;
try {
let value: float64 = LoadDoubleWithHoleCheck(elements, k)
otherwise found_hole;
let boxed_value: HeapNumber = AllocateHeapNumberWithValue(value);
Call(context, callbackfn, thisArg, boxed_value, k, a);
let value: Object =
LoadElementNoHole<FixedArrayType>(a, k) otherwise FoundHole;
Call(context, callbackfn, thisArg, value, k, a);
}
label found_hole {
label FoundHole {
// If we found the hole, we need to bail out if the initial
// array prototype has had elements inserted. This is preferable
// to walking the prototype chain looking for elements.
......@@ -389,39 +386,7 @@ module array {
}
}
}
label slow {
goto Bailout(k);
}
}
macro VisitAllFastElements(
context: Context, a: JSArray, len: Smi, callbackfn: Callable,
thisArg: Object): void labels
Bailout(Smi) {
let k: Smi = 0;
let map: Map = a.map;
try {
// Build a fast loop over the smi array.
for (; k < len; k = k + 1) {
// Ensure that the map didn't change.
if (map != a.map) goto slow;
// Ensure that we haven't walked beyond a possibly updated length.
if (k >= a.length) goto slow;
let elements: FixedArray = cast<FixedArray>(a.elements)
otherwise slow;
let value: Object = elements[k];
if (value != Hole) {
Call(context, callbackfn, thisArg, value, k, a);
} else {
// If we found the hole, we need to bail out if the initial
// array prototype has had elements inserted. This is preferable
// to walking the prototype chain looking for elements.
if (IsNoElementsProtectorCellInvalid()) goto slow;
}
}
}
label slow {
label Slow {
goto Bailout(k);
}
}
......@@ -432,23 +397,24 @@ module array {
Bailout(Smi) {
let k: Smi = 0;
try {
let smi_len: Smi = cast<Smi>(len) otherwise slow;
let a: JSArray = cast<JSArray>(o) otherwise slow;
let smi_len: Smi = cast<Smi>(len) otherwise Slow;
let a: JSArray = cast<JSArray>(o) otherwise Slow;
let map: Map = a.map;
if (!IsPrototypeInitialArrayPrototype(context, map)) goto slow;
if (!IsPrototypeInitialArrayPrototype(context, map)) goto Slow;
let elementsKind: ElementsKind = map.elements_kind;
if (!IsFastElementsKind(elementsKind)) goto slow;
if (!IsFastElementsKind(elementsKind)) goto Slow;
if (IsElementsKindGreaterThan(elementsKind, HOLEY_ELEMENTS)) {
VisitAllDoubleElements(context, a, smi_len, callbackfn, thisArg)
VisitAllElements<FixedDoubleArray>(
context, a, smi_len, callbackfn, thisArg)
otherwise Bailout;
} else {
VisitAllFastElements(context, a, smi_len, callbackfn, thisArg)
VisitAllElements<FixedArray>(context, a, smi_len, callbackfn, thisArg)
otherwise Bailout;
}
}
label slow {
label Slow {
goto Bailout(k);
}
return Undefined;
......
......@@ -296,6 +296,40 @@ extern macro ExtractFixedArray(
extern builtin ExtractFastJSArray(Context, JSArray, Smi, Smi): JSArray;
macro LoadElementNoHole<T : type>(a: JSArray, index: Smi): Object
labels IfHole {
unreachable;
}
LoadElementNoHole<FixedArray>(a: JSArray, index: Smi): Object
labels IfHole {
try {
let elements: FixedArray =
cast<FixedArray>(a.elements) otherwise Unexpected;
let e: Object = elements[index];
if (e == Hole) {
goto IfHole;
}
return e;
}
label Unexpected {
unreachable;
}
}
LoadElementNoHole<FixedDoubleArray>(a: JSArray, index: Smi): Object
labels IfHole {
try {
let elements: FixedDoubleArray =
cast<FixedDoubleArray>(a.elements) otherwise Unexpected;
let e: float64 = LoadDoubleWithHoleCheck(elements, index) otherwise IfHole;
return AllocateHeapNumberWithValue(e);
}
label Unexpected {
unreachable;
}
}
macro HasPropertyObject(
o: Object, p: Object, c: Context, f: HasPropertyFlag): Oddball {
try {
......
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