[6a3a178] | 1 | 'use strict';
|
---|
| 2 | // TODO: Remove from `core-js@4` since it's moved to entry points
|
---|
| 3 | require('../modules/es.regexp.exec');
|
---|
| 4 | var redefine = require('../internals/redefine');
|
---|
| 5 | var regexpExec = require('../internals/regexp-exec');
|
---|
| 6 | var fails = require('../internals/fails');
|
---|
| 7 | var wellKnownSymbol = require('../internals/well-known-symbol');
|
---|
| 8 | var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
|
---|
| 9 |
|
---|
| 10 | var SPECIES = wellKnownSymbol('species');
|
---|
| 11 | var RegExpPrototype = RegExp.prototype;
|
---|
| 12 |
|
---|
| 13 | module.exports = function (KEY, exec, FORCED, SHAM) {
|
---|
| 14 | var SYMBOL = wellKnownSymbol(KEY);
|
---|
| 15 |
|
---|
| 16 | var DELEGATES_TO_SYMBOL = !fails(function () {
|
---|
| 17 | // String methods call symbol-named RegEp methods
|
---|
| 18 | var O = {};
|
---|
| 19 | O[SYMBOL] = function () { return 7; };
|
---|
| 20 | return ''[KEY](O) != 7;
|
---|
| 21 | });
|
---|
| 22 |
|
---|
| 23 | var DELEGATES_TO_EXEC = DELEGATES_TO_SYMBOL && !fails(function () {
|
---|
| 24 | // Symbol-named RegExp methods call .exec
|
---|
| 25 | var execCalled = false;
|
---|
| 26 | var re = /a/;
|
---|
| 27 |
|
---|
| 28 | if (KEY === 'split') {
|
---|
| 29 | // We can't use real regex here since it causes deoptimization
|
---|
| 30 | // and serious performance degradation in V8
|
---|
| 31 | // https://github.com/zloirock/core-js/issues/306
|
---|
| 32 | re = {};
|
---|
| 33 | // RegExp[@@split] doesn't call the regex's exec method, but first creates
|
---|
| 34 | // a new one. We need to return the patched regex when creating the new one.
|
---|
| 35 | re.constructor = {};
|
---|
| 36 | re.constructor[SPECIES] = function () { return re; };
|
---|
| 37 | re.flags = '';
|
---|
| 38 | re[SYMBOL] = /./[SYMBOL];
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | re.exec = function () { execCalled = true; return null; };
|
---|
| 42 |
|
---|
| 43 | re[SYMBOL]('');
|
---|
| 44 | return !execCalled;
|
---|
| 45 | });
|
---|
| 46 |
|
---|
| 47 | if (
|
---|
| 48 | !DELEGATES_TO_SYMBOL ||
|
---|
| 49 | !DELEGATES_TO_EXEC ||
|
---|
| 50 | FORCED
|
---|
| 51 | ) {
|
---|
| 52 | var nativeRegExpMethod = /./[SYMBOL];
|
---|
| 53 | var methods = exec(SYMBOL, ''[KEY], function (nativeMethod, regexp, str, arg2, forceStringMethod) {
|
---|
| 54 | var $exec = regexp.exec;
|
---|
| 55 | if ($exec === regexpExec || $exec === RegExpPrototype.exec) {
|
---|
| 56 | if (DELEGATES_TO_SYMBOL && !forceStringMethod) {
|
---|
| 57 | // The native String method already delegates to @@method (this
|
---|
| 58 | // polyfilled function), leasing to infinite recursion.
|
---|
| 59 | // We avoid it by directly calling the native @@method method.
|
---|
| 60 | return { done: true, value: nativeRegExpMethod.call(regexp, str, arg2) };
|
---|
| 61 | }
|
---|
| 62 | return { done: true, value: nativeMethod.call(str, regexp, arg2) };
|
---|
| 63 | }
|
---|
| 64 | return { done: false };
|
---|
| 65 | });
|
---|
| 66 |
|
---|
| 67 | redefine(String.prototype, KEY, methods[0]);
|
---|
| 68 | redefine(RegExpPrototype, SYMBOL, methods[1]);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | if (SHAM) createNonEnumerableProperty(RegExpPrototype[SYMBOL], 'sham', true);
|
---|
| 72 | };
|
---|