Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Unified Diff: test/mjsunit/harmony/array-concat.js

Issue 1525983002: [proxies] Make Array.prototype.concat work correctly with proxies. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/js/runtime.js ('k') | test/mjsunit/harmony/proxies-json.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/array-concat.js
diff --git a/test/mjsunit/harmony/array-concat.js b/test/mjsunit/harmony/array-concat.js
index 71b6790bc7b13e238930ed824d165d76ec903c2c..cabdf2df0817824015b480c7c2bbebe7bbd79110 100644
--- a/test/mjsunit/harmony/array-concat.js
+++ b/test/mjsunit/harmony/array-concat.js
@@ -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);
})();
« no previous file with comments | « src/js/runtime.js ('k') | test/mjsunit/harmony/proxies-json.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698