10040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// Copyright 2011 the V8 project authors. All rights reserved.
20040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// Redistribution and use in source and binary forms, with or without
30040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// modification, are permitted provided that the following conditions are
40040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// met:
50040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org//
60040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org//     * Redistributions of source code must retain the above copyright
70040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org//       notice, this list of conditions and the following disclaimer.
80040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org//     * Redistributions in binary form must reproduce the above
90040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org//       copyright notice, this list of conditions and the following
100040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org//       disclaimer in the documentation and/or other materials provided
110040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org//       with the distribution.
120040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org//     * Neither the name of Google Inc. nor the names of its
130040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org//       contributors may be used to endorse or promote products derived
140040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org//       from this software without specific prior written permission.
150040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org//
160040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
170040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
180040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
190040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
200040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
210040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
220040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
230040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
240040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
250040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
260040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
270040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
280040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
290040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgvar should_throw_on_null_and_undefined =
300040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    [Object.prototype.toLocaleString,
310040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Object.prototype.valueOf,
320040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Object.prototype.hasOwnProperty,
330040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Object.prototype.isPrototypeOf,
340040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Object.prototype.propertyIsEnumerable,
350040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.concat,
360040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.join,
370040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.pop,
380040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.push,
390040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.reverse,
400040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.shift,
410040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.slice,
420040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.sort,
430040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.splice,
440040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.unshift,
450040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.indexOf,
460040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.lastIndexOf,
470040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.every,
480040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.some,
490040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.forEach,
500040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.map,
510040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.filter,
520040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.reduce,
530040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.reduceRight,
540040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.charAt,
550040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.charCodeAt,
560040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.concat,
570040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.indexOf,
580040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.lastIndexOf,
590040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.localeCompare,
600040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.match,
610040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.replace,
620040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.search,
630040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.slice,
640040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.split,
650040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.substring,
660040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.toLowerCase,
670040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.toLocaleLowerCase,
680040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.toUpperCase,
690040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.toLocaleUpperCase,
70cddc71f18a46e6117cc765b1c53ca122e7aaa318rossberg@chromium.org     String.prototype.trim];
710040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
720040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// Non generic natives do not work on any input other than the specific
730040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// type, but since this change will allow call to be invoked with undefined
740040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// or null as this we still explicitly test that we throw on these here.
750040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgvar non_generic =
760040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    [Array.prototype.toString,
770040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.toLocaleString,
780040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Function.prototype.toString,
790040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Function.prototype.call,
800040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Function.prototype.apply,
810040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.toString,
820040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     String.prototype.valueOf,
830040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Boolean.prototype.toString,
840040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Boolean.prototype.valueOf,
850040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Number.prototype.toString,
860040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Number.prototype.valueOf,
870040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Number.prototype.toFixed,
880040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Number.prototype.toExponential,
890040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Number.prototype.toPrecision,
900040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.toString,
910040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.toDateString,
920040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.toTimeString,
930040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.toLocaleString,
940040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.toLocaleDateString,
950040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.toLocaleTimeString,
960040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.valueOf,
970040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getTime,
980040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getFullYear,
990040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getUTCFullYear,
1000040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getMonth,
1010040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getUTCMonth,
1020040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getDate,
1030040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getUTCDate,
1040040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getDay,
1050040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getUTCDay,
1060040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getHours,
1070040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getUTCHours,
1080040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getMinutes,
1090040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getUTCMinutes,
1100040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getSeconds,
1110040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getUTCSeconds,
1120040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getMilliseconds,
1130040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getUTCMilliseconds,
1140040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.getTimezoneOffset,
1150040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setTime,
1160040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setMilliseconds,
1170040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setUTCMilliseconds,
1180040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setSeconds,
1190040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setUTCSeconds,
1200040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setMinutes,
1210040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setUTCMinutes,
1220040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setHours,
1230040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setUTCHours,
1240040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setDate,
1250040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setUTCDate,
1260040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setMonth,
1270040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setUTCMonth,
1280040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setFullYear,
1290040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.setUTCFullYear,
1300040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.toUTCString,
1310040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.toISOString,
1320040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Date.prototype.toJSON,
1330040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     RegExp.prototype.exec,
1340040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     RegExp.prototype.test,
1359a21ec41a2007f01ba18cf5fa48f7987e40e5109ulan@chromium.org     RegExp.prototype.toString,
1369a21ec41a2007f01ba18cf5fa48f7987e40e5109ulan@chromium.org     Error.prototype.toString];
1370040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
1380040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
1390040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// Mapping functions.
1400040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgvar mapping_functions =
1410040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    [Array.prototype.every,
1420040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.some,
1430040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.forEach,
1440040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.map,
1450040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.filter];
1460040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
1470040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// Reduce functions.
1480040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgvar reducing_functions =
1490040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    [Array.prototype.reduce,
1500040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org     Array.prototype.reduceRight];
1510040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
152fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.orgfunction checkExpectedMessage(e) {
153fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.org  assertTrue(e.message.indexOf("called on null or undefined") >= 0 ||
154e0e1b0d3e70c933d36ed381d511e9fda39f2a751mstarzinger@chromium.org      e.message.indexOf("invoked on undefined or null value") >= 0 ||
155e0e1b0d3e70c933d36ed381d511e9fda39f2a751mstarzinger@chromium.org      e.message.indexOf("Cannot convert undefined or null to object") >= 0);
156fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.org}
157fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.org
1580040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// Test that all natives using the ToObject call throw the right exception.
1590040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgfor (var i = 0; i < should_throw_on_null_and_undefined.length; i++) {
1600040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  // Sanity check that all functions are correct
1610040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  assertEquals(typeof(should_throw_on_null_and_undefined[i]), "function");
1620040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
163d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  var exception = false;
1640040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  try {
1650040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    // We call all functions with no parameters, which means that essential
1660040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    // parameters will have the undefined value.
1670040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    // The test for whether the "this" value is null or undefined is always
1680040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    // performed before access to the other parameters, so even if the
1690040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    // undefined value is an invalid argument value, it mustn't change
1700040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    // the result of the test.
1710040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    should_throw_on_null_and_undefined[i].call(null);
1720040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  } catch (e) {
173d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = true;
174fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.org    checkExpectedMessage(e);
1750040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
176d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  assertTrue(exception);
1770040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
178d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  exception = false;
1790040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  try {
1800040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    should_throw_on_null_and_undefined[i].call(undefined);
1810040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  } catch (e) {
182d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = true;
183fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.org    checkExpectedMessage(e);
1840040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
185d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  assertTrue(exception);
1860040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
187d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  exception = false;
1880040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  try {
1890040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    should_throw_on_null_and_undefined[i].apply(null);
1900040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  } catch (e) {
191d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = true;
192fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.org    checkExpectedMessage(e);
1930040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
194d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  assertTrue(exception);
1950040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
196d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  exception = false;
1970040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  try {
1980040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    should_throw_on_null_and_undefined[i].apply(undefined);
1990040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  } catch (e) {
200d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = true;
201fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.org    checkExpectedMessage(e);
2020040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
203d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  assertTrue(exception);
2040040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org}
2050040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
2060040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// Test that all natives that are non generic throw on null and undefined.
2070040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgfor (var i = 0; i < non_generic.length; i++) {
2080040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  // Sanity check that all functions are correct
2090040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  assertEquals(typeof(non_generic[i]), "function");
210d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org
211d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  exception = false;
2120040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  try {
2130040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    non_generic[i].call(null);
2140040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  } catch (e) {
215d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = true;
2160040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    assertTrue(e instanceof TypeError);
2170040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
218d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  assertTrue(exception);
2190040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
220d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  exception = false;
2210040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  try {
2220040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    non_generic[i].call(null);
2230040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  } catch (e) {
224d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = true;
2250040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    assertTrue(e instanceof TypeError);
2260040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
227d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  assertTrue(exception);
2280040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
229d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  exception = false;
2300040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  try {
2310040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    non_generic[i].apply(null);
2320040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  } catch (e) {
233d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = true;
2340040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    assertTrue(e instanceof TypeError);
2350040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
236d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  assertTrue(exception);
2370040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
238d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  exception = false;
2390040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  try {
2400040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    non_generic[i].apply(null);
2410040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  } catch (e) {
242d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = true;
2430040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    assertTrue(e instanceof TypeError);
2440040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
245d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org  assertTrue(exception);
2460040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org}
2470040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
2480040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
2490040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// Test that we still throw when calling with thisArg null or undefined
2500040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// through an array mapping function.
2510040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgvar array = [1,2,3,4,5];
2520040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgfor (var j = 0; j < mapping_functions.length; j++) {
2530040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) {
254d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = false;
2550040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    try {
2560040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      mapping_functions[j].call(array,
2570040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org                                should_throw_on_null_and_undefined[i],
2580040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org                                null);
2590040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    } catch (e) {
260d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org      exception = true;
261fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.org      checkExpectedMessage(e);
2620040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    }
263d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    assertTrue(exception);
2640040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
265d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = false;
2660040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    try {
2670040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      mapping_functions[j].call(array,
2680040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org                                should_throw_on_null_and_undefined[i],
2690040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org                                undefined);
2700040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    } catch (e) {
271d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org      exception = true;
272fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.org      checkExpectedMessage(e);
2730040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    }
274d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    assertTrue(exception);
2750040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
2760040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org}
2770040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
2780040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgfor (var j = 0; j < mapping_functions.length; j++) {
2790040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  for (var i = 0; i < non_generic.length; i++) {
280d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = false;
2810040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    try {
2820040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      mapping_functions[j].call(array,
2830040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org                                non_generic[i],
2840040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org                                null);
2850040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    } catch (e) {
286d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org      exception = true;
2870040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      assertTrue(e instanceof TypeError);
2880040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    }
289d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    assertTrue(exception);
2900040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
291d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = false;
2920040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    try {
2930040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      mapping_functions[j].call(array,
2940040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org                                non_generic[i],
2950040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org                                undefined);
2960040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    } catch (e) {
297d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org      exception = true;
2980040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      assertTrue(e instanceof TypeError);
2990040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    }
300d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    assertTrue(exception);
3010040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
3020040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org}
3030040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
3040040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
3050040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// Reduce functions do a call with null as this argument.
3060040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgfor (var j = 0; j < reducing_functions.length; j++) {
3070040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  for (var i = 0; i < should_throw_on_null_and_undefined.length; i++) {
308d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = false;
3090040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    try {
3100040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      reducing_functions[j].call(array, should_throw_on_null_and_undefined[i]);
3110040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    } catch (e) {
312d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org      exception = true;
313fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.org      checkExpectedMessage(e);
3140040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    }
315d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    assertTrue(exception);
3160040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
317d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = false;
3180040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    try {
3190040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      reducing_functions[j].call(array, should_throw_on_null_and_undefined[i]);
3200040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    } catch (e) {
321d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org      exception = true;
322fb37721ea34922d8758d5cb26ae465aaf241e6b6yangguo@chromium.org      checkExpectedMessage(e);
3230040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    }
324d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    assertTrue(exception);
3250040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
3260040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org}
3270040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
3280040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgfor (var j = 0; j < reducing_functions.length; j++) {
3290040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  for (var i = 0; i < non_generic.length; i++) {
330d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = false;
3310040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    try {
3320040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      reducing_functions[j].call(array, non_generic[i]);
3330040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    } catch (e) {
334d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org      exception = true;
3350040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      assertTrue(e instanceof TypeError);
3360040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    }
337d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    assertTrue(exception);
3380040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
339d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    exception = false;
3400040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    try {
3410040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      reducing_functions[j].call(array, non_generic[i]);
3420040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    } catch (e) {
343d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org      exception = true;
3440040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org      assertTrue(e instanceof TypeError);
3450040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org    }
346d2be901879306d8ff27e78e37783028d581d46fcricow@chromium.org    assertTrue(exception);
3470040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org  }
3480040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org}
3490040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
3500040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
3510040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org// Object.prototype.toString()
3520040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgassertEquals(Object.prototype.toString.call(null),
3530040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org             '[object Null]')
3540040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org
3550040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.orgassertEquals(Object.prototype.toString.call(undefined),
3560040ca93e7ca9c0968f7bc27088ce045cb32c402lrn@chromium.org             '[object Undefined]')
357