Commit e7f0beea authored by yangguo@chromium.org's avatar yangguo@chromium.org

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

Contributed by Mathias Bynens <mathiasb@opera.com>.

TEST=mjsunit/harmony
BUG=v8:3261
LOG=Y
R=yangguo@chromium.org, arv@chromium.org, ishell@chromium.org

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

Patch from Mathias Bynens <mathiasb@opera.com>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20534 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 87a9c8e9
......@@ -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) {
......
......@@ -507,9 +507,9 @@ TEST(BootUpMemoryUse) {
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()) {
......
......@@ -76,8 +76,6 @@ var TEST_INPUT = [{
msg: "Boolean true", val: true
}, {
msg: "Boolean false", val: false
}, {
msg: "Regular expression /\d+/", val: /\d+/
}, {
msg: "Empty array []", 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);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment