Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: src/js/string.js

Issue 2222893002: Move family of MakeError functions to C++ (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix in prologue.js Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, utils) { 5 (function(global, utils) {
6 6
7 %CheckIsBootstrapping(); 7 %CheckIsBootstrapping();
8 8
9 // ------------------------------------------------------------------- 9 // -------------------------------------------------------------------
10 // Imports 10 // Imports
11 11
12 var ArrayIndexOf; 12 var ArrayIndexOf;
13 var ArrayJoin; 13 var ArrayJoin;
14 var GlobalRegExp = global.RegExp; 14 var GlobalRegExp = global.RegExp;
15 var GlobalString = global.String; 15 var GlobalString = global.String;
16 var IsRegExp; 16 var IsRegExp;
17 var MakeRangeError;
18 var MakeTypeError;
19 var MaxSimple; 17 var MaxSimple;
20 var MinSimple; 18 var MinSimple;
21 var RegExpInitialize; 19 var RegExpInitialize;
22 var matchSymbol = utils.ImportNow("match_symbol"); 20 var matchSymbol = utils.ImportNow("match_symbol");
23 var replaceSymbol = utils.ImportNow("replace_symbol"); 21 var replaceSymbol = utils.ImportNow("replace_symbol");
24 var searchSymbol = utils.ImportNow("search_symbol"); 22 var searchSymbol = utils.ImportNow("search_symbol");
25 var splitSymbol = utils.ImportNow("split_symbol"); 23 var splitSymbol = utils.ImportNow("split_symbol");
26 24
27 utils.Import(function(from) { 25 utils.Import(function(from) {
28 ArrayIndexOf = from.ArrayIndexOf; 26 ArrayIndexOf = from.ArrayIndexOf;
29 ArrayJoin = from.ArrayJoin; 27 ArrayJoin = from.ArrayJoin;
30 IsRegExp = from.IsRegExp; 28 IsRegExp = from.IsRegExp;
31 MakeRangeError = from.MakeRangeError;
32 MakeTypeError = from.MakeTypeError;
33 MaxSimple = from.MaxSimple; 29 MaxSimple = from.MaxSimple;
34 MinSimple = from.MinSimple; 30 MinSimple = from.MinSimple;
35 RegExpInitialize = from.RegExpInitialize; 31 RegExpInitialize = from.RegExpInitialize;
36 }); 32 });
37 33
38 //------------------------------------------------------------------- 34 //-------------------------------------------------------------------
39 35
40 // ECMA-262, section 15.5.4.6 36 // ECMA-262, section 15.5.4.6
41 function StringConcat(other /* and more */) { // length == 1 37 function StringConcat(other /* and more */) { // length == 1
42 "use strict"; 38 "use strict";
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 // proper functionality. 127 // proper functionality.
132 function StringNormalize(formArg) { // length == 0 128 function StringNormalize(formArg) { // length == 0
133 CHECK_OBJECT_COERCIBLE(this, "String.prototype.normalize"); 129 CHECK_OBJECT_COERCIBLE(this, "String.prototype.normalize");
134 var s = TO_STRING(this); 130 var s = TO_STRING(this);
135 131
136 var form = IS_UNDEFINED(formArg) ? 'NFC' : TO_STRING(formArg); 132 var form = IS_UNDEFINED(formArg) ? 'NFC' : TO_STRING(formArg);
137 133
138 var NORMALIZATION_FORMS = ['NFC', 'NFD', 'NFKC', 'NFKD']; 134 var NORMALIZATION_FORMS = ['NFC', 'NFD', 'NFKC', 'NFKD'];
139 var normalizationForm = %_Call(ArrayIndexOf, NORMALIZATION_FORMS, form); 135 var normalizationForm = %_Call(ArrayIndexOf, NORMALIZATION_FORMS, form);
140 if (normalizationForm === -1) { 136 if (normalizationForm === -1) {
141 throw MakeRangeError(kNormalizationForm, 137 throw %make_range_error(kNormalizationForm,
142 %_Call(ArrayJoin, NORMALIZATION_FORMS, ', ')); 138 %_Call(ArrayJoin, NORMALIZATION_FORMS, ', '));
143 } 139 }
144 140
145 return s; 141 return s;
146 } 142 }
147 143
148 %FunctionSetLength(StringNormalize, 0); 144 %FunctionSetLength(StringNormalize, 0);
149 145
150 146
151 // This has the same size as the RegExpLastMatchInfo array, and can be used 147 // This has the same size as the RegExpLastMatchInfo array, and can be used
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 return "<sup>" + TO_STRING(this) + "</sup>"; 572 return "<sup>" + TO_STRING(this) + "</sup>";
577 } 573 }
578 574
579 // ES6, section 21.1.3.13 575 // ES6, section 21.1.3.13
580 function StringRepeat(count) { 576 function StringRepeat(count) {
581 CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat"); 577 CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat");
582 578
583 var s = TO_STRING(this); 579 var s = TO_STRING(this);
584 var n = TO_INTEGER(count); 580 var n = TO_INTEGER(count);
585 581
586 if (n < 0 || n === INFINITY) throw MakeRangeError(kInvalidCountValue); 582 if (n < 0 || n === INFINITY) throw %make_range_error(kInvalidCountValue);
587 583
588 // Early return to allow an arbitrarily-large repeat of the empty string. 584 // Early return to allow an arbitrarily-large repeat of the empty string.
589 if (s.length === 0) return ""; 585 if (s.length === 0) return "";
590 586
591 // The maximum string length is stored in a smi, so a longer repeat 587 // The maximum string length is stored in a smi, so a longer repeat
592 // must result in a range error. 588 // must result in a range error.
593 if (n > %_MaxSmi()) throw MakeRangeError(kInvalidCountValue); 589 if (n > %_MaxSmi()) throw %make_range_error(kInvalidCountValue);
594 590
595 var r = ""; 591 var r = "";
596 while (true) { 592 while (true) {
597 if (n & 1) r += s; 593 if (n & 1) r += s;
598 n >>= 1; 594 n >>= 1;
599 if (n === 0) return r; 595 if (n === 0) return r;
600 s += s; 596 s += s;
601 } 597 }
602 } 598 }
603 599
604 600
605 // ES6 draft 04-05-14, section 21.1.3.18 601 // ES6 draft 04-05-14, section 21.1.3.18
606 function StringStartsWith(searchString, position) { // length == 1 602 function StringStartsWith(searchString, position) { // length == 1
607 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith"); 603 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith");
608 604
609 var s = TO_STRING(this); 605 var s = TO_STRING(this);
610 606
611 if (IsRegExp(searchString)) { 607 if (IsRegExp(searchString)) {
612 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.startsWith"); 608 throw %make_type_error(kFirstArgumentNotRegExp, "String.prototype.startsWith ");
613 } 609 }
614 610
615 var ss = TO_STRING(searchString); 611 var ss = TO_STRING(searchString);
616 var pos = TO_INTEGER(position); 612 var pos = TO_INTEGER(position);
617 613
618 var s_len = s.length; 614 var s_len = s.length;
619 var start = MinSimple(MaxSimple(pos, 0), s_len); 615 var start = MinSimple(MaxSimple(pos, 0), s_len);
620 var ss_len = ss.length; 616 var ss_len = ss.length;
621 if (ss_len + start > s_len) { 617 if (ss_len + start > s_len) {
622 return false; 618 return false;
623 } 619 }
624 620
625 return %_SubString(s, start, start + ss_len) === ss; 621 return %_SubString(s, start, start + ss_len) === ss;
626 } 622 }
627 623
628 %FunctionSetLength(StringStartsWith, 1); 624 %FunctionSetLength(StringStartsWith, 1);
629 625
630 626
631 // ES6 draft 04-05-14, section 21.1.3.7 627 // ES6 draft 04-05-14, section 21.1.3.7
632 function StringEndsWith(searchString, position) { // length == 1 628 function StringEndsWith(searchString, position) { // length == 1
633 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith"); 629 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith");
634 630
635 var s = TO_STRING(this); 631 var s = TO_STRING(this);
636 632
637 if (IsRegExp(searchString)) { 633 if (IsRegExp(searchString)) {
638 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.endsWith"); 634 throw %make_type_error(kFirstArgumentNotRegExp, "String.prototype.endsWith") ;
639 } 635 }
640 636
641 var ss = TO_STRING(searchString); 637 var ss = TO_STRING(searchString);
642 var s_len = s.length; 638 var s_len = s.length;
643 var pos = !IS_UNDEFINED(position) ? TO_INTEGER(position) : s_len 639 var pos = !IS_UNDEFINED(position) ? TO_INTEGER(position) : s_len
644 640
645 var end = MinSimple(MaxSimple(pos, 0), s_len); 641 var end = MinSimple(MaxSimple(pos, 0), s_len);
646 var ss_len = ss.length; 642 var ss_len = ss.length;
647 var start = end - ss_len; 643 var start = end - ss_len;
648 if (start < 0) { 644 if (start < 0) {
649 return false; 645 return false;
650 } 646 }
651 647
652 return %_SubString(s, start, start + ss_len) === ss; 648 return %_SubString(s, start, start + ss_len) === ss;
653 } 649 }
654 650
655 %FunctionSetLength(StringEndsWith, 1); 651 %FunctionSetLength(StringEndsWith, 1);
656 652
657 653
658 // ES6 draft 04-05-14, section 21.1.3.6 654 // ES6 draft 04-05-14, section 21.1.3.6
659 function StringIncludes(searchString, position) { // length == 1 655 function StringIncludes(searchString, position) { // length == 1
660 CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes"); 656 CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes");
661 657
662 var string = TO_STRING(this); 658 var string = TO_STRING(this);
663 659
664 if (IsRegExp(searchString)) { 660 if (IsRegExp(searchString)) {
665 throw MakeTypeError(kFirstArgumentNotRegExp, "String.prototype.includes"); 661 throw %make_type_error(kFirstArgumentNotRegExp, "String.prototype.includes") ;
666 } 662 }
667 663
668 searchString = TO_STRING(searchString); 664 searchString = TO_STRING(searchString);
669 var pos = TO_INTEGER(position); 665 var pos = TO_INTEGER(position);
670 666
671 var stringLength = string.length; 667 var stringLength = string.length;
672 if (pos < 0) pos = 0; 668 if (pos < 0) pos = 0;
673 if (pos > stringLength) pos = stringLength; 669 if (pos > stringLength) pos = stringLength;
674 var searchStringLength = searchString.length; 670 var searchStringLength = searchString.length;
675 671
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
784 to.StringLastIndexOf = StringLastIndexOf; 780 to.StringLastIndexOf = StringLastIndexOf;
785 to.StringMatch = StringMatchJS; 781 to.StringMatch = StringMatchJS;
786 to.StringReplace = StringReplace; 782 to.StringReplace = StringReplace;
787 to.StringSlice = StringSlice; 783 to.StringSlice = StringSlice;
788 to.StringSplit = StringSplitJS; 784 to.StringSplit = StringSplitJS;
789 to.StringSubstr = StringSubstr; 785 to.StringSubstr = StringSubstr;
790 to.StringSubstring = StringSubstring; 786 to.StringSubstring = StringSubstring;
791 }); 787 });
792 788
793 }) 789 })
OLDNEW
« src/bootstrapper.cc ('K') | « src/js/spread.js ('k') | src/js/string-iterator.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698