1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // This file relies on the fact that the following declaration has been made
32 // var $String = global.String;
33 // var $Array = global.Array;
35 // -------------------------------------------------------------------
37 // ES6 draft 01-20-14, section 21.1.3.13
38 function StringRepeat(count) {
39 CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat");
41 var s = TO_STRING_INLINE(this);
42 var n = ToInteger(count);
43 if (n < 0 || !NUMBER_IS_FINITE(n)) {
44 throw MakeRangeError("invalid_count_value", []);
47 var elements = new InternalArray(n);
48 for (var i = 0; i < n; i++) {
52 return %StringBuilderConcat(elements, n, "");
56 // ES6 draft 01-20-14, section 21.1.3.18
57 function StringStartsWith(searchString /* position */) { // length == 1
58 CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith");
60 var s = TO_STRING_INLINE(this);
62 if (IS_REGEXP(searchString)) {
63 throw MakeTypeError("first_argument_not_regexp",
64 ["String.prototype.startsWith"]);
67 var ss = TO_STRING_INLINE(searchString);
69 if (%_ArgumentsLength() > 1) {
70 pos = %_Arguments(1); // position
75 var start = MathMin(MathMax(pos, 0), s_len);
76 var ss_len = ss.length;
77 if (ss_len + start > s_len) {
81 return %StringIndexOf(s, ss, start) === start;
85 // ES6 draft 01-20-14, section 21.1.3.7
86 function StringEndsWith(searchString /* position */) { // length == 1
87 CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith");
89 var s = TO_STRING_INLINE(this);
91 if (IS_REGEXP(searchString)) {
92 throw MakeTypeError("first_argument_not_regexp",
93 ["String.prototype.endsWith"]);
96 var ss = TO_STRING_INLINE(searchString);
99 if (%_ArgumentsLength() > 1) {
100 var arg = %_Arguments(1); // position
101 if (!IS_UNDEFINED(arg)) {
102 pos = ToInteger(arg);
106 var end = MathMin(MathMax(pos, 0), s_len);
107 var ss_len = ss.length;
108 var start = end - ss_len;
113 return %StringLastIndexOf(s, ss, start) === start;
117 // ES6 draft 01-20-14, section 21.1.3.6
118 function StringContains(searchString /* position */) { // length == 1
119 CHECK_OBJECT_COERCIBLE(this, "String.prototype.contains");
121 var s = TO_STRING_INLINE(this);
122 var ss = TO_STRING_INLINE(searchString);
124 if (%_ArgumentsLength() > 1) {
125 pos = %_Arguments(1); // position
126 pos = ToInteger(pos);
129 var s_len = s.length;
130 var start = MathMin(MathMax(pos, 0), s_len);
131 var ss_len = ss.length;
132 if (ss_len + start > s_len) {
136 return %StringIndexOf(s, ss, start) !== -1;
140 // -------------------------------------------------------------------
142 function ExtendStringPrototype() {
143 %CheckIsBootstrapping();
145 // Set up the non-enumerable functions on the String prototype object.
146 InstallFunctions($String.prototype, DONT_ENUM, $Array(
147 "repeat", StringRepeat,
148 "startsWith", StringStartsWith,
149 "endsWith", StringEndsWith,
150 "contains", StringContains
154 ExtendStringPrototype();