Ignore:
Timestamp:
May 26, 2015, 11:23:59 AM (10 years ago)
Author:
Yusuke Suzuki
Message:

[ES6] Implement Array.prototype.copyWithin
https://bugs.webkit.org/show_bug.cgi?id=145107

Reviewed by Darin Adler.

Source/JavaScriptCore:

This patch implements ES6 Array.prototype.copyWithin.
It is intended to be used for copying the region to the other region
in the callee array itself safely (like memmove, not memcpy).
This function is proposed in the context of WebGL.

  • builtins/Array.prototype.js:

(.maxWithPositives):
(.minWithMaybeNegativeZeroAndPositive):
(copyWithin):

  • runtime/ArrayPrototype.cpp:

(JSC::ArrayPrototype::finishCreation):

  • tests/stress/array-copywithin.js: Added.

(shouldBe):
(shouldBeArray):
(shouldThrow):
(arrayToObject):
(valueOf):

LayoutTests:

  • js/Object-getOwnPropertyNames-expected.txt:
  • js/script-tests/Object-getOwnPropertyNames.js:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/builtins/Array.prototype.js

    r184848 r184863  
    456456    return array;
    457457}
     458
     459function copyWithin(target, start)
     460{
     461    "use strict";
     462
     463    function maxWithPositives(a, b)
     464    {
     465        return (a < b) ? b : a;
     466    }
     467
     468    function minWithMaybeNegativeZeroAndPositive(maybeNegativeZero, positive)
     469    {
     470        return (maybeNegativeZero < positive) ? maybeNegativeZero : positive;
     471    }
     472
     473    var thisValue = this;
     474    if (thisValue === null || thisValue === undefined)
     475        throw new @TypeError("Array.copyWithin requires that |this| be not null or undefined");
     476    var thisObject = @Object(thisValue);
     477
     478    var length = @ToLength(thisObject.length);
     479
     480    var relativeTarget = @ToInteger(target);
     481    var to = (relativeTarget < 0) ? maxWithPositives(length + relativeTarget, 0) : minWithMaybeNegativeZeroAndPositive(relativeTarget, length);
     482
     483    var relativeStart = @ToInteger(start);
     484    var from = (relativeStart < 0) ? maxWithPositives(length + relativeStart, 0) : minWithMaybeNegativeZeroAndPositive(relativeStart, length);
     485
     486    var relativeEnd;
     487    if (arguments.length >= 3) {
     488        var end = arguments[2];
     489        if (end === undefined)
     490            relativeEnd = length;
     491        else
     492            relativeEnd = @ToInteger(end);
     493    } else
     494        relativeEnd = length;
     495
     496    var finalValue = (relativeEnd < 0) ? maxWithPositives(length + relativeEnd, 0) : minWithMaybeNegativeZeroAndPositive(relativeEnd, length);
     497
     498    var count = minWithMaybeNegativeZeroAndPositive(finalValue - from, length - to);
     499
     500    var direction = 1;
     501    if (from < to && to < from + count) {
     502        direction = -1;
     503        from = from + count - 1;
     504        to = to + count - 1;
     505    }
     506
     507    for (var i = 0; i < count; ++i, from += direction, to += direction) {
     508        if (from in thisObject)
     509            thisObject[to] = thisObject[from];
     510        else
     511            delete thisObject[to];
     512    }
     513
     514    return thisObject;
     515}
Note: See TracChangeset for help on using the changeset viewer.