[array] Move Array.p.copyWithin to Torque
This CL adds a baseline Torque implementation without fast-paths for Array.p.copyWithin. The JS version in array.js is removed. R=cbruni@chromium.org, jgruber@chromium.org Bug: v8:7624 Change-Id: Ie53047883a65dd9310ea8f8d0edb440f431044ea Reviewed-on: https://chromium-review.googlesource.com/1165223 Reviewed-by: Jakob Gruber <jgruber@chromium.org> Reviewed-by: Camillo Bruni <cbruni@chromium.org> Commit-Queue: Simon Zünd <szuend@google.com> Cr-Commit-Position: refs/heads/master@{#55000}
This commit is contained in:
parent
01c289aa59
commit
c112c962c4
1
BUILD.gn
1
BUILD.gn
@ -870,6 +870,7 @@ action("postmortem-metadata") {
|
||||
torque_files = [
|
||||
"src/builtins/base.tq",
|
||||
"src/builtins/array.tq",
|
||||
"src/builtins/array-copywithin.tq",
|
||||
"src/builtins/array-foreach.tq",
|
||||
"src/builtins/array-sort.tq",
|
||||
"src/builtins/typed-array.tq",
|
||||
|
@ -1722,6 +1722,8 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
|
||||
|
||||
SimpleInstallFunction(isolate_, proto, "concat", Builtins::kArrayConcat, 1,
|
||||
false);
|
||||
SimpleInstallFunction(isolate_, proto, "copyWithin",
|
||||
Builtins::kArrayPrototypeCopyWithin, 2, false);
|
||||
SimpleInstallFunction(isolate_, proto, "fill",
|
||||
Builtins::kArrayPrototypeFill, 1, false);
|
||||
SimpleInstallFunction(isolate_, proto, "find",
|
||||
|
93
src/builtins/array-copywithin.tq
Normal file
93
src/builtins/array-copywithin.tq
Normal file
@ -0,0 +1,93 @@
|
||||
// Copyright 2018 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
module array {
|
||||
macro ConvertToRelativeIndex(index: Number, length: Number): Number {
|
||||
return index < 0 ? max(index + length, 0) : min(index, length);
|
||||
}
|
||||
|
||||
// https://tc39.github.io/ecma262/#sec-array.prototype.copyWithin
|
||||
javascript builtin ArrayPrototypeCopyWithin(
|
||||
context: Context, receiver: Object, ...arguments): Object {
|
||||
// 1. Let O be ? ToObject(this value).
|
||||
const object: JSReceiver = ToObject(context, receiver);
|
||||
|
||||
// 2. Let len be ? ToLength(? Get(O, "length")).
|
||||
const length: Number = GetLengthProperty(context, object);
|
||||
|
||||
// 3. Let relativeTarget be ? ToInteger(target).
|
||||
const relative_target: Number = ToInteger_Inline(context, arguments[0]);
|
||||
|
||||
// 4. If relativeTarget < 0, let to be max((len + relativeTarget), 0);
|
||||
// else let to be min(relativeTarget, len).
|
||||
let to: Number = ConvertToRelativeIndex(relative_target, length);
|
||||
|
||||
// 5. Let relativeStart be ? ToInteger(start).
|
||||
const relative_start: Number = ToInteger_Inline(context, arguments[1]);
|
||||
|
||||
// 6. If relativeStart < 0, let from be max((len + relativeStart), 0);
|
||||
// else let from be min(relativeStart, len).
|
||||
let from: Number = ConvertToRelativeIndex(relative_start, length);
|
||||
|
||||
// 7. If end is undefined, let relativeEnd be len;
|
||||
// else let relativeEnd be ? ToInteger(end).
|
||||
let relative_end: Number = length;
|
||||
if (arguments[2] != Undefined) {
|
||||
relative_end = ToInteger_Inline(context, arguments[2]);
|
||||
}
|
||||
|
||||
// 8. If relativeEnd < 0, let final be max((len + relativeEnd), 0);
|
||||
// else let final be min(relativeEnd, len).
|
||||
const final: Number = ConvertToRelativeIndex(relative_end, length);
|
||||
|
||||
// 9. Let count be min(final-from, len-to).
|
||||
let count: Number = min(final - from, length - to);
|
||||
|
||||
// 10. If from<to and to<from+count, then.
|
||||
let direction: Number = 1;
|
||||
|
||||
if (from < to && to < (from + count)) {
|
||||
// a. Let direction be -1.
|
||||
direction = -1;
|
||||
|
||||
// b. Let from be from + count - 1.
|
||||
from = from + count - 1;
|
||||
|
||||
// c. Let to be to + count - 1.
|
||||
to = to + count - 1;
|
||||
}
|
||||
|
||||
// 12. Repeat, while count > 0.
|
||||
while (count > 0) {
|
||||
// a. Let fromKey be ! ToString(from).
|
||||
// b. Let toKey be ! ToString(to).
|
||||
// c. Let fromPresent be ? HasProperty(O, fromKey).
|
||||
const from_present: Boolean = HasProperty(context, from, object);
|
||||
|
||||
// d. If fromPresent is true, then.
|
||||
if (from_present == True) {
|
||||
// i. Let fromVal be ? Get(O, fromKey).
|
||||
const from_val: Object = GetProperty(context, object, from);
|
||||
|
||||
// ii. Perform ? Set(O, toKey, fromVal, true).
|
||||
SetProperty(context, object, to, from_val, kStrict);
|
||||
} else {
|
||||
// i. Perform ? DeletePropertyOrThrow(O, toKey).
|
||||
DeleteProperty(context, object, to, kStrict);
|
||||
}
|
||||
|
||||
// f. Let from be from + direction.
|
||||
from = from + direction;
|
||||
|
||||
// g. Let to be to + direction.
|
||||
to = to + direction;
|
||||
|
||||
// h. Let count be count - 1.
|
||||
--count;
|
||||
}
|
||||
|
||||
// 13. Return O.
|
||||
return object;
|
||||
}
|
||||
}
|
@ -862,62 +862,6 @@ DEFINE_METHOD_LEN(
|
||||
);
|
||||
|
||||
|
||||
// ES#sec-array.prototype.copywithin
|
||||
// (Array.prototype.copyWithin ( target, start [ , end ] )
|
||||
DEFINE_METHOD_LEN(
|
||||
GlobalArray.prototype,
|
||||
copyWithin(target, start, end) {
|
||||
var array = TO_OBJECT(this);
|
||||
var length = TO_LENGTH(array.length);
|
||||
|
||||
target = TO_INTEGER(target);
|
||||
var to;
|
||||
if (target < 0) {
|
||||
to = MathMax(length + target, 0);
|
||||
} else {
|
||||
to = MathMin(target, length);
|
||||
}
|
||||
|
||||
start = TO_INTEGER(start);
|
||||
var from;
|
||||
if (start < 0) {
|
||||
from = MathMax(length + start, 0);
|
||||
} else {
|
||||
from = MathMin(start, length);
|
||||
}
|
||||
|
||||
end = IS_UNDEFINED(end) ? length : TO_INTEGER(end);
|
||||
var final;
|
||||
if (end < 0) {
|
||||
final = MathMax(length + end, 0);
|
||||
} else {
|
||||
final = MathMin(end, length);
|
||||
}
|
||||
|
||||
var count = MathMin(final - from, length - to);
|
||||
var direction = 1;
|
||||
if (from < to && to < (from + count)) {
|
||||
direction = -1;
|
||||
from = from + count - 1;
|
||||
to = to + count - 1;
|
||||
}
|
||||
|
||||
while (count > 0) {
|
||||
if (from in array) {
|
||||
array[to] = array[from];
|
||||
} else {
|
||||
delete array[to];
|
||||
}
|
||||
from = from + direction;
|
||||
to = to + direction;
|
||||
count--;
|
||||
}
|
||||
|
||||
return array;
|
||||
},
|
||||
2 /* Set function length */
|
||||
);
|
||||
|
||||
|
||||
// Set up unscopable properties on the Array.prototype object.
|
||||
var unscopables = {
|
||||
|
Loading…
Reference in New Issue
Block a user