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
|
function testNewTargetGuard() {
var weirdNewTarget = function() {};
var fun = function() { return new.target; };
var boundFun = fun.bind(null);
for (var i = 0; i < 60; i++) {
var newTarget = i < 40 ? boundFun : weirdNewTarget;
var res = Reflect.construct(boundFun, [], newTarget);
assertEq(res, i < 40 ? fun : weirdNewTarget);
}
}
testNewTargetGuard();
function testPrototypeGuard() {
var fun = function() {};
var boundFun = fun.bind(null);
var customPrototype1 = {};
var customPrototype2 = {};
fun.prototype = customPrototype1;
for (var i = 0; i < 60; i++) {
if (i === 40) {
fun.prototype = customPrototype2;
}
var res = new boundFun();
assertEq(Object.getPrototypeOf(res), i < 40 ? customPrototype1 : customPrototype2);
}
}
testPrototypeGuard();
function testNonObjectPrototypeGuard() {
var fun = function() {};
var boundFun = fun.bind(null);
fun.prototype = null;
var customPrototype = {};
for (var i = 0; i < 60; i++) {
if (i === 40) {
fun.prototype = customPrototype;
}
var res = new boundFun();
assertEq(Object.getPrototypeOf(res), i < 40 ? Object.prototype : customPrototype);
}
}
testNonObjectPrototypeGuard();
function testObjectReturnValue() {
var fun = function() { return Math; };
var boundFun = fun.bind(null);
for (var i = 0; i < 60; i++) {
var res = new boundFun();
assertEq(res, Math);
}
}
testObjectReturnValue();
function testManyArgs() {
var fun = function(a, b, c, d, e, f, g, h, i, j) {
this.values = [a, b, c, d, e, f, g, h, i, j].join(",");
};
var boundFun1 = fun.bind(null, 1, 2);
var boundFun2 = fun.bind(null, 1, 2, 3, 4, 5, 6);
for (var i = 0; i < 60; i++) {
assertEq(new boundFun1().values, "1,2,,,,,,,,");
assertEq(new boundFun1(10, 11, 12, 13, 14).values, "1,2,10,11,12,13,14,,,");
assertEq(new boundFun1(10, 11, 12, 13, 14, 15, 16, 17).values, "1,2,10,11,12,13,14,15,16,17");
assertEq(new boundFun2().values, "1,2,3,4,5,6,,,,");
assertEq(new boundFun2(10, 11).values, "1,2,3,4,5,6,10,11,,");
assertEq(new boundFun2(10, 11, 12, 13, 14, 15, 16, 17).values, "1,2,3,4,5,6,10,11,12,13");
}
}
testManyArgs();
|