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

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

Issue 120683002: Make `String.prototype.{starts,ends}With` throw when passing a regular expression (Closed) Base URL: git@github.com:v8/v8.git@master
Patch Set: Use the new `CHECK_OBJECT_COERCIBLE` macro Created 6 years, 10 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
« no previous file with comments | « no previous file | src/messages.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
(...skipping 15 matching lines...) Expand all
27 27
28 'use strict'; 28 'use strict';
29 29
30 // This file relies on the fact that the following declaration has been made 30 // This file relies on the fact that the following declaration has been made
31 // in runtime.js: 31 // in runtime.js:
32 // var $String = global.String; 32 // var $String = global.String;
33 // var $Array = global.Array; 33 // var $Array = global.Array;
34 34
35 // ------------------------------------------------------------------- 35 // -------------------------------------------------------------------
36 36
37 // ES6 draft 07-15-13, section 15.5.3.21 37 // ES6 draft 01-20-14, section 21.1.3.13
38 function StringRepeat(count) { 38 function StringRepeat(count) {
39 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 39 CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat");
40 throw MakeTypeError("called_on_null_or_undefined",
41 ["String.prototype.repeat"]);
42 }
43 40
44 var s = TO_STRING_INLINE(this); 41 var s = TO_STRING_INLINE(this);
45 var n = ToInteger(count); 42 var n = ToInteger(count);
46 if (n < 0 || !NUMBER_IS_FINITE(n)) { 43 if (n < 0 || !NUMBER_IS_FINITE(n)) {
47 throw MakeRangeError("invalid_count_value", []); 44 throw MakeRangeError("invalid_count_value", []);
48 } 45 }
49 46
50 var elements = new InternalArray(n); 47 var elements = new InternalArray(n);
51 for (var i = 0; i < n; i++) { 48 for (var i = 0; i < n; i++) {
52 elements[i] = s; 49 elements[i] = s;
53 } 50 }
54 51
55 return %StringBuilderConcat(elements, n, ""); 52 return %StringBuilderConcat(elements, n, "");
56 } 53 }
57 54
58 55
59 // ES6 draft 07-15-13, section 15.5.3.22 56 // ES6 draft 01-20-14, section 21.1.3.18
60 function StringStartsWith(searchString /* position */) { // length == 1 57 function StringStartsWith(searchString /* position */) { // length == 1
61 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 58 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith");
62 throw MakeTypeError("called_on_null_or_undefined", 59
60 var s = TO_STRING_INLINE(this);
61
62 if (IS_REGEXP(searchString)) {
63 throw MakeTypeError("first_argument_not_regexp",
63 ["String.prototype.startsWith"]); 64 ["String.prototype.startsWith"]);
64 } 65 }
65 66
66 var s = TO_STRING_INLINE(this);
67 var ss = TO_STRING_INLINE(searchString); 67 var ss = TO_STRING_INLINE(searchString);
68 var pos = 0; 68 var pos = 0;
69 if (%_ArgumentsLength() > 1) { 69 if (%_ArgumentsLength() > 1) {
70 pos = %_Arguments(1); // position 70 pos = %_Arguments(1); // position
71 pos = ToInteger(pos); 71 pos = ToInteger(pos);
72 } 72 }
73 73
74 var s_len = s.length; 74 var s_len = s.length;
75 var start = MathMin(MathMax(pos, 0), s_len); 75 var start = MathMin(MathMax(pos, 0), s_len);
76 var ss_len = ss.length; 76 var ss_len = ss.length;
77 if (ss_len + start > s_len) { 77 if (ss_len + start > s_len) {
78 return false; 78 return false;
79 } 79 }
80 80
81 return %StringIndexOf(s, ss, start) === start; 81 return %StringIndexOf(s, ss, start) === start;
82 } 82 }
83 83
84 84
85 // ES6 draft 07-15-13, section 15.5.3.23 85 // ES6 draft 01-20-14, section 21.1.3.7
86 function StringEndsWith(searchString /* position */) { // length == 1 86 function StringEndsWith(searchString /* position */) { // length == 1
87 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 87 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith");
88 throw MakeTypeError("called_on_null_or_undefined", 88
89 var s = TO_STRING_INLINE(this);
90
91 if (IS_REGEXP(searchString)) {
92 throw MakeTypeError("first_argument_not_regexp",
89 ["String.prototype.endsWith"]); 93 ["String.prototype.endsWith"]);
90 } 94 }
91 95
92 var s = TO_STRING_INLINE(this);
93 var ss = TO_STRING_INLINE(searchString); 96 var ss = TO_STRING_INLINE(searchString);
94 var s_len = s.length; 97 var s_len = s.length;
95 var pos = s_len; 98 var pos = s_len;
96 if (%_ArgumentsLength() > 1) { 99 if (%_ArgumentsLength() > 1) {
97 var arg = %_Arguments(1); // position 100 var arg = %_Arguments(1); // position
98 if (!IS_UNDEFINED(arg)) { 101 if (!IS_UNDEFINED(arg)) {
99 pos = ToInteger(arg); 102 pos = ToInteger(arg);
100 } 103 }
101 } 104 }
102 105
103 var end = MathMin(MathMax(pos, 0), s_len); 106 var end = MathMin(MathMax(pos, 0), s_len);
104 var ss_len = ss.length; 107 var ss_len = ss.length;
105 var start = end - ss_len; 108 var start = end - ss_len;
106 if (start < 0) { 109 if (start < 0) {
107 return false; 110 return false;
108 } 111 }
109 112
110 return %StringLastIndexOf(s, ss, start) === start; 113 return %StringLastIndexOf(s, ss, start) === start;
111 } 114 }
112 115
113 116
114 // ES6 draft 07-15-13, section 15.5.3.24 117 // ES6 draft 01-20-14, section 21.1.3.6
115 function StringContains(searchString /* position */) { // length == 1 118 function StringContains(searchString /* position */) { // length == 1
116 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 119 CHECK_OBJECT_COERCIBLE(this, "String.prototype.contains");
117 throw MakeTypeError("called_on_null_or_undefined",
118 ["String.prototype.contains"]);
119 }
120 120
121 var s = TO_STRING_INLINE(this); 121 var s = TO_STRING_INLINE(this);
122 var ss = TO_STRING_INLINE(searchString); 122 var ss = TO_STRING_INLINE(searchString);
123 var pos = 0; 123 var pos = 0;
124 if (%_ArgumentsLength() > 1) { 124 if (%_ArgumentsLength() > 1) {
125 pos = %_Arguments(1); // position 125 pos = %_Arguments(1); // position
126 pos = ToInteger(pos); 126 pos = ToInteger(pos);
127 } 127 }
128 128
129 var s_len = s.length; 129 var s_len = s.length;
(...skipping 15 matching lines...) Expand all
145 // Set up the non-enumerable functions on the String prototype object. 145 // Set up the non-enumerable functions on the String prototype object.
146 InstallFunctions($String.prototype, DONT_ENUM, $Array( 146 InstallFunctions($String.prototype, DONT_ENUM, $Array(
147 "repeat", StringRepeat, 147 "repeat", StringRepeat,
148 "startsWith", StringStartsWith, 148 "startsWith", StringStartsWith,
149 "endsWith", StringEndsWith, 149 "endsWith", StringEndsWith,
150 "contains", StringContains 150 "contains", StringContains
151 )); 151 ));
152 } 152 }
153 153
154 ExtendStringPrototype(); 154 ExtendStringPrototype();
OLDNEW
« no previous file with comments | « no previous file | src/messages.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698