Index: src/string.js |
diff --git a/src/string.js b/src/string.js |
index cb82c166346fc7ba5491a1e9e5d28e0e9b5a5108..96d68e249fe5749fa8ef2500e5694555c1122340 100644 |
--- a/src/string.js |
+++ b/src/string.js |
@@ -882,6 +882,43 @@ function StringFromCharCode(code) { |
} |
+// ES6 draft 07-15-13, section 15.5.3.21 |
+function StringRepeat(count) { |
+ if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
+ throw MakeTypeError("called_on_null_or_undefined", |
+ ["String.prototype.repeat"]); |
+ } |
+ |
Sven Panne
2013/07/29 13:27:53
We have to call TO_STRING_INLINE here, not later,
|
+ if (IS_NULL_OR_UNDEFINED(count) && !IS_UNDETECTABLE(count)) { |
Sven Panne
2013/07/29 13:27:53
Why test this? What should really happen at that p
|
+ throw MakeTypeError("called_on_null_or_undefined", |
+ ["String.prototype.repeat"]); |
+ } |
+ |
+ var s = TO_STRING_INLINE(this); |
+ if (s.length === 0) { |
Sven Panne
2013/07/29 13:27:53
This optimization is not allowed by the spec, at l
|
+ return s; |
+ } |
+ |
+ var n = ToNumber(count); |
+ if (NUMBER_IS_NAN(n) || n < 0 || !NUMBER_IS_FINITE(n)) { |
+ throw MakeRangeError("invalid_count_value", []); |
+ } |
+ |
+ n = TO_INTEGER(n); |
+ if (n === 0) { |
Sven Panne
2013/07/29 13:27:53
I am not sure if a zero count is a common case, we
|
+ return ""; |
+ } |
+ |
+ var elements = new InternalArray(n); |
+ var i = 0; |
Sven Panne
2013/07/29 13:27:53
Style nit: Use a for loop for readability.
|
+ while (i < n) { |
+ elements[i++] = s; |
+ } |
+ |
+ return %StringBuilderConcat(elements, n, ""); |
+} |
+ |
+ |
// Helper function for very basic XSS protection. |
function HtmlEscape(str) { |
return TO_STRING_INLINE(str).replace(/</g, "<") |
@@ -1010,7 +1047,8 @@ function SetUpString() { |
"small", StringSmall, |
"strike", StringStrike, |
"sub", StringSub, |
- "sup", StringSup |
+ "sup", StringSup, |
+ "repeat", StringRepeat |
)); |
} |