paulmillr / es6-shim

ECMAScript 6 compatibility shims for legacy JS engines
http://paulmillr.com
MIT License
3.11k stars 387 forks source link

Function.prototype.name getter brokes function name getter #454

Closed ColCh closed 5 years ago

ColCh commented 5 years ago

This code breaks Function.prototype.name in 100% of cases

var funcNameBeforeProtoCall = (function bar() {}).name
var funcNameBeforeProtoCallEquals = funcNameBeforeProtoCall === 'bar';
debugger;
Function.prototype.name;
var funcNameAfterProtoCall = (function foo() {}).name
var funcNameAfterProtoCallEquals = funcNameAfterProtoCall === 'foo';
debugger

launchable code: https://codepen.io/ColCh/pen/Oqpqqp

Try it in IE 11 and this would be resulted in this:

image

WHY ?!

Because this code is executed against this variable of Function.prototype

https://github.com/paulmillr/es6-shim/blob/271142ca5c58b7bfec6970b1c070515da0d8ef98/es6-sham.js#L158-L159

and this code assigns getter with for Function.prototype name property with null value.

https://github.com/paulmillr/es6-shim/blob/271142ca5c58b7bfec6970b1c070515da0d8ef98/es6-sham.js#L160-L165

Solution: skip defineProperty for null names

ljharb commented 5 years ago

ah, that's a really good catch - I don't think we ever expected anyone would refer to the name getter directly. Fix incoming.

ColCh commented 5 years ago

Omg that was fast! Thank you!