Debian Codegraph @

js/src/builtin/String.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 function ThrowIncompatibleMethod(name, thisv) {
6 ThrowTypeError(JSMSG_INCOMPATIBLE_PROTO, "String", name, ToString(thisv));
7 }
8
9 // ES 2016 draft Mar 25, 2016 21.1.3.11.
10 function String_match(regexp) {
11 // Step 1.
12 if (IsNullOrUndefined(this)) {
13 ThrowIncompatibleMethod("match", this);
14 }
15
16 // Step 2.
17 var isPatternString = typeof regexp === "string";
18 if (
19 !(isPatternString && CanOptimizeStringProtoSymbolLookup()) &&
20 !IsNullOrUndefined(regexp)
21 ) {
22 // Fast path for regular expressions with the original
23 // RegExp.prototype[@@match] function.
24 if (IsObject(regexp) && IsOptimizableRegExpObject(regexp)) {
25 return callFunction(RegExpMatch, regexp, this);
26 }
27
28 // Step 2.a.
29 var matcher = GetMethod(regexp, GetBuiltinSymbol("match"));
30
31 // Step 2.b.
32 if (matcher !== undefined) {
33 if (!IsObject(regexp)) {
34 RegExpSymbolProtocolOnPrimitiveCounter();
35 }
36 return callContentFunction(matcher, regexp, this);
37 }
38 }
39
40 // Step 3.
41 var S = ToString(this);
42
43 if (isPatternString && IsRegExpPrototypeOptimizable()) {
44 var flatResult = FlatStringMatch(S, regexp);
45 if (flatResult !== undefined) {
46 return flatResult;
47 }
48 }
49
50 // Step 4.
51 var rx = RegExpCreate(regexp);
52
53 // Step 5 (optimized case).
54 if (IsRegExpPrototypeOptimizable()) {
55 return RegExpMatcher(rx, S, 0);
56 }
57
58 // Step 5.
59 return callContentFunction(GetMethod(rx, GetBuiltinSymbol("match")), rx, S);
60 }
61
62 // String.prototype.matchAll proposal.
63 //
64 // String.prototype.matchAll ( regexp )
65 function String_matchAll(regexp) {
66 // Step 1.
67 if (IsNullOrUndefined(this)) {
68 ThrowIncompatibleMethod("matchAll", this);
69 }
70
71 // Step 2.
72 if (!IsNullOrUndefined(regexp)) {
73 // Steps 2.a-b.
74 if (IsRegExp(regexp)) {
75 // Step 2.b.i.
76 var flags = regexp.flags;
77
78 // Step 2.b.ii.
79 if (IsNullOrUndefined(flags)) {
80 ThrowTypeError(JSMSG_FLAGS_UNDEFINED_OR_NULL);
81 }
82
83 // Step 2.b.iii.
84 if (!callFunction(std_String_includes, ToString(flags), "g")) {
85 ThrowTypeError(JSMSG_REQUIRES_GLOBAL_REGEXP, "matchAll");
86 }
87 }
88
89 // Fast path for regular expressions with the original
90 // RegExp.prototype[@@matchAll] function.
91 if (IsObject(regexp) && IsOptimizableRegExpObject(regexp)) {
92 return callFunction(RegExpMatchAll, regexp, this);
93 }
94
95 // Step 2.c.
96 var matcher = GetMethod(regexp, GetBuiltinSymbol("matchAll"));
97
98 // Step 2.d.
99 if (matcher !== undefined) {
100 if (!IsObject(regexp)) {
101 RegExpSymbolProtocolOnPrimitiveCounter();
102 }
103 return callContentFunction(matcher, regexp, this);
104 }
105 }
106
107 // Step 3.
108 var string = ToString(this);
109
110 // Step 4.
111 var rx = RegExpCreate(regexp, "g");
112
113 // Step 5.
114 return callContentFunction(
115 GetMethod(rx, GetBuiltinSymbol("matchAll")),
116 rx,
117 string
118 );
119 }
120
121 /**
122 * A helper function implementing the logic for both String.prototype.padStart
123 * and String.prototype.padEnd as described in ES7 Draft March 29, 2016
124 */
125 function String_pad(maxLength, fillString, padEnd) {
126 // Step 1.
127 if (IsNullOrUndefined(this)) {
128 ThrowIncompatibleMethod(padEnd ? "padEnd" : "padStart", this);
129 }
130
131 // Step 2.
132 var str = ToString(this);
133
134 // Steps 3-4.
135 var intMaxLength = ToLength(maxLength);
136 var strLen = str.length;
137
138 // Step 5.
139 if (intMaxLength <= strLen) {
140 return str;
141 }
142
143 // Steps 6-7.
144 assert(fillString !== undefined, "never called when fillString is undefined");
145 var filler = ToString(fillString);
146
147 // Step 8.
148 if (filler === "") {
149 return str;
150 }
151
152 // Throw an error if the final string length exceeds the maximum string
153 // length. Perform this check early so we can use int32 operations below.
154 if (intMaxLength > MAX_STRING_LENGTH) {
155 ThrowRangeError(JSMSG_RESULTING_STRING_TOO_LARGE);
156 }
157
158 // Step 9.
159 var fillLen = intMaxLength - strLen;
160
161 // Step 10.
162 // Perform an int32 division to ensure String_repeat is not called with a
163 // double to avoid repeated bailouts in ToInteger.
164 var truncatedStringFiller = callFunction(
165 String_repeat,
166 filler,
167 (fillLen / filler.length) | 0
168 );
169
170 truncatedStringFiller += Substring(filler, 0, fillLen % filler.length);
171
172 // Step 11.
173 if (padEnd === true) {
174 return str + truncatedStringFiller;
175 }
176 return truncatedStringFiller + str;
177 }
178
179 function String_pad_start(maxLength, fillString = " ") {
180 return callFunction(String_pad, this, maxLength, fillString, false);
181 }
182
183 function String_pad_end(maxLength, fillString = " ") {
184 return callFunction(String_pad, this, maxLength, fillString, true);
185 }
186
187 // A thin wrapper to call SubstringKernel with int32-typed arguments.
188 // Caller should check the range of |from| and |length|.
189 function Substring(str, from, length) {
190 assert(typeof str === "string", "|str| should be a string");
191 assert(
192 (from | 0) === from,
193 "coercing |from| into int32 should not change the value"
194 );
195 assert(
196 (length | 0) === length,
197 "coercing |length| into int32 should not change the value"
198 );
199
200 return SubstringKernel(
201 str,
202 std_Math_max(from, 0) | 0,
203 std_Math_max(length, 0) | 0
204 );
205 }
206
207 // ES 2016 draft Mar 25, 2016 21.1.3.14.
208 function String_replace(searchValue, replaceValue) {
209 // Step 1.
210 if (IsNullOrUndefined(this)) {
211 ThrowIncompatibleMethod("replace", this);
212 }
213
214 // Step 2.
215 if (
216 !(typeof searchValue === "string" && CanOptimizeStringProtoSymbolLookup()) &&
217 !IsNullOrUndefined(searchValue)
218 ) {
219 // Fast path for regular expressions with the original
220 // RegExp.prototype[@@replace] function.
221 if (IsObject(searchValue) && IsOptimizableRegExpObject(searchValue)) {
222 return callFunction(RegExpReplace, searchValue, this, replaceValue);
223 }
224
225 // Step 2.a.
226 var replacer = GetMethod(searchValue, GetBuiltinSymbol("replace"));
227
228 // Step 2.b.
229 if (replacer !== undefined) {
230 if (!IsObject(searchValue)) {
231 RegExpSymbolProtocolOnPrimitiveCounter();
232 }
233 return callContentFunction(replacer, searchValue, this, replaceValue);
234 }
235 }
236
237 // Step 3.
238 var string = ToString(this);
239
240 // Step 4.
241 var searchString = ToString(searchValue);
242
243 if (typeof replaceValue === "string") {
244 // Steps 6-12: Optimized for string case.
245 return StringReplaceString(string, searchString, replaceValue);
246 }
247
248 // Step 5.
249 if (!IsCallable(replaceValue)) {
250 // Steps 6-12.
251 return StringReplaceString(string, searchString, ToString(replaceValue));
252 }
253
254 // Step 7.
255 var pos = callFunction(std_String_indexOf, string, searchString);
256 if (pos === -1) {
257 return string;
258 }
259
260 // Step 8.
261 var replStr = ToString(
262 callContentFunction(replaceValue, undefined, searchString, pos, string)
263 );
264
265 // Step 10.
266 var tailPos = pos + searchString.length;
267
268 // Step 11.
269 var newString;
270 if (pos === 0) {
271 newString = "";
272 } else {
273 newString = Substring(string, 0, pos);
274 }
275
276 newString += replStr;
277 var stringLength = string.length;
278 if (tailPos < stringLength) {
279 newString += Substring(string, tailPos, stringLength - tailPos);
280 }
281
282 // Step 12.
283 return newString;
284 }
285
286 // String.prototype.replaceAll (Stage 3 proposal)
287 // https://tc39.es/proposal-string-replaceall/
288 //
289 // String.prototype.replaceAll ( searchValue, replaceValue )
290 function String_replaceAll(searchValue, replaceValue) {
291 // Step 1.
292 if (IsNullOrUndefined(this)) {
293 ThrowIncompatibleMethod("replaceAll", this);
294 }
295
296 // Step 2.
297 if (!IsNullOrUndefined(searchValue)) {
298 // Steps 2.a-b.
299 if (IsRegExp(searchValue)) {
300 // Step 2.b.i.
301 var flags = searchValue.flags;
302
303 // Step 2.b.ii.
304 if (IsNullOrUndefined(flags)) {
305 ThrowTypeError(JSMSG_FLAGS_UNDEFINED_OR_NULL);
306 }
307
308 // Step 2.b.iii.
309 if (!callFunction(std_String_includes, ToString(flags), "g")) {
310 ThrowTypeError(JSMSG_REQUIRES_GLOBAL_REGEXP, "replaceAll");
311 }
312 }
313
314 // Fast path for regular expressions with the original
315 // RegExp.prototype[@@replace] function.
316 if (IsObject(searchValue) && IsOptimizableRegExpObject(searchValue)) {
317 return callFunction(RegExpReplace, searchValue, this, replaceValue);
318 }
319
320 // Step 2.c.
321 var replacer = GetMethod(searchValue, GetBuiltinSymbol("replace"));
322
323 // Step 2.b.
324 if (replacer !== undefined) {
325 if (!IsObject(searchValue)) {
326 RegExpSymbolProtocolOnPrimitiveCounter();
327 }
328 return callContentFunction(replacer, searchValue, this, replaceValue);
329 }
330 }
331
332 // Step 3.
333 var string = ToString(this);
334
335 // Step 4.
336 var searchString = ToString(searchValue);
337
338 // Steps 5-6.
339 if (!IsCallable(replaceValue)) {
340 // Steps 7-16.
341 return StringReplaceAllString(string, searchString, ToString(replaceValue));
342 }
343
344 // Step 7.
345 var searchLength = searchString.length;
346
347 // Step 8.
348 var advanceBy = std_Math_max(1, searchLength);
349
350 // Step 9 (not needed in this implementation).
351
352 // Step 12.
353 var endOfLastMatch = 0;
354
355 // Step 13.
356 var result = "";
357
358 // Steps 10-11, 14.
359 var position = 0;
360 while (true) {
361 // Steps 10-11.
362 //
363 // StringIndexOf doesn't clamp the |position| argument to the input
364 // string length, i.e. |StringIndexOf("abc", "", 4)| returns -1,
365 // whereas |"abc".indexOf("", 4)| returns 3. That means we need to
366 // exit the loop when |nextPosition| is smaller than |position| and
367 // not just when |nextPosition| is -1.
368 var nextPosition = callFunction(
369 std_String_indexOf,
370 string,
371 searchString,
372 position
373 );
374 if (nextPosition < position) {
375 break;
376 }
377 position = nextPosition;
378
379 // Step 14.a.
380 var replacement = ToString(
381 callContentFunction(
382 replaceValue,
383 undefined,
384 searchString,
385 position,
386 string
387 )
388 );
389
390 // Step 14.b (not applicable).
391
392 // Step 14.c.
393 var stringSlice = Substring(
394 string,
395 endOfLastMatch,
396 position - endOfLastMatch
397 );
398
399 // Step 14.d.
400 result += stringSlice + replacement;
401
402 // Step 14.e.
403 endOfLastMatch = position + searchLength;
404
405 // Step 11.b.
406 position += advanceBy;
407 }
408
409 // Step 15.
410 if (endOfLastMatch < string.length) {
411 // Step 15.a.
412 result += Substring(string, endOfLastMatch, string.length - endOfLastMatch);
413 }
414
415 // Step 16.
416 return result;
417 }
418
419 // ES 2016 draft Mar 25, 2016 21.1.3.15.
420 function String_search(regexp) {
421 // Step 1.
422 if (IsNullOrUndefined(this)) {
423 ThrowIncompatibleMethod("search", this);
424 }
425
426 // Step 2.
427 var isPatternString = typeof regexp === "string";
428 if (
429 !(isPatternString && CanOptimizeStringProtoSymbolLookup()) &&
430 !IsNullOrUndefined(regexp)
431 ) {
432 // Fast path for regular expressions with the original
433 // RegExp.prototype[@@search] function.
434 if (IsObject(regexp) && IsOptimizableRegExpObject(regexp)) {
435 return callFunction(RegExpSearch, regexp, this);
436 }
437
438 // Step 2.a.
439 var searcher = GetMethod(regexp, GetBuiltinSymbol("search"));
440
441 // Step 2.b.
442 if (searcher !== undefined) {
443 if (!IsObject(regexp)) {
444 RegExpSymbolProtocolOnPrimitiveCounter();
445 }
446 return callContentFunction(searcher, regexp, this);
447 }
448 }
449
450 // Step 3.
451 var string = ToString(this);
452
453 if (isPatternString && IsRegExpPrototypeOptimizable()) {
454 var flatResult = FlatStringSearch(string, regexp);
455 if (flatResult !== -2) {
456 return flatResult;
457 }
458 }
459
460 // Step 4.
461 var rx = RegExpCreate(regexp);
462
463 // Step 5.
464 return callContentFunction(
465 GetMethod(rx, GetBuiltinSymbol("search")),
466 rx,
467 string
468 );
469 }
470
471 // ES 2016 draft Mar 25, 2016 21.1.3.17.
472 function String_split(separator, limit) {
473 // Step 1.
474 if (IsNullOrUndefined(this)) {
475 ThrowIncompatibleMethod("split", this);
476 }
477
478 // Optimized path for string.split(string), especially when both strings
479 // are constants. Following sequence of if's cannot be put together in
480 // order that IonMonkey sees the constant if present (bug 1246141).
481 if (typeof this === "string") {
482 if (CanOptimizeStringProtoSymbolLookup()) {
483 if (typeof separator === "string") {
484 if (limit === undefined) {
485 // inlineConstantStringSplitString needs both arguments to
486 // be MConstant, so pass them directly.
487 return StringSplitString(this, separator);
488 }
489 }
490 }
491 }
492
493 // Step 2.
494 if (
495 !(typeof separator === "string" && CanOptimizeStringProtoSymbolLookup()) &&
496 !IsNullOrUndefined(separator)
497 ) {
498 // Fast path for regular expressions with the original
499 // RegExp.prototype[@@split] function.
500 if (IsObject(separator) && IsOptimizableRegExpObject(separator)) {
501 return callFunction(RegExpSplit, separator, this, limit);
502 }
503
504 // Step 2.a.
505 var splitter = GetMethod(separator, GetBuiltinSymbol("split"));
506
507 // Step 2.b.
508 if (splitter !== undefined) {
509 if (!IsObject(separator)) {
510 RegExpSymbolProtocolOnPrimitiveCounter();
511 }
512 return callContentFunction(splitter, separator, this, limit);
513 }
514 }
515
516 // Step 3.
517 var S = ToString(this);
518
519 // Step 6.
520 var R;
521 if (limit !== undefined) {
522 var lim = limit >>> 0;
523
524 // Step 9.
525 R = ToString(separator);
526
527 // Step 10.
528 if (lim === 0) {
529 return [];
530 }
531
532 // Step 11.
533 if (separator === undefined) {
534 return [S];
535 }
536
537 // Steps 4, 8, 12-18.
538 return StringSplitStringLimit(S, R, lim);
539 }
540
541 // Step 9.
542 R = ToString(separator);
543
544 // Step 11.
545 if (separator === undefined) {
546 return [S];
547 }
548
549 // Optimized path.
550 // Steps 4, 8, 12-18.
551 return StringSplitString(S, R);
552 }
553
554 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
555 // 21.1.3.22 String.prototype.substring ( start, end )
556 function String_substring(start, end) {
557 // Step 1.
558 if (IsNullOrUndefined(this)) {
559 ThrowIncompatibleMethod("substring", this);
560 }
561
562 // Step 2.
563 var str = ToString(this);
564
565 // Step 3.
566 var len = str.length;
567
568 // Step 4.
569 var intStart = ToInteger(start);
570
571 // Step 5.
572 var intEnd = end === undefined ? len : ToInteger(end);
573
574 // Step 6.
575 var finalStart = std_Math_min(std_Math_max(intStart, 0), len);
576
577 // Step 7.
578 var finalEnd = std_Math_min(std_Math_max(intEnd, 0), len);
579
580 // Step 8.
581 var from = std_Math_min(finalStart, finalEnd);
582
583 // Step 9.
584 var to = std_Math_max(finalStart, finalEnd);
585
586 // Step 10.
587 // While |from| and |to - from| are bounded to the length of |str| and this
588 // and thus definitely in the int32 range, they can still be typed as
589 // double. Eagerly truncate since SubstringKernel only accepts int32.
590 return SubstringKernel(str, from | 0, (to - from) | 0);
591 }
592 SetIsInlinableLargeFunction(String_substring);
593
594 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
595 // B.2.3.1 String.prototype.substr ( start, length )
596 function String_substr(start, length) {
597 // Steps 1.
598 if (IsNullOrUndefined(this)) {
599 ThrowIncompatibleMethod("substr", this);
600 }
601
602 // Step 2.
603 var str = ToString(this);
604
605 // Step 3.
606 var intStart = ToInteger(start);
607
608 // Steps 4-5.
609 var size = str.length;
610 // Use |size| instead of +Infinity to avoid performing calculations with
611 // doubles. (The result is the same either way.)
612 var end = length === undefined ? size : ToInteger(length);
613
614 // Step 6.
615 if (intStart < 0) {
616 intStart = std_Math_max(intStart + size, 0);
617 } else {
618 // Restrict the input range to allow better Ion optimizations.
619 intStart = std_Math_min(intStart, size);
620 }
621
622 // Step 7.
623 var resultLength = std_Math_min(std_Math_max(end, 0), size - intStart);
624
625 // Step 8.
626 assert(
627 0 <= resultLength && resultLength <= size - intStart,
628 "resultLength is a valid substring length value"
629 );
630
631 // Step 9.
632 // While |intStart| and |resultLength| are bounded to the length of |str|
633 // and thus definitely in the int32 range, they can still be typed as
634 // double. Eagerly truncate since SubstringKernel only accepts int32.
635 return SubstringKernel(str, intStart | 0, resultLength | 0);
636 }
637 SetIsInlinableLargeFunction(String_substr);
638
639 // ES2021 draft rev 12a546b92275a0e2f834017db2727bb9c6f6c8fd
640 // 21.1.3.4 String.prototype.concat ( ...args )
641 // Note: String.prototype.concat.length is 1.
642 function String_concat(arg1) {
643 // Step 1.
644 if (IsNullOrUndefined(this)) {
645 ThrowIncompatibleMethod("concat", this);
646 }
647
648 // Step 2.
649 var str = ToString(this);
650
651 // Specialize for the most common number of arguments for better inlining.
652 if (ArgumentsLength() === 0) {
653 return str;
654 }
655 if (ArgumentsLength() === 1) {
656 return str + ToString(GetArgument(0));
657 }
658 if (ArgumentsLength() === 2) {
659 return str + ToString(GetArgument(0)) + ToString(GetArgument(1));
660 }
661
662 // Step 3. (implicit)
663 // Step 4.
664 var result = str;
665
666 // Step 5.
667 for (var i = 0; i < ArgumentsLength(); i++) {
668 // Steps 5.a-b.
669 var nextString = ToString(GetArgument(i));
670 // Step 5.c.
671 result += nextString;
672 }
673
674 // Step 6.
675 return result;
676 }
677
678 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
679 // 21.1.3.19 String.prototype.slice ( start, end )
680 function String_slice(start, end) {
681 // Step 1.
682 if (IsNullOrUndefined(this)) {
683 ThrowIncompatibleMethod("slice", this);
684 }
685
686 // Step 2.
687 var str = ToString(this);
688
689 // Step 3.
690 var len = str.length;
691
692 // Step 4.
693 var intStart = ToInteger(start);
694
695 // Step 5.
696 var intEnd = end === undefined ? len : ToInteger(end);
697
698 // Step 6.
699 var from =
700 intStart < 0
701 ? std_Math_max(len + intStart, 0)
702 : std_Math_min(intStart, len);
703
704 // Step 7.
705 var to =
706 intEnd < 0 ? std_Math_max(len + intEnd, 0) : std_Math_min(intEnd, len);
707
708 // Step 8.
709 var span = std_Math_max(to - from, 0);
710
711 // Step 9.
712 // While |from| and |span| are bounded to the length of |str|
713 // and thus definitely in the int32 range, they can still be typed as
714 // double. Eagerly truncate since SubstringKernel only accepts int32.
715 return SubstringKernel(str, from | 0, span | 0);
716 }
717 SetIsInlinableLargeFunction(String_slice);
718
719 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
720 // 21.1.3.16 String.prototype.repeat ( count )
721 function String_repeat(count) {
722 // Step 1.
723 if (IsNullOrUndefined(this)) {
724 ThrowIncompatibleMethod("repeat", this);
725 }
726
727 // Step 2.
728 var S = ToString(this);
729
730 // Step 3.
731 var n = ToInteger(count);
732
733 // Step 4.
734 if (n < 0) {
735 ThrowRangeError(JSMSG_NEGATIVE_REPETITION_COUNT);
736 }
737
738 // Step 5.
739 // Inverted condition to handle |Infinity * 0 = NaN| correctly.
740 if (!(n * S.length <= MAX_STRING_LENGTH)) {
741 ThrowRangeError(JSMSG_RESULTING_STRING_TOO_LARGE);
742 }
743
744 // Communicate |n|'s possible range to the compiler. We actually use
745 // MAX_STRING_LENGTH + 1 as range because that's a valid bit mask. That's
746 // fine because it's only used as optimization hint.
747 assert(
748 TO_INT32(MAX_STRING_LENGTH + 1) === MAX_STRING_LENGTH + 1,
749 "MAX_STRING_LENGTH + 1 must fit in int32"
750 );
751 assert(
752 ((MAX_STRING_LENGTH + 1) & (MAX_STRING_LENGTH + 2)) === 0,
753 "MAX_STRING_LENGTH + 1 can be used as a bitmask"
754 );
755 n = n & (MAX_STRING_LENGTH + 1);
756
757 // Steps 6-7.
758 var T = "";
759 for (;;) {
760 if (n & 1) {
761 T += S;
762 }
763 n >>= 1;
764 if (n) {
765 S += S;
766 } else {
767 break;
768 }
769 }
770 return T;
771 }
772
773 // ES6 draft specification, section 21.1.3.27, version 2013-09-27.
774 function String_iterator() {
775 // Step 1.
776 if (IsNullOrUndefined(this)) {
777 ThrowTypeError(
778 JSMSG_INCOMPATIBLE_PROTO2,
779 "String",
780 "Symbol.iterator",
781 ToString(this)
782 );
783 }
784
785 // Step 2.
786 var S = ToString(this);
787
788 // Step 3.
789 var iterator = NewStringIterator();
790 UnsafeSetReservedSlot(iterator, ITERATOR_SLOT_TARGET, S);
791 UnsafeSetReservedSlot(iterator, ITERATOR_SLOT_NEXT_INDEX, 0);
792 return iterator;
793 }
794
795 function StringIteratorNext() {
796 var obj = this;
797 if (!IsObject(obj) || (obj = GuardToStringIterator(obj)) === null) {
798 return callFunction(
799 CallStringIteratorMethodIfWrapped,
800 this,
801 "StringIteratorNext"
802 );
803 }
804
805 var S = UnsafeGetStringFromReservedSlot(obj, ITERATOR_SLOT_TARGET);
806 // We know that JSString::MAX_LENGTH <= INT32_MAX (and assert this in
807 // SelfHostring.cpp) so our current index can never be anything other than
808 // an Int32Value.
809 var index = UnsafeGetInt32FromReservedSlot(obj, ITERATOR_SLOT_NEXT_INDEX);
810 var size = S.length;
811 var result = { value: undefined, done: false };
812
813 if (index >= size) {
814 result.done = true;
815 return result;
816 }
817
818 var codePoint = callFunction(std_String_codePointAt, S, index);
819 var charCount = 1 + (codePoint > 0xffff);
820
821 UnsafeSetReservedSlot(obj, ITERATOR_SLOT_NEXT_INDEX, index + charCount);
822
823 result.value = callFunction(std_String_fromCodePoint, null, codePoint);
824
825 return result;
826 }
827 SetIsInlinableLargeFunction(StringIteratorNext);
828
829 #if JS_HAS_INTL_API
830 var collatorCache = new_Record();
831
832 /**
833 * Compare this String against that String, using the locale and collation
834 * options provided.
835 *
836 * Spec: ECMAScript Internationalization API Specification, 13.1.1.
837 */
838 function String_localeCompare(that) {
839 // Step 1.
840 if (IsNullOrUndefined(this)) {
841 ThrowIncompatibleMethod("localeCompare", this);
842 }
843
844 // Steps 2-3.
845 var S = ToString(this);
846 var That = ToString(that);
847
848 // Steps 4-5.
849 var locales = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
850 var options = ArgumentsLength() > 2 ? GetArgument(2) : undefined;
851
852 // Step 6.
853 var collator;
854 if (locales === undefined && options === undefined) {
855 // This cache only optimizes for the old ES5 localeCompare without
856 // locales and options.
857 if (!intl_IsRuntimeDefaultLocale(collatorCache.runtimeDefaultLocale)) {
858 collatorCache.collator = intl_Collator(locales, options);
859 collatorCache.runtimeDefaultLocale = intl_RuntimeDefaultLocale();
860 }
861 collator = collatorCache.collator;
862 } else {
863 collator = intl_Collator(locales, options);
864 }
865
866 // Step 7.
867 return intl_CompareStrings(collator, S, That);
868 }
869
870 /**
871 * 13.1.2 String.prototype.toLocaleLowerCase ( [ locales ] )
872 *
873 * ES2017 Intl draft rev 94045d234762ad107a3d09bb6f7381a65f1a2f9b
874 */
875 function String_toLocaleLowerCase() {
876 // Step 1.
877 if (IsNullOrUndefined(this)) {
878 ThrowIncompatibleMethod("toLocaleLowerCase", this);
879 }
880
881 // Step 2.
882 var string = ToString(this);
883
884 // Handle the common cases (no locales argument or a single string
885 // argument) first.
886 var locales = ArgumentsLength() ? GetArgument(0) : undefined;
887 var requestedLocale;
888 if (locales === undefined) {
889 // Steps 3, 6.
890 requestedLocale = undefined;
891 } else if (typeof locales === "string") {
892 // Steps 3, 5.
893 requestedLocale = intl_ValidateAndCanonicalizeLanguageTag(locales, false);
894 } else {
895 // Step 3.
896 var requestedLocales = CanonicalizeLocaleList(locales);
897
898 // Steps 4-6.
899 requestedLocale = requestedLocales.length ? requestedLocales[0] : undefined;
900 }
901
902 // Trivial case: When the input is empty, directly return the empty string.
903 if (string.length === 0) {
904 return "";
905 }
906
907 if (requestedLocale === undefined) {
908 requestedLocale = DefaultLocale();
909 }
910
911 // Steps 7-16.
912 return intl_toLocaleLowerCase(string, requestedLocale);
913 }
914
915 /**
916 * 13.1.3 String.prototype.toLocaleUpperCase ( [ locales ] )
917 *
918 * ES2017 Intl draft rev 94045d234762ad107a3d09bb6f7381a65f1a2f9b
919 */
920 function String_toLocaleUpperCase() {
921 // Step 1.
922 if (IsNullOrUndefined(this)) {
923 ThrowIncompatibleMethod("toLocaleUpperCase", this);
924 }
925
926 // Step 2.
927 var string = ToString(this);
928
929 // Handle the common cases (no locales argument or a single string
930 // argument) first.
931 var locales = ArgumentsLength() ? GetArgument(0) : undefined;
932 var requestedLocale;
933 if (locales === undefined) {
934 // Steps 3, 6.
935 requestedLocale = undefined;
936 } else if (typeof locales === "string") {
937 // Steps 3, 5.
938 requestedLocale = intl_ValidateAndCanonicalizeLanguageTag(locales, false);
939 } else {
940 // Step 3.
941 var requestedLocales = CanonicalizeLocaleList(locales);
942
943 // Steps 4-6.
944 requestedLocale = requestedLocales.length ? requestedLocales[0] : undefined;
945 }
946
947 // Trivial case: When the input is empty, directly return the empty string.
948 if (string.length === 0) {
949 return "";
950 }
951
952 if (requestedLocale === undefined) {
953 requestedLocale = DefaultLocale();
954 }
955
956 // Steps 7-16.
957 return intl_toLocaleUpperCase(string, requestedLocale);
958 }
959 #endif // JS_HAS_INTL_API
960
961 // ES2018 draft rev 8fadde42cf6a9879b4ab0cb6142b31c4ee501667
962 // 21.1.2.4 String.raw ( template, ...substitutions )
963 function String_static_raw(callSite /*, ...substitutions*/) {
964 // Steps 1-2 (not applicable).
965
966 // Step 3.
967 var cooked = ToObject(callSite);
968
969 // Step 4.
970 var raw = ToObject(cooked.raw);
971
972 // Step 5.
973 var literalSegments = ToLength(raw.length);
974
975 // Step 6.
976 if (literalSegments === 0) {
977 return "";
978 }
979
980 // Special case for |String.raw `<literal>`| callers to avoid falling into
981 // the loop code below.
982 if (literalSegments === 1) {
983 return ToString(raw[0]);
984 }
985
986 // Steps 7-9 were reordered to use ArgumentsLength/GetArgument instead of a
987 // rest parameter, because the former is currently more optimized.
988 //
989 // String.raw intersperses the substitution elements between the literal
990 // segments, i.e. a substitution is added iff there are still pending
991 // literal segments. Furthermore by moving the access to |raw[0]| outside
992 // of the loop, we can use |nextIndex| to index into both, the |raw| array
993 // and the arguments.
994
995 // Steps 7 (implicit) and 9.a-c.
996 var resultString = ToString(raw[0]);
997
998 // Steps 8-9, 9.d, and 9.i.
999 for (var nextIndex = 1; nextIndex < literalSegments; nextIndex++) {
1000 // Steps 9.e-h.
1001 if (nextIndex < ArgumentsLength()) {
1002 resultString += ToString(GetArgument(nextIndex));
1003 }
1004
1005 // Steps 9.a-c.
1006 resultString += ToString(raw[nextIndex]);
1007 }
1008
1009 // Step 9.d.i.
1010 return resultString;
1011 }
1012
1013 // ES6 draft 2014-04-27 B.2.3.3
1014 function String_big() {
1015 if (IsNullOrUndefined(this)) {
1016 ThrowIncompatibleMethod("big", this);
1017 }
1018 return "<big>" + ToString(this) + "</big>";
1019 }
1020
1021 // ES6 draft 2014-04-27 B.2.3.4
1022 function String_blink() {
1023 if (IsNullOrUndefined(this)) {
1024 ThrowIncompatibleMethod("blink", this);
1025 }
1026 return "<blink>" + ToString(this) + "</blink>";
1027 }
1028
1029 // ES6 draft 2014-04-27 B.2.3.5
1030 function String_bold() {
1031 if (IsNullOrUndefined(this)) {
1032 ThrowIncompatibleMethod("bold", this);
1033 }
1034 return "<b>" + ToString(this) + "</b>";
1035 }
1036
1037 // ES6 draft 2014-04-27 B.2.3.6
1038 function String_fixed() {
1039 if (IsNullOrUndefined(this)) {
1040 ThrowIncompatibleMethod("fixed", this);
1041 }
1042 return "<tt>" + ToString(this) + "</tt>";
1043 }
1044
1045 // ES6 draft 2014-04-27 B.2.3.9
1046 function String_italics() {
1047 if (IsNullOrUndefined(this)) {
1048 ThrowIncompatibleMethod("italics", this);
1049 }
1050 return "<i>" + ToString(this) + "</i>";
1051 }
1052
1053 // ES6 draft 2014-04-27 B.2.3.11
1054 function String_small() {
1055 if (IsNullOrUndefined(this)) {
1056 ThrowIncompatibleMethod("small", this);
1057 }
1058 return "<small>" + ToString(this) + "</small>";
1059 }
1060
1061 // ES6 draft 2014-04-27 B.2.3.12
1062 function String_strike() {
1063 if (IsNullOrUndefined(this)) {
1064 ThrowIncompatibleMethod("strike", this);
1065 }
1066 return "<strike>" + ToString(this) + "</strike>";
1067 }
1068
1069 // ES6 draft 2014-04-27 B.2.3.13
1070 function String_sub() {
1071 if (IsNullOrUndefined(this)) {
1072 ThrowIncompatibleMethod("sub", this);
1073 }
1074 return "<sub>" + ToString(this) + "</sub>";
1075 }
1076
1077 // ES6 draft 2014-04-27 B.2.3.14
1078 function String_sup() {
1079 if (IsNullOrUndefined(this)) {
1080 ThrowIncompatibleMethod("sup", this);
1081 }
1082 return "<sup>" + ToString(this) + "</sup>";
1083 }
1084
1085 function EscapeAttributeValue(v) {
1086 var inputStr = ToString(v);
1087 return StringReplaceAllString(inputStr, '"', "&quot;");
1088 }
1089
1090 // ES6 draft 2014-04-27 B.2.3.2
1091 function String_anchor(name) {
1092 if (IsNullOrUndefined(this)) {
1093 ThrowIncompatibleMethod("anchor", this);
1094 }
1095 var S = ToString(this);
1096 return '<a name="' + EscapeAttributeValue(name) + '">' + S + "</a>";
1097 }
1098
1099 // ES6 draft 2014-04-27 B.2.3.7
1100 function String_fontcolor(color) {
1101 if (IsNullOrUndefined(this)) {
1102 ThrowIncompatibleMethod("fontcolor", this);
1103 }
1104 var S = ToString(this);
1105 return '<font color="' + EscapeAttributeValue(color) + '">' + S + "</font>";
1106 }
1107
1108 // ES6 draft 2014-04-27 B.2.3.8
1109 function String_fontsize(size) {
1110 if (IsNullOrUndefined(this)) {
1111 ThrowIncompatibleMethod("fontsize", this);
1112 }
1113 var S = ToString(this);
1114 return '<font size="' + EscapeAttributeValue(size) + '">' + S + "</font>";
1115 }
1116
1117 // ES6 draft 2014-04-27 B.2.3.10
1118 function String_link(url) {
1119 if (IsNullOrUndefined(this)) {
1120 ThrowIncompatibleMethod("link", this);
1121 }
1122 var S = ToString(this);
1123 return '<a href="' + EscapeAttributeValue(url) + '">' + S + "</a>";
1124 }