Skip to content

Commit b4534bd

Browse files
committed
detect and replace broken third-party Function#bind polyfills
1 parent cceecb7 commit b4534bd

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Changelog
22
##### Unreleased
3-
- Nothing
3+
- Detects and replaces broken third-party `Function#bind` polyfills
44

55
##### 3.20.2 - 2022.01.02
66
- 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)

packages/core-js/internals/function-bind.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
var global = require('../internals/global');
33
var uncurryThis = require('../internals/function-uncurry-this');
4+
var fails = require('../internals/fails');
45
var aCallable = require('../internals/a-callable');
56
var isObject = require('../internals/is-object');
67
var hasOwn = require('../internals/has-own-property');
@@ -20,7 +21,11 @@ var construct = function (C, argsLength, args) {
2021

2122
// `Function.prototype.bind` method implementation
2223
// https://tc39.es/ecma262/#sec-function.prototype.bind
23-
module.exports = Function.bind || function bind(that /* , ...args */) {
24+
module.exports = fails(function () {
25+
// detect broken third-party polyfills
26+
var Test = function () { /* empty */ };
27+
return !(new (Test.bind())() instanceof Test) || (function (a, b) { return this + a + b; }).bind(1, 2)(3) !== 6;
28+
}) ? function bind(that /* , ...args */) {
2429
var F = aCallable(this);
2530
var Prototype = F.prototype;
2631
var partArgs = arraySlice(arguments, 1);
@@ -30,4 +35,4 @@ module.exports = Function.bind || function bind(that /* , ...args */) {
3035
};
3136
if (isObject(Prototype)) boundFunction.prototype = Prototype;
3237
return boundFunction;
33-
};
38+
} : Function.bind;

packages/core-js/modules/es.function.bind.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ var bind = require('../internals/function-bind');
33

44
// `Function.prototype.bind` method
55
// https://tc39.es/ecma262/#sec-function.prototype.bind
6-
$({ target: 'Function', proto: true }, {
6+
$({ target: 'Function', proto: true, forced: Function.bind !== bind }, {
77
bind: bind
88
});

tests/compat/tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,8 @@ GLOBAL.tests = {
528528
return escape;
529529
},
530530
'es.function.bind': function () {
531-
return Function.prototype.bind;
531+
var Test = function () { /* empty */ };
532+
return (new (Test.bind())() instanceof Test) && (function (a, b) { return this + a + b; }).bind(1, 2)(3) === 6;
532533
},
533534
'es.function.has-instance': [SYMBOLS_SUPPORT, function () {
534535
return Symbol.hasInstance in Function.prototype;

0 commit comments

Comments
 (0)