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()); |
43 | 44 |
44 f = foo.bind(foo, 1); | 45 f = foo.bind(foo, 1); |
45 assertEquals([foo, 3, 1], f(2, 3)); | 46 assertEquals([foo, 3, 1], f(2, 3)); |
46 assertEquals(2, f.length); | 47 assertEquals(2, f.length); |
| 48 assertEquals("function () { [native code] }", f.toString()); |
47 | 49 |
48 f = foo.bind(foo, 1, 2); | 50 f = foo.bind(foo, 1, 2); |
49 assertEquals([foo, 3, 1], f(3)); | 51 assertEquals([foo, 3, 1], f(3)); |
50 assertEquals(1, f.length); | 52 assertEquals(1, f.length); |
| 53 assertEquals("function () { [native code] }", f.toString()); |
51 | 54 |
52 f = foo.bind(foo, 1, 2, 3); | 55 f = foo.bind(foo, 1, 2, 3); |
53 assertEquals([foo, 3, 1], f()); | 56 assertEquals([foo, 3, 1], f()); |
54 assertEquals(0, f.length); | 57 assertEquals(0, f.length); |
| 58 assertEquals("function () { [native code] }", f.toString()); |
55 | 59 |
56 // Test that length works correctly even if more than the actual number | 60 // Test that length works correctly even if more than the actual number |
57 // of arguments are given when binding. | 61 // of arguments are given when binding. |
58 f = foo.bind(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9); | 62 f = foo.bind(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9); |
59 assertEquals([foo, 9, 1], f()); | 63 assertEquals([foo, 9, 1], f()); |
60 assertEquals(0, f.length); | 64 assertEquals(0, f.length); |
| 65 assertEquals("function () { [native code] }", f.toString()); |
61 | 66 |
62 // Use a different bound object. | 67 // Use a different bound object. |
63 var obj = {x: 42, y: 43}; | 68 var obj = {x: 42, y: 43}; |
64 // Values that would normally be in "this" when calling f_bound_this. | 69 // Values that would normally be in "this" when calling f_bound_this. |
65 var x = 42; | 70 var x = 42; |
66 var y = 44; | 71 var y = 44; |
67 | 72 |
68 function f_bound_this(z) { | 73 function f_bound_this(z) { |
69 return z + this.y - this.x; | 74 return z + this.y - this.x; |
70 } | 75 } |
71 | 76 |
72 assertEquals(3, f_bound_this(1)) | 77 assertEquals(3, f_bound_this(1)) |
73 f = f_bound_this.bind(obj); | 78 f = f_bound_this.bind(obj); |
74 assertEquals(2, f(1)); | 79 assertEquals(2, f(1)); |
75 assertEquals(1, f.length); | 80 assertEquals(1, f.length); |
76 | 81 |
77 f = f_bound_this.bind(obj, 2); | 82 f = f_bound_this.bind(obj, 2); |
78 assertEquals(3, f()); | 83 assertEquals(3, f()); |
79 assertEquals(0, f.length); | 84 assertEquals(0, f.length); |
| 85 assertEquals('[object Function]', Object.prototype.toString.call(f)); |
80 | 86 |
81 // Test chained binds. | 87 // Test chained binds. |
82 | 88 |
83 // When only giving the thisArg, any number of binds should have | 89 // When only giving the thisArg, any number of binds should have |
84 // the same effect. | 90 // the same effect. |
85 f = foo.bind(foo); | 91 f = foo.bind(foo); |
86 assertEquals([foo, 3, 1], f(1, 2, 3)); | 92 assertEquals([foo, 3, 1], f(1, 2, 3)); |
87 | 93 |
88 var not_foo = {}; | 94 var not_foo = {}; |
89 f = foo.bind(foo).bind(not_foo).bind(not_foo).bind(not_foo); | 95 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... |
311 var bound = fun.bind({}); | 317 var bound = fun.bind({}); |
312 assertEquals(proto, Object.getPrototypeOf(bound)); | 318 assertEquals(proto, Object.getPrototypeOf(bound)); |
313 | 319 |
314 var bound2 = fun.bind({}); | 320 var bound2 = fun.bind({}); |
315 assertTrue(%HaveSameMap(new bound, new bound2)); | 321 assertTrue(%HaveSameMap(new bound, new bound2)); |
316 | 322 |
317 Object.setPrototypeOf(fun, null); | 323 Object.setPrototypeOf(fun, null); |
318 bound = Function.prototype.bind.call(fun, {}); | 324 bound = Function.prototype.bind.call(fun, {}); |
319 assertEquals(null, Object.getPrototypeOf(bound)); | 325 assertEquals(null, Object.getPrototypeOf(bound)); |
320 })(); | 326 })(); |
OLD | NEW |