summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_5/Function/function-bind.js
blob: 78ee183104fe3d9879bb44f1880d9c9ab7b1abaf (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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/
 */

var gTestfile = 'function-bind.js';
var BUGNUMBER = 429507;
var summary = "ES5: Function.prototype.bind";

print(BUGNUMBER + ": " + summary);

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

// ad-hoc testing

assertEq(Function.prototype.hasOwnProperty("bind"), true);

var bind = Function.prototype.bind;
assertEq(bind.length, 1);


var strictReturnThis = function() { "use strict"; return this; };

assertEq(strictReturnThis.bind(undefined)(), undefined);
assertEq(strictReturnThis.bind(null)(), null);

var obj = {};
assertEq(strictReturnThis.bind(obj)(), obj);

assertEq(strictReturnThis.bind(NaN)(), NaN);

assertEq(strictReturnThis.bind(true)(), true);
assertEq(strictReturnThis.bind(false)(), false);

assertEq(strictReturnThis.bind("foopy")(), "foopy");


// rigorous, step-by-step testing

function expectThrowTypeError(fun)
{
  try
  {
    var r = fun();
    throw new Error("didn't throw TypeError, returned " + r);
  }
  catch (e)
  {
    assertEq(e instanceof TypeError, true,
             "didn't throw TypeError, got: " + e);
  }
}

/*
 * 1. Let Target be the this value.
 * 2. If IsCallable(Target) is false, throw a TypeError exception.
 */
expectThrowTypeError(function() { bind.call(null); });
expectThrowTypeError(function() { bind.call(undefined); });
expectThrowTypeError(function() { bind.call(NaN); });
expectThrowTypeError(function() { bind.call(0); });
expectThrowTypeError(function() { bind.call(-0); });
expectThrowTypeError(function() { bind.call(17); });
expectThrowTypeError(function() { bind.call(42); });
expectThrowTypeError(function() { bind.call("foobar"); });
expectThrowTypeError(function() { bind.call(true); });
expectThrowTypeError(function() { bind.call(false); });
expectThrowTypeError(function() { bind.call([]); });
expectThrowTypeError(function() { bind.call({}); });


/*
 * 3. Let A be a new (possibly empty) internal list of all of the argument
 *    values provided after thisArg (arg1, arg2 etc), in order.
 * 4. Let F be a new native ECMAScript object .
 * 5. Set all the internal methods, except for [[Get]], of F as specified in
 *    8.12.
 * 6. Set the [[Get]] internal property of F as specified in 15.3.5.4.
 * 7. Set the [[TargetFunction]] internal property of F to Target.
 * 8. Set the [[BoundThis]] internal property of F to the value of thisArg.
 * 9. Set the [[BoundArgs]] internal property of F to A.
 */
// throughout


/* 10. Set the [[Class]] internal property of F to "Function". */
var toString = Object.prototype.toString;
assertEq(toString.call(function(){}), "[object Function]");
assertEq(toString.call(function a(){}), "[object Function]");
assertEq(toString.call(function(a){}), "[object Function]");
assertEq(toString.call(function a(b){}), "[object Function]");
assertEq(toString.call(function(){}.bind()), "[object Function]");
assertEq(toString.call(function a(){}.bind()), "[object Function]");
assertEq(toString.call(function(a){}.bind()), "[object Function]");
assertEq(toString.call(function a(b){}.bind()), "[object Function]");


/*
 * 11. Set the [[Prototype]] internal property of F to the standard built-in
 *     Function prototype object as specified in 15.3.3.1.
 */
assertEq(Object.getPrototypeOf(bind.call(function(){})), Function.prototype);
assertEq(Object.getPrototypeOf(bind.call(function a(){})), Function.prototype);
assertEq(Object.getPrototypeOf(bind.call(function(a){})), Function.prototype);
assertEq(Object.getPrototypeOf(bind.call(function a(b){})), Function.prototype);


/*
 * 12. Set the [[Call]] internal property of F as described in 15.3.4.5.1.
 */
var a = Array.bind(1, 2);
assertEq(a().length, 2);
assertEq(a(4).length, 2);
assertEq(a(4, 8).length, 3);

function t() { return this; }
var bt = t.bind(t);
assertEq(bt(), t);

function callee() { return arguments.callee; }
var call = callee.bind();
assertEq(call(), callee);
assertEq(new call(), callee);


/*
 * 13. Set the [[Construct]] internal property of F as described in 15.3.4.5.2.
 */
function Point(x, y)
{
  this.x = x;
  this.y = y;
}
var YAxisPoint = Point.bind(null, 0)

assertEq(YAxisPoint.hasOwnProperty("prototype"), false);
var p = new YAxisPoint(5);
assertEq(p.x, 0);
assertEq(p.y, 5);
assertEq(p instanceof Point, true);
assertEq(p instanceof YAxisPoint, true);
assertEq(Object.prototype.toString.call(YAxisPoint), "[object Function]");
assertEq(YAxisPoint.length, 1);


/*
 * 14. Set the [[HasInstance]] internal property of F as described in
 *     15.3.4.5.3.
 */
function JoinArguments()
{
  this.args = Array.prototype.join.call(arguments, ", ");
}

var Join1 = JoinArguments.bind(null, 1);
var Join2 = Join1.bind(null, 2);
var Join3 = Join2.bind(null, 3);
var Join4 = Join3.bind(null, 4);
var Join5 = Join4.bind(null, 5);
var Join6 = Join5.bind(null, 6);

var r = new Join6(7);
assertEq(r instanceof Join6, true);
assertEq(r instanceof Join5, true);
assertEq(r instanceof Join4, true);
assertEq(r instanceof Join3, true);
assertEq(r instanceof Join2, true);
assertEq(r instanceof Join1, true);
assertEq(r instanceof JoinArguments, true);
assertEq(r.args, "1, 2, 3, 4, 5, 6, 7");


/*
 * 15. If the [[Class]] internal property of Target is "Function", then
 *   a. Let L be the length property of Target minus the length of A.
 *   b. Set the length own property of F to either 0 or L, whichever is larger.
 * 16. Else set the length own property of F to 0.
 */
function none() { return arguments.length; }
assertEq(none.bind(1, 2)(3, 4), 3);
assertEq(none.bind(1, 2)(), 1);
assertEq(none.bind(1)(2, 3), 2);
assertEq(none.bind().length, 0);
assertEq(none.bind(null).length, 0);
assertEq(none.bind(null, 1).length, 0);
assertEq(none.bind(null, 1, 2).length, 0);

function one(a) { }
assertEq(one.bind().length, 1);
assertEq(one.bind(null).length, 1);
assertEq(one.bind(null, 1).length, 0);
assertEq(one.bind(null, 1, 2).length, 0);

// retch
var br = Object.create(null, { length: { value: 0 } });
try
{
  br = bind.call(/a/g, /a/g, "aaaa");
}
catch (e) { /* nothing */ }
assertEq(br.length, 0);


/*
 * 17. Set the attributes of the length own property of F to the values
 *     specified in 15.3.5.1.
 */
var len1Desc =
  Object.getOwnPropertyDescriptor(function(a, b, c){}.bind(), "length");
assertEq(len1Desc.value, 3);
assertEq(len1Desc.writable, false);
assertEq(len1Desc.enumerable, false);
assertEq(len1Desc.configurable, true);

var len2Desc =
  Object.getOwnPropertyDescriptor(function(a, b, c){}.bind(null, 2), "length");
assertEq(len2Desc.value, 2);
assertEq(len2Desc.writable, false);
assertEq(len2Desc.enumerable, false);
assertEq(len2Desc.configurable, true);


/*
 * 18. Set the [[Extensible]] internal property of F to true.
 */
var bound = (function() { }).bind();

var isExtensible = Object.isExtensible || function() { return true; };
assertEq(isExtensible(bound), true);

bound.foo = 17;
var fooDesc = Object.getOwnPropertyDescriptor(bound, "foo");
assertEq(fooDesc.value, 17);
assertEq(fooDesc.writable, true);
assertEq(fooDesc.enumerable, true);
assertEq(fooDesc.configurable, true);


/*
 * Steps 19-21 are removed from ES6, instead implemented through "arguments" and
 * "caller" accessors on Function.prototype.  So no own properties, but do check
 * for the same observable behavior (modulo where the accessors live).
 */
function strict() { "use strict"; }
function nonstrict() {}

function testBound(fun)
{
  var boundf = fun.bind();

  assertEq(Object.getOwnPropertyDescriptor(boundf, "arguments"), undefined,
           "should be no arguments property");
  assertEq(Object.getOwnPropertyDescriptor(boundf, "caller"), undefined,
           "should be no caller property");

  expectThrowTypeError(function() { return boundf.arguments; });
  expectThrowTypeError(function() { return boundf.caller; });
}

testBound(strict);
testBound(nonstrict);

assertEq((function unbound(){"body"}).bind().toString(), `function bound unbound() {
    [native code]
}`);

assertEq((function unbound(){"body"}).bind().toSource(), `function bound unbound() {
    [native code]
}`);

assertEq(uneval((function unbound(){"body"}).bind()), `function bound unbound() {
    [native code]
}`);


/* 22. Return F. */
var passim = function p(){}.bind(1);
assertEq(typeof passim, "function");


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

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

print("All tests passed!");