Commit d0ef84b3 authored by neis's avatar neis Committed by Commit bot

[proxies] Make Array.prototype.concat work correctly with proxies.

R=rossberg
BUG=v8:1543
LOG=n

Review URL: https://codereview.chromium.org/1525983002

Cr-Commit-Position: refs/heads/master@{#32900}
parent 2bb51df9
This diff is collapsed.
......@@ -219,15 +219,6 @@ function AddIndexedProperty(obj, index, value) {
%SetForceInlineFlag(AddIndexedProperty);
// ES6, draft 10-14-14, section 22.1.3.1.1
function IsConcatSpreadable(O) {
if (!IS_SPEC_OBJECT(O)) return false;
var spreadable = O[isConcatSpreadableSymbol];
if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O);
return TO_BOOLEAN(spreadable);
}
function ToPositiveInteger(x, rangeErrorIndex) {
var i = TO_INTEGER_MAP_MINUS_ZERO(x);
if (i < 0) throw MakeRangeError(rangeErrorIndex);
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-concat-spreadable
// Flags: --harmony-concat-spreadable --harmony-proxies --harmony-reflect
(function testArrayConcatArity() {
"use strict";
......@@ -705,4 +705,170 @@ function testConcatTypedArray(type, elems, modulo) {
var r4 = [0].concat(arr3, arr3);
assertEquals(1 + arr3.length * 2, r4.length);
assertEquals(expectedTrace, trace);
// Clean up.
delete Array.prototype[123];
delete Array.prototype["123"];
delete Array.prototype["moe"];
})();
////////////////////////////////////////////////////////////////////////////////
// Tests with proxies
// Note: concat does not currently support species so there is no difference
// between [].concat(foo) and Array.prototype.concat.apply(foo).
var log = [];
var logger = {};
var handler = new Proxy({}, logger);
logger.get = function(t, trap, r) {
return function(...args) {
log.push([trap, ...args]);
return Reflect[trap](...args);
}
};
(function testUnspreadableNonArrayLikeProxy() {
var target = {0: "a", 1: "b"};
var obj = new Proxy(target, handler);
log.length = 0;
assertEquals([obj], [].concat(obj));
assertEquals(1, log.length);
for (var i in log) assertSame(target, log[i][1]);
assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]);
log.length = 0;
assertEquals([obj], Array.prototype.concat.apply(obj));
assertEquals(1, log.length);
for (var i in log) assertSame(target, log[i][1]);
assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]);
})();
(function testSpreadableNonArrayLikeProxy() {
var target = {0: "a", 1: "b", [Symbol.isConcatSpreadable]: "truish"};
var obj = new Proxy(target, handler);
log.length = 0;
assertEquals([], [].concat(obj));
assertEquals(2, log.length);
for (var i in log) assertSame(target, log[i][1]);
assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]);
assertEquals(["get", target, "length", obj], log[1]);
log.length = 0;
assertEquals([], Array.prototype.concat.apply(obj));
assertEquals(2, log.length);
for (var i in log) assertSame(target, log[i][1]);
assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]);
assertEquals(["get", target, "length", obj], log[1]);
target.length = 3;
log.length = 0;
assertEquals(["a", "b", undefined], [].concat(obj));
assertEquals(7, log.length);
for (var i in log) assertSame(target, log[i][1]);
assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]);
assertEquals(["get", target, "length", obj], log[1]);
assertEquals(["has", target, "0"], log[2]);
assertEquals(["get", target, "0", obj], log[3]);
assertEquals(["has", target, "1"], log[4]);
assertEquals(["get", target, "1", obj], log[5]);
assertEquals(["has", target, "2"], log[6]);
log.length = 0;
assertEquals(["a", "b", undefined], Array.prototype.concat.apply(obj));
assertEquals(7, log.length);
for (var i in log) assertSame(target, log[i][1]);
assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]);
assertEquals(["get", target, "length", obj], log[1]);
assertEquals(["has", target, "0"], log[2]);
assertEquals(["get", target, "0", obj], log[3]);
assertEquals(["has", target, "1"], log[4]);
assertEquals(["get", target, "1", obj], log[5]);
assertEquals(["has", target, "2"], log[6]);
})();
(function testUnspreadableArrayLikeProxy() {
var target = ["a", "b"];
target[Symbol.isConcatSpreadable] = "";
var obj = new Proxy(target, handler);
log.length = 0;
assertEquals([obj], [].concat(obj));
assertEquals(1, log.length);
for (var i in log) assertSame(target, log[i][1]);
assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]);
log.length = 0;
assertEquals([obj], Array.prototype.concat.apply(obj));
assertEquals(1, log.length);
for (var i in log) assertSame(target, log[i][1]);
assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]);
})();
(function testSpreadableArrayLikeProxy() {
var target = ["a", "b"];
target[Symbol.isConcatSpreadable] = undefined;
var obj = new Proxy(target, handler);
log.length = 0;
assertEquals(["a", "b"], [].concat(obj));
assertEquals(6, log.length);
for (var i in log) assertSame(target, log[i][1]);
assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]);
assertEquals(["get", target, "length", obj], log[1]);
assertEquals(["has", target, "0"], log[2]);
assertEquals(["get", target, "0", obj], log[3]);
assertEquals(["has", target, "1"], log[4]);
assertEquals(["get", target, "1", obj], log[5]);
log.length = 0;
assertEquals(["a", "b"], Array.prototype.concat.apply(obj));
assertEquals(6, log.length);
for (var i in log) assertSame(target, log[i][1]);
assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]);
assertEquals(["get", target, "length", obj], log[1]);
assertEquals(["has", target, "0"], log[2]);
assertEquals(["get", target, "0", obj], log[3]);
assertEquals(["has", target, "1"], log[4]);
assertEquals(["get", target, "1", obj], log[5]);
})();
(function testSpreadableArrayLikeProxyWithNontrivialLength() {
var getTrap = function(t, key) {
if (key === "length") return {[Symbol.toPrimitive]() {return 3}};
if (key === "2") return "baz";
if (key === "3") return "bar";
};
var target = [];
var obj = new Proxy(target, {get: getTrap, has: () => true});
assertEquals([undefined, undefined, "baz"], [].concat(obj));
assertEquals([undefined, undefined, "baz"], Array.prototype.concat.apply(obj))
})();
(function testSpreadableArrayLikeProxyWithBogusLength() {
var getTrap = function(t, key) {
if (key === "length") return Symbol();
if (key === "2") return "baz";
if (key === "3") return "bar";
};
var target = [];
var obj = new Proxy(target, {get: getTrap, has: () => true});
assertThrows(() => [].concat(obj), TypeError);
assertThrows(() => Array.prototype.concat.apply(obj), TypeError);
})();
......@@ -421,7 +421,7 @@ var target = [];
var proxy = new Proxy(target, {get: getTrap});
var replacer = (key, val) => key === "goo" ? proxy : val;
var object = {foo: true, goo: false};
assertThrows(() => JSON.stringify(object, replacer));
assertThrows(() => JSON.stringify(object, replacer), TypeError);
// Replacer returns a callable proxy
......
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