OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 22 matching lines...) Expand all Loading... |
33 // Simple tests. | 33 // Simple tests. |
34 function foo(x, y, z) { | 34 function foo(x, y, z) { |
35 return [this, arguments.length, x]; | 35 return [this, arguments.length, x]; |
36 } | 36 } |
37 | 37 |
38 assertEquals(3, foo.length); | 38 assertEquals(3, foo.length); |
39 | 39 |
40 var f = foo.bind(foo); | 40 var f = foo.bind(foo); |
41 assertEquals([foo, 3, 1], f(1, 2, 3)); | 41 assertEquals([foo, 3, 1], f(1, 2, 3)); |
42 assertEquals(3, f.length); | 42 assertEquals(3, f.length); |
43 assertEquals("function () { [native code] }", f.toString()); | |
44 | 43 |
45 f = foo.bind(foo, 1); | 44 f = foo.bind(foo, 1); |
46 assertEquals([foo, 3, 1], f(2, 3)); | 45 assertEquals([foo, 3, 1], f(2, 3)); |
47 assertEquals(2, f.length); | 46 assertEquals(2, f.length); |
48 assertEquals("function () { [native code] }", f.toString()); | |
49 | 47 |
50 f = foo.bind(foo, 1, 2); | 48 f = foo.bind(foo, 1, 2); |
51 assertEquals([foo, 3, 1], f(3)); | 49 assertEquals([foo, 3, 1], f(3)); |
52 assertEquals(1, f.length); | 50 assertEquals(1, f.length); |
53 assertEquals("function () { [native code] }", f.toString()); | |
54 | 51 |
55 f = foo.bind(foo, 1, 2, 3); | 52 f = foo.bind(foo, 1, 2, 3); |
56 assertEquals([foo, 3, 1], f()); | 53 assertEquals([foo, 3, 1], f()); |
57 assertEquals(0, f.length); | 54 assertEquals(0, f.length); |
58 assertEquals("function () { [native code] }", f.toString()); | |
59 | 55 |
60 // Test that length works correctly even if more than the actual number | 56 // Test that length works correctly even if more than the actual number |
61 // of arguments are given when binding. | 57 // of arguments are given when binding. |
62 f = foo.bind(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9); | 58 f = foo.bind(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9); |
63 assertEquals([foo, 9, 1], f()); | 59 assertEquals([foo, 9, 1], f()); |
64 assertEquals(0, f.length); | 60 assertEquals(0, f.length); |
65 assertEquals("function () { [native code] }", f.toString()); | |
66 | 61 |
67 // Use a different bound object. | 62 // Use a different bound object. |
68 var obj = {x: 42, y: 43}; | 63 var obj = {x: 42, y: 43}; |
69 // Values that would normally be in "this" when calling f_bound_this. | 64 // Values that would normally be in "this" when calling f_bound_this. |
70 var x = 42; | 65 var x = 42; |
71 var y = 44; | 66 var y = 44; |
72 | 67 |
73 function f_bound_this(z) { | 68 function f_bound_this(z) { |
74 return z + this.y - this.x; | 69 return z + this.y - this.x; |
75 } | 70 } |
76 | 71 |
77 assertEquals(3, f_bound_this(1)) | 72 assertEquals(3, f_bound_this(1)) |
78 f = f_bound_this.bind(obj); | 73 f = f_bound_this.bind(obj); |
79 assertEquals(2, f(1)); | 74 assertEquals(2, f(1)); |
80 assertEquals(1, f.length); | 75 assertEquals(1, f.length); |
81 | 76 |
82 f = f_bound_this.bind(obj, 2); | 77 f = f_bound_this.bind(obj, 2); |
83 assertEquals(3, f()); | 78 assertEquals(3, f()); |
84 assertEquals(0, f.length); | 79 assertEquals(0, f.length); |
85 assertEquals('[object Function]', Object.prototype.toString.call(f)); | |
86 | 80 |
87 // Test chained binds. | 81 // Test chained binds. |
88 | 82 |
89 // When only giving the thisArg, any number of binds should have | 83 // When only giving the thisArg, any number of binds should have |
90 // the same effect. | 84 // the same effect. |
91 f = foo.bind(foo); | 85 f = foo.bind(foo); |
92 assertEquals([foo, 3, 1], f(1, 2, 3)); | 86 assertEquals([foo, 3, 1], f(1, 2, 3)); |
93 | 87 |
94 var not_foo = {}; | 88 var not_foo = {}; |
95 f = foo.bind(foo).bind(not_foo).bind(not_foo).bind(not_foo); | 89 f = foo.bind(foo).bind(not_foo).bind(not_foo).bind(not_foo); |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 var bound = fun.bind({}); | 311 var bound = fun.bind({}); |
318 assertEquals(proto, Object.getPrototypeOf(bound)); | 312 assertEquals(proto, Object.getPrototypeOf(bound)); |
319 | 313 |
320 var bound2 = fun.bind({}); | 314 var bound2 = fun.bind({}); |
321 assertTrue(%HaveSameMap(new bound, new bound2)); | 315 assertTrue(%HaveSameMap(new bound, new bound2)); |
322 | 316 |
323 Object.setPrototypeOf(fun, null); | 317 Object.setPrototypeOf(fun, null); |
324 bound = Function.prototype.bind.call(fun, {}); | 318 bound = Function.prototype.bind.call(fun, {}); |
325 assertEquals(null, Object.getPrototypeOf(bound)); | 319 assertEquals(null, Object.getPrototypeOf(bound)); |
326 })(); | 320 })(); |
OLD | NEW |