Debian Codegraph @

js/src/builtin/RegExp.js

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 // https://github.com/tc39/ecma262/pull/2418 22.2.6.4 get RegExp.prototype.flags
6 // https://arai-a.github.io/ecma262-compare/?pr=2418&id=sec-get-regexp.prototype.flags
7 // Uncloned functions with `$` prefix are allocated as extended function
8 // to store the original name in `SetCanonicalName`.
9 function $RegExpFlagsGetter() {
10 // Steps 1-2.
11 var R = this;
12 if (!IsObject(R)) {
13 ThrowTypeError(JSMSG_OBJECT_REQUIRED, R === null ? "null" : typeof R);
14 }
15
16 // Step 3.
17 var result = "";
18
19 // Steps 4-5.
20 if (R.hasIndices) {
21 result += "d";
22 }
23
24 // Steps 6-7.
25 if (R.global) {
26 result += "g";
27 }
28
29 // Steps 8-9.
30 if (R.ignoreCase) {
31 result += "i";
32 }
33
34 // Steps 10-11.
35 if (R.multiline) {
36 result += "m";
37 }
38
39 // Steps 12-13.
40 if (R.dotAll) {
41 result += "s";
42 }
43
44 // Steps 14-15.
45 if (R.unicode) {
46 result += "u";
47 }
48
49 // Steps 16-17.
50 if (R.unicodeSets) {
51 result += "v";
52 }
53
54 // Steps 18-19
55 if (R.sticky) {
56 result += "y";
57 }
58
59 // Step 20.
60 return result;
61 }
62 SetCanonicalName($RegExpFlagsGetter, "get flags");
63
64 // ES 2017 draft 40edb3a95a475c1b251141ac681b8793129d9a6d 21.2.5.14.
65 function $RegExpToString() {
66 // Step 1.
67 var R = this;
68
69 // Step 2.
70 if (!IsObject(R)) {
71 ThrowTypeError(JSMSG_OBJECT_REQUIRED, R === null ? "null" : typeof R);
72 }
73
74 // Step 3.
75 var pattern = ToString(R.source);
76
77 // Step 4.
78 var flags = ToString(R.flags);
79
80 // Steps 5-6.
81 return "/" + pattern + "/" + flags;
82 }
83 SetCanonicalName($RegExpToString, "toString");
84
85 // ES 2016 draft Mar 25, 2016 21.2.5.2.3.
86 function AdvanceStringIndex(S, index) {
87 // Step 1.
88 assert(typeof S === "string", "Expected string as 1st argument");
89
90 // Step 2.
91 assert(
92 index >= 0 && index <= MAX_NUMERIC_INDEX,
93 "Expected integer as 2nd argument"
94 );
95
96 // Step 3 (skipped).
97
98 // Step 4 (skipped).
99
100 // Steps 5-11.
101 var supplementary = (
102 index < S.length &&
103 callFunction(std_String_codePointAt, S, index) > 0xffff
104 );
105 return index + 1 + supplementary;
106 }
107
108 // ES2023 draft rev 2c78e6f6b5bc6bfbf79dd8a12a9593e5b57afcd2
109 // 22.2.5.8 RegExp.prototype [ @@match ] ( string )
110 function RegExpMatch(string) {
111 // Step 1.
112 var rx = this;
113
114 // Step 2.
115 if (!IsObject(rx)) {
116 ThrowTypeError(JSMSG_OBJECT_REQUIRED, rx === null ? "null" : typeof rx);
117 }
118
119 // Step 3.
120 var S = ToString(string);
121
122 // Optimized paths for simple cases.
123 if (IsOptimizableRegExpObject(rx)) {
124 // Step 4.
125 var flags = UnsafeGetInt32FromReservedSlot(rx, REGEXP_FLAGS_SLOT);
126 var global = !!(flags & REGEXP_GLOBAL_FLAG);
127
128 if (global) {
129 // Step 6.a.
130 var fullUnicode = !!(flags & REGEXP_UNICODE_FLAG) || !!(flags & REGEXP_UNICODESETS_FLAG);
131
132 // Steps 6.b-e.
133 return RegExpGlobalMatchOpt(rx, S, fullUnicode);
134 }
135
136 // Step 5.
137 return RegExpBuiltinExec(rx, S);
138 }
139
140 // Stes 4-6
141 return RegExpMatchSlowPath(rx, S);
142 }
143
144 // ES2023 draft rev 2c78e6f6b5bc6bfbf79dd8a12a9593e5b57afcd2
145 // 22.2.5.8 RegExp.prototype [ @@match ] ( string )
146 // Steps 4-6
147 function RegExpMatchSlowPath(rx, S) {
148 // Step 4.
149 var flags = ToString(rx.flags);
150
151 // Step 5.
152 if (!callFunction(std_String_includes, flags, "g")) {
153 return RegExpExec(rx, S);
154 }
155
156 // Step 6.a.
157 var fullUnicode = callFunction(std_String_includes, flags, "u") || callFunction(std_String_includes, flags, "v");
158
159 // Step 6.b.
160 rx.lastIndex = 0;
161
162 // Step 6.c.
163 var A = [];
164
165 // Step 6.d.
166 var n = 0;
167
168 // Step 6.e.
169 while (true) {
170 // Step 6.e.i.
171 var result = RegExpExec(rx, S);
172
173 // Step 6.e.ii.
174 if (result === null) {
175 return n === 0 ? null : A;
176 }
177
178 // Step 6.e.iii.1.
179 var matchStr = ToString(result[0]);
180
181 // Step 6.e.iii.2.
182 DefineDataProperty(A, n, matchStr);
183
184 // Step 6.e.iii.3.
185 if (matchStr === "") {
186 var lastIndex = ToLength(rx.lastIndex);
187 rx.lastIndex = fullUnicode
188 ? AdvanceStringIndex(S, lastIndex)
189 : lastIndex + 1;
190 }
191
192 // Step 6.e.iii.4.
193 n++;
194 }
195 }
196
197 // ES 2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e 21.2.5.6.
198 // Steps 6.b-e.
199 // Optimized path for @@match with global flag.
200 function RegExpGlobalMatchOpt(rx, S, fullUnicode) {
201 // Step 6.b.
202 var lastIndex = 0;
203 rx.lastIndex = 0;
204
205 // Step 6.c.
206 var A = [];
207
208 // Step 6.d.
209 var n = 0;
210
211 var lengthS = S.length;
212
213 // Step 6.e.
214 while (true) {
215 // Step 6.e.i.
216 var position = RegExpSearcher(rx, S, lastIndex);
217
218 // Step 6.e.ii.
219 if (position === -1) {
220 return n === 0 ? null : A;
221 }
222
223 lastIndex = RegExpSearcherLastLimit(S);
224
225 // Step 6.e.iii.1.
226 var matchStr = Substring(S, position, lastIndex - position);
227
228 // Step 6.e.iii.2.
229 DefineDataProperty(A, n, matchStr);
230
231 // Step 6.e.iii.4.
232 if (matchStr === "") {
233 lastIndex = fullUnicode
234 ? AdvanceStringIndex(S, lastIndex)
235 : lastIndex + 1;
236 if (lastIndex > lengthS) {
237 return A;
238 }
239 }
240
241 // Step 6.e.iii.5.
242 n++;
243 }
244 }
245
246 // ES2023 draft rev 2c78e6f6b5bc6bfbf79dd8a12a9593e5b57afcd2
247 // 22.2.5.11 RegExp.prototype [ @@replace ] ( string, replaceValue )
248 function RegExpReplace(string, replaceValue) {
249 // Step 1.
250 var rx = this;
251
252 // Step 2.
253 if (!IsObject(rx)) {
254 ThrowTypeError(JSMSG_OBJECT_REQUIRED, rx === null ? "null" : typeof rx);
255 }
256
257 // Step 3.
258 var S = ToString(string);
259
260 // Step 4.
261 var lengthS = S.length;
262
263 // Step 5.
264 var functionalReplace = IsCallable(replaceValue);
265
266 // Step 6.
267 var firstDollarIndex = -1;
268 if (!functionalReplace) {
269 // Step 6.a.
270 replaceValue = ToString(replaceValue);
271
272 // Skip if replaceValue is an empty string or a single character.
273 // A single character string may contain "$", but that cannot be a
274 // substitution.
275 if (replaceValue.length > 1) {
276 firstDollarIndex = GetFirstDollarIndex(replaceValue);
277 }
278 }
279
280 // Optimized paths.
281 if (IsOptimizableRegExpObject(rx)) {
282 // Step 7.
283 var flags = UnsafeGetInt32FromReservedSlot(rx, REGEXP_FLAGS_SLOT);
284
285 // Step 9.
286 var global = !!(flags & REGEXP_GLOBAL_FLAG);
287
288 // Steps 9-17.
289 if (global) {
290 if (functionalReplace) {
291 // For large strings check if the replacer function is
292 // applicable for the elem-base optimization.
293 if (lengthS > 5000) {
294 var elemBase = GetElemBaseForLambda(replaceValue);
295 if (IsObject(elemBase)) {
296 return RegExpGlobalReplaceOptElemBase(
297 rx,
298 S,
299 lengthS,
300 replaceValue,
301 flags,
302 elemBase
303 );
304 }
305 }
306 return RegExpGlobalReplaceOptFunc(rx, S, lengthS, replaceValue, flags);
307 }
308 if (firstDollarIndex !== -1) {
309 return RegExpGlobalReplaceOptSubst(
310 rx,
311 S,
312 lengthS,
313 replaceValue,
314 flags,
315 firstDollarIndex
316 );
317 }
318 return RegExpGlobalReplaceOptSimple(rx, S, lengthS, replaceValue, flags);
319 }
320
321 if (functionalReplace) {
322 return RegExpLocalReplaceOptFunc(rx, S, lengthS, replaceValue);
323 }
324 if (firstDollarIndex !== -1) {
325 return RegExpLocalReplaceOptSubst(
326 rx,
327 S,
328 lengthS,
329 replaceValue,
330 firstDollarIndex
331 );
332 }
333 return RegExpLocalReplaceOptSimple(rx, S, lengthS, replaceValue);
334 }
335
336 // Steps 7-17.
337 return RegExpReplaceSlowPath(
338 rx,
339 S,
340 lengthS,
341 replaceValue,
342 functionalReplace,
343 firstDollarIndex
344 );
345 }
346
347 // ES2023 draft rev 2c78e6f6b5bc6bfbf79dd8a12a9593e5b57afcd2
348 // 22.2.5.11 RegExp.prototype [ @@replace ] ( string, replaceValue )
349 // Steps 7-17.
350 // Slow path for @@replace.
351 function RegExpReplaceSlowPath(
352 rx,
353 S,
354 lengthS,
355 replaceValue,
356 functionalReplace,
357 firstDollarIndex
358 ) {
359 // Step 7.
360 var flags = ToString(rx.flags);
361
362 // Step 8.
363 var global = callFunction(std_String_includes, flags, "g");
364
365 // Step 9.
366 var fullUnicode = false;
367 if (global) {
368 // Step 9.a.
369 fullUnicode = callFunction(std_String_includes, flags, "u") || callFunction(std_String_includes, flags, "v");
370
371 // Step 9.b.
372 rx.lastIndex = 0;
373 }
374
375 // Step 10.
376 var results = new_List();
377 var nResults = 0;
378
379 // Steps 11-12.
380 while (true) {
381 // Step 12.a.
382 var result = RegExpExec(rx, S);
383
384 // Step 12.b.
385 if (result === null) {
386 break;
387 }
388
389 // Step 12.c.i.
390 DefineDataProperty(results, nResults++, result);
391
392 // Step 12.c.ii.
393 if (!global) {
394 break;
395 }
396
397 // Step 12.c.iii.1.
398 var matchStr = ToString(result[0]);
399
400 // Step 12.c.iii.2.
401 if (matchStr === "") {
402 var lastIndex = ToLength(rx.lastIndex);
403 rx.lastIndex = fullUnicode
404 ? AdvanceStringIndex(S, lastIndex)
405 : lastIndex + 1;
406 }
407 }
408
409 // Step 13.
410 var accumulatedResult = "";
411
412 // Step 14.
413 var nextSourcePosition = 0;
414
415 // Step 15.
416 for (var i = 0; i < nResults; i++) {
417 result = results[i];
418
419 // Steps 15.a-b.
420 var nCaptures = std_Math_max(ToLength(result.length) - 1, 0);
421
422 // Step 15.c.
423 var matched = ToString(result[0]);
424
425 // Step 15.d.
426 var matchLength = matched.length;
427
428 // Steps 15.e-f.
429 var position = std_Math_max(
430 std_Math_min(ToInteger(result.index), lengthS),
431 0
432 );
433
434 var replacement;
435 if (functionalReplace || firstDollarIndex !== -1) {
436 // Steps 15.g-l.
437 replacement = RegExpGetComplexReplacement(
438 result,
439 matched,
440 S,
441 position,
442 nCaptures,
443 replaceValue,
444 functionalReplace,
445 firstDollarIndex
446 );
447 } else {
448 // Steps 15.g, 15.i, 15.i.iv.
449 // We don't need captures array, but ToString is visible to script.
450 for (var n = 1; n <= nCaptures; n++) {
451 // Steps 15.i.i-ii.
452 var capN = result[n];
453
454 // Step 15.i.ii.
455 if (capN !== undefined) {
456 ToString(capN);
457 }
458 }
459
460 // Steps 15.j, 15.l.i.
461 // We don't need namedCaptures, but ToObject is visible to script.
462 var namedCaptures = result.groups;
463 if (namedCaptures !== undefined) {
464 ToObject(namedCaptures);
465 }
466
467 // Step 15.l.ii.
468 replacement = replaceValue;
469 }
470
471 // Step 15.m.
472 if (position >= nextSourcePosition) {
473 // Step 15.m.ii.
474 accumulatedResult +=
475 Substring(S, nextSourcePosition, position - nextSourcePosition) +
476 replacement;
477
478 // Step 15.m.iii.
479 nextSourcePosition = position + matchLength;
480 }
481 }
482
483 // Step 16.
484 if (nextSourcePosition >= lengthS) {
485 return accumulatedResult;
486 }
487
488 // Step 17.
489 return (
490 accumulatedResult +
491 Substring(S, nextSourcePosition, lengthS - nextSourcePosition)
492 );
493 }
494
495 // ES2023 draft rev 2c78e6f6b5bc6bfbf79dd8a12a9593e5b57afcd2
496 // 22.2.5.11 RegExp.prototype [ @@replace ] ( string, replaceValue )
497 // https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
498 // Steps 15.g-l.
499 // Calculates functional/substitution replacement from match result.
500 // Used in the following functions:
501 // * RegExpReplaceSlowPath
502 function RegExpGetComplexReplacement(
503 result,
504 matched,
505 S,
506 position,
507 nCaptures,
508 replaceValue,
509 functionalReplace,
510 firstDollarIndex
511 ) {
512 // Step 15.g.
513 var captures = new_List();
514 var capturesLength = 0;
515
516 // Step 15.k.i (reordered).
517 DefineDataProperty(captures, capturesLength++, matched);
518
519 // String substitutions can only reference $1 through $99. Keep getting and
520 // stringifying all captures because those operations are observable.
521 var storedCaptures = functionalReplace
522 ? nCaptures
523 : std_Math_min(nCaptures, REGEXP_MAX_SUBSTITUTION_CAPTURES);
524
525 // Steps 15.h, 15.i, 15.i.v.
526 for (var n = 1; n <= nCaptures; n++) {
527 // Step 15.i.i.
528 var capN = result[n];
529
530 // Step 15.i.ii.
531 if (capN !== undefined) {
532 capN = ToString(capN);
533 }
534
535 // Step 15.i.iii.
536 if (n <= storedCaptures) {
537 DefineDataProperty(captures, capturesLength++, capN);
538 }
539 }
540
541 // Step 15.j.
542 var namedCaptures = result.groups;
543
544 // Step 15.k.
545 if (functionalReplace) {
546 // For `nCaptures` <= 4 case, call `replaceValue` directly, otherwise
547 // use `std_Function_apply` with all arguments stored in `captures`.
548 if (namedCaptures === undefined) {
549 switch (nCaptures) {
550 case 0:
551 return ToString(
552 callContentFunction(
553 replaceValue,
554 undefined,
555 SPREAD(captures, 1),
556 position,
557 S
558 )
559 );
560 case 1:
561 return ToString(
562 callContentFunction(
563 replaceValue,
564 undefined,
565 SPREAD(captures, 2),
566 position,
567 S
568 )
569 );
570 case 2:
571 return ToString(
572 callContentFunction(
573 replaceValue,
574 undefined,
575 SPREAD(captures, 3),
576 position,
577 S
578 )
579 );
580 case 3:
581 return ToString(
582 callContentFunction(
583 replaceValue,
584 undefined,
585 SPREAD(captures, 4),
586 position,
587 S
588 )
589 );
590 case 4:
591 return ToString(
592 callContentFunction(
593 replaceValue,
594 undefined,
595 SPREAD(captures, 5),
596 position,
597 S
598 )
599 );
600 }
601 }
602
603 // Steps 15.k.ii-vi.
604 DefineDataProperty(captures, capturesLength++, position);
605 DefineDataProperty(captures, capturesLength++, S);
606 if (namedCaptures !== undefined) {
607 DefineDataProperty(captures, capturesLength++, namedCaptures);
608 }
609 return ToString(
610 callFunction(std_Function_apply, replaceValue, undefined, captures)
611 );
612 }
613
614 // Step 15.l.
615 if (namedCaptures !== undefined) {
616 namedCaptures = ToObject(namedCaptures);
617 }
618 return RegExpGetSubstitution(
619 captures,
620 S,
621 position,
622 replaceValue,
623 firstDollarIndex,
624 namedCaptures
625 );
626 }
627
628 // ES2023 draft rev 2c78e6f6b5bc6bfbf79dd8a12a9593e5b57afcd2
629 // 22.2.5.11 RegExp.prototype [ @@replace ] ( string, replaceValue )
630 // https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
631 // Steps 15.g-k.
632 // Calculates functional replacement from match result.
633 // Used in the following functions:
634 // * RegExpGlobalReplaceOptFunc
635 // * RegExpGlobalReplaceOptElemBase
636 // * RegExpLocalReplaceOptFunc
637 function RegExpGetFunctionalReplacement(result, S, position, replaceValue) {
638 // For `nCaptures` <= 4 case, call `replaceValue` directly, otherwise
639 // use `std_Function_apply` with all arguments stored in `captures`.
640 assert(result.length >= 1, "RegExpMatcher doesn't return an empty array");
641 var nCaptures = result.length - 1;
642
643 // Step 15.j (reordered)
644 var namedCaptures = result.groups;
645
646 if (namedCaptures === undefined) {
647 switch (nCaptures) {
648 case 0:
649 return ToString(
650 callContentFunction(
651 replaceValue,
652 undefined,
653 SPREAD(result, 1),
654 position,
655 S
656 )
657 );
658 case 1:
659 return ToString(
660 callContentFunction(
661 replaceValue,
662 undefined,
663 SPREAD(result, 2),
664 position,
665 S
666 )
667 );
668 case 2:
669 return ToString(
670 callContentFunction(
671 replaceValue,
672 undefined,
673 SPREAD(result, 3),
674 position,
675 S
676 )
677 );
678 case 3:
679 return ToString(
680 callContentFunction(
681 replaceValue,
682 undefined,
683 SPREAD(result, 4),
684 position,
685 S
686 )
687 );
688 case 4:
689 return ToString(
690 callContentFunction(
691 replaceValue,
692 undefined,
693 SPREAD(result, 5),
694 position,
695 S
696 )
697 );
698 }
699 }
700
701 // Steps 15.g-i, 15.k.i-ii.
702 var captures = new_List();
703 for (var n = 0; n <= nCaptures; n++) {
704 assert(
705 typeof result[n] === "string" || result[n] === undefined,
706 "RegExpMatcher returns only strings and undefined"
707 );
708 DefineDataProperty(captures, n, result[n]);
709 }
710
711 // Step 15.k.iii.
712 DefineDataProperty(captures, nCaptures + 1, position);
713 DefineDataProperty(captures, nCaptures + 2, S);
714
715 // Step 15.k.iv.
716 if (namedCaptures !== undefined) {
717 DefineDataProperty(captures, nCaptures + 3, namedCaptures);
718 }
719
720 // Steps 15.k.v-vi.
721 return ToString(
722 callFunction(std_Function_apply, replaceValue, undefined, captures)
723 );
724 }
725
726 // ES2023 draft rev 2c78e6f6b5bc6bfbf79dd8a12a9593e5b57afcd2
727 // 22.2.5.11 RegExp.prototype [ @@replace ] ( string, replaceValue )
728 // Steps 9.b-17.
729 // Optimized path for @@replace with the following conditions:
730 // * global flag is true
731 // * replaceValue is a string without "$"
732 function RegExpGlobalReplaceOptSimple(rx, S, lengthS, replaceValue, flags) {
733 // Step 9.a.
734 var fullUnicode = !!(flags & REGEXP_UNICODE_FLAG) || !!(flags & REGEXP_UNICODESETS_FLAG);
735
736 // Step 9.b.
737 var lastIndex = 0;
738 rx.lastIndex = 0;
739
740 // Step 13 (reordered).
741 var accumulatedResult = "";
742
743 // Step 14 (reordered).
744 var nextSourcePosition = 0;
745
746 // Step 12.
747 while (true) {
748 // Step 12.a.
749 var position = RegExpSearcher(rx, S, lastIndex);
750
751 // Step 12.b.
752 if (position === -1) {
753 break;
754 }
755
756 lastIndex = RegExpSearcherLastLimit(S);
757
758 // Step 15.m.ii.
759 accumulatedResult +=
760 Substring(S, nextSourcePosition, position - nextSourcePosition) +
761 replaceValue;
762
763 // Step 15.m.iii.
764 nextSourcePosition = lastIndex;
765
766 // Step 12.c.iii.2.
767 if (lastIndex === position) {
768 lastIndex = fullUnicode
769 ? AdvanceStringIndex(S, lastIndex)
770 : lastIndex + 1;
771 if (lastIndex > lengthS) {
772 break;
773 }
774 }
775 }
776
777 // Step 16.
778 if (nextSourcePosition >= lengthS) {
779 return accumulatedResult;
780 }
781
782 // Step 17.
783 return (
784 accumulatedResult +
785 Substring(S, nextSourcePosition, lengthS - nextSourcePosition)
786 );
787 }
788
789 // ES2023 draft rev 2c78e6f6b5bc6bfbf79dd8a12a9593e5b57afcd2
790 // 22.2.5.11 RegExp.prototype [ @@replace ] ( string, replaceValue )
791 // Steps 7-17.
792 // Optimized path for @@replace.
793
794 // Conditions:
795 // * global flag is true
796 // * replaceValue is a function
797 #define FUNC_NAME RegExpGlobalReplaceOptFunc
798 #define FUNCTIONAL
799 #include "RegExpGlobalReplaceOpt.h.js"
800 #undef FUNCTIONAL
801 #undef FUNC_NAME
802 /* global RegExpGlobalReplaceOptFunc */
803
804 // Conditions:
805 // * global flag is true
806 // * replaceValue is a function that returns element of an object
807 #define FUNC_NAME RegExpGlobalReplaceOptElemBase
808 #define ELEMBASE
809 #include "RegExpGlobalReplaceOpt.h.js"
810 #undef ELEMBASE
811 #undef FUNC_NAME
812 /* global RegExpGlobalReplaceOptElemBase */
813
814 // Conditions:
815 // * global flag is true
816 // * replaceValue is a string with "$"
817 #define FUNC_NAME RegExpGlobalReplaceOptSubst
818 #define SUBSTITUTION
819 #include "RegExpGlobalReplaceOpt.h.js"
820 #undef SUBSTITUTION
821 #undef FUNC_NAME
822 /* global RegExpGlobalReplaceOptSubst */
823
824 // Conditions:
825 // * global flag is false
826 // * replaceValue is a string without "$"
827 #define FUNC_NAME RegExpLocalReplaceOptSimple
828 #define SIMPLE
829 #include "RegExpLocalReplaceOpt.h.js"
830 #undef SIMPLE
831 #undef FUNC_NAME
832 /* global RegExpLocalReplaceOptSimple */
833
834 // Conditions:
835 // * global flag is false
836 // * replaceValue is a function
837 #define FUNC_NAME RegExpLocalReplaceOptFunc
838 #define FUNCTIONAL
839 #include "RegExpLocalReplaceOpt.h.js"
840 #undef FUNCTIONAL
841 #undef FUNC_NAME
842 /* global RegExpLocalReplaceOptFunc */
843
844 // Conditions:
845 // * global flag is false
846 // * replaceValue is a string with "$"
847 #define FUNC_NAME RegExpLocalReplaceOptSubst
848 #define SUBSTITUTION
849 #include "RegExpLocalReplaceOpt.h.js"
850 #undef SUBSTITUTION
851 #undef FUNC_NAME
852 /* global RegExpLocalReplaceOptSubst */
853
854 // ES2017 draft rev 6390c2f1b34b309895d31d8c0512eac8660a0210
855 // 21.2.5.9 RegExp.prototype [ @@search ] ( string )
856 function RegExpSearch(string) {
857 // Step 1.
858 var rx = this;
859
860 // Step 2.
861 if (!IsObject(rx)) {
862 ThrowTypeError(JSMSG_OBJECT_REQUIRED, rx === null ? "null" : typeof rx);
863 }
864
865 // Step 3.
866 var S = ToString(string);
867
868 // Step 4.
869 var previousLastIndex = rx.lastIndex;
870
871 // Step 5.
872 var lastIndexIsZero = SameValue(previousLastIndex, 0);
873 if (!lastIndexIsZero) {
874 rx.lastIndex = 0;
875 }
876
877 if (IsOptimizableRegExpObject(rx) && S.length < 0x7fff) {
878 // Step 6.
879 var result = RegExpSearcher(rx, S, 0);
880
881 // We need to consider two cases:
882 //
883 // 1. Neither global nor sticky is set:
884 // RegExpBuiltinExec doesn't modify lastIndex for local RegExps, that
885 // means |SameValue(rx.lastIndex, 0)| is true after calling exec. The
886 // comparison in steps 7-8 |SameValue(rx.lastIndex, previousLastIndex)|
887 // is therefore equal to the already computed |lastIndexIsZero| value.
888 //
889 // 2. Global or sticky flag is set.
890 // RegExpBuiltinExec will always update lastIndex and we need to
891 // restore the property to its original value.
892
893 // Steps 7-8.
894 if (!lastIndexIsZero) {
895 rx.lastIndex = previousLastIndex;
896 } else {
897 var flags = UnsafeGetInt32FromReservedSlot(rx, REGEXP_FLAGS_SLOT);
898 if (flags & (REGEXP_GLOBAL_FLAG | REGEXP_STICKY_FLAG)) {
899 rx.lastIndex = previousLastIndex;
900 }
901 }
902
903 // Steps 9-10.
904 return result;
905 }
906
907 return RegExpSearchSlowPath(rx, S, previousLastIndex);
908 }
909
910 // ES2017 draft rev 6390c2f1b34b309895d31d8c0512eac8660a0210
911 // 21.2.5.9 RegExp.prototype [ @@search ] ( string )
912 // Steps 6-10.
913 function RegExpSearchSlowPath(rx, S, previousLastIndex) {
914 // Step 6.
915 var result = RegExpExec(rx, S);
916
917 // Step 7.
918 var currentLastIndex = rx.lastIndex;
919
920 // Step 8.
921 if (!SameValue(currentLastIndex, previousLastIndex)) {
922 rx.lastIndex = previousLastIndex;
923 }
924
925 // Step 9.
926 if (result === null) {
927 return -1;
928 }
929
930 // Step 10.
931 return result.index;
932 }
933
934 // ES 2017 draft 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e 21.2.5.11.
935 function RegExpSplit(string, limit) {
936 // Step 1.
937 var rx = this;
938
939 // Step 2.
940 if (!IsObject(rx)) {
941 ThrowTypeError(JSMSG_OBJECT_REQUIRED, rx === null ? "null" : typeof rx);
942 }
943
944 // Step 3.
945 var S = ToString(string);
946
947 // Step 4.
948 var builtinCtor = GetBuiltinConstructor("RegExp");
949 var C = SpeciesConstructor(rx, builtinCtor);
950
951 var optimizable =
952 IsOptimizableRegExpObject(rx) &&
953 C === builtinCtor &&
954 (limit === undefined || typeof limit === "number");
955
956 var flags, unicodeMatching, splitter;
957 if (optimizable) {
958 // Step 5.
959 flags = UnsafeGetInt32FromReservedSlot(rx, REGEXP_FLAGS_SLOT);
960
961 // Steps 6-7.
962 unicodeMatching = !!(flags & REGEXP_UNICODE_FLAG);
963
964 // Steps 8-10.
965 // If split operation is optimizable, perform non-sticky match.
966 if (flags & REGEXP_STICKY_FLAG) {
967 var source = UnsafeGetStringFromReservedSlot(rx, REGEXP_SOURCE_SLOT);
968 splitter = RegExpConstructRaw(source, flags & ~REGEXP_STICKY_FLAG);
969 } else {
970 splitter = rx;
971 }
972 } else {
973 // Step 5.
974 flags = ToString(rx.flags);
975
976 // Steps 6-7.
977 unicodeMatching = callFunction(std_String_includes, flags, "u");
978
979 // Steps 8-9.
980 var newFlags;
981 if (callFunction(std_String_includes, flags, "y")) {
982 newFlags = flags;
983 } else {
984 newFlags = flags + "y";
985 }
986
987 // Step 10.
988 splitter = constructContentFunction(C, C, rx, newFlags);
989 }
990
991 // Step 11.
992 var A = [];
993
994 // Step 12.
995 var lengthA = 0;
996
997 // Step 13.
998 var lim;
999 if (limit === undefined) {
1000 lim = MAX_UINT32;
1001 } else {
1002 lim = limit >>> 0;
1003 }
1004
1005 // Step 15.
1006 var p = 0;
1007
1008 // Step 16.
1009 if (lim === 0) {
1010 return A;
1011 }
1012
1013 // Step 14 (reordered).
1014 var size = S.length;
1015
1016 // Step 17.
1017 if (size === 0) {
1018 // Step 17.a-b.
1019 if (optimizable) {
1020 if (RegExpSearcher(splitter, S, 0) !== -1) {
1021 return A;
1022 }
1023 } else {
1024 if (RegExpExec(splitter, S) !== null) {
1025 return A;
1026 }
1027 }
1028
1029 // Step 17.d.
1030 DefineDataProperty(A, 0, S);
1031
1032 // Step 17.e.
1033 return A;
1034 }
1035
1036 // Step 18.
1037 var q = p;
1038
1039 var optimizableNoCaptures = optimizable && !RegExpHasCaptureGroups(splitter, S);
1040
1041 // Step 19.
1042 while (q < size) {
1043 var e, z;
1044 if (optimizableNoCaptures) {
1045 // If there are no capturing groups, avoid allocating the match result
1046 // object |z| (we set it to null). This is the only difference between
1047 // this branch and the |if (optimizable)| case below.
1048
1049 // Step 19.a (skipped).
1050 // splitter.lastIndex is not used.
1051
1052 // Steps 19.b-c.
1053 q = RegExpSearcher(splitter, S, q);
1054 if (q === -1 || q >= size) {
1055 break;
1056 }
1057
1058 // Step 19.d.i.
1059 e = RegExpSearcherLastLimit(S);
1060 z = null;
1061 } else if (optimizable) {
1062 // Step 19.a (skipped).
1063 // splitter.lastIndex is not used.
1064
1065 // Step 19.b.
1066 z = RegExpMatcher(splitter, S, q);
1067
1068 // Step 19.c.
1069 if (z === null) {
1070 break;
1071 }
1072
1073 // splitter.lastIndex is not updated.
1074 q = z.index;
1075 if (q >= size) {
1076 break;
1077 }
1078
1079 // Step 19.d.i.
1080 e = q + z[0].length;
1081 } else {
1082 // Step 19.a.
1083 splitter.lastIndex = q;
1084
1085 // Step 19.b.
1086 z = RegExpExec(splitter, S);
1087
1088 // Step 19.c.
1089 if (z === null) {
1090 q = unicodeMatching ? AdvanceStringIndex(S, q) : q + 1;
1091 continue;
1092 }
1093
1094 // Step 19.d.i.
1095 e = ToLength(splitter.lastIndex);
1096 }
1097
1098 // Step 19.d.iii.
1099 if (e === p) {
1100 q = unicodeMatching ? AdvanceStringIndex(S, q) : q + 1;
1101 continue;
1102 }
1103
1104 // Steps 19.d.iv.1-3.
1105 DefineDataProperty(A, lengthA, Substring(S, p, q - p));
1106
1107 // Step 19.d.iv.4.
1108 lengthA++;
1109
1110 // Step 19.d.iv.5.
1111 if (lengthA === lim) {
1112 return A;
1113 }
1114
1115 // Step 19.d.iv.6.
1116 p = e;
1117
1118 if (z !== null) {
1119 // Steps 19.d.iv.7-8.
1120 var numberOfCaptures = std_Math_max(ToLength(z.length) - 1, 0);
1121
1122 // Step 19.d.iv.9.
1123 var i = 1;
1124
1125 // Step 19.d.iv.10.
1126 while (i <= numberOfCaptures) {
1127 // Steps 19.d.iv.10.a-b.
1128 DefineDataProperty(A, lengthA, z[i]);
1129
1130 // Step 19.d.iv.10.c.
1131 i++;
1132
1133 // Step 19.d.iv.10.d.
1134 lengthA++;
1135
1136 // Step 19.d.iv.10.e.
1137 if (lengthA === lim) {
1138 return A;
1139 }
1140 }
1141 }
1142
1143 // Step 19.d.iv.11.
1144 q = p;
1145 }
1146
1147 // Steps 20-22.
1148 if (p >= size) {
1149 DefineDataProperty(A, lengthA, "");
1150 } else {
1151 DefineDataProperty(A, lengthA, Substring(S, p, size - p));
1152 }
1153
1154 // Step 23.
1155 return A;
1156 }
1157
1158 // ES6 21.2.5.2.
1159 // NOTE: This is not RegExpExec (21.2.5.2.1).
1160 function RegExp_prototype_Exec(string) {
1161 // Steps 1-3.
1162 var R = this;
1163 if (!IsObject(R) || !IsRegExpObject(R)) {
1164 return callFunction(
1165 CallRegExpMethodIfWrapped,
1166 R,
1167 string,
1168 "RegExp_prototype_Exec"
1169 );
1170 }
1171
1172 // Steps 4-5.
1173 var S = ToString(string);
1174
1175 // Step 6.
1176 return RegExpBuiltinExec(R, S);
1177 }
1178
1179 // ES6 21.2.5.13.
1180 function RegExpTest(string) {
1181 // Steps 1-2.
1182 var R = this;
1183 if (!IsObject(R)) {
1184 ThrowTypeError(JSMSG_OBJECT_REQUIRED, R === null ? "null" : typeof R);
1185 }
1186
1187 // Steps 3-4.
1188 var S = ToString(string);
1189
1190 // Steps 5-6.
1191 return RegExpExecForTest(R, S);
1192 }
1193
1194 // ES 2016 draft Mar 25, 2016 21.2.4.2.
1195 function $RegExpSpecies() {
1196 // Step 1.
1197 return this;
1198 }
1199 SetCanonicalName($RegExpSpecies, "get [Symbol.species]");
1200
1201 // String.prototype.matchAll proposal.
1202 //
1203 // RegExp.prototype [ @@matchAll ] ( string )
1204 function RegExpMatchAll(string) {
1205 // Step 1.
1206 var rx = this;
1207
1208 // Step 2.
1209 if (!IsObject(rx)) {
1210 ThrowTypeError(JSMSG_OBJECT_REQUIRED, rx === null ? "null" : typeof rx);
1211 }
1212
1213 // Step 3.
1214 var str = ToString(string);
1215
1216 // Step 4.
1217 var builtinCtor = GetBuiltinConstructor("RegExp");
1218 var C = SpeciesConstructor(rx, builtinCtor);
1219
1220 var source, flags, matcher, lastIndex;
1221 if (IsOptimizableRegExpObject(rx) && C === builtinCtor) {
1222 // Step 5, 9-12.
1223 source = UnsafeGetStringFromReservedSlot(rx, REGEXP_SOURCE_SLOT);
1224 flags = UnsafeGetInt32FromReservedSlot(rx, REGEXP_FLAGS_SLOT);
1225
1226 // Step 6.
1227 matcher = rx;
1228
1229 // Step 7.
1230 lastIndex = ToLength(rx.lastIndex);
1231
1232 // Step 8 (not applicable for the optimized path).
1233 } else {
1234 // Step 5.
1235 source = "";
1236 flags = ToString(rx.flags);
1237
1238 // Step 6.
1239 matcher = constructContentFunction(C, C, rx, flags);
1240
1241 // Steps 7-8.
1242 matcher.lastIndex = ToLength(rx.lastIndex);
1243
1244 // Steps 9-12.
1245 flags =
1246 (callFunction(std_String_includes, flags, "g") ? REGEXP_GLOBAL_FLAG : 0) |
1247 (callFunction(std_String_includes, flags, "u") ? REGEXP_UNICODE_FLAG : 0);
1248
1249 // Take the non-optimized path.
1250 lastIndex = REGEXP_STRING_ITERATOR_LASTINDEX_SLOW;
1251 }
1252
1253 // Step 13.
1254 return CreateRegExpStringIterator(matcher, str, source, flags, lastIndex);
1255 }
1256
1257 // String.prototype.matchAll proposal.
1258 //
1259 // CreateRegExpStringIterator ( R, S, global, fullUnicode )
1260 function CreateRegExpStringIterator(regexp, string, source, flags, lastIndex) {
1261 // Step 1.
1262 assert(typeof string === "string", "|string| is a string value");
1263
1264 // Steps 2-3.
1265 assert(typeof flags === "number", "|flags| is a number value");
1266
1267 assert(typeof source === "string", "|source| is a string value");
1268 assert(typeof lastIndex === "number", "|lastIndex| is a number value");
1269
1270 // Steps 4-9.
1271 var iterator = NewRegExpStringIterator();
1272 UnsafeSetReservedSlot(iterator, REGEXP_STRING_ITERATOR_REGEXP_SLOT, regexp);
1273 UnsafeSetReservedSlot(iterator, REGEXP_STRING_ITERATOR_STRING_SLOT, string);
1274 UnsafeSetReservedSlot(iterator, REGEXP_STRING_ITERATOR_SOURCE_SLOT, source);
1275 UnsafeSetReservedSlot(iterator, REGEXP_STRING_ITERATOR_FLAGS_SLOT, flags | 0);
1276 UnsafeSetReservedSlot(
1277 iterator,
1278 REGEXP_STRING_ITERATOR_LASTINDEX_SLOT,
1279 lastIndex
1280 );
1281
1282 // Step 10.
1283 return iterator;
1284 }
1285
1286 // String.prototype.matchAll proposal.
1287 //
1288 // %RegExpStringIteratorPrototype%.next ( )
1289 function RegExpStringIteratorNext() {
1290 // Steps 1-3.
1291 var obj = this;
1292 if (!IsObject(obj) || (obj = GuardToRegExpStringIterator(obj)) === null) {
1293 return callFunction(
1294 CallRegExpStringIteratorMethodIfWrapped,
1295 this,
1296 "RegExpStringIteratorNext"
1297 );
1298 }
1299
1300 var result = { value: undefined, done: false };
1301
1302 // Step 4.
1303 var lastIndex = UnsafeGetReservedSlot(
1304 obj,
1305 REGEXP_STRING_ITERATOR_LASTINDEX_SLOT
1306 );
1307 if (lastIndex === REGEXP_STRING_ITERATOR_LASTINDEX_DONE) {
1308 result.done = true;
1309 return result;
1310 }
1311
1312 // Step 5.
1313 var regexp = UnsafeGetObjectFromReservedSlot(
1314 obj,
1315 REGEXP_STRING_ITERATOR_REGEXP_SLOT
1316 );
1317
1318 // Step 6.
1319 var string = UnsafeGetStringFromReservedSlot(
1320 obj,
1321 REGEXP_STRING_ITERATOR_STRING_SLOT
1322 );
1323
1324 // Steps 7-8.
1325 var flags = UnsafeGetInt32FromReservedSlot(
1326 obj,
1327 REGEXP_STRING_ITERATOR_FLAGS_SLOT
1328 );
1329 var global = !!(flags & REGEXP_GLOBAL_FLAG);
1330 var fullUnicode = !!(flags & REGEXP_UNICODE_FLAG) || !!(flags & REGEXP_UNICODESETS_FLAG);
1331
1332 if (lastIndex >= 0) {
1333 assert(IsRegExpObject(regexp), "|regexp| is a RegExp object");
1334
1335 var source = UnsafeGetStringFromReservedSlot(
1336 obj,
1337 REGEXP_STRING_ITERATOR_SOURCE_SLOT
1338 );
1339 if (
1340 IsRegExpPrototypeOptimizable() &&
1341 UnsafeGetStringFromReservedSlot(regexp, REGEXP_SOURCE_SLOT) === source &&
1342 UnsafeGetInt32FromReservedSlot(regexp, REGEXP_FLAGS_SLOT) === flags
1343 ) {
1344 // Step 9 (Inlined RegExpBuiltinExec).
1345 var globalOrSticky = !!(
1346 flags &
1347 (REGEXP_GLOBAL_FLAG | REGEXP_STICKY_FLAG)
1348 );
1349 if (!globalOrSticky) {
1350 lastIndex = 0;
1351 }
1352
1353 var match =
1354 lastIndex <= string.length
1355 ? RegExpMatcher(regexp, string, lastIndex)
1356 : null;
1357
1358 // Step 10.
1359 if (match === null) {
1360 // Step 10.a.
1361 UnsafeSetReservedSlot(
1362 obj,
1363 REGEXP_STRING_ITERATOR_LASTINDEX_SLOT,
1364 REGEXP_STRING_ITERATOR_LASTINDEX_DONE
1365 );
1366
1367 // Step 10.b.
1368 result.done = true;
1369 return result;
1370 }
1371
1372 // Step 11.a.
1373 if (global) {
1374 // Step 11.a.i.
1375 var matchLength = match[0].length;
1376 lastIndex = match.index + matchLength;
1377
1378 // Step 11.a.ii.
1379 if (matchLength === 0) {
1380 // Steps 11.a.ii.1-3.
1381 lastIndex = fullUnicode
1382 ? AdvanceStringIndex(string, lastIndex)
1383 : lastIndex + 1;
1384 }
1385
1386 UnsafeSetReservedSlot(
1387 obj,
1388 REGEXP_STRING_ITERATOR_LASTINDEX_SLOT,
1389 lastIndex
1390 );
1391 } else {
1392 // Step 11.b.i.
1393 UnsafeSetReservedSlot(
1394 obj,
1395 REGEXP_STRING_ITERATOR_LASTINDEX_SLOT,
1396 REGEXP_STRING_ITERATOR_LASTINDEX_DONE
1397 );
1398 }
1399
1400 // Steps 11.a.iii and 11.b.ii.
1401 result.value = match;
1402 return result;
1403 }
1404
1405 // Reify the RegExp object.
1406 regexp = RegExpConstructRaw(source, flags);
1407 regexp.lastIndex = lastIndex;
1408 UnsafeSetReservedSlot(obj, REGEXP_STRING_ITERATOR_REGEXP_SLOT, regexp);
1409
1410 // Mark the iterator as no longer optimizable.
1411 UnsafeSetReservedSlot(
1412 obj,
1413 REGEXP_STRING_ITERATOR_LASTINDEX_SLOT,
1414 REGEXP_STRING_ITERATOR_LASTINDEX_SLOW
1415 );
1416 }
1417
1418 // Step 9.
1419 var match = RegExpExec(regexp, string);
1420
1421 // Step 10.
1422 if (match === null) {
1423 // Step 10.a.
1424 UnsafeSetReservedSlot(
1425 obj,
1426 REGEXP_STRING_ITERATOR_LASTINDEX_SLOT,
1427 REGEXP_STRING_ITERATOR_LASTINDEX_DONE
1428 );
1429
1430 // Step 10.b.
1431 result.done = true;
1432 return result;
1433 }
1434
1435 // Step 11.a.
1436 if (global) {
1437 // Step 11.a.i.
1438 var matchStr = ToString(match[0]);
1439
1440 // Step 11.a.ii.
1441 if (matchStr.length === 0) {
1442 // Step 11.a.ii.1.
1443 var thisIndex = ToLength(regexp.lastIndex);
1444
1445 // Step 11.a.ii.2.
1446 var nextIndex = fullUnicode
1447 ? AdvanceStringIndex(string, thisIndex)
1448 : thisIndex + 1;
1449
1450 // Step 11.a.ii.3.
1451 regexp.lastIndex = nextIndex;
1452 }
1453 } else {
1454 // Step 11.b.i.
1455 UnsafeSetReservedSlot(
1456 obj,
1457 REGEXP_STRING_ITERATOR_LASTINDEX_SLOT,
1458 REGEXP_STRING_ITERATOR_LASTINDEX_DONE
1459 );
1460 }
1461
1462 // Steps 11.a.iii and 11.b.ii.
1463 result.value = match;
1464 return result;
1465 }
1466
1467 // ES2020 draft rev e97c95d064750fb949b6778584702dd658cf5624
1468 // 7.2.8 IsRegExp ( argument )
1469 function IsRegExp(argument) {
1470 // Step 1.
1471 if (!IsObject(argument)) {
1472 return false;
1473 }
1474
1475 // Step 2.
1476 var matcher = argument[GetBuiltinSymbol("match")];
1477
1478 // Step 3.
1479 if (matcher !== undefined) {
1480 return !!matcher;
1481 }
1482
1483 // Steps 4-5.
1484 return IsPossiblyWrappedRegExpObject(argument);
1485 }