Commit 57a25168 authored by Georg Neis's avatar Georg Neis Committed by Commit Bot

Add more tests for array spread literals.

Also fix a bogus test.

R=bmeurer@chromium.org

Bug: v8:5940
Change-Id: I6e201ce35f9021e12d1e4b6127d4accc1425cdb9
Reviewed-on: https://chromium-review.googlesource.com/870596
Commit-Queue: Georg Neis <neis@chromium.org>
Reviewed-by: 's avatarBenedikt Meurer <bmeurer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50674}
parent 8888d5e4
// Copyright 2015 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.
// Flags: --allow-natives-syntax
// NOTE:
// Tests in this file are meant to run in the presence of an invalidated
// NoElementsProtector, as effected by the following line.
Array.prototype[0] = 42;
delete Array.prototype[0];
(function TestBasics() {
var a = [1, 2];
var b = [...a];
assertArrayEquals([1, 2], b)
assertArrayEquals(['a', 'b', 'c', 'd', 'e', 'f'],
['a', ...'bc', 'd', ...'ef'])
})();
var log = [];
function* gen(n) {
log.push(n, 1);
yield 1;
log.push(n, 2);
yield 2;
log.push(n, 3);
yield 3;
log.push(n, 'done');
}
function id(v) {
log.push(v);
return v;
}
(function TestGenerator() {
assertArrayEquals([1, 2, 3], [...gen('a')]);
assertArrayEquals(['x', 1, 2, 3, 'y', 1, 2, 3, 'z'],
['x', ...gen('a'), 'y', ...gen('b'), 'z']);
})();
(function TestOrderOfExecution() {
log = [];
assertArrayEquals(['x', 1, 2, 3, 'y', 1, 2, 3, 'z'],
[id('x'), ...gen('a'), id('y'), ...gen('b'), id('z')]);
assertArrayEquals([
'x', 'a', 1, 'a', 2, 'a', 3, 'a', 'done',
'y', 'b', 1, 'b', 2, 'b', 3, 'b', 'done',
'z'
], log);
})();
(function TestNotIterable() {
var a;
assertThrows(function() {
a = [...42];
}, TypeError);
assertSame(undefined, a);
})();
(function TestInvalidIterator() {
var iter = {
[Symbol.iterator]: 42
};
var a;
assertThrows(function() {
a = [...iter];
}, TypeError);
assertSame(undefined, a);
})();
(function TestIteratorNotAnObject() {
var iter = {
[Symbol.iterator]() {
return 42;
}
};
var a;
assertThrows(function() {
a = [...iter];
}, TypeError);
assertSame(undefined, a);
})();
(function TestIteratorNoNext() {
var iter = {
[Symbol.iterator]() {
return {};
}
};
var a;
assertThrows(function() {
a = [...iter];
}, TypeError);
assertSame(undefined, a);
})();
(function TestIteratorResultDoneThrows() {
function MyError() {}
var iter = {
[Symbol.iterator]() {
return {
next() {
return {
get done() {
throw new MyError();
}
}
}
};
}
};
var a;
assertThrows(function() {
a = [...iter];
}, MyError);
assertSame(undefined, a);
})();
(function TestIteratorResultValueThrows() {
function MyError() {}
var iter = {
[Symbol.iterator]() {
return {
next() {
return {
done: false,
get value() {
throw new MyError();
}
}
}
};
}
};
var a;
assertThrows(function() {
a = [...iter];
}, MyError);
assertSame(undefined, a);
})();
(function TestOptimize() {
function f() {
return [...'abc'];
}
assertArrayEquals(['a', 'b', 'c'], f());
%OptimizeFunctionOnNextCall(f);
assertArrayEquals(['a', 'b', 'c'], f());
})();
(function TestDeoptimize() {
var iter = {
[Symbol.iterator]() {
var i = 0;
return {
next() {
%DeoptimizeFunction(f);
return {value: ++i, done: i === 3};
}
};
}
};
function f() {
return [0, ...iter];
}
assertArrayEquals([0, 1, 2], f());
})();
(function TestPrototypeSetter1() {
Object.defineProperty(Array.prototype, 3, {set() {throw 666}})
Object.defineProperty(Array.prototype, 4, {set() {throw 666}})
function f() {
return ['a', ...['b', 'c', 'd'], 'e']
}
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
%OptimizeFunctionOnNextCall(f);
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
delete Array.prototype[3];
delete Array.prototype[4];
})();
(function TestPrototypeSetter2() {
Object.defineProperty(Array.prototype.__proto__, 3, {set() {throw 666}})
Object.defineProperty(Array.prototype.__proto__, 4, {set() {throw 666}})
function f() {
return ['a', ...['b', 'c', 'd'], 'e']
}
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
%OptimizeFunctionOnNextCall(f);
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
delete Array.prototype.__proto__[3];
delete Array.prototype.__proto__[4];
})();
(function TestPrototypeProxy() {
const backup = Array.prototype.__proto__;
Array.prototype.__proto__ = new Proxy({}, {set() {throw 666}});
function f() {
return ['a', ...['b', 'c', 'd'], 'e']
}
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
%OptimizeFunctionOnNextCall(f);
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
Object.setPrototypeOf(Array.prototype, backup);
})();
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
// Flags: --allow-natives-syntax // Flags: --allow-natives-syntax
// NOTE:
// Tests in this file are meant to run in the presence of a valid
// NoElementsProtector. Do not touch Array.prototype here.
(function TestBasics() { (function TestBasics() {
var a = [1, 2]; var a = [1, 2];
var b = [...a]; var b = [...a];
...@@ -165,7 +169,7 @@ function id(v) { ...@@ -165,7 +169,7 @@ function id(v) {
var i = 0; var i = 0;
return { return {
next() { next() {
$DeoptimizeFunction(f); %DeoptimizeFunction(f);
return {value: ++i, done: i === 3}; return {value: ++i, done: i === 3};
} }
}; };
...@@ -176,4 +180,4 @@ function id(v) { ...@@ -176,4 +180,4 @@ function id(v) {
} }
assertArrayEquals([0, 1, 2], f()); assertArrayEquals([0, 1, 2], f());
}); })();
// 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.
// Flags: --allow-natives-syntax
(function TestPrototypeProxy() {
const backup = Array.prototype.__proto__;
Array.prototype.__proto__ = new Proxy({}, {set() {throw 666}});
function f() {
return ['a', ...['b', 'c', 'd'], 'e']
}
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
%OptimizeFunctionOnNextCall(f);
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
Object.setPrototypeOf(Array.prototype, backup);
})();
// 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.
// Flags: --allow-natives-syntax
(function TestPrototypeSetter1() {
Object.defineProperty(Array.prototype, 3, {set() {throw 666}})
Object.defineProperty(Array.prototype, 4, {set() {throw 666}})
function f() {
return ['a', ...['b', 'c', 'd'], 'e']
}
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
%OptimizeFunctionOnNextCall(f);
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
delete Array.prototype[3];
delete Array.prototype[4];
})();
// 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.
// Flags: --allow-natives-syntax
(function TestPrototypeSetter2() {
Object.defineProperty(Array.prototype.__proto__, 3, {set() {throw 666}})
Object.defineProperty(Array.prototype.__proto__, 4, {set() {throw 666}})
function f() {
return ['a', ...['b', 'c', 'd'], 'e']
}
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
%OptimizeFunctionOnNextCall(f);
assertArrayEquals(['a', 'b', 'c', 'd', 'e'], f());
delete Array.prototype.__proto__[3];
delete Array.prototype.__proto__[4];
})();
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