UNPKG

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