v8/test/mjsunit/es6/collections-constructor-custom-iterator.js
Ross McIlroy 4a395137a1 [Test] Make collections-constructor-custom-iterator resilient to GCs.
The optimized code for TestSetWithCustomIterator holds a weak reference to the map
for the entries object. If this is collected by the GC then the optimized code deopts
which causes the test to fail. To prevent this, hold onto an entires object to keep
it's map alive.

Change-Id: I5796e74fc1d7c5061bf8c98f7a82fe582d6be76a
Reviewed-on: https://chromium-review.googlesource.com/c/1357043
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Commit-Queue: Ross McIlroy <rmcilroy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57978}
2018-11-30 18:07:14 +00:00

74 lines
2.2 KiB
JavaScript

// 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 --opt
var global;
function TestSetWithCustomIterator(ctor) {
const k1 = {};
const k2 = {};
const entries = [k1];
let callCount = 0;
entries[Symbol.iterator] = () => ({
next: () =>
callCount++ === 0
? { value: k2, done: false }
: { done: true }
});
const set = new ctor(entries);
assertFalse(set.has(k1));
assertTrue(set.has(k2));
assertEquals(2, callCount);
// Keep entries alive to avoid collection of the weakly held map in optimized
// code which causes the code to deopt.
global = entries;
}
TestSetWithCustomIterator(Set);
TestSetWithCustomIterator(Set);
TestSetWithCustomIterator(Set);
%OptimizeFunctionOnNextCall(TestSetWithCustomIterator);
TestSetWithCustomIterator(Set);
assertOptimized(TestSetWithCustomIterator);
TestSetWithCustomIterator(WeakSet);
TestSetWithCustomIterator(WeakSet);
TestSetWithCustomIterator(WeakSet);
%OptimizeFunctionOnNextCall(TestSetWithCustomIterator);
TestSetWithCustomIterator(WeakSet);
assertOptimized(TestSetWithCustomIterator);
function TestMapWithCustomIterator(ctor) {
const k1 = {};
const k2 = {};
const entries = [[k1, 1]];
let callCount = 0;
entries[Symbol.iterator] = () => ({
next: () =>
callCount++ === 0
? { value: [k2, 2], done: false }
: { done: true }
});
const map = new ctor(entries);
assertFalse(map.has(k1));
assertEquals(2, map.get(k2));
assertEquals(2, callCount);
// Keep entries alive to avoid collection of the weakly held map in optimized
// code which causes the code to deopt.
global = entries;
}
TestMapWithCustomIterator(Map);
TestMapWithCustomIterator(Map);
TestMapWithCustomIterator(Map);
%OptimizeFunctionOnNextCall(TestMapWithCustomIterator);
TestMapWithCustomIterator(Map);
assertOptimized(TestMapWithCustomIterator);
TestMapWithCustomIterator(WeakMap);
TestMapWithCustomIterator(WeakMap);
TestMapWithCustomIterator(WeakMap);
%OptimizeFunctionOnNextCall(TestMapWithCustomIterator);
TestMapWithCustomIterator(WeakMap);
assertOptimized(TestMapWithCustomIterator);