Skip to content

Commit

Permalink
Make String.prototype.{starts,ends}With throw when passing a regula…
Browse files Browse the repository at this point in the history
…r expression

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

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

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

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@18870 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
  • Loading branch information
ishell@chromium.org committed Jan 28, 2014
1 parent 9e46250 commit 1776dff
Show file tree
Hide file tree
Showing 4 changed files with 570 additions and 25 deletions.
38 changes: 19 additions & 19 deletions src/harmony-string.js
@@ -1,4 +1,4 @@
// Copyright 2013 the V8 project authors. All rights reserved.
// Copyright 2014 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
Expand Down Expand Up @@ -34,12 +34,9 @@

// -------------------------------------------------------------------

// ES6 draft 07-15-13, section 15.5.3.21
// ES6 draft 01-20-14, section 21.1.3.13
function StringRepeat(count) {
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
throw MakeTypeError("called_on_null_or_undefined",
["String.prototype.repeat"]);
}
CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat");

var s = TO_STRING_INLINE(this);
var n = ToInteger(count);
Expand All @@ -56,14 +53,17 @@ function StringRepeat(count) {
}


// ES6 draft 07-15-13, section 15.5.3.22
// ES6 draft 01-20-14, section 21.1.3.18
function StringStartsWith(searchString /* position */) { // length == 1
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
throw MakeTypeError("called_on_null_or_undefined",
CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith");

var s = TO_STRING_INLINE(this);

if (IS_REGEXP(searchString)) {
throw MakeTypeError("first_argument_not_regexp",
["String.prototype.startsWith"]);
}

var s = TO_STRING_INLINE(this);
var ss = TO_STRING_INLINE(searchString);
var pos = 0;
if (%_ArgumentsLength() > 1) {
Expand All @@ -82,14 +82,17 @@ function StringStartsWith(searchString /* position */) { // length == 1
}


// ES6 draft 07-15-13, section 15.5.3.23
// ES6 draft 01-20-14, section 21.1.3.7
function StringEndsWith(searchString /* position */) { // length == 1
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
throw MakeTypeError("called_on_null_or_undefined",
CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith");

var s = TO_STRING_INLINE(this);

if (IS_REGEXP(searchString)) {
throw MakeTypeError("first_argument_not_regexp",
["String.prototype.endsWith"]);
}

var s = TO_STRING_INLINE(this);
var ss = TO_STRING_INLINE(searchString);
var s_len = s.length;
var pos = s_len;
Expand All @@ -111,12 +114,9 @@ function StringEndsWith(searchString /* position */) { // length == 1
}


// ES6 draft 07-15-13, section 15.5.3.24
// ES6 draft 01-20-14, section 21.1.3.6
function StringContains(searchString /* position */) { // length == 1
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
throw MakeTypeError("called_on_null_or_undefined",
["String.prototype.contains"]);
}
CHECK_OBJECT_COERCIBLE(this, "String.prototype.contains");

var s = TO_STRING_INLINE(this);
var ss = TO_STRING_INLINE(searchString);
Expand Down
1 change: 1 addition & 0 deletions src/messages.js
Expand Up @@ -114,6 +114,7 @@ var kMessages = {
promise_cyclic: ["Chaining cycle detected for promise ", "%0"],
array_functions_on_frozen: ["Cannot modify frozen array elements"],
array_functions_change_sealed: ["Cannot add/remove sealed array elements"],
first_argument_not_regexp: ["First argument to ", "%0", " must not be a regular expression"],
// RangeError
invalid_array_length: ["Invalid array length"],
invalid_array_buffer_length: ["Invalid array buffer length"],
Expand Down
282 changes: 279 additions & 3 deletions test/mjsunit/harmony/string-endswith.js
@@ -1,4 +1,4 @@
// Copyright 2013 the V8 project authors. All rights reserved.
// Copyright 2014 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
Expand Down Expand Up @@ -65,8 +65,6 @@ var TEST_INPUT = [{
msg: "Boolean true", val: true
}, {
msg: "Boolean false", val: false
}, {
msg: "Regular expression /\d+/", val: /\d+/
}, {
msg: "Empty array []", val: []
}, {
Expand Down Expand Up @@ -134,3 +132,281 @@ assertTrue("abc".endsWith("bc", undefined));
assertFalse("abc".endsWith("bc", -43));
assertFalse("abc".endsWith("bc", -Infinity));
assertFalse("abc".endsWith("bc", NaN));

// Test cases taken from
// https://github.com/mathiasbynens/String.prototype.endsWith/blob/master/tests/tests.js
Object.prototype[1] = 2; // try to break `arguments[1]`

assertEquals(String.prototype.endsWith.length, 1);
assertEquals(String.prototype.propertyIsEnumerable("endsWith"), false);

assertEquals("undefined".endsWith(), true);
assertEquals("undefined".endsWith(undefined), true);
assertEquals("undefined".endsWith(null), false);
assertEquals("null".endsWith(), false);
assertEquals("null".endsWith(undefined), false);
assertEquals("null".endsWith(null), true);

assertEquals("abc".endsWith(), false);
assertEquals("abc".endsWith(""), true);
assertEquals("abc".endsWith("\0"), false);
assertEquals("abc".endsWith("c"), true);
assertEquals("abc".endsWith("b"), false);
assertEquals("abc".endsWith("ab"), false);
assertEquals("abc".endsWith("bc"), true);
assertEquals("abc".endsWith("abc"), true);
assertEquals("abc".endsWith("bcd"), false);
assertEquals("abc".endsWith("abcd"), false);
assertEquals("abc".endsWith("bcde"), false);

assertEquals("abc".endsWith("", NaN), true);
assertEquals("abc".endsWith("\0", NaN), false);
assertEquals("abc".endsWith("c", NaN), false);
assertEquals("abc".endsWith("b", NaN), false);
assertEquals("abc".endsWith("ab", NaN), false);
assertEquals("abc".endsWith("bc", NaN), false);
assertEquals("abc".endsWith("abc", NaN), false);
assertEquals("abc".endsWith("bcd", NaN), false);
assertEquals("abc".endsWith("abcd", NaN), false);
assertEquals("abc".endsWith("bcde", NaN), false);

assertEquals("abc".endsWith("", false), true);
assertEquals("abc".endsWith("\0", false), false);
assertEquals("abc".endsWith("c", false), false);
assertEquals("abc".endsWith("b", false), false);
assertEquals("abc".endsWith("ab", false), false);
assertEquals("abc".endsWith("bc", false), false);
assertEquals("abc".endsWith("abc", false), false);
assertEquals("abc".endsWith("bcd", false), false);
assertEquals("abc".endsWith("abcd", false), false);
assertEquals("abc".endsWith("bcde", false), false);

assertEquals("abc".endsWith("", undefined), true);
assertEquals("abc".endsWith("\0", undefined), false);
assertEquals("abc".endsWith("c", undefined), true);
assertEquals("abc".endsWith("b", undefined), false);
assertEquals("abc".endsWith("ab", undefined), false);
assertEquals("abc".endsWith("bc", undefined), true);
assertEquals("abc".endsWith("abc", undefined), true);
assertEquals("abc".endsWith("bcd", undefined), false);
assertEquals("abc".endsWith("abcd", undefined), false);
assertEquals("abc".endsWith("bcde", undefined), false);

assertEquals("abc".endsWith("", null), true);
assertEquals("abc".endsWith("\0", null), false);
assertEquals("abc".endsWith("c", null), false);
assertEquals("abc".endsWith("b", null), false);
assertEquals("abc".endsWith("ab", null), false);
assertEquals("abc".endsWith("bc", null), false);
assertEquals("abc".endsWith("abc", null), false);
assertEquals("abc".endsWith("bcd", null), false);
assertEquals("abc".endsWith("abcd", null), false);
assertEquals("abc".endsWith("bcde", null), false);

assertEquals("abc".endsWith("", -Infinity), true);
assertEquals("abc".endsWith("\0", -Infinity), false);
assertEquals("abc".endsWith("c", -Infinity), false);
assertEquals("abc".endsWith("b", -Infinity), false);
assertEquals("abc".endsWith("ab", -Infinity), false);
assertEquals("abc".endsWith("bc", -Infinity), false);
assertEquals("abc".endsWith("abc", -Infinity), false);
assertEquals("abc".endsWith("bcd", -Infinity), false);
assertEquals("abc".endsWith("abcd", -Infinity), false);
assertEquals("abc".endsWith("bcde", -Infinity), false);

assertEquals("abc".endsWith("", -1), true);
assertEquals("abc".endsWith("\0", -1), false);
assertEquals("abc".endsWith("c", -1), false);
assertEquals("abc".endsWith("b", -1), false);
assertEquals("abc".endsWith("ab", -1), false);
assertEquals("abc".endsWith("bc", -1), false);
assertEquals("abc".endsWith("abc", -1), false);
assertEquals("abc".endsWith("bcd", -1), false);
assertEquals("abc".endsWith("abcd", -1), false);
assertEquals("abc".endsWith("bcde", -1), false);

assertEquals("abc".endsWith("", -0), true);
assertEquals("abc".endsWith("\0", -0), false);
assertEquals("abc".endsWith("c", -0), false);
assertEquals("abc".endsWith("b", -0), false);
assertEquals("abc".endsWith("ab", -0), false);
assertEquals("abc".endsWith("bc", -0), false);
assertEquals("abc".endsWith("abc", -0), false);
assertEquals("abc".endsWith("bcd", -0), false);
assertEquals("abc".endsWith("abcd", -0), false);
assertEquals("abc".endsWith("bcde", -0), false);

assertEquals("abc".endsWith("", +0), true);
assertEquals("abc".endsWith("\0", +0), false);
assertEquals("abc".endsWith("c", +0), false);
assertEquals("abc".endsWith("b", +0), false);
assertEquals("abc".endsWith("ab", +0), false);
assertEquals("abc".endsWith("bc", +0), false);
assertEquals("abc".endsWith("abc", +0), false);
assertEquals("abc".endsWith("bcd", +0), false);
assertEquals("abc".endsWith("abcd", +0), false);
assertEquals("abc".endsWith("bcde", +0), false);

assertEquals("abc".endsWith("", 1), true);
assertEquals("abc".endsWith("\0", 1), false);
assertEquals("abc".endsWith("c", 1), false);
assertEquals("abc".endsWith("b", 1), false);
assertEquals("abc".endsWith("ab", 1), false);
assertEquals("abc".endsWith("bc", 1), false);
assertEquals("abc".endsWith("abc", 1), false);
assertEquals("abc".endsWith("bcd", 1), false);
assertEquals("abc".endsWith("abcd", 1), false);
assertEquals("abc".endsWith("bcde", 1), false);

assertEquals("abc".endsWith("", 2), true);
assertEquals("abc".endsWith("\0", 2), false);
assertEquals("abc".endsWith("c", 2), false);
assertEquals("abc".endsWith("b", 2), true);
assertEquals("abc".endsWith("ab", 2), true);
assertEquals("abc".endsWith("bc", 2), false);
assertEquals("abc".endsWith("abc", 2), false);
assertEquals("abc".endsWith("bcd", 2), false);
assertEquals("abc".endsWith("abcd", 2), false);
assertEquals("abc".endsWith("bcde", 2), false);

assertEquals("abc".endsWith("", +Infinity), true);
assertEquals("abc".endsWith("\0", +Infinity), false);
assertEquals("abc".endsWith("c", +Infinity), true);
assertEquals("abc".endsWith("b", +Infinity), false);
assertEquals("abc".endsWith("ab", +Infinity), false);
assertEquals("abc".endsWith("bc", +Infinity), true);
assertEquals("abc".endsWith("abc", +Infinity), true);
assertEquals("abc".endsWith("bcd", +Infinity), false);
assertEquals("abc".endsWith("abcd", +Infinity), false);
assertEquals("abc".endsWith("bcde", +Infinity), false);

assertEquals("abc".endsWith("", true), true);
assertEquals("abc".endsWith("\0", true), false);
assertEquals("abc".endsWith("c", true), false);
assertEquals("abc".endsWith("b", true), false);
assertEquals("abc".endsWith("ab", true), false);
assertEquals("abc".endsWith("bc", true), false);
assertEquals("abc".endsWith("abc", true), false);
assertEquals("abc".endsWith("bcd", true), false);
assertEquals("abc".endsWith("abcd", true), false);
assertEquals("abc".endsWith("bcde", true), false);

assertEquals("abc".endsWith("", "x"), true);
assertEquals("abc".endsWith("\0", "x"), false);
assertEquals("abc".endsWith("c", "x"), false);
assertEquals("abc".endsWith("b", "x"), false);
assertEquals("abc".endsWith("ab", "x"), false);
assertEquals("abc".endsWith("bc", "x"), false);
assertEquals("abc".endsWith("abc", "x"), false);
assertEquals("abc".endsWith("bcd", "x"), false);
assertEquals("abc".endsWith("abcd", "x"), false);
assertEquals("abc".endsWith("bcde", "x"), false);

assertEquals("[a-z]+(bar)?".endsWith("(bar)?"), true);
assertThrows(function() { "[a-z]+(bar)?".endsWith(/(bar)?/);
}, TypeError);
assertEquals("[a-z]+(bar)?".endsWith("[a-z]+", 6), true);
assertThrows(function() { "[a-z]+(bar)?".endsWith(/(bar)?/);
}, TypeError);
assertThrows(function() { "[a-z]+/(bar)?/".endsWith(/(bar)?/);
}, TypeError);

// http://mathiasbynens.be/notes/javascript-unicode#poo-test
var string = "I\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\u2603\uD83D\uDCA9";
assertEquals(string.endsWith(""), true);
assertEquals(string.endsWith("\xF1t\xEBr"), false);
assertEquals(string.endsWith("\xF1t\xEBr", 5), true);
assertEquals(string.endsWith("\xE0liz\xE6"), false);
assertEquals(string.endsWith("\xE0liz\xE6", 16), true);
assertEquals(string.endsWith("\xF8n\u2603\uD83D\uDCA9"), true);
assertEquals(string.endsWith("\xF8n\u2603\uD83D\uDCA9", 23), true);
assertEquals(string.endsWith("\u2603"), false);
assertEquals(string.endsWith("\u2603", 21), true);
assertEquals(string.endsWith("\uD83D\uDCA9"), true);
assertEquals(string.endsWith("\uD83D\uDCA9", 23), true);

assertThrows(function() {
String.prototype.endsWith.call(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.endsWith.call(undefined, "b");
}, TypeError);
assertThrows(function() {
String.prototype.endsWith.call(undefined, "b", 4);
}, TypeError);
assertThrows(function() {
String.prototype.endsWith.call(null);
}, TypeError);
assertThrows(function() {
String.prototype.endsWith.call(null, "b");
}, TypeError);
assertThrows(function() {
String.prototype.endsWith.call(null, "b", 4);
}, TypeError);
assertEquals(String.prototype.endsWith.call(42, "2"), true);
assertEquals(String.prototype.endsWith.call(42, "4"), false);
assertEquals(String.prototype.endsWith.call(42, "b", 4), false);
assertEquals(String.prototype.endsWith.call(42, "2", 1), false);
assertEquals(String.prototype.endsWith.call(42, "2", 4), true);
assertEquals(String.prototype.endsWith.call({
"toString": function() { return "abc"; }
}, "b", 0), false);
assertEquals(String.prototype.endsWith.call({
"toString": function() { return "abc"; }
}, "b", 1), false);
assertEquals(String.prototype.endsWith.call({
"toString": function() { return "abc"; }
}, "b", 2), true);
assertThrows(function() {
String.prototype.endsWith.call({
"toString": function() { throw RangeError(); }
}, /./);
}, RangeError);
assertThrows(function() {
String.prototype.endsWith.call({
"toString": function() { return "abc"; }
}, /./);
}, TypeError);

assertThrows(function() {
String.prototype.endsWith.apply(undefined);
}, TypeError);
assertThrows(function() {
String.prototype.endsWith.apply(undefined, ["b"]); },
TypeError);
assertThrows(function() {
String.prototype.endsWith.apply(undefined, ["b", 4]);
}, TypeError);
assertThrows(function() {
String.prototype.endsWith.apply(null);
}, TypeError);
assertThrows(function() {
String.prototype.endsWith.apply(null, ["b"]);
}, TypeError);
assertThrows(function() {
String.prototype.endsWith.apply(null, ["b", 4]);
}, TypeError);
assertEquals(String.prototype.endsWith.apply(42, ["2"]), true);
assertEquals(String.prototype.endsWith.apply(42, ["4"]), false);
assertEquals(String.prototype.endsWith.apply(42, ["b", 4]), false);
assertEquals(String.prototype.endsWith.apply(42, ["2", 1]), false);
assertEquals(String.prototype.endsWith.apply(42, ["2", 4]), true);
assertEquals(String.prototype.endsWith.apply({
"toString": function() { return "abc"; }
}, ["b", 0]), false);
assertEquals(String.prototype.endsWith.apply({
"toString": function() { return "abc"; }
}, ["b", 1]), false);
assertEquals(String.prototype.endsWith.apply({
"toString": function() { return "abc"; }
}, ["b", 2]), true);
assertThrows(function() {
String.prototype.endsWith.apply({
"toString": function() { throw RangeError(); }
}, [/./]);
}, RangeError);
assertThrows(function() {
String.prototype.endsWith.apply({
"toString": function() { return "abc"; }
}, [/./]);
}, TypeError);

0 comments on commit 1776dff

Please sign in to comment.