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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/
* Contributor:
* Jeff Walden <jwalden+code@mit.edu>
*/
//-----------------------------------------------------------------------------
var BUGNUMBER = 562448;
var summary = 'Function.prototype.apply should accept any arraylike arguments';
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;
/* 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.apply.apply(nonfuns[i], [1, 2, 3]);
};
var msg =
"expected TypeError calling Function.prototype.apply with uncallable this";
expectTypeError(f, msg);
}
/* Step 2. */
var thisObj = {};
var currentThis, currentThisBox;
function funLength()
{
assertEq(arguments.length, 0, "should have been called with no arguments");
assertEq(this, currentThis, "wrong this");
}
function strictFunLength()
{
"use strict";
assertEq(arguments.length, 0, "should have been called with no arguments");
assertEq(this, currentThis, "wrong this");
}
currentThis = global;
funLength.apply();
funLength.apply(undefined);
funLength.apply(undefined, undefined);
funLength.apply(undefined, null);
currentThis = undefined;
strictFunLength.apply();
strictFunLength.apply(undefined);
strictFunLength.apply(undefined, undefined);
strictFunLength.apply(undefined, null);
currentThis = null;
strictFunLength.apply(null);
strictFunLength.apply(null, undefined);
strictFunLength.apply(null, null);
currentThis = thisObj;
funLength.apply(thisObj);
funLength.apply(thisObj, null);
funLength.apply(thisObj, undefined);
strictFunLength.apply(thisObj);
strictFunLength.apply(thisObj, null);
strictFunLength.apply(thisObj, undefined);
currentThis = 17;
strictFunLength.apply(17);
strictFunLength.apply(17, null);
strictFunLength.apply(17, undefined);
function funThisPrimitive()
{
assertEq(arguments.length, 0, "should have been called with no arguments");
assertEq(this instanceof currentThisBox, true,
"this not instanceof " + currentThisBox);
assertEq(this.valueOf(), currentThis,
"wrong this valueOf()");
}
currentThis = 17;
currentThisBox = Number;
funThisPrimitive.apply(17);
funThisPrimitive.apply(17, undefined);
funThisPrimitive.apply(17, null);
currentThis = "foopy";
currentThisBox = String;
funThisPrimitive.apply("foopy");
funThisPrimitive.apply("foopy", undefined);
funThisPrimitive.apply("foopy", null);
currentThis = false;
currentThisBox = Boolean;
funThisPrimitive.apply(false);
funThisPrimitive.apply(false, undefined);
funThisPrimitive.apply(false, null);
/* Step 3. */
var nonobjs = [1, -1, 2.5, "[[Call]]", true, false];
for (var i = 0, sz = nonobjs.length; i < sz; i++)
{
var f = function() { fun.apply(thisObj, nonobjs[i]); };
var msg = "should have thrown a TypeError with non-object arguments";
expectTypeError(f, msg);
}
/* Step 4. */
var args = { get length() { throw 42; } };
try
{
fun.apply(thisObj, args);
}
catch (e)
{
assertEq(e, 42, "didn't throw result of [[Get]] on arguments object");
}
/*
* NB: There was an erratum removing the steps numbered 5 and 7 in the original
* version of ES5; see also the comments in js_fun_apply.
*/
/* Step 5. */
var called = false;
var argsObjectLength =
{ length: { valueOf: function() { called = true; return 17; } } };
fun.apply({}, argsObjectLength);
assertEq(called, true, "should have been set in valueOf called via ToUint32");
var upvar = "unset";
var argsObjectPrimitiveLength =
{
length:
{
valueOf: function() { upvar = "valueOf"; return {}; },
toString: function()
{
upvar = upvar === "valueOf" ? "both" : "toString";
return 17;
}
}
};
fun.apply({}, argsObjectPrimitiveLength);
assertEq(upvar, "both", "didn't call all hooks properly");
/* Step 6-9. */
var seenThis, res, steps;
var argsAccessors =
{
length: 4,
get 0() { steps.push("0"); return 1; },
get 1() { steps.push("1"); return 2; },
// make sure values shine through holes
get 3() { steps.push("3"); return 8; },
};
Object.prototype[2] = 729;
seenThis = "not seen";
function argsAsArray()
{
seenThis = this;
steps.push(Math.PI);
return Array.prototype.map.call(arguments, function(v) { return v; });
}
steps = [];
res = argsAsArray.apply(thisObj, argsAccessors);
assertEq(seenThis, thisObj, "saw wrong this");
assertEq(steps.length, 4, "wrong steps: " + steps);
assertEq(steps[0], "0", "bad step 0");
assertEq(steps[1], "1", "bad step 1");
assertEq(steps[2], "3", "bad step 3");
assertEq(steps[3], Math.PI, "bad last step");
assertEq(res.length, 4, "wrong return: " + res);
assertEq(res[0], 1, "wrong ret[0]");
assertEq(res[1], 2, "wrong ret[0]");
assertEq(res[2], 729, "wrong ret[0]");
assertEq(res[3], 8, "wrong ret[0]");
seenThis = "not seen";
function strictArgsAsArray()
{
"use strict";
seenThis = this;
steps.push(NaN);
return Array.prototype.map.call(arguments, function(v) { return v; });
}
steps = [];
res = strictArgsAsArray.apply(null, argsAccessors);
assertEq(seenThis, null, "saw wrong this");
assertEq(steps.length, 4, "wrong steps: " + steps);
assertEq(steps[0], "0", "bad step 0");
assertEq(steps[1], "1", "bad step 1");
assertEq(steps[2], "3", "bad step 3");
assertEq(steps[3], 0 / 0, "bad last step");
assertEq(res.length, 4, "wrong return: " + res);
assertEq(res[0], 1, "wrong ret[0]");
assertEq(res[1], 2, "wrong ret[0]");
assertEq(res[2], 729, "wrong ret[0]");
assertEq(res[3], 8, "wrong ret[0]");
strictArgsAsArray.apply(17, argsAccessors);
assertEq(seenThis, 17, "saw wrong this");
/******************************************************************************/
if (typeof reportCompare === "function")
reportCompare(true, true);
print("All tests passed!");
|