Function.prototype.call returns the function itself instead of the real return value
I have convinced myself, that I am looking at an optimization issue in EdgeHTML 14. The most annoying part is, that it doesn't reproduce when the F12 tools are open.
I'm building an application including the new d3 version 4, angular2, and core-js' es6 modules (amongst other things). Everything is bundled with webpack. I have tried to reduce the problem by editing the webpack-generated JavaScript and removing some of the moving parts, but that seems to fix it. I'll try to describe my environment, some code snippets etc., but I wouldn't expect this to be easy to reproduce.
The problem appears in the range function, more exactly in the range = slice.call(_) assignment in the first branch. slice is imported from array.js and is just Array.prototype.slice.
The first three times this function gets called (when arguments.length === 1, an array with two elements), the .call(_) returns the expected copy of that array. The fourth time, range gets function slice() { [native code] } assigned instead.
With webpack in the picture, here is what the browser actually executes:
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__array__ = __webpack_require__(22);
// […]
scale.range = function(_) {
return arguments.length ? (range = __WEBPACK_IMPORTED_MODULE_2__array__["a" /* slice */].call(_), rescale()) : range.slice();
};
or my debug version:
scale.range = function (_) {
var result;
if (arguments.length) {
console.log('change!');
console.log(range);
range = __WEBPACK_IMPORTED_MODULE_2__array__["a" /* slice */].call(_);
console.log(range);
result = rescale();
}
else {
result = range.slice();
}
return result;
};
I have rerouted console.log() and console.error() to append the logs to a DOM element, so I have a chance to see what happened when I open the F12 tools after things blew up. The log is:
Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
change!
0,1
278.3333333333333,0
change!
0,1
0,885
change!
0,1
278.3333333333333,0
change!
0,1
function slice() { [native code] }
Continuing the program executing then runs into an exception when the range function is called without arguments:
TypeError: Object doesn't support property or method 'slice'
at scale.range (…)
at …
I have verified, that the webpack imported slice function is always the same.
I have tried to inline the webpack infrastructure, but that fixes the problem.
The same code works beautifully in Chrome.
The code works (much slower) when the F12 are open.
Help!
@Tragetaschen would you mind providing a jsfiddle or some other mechanism by which we can run the full repro to confirm the issue (and later confirm whether we have fixed it)?
For example I put this together based on your description:
Object.prototype.blah = function() {
console.log(this);
console.log("hello");
return 42;
};
var obj = {}
for (var i = 0; i < 5; i++) {
console.log(Object.blah.call(obj));
}
But it does not repro the issue. I think something more subtle is happening in your example.
I was hoping for 'yeah, that's known problem x and a solution is in flight', but the second best thing is 'looks like an actual bug' so the time for the next steps is well spent. :)
I'll do my best to reduce it tomorrow.
@Tragetaschen also, can your provide the OS version e.g. 10.0.x.y, Edge version e.g. 38.x.0.0, and EdgeHTML version e.g. 14.x?
OS: 1607 / 14393.51 Microsoft Edge 38.14393.0.0 Microsoft EdgeHTML 14.14393
This is WEIRD! I have reduced the problem somewhat to the following entry point snippet from my webpack build:
var platform_browser_dynamic_1 = __webpack_require__(184);
var AppComponent_1 = __webpack_require__(929);
var continuous = __webpack_require__(54)["a"]; // 1
var s = continuous().range([1, 0]); // 2
for (var i = 0; i < 96; ++i) // 3
s.range();
platform_browser_dynamic_1.bootstrap(AppComponent_1.AppComponent, []); // 4
s.range([1, 0]); // 5
var continuous at (1) is d3's continuous.js default export, my AppComponent is as bare as it can get (see below), the call at (5) is my sentinel, that screws up the internal state or doesn't.
In that setup, all parts are still required:
When I remove the range-call at (2), it doesn't break.
When I do less than 96 calls to range() in (3), it doesn't break.
When I move the bootstrap call (4) to other positions (e.g. before the for loop or after (5)), it doesn't break. That means initiating the angular 2 bootstrap is currently a requirement to trigger this.
Attachments
var __decorate = ... // TypeScript's standard function
var core_1 = __webpack_require__(0);
var AppComponent = (function () {
function AppComponent() {}
AppComponent = __decorate([
core_1.Component({
selector: 'app',
template: '<div>Hello World</div>',
})
], AppComponent);
return AppComponent;
var _a;
}());
exports.AppComponent = AppComponent;
It doesn't get better:
// [...] webpack module infrastructure
var continuous = __webpack_require__(54)["a"];
var s = continuous().range([1, 0]);
for (var i = 0; i < 1000; ++i) // 3
s.range();
s.range([1, 0]); // 5
I'm able to remove remove the bootstrap call at (4) (which kind-of removes the Angular dependency). Then, I have to increase the loop at (3) to 1000 to make it fail reliably.
I tried to narrow down that number, but when I thought I had a good pair I noticed that with each refresh of the page I get a different result: With a loop counter of 600, sometimes the sentinel at (5) breaks and sometimes it doesn't (about 50/50).
I have created https://github.com/Tragetaschen/ms-edge-d3-scale-issue-1415
I'm able to either fail (1000 iterations) or succeed (100 iterations) depending on what is active in main.js.
I have tried to reproduce this without relying on vendor.js and webpack's CommonsChunkPlugin, but apparently, this file split seems to be a requirement as well.
This sounds like it could be something weird happening with JIT, code size, or hosting.
Maybe @pleath and @yongqu could weigh in?
OS: 1511 / 10586.494 Microsoft Edge 25.10586.0.0 Microsoft EdgeHTML 13.10586
The application breaks the same as on my 1607 machine. I didn't fully verify that it's actually the same problem (by writing the logs to a DOM elements), though, but it behaves exactly the same. Firefox on that machine works just fine with my application.
@Tragetaschen Thanks for the additional info!
How is this going to go forward?
I know it's a complicated thing to ask, but I would greatly appreciate a ball park estimate. I need to make a decision regarding my application how I'm going to proceed, and the decision varies depending on whether it's "a KB number on a Patch Tuesday" or "in the next OS update".
Can't say for sure until we address the severity of the issue. Is it possible to re-write portions of your app to work around this issue?
If I have to (read "next OS update"), I'll inline (parts of) the d3 code instead of letting it run through webpack. This seemed to circumvent this bug when I started to reduce the issue to the reproduction. This still is a sword of Damocles situation, though :)
This looks to be a JIT issue. We are investigating.
We've just spent most of today hunting this bug too. A quick workaround is to put the following snippet at the top of your page:
<script>
Function.prototype.call = function(t) {
return this.apply(t, Array.prototype.slice.apply(arguments, [1]));
}
</script>
Whatever breaks 'call' seems to not affect 'apply'.
@Tragetaschen, I'm looking into this right now. I was able to repro the issue on the OS build you were trying it on. However, it seems fixed in ChakraCore master. I'm trying to figure out what fixed it and when.
Never mind that, it does repro in latest ChakraCore. I'm investigating.
Ok, figured out the issue after some heavy-duty reducing - it is an issue in the inliner of the JIT when the target of a .call has a getter. We were ending up inlining the getter instead of its return value. @ccantill the issue does not repro with your snippet because of some restrictions on the inlining of .apply and its target.
I am hoping to have a fix out shortly.
That's awesome.
Any chance for a backport to 1607 or 1511? Any address for a bribery cake?
This one will only flow out to future flights.
@EdMaurer What does that mean "This one will only flow out to future flights.". Is that a reference to some release date? Why was this closed?