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