Reviewers: arv, Igor Sheludko, Yang,

Description:
Make `String.prototype.contains` throw when passing a regular expression

Contributed by Mathias Bynens <[email protected]>.

TEST=mjsunit/harmony
BUG=v8:3261
LOG=Y
[email protected], [email protected], [email protected]

Please review this at https://codereview.chromium.org/227113005/

SVN Base: [email protected]:v8/v8.git@master

Affected files (+27, -6 lines):
  M src/harmony-string.js
  M test/mjsunit/harmony/string-contains.js


Index: src/harmony-string.js
diff --git a/src/harmony-string.js b/src/harmony-string.js
index cc3c5cf93c2a1ca45d269b0782e712fd2ef4fedb..e1da24caa39b8edfb4fb71476a1264ed95cbf127 100644
--- a/src/harmony-string.js
+++ b/src/harmony-string.js
@@ -53,7 +53,7 @@ function StringRepeat(count) {
 }


-// ES6 draft 01-20-14, section 21.1.3.18
+// ES6 draft 04-05-14, section 21.1.3.18
 function StringStartsWith(searchString /* position */) {  // length == 1
   CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith");

@@ -82,7 +82,7 @@ function StringStartsWith(searchString /* position */) { // length == 1
 }


-// ES6 draft 01-20-14, section 21.1.3.7
+// ES6 draft 04-05-14, section 21.1.3.7
 function StringEndsWith(searchString /* position */) {  // length == 1
   CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith");

@@ -114,11 +114,17 @@ function StringEndsWith(searchString /* position */) { // length == 1
 }


-// ES6 draft 01-20-14, section 21.1.3.6
+// ES6 draft 04-05-14, section 21.1.3.6
 function StringContains(searchString /* position */) {  // length == 1
   CHECK_OBJECT_COERCIBLE(this, "String.prototype.contains");

   var s = TO_STRING_INLINE(this);
+
+  if (IS_REGEXP(searchString)) {
+    throw MakeTypeError("first_argument_not_regexp",
+                        ["String.prototype.contains"]);
+  }
+
   var ss = TO_STRING_INLINE(searchString);
   var pos = 0;
   if (%_ArgumentsLength() > 1) {
Index: test/mjsunit/harmony/string-contains.js
diff --git a/test/mjsunit/harmony/string-contains.js b/test/mjsunit/harmony/string-contains.js index 700a6ed6bc1ab4edcca1a5d1bf0a6d849fb9b96b..b853ed99f783fb5525af0c964317c2d59360caf4 100644
--- a/test/mjsunit/harmony/string-contains.js
+++ b/test/mjsunit/harmony/string-contains.js
@@ -77,8 +77,6 @@ var TEST_INPUT = [{
 }, {
   msg: "Boolean false", val: false
 }, {
-  msg: "Regular expression /\d+/", val: /\d+/
-}, {
   msg: "Empty array []", val: []
 }, {
   msg: "Empty object {}", val: {}
@@ -126,7 +124,7 @@ assertTrue("abc".contains("ab", NaN));
 assertFalse("abc".contains("cd", NaN));
 assertFalse("xyzzy".contains("zy\0", 2));

-var dots = Array(10000).join('.');
+var dots = Array(10000).join(".");
 assertFalse(dots.contains("\x01", 10000));
 assertFalse(dots.contains("\0", 10000));

@@ -149,3 +147,20 @@ myobj = {
   },
   contains: String.prototype.contains
 };
+
+assertEquals("foo[a-z]+(bar)?".contains("[a-z]+"), true);
+assertThrows("'foo[a-z]+(bar)?'.contains(/[a-z]+/)", TypeError);
+assertThrows("'foo/[a-z]+/(bar)?'.contains(/[a-z]+/)", TypeError);
+assertEquals("foo[a-z]+(bar)?".contains("(bar)?"), true);
+assertThrows("'foo[a-z]+(bar)?'.contains(/(bar)?/)", TypeError);
+assertThrows("'foo[a-z]+/(bar)?/'.contains(/(bar)?/)", TypeError);
+
+assertThrows("String.prototype.contains.call({ 'toString': function() { " +
+  "throw RangeError(); } }, /./)", RangeError);
+assertThrows("String.prototype.contains.call({ 'toString': function() { " +
+  "return 'abc'; } }, /./)", TypeError);
+
+assertThrows("String.prototype.contains.apply({ 'toString': function() { " +
+  "throw RangeError(); } }, [/./])", RangeError);
+assertThrows("String.prototype.contains.apply({ 'toString': function() { " +
+  "return 'abc'; } }, [/./])", TypeError);


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to