Skip to content

Commit

Permalink
detect and replace broken third-party Function#bind polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jan 5, 2022
1 parent cceecb7 commit b4534bd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Changelog
##### Unreleased
- Nothing
- Detects and replaces broken third-party `Function#bind` polyfills

##### 3.20.2 - 2022.01.02
- Added a fix of [a V8 ~ Chrome 36- `Object.{ defineProperty, defineProperties }` bug](https://bugs.chromium.org/p/v8/issues/detail?id=3334), [Babel issue](https://github.com/babel/babel/issues/14056)
Expand Down
9 changes: 7 additions & 2 deletions packages/core-js/internals/function-bind.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var global = require('../internals/global');
var uncurryThis = require('../internals/function-uncurry-this');
var fails = require('../internals/fails');
var aCallable = require('../internals/a-callable');
var isObject = require('../internals/is-object');
var hasOwn = require('../internals/has-own-property');
Expand All @@ -20,7 +21,11 @@ var construct = function (C, argsLength, args) {

// `Function.prototype.bind` method implementation
// https://tc39.es/ecma262/#sec-function.prototype.bind
module.exports = Function.bind || function bind(that /* , ...args */) {
module.exports = fails(function () {
// detect broken third-party polyfills
var Test = function () { /* empty */ };
return !(new (Test.bind())() instanceof Test) || (function (a, b) { return this + a + b; }).bind(1, 2)(3) !== 6;
}) ? function bind(that /* , ...args */) {
var F = aCallable(this);
var Prototype = F.prototype;
var partArgs = arraySlice(arguments, 1);
Expand All @@ -30,4 +35,4 @@ module.exports = Function.bind || function bind(that /* , ...args */) {
};
if (isObject(Prototype)) boundFunction.prototype = Prototype;
return boundFunction;
};
} : Function.bind;
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.function.bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ var bind = require('../internals/function-bind');

// `Function.prototype.bind` method
// https://tc39.es/ecma262/#sec-function.prototype.bind
$({ target: 'Function', proto: true }, {
$({ target: 'Function', proto: true, forced: Function.bind !== bind }, {
bind: bind
});
3 changes: 2 additions & 1 deletion tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,8 @@ GLOBAL.tests = {
return escape;
},
'es.function.bind': function () {
return Function.prototype.bind;
var Test = function () { /* empty */ };
return (new (Test.bind())() instanceof Test) && (function (a, b) { return this + a + b; }).bind(1, 2)(3) === 6;
},
'es.function.has-instance': [SYMBOLS_SUPPORT, function () {
return Symbol.hasInstance in Function.prototype;
Expand Down

0 comments on commit b4534bd

Please sign in to comment.