Revision: 20534
Author:   [email protected]
Date:     Mon Apr  7 10:24:01 2014 UTC
Log: 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]

Review URL: https://codereview.chromium.org/227113005

Patch from Mathias Bynens <[email protected]>.
http://code.google.com/p/v8/source/detail?r=20534

Modified:
 /branches/bleeding_edge/src/harmony-string.js
 /branches/bleeding_edge/test/cctest/test-mark-compact.cc
 /branches/bleeding_edge/test/mjsunit/harmony/string-contains.js

=======================================
--- /branches/bleeding_edge/src/harmony-string.js Tue Jan 28 10:31:05 2014 UTC +++ /branches/bleeding_edge/src/harmony-string.js Mon Apr 7 10:24:01 2014 UTC
@@ -53,7 +53,7 @@
 }


-// 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 @@
 }


-// 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 @@
 }


-// 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) {
=======================================
--- /branches/bleeding_edge/test/cctest/test-mark-compact.cc Fri Apr 4 12:06:11 2014 UTC +++ /branches/bleeding_edge/test/cctest/test-mark-compact.cc Mon Apr 7 10:24:01 2014 UTC
@@ -507,9 +507,9 @@
     printf("delta: %" V8_PTR_PREFIX "d kB\n", delta / 1024);
     if (sizeof(initial_memory) == 8) {  // 64-bit.
       if (v8::internal::Snapshot::IsEnabled()) {
-        CHECK_LE(delta, 4000 * 1024);
+        CHECK_LE(delta, 4100 * 1024);
       } else {
-        CHECK_LE(delta, 4500 * 1024);
+        CHECK_LE(delta, 4600 * 1024);
       }
     } else {                            // 32-bit.
       if (v8::internal::Snapshot::IsEnabled()) {
=======================================
--- /branches/bleeding_edge/test/mjsunit/harmony/string-contains.js Tue Jul 30 16:33:08 2013 UTC +++ /branches/bleeding_edge/test/mjsunit/harmony/string-contains.js Mon Apr 7 10:24:01 2014 UTC
@@ -76,8 +76,6 @@
   msg: "Boolean true", val: true
 }, {
   msg: "Boolean false", val: false
-}, {
-  msg: "Regular expression /\d+/", val: /\d+/
 }, {
   msg: "Empty array []", val: []
 }, {
@@ -126,7 +124,7 @@
 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 @@
   },
   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