Skip to content

Commit 1a3c1ca

Browse files
committed
avoid internal usage of third-party .bind polyfills, close #1034
1 parent b4534bd commit 1a3c1ca

File tree

7 files changed

+25
-15
lines changed

7 files changed

+25
-15
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
var NATIVE_BIND = require('../internals/function-bind-native');
2+
13
var FunctionPrototype = Function.prototype;
24
var apply = FunctionPrototype.apply;
3-
var bind = FunctionPrototype.bind;
45
var call = FunctionPrototype.call;
56

67
// eslint-disable-next-line es/no-reflect -- safe
7-
module.exports = typeof Reflect == 'object' && Reflect.apply || (bind ? call.bind(apply) : function () {
8+
module.exports = typeof Reflect == 'object' && Reflect.apply || (NATIVE_BIND ? call.bind(apply) : function () {
89
return call.apply(apply, arguments);
910
});
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
var uncurryThis = require('../internals/function-uncurry-this');
22
var aCallable = require('../internals/a-callable');
3+
var NATIVE_BIND = require('../internals/function-bind-native');
34

45
var bind = uncurryThis(uncurryThis.bind);
56

67
// optional / simple context binding
78
module.exports = function (fn, that) {
89
aCallable(fn);
9-
return that === undefined ? fn : bind ? bind(fn, that) : function (/* ...args */) {
10+
return that === undefined ? fn : NATIVE_BIND ? bind(fn, that) : function (/* ...args */) {
1011
return fn.apply(that, arguments);
1112
};
1213
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var fails = require('../internals/fails');
2+
3+
module.exports = !fails(function () {
4+
var test = (function () { /* empty */ }).bind();
5+
// eslint-disable-next-line no-prototype-builtins -- safe
6+
return typeof test != 'function' || test.hasOwnProperty('prototype');
7+
});

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22
var global = require('../internals/global');
33
var uncurryThis = require('../internals/function-uncurry-this');
4-
var fails = require('../internals/fails');
54
var aCallable = require('../internals/a-callable');
65
var isObject = require('../internals/is-object');
76
var hasOwn = require('../internals/has-own-property');
87
var arraySlice = require('../internals/array-slice');
8+
var NATIVE_BIND = require('../internals/function-bind-native');
99

1010
var Function = global.Function;
1111
var concat = uncurryThis([].concat);
@@ -21,11 +21,7 @@ var construct = function (C, argsLength, args) {
2121

2222
// `Function.prototype.bind` method implementation
2323
// https://tc39.es/ecma262/#sec-function.prototype.bind
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 */) {
24+
module.exports = NATIVE_BIND ? Function.bind : function bind(that /* , ...args */) {
2925
var F = aCallable(this);
3026
var Prototype = F.prototype;
3127
var partArgs = arraySlice(arguments, 1);
@@ -35,4 +31,4 @@ module.exports = fails(function () {
3531
};
3632
if (isObject(Prototype)) boundFunction.prototype = Prototype;
3733
return boundFunction;
38-
} : Function.bind;
34+
};
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
var NATIVE_BIND = require('../internals/function-bind-native');
2+
13
var call = Function.prototype.call;
24

3-
module.exports = call.bind ? call.bind(call) : function () {
5+
module.exports = NATIVE_BIND ? call.bind(call) : function () {
46
return call.apply(call, arguments);
57
};

packages/core-js/internals/function-uncurry-this.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
var NATIVE_BIND = require('../internals/function-bind-native');
2+
13
var FunctionPrototype = Function.prototype;
24
var bind = FunctionPrototype.bind;
35
var call = FunctionPrototype.call;
4-
var uncurryThis = bind && bind.bind(call, call);
6+
var uncurryThis = NATIVE_BIND && bind.bind(call, call);
57

6-
module.exports = bind ? function (fn) {
8+
module.exports = NATIVE_BIND ? function (fn) {
79
return fn && uncurryThis(fn);
810
} : function (fn) {
911
return fn && function () {

tests/compat/tests.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,9 @@ GLOBAL.tests = {
528528
return escape;
529529
},
530530
'es.function.bind': function () {
531-
var Test = function () { /* empty */ };
532-
return (new (Test.bind())() instanceof Test) && (function (a, b) { return this + a + b; }).bind(1, 2)(3) === 6;
531+
var test = (function () { /* empty */ }).bind();
532+
// eslint-disable-next-line no-prototype-builtins -- safe
533+
return typeof test == 'function' && !test.hasOwnProperty('prototype');
533534
},
534535
'es.function.has-instance': [SYMBOLS_SUPPORT, function () {
535536
return Symbol.hasInstance in Function.prototype;

0 commit comments

Comments
 (0)