Skip to content

Commit

Permalink
add uncurryThis internal helper to avoid some .call that could be…
Browse files Browse the repository at this point in the history
… used for breaking / observing the internal state
  • Loading branch information
zloirock committed Oct 17, 2021
1 parent b2144bb commit fcbc2c8
Show file tree
Hide file tree
Showing 98 changed files with 358 additions and 274 deletions.
3 changes: 2 additions & 1 deletion packages/core-js-pure/override/internals/export.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var global = require('../internals/global');
var uncurryThis = require('../internals/function-uncurry-this');
var isCallable = require('../internals/is-callable');
var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
var isForced = require('../internals/is-forced');
Expand Down Expand Up @@ -73,7 +74,7 @@ module.exports = function (options, source) {
// wrap global constructors for prevent changs in this version
else if (options.wrap && USE_NATIVE) resultProperty = wrapConstructor(sourceProperty);
// make static versions for prototype methods
else if (PROTO && isCallable(sourceProperty)) resultProperty = bind(Function.call, sourceProperty);
else if (PROTO && isCallable(sourceProperty)) resultProperty = uncurryThis(sourceProperty);
// default case
else resultProperty = sourceProperty;

Expand Down
5 changes: 2 additions & 3 deletions packages/core-js/es/date/to-primitive.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require('../../modules/es.date.to-primitive');
var uncurryThis = require('../../internals/function-uncurry-this');
var toPrimitive = require('../../internals/date-to-primitive');

module.exports = function (it, hint) {
return toPrimitive.call(it, hint);
};
module.exports = uncurryThis(toPrimitive);
5 changes: 2 additions & 3 deletions packages/core-js/es/date/to-string.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require('../../modules/es.date.to-string');
var uncurryThis = require('../../internals/function-uncurry-this');
var dateToString = Date.prototype.toString;

module.exports = function toString(it) {
return dateToString.call(it);
};
module.exports = uncurryThis(dateToString);
5 changes: 3 additions & 2 deletions packages/core-js/es/promise/all-settled.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/es.promise.all-settled');
require('../../modules/es.string.iterator');
var uncurryThis = require('../../internals/function-uncurry-this');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

var Promise = path.Promise;
var $allSettled = Promise.allSettled;
var $allSettled = uncurryThis(Promise.allSettled);

module.exports = function allSettled(iterable) {
return $allSettled.call(isCallable(this) ? this : Promise, iterable);
return $allSettled(isCallable(this) ? this : Promise, iterable);
};
5 changes: 3 additions & 2 deletions packages/core-js/es/promise/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/es.promise.any');
require('../../modules/es.string.iterator');
var uncurryThis = require('../../internals/function-uncurry-this');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

var Promise = path.Promise;
var $any = Promise.any;
var $any = uncurryThis(Promise.any);

module.exports = function any(iterable) {
return $any.call(isCallable(this) ? this : Promise, iterable);
return $any(isCallable(this) ? this : Promise, iterable);
};
7 changes: 3 additions & 4 deletions packages/core-js/es/regexp/flags.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require('../../modules/es.regexp.flags');
var flags = require('../../internals/regexp-flags');
var uncurryThis = require('../../internals/function-uncurry-this');
var regExpFlags = require('../../internals/regexp-flags');

module.exports = function (it) {
return flags.call(it);
};
module.exports = uncurryThis(regExpFlags);
5 changes: 2 additions & 3 deletions packages/core-js/es/regexp/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require('../../modules/es.regexp.exec');
require('../../modules/es.regexp.test');
var uncurryThis = require('../../internals/function-uncurry-this');

module.exports = function (re, string) {
return RegExp.prototype.test.call(re, string);
};
module.exports = uncurryThis(/./.test);
5 changes: 2 additions & 3 deletions packages/core-js/es/regexp/to-string.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require('../../modules/es.regexp.to-string');
var uncurryThis = require('../../internals/function-uncurry-this');

module.exports = function toString(it) {
return RegExp.prototype.toString.call(it);
};
module.exports = uncurryThis(/./.toString);
5 changes: 3 additions & 2 deletions packages/core-js/features/map/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ require('../../modules/es.map');
require('../../modules/es.string.iterator');
require('../../modules/esnext.map.from');
require('../../modules/web.dom-collections.iterator');
var uncurryThis = require('../../internals/function-uncurry-this');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

var Map = path.Map;
var mapFrom = Map.from;
var mapFrom = uncurryThis(Map.from);

module.exports = function from(source, mapFn, thisArg) {
return mapFrom.call(isCallable(this) ? this : Map, source, mapFn, thisArg);
return mapFrom(isCallable(this) ? this : Map, source, mapFn, thisArg);
};
5 changes: 3 additions & 2 deletions packages/core-js/features/map/group-by.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
require('../../modules/es.map');
require('../../modules/esnext.map.group-by');
var uncurryThis = require('../../internals/function-uncurry-this');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

var Map = path.Map;
var mapGroupBy = Map.groupBy;
var mapGroupBy = uncurryThis(Map.groupBy);

module.exports = function groupBy(source, iterable, keyDerivative) {
return mapGroupBy.call(isCallable(this) ? this : Map, source, iterable, keyDerivative);
return mapGroupBy(isCallable(this) ? this : Map, source, iterable, keyDerivative);
};
5 changes: 3 additions & 2 deletions packages/core-js/features/map/key-by.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
require('../../modules/es.map');
require('../../modules/esnext.map.key-by');
var uncurryThis = require('../../internals/function-uncurry-this');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

var Map = path.Map;
var mapKeyBy = Map.keyBy;
var mapKeyBy = uncurryThis(Map.keyBy);

module.exports = function keyBy(source, iterable, keyDerivative) {
return mapKeyBy.call(isCallable(this) ? this : Map, source, iterable, keyDerivative);
return mapKeyBy(isCallable(this) ? this : Map, source, iterable, keyDerivative);
};
5 changes: 3 additions & 2 deletions packages/core-js/features/promise/try.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
require('../../modules/es.promise');
require('../../modules/esnext.promise.try');
var uncurryThis = require('../../internals/function-uncurry-this');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

var Promise = path.Promise;
var promiseTry = Promise['try'];
var promiseTry = uncurryThis(Promise['try']);

module.exports = { 'try': function (callbackfn) {
return promiseTry.call(isCallable(this) ? this : Promise, callbackfn);
return promiseTry(isCallable(this) ? this : Promise, callbackfn);
} }['try'];
5 changes: 3 additions & 2 deletions packages/core-js/features/set/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ require('../../modules/es.set');
require('../../modules/es.string.iterator');
require('../../modules/esnext.set.from');
require('../../modules/web.dom-collections.iterator');
var uncurryThis = require('../../internals/function-uncurry-this');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

var Set = path.Set;
var setFrom = Set.from;
var setFrom = uncurryThis(Set.from);

module.exports = function from(source, mapFn, thisArg) {
return setFrom.call(isCallable(this) ? this : Set, source, mapFn, thisArg);
return setFrom(isCallable(this) ? this : Set, source, mapFn, thisArg);
};
5 changes: 3 additions & 2 deletions packages/core-js/features/weak-map/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ require('../../modules/es.string.iterator');
require('../../modules/es.weak-map');
require('../../modules/esnext.weak-map.from');
require('../../modules/web.dom-collections.iterator');
var uncurryThis = require('../../internals/function-uncurry-this');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

var WeakMap = path.WeakMap;
var weakMapFrom = WeakMap.from;
var weakMapFrom = uncurryThis(WeakMap.from);

module.exports = function from(source, mapFn, thisArg) {
return weakMapFrom.call(isCallable(this) ? this : WeakMap, source, mapFn, thisArg);
return weakMapFrom(isCallable(this) ? this : WeakMap, source, mapFn, thisArg);
};
5 changes: 3 additions & 2 deletions packages/core-js/features/weak-set/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ require('../../modules/es.string.iterator');
require('../../modules/es.weak-set');
require('../../modules/esnext.weak-set.from');
require('../../modules/web.dom-collections.iterator');
var uncurryThis = require('../../internals/function-uncurry-this');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

var WeakSet = path.WeakSet;
var weakSetfrom = WeakSet.from;
var weakSetfrom = uncurryThis(WeakSet.from);

module.exports = function from(source, mapFn, thisArg) {
return weakSetfrom.call(isCallable(this) ? this : WeakSet, source, mapFn, thisArg);
return weakSetfrom(isCallable(this) ? this : WeakSet, source, mapFn, thisArg);
};
5 changes: 3 additions & 2 deletions packages/core-js/internals/array-buffer-view-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var NATIVE_ARRAY_BUFFER = require('../internals/array-buffer-native');
var DESCRIPTORS = require('../internals/descriptors');
var global = require('../internals/global');
var uncurryThis = require('../internals/function-uncurry-this');
var isCallable = require('../internals/is-callable');
var isObject = require('../internals/is-object');
var hasOwn = require('../internals/has-own-property');
Expand All @@ -22,7 +23,7 @@ var Uint8ClampedArrayPrototype = Uint8ClampedArray && Uint8ClampedArray.prototyp
var TypedArray = Int8Array && getPrototypeOf(Int8Array);
var TypedArrayPrototype = Int8ArrayPrototype && getPrototypeOf(Int8ArrayPrototype);
var ObjectPrototype = Object.prototype;
var isPrototypeOf = ObjectPrototype.isPrototypeOf;
var isPrototypeOf = uncurryThis(ObjectPrototype.isPrototypeOf);

var TO_STRING_TAG = wellKnownSymbol('toStringTag');
var TYPED_ARRAY_TAG = uid('TYPED_ARRAY_TAG');
Expand Down Expand Up @@ -70,7 +71,7 @@ var aTypedArray = function (it) {
};

var aTypedArrayConstructor = function (C) {
if (isCallable(C) && (!setPrototypeOf || isPrototypeOf.call(TypedArray, C))) return C;
if (isCallable(C) && (!setPrototypeOf || isPrototypeOf(TypedArray, C))) return C;
throw TypeError(tryToString(C) + ' is not a typed array constructor');
};

Expand Down
12 changes: 7 additions & 5 deletions packages/core-js/internals/array-buffer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var global = require('../internals/global');
var uncurryThis = require('../internals/function-uncurry-this');
var DESCRIPTORS = require('../internals/descriptors');
var NATIVE_ARRAY_BUFFER = require('../internals/array-buffer-native');
var FunctionName = require('../internals/function-name');
Expand All @@ -15,7 +16,7 @@ var getPrototypeOf = require('../internals/object-get-prototype-of');
var setPrototypeOf = require('../internals/object-set-prototype-of');
var getOwnPropertyNames = require('../internals/object-get-own-property-names').f;
var defineProperty = require('../internals/object-define-property').f;
var arrayFill = require('../internals/array-fill');
var $arrayFill = require('../internals/array-fill');
var setToStringTag = require('../internals/set-to-string-tag');
var InternalStateModule = require('../internals/internal-state');

Expand All @@ -34,6 +35,7 @@ var $DataView = global[DATA_VIEW];
var $DataViewPrototype = $DataView && $DataView[PROTOTYPE];
var ObjectPrototype = Object.prototype;
var RangeError = global.RangeError;
var arrayFill = uncurryThis($arrayFill);

var packIEEE754 = IEEE754.pack;
var unpackIEEE754 = IEEE754.unpack;
Expand Down Expand Up @@ -91,7 +93,7 @@ if (!NATIVE_ARRAY_BUFFER) {
anInstance(this, $ArrayBuffer, ARRAY_BUFFER);
var byteLength = toIndex(length);
setInternalState(this, {
bytes: arrayFill.call(new Array(byteLength), 0),
bytes: arrayFill(new Array(byteLength), 0),
byteLength: byteLength
});
if (!DESCRIPTORS) this.byteLength = byteLength;
Expand Down Expand Up @@ -212,15 +214,15 @@ if (!NATIVE_ARRAY_BUFFER) {

// iOS Safari 7.x bug
var testView = new $DataView(new $ArrayBuffer(2));
var $setInt8 = $DataViewPrototype.setInt8;
var $setInt8 = uncurryThis($DataViewPrototype.setInt8);
testView.setInt8(0, 2147483648);
testView.setInt8(1, 2147483649);
if (testView.getInt8(0) || !testView.getInt8(1)) redefineAll($DataViewPrototype, {
setInt8: function setInt8(byteOffset, value) {
$setInt8.call(this, byteOffset, value << 24 >> 24);
$setInt8(this, byteOffset, value << 24 >> 24);
},
setUint8: function setUint8(byteOffset, value) {
$setInt8.call(this, byteOffset, value << 24 >> 24);
$setInt8(this, byteOffset, value << 24 >> 24);
}
}, { unsafe: true });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/internals/array-from-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function fromAsync(asyncItems /* , mapfn = undefined, thisArg =
var thisArg = argumentsLength > 2 ? arguments[2] : undefined;
return new (getBuiltIn('Promise'))(function (resolve) {
var O = toObject(asyncItems);
if (mapfn !== undefined) mapfn = bind(mapfn, thisArg, 2);
if (mapfn !== undefined) mapfn = bind(mapfn, thisArg);
var usingAsyncIterator = getMethod(O, ASYNC_ITERATOR);
var usingSyncIterator = usingAsyncIterator ? undefined : getIteratorMethod(O) || arrayIterator;
var A = isConstructor(C) ? new C() : [];
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/internals/array-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function from(arrayLike /* , mapfn = undefined, thisArg = undef
var argumentsLength = arguments.length;
var mapfn = argumentsLength > 1 ? arguments[1] : undefined;
var mapping = mapfn !== undefined;
if (mapping) mapfn = bind(mapfn, argumentsLength > 2 ? arguments[2] : undefined, 2);
if (mapping) mapfn = bind(mapfn, argumentsLength > 2 ? arguments[2] : undefined);
var iteratorMethod = getIteratorMethod(O);
var index = 0;
var length, result, step, iterator, next, value;
Expand Down
7 changes: 4 additions & 3 deletions packages/core-js/internals/array-group-by.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
var bind = require('../internals/function-bind-context');
var uncurryThis = require('../internals/function-uncurry-this');
var IndexedObject = require('../internals/indexed-object');
var toObject = require('../internals/to-object');
var toPropertyKey = require('../internals/to-property-key');
var lengthOfArrayLike = require('../internals/length-of-array-like');
var objectCreate = require('../internals/object-create');
var arrayFromConstructorAndList = require('../internals/array-from-constructor-and-list');

var push = [].push;
var push = uncurryThis([].push);

module.exports = function ($this, callbackfn, that, specificConstructor) {
var O = toObject($this);
var self = IndexedObject(O);
var boundFunction = bind(callbackfn, that, 3);
var boundFunction = bind(callbackfn, that);
var target = objectCreate(null);
var length = lengthOfArrayLike(self);
var index = 0;
Expand All @@ -21,7 +22,7 @@ module.exports = function ($this, callbackfn, that, specificConstructor) {
key = toPropertyKey(boundFunction(value, index, O));
// in some IE10 builds, `hasOwnProperty` returns incorrect result on integer keys
// but since it's a `null` prototype object, we can safely use `in`
if (key in target) push.call(target[key], value);
if (key in target) push(target[key], value);
else target[key] = [value];
}
if (specificConstructor) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/internals/array-iteration-from-last.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var createMethod = function (TYPE) {
return function ($this, callbackfn, that) {
var O = toObject($this);
var self = IndexedObject(O);
var boundFunction = bind(callbackfn, that, 3);
var boundFunction = bind(callbackfn, that);
var index = lengthOfArrayLike(self);
var value, result;
while (index-- > 0) {
Expand Down
9 changes: 5 additions & 4 deletions packages/core-js/internals/array-iteration.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var bind = require('../internals/function-bind-context');
var uncurryThis = require('../internals/function-uncurry-this');
var IndexedObject = require('../internals/indexed-object');
var toObject = require('../internals/to-object');
var lengthOfArrayLike = require('../internals/length-of-array-like');
var arraySpeciesCreate = require('../internals/array-species-create');

var push = [].push;
var push = uncurryThis([].push);

// `Array.prototype.{ forEach, map, filter, some, every, find, findIndex, filterReject }` methods implementation
var createMethod = function (TYPE) {
Expand All @@ -18,7 +19,7 @@ var createMethod = function (TYPE) {
return function ($this, callbackfn, that, specificCreate) {
var O = toObject($this);
var self = IndexedObject(O);
var boundFunction = bind(callbackfn, that, 3);
var boundFunction = bind(callbackfn, that);
var length = lengthOfArrayLike(self);
var index = 0;
var create = specificCreate || arraySpeciesCreate;
Expand All @@ -33,10 +34,10 @@ var createMethod = function (TYPE) {
case 3: return true; // some
case 5: return value; // find
case 6: return index; // findIndex
case 2: push.call(target, value); // filter
case 2: push(target, value); // filter
} else switch (TYPE) {
case 4: return false; // every
case 7: push.call(target, value); // filterReject
case 7: push(target, value); // filterReject
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core-js/internals/array-unique-by.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';
var uncurryThis = require('../internals/function-uncurry-this');
var aCallable = require('../internals/a-callable');
var lengthOfArrayLike = require('../internals/length-of-array-like');
var toObject = require('../internals/to-object');
var getBuiltIn = require('../internals/get-built-in');
var arraySpeciesCreate = require('../internals/array-species-create');

var push = [].push;
var push = uncurryThis([].push);

// `Array.prototype.uniqueBy` method
// https://github.com/tc39/proposal-array-unique
Expand All @@ -26,7 +27,7 @@ module.exports = function uniqueBy(resolver) {
if (!map.has(key)) map.set(key, item);
}
map.forEach(function (value) {
push.call(result, value);
push(result, value);
});
return result;
};
Loading

0 comments on commit fcbc2c8

Please sign in to comment.