web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.7 KiB

  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 fails = require('../internals/fails');
  6. var wellKnownSymbol = require('../internals/well-known-symbol');
  7. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  8. var SPECIES = wellKnownSymbol('species');
  9. var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
  10. // #replace needs built-in support for named groups.
  11. // #match works fine because it just return the exec results, even if it has
  12. // a "grops" property.
  13. var re = /./;
  14. re.exec = function () {
  15. var result = [];
  16. result.groups = { a: '7' };
  17. return result;
  18. };
  19. return ''.replace(re, '$<a>') !== '7';
  20. });
  21. // IE <= 11 replaces $0 with the whole match, as if it was $&
  22. // https://stackoverflow.com/questions/6024666/getting-ie-to-replace-a-regex-with-the-literal-string-0
  23. var REPLACE_KEEPS_$0 = (function () {
  24. // eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing
  25. return 'a'.replace(/./, '$0') === '$0';
  26. })();
  27. var REPLACE = wellKnownSymbol('replace');
  28. // Safari <= 13.0.3(?) substitutes nth capture where n>m with an empty string
  29. var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = (function () {
  30. if (/./[REPLACE]) {
  31. return /./[REPLACE]('a', '$0') === '';
  32. }
  33. return false;
  34. })();
  35. // Chrome 51 has a buggy "split" implementation when RegExp#exec !== nativeExec
  36. // Weex JS has frozen built-in prototypes, so use try / catch wrapper
  37. var SPLIT_WORKS_WITH_OVERWRITTEN_EXEC = !fails(function () {
  38. // eslint-disable-next-line regexp/no-empty-group -- required for testing
  39. var re = /(?:)/;
  40. var originalExec = re.exec;
  41. re.exec = function () { return originalExec.apply(this, arguments); };
  42. var result = 'ab'.split(re);
  43. return result.length !== 2 || result[0] !== 'a' || result[1] !== 'b';
  44. });
  45. module.exports = function (KEY, length, exec, sham) {
  46. var SYMBOL = wellKnownSymbol(KEY);
  47. var DELEGATES_TO_SYMBOL = !fails(function () {
  48. // String methods call symbol-named RegEp methods
  49. var O = {};
  50. O[SYMBOL] = function () { return 7; };
  51. return ''[KEY](O) != 7;
  52. });
  53. var DELEGATES_TO_EXEC = DELEGATES_TO_SYMBOL && !fails(function () {
  54. // Symbol-named RegExp methods call .exec
  55. var execCalled = false;
  56. var re = /a/;
  57. if (KEY === 'split') {
  58. // We can't use real regex here since it causes deoptimization
  59. // and serious performance degradation in V8
  60. // https://github.com/zloirock/core-js/issues/306
  61. re = {};
  62. // RegExp[@@split] doesn't call the regex's exec method, but first creates
  63. // a new one. We need to return the patched regex when creating the new one.
  64. re.constructor = {};
  65. re.constructor[SPECIES] = function () { return re; };
  66. re.flags = '';
  67. re[SYMBOL] = /./[SYMBOL];
  68. }
  69. re.exec = function () { execCalled = true; return null; };
  70. re[SYMBOL]('');
  71. return !execCalled;
  72. });
  73. if (
  74. !DELEGATES_TO_SYMBOL ||
  75. !DELEGATES_TO_EXEC ||
  76. (KEY === 'replace' && !(
  77. REPLACE_SUPPORTS_NAMED_GROUPS &&
  78. REPLACE_KEEPS_$0 &&
  79. !REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
  80. )) ||
  81. (KEY === 'split' && !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC)
  82. ) {
  83. var nativeRegExpMethod = /./[SYMBOL];
  84. var methods = exec(SYMBOL, ''[KEY], function (nativeMethod, regexp, str, arg2, forceStringMethod) {
  85. if (regexp.exec === RegExp.prototype.exec) {
  86. if (DELEGATES_TO_SYMBOL && !forceStringMethod) {
  87. // The native String method already delegates to @@method (this
  88. // polyfilled function), leasing to infinite recursion.
  89. // We avoid it by directly calling the native @@method method.
  90. return { done: true, value: nativeRegExpMethod.call(regexp, str, arg2) };
  91. }
  92. return { done: true, value: nativeMethod.call(str, regexp, arg2) };
  93. }
  94. return { done: false };
  95. }, {
  96. REPLACE_KEEPS_$0: REPLACE_KEEPS_$0,
  97. REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE: REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
  98. });
  99. var stringMethod = methods[0];
  100. var regexMethod = methods[1];
  101. redefine(String.prototype, KEY, stringMethod);
  102. redefine(RegExp.prototype, SYMBOL, length == 2
  103. // 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue)
  104. // 21.2.5.11 RegExp.prototype[@@split](string, limit)
  105. ? function (string, arg) { return regexMethod.call(string, this, arg); }
  106. // 21.2.5.6 RegExp.prototype[@@match](string)
  107. // 21.2.5.9 RegExp.prototype[@@search](string)
  108. : function (string) { return regexMethod.call(string, this); }
  109. );
  110. }
  111. if (sham) createNonEnumerableProperty(RegExp.prototype[SYMBOL], 'sham', true);
  112. };