summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_5/Function/function-call.js
blob: 756b583842d55d220abf9f2cb0d13c53fd925dc4 (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
/*
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/
 * Contributor:
 *   Jeff Walden <jwalden+code@mit.edu>
 */

//-----------------------------------------------------------------------------
var BUGNUMBER = 575535;
var summary = 'Function.prototype.call';
print(BUGNUMBER + ": " + summary);

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

function expectTypeError(fun, msg)
{
  try
  {
    fun();
    assertEq(true, false, "should have thrown a TypeError");
  }
  catch (e)
  {
    assertEq(e instanceof TypeError, true, msg + "; instead threw " + e);
  }
}

function fun() { }

var global = this;

assertEq(Function.prototype.call.length, 1);


/* Step 1. */
var nonfuns = [null, 1, -1, 2.5, "[[Call]]", undefined, true, false, {}];
for (var i = 0, sz = nonfuns.length; i < sz; i++)
{
  var f = function()
  {
    Function.prototype.call.apply(nonfuns[i]);
  };
  var msg =
    "expected TypeError calling Function.prototype.call with uncallable this";
  expectTypeError(f, msg);
}


/* Steps 2-4. */
function none()
{
  assertEq(this, global, "bad this");
  assertEq(arguments.length, 0, "wrong arguments");
}

none.call();
none.call(undefined);
none.call(null);

var seenThis;
function strictNone()
{
  "use strict";
  assertEq(this, seenThis, "bad this");
  assertEq(arguments.length, 0, "wrong arguments");
}

seenThis = undefined;
strictNone.call();
strictNone.call(undefined);

seenThis = null;
strictNone.call(null);

seenThis = 17;
strictNone.call(17);

var seenThisBox, args;
function some()
{
  assertEq(this instanceof seenThisBox, true,
           "this not instanceof " + seenThisBox);
  assertEq(this.valueOf(), seenThis,
           "wrong this valueOf()");
  assertEq(arguments.length, args.length, "wrong arguments count");
  for (var i = 0; i < args.length; i++)
    assertEq(arguments[i], args[i], "wrong argument " + i);
}

seenThis = false;
seenThisBox = Boolean;
args = [8, 6, 7, NaN, undefined, 0.3];
some.call(false, 8, 6, 7, NaN, undefined, 0.3);

var obj = {};

seenThis = "foo";
seenThisBox = String;
args = [obj];
some.call("foo", obj);

seenThis = obj;
seenThisBox = Object;
some.call(obj, obj);

function strictSome()
{
  "use strict";
  assertEq(this, seenThis, "wrong this");
  assertEq(arguments.length, args.length, "wrong arguments count");
  for (var i = 0; i < args.length; i++)
    assertEq(arguments[i], args[i], "wrong argument " + i);
}

seenThis = NaN;
args = [8, 6, 7, NaN, undefined, 0.3];
strictSome.call(NaN, 8, 6, 7, NaN, undefined, 0.3);

seenThis = "foo";
args = [obj];
strictSome.call("foo", obj);

seenThis = obj;
strictSome.call(obj, obj);


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

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

print("All tests passed!");