v8/test/mjsunit/es6/call-with-spread-modify-next.js
Caitlin Potter 2d889aa9a4 Reland "[esnext] load iterator.next only once at beginning of iteration"
https://github.com/tc39/ecma262/pull/988 gained concensus during the
september 2017 TC39 meetings. This moves the load of the "next" method
to the very beginning of the iteration protocol, rather than during
each iteration step.

This impacts:

- yield*
- for-of loops
- spread arguments
- array spreads

In the v8 implementation, this also affects async iteration versions of
these things (the sole exception being the Async-From-Sync iterator,
which requires a few more changes to work with this, likely done in a
followup patch).

This change introduces a new AST node, ResolvedProperty, which can be used
as a callee by Call nodes to produce the same bytecode as Property calls,
without observably re-loading the property. This is used in several
AST-desugarings involving the iteration protocol.

BUG=v8:6861, v8:5699
R=rmcilroy@chromium.org
TBR=neis@chromium.org, adamk@chromium.org

Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: I9685db6e85315ba8a2df87a4537c2bf491e1e35b
Reviewed-on: https://chromium-review.googlesource.com/857593
Commit-Queue: Caitlin Potter <caitp@igalia.com>
Reviewed-by: Ross McIlroy <rmcilroy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50518}
2018-01-11 20:27:13 +00:00

45 lines
950 B
JavaScript

// Copyright 2017 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 modifyNext() {
'use strict';
var a = [];
var ai = a[Symbol.iterator]();
var original_next = ai.__proto__['next'];
function maxWithZero(...args) {
return Math.max(0, ...args);
}
function testMax(x, y) {
return maxWithZero(x, y);
}
testMax(1, 2);
testMax(1, 2);
%OptimizeFunctionOnNextCall(testMax);
var r = testMax(1, 2);
assertEquals(2, r);
var called = 0;
Object.defineProperty(ai.__proto__, 'next', {
get: function() {
called++;
return original_next;
}
});
var r2 = testMax(1, 2);
// .next() is only loaded once during the iteration prologue (see
// https://github.com/tc39/ecma262/pull/988/ and v8:6861)
assertEquals(1, called);
assertEquals(2, r2);
})();