2017-06-07 13:23:33 +00:00
|
|
|
// 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 --expose-gc --turbo-inline-array-builtins
|
|
|
|
|
2019-06-12 14:00:50 +00:00
|
|
|
var a = [
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
|
|
|
|
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0
|
|
|
|
];
|
|
|
|
var b = [
|
|
|
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
|
|
|
|
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
|
|
|
|
];
|
|
|
|
var c = [
|
|
|
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
|
|
|
|
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
|
|
|
|
];
|
2017-06-07 13:23:33 +00:00
|
|
|
|
|
|
|
// Unknown field access leads to soft-deopt unrelated to forEach, should still
|
|
|
|
// lead to correct result.
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var result = 0;
|
|
|
|
var eagerDeoptInCalled = function(deopt) {
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += v;
|
|
|
|
if (i == 13 && deopt) {
|
|
|
|
a.abc = 25;
|
2017-06-07 13:23:33 +00:00
|
|
|
}
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
a.forEach(sum);
|
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(eagerDeoptInCalled);
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
%OptimizeFunctionOnNextCall(eagerDeoptInCalled);
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
eagerDeoptInCalled(true);
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
assertEquals(1500, result);
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Length change detected during loop, must cause properly handled eager deopt.
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var result = 0;
|
|
|
|
var eagerDeoptInCalled = function(deopt) {
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += v;
|
|
|
|
a.length = i == 13 && deopt ? 25 : 27;
|
|
|
|
};
|
|
|
|
a.forEach(sum);
|
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(eagerDeoptInCalled);
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
%OptimizeFunctionOnNextCall(eagerDeoptInCalled);
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
eagerDeoptInCalled(true);
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
assertEquals(1500, result);
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Escape analyzed array
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var result = 0;
|
|
|
|
var eagerDeoptInCalled = function(deopt) {
|
|
|
|
var a_noescape = [0, 1, 2, 3, 4, 5];
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += v;
|
|
|
|
if (i == 13 && deopt) {
|
|
|
|
a_noescape.length = 25;
|
2017-06-07 13:23:33 +00:00
|
|
|
}
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
a_noescape.forEach(sum);
|
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(eagerDeoptInCalled);
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
%OptimizeFunctionOnNextCall(eagerDeoptInCalled);
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
eagerDeoptInCalled(true);
|
|
|
|
eagerDeoptInCalled();
|
|
|
|
assertEquals(75, result);
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Escape analyzed array where sum function isn't inlined, forcing a lazy deopt
|
|
|
|
// with GC that relies on the stashed-away return result fro the lazy deopt
|
|
|
|
// being properly stored in a place on the stack that gets GC'ed.
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var result = 0;
|
|
|
|
var lazyDeopt = function(deopt) {
|
|
|
|
var b = [1, 2, 3];
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += i;
|
|
|
|
if (i == 1 && deopt) {
|
|
|
|
%DeoptimizeFunction(lazyDeopt);
|
|
|
|
}
|
|
|
|
gc();
|
|
|
|
gc();
|
|
|
|
};
|
|
|
|
%NeverOptimizeFunction(sum);
|
|
|
|
b.forEach(sum);
|
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt();
|
|
|
|
%OptimizeFunctionOnNextCall(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt(true);
|
|
|
|
lazyDeopt();
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Lazy deopt from runtime call from inlined callback function.
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var result = 0;
|
|
|
|
var lazyDeopt = function(deopt) {
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += i;
|
|
|
|
if (i == 13 && deopt) {
|
|
|
|
%DeoptimizeNow();
|
2017-06-07 13:23:33 +00:00
|
|
|
}
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
b.forEach(sum);
|
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt();
|
|
|
|
%OptimizeFunctionOnNextCall(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt(true);
|
|
|
|
lazyDeopt();
|
|
|
|
assertEquals(1500, result);
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Lazy deopt from runtime call from non-inline callback function.
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var result = 0;
|
|
|
|
var lazyDeopt = function(deopt) {
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += i;
|
|
|
|
if (i == 13 && deopt) {
|
|
|
|
%DeoptimizeNow();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
%NeverOptimizeFunction(sum);
|
|
|
|
b.forEach(sum);
|
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt();
|
|
|
|
%OptimizeFunctionOnNextCall(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt(true);
|
|
|
|
lazyDeopt();
|
|
|
|
assertEquals(1500, result);
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var result = 0;
|
|
|
|
var lazyDeopt = function(deopt) {
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += i;
|
|
|
|
if (i == 13 && deopt) {
|
|
|
|
%DeoptimizeNow();
|
|
|
|
gc();
|
|
|
|
gc();
|
|
|
|
gc();
|
2017-06-07 13:23:33 +00:00
|
|
|
}
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
c.forEach(sum);
|
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt();
|
|
|
|
%OptimizeFunctionOnNextCall(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt(true);
|
|
|
|
lazyDeopt();
|
|
|
|
assertEquals(1500, result);
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Call to a.forEach is done inside a try-catch block and the callback function
|
|
|
|
// being called actually throws.
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var caught = false;
|
|
|
|
var result = 0;
|
|
|
|
var lazyDeopt = function(deopt) {
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += i;
|
|
|
|
if (i == 1 && deopt) {
|
|
|
|
throw 'a';
|
2017-06-07 13:23:33 +00:00
|
|
|
}
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
try {
|
|
|
|
c.forEach(sum);
|
|
|
|
} catch (e) {
|
|
|
|
caught = true;
|
2017-06-07 13:23:33 +00:00
|
|
|
}
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt();
|
|
|
|
%OptimizeFunctionOnNextCall(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
assertDoesNotThrow(lazyDeopt.bind(this, true));
|
|
|
|
assertTrue(caught);
|
|
|
|
lazyDeopt();
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Call to a.forEach is done inside a try-catch block and the callback function
|
|
|
|
// being called actually throws, but the callback is not inlined.
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var caught = false;
|
|
|
|
var result = 0;
|
|
|
|
var lazyDeopt = function(deopt) {
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += i;
|
|
|
|
if (i == 1 && deopt) {
|
|
|
|
throw 'a';
|
2017-06-07 13:23:33 +00:00
|
|
|
}
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
%NeverOptimizeFunction(sum);
|
|
|
|
try {
|
|
|
|
c.forEach(sum);
|
|
|
|
} catch (e) {
|
|
|
|
caught = true;
|
2017-06-07 13:23:33 +00:00
|
|
|
}
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt();
|
|
|
|
%OptimizeFunctionOnNextCall(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
assertDoesNotThrow(lazyDeopt.bind(this, true));
|
|
|
|
assertTrue(caught);
|
|
|
|
lazyDeopt();
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
2017-07-24 08:33:34 +00:00
|
|
|
// Call to a.forEach is done inside a try-catch block and the callback function
|
|
|
|
// being called throws into a deoptimized caller function.
|
|
|
|
(function TestThrowIntoDeoptimizedOuter() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var a = [1, 2, 3, 4];
|
2017-07-24 08:33:34 +00:00
|
|
|
var lazyDeopt = function(deopt) {
|
2019-06-12 14:00:50 +00:00
|
|
|
var sum = function(v, i, o) {
|
2017-07-24 08:33:34 +00:00
|
|
|
result += v;
|
|
|
|
if (i == 1 && deopt) {
|
|
|
|
%DeoptimizeFunction(lazyDeopt);
|
|
|
|
throw "some exception";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
%NeverOptimizeFunction(sum);
|
|
|
|
var result = 0;
|
|
|
|
try {
|
|
|
|
a.forEach(sum);
|
|
|
|
} catch (e) {
|
2019-06-12 14:00:50 +00:00
|
|
|
assertEquals('some exception', e);
|
2017-07-24 08:33:34 +00:00
|
|
|
result += 100;
|
|
|
|
}
|
|
|
|
return result;
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
;
|
|
|
|
%PrepareFunctionForOptimization(lazyDeopt);
|
2017-07-24 08:33:34 +00:00
|
|
|
assertEquals(10, lazyDeopt(false));
|
|
|
|
assertEquals(10, lazyDeopt(false));
|
|
|
|
assertEquals(103, lazyDeopt(true));
|
|
|
|
assertEquals(103, lazyDeopt(true));
|
|
|
|
%OptimizeFunctionOnNextCall(lazyDeopt);
|
|
|
|
assertEquals(10, lazyDeopt(false));
|
|
|
|
assertEquals(103, lazyDeopt(true));
|
|
|
|
})();
|
|
|
|
|
2017-06-07 13:23:33 +00:00
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var re = /Array\.forEach/;
|
|
|
|
var lazyDeopt = function foobar(deopt) {
|
|
|
|
var b = [1, 2, 3];
|
|
|
|
var result = 0;
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += v;
|
|
|
|
if (i == 1) {
|
|
|
|
var e = new Error();
|
|
|
|
assertTrue(re.exec(e.stack) !== null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var o = [1, 2, 3];
|
|
|
|
b.forEach(sum);
|
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt();
|
|
|
|
%OptimizeFunctionOnNextCall(lazyDeopt);
|
|
|
|
lazyDeopt();
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var re = /Array\.forEach/;
|
|
|
|
var lazyDeopt = function(deopt) {
|
|
|
|
var b = [1, 2, 3];
|
|
|
|
var result = 0;
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += v;
|
|
|
|
if (i == 1) {
|
|
|
|
var e = new Error();
|
|
|
|
assertTrue(re.exec(e.stack) !== null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
%NeverOptimizeFunction(sum);
|
|
|
|
var o = [1, 2, 3];
|
|
|
|
b.forEach(sum);
|
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt();
|
|
|
|
%OptimizeFunctionOnNextCall(lazyDeopt);
|
|
|
|
lazyDeopt();
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var re = /Array\.forEach/;
|
|
|
|
var lazyDeopt = function(deopt) {
|
|
|
|
var b = [1, 2, 3];
|
|
|
|
var result = 0;
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += v;
|
|
|
|
if (i == 1) {
|
|
|
|
%DeoptimizeNow();
|
|
|
|
} else if (i == 2) {
|
|
|
|
var e = new Error();
|
|
|
|
assertTrue(re.exec(e.stack) !== null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var o = [1, 2, 3];
|
|
|
|
b.forEach(sum);
|
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(lazyDeopt);
|
|
|
|
lazyDeopt();
|
|
|
|
lazyDeopt();
|
|
|
|
%OptimizeFunctionOnNextCall(lazyDeopt);
|
|
|
|
lazyDeopt();
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2019-06-12 14:00:50 +00:00
|
|
|
var re = /Array\.forEach/;
|
|
|
|
var a = [1, 2, 3];
|
|
|
|
var result = 0;
|
|
|
|
var lazyDeopt = function() {
|
|
|
|
var sum = function(v, i, o) {
|
|
|
|
result += i;
|
|
|
|
if (i == 1) {
|
|
|
|
%DeoptimizeFunction(lazyDeopt);
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
a.forEach(sum);
|
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(lazyDeopt);
|
|
|
|
assertThrows(() => lazyDeopt());
|
|
|
|
assertThrows(() => lazyDeopt());
|
|
|
|
try {
|
|
|
|
lazyDeopt();
|
|
|
|
} catch (e) {
|
|
|
|
assertTrue(re.exec(e.stack) !== null);
|
|
|
|
}
|
|
|
|
%OptimizeFunctionOnNextCall(lazyDeopt);
|
|
|
|
try {
|
|
|
|
lazyDeopt();
|
|
|
|
} catch (e) {
|
|
|
|
assertTrue(re.exec(e.stack) !== null);
|
|
|
|
}
|
2017-06-07 13:23:33 +00:00
|
|
|
})();
|
2018-01-02 12:24:11 +00:00
|
|
|
|
|
|
|
// Verify holes are skipped.
|
|
|
|
(() => {
|
|
|
|
const a = [1, 2, , 3, 4];
|
|
|
|
function withHoles() {
|
|
|
|
const callback_values = [];
|
|
|
|
a.forEach(v => {
|
|
|
|
callback_values.push(v);
|
|
|
|
});
|
|
|
|
return callback_values;
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(withHoles);
|
2018-01-02 12:24:11 +00:00
|
|
|
withHoles();
|
|
|
|
withHoles();
|
|
|
|
%OptimizeFunctionOnNextCall(withHoles);
|
|
|
|
assertArrayEquals([1, 2, 3, 4], withHoles());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(() => {
|
|
|
|
const a = [1.5, 2.5, , 3.5, 4.5];
|
|
|
|
function withHoles() {
|
|
|
|
const callback_values = [];
|
|
|
|
a.forEach(v => {
|
|
|
|
callback_values.push(v);
|
|
|
|
});
|
|
|
|
return callback_values;
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(withHoles);
|
2018-01-02 12:24:11 +00:00
|
|
|
withHoles();
|
|
|
|
withHoles();
|
|
|
|
%OptimizeFunctionOnNextCall(withHoles);
|
|
|
|
assertArrayEquals([1.5, 2.5, 3.5, 4.5], withHoles());
|
|
|
|
})();
|
2018-01-16 13:22:10 +00:00
|
|
|
|
|
|
|
// Ensure that we handle side-effects between load and call.
|
|
|
|
(() => {
|
2019-06-12 14:00:50 +00:00
|
|
|
function side_effect(a, b) {
|
|
|
|
if (b) a.foo = 3;
|
|
|
|
return a;
|
|
|
|
}
|
2018-01-16 13:22:10 +00:00
|
|
|
%NeverOptimizeFunction(side_effect);
|
|
|
|
|
|
|
|
function unreliable(a, b) {
|
|
|
|
let sum = 0;
|
|
|
|
return a.forEach(x => sum += x, side_effect(a, b));
|
2019-06-12 14:00:50 +00:00
|
|
|
};
|
|
|
|
%PrepareFunctionForOptimization(unreliable);
|
2018-01-16 13:22:10 +00:00
|
|
|
let a = [1, 2, 3];
|
|
|
|
unreliable(a, false);
|
|
|
|
unreliable(a, false);
|
|
|
|
%OptimizeFunctionOnNextCall(unreliable);
|
|
|
|
unreliable(a, false);
|
|
|
|
// Now actually do change the map.
|
|
|
|
unreliable(a, true);
|
|
|
|
})();
|