Skip to content

Commit

Permalink
avoid internal usage of third-party .bind polyfills, close #1034
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jan 6, 2022
1 parent b4534bd commit 1a3c1ca
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
5 changes: 3 additions & 2 deletions packages/core-js/internals/function-apply.js
@@ -1,9 +1,10 @@
var NATIVE_BIND = require('../internals/function-bind-native');

var FunctionPrototype = Function.prototype;
var apply = FunctionPrototype.apply;
var bind = FunctionPrototype.bind;
var call = FunctionPrototype.call;

// eslint-disable-next-line es/no-reflect -- safe
module.exports = typeof Reflect == 'object' && Reflect.apply || (bind ? call.bind(apply) : function () {
module.exports = typeof Reflect == 'object' && Reflect.apply || (NATIVE_BIND ? call.bind(apply) : function () {
return call.apply(apply, arguments);
});
3 changes: 2 additions & 1 deletion packages/core-js/internals/function-bind-context.js
@@ -1,12 +1,13 @@
var uncurryThis = require('../internals/function-uncurry-this');
var aCallable = require('../internals/a-callable');
var NATIVE_BIND = require('../internals/function-bind-native');

var bind = uncurryThis(uncurryThis.bind);

// optional / simple context binding
module.exports = function (fn, that) {
aCallable(fn);
return that === undefined ? fn : bind ? bind(fn, that) : function (/* ...args */) {
return that === undefined ? fn : NATIVE_BIND ? bind(fn, that) : function (/* ...args */) {
return fn.apply(that, arguments);
};
};
7 changes: 7 additions & 0 deletions packages/core-js/internals/function-bind-native.js
@@ -0,0 +1,7 @@
var fails = require('../internals/fails');

module.exports = !fails(function () {
var test = (function () { /* empty */ }).bind();
// eslint-disable-next-line no-prototype-builtins -- safe
return typeof test != 'function' || test.hasOwnProperty('prototype');
});
10 changes: 3 additions & 7 deletions packages/core-js/internals/function-bind.js
@@ -1,11 +1,11 @@
'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');
var arraySlice = require('../internals/array-slice');
var NATIVE_BIND = require('../internals/function-bind-native');

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

// `Function.prototype.bind` method implementation
// https://tc39.es/ecma262/#sec-function.prototype.bind
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 */) {
module.exports = NATIVE_BIND ? Function.bind : function bind(that /* , ...args */) {
var F = aCallable(this);
var Prototype = F.prototype;
var partArgs = arraySlice(arguments, 1);
Expand All @@ -35,4 +31,4 @@ module.exports = fails(function () {
};
if (isObject(Prototype)) boundFunction.prototype = Prototype;
return boundFunction;
} : Function.bind;
};
4 changes: 3 additions & 1 deletion packages/core-js/internals/function-call.js
@@ -1,5 +1,7 @@
var NATIVE_BIND = require('../internals/function-bind-native');

var call = Function.prototype.call;

module.exports = call.bind ? call.bind(call) : function () {
module.exports = NATIVE_BIND ? call.bind(call) : function () {
return call.apply(call, arguments);
};
6 changes: 4 additions & 2 deletions packages/core-js/internals/function-uncurry-this.js
@@ -1,9 +1,11 @@
var NATIVE_BIND = require('../internals/function-bind-native');

var FunctionPrototype = Function.prototype;
var bind = FunctionPrototype.bind;
var call = FunctionPrototype.call;
var uncurryThis = bind && bind.bind(call, call);
var uncurryThis = NATIVE_BIND && bind.bind(call, call);

module.exports = bind ? function (fn) {
module.exports = NATIVE_BIND ? function (fn) {
return fn && uncurryThis(fn);
} : function (fn) {
return fn && function () {
Expand Down
5 changes: 3 additions & 2 deletions tests/compat/tests.js
Expand Up @@ -528,8 +528,9 @@ GLOBAL.tests = {
return escape;
},
'es.function.bind': function () {
var Test = function () { /* empty */ };
return (new (Test.bind())() instanceof Test) && (function (a, b) { return this + a + b; }).bind(1, 2)(3) === 6;
var test = (function () { /* empty */ }).bind();
// eslint-disable-next-line no-prototype-builtins -- safe
return typeof test == 'function' && !test.hasOwnProperty('prototype');
},
'es.function.has-instance': [SYMBOLS_SUPPORT, function () {
return Symbol.hasInstance in Function.prototype;
Expand Down

0 comments on commit 1a3c1ca

Please sign in to comment.