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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// ES7 draft (January 21, 2016) 19.2.3.2 Function.prototype.bind
function FunctionBind(thisArg, ...boundArgs) {
// Step 1.
var target = this;
// Step 2.
if (!IsCallable(target))
ThrowTypeError(JSMSG_INCOMPATIBLE_PROTO, "Function", "bind", target);
// Step 3 (implicit).
// Step 4.
var F;
var argCount = boundArgs.length;
switch (argCount) {
case 0:
F = bind_bindFunction0(target, thisArg, boundArgs);
break;
case 1:
F = bind_bindFunction1(target, thisArg, boundArgs);
break;
case 2:
F = bind_bindFunction2(target, thisArg, boundArgs);
break;
default:
F = bind_bindFunctionN(target, thisArg, boundArgs);
}
// Steps 5-11.
_FinishBoundFunctionInit(F, target, argCount);
// Ensure that the apply intrinsic has been cloned so it can be baked into
// JIT code.
void std_Function_apply;
// Step 12.
return F;
}
/**
* bind_bindFunction{0,1,2} are special cases of the generic bind_bindFunctionN
* below. They avoid the need to merge the lists of bound arguments and call
* arguments to the bound function into a new list which is then used in a
* destructuring call of the bound function.
*
* All three of these functions again have special-cases for call argument
* counts between 0 and 5. For calls with 6+ arguments, all - bound and call -
* arguments are copied into an array before invoking the generic call and
* construct helper functions. This avoids having to use rest parameters and
* destructuring in the fast path.
*
* Directly embedding the for-loop to combine bound and call arguments may
* inhibit inlining of the bound function, so we use a separate combiner
* function to perform this task. This combiner function is created lazily to
* ensure we only pay its construction cost when needed.
*
* All bind_bindFunction{X} functions have the same signature to enable simple
* reading out of closed-over state by debugging functions.
*/
function bind_bindFunction0(fun, thisArg, boundArgs) {
return function bound() {
// Ensure we allocate a call-object slot for |boundArgs|, so the
// debugger can access this value.
if (false) void boundArgs;
var newTarget;
if (_IsConstructing()) {
newTarget = new.target;
if (newTarget === bound)
newTarget = fun;
switch (arguments.length) {
case 0:
return constructContentFunction(fun, newTarget);
case 1:
return constructContentFunction(fun, newTarget, SPREAD(arguments, 1));
case 2:
return constructContentFunction(fun, newTarget, SPREAD(arguments, 2));
case 3:
return constructContentFunction(fun, newTarget, SPREAD(arguments, 3));
case 4:
return constructContentFunction(fun, newTarget, SPREAD(arguments, 4));
case 5:
return constructContentFunction(fun, newTarget, SPREAD(arguments, 5));
default:
var args = FUN_APPLY(bind_mapArguments, null, arguments);
return bind_constructFunctionN(fun, newTarget, args);
}
} else {
switch (arguments.length) {
case 0:
return callContentFunction(fun, thisArg);
case 1:
return callContentFunction(fun, thisArg, SPREAD(arguments, 1));
case 2:
return callContentFunction(fun, thisArg, SPREAD(arguments, 2));
case 3:
return callContentFunction(fun, thisArg, SPREAD(arguments, 3));
case 4:
return callContentFunction(fun, thisArg, SPREAD(arguments, 4));
case 5:
return callContentFunction(fun, thisArg, SPREAD(arguments, 5));
default:
return FUN_APPLY(fun, thisArg, arguments);
}
}
};
}
function bind_bindFunction1(fun, thisArg, boundArgs) {
var bound1 = boundArgs[0];
var combiner = null;
return function bound() {
// Ensure we allocate a call-object slot for |boundArgs|, so the
// debugger can access this value.
if (false) void boundArgs;
var newTarget;
if (_IsConstructing()) {
newTarget = new.target;
if (newTarget === bound)
newTarget = fun;
switch (arguments.length) {
case 0:
return constructContentFunction(fun, newTarget, bound1);
case 1:
return constructContentFunction(fun, newTarget, bound1, SPREAD(arguments, 1));
case 2:
return constructContentFunction(fun, newTarget, bound1, SPREAD(arguments, 2));
case 3:
return constructContentFunction(fun, newTarget, bound1, SPREAD(arguments, 3));
case 4:
return constructContentFunction(fun, newTarget, bound1, SPREAD(arguments, 4));
case 5:
return constructContentFunction(fun, newTarget, bound1, SPREAD(arguments, 5));
}
} else {
switch (arguments.length) {
case 0:
return callContentFunction(fun, thisArg, bound1);
case 1:
return callContentFunction(fun, thisArg, bound1, SPREAD(arguments, 1));
case 2:
return callContentFunction(fun, thisArg, bound1, SPREAD(arguments, 2));
case 3:
return callContentFunction(fun, thisArg, bound1, SPREAD(arguments, 3));
case 4:
return callContentFunction(fun, thisArg, bound1, SPREAD(arguments, 4));
case 5:
return callContentFunction(fun, thisArg, bound1, SPREAD(arguments, 5));
}
}
if (combiner === null) {
combiner = function() {
var callArgsCount = arguments.length;
var args = std_Array(1 + callArgsCount);
_DefineDataProperty(args, 0, bound1);
for (var i = 0; i < callArgsCount; i++)
_DefineDataProperty(args, i + 1, arguments[i]);
return args;
};
}
var args = FUN_APPLY(combiner, null, arguments);
if (newTarget === undefined)
return bind_applyFunctionN(fun, thisArg, args);
return bind_constructFunctionN(fun, newTarget, args);
};
}
function bind_bindFunction2(fun, thisArg, boundArgs) {
var bound1 = boundArgs[0];
var bound2 = boundArgs[1];
var combiner = null;
return function bound() {
// Ensure we allocate a call-object slot for |boundArgs|, so the
// debugger can access this value.
if (false) void boundArgs;
var newTarget;
if (_IsConstructing()) {
newTarget = new.target;
if (newTarget === bound)
newTarget = fun;
switch (arguments.length) {
case 0:
return constructContentFunction(fun, newTarget, bound1, bound2);
case 1:
return constructContentFunction(fun, newTarget, bound1, bound2, SPREAD(arguments, 1));
case 2:
return constructContentFunction(fun, newTarget, bound1, bound2, SPREAD(arguments, 2));
case 3:
return constructContentFunction(fun, newTarget, bound1, bound2, SPREAD(arguments, 3));
case 4:
return constructContentFunction(fun, newTarget, bound1, bound2, SPREAD(arguments, 4));
case 5:
return constructContentFunction(fun, newTarget, bound1, bound2, SPREAD(arguments, 5));
}
} else {
switch (arguments.length) {
case 0:
return callContentFunction(fun, thisArg, bound1, bound2);
case 1:
return callContentFunction(fun, thisArg, bound1, bound2, SPREAD(arguments, 1));
case 2:
return callContentFunction(fun, thisArg, bound1, bound2, SPREAD(arguments, 2));
case 3:
return callContentFunction(fun, thisArg, bound1, bound2, SPREAD(arguments, 3));
case 4:
return callContentFunction(fun, thisArg, bound1, bound2, SPREAD(arguments, 4));
case 5:
return callContentFunction(fun, thisArg, bound1, bound2, SPREAD(arguments, 5));
}
}
if (combiner === null) {
combiner = function() {
var callArgsCount = arguments.length;
var args = std_Array(2 + callArgsCount);
_DefineDataProperty(args, 0, bound1);
_DefineDataProperty(args, 1, bound2);
for (var i = 0; i < callArgsCount; i++)
_DefineDataProperty(args, i + 2, arguments[i]);
return args;
};
}
var args = FUN_APPLY(combiner, null, arguments);
if (newTarget === undefined)
return bind_applyFunctionN(fun, thisArg, args);
return bind_constructFunctionN(fun, newTarget, args);
};
}
function bind_bindFunctionN(fun, thisArg, boundArgs) {
assert(boundArgs.length > 2, "Fast paths should be used for few-bound-args cases.");
var combiner = null;
return function bound() {
var newTarget;
if (_IsConstructing()) {
newTarget = new.target;
if (newTarget === bound)
newTarget = fun;
}
if (arguments.length === 0) {
if (newTarget !== undefined)
return bind_constructFunctionN(fun, newTarget, boundArgs);
return bind_applyFunctionN(fun, thisArg, boundArgs);
}
if (combiner === null) {
combiner = function() {
var boundArgsCount = boundArgs.length;
var callArgsCount = arguments.length;
var args = std_Array(boundArgsCount + callArgsCount);
for (var i = 0; i < boundArgsCount; i++)
_DefineDataProperty(args, i, boundArgs[i]);
for (var i = 0; i < callArgsCount; i++)
_DefineDataProperty(args, i + boundArgsCount, arguments[i]);
return args;
};
}
var args = FUN_APPLY(combiner, null, arguments);
if (newTarget !== undefined)
return bind_constructFunctionN(fun, newTarget, args);
return bind_applyFunctionN(fun, thisArg, args);
};
}
function bind_mapArguments() {
var len = arguments.length;
var args = std_Array(len);
for (var i = 0; i < len; i++)
_DefineDataProperty(args, i, arguments[i]);
return args;
}
function bind_applyFunctionN(fun, thisArg, args) {
switch (args.length) {
case 0:
return callContentFunction(fun, thisArg);
case 1:
return callContentFunction(fun, thisArg, SPREAD(args, 1));
case 2:
return callContentFunction(fun, thisArg, SPREAD(args, 2));
case 3:
return callContentFunction(fun, thisArg, SPREAD(args, 3));
case 4:
return callContentFunction(fun, thisArg, SPREAD(args, 4));
case 5:
return callContentFunction(fun, thisArg, SPREAD(args, 5));
case 6:
return callContentFunction(fun, thisArg, SPREAD(args, 6));
case 7:
return callContentFunction(fun, thisArg, SPREAD(args, 7));
case 8:
return callContentFunction(fun, thisArg, SPREAD(args, 8));
case 9:
return callContentFunction(fun, thisArg, SPREAD(args, 9));
default:
return FUN_APPLY(fun, thisArg, args);
}
}
function bind_constructFunctionN(fun, newTarget, args) {
switch (args.length) {
case 1:
return constructContentFunction(fun, newTarget, SPREAD(args, 1));
case 2:
return constructContentFunction(fun, newTarget, SPREAD(args, 2));
case 3:
return constructContentFunction(fun, newTarget, SPREAD(args, 3));
case 4:
return constructContentFunction(fun, newTarget, SPREAD(args, 4));
case 5:
return constructContentFunction(fun, newTarget, SPREAD(args, 5));
case 6:
return constructContentFunction(fun, newTarget, SPREAD(args, 6));
case 7:
return constructContentFunction(fun, newTarget, SPREAD(args, 7));
case 8:
return constructContentFunction(fun, newTarget, SPREAD(args, 8));
case 9:
return constructContentFunction(fun, newTarget, SPREAD(args, 9));
case 10:
return constructContentFunction(fun, newTarget, SPREAD(args, 10));
case 11:
return constructContentFunction(fun, newTarget, SPREAD(args, 11));
case 12:
return constructContentFunction(fun, newTarget, SPREAD(args, 12));
default:
assert(args.length !== 0,
"bound function construction without args should be handled by caller");
return _ConstructFunction(fun, newTarget, args);
}
}
|