summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/misc/builtin-methods-reject-null-undefined-this.js
blob: c6f0a8f39a7fc23cc1c1964c8c6e4be9ffaf54d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/
 */

//-----------------------------------------------------------------------------
var BUGNUMBER = 619283;
var summary =
  "ECMAScript built-in methods that immediately throw when |this| is " +
  "|undefined| or |null| (due to CheckObjectCoercible, ToObject, or ToString)";

print(BUGNUMBER + ": " + summary);

/**************
 * BEGIN TEST *
 **************/

// We can't just exhaustively loop over everything because 1) method properties
// might be extensions with special |this| handling, and 2) some methods don't
// *quite* immediately throw a TypeError, first thing, if |this| is |undefined|
// or |null|, or their algorithms are very slightly ambiguous about whether they
// do.  Why?  Ipse-dixitism.  *shrug*

var ClassToMethodMap =
  {
    Object:  [/* "toString" has special |this| handling */
              "toLocaleString", "valueOf", "hasOwnProperty",
              /*
               * "isPrototypeOf" has special |this| handling already tested in
               * non262/Object/isPrototypeOf.js.
               */
              /*
               * "isPrototypeOf" has special |this| handling already tested in
               * non262/Object/propertyIsEnumerable.js.
               */
              "__defineGetter__", "__defineSetter__",
              "__lookupGetter__", "__lookupSetter__",
              ],
    // Function methods often don't ToObject(this) as their very first step,
    // and they're already stepwise well-tested such that manual tests here
    // would be redundant.
    Array:   ["toString", "toLocaleString", "concat", "join", "pop", "push",
              "reverse", "shift", "slice", "sort", "splice", "unshift",
              "indexOf", "lastIndexOf", "every", "some", "forEach", "map",
              "filter", "reduce", "reduceRight"],
    String:  ["toString", "valueOf", "charAt", "charCodeAt", "concat",
              "indexOf", "lastIndexOf", "localeCompare", "match", "replace",
              "search", "slice", "split", "substring", "toLowerCase",
              "toLocaleLowerCase", "toUpperCase", "toLocaleUpperCase", "trim",
              "bold", "italics", "fixed", "fontsize",
              "fontcolor", "link", "anchor", "strike", "small", "big", "blink",
              "sup", "sub", "substr", "trimLeft", "trimRight",
              ],
    Boolean: ["toString", "valueOf"],
    Number:  ["toString", "toLocaleString", "valueOf",
              /*
               * toFixed doesn't *immediately* test |this| for number or
               * Number-ness, but because the ToInteger(void 0) which arguably
               * precedes it in the toFixed algorithm won't throw in this test,
               * we don't need to specially test it.
               */
              "toFixed",
              "toExponential", "toPrecision"],
    Date:    ["toDateString", "toTimeString", "toLocaleString",
              "toLocaleDateString", "toLocaleTimeString", "valueOf", "getTime",
              "getFullYear", "getUTCFullYear", "getMonth", "getUTCMonth",
              "getDate", "getUTCDate", "getDay", "getUTCDay", "getHours",
              "getUTCHours", "getMinutes", "getUTCMinutes", "getSeconds",
              "getUTCSeconds", "getMilliseconds", "getUTCMilliseconds",
              /*
               * toFixed doesn't *immediately* test |this| for number or
               * Number-ness, but because the TimeClip(ToNumber(void 0)) which
               * arguably precedes it in the setTime algorithm won't throw in
               * this test, we don't need to specially test it.
               */
              "setTime",
              "getTimezoneOffset", "setMilliseconds", "setUTCMilliseconds",
              "setSeconds", "setUTCSeconds", "setMinutes", "setUTCMinutes",
              "setHours", "setUTCHours", "setDate", "setUTCDate",  "setMonth",
              "setUTCMonth", "setFullYear", "setUTCFullYear", "toUTCString",
              "toISOString", "toJSON",
              "getYear", "setYear",  "toGMTString"],
    RegExp:  ["exec", "test", "toString"],
    Error:   ["toString"],
  };

var badThisValues = [null, undefined];

function testMethod(Class, className, method)
{
  var expr;

  // Try out explicit this values
  for (var i = 0, sz = badThisValues.length; i < sz; i++)
  {
    var badThis = badThisValues[i];

    expr = className + ".prototype." + method + ".call(" + badThis + ")";
    try
    {
      Class.prototype[method].call(badThis);
      throw new Error(expr + " didn't throw a TypeError");
    }
    catch (e)
    {
      assertEq(e instanceof TypeError, true,
               "wrong error for " + expr + ", instead threw " + e);
    }

    expr = className + ".prototype." + method + ".apply(" + badThis + ")";
    try
    {
      Class.prototype[method].apply(badThis);
      throw new Error(expr + " didn't throw a TypeError");
    }
    catch (e)
    {
      assertEq(e instanceof TypeError, true,
               "wrong error for " + expr + ", instead threw " + e);
    }
  }

  // ..and for good measure..

  expr = "(0, " + className + ".prototype." + method + ")()"
  try
  {
    // comma operator to call GetValue() on the method and de-Reference it
    (0, Class.prototype[method])();
    throw new Error(expr + " didn't throw a TypeError");
  }
  catch (e)
  {
    assertEq(e instanceof TypeError, true,
             "wrong error for " + expr + ", instead threw " + e);
  }
}

for (var className in ClassToMethodMap)
{
  var Class = this[className];

  var methodNames = ClassToMethodMap[className];
  for (var i = 0, sz = methodNames.length; i < sz; i++)
  {
    var method = methodNames[i];
    testMethod(Class, className, method);
  }
}

/******************************************************************************/

if (typeof reportCompare === "function")
  reportCompare(true, true);

print("All tests passed!");