Skip to content

Commit

Permalink
prevent some more problems related to patching / deleting built-ins
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 18, 2021
1 parent 277cb33 commit f2fa2d4
Show file tree
Hide file tree
Showing 46 changed files with 339 additions and 258 deletions.
6 changes: 3 additions & 3 deletions packages/core-js/features/map/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +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 call = require('../../internals/function-call');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

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

module.exports = function from(source, mapFn, thisArg) {
return mapFrom(isCallable(this) ? this : Map, source, mapFn, thisArg);
return call($from, isCallable(this) ? this : Map, source, mapFn, thisArg);
};
6 changes: 3 additions & 3 deletions packages/core-js/features/set/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +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 call = require('../../internals/function-call');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

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

module.exports = function from(source, mapFn, thisArg) {
return setFrom(isCallable(this) ? this : Set, source, mapFn, thisArg);
return call($from, isCallable(this) ? this : Set, source, mapFn, thisArg);
};
6 changes: 3 additions & 3 deletions packages/core-js/features/weak-map/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +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 call = require('../../internals/function-call');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

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

module.exports = function from(source, mapFn, thisArg) {
return weakMapFrom(isCallable(this) ? this : WeakMap, source, mapFn, thisArg);
return call($from, isCallable(this) ? this : WeakMap, source, mapFn, thisArg);
};
6 changes: 3 additions & 3 deletions packages/core-js/features/weak-set/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +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 call = require('../../internals/function-call');
var isCallable = require('../../internals/is-callable');
var path = require('../../internals/path');

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

module.exports = function from(source, mapFn, thisArg) {
return weakSetfrom(isCallable(this) ? this : WeakSet, source, mapFn, thisArg);
return call($from, isCallable(this) ? this : WeakSet, source, mapFn, thisArg);
};
2 changes: 1 addition & 1 deletion packages/core-js/internals/collection-add-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');

// https://github.com/tc39/collection-methods
module.exports = function (/* ...elements */) {
module.exports = function addAll(/* ...elements */) {
var set = anObject(this);
var adder = aCallable(set.add);
for (var k = 0, len = arguments.length; k < len; k++) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/internals/collection-delete-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');

// https://github.com/tc39/collection-methods
module.exports = function (/* ...elements */) {
module.exports = function deleteAll(/* ...elements */) {
var collection = anObject(this);
var remover = aCallable(collection['delete']);
var allDeleted = true;
Expand Down
16 changes: 11 additions & 5 deletions packages/core-js/modules/es.escape.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var toString = require('../internals/to-string');

var raw = /[\w*+\-./@]/;
var charAt = uncurryThis(''.charAt);
var charCodeAt = uncurryThis(''.charCodeAt);
var exec = uncurryThis(raw.exec);
var numberToString = uncurryThis(1.0.toString);
var toUpperCase = uncurryThis(''.toUpperCase);

var hex = function (code, length) {
var result = code.toString(16);
var result = numberToString(code, 16);
while (result.length < length) result = '0' + result;
return result;
};
Expand All @@ -20,15 +26,15 @@ $({ global: true }, {
var index = 0;
var chr, code;
while (index < length) {
chr = str.charAt(index++);
if (raw.test(chr)) {
chr = charAt(str, index++);
if (exec(raw, chr)) {
result += chr;
} else {
code = chr.charCodeAt(0);
code = charCodeAt(chr, 0);
if (code < 256) {
result += '%' + hex(code, 2);
} else {
result += '%u' + hex(code, 4).toUpperCase();
result += '%u' + toUpperCase(hex(code, 4));
}
}
} return result;
Expand Down
5 changes: 3 additions & 2 deletions packages/core-js/modules/es.function.has-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ var FunctionPrototype = Function.prototype;
if (!(HAS_INSTANCE in FunctionPrototype)) {
definePropertyModule.f(FunctionPrototype, HAS_INSTANCE, { value: function (O) {
if (!isCallable(this) || !isObject(O)) return false;
if (!isObject(this.prototype)) return O instanceof this;
var P = this.prototype;
if (!isObject(P)) return O instanceof this;
// for environment w/o native `@@hasInstance` logic enough `instanceof`, but add this:
while (O = getPrototypeOf(O)) if (this.prototype === O) return true;
while (O = getPrototypeOf(O)) if (P === O) return true;
return false;
} });
}
4 changes: 3 additions & 1 deletion packages/core-js/modules/es.json.stringify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var apply = require('../internals/function-apply');
var uncurryThis = require('../internals/function-uncurry-this');
var fails = require('../internals/fails');

Expand Down Expand Up @@ -33,7 +34,8 @@ if ($stringify) {
$({ target: 'JSON', stat: true, forced: FORCED }, {
// eslint-disable-next-line no-unused-vars -- required for `.length`
stringify: function stringify(it, replacer, space) {
var result = $stringify.apply(null, arguments);
for (var i = 0, l = arguments.length, args = Array(l); i < l; i++) args[i] = arguments[i];
var result = apply($stringify, null, args);
return typeof result == 'string' ? replace(result, tester, fix) : result;
}
});
Expand Down
13 changes: 8 additions & 5 deletions packages/core-js/modules/es.number.constructor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var DESCRIPTORS = require('../internals/descriptors');
var global = require('../internals/global');
var uncurryThis = require('../internals/function-uncurry-this');
var isForced = require('../internals/is-forced');
var redefine = require('../internals/redefine');
var hasOwn = require('../internals/has-own-property');
Expand All @@ -17,6 +18,8 @@ var trim = require('../internals/string-trim').trim;
var NUMBER = 'Number';
var NativeNumber = global[NUMBER];
var NumberPrototype = NativeNumber.prototype;
var arraySlice = uncurryThis(''.slice);
var charCodeAt = uncurryThis(''.charCodeAt);

// `ToNumeric` abstract operation
// https://tc39.es/ecma262/#sec-tonumeric
Expand All @@ -33,20 +36,20 @@ var toNumber = function (argument) {
if (isSymbol(it)) throw TypeError('Cannot convert a Symbol value to a number');
if (typeof it == 'string' && it.length > 2) {
it = trim(it);
first = it.charCodeAt(0);
first = charCodeAt(it, 0);
if (first === 43 || first === 45) {
third = it.charCodeAt(2);
third = charCodeAt(it, 2);
if (third === 88 || third === 120) return NaN; // Number('+0x1') should be NaN, old V8 fix
} else if (first === 48) {
switch (it.charCodeAt(1)) {
switch (charCodeAt(it, 1)) {
case 66: case 98: radix = 2; maxCode = 49; break; // fast equal of /^0b[01]+$/i
case 79: case 111: radix = 8; maxCode = 55; break; // fast equal of /^0o[0-7]+$/i
default: return +it;
}
digits = it.slice(2);
digits = arraySlice(it, 2);
length = digits.length;
for (index = 0; index < length; index++) {
code = digits.charCodeAt(index);
code = charCodeAt(digits, index);
// parseInt parses a string to a first unavailable symbol
// but ToNumber should return NaN if a string contains unavailable symbols
if (code < 48 || code > maxCode) return NaN;
Expand Down
7 changes: 4 additions & 3 deletions packages/core-js/modules/es.number.to-fixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ var thisNumberValue = require('../internals/this-number-value');
var $repeat = require('../internals/string-repeat');
var fails = require('../internals/fails');

var un$ToFixed = uncurryThis(1.0.toFixed);
var repeat = uncurryThis($repeat);
var floor = Math.floor;
var repeat = uncurryThis($repeat);
var stringSlice = uncurryThis(''.slice);
var un$ToFixed = uncurryThis(1.0.toFixed);

var pow = function (x, n, acc) {
return n === 0 ? acc : n % 2 === 1 ? pow(x, n - 1, acc * x) : pow(x * x, n / 2, acc);
Expand Down Expand Up @@ -119,7 +120,7 @@ $({ target: 'Number', proto: true, forced: FORCED }, {
k = result.length;
result = sign + (k <= fractDigits
? '0.' + repeat('0', fractDigits - k) + result
: result.slice(0, k - fractDigits) + '.' + result.slice(k - fractDigits));
: stringSlice(result, 0, k - fractDigits) + '.' + stringSlice(result, k - fractDigits));
} else {
result = sign + result;
} return result;
Expand Down
1 change: 0 additions & 1 deletion packages/core-js/modules/es.promise.all-settled.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ $({ target: 'Promise', stat: true }, {
iterate(iterable, function (promise) {
var index = counter++;
var alreadyCalled = false;
values.push(undefined);
remaining++;
call(promiseResolve, C, promise).then(function (value) {
if (alreadyCalled) return;
Expand Down
6 changes: 3 additions & 3 deletions packages/core-js/modules/es.promise.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var PROMISE_ANY_ERROR = 'No one promise resolved';
$({ target: 'Promise', stat: true }, {
any: function any(iterable) {
var C = this;
var AggregateError = getBuiltIn('AggregateError');
var capability = newPromiseCapabilityModule.f(C);
var resolve = capability.resolve;
var reject = capability.reject;
Expand All @@ -26,7 +27,6 @@ $({ target: 'Promise', stat: true }, {
iterate(iterable, function (promise) {
var index = counter++;
var alreadyRejected = false;
errors.push(undefined);
remaining++;
call(promiseResolve, C, promise).then(function (value) {
if (alreadyRejected || alreadyResolved) return;
Expand All @@ -36,10 +36,10 @@ $({ target: 'Promise', stat: true }, {
if (alreadyRejected || alreadyResolved) return;
alreadyRejected = true;
errors[index] = error;
--remaining || reject(new (getBuiltIn('AggregateError'))(errors, PROMISE_ANY_ERROR));
--remaining || reject(new AggregateError(errors, PROMISE_ANY_ERROR));
});
});
--remaining || reject(new (getBuiltIn('AggregateError'))(errors, PROMISE_ANY_ERROR));
--remaining || reject(new AggregateError(errors, PROMISE_ANY_ERROR));
});
if (result.error) reject(result.value);
return capability.promise;
Expand Down
7 changes: 5 additions & 2 deletions packages/core-js/modules/es.promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var V8_VERSION = require('../internals/engine-v8-version');

var SPECIES = wellKnownSymbol('species');
var PROMISE = 'Promise';

var getInternalState = InternalStateModule.get;
var setInternalState = InternalStateModule.set;
var getInternalPromiseState = InternalStateModule.getterFor(PROMISE);
Expand All @@ -44,6 +45,7 @@ var document = global.document;
var process = global.process;
var newPromiseCapability = newPromiseCapabilityModule.f;
var newGenericPromiseCapability = newPromiseCapability;

var DISPATCH_EVENT = !!(document && document.createEvent && global.dispatchEvent);
var NATIVE_REJECTION_EVENT = isCallable(global.PromiseRejectionEvent);
var UNHANDLED_REJECTION = 'unhandledrejection';
Expand All @@ -54,6 +56,7 @@ var REJECTED = 2;
var HANDLED = 1;
var UNHANDLED = 2;
var SUBCLASSING = false;

var Internal, OwnPromiseCapability, PromiseWrapper, nativeThen;

var FORCED = isForced(PROMISE, function () {
Expand Down Expand Up @@ -262,12 +265,13 @@ if (FORCED) {
// https://tc39.es/ecma262/#sec-promise.prototype.then
then: function then(onFulfilled, onRejected) {
var state = getInternalPromiseState(this);
var reactions = state.reactions;
var reaction = newPromiseCapability(speciesConstructor(this, PromiseConstructor));
reaction.ok = isCallable(onFulfilled) ? onFulfilled : true;
reaction.fail = isCallable(onRejected) && onRejected;
reaction.domain = IS_NODE ? process.domain : undefined;
state.parent = true;
state.reactions.push(reaction);
reactions[reactions.length] = reaction;
if (state.state != PENDING) notify(state, false);
return reaction.promise;
},
Expand Down Expand Up @@ -363,7 +367,6 @@ $({ target: PROMISE, stat: true, forced: INCORRECT_ITERATION }, {
iterate(iterable, function (promise) {
var index = counter++;
var alreadyCalled = false;
values.push(undefined);
remaining++;
call($promiseResolve, C, promise).then(function (value) {
if (alreadyCalled) return;
Expand Down
25 changes: 15 additions & 10 deletions packages/core-js/modules/es.regexp.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ var MATCH = wellKnownSymbol('match');
var NativeRegExp = global.RegExp;
var RegExpPrototype = NativeRegExp.prototype;
var getFlags = uncurryThis(regExpFlags);
var exec = uncurryThis(RegExpPrototype.exec);
var charAt = uncurryThis(''.charAt);
var replace = uncurryThis(''.replace);
var stringIndexOf = uncurryThis(''.indexOf);
var stringSlice = uncurryThis(''.slice);
// TODO: Use only propper RegExpIdentifierName
var IS_NCG = /^\?<[^\s\d!#%&*+<=>@^][^\s!#%&*+<=>@^]*>/;
var re1 = /a/g;
Expand All @@ -47,9 +52,9 @@ var handleDotAll = function (string) {
var brackets = false;
var chr;
for (; index <= length; index++) {
chr = string.charAt(index);
chr = charAt(string, index);
if (chr === '\\') {
result += chr + string.charAt(++index);
result += chr + charAt(string, ++index);
continue;
}
if (!brackets && chr === '.') {
Expand All @@ -76,17 +81,17 @@ var handleNCG = function (string) {
var groupname = '';
var chr;
for (; index <= length; index++) {
chr = string.charAt(index);
chr = charAt(string, index);
if (chr === '\\') {
chr = chr + string.charAt(++index);
chr = chr + charAt(string, ++index);
} else if (chr === ']') {
brackets = false;
} else if (!brackets) switch (true) {
case chr === '[':
brackets = true;
break;
case chr === '(':
if (IS_NCG.test(string.slice(index + 1))) {
if (exec(IS_NCG, stringSlice(string, index + 1))) {
index += 2;
ncg = true;
}
Expand All @@ -98,7 +103,7 @@ var handleNCG = function (string) {
throw new SyntaxError('Invalid capture group name');
}
names[groupname] = true;
named.push([groupname, groupid]);
named[named.length] = [groupname, groupid];
ncg = false;
groupname = '';
continue;
Expand Down Expand Up @@ -133,15 +138,15 @@ if (isForced('RegExp', BASE_FORCED)) {
rawPattern = pattern;

if (UNSUPPORTED_DOT_ALL && 'dotAll' in re1) {
dotAll = !!flags && flags.indexOf('s') > -1;
if (dotAll) flags = flags.replace(/s/g, '');
dotAll = !!flags && stringIndexOf(flags, 's') > -1;
if (dotAll) flags = replace(flags, /s/g, '');
}

rawFlags = flags;

if (UNSUPPORTED_Y && 'sticky' in re1) {
sticky = !!flags && flags.indexOf('y') > -1;
if (sticky) flags = flags.replace(/y/g, '');
sticky = !!flags && stringIndexOf(flags, 'y') > -1;
if (sticky) flags = replace(flags, /y/g, '');
}

if (UNSUPPORTED_NCG) {
Expand Down
5 changes: 4 additions & 1 deletion packages/core-js/modules/es.string.at-alternative.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var requireObjectCoercible = require('../internals/require-object-coercible');
var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
var toString = require('../internals/to-string');
var fails = require('../internals/fails');

var charAt = uncurryThis(''.charAt);

var FORCED = fails(function () {
return '𠮷'.at(0) !== '\uD842';
});
Expand All @@ -17,6 +20,6 @@ $({ target: 'String', proto: true, forced: FORCED }, {
var len = S.length;
var relativeIndex = toIntegerOrInfinity(index);
var k = relativeIndex >= 0 ? relativeIndex : len + relativeIndex;
return (k < 0 || k >= len) ? undefined : S.charAt(k);
return (k < 0 || k >= len) ? undefined : charAt(S, k);
}
});
Loading

0 comments on commit f2fa2d4

Please sign in to comment.