2011-05-05 05:21:30 +00:00
|
|
|
// Copyright 2011 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:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
var should_throw_on_null_and_undefined =
|
|
|
|
[Object.prototype.toLocaleString,
|
|
|
|
Object.prototype.valueOf,
|
|
|
|
Object.prototype.hasOwnProperty,
|
|
|
|
Object.prototype.isPrototypeOf,
|
|
|
|
Object.prototype.propertyIsEnumerable,
|
|
|
|
Array.prototype.concat,
|
|
|
|
Array.prototype.join,
|
|
|
|
Array.prototype.pop,
|
|
|
|
Array.prototype.push,
|
|
|
|
Array.prototype.reverse,
|
|
|
|
Array.prototype.shift,
|
|
|
|
Array.prototype.slice,
|
|
|
|
Array.prototype.sort,
|
|
|
|
Array.prototype.splice,
|
|
|
|
Array.prototype.unshift,
|
|
|
|
Array.prototype.indexOf,
|
|
|
|
Array.prototype.lastIndexOf,
|
|
|
|
Array.prototype.every,
|
|
|
|
Array.prototype.some,
|
|
|
|
Array.prototype.forEach,
|
|
|
|
Array.prototype.map,
|
|
|
|
Array.prototype.filter,
|
|
|
|
Array.prototype.reduce,
|
|
|
|
Array.prototype.reduceRight,
|
|
|
|
String.prototype.charAt,
|
|
|
|
String.prototype.charCodeAt,
|
|
|
|
String.prototype.concat,
|
|
|
|
String.prototype.indexOf,
|
|
|
|
String.prototype.lastIndexOf,
|
|
|
|
String.prototype.localeCompare,
|
|
|
|
String.prototype.match,
|
|
|
|
String.prototype.replace,
|
|
|
|
String.prototype.search,
|
|
|
|
String.prototype.slice,
|
|
|
|
String.prototype.split,
|
|
|
|
String.prototype.substring,
|
|
|
|
String.prototype.toLowerCase,
|
|
|
|
String.prototype.toLocaleLowerCase,
|
|
|
|
String.prototype.toUpperCase,
|
|
|
|
String.prototype.toLocaleUpperCase,
|
2012-12-07 10:20:35 +00:00
|
|
|
String.prototype.trim];
|
2011-05-05 05:21:30 +00:00
|
|
|
|
|
|
|
// Non generic natives do not work on any input other than the specific
|
|
|
|
// type, but since this change will allow call to be invoked with undefined
|
|
|
|
// or null as this we still explicitly test that we throw on these here.
|
|
|
|
var non_generic =
|
|
|
|
[Array.prototype.toString,
|
|
|
|
Array.prototype.toLocaleString,
|
|
|
|
Function.prototype.toString,
|
|
|
|
Function.prototype.call,
|
|
|
|
Function.prototype.apply,
|
|
|
|
String.prototype.toString,
|
|
|
|
String.prototype.valueOf,
|
|
|
|
Boolean.prototype.toString,
|
|
|
|
Boolean.prototype.valueOf,
|
|
|
|
Number.prototype.toString,
|
|
|
|
Number.prototype.valueOf,
|
|
|
|
Number.prototype.toFixed,
|
|
|
|
Number.prototype.toExponential,
|
|
|
|
Number.prototype.toPrecision,
|
|
|
|
Date.prototype.toString,
|
|
|
|
Date.prototype.toDateString,
|
|
|
|
Date.prototype.toTimeString,
|
|
|
|
Date.prototype.toLocaleString,
|
|
|
|
Date.prototype.toLocaleDateString,
|
|
|
|
Date.prototype.toLocaleTimeString,
|
|
|
|
Date.prototype.valueOf,
|
|
|
|
Date.prototype.getTime,
|
|
|
|
Date.prototype.getFullYear,
|
|
|
|
Date.prototype.getUTCFullYear,
|
|
|
|
Date.prototype.getMonth,
|
|
|
|
Date.prototype.getUTCMonth,
|
|
|
|
Date.prototype.getDate,
|
|
|
|
Date.prototype.getUTCDate,
|
|
|
|
Date.prototype.getDay,
|
|
|
|
Date.prototype.getUTCDay,
|
|
|
|
Date.prototype.getHours,
|
|
|
|
Date.prototype.getUTCHours,
|
|
|
|
Date.prototype.getMinutes,
|
|
|
|
Date.prototype.getUTCMinutes,
|
|
|
|
Date.prototype.getSeconds,
|
|
|
|
Date.prototype.getUTCSeconds,
|
|
|
|
Date.prototype.getMilliseconds,
|
|
|
|
Date.prototype.getUTCMilliseconds,
|
|
|
|
Date.prototype.getTimezoneOffset,
|
|
|
|
Date.prototype.setTime,
|
|
|
|
Date.prototype.setMilliseconds,
|
|
|
|
Date.prototype.setUTCMilliseconds,
|
|
|
|
Date.prototype.setSeconds,
|
|
|
|
Date.prototype.setUTCSeconds,
|
|
|
|
Date.prototype.setMinutes,
|
|
|
|
Date.prototype.setUTCMinutes,
|
|
|
|
Date.prototype.setHours,
|
|
|
|
Date.prototype.setUTCHours,
|
|
|
|
Date.prototype.setDate,
|
|
|
|
Date.prototype.setUTCDate,
|
|
|
|
Date.prototype.setMonth,
|
|
|
|
Date.prototype.setUTCMonth,
|
|
|
|
Date.prototype.setFullYear,
|
|
|
|
Date.prototype.setUTCFullYear,
|
|
|
|
Date.prototype.toUTCString,
|
|
|
|
Date.prototype.toISOString,
|
|
|
|
Date.prototype.toJSON,
|
|
|
|
RegExp.prototype.exec,
|
|
|
|
RegExp.prototype.test,
|
2012-03-05 13:57:48 +00:00
|
|
|
RegExp.prototype.toString,
|
|
|
|
Error.prototype.toString];
|
2011-05-05 05:21:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Mapping functions.
|
|
|
|
var mapping_functions =
|
|
|
|
[Array.prototype.every,
|
|
|
|
Array.prototype.some,
|
|
|
|
Array.prototype.forEach,
|
|
|
|
Array.prototype.map,
|
|
|
|
Array.prototype.filter];
|
|
|
|
|
|
|
|
// Reduce functions.
|
|
|
|
var reducing_functions =
|
|
|
|
[Array.prototype.reduce,
|
|
|
|
Array.prototype.reduceRight];
|
|
|
|
|
2012-11-14 09:14:47 +00:00
|
|
|
function checkExpectedMessage(e) {
|
|
|
|
assertTrue(e.message.indexOf("called on null or undefined") >= 0 ||
|
2013-07-05 08:34:31 +00:00
|
|
|
e.message.indexOf("invoked on undefined or null value") >= 0 ||
|
|
|
|
e.message.indexOf("Cannot convert undefined or null to object") >= 0);
|
2012-11-14 09:14:47 +00:00
|
|
|
}
|
|
|
|
|
2011-05-05 05:21:30 +00:00
|
|
|
// Test that all natives using the ToObject call throw the right exception.
|
|
|
|
for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) {
|
|
|
|
// Sanity check that all functions are correct
|
|
|
|
assertEquals(typeof(should_throw_on_null_and_undefined[i]), "function");
|
|
|
|
|
2011-05-31 08:08:42 +00:00
|
|
|
var exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
2014-11-04 16:13:49 +00:00
|
|
|
// We need to pass a dummy object argument ({}) to these functions because
|
|
|
|
// of Object.prototype.isPrototypeOf's special behavior, see issue 3483
|
|
|
|
// for more details.
|
|
|
|
should_throw_on_null_and_undefined[i].call(null, {});
|
2011-05-05 05:21:30 +00:00
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2012-11-14 09:14:47 +00:00
|
|
|
checkExpectedMessage(e);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
2014-11-04 16:13:49 +00:00
|
|
|
should_throw_on_null_and_undefined[i].call(undefined, {});
|
2011-05-05 05:21:30 +00:00
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2012-11-14 09:14:47 +00:00
|
|
|
checkExpectedMessage(e);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
2014-11-04 16:13:49 +00:00
|
|
|
should_throw_on_null_and_undefined[i].apply(null, [{}]);
|
2011-05-05 05:21:30 +00:00
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2012-11-14 09:14:47 +00:00
|
|
|
checkExpectedMessage(e);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
2014-11-04 16:13:49 +00:00
|
|
|
should_throw_on_null_and_undefined[i].apply(undefined, [{}]);
|
2011-05-05 05:21:30 +00:00
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2012-11-14 09:14:47 +00:00
|
|
|
checkExpectedMessage(e);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that all natives that are non generic throw on null and undefined.
|
|
|
|
for (var i = 0; i < non_generic.length; i++) {
|
|
|
|
// Sanity check that all functions are correct
|
|
|
|
assertEquals(typeof(non_generic[i]), "function");
|
2011-05-31 08:08:42 +00:00
|
|
|
|
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
non_generic[i].call(null);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2011-05-05 05:21:30 +00:00
|
|
|
assertTrue(e instanceof TypeError);
|
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
non_generic[i].call(null);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2011-05-05 05:21:30 +00:00
|
|
|
assertTrue(e instanceof TypeError);
|
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
non_generic[i].apply(null);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2011-05-05 05:21:30 +00:00
|
|
|
assertTrue(e instanceof TypeError);
|
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
non_generic[i].apply(null);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2011-05-05 05:21:30 +00:00
|
|
|
assertTrue(e instanceof TypeError);
|
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test that we still throw when calling with thisArg null or undefined
|
|
|
|
// through an array mapping function.
|
2014-11-04 16:13:49 +00:00
|
|
|
// We need to make sure that the elements of `array` are all object values,
|
|
|
|
// see issue 3483 for more details.
|
|
|
|
var array = [{}, [], new Number, new Map, new WeakSet];
|
2011-05-05 05:21:30 +00:00
|
|
|
for (var j = 0; j < mapping_functions.length; j++) {
|
|
|
|
for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
mapping_functions[j].call(array,
|
|
|
|
should_throw_on_null_and_undefined[i],
|
|
|
|
null);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2012-11-14 09:14:47 +00:00
|
|
|
checkExpectedMessage(e);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
mapping_functions[j].call(array,
|
|
|
|
should_throw_on_null_and_undefined[i],
|
|
|
|
undefined);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2012-11-14 09:14:47 +00:00
|
|
|
checkExpectedMessage(e);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var j = 0; j < mapping_functions.length; j++) {
|
|
|
|
for (var i = 0; i < non_generic.length; i++) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
mapping_functions[j].call(array,
|
|
|
|
non_generic[i],
|
|
|
|
null);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2011-05-05 05:21:30 +00:00
|
|
|
assertTrue(e instanceof TypeError);
|
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
mapping_functions[j].call(array,
|
|
|
|
non_generic[i],
|
|
|
|
undefined);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2011-05-05 05:21:30 +00:00
|
|
|
assertTrue(e instanceof TypeError);
|
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reduce functions do a call with null as this argument.
|
|
|
|
for (var j = 0; j < reducing_functions.length; j++) {
|
|
|
|
for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
reducing_functions[j].call(array, should_throw_on_null_and_undefined[i]);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2012-11-14 09:14:47 +00:00
|
|
|
checkExpectedMessage(e);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
reducing_functions[j].call(array, should_throw_on_null_and_undefined[i]);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2012-11-14 09:14:47 +00:00
|
|
|
checkExpectedMessage(e);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var j = 0; j < reducing_functions.length; j++) {
|
|
|
|
for (var i = 0; i < non_generic.length; i++) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
reducing_functions[j].call(array, non_generic[i]);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2011-05-05 05:21:30 +00:00
|
|
|
assertTrue(e instanceof TypeError);
|
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = false;
|
2011-05-05 05:21:30 +00:00
|
|
|
try {
|
|
|
|
reducing_functions[j].call(array, non_generic[i]);
|
|
|
|
} catch (e) {
|
2011-05-31 08:08:42 +00:00
|
|
|
exception = true;
|
2011-05-05 05:21:30 +00:00
|
|
|
assertTrue(e instanceof TypeError);
|
|
|
|
}
|
2011-05-31 08:08:42 +00:00
|
|
|
assertTrue(exception);
|
2011-05-05 05:21:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Object.prototype.toString()
|
|
|
|
assertEquals(Object.prototype.toString.call(null),
|
|
|
|
'[object Null]')
|
|
|
|
|
|
|
|
assertEquals(Object.prototype.toString.call(undefined),
|
|
|
|
'[object Undefined]')
|