OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Flags: --harmony-classes --allow-natives-syntax | 5 // Flags: --harmony-classes --allow-natives-syntax |
6 | 6 |
7 | 7 |
8 // Function.prototype.toMethod was removed from the spec so this test | |
adamk
2015/02/10 21:27:29
I don't think this comment is helpful. git blame w
| |
9 // was modified to call the runtime function directly. | |
10 | |
11 | |
8 (function TestSingleClass() { | 12 (function TestSingleClass() { |
9 function f(x) { | 13 function f(x) { |
10 var a = [0, 1, 2] | 14 var a = [0, 1, 2] |
11 return a[x]; | 15 return a[x]; |
12 } | 16 } |
13 | 17 |
14 function ClassD() { } | 18 function ClassD() { } |
15 | 19 |
16 assertEquals(1, f(1)); | 20 assertEquals(1, f(1)); |
17 var g = f.toMethod(ClassD.prototype); | 21 var g = %ToMethod(f, ClassD.prototype); |
18 assertEquals(1, g(1)); | 22 assertEquals(1, g(1)); |
19 assertEquals(undefined, f[%HomeObjectSymbol()]); | 23 assertEquals(undefined, f[%HomeObjectSymbol()]); |
20 assertEquals(ClassD.prototype, g[%HomeObjectSymbol()]); | 24 assertEquals(ClassD.prototype, g[%HomeObjectSymbol()]); |
21 }()); | 25 }()); |
22 | 26 |
23 | 27 |
24 (function TestClassHierarchy() { | 28 (function TestClassHierarchy() { |
25 function f(x) { | 29 function f(x) { |
26 return function g(y) { x++; return x + y; }; | 30 return function g(y) { x++; return x + y; }; |
27 } | 31 } |
28 | 32 |
29 function Base() {} | 33 function Base() {} |
30 function Derived() { } | 34 function Derived() { } |
31 Derived.prototype = Object.create(Base.prototype); | 35 Derived.prototype = Object.create(Base.prototype); |
32 | 36 |
33 var q = f(0); | 37 var q = f(0); |
34 assertEquals(2, q(1)); | 38 assertEquals(2, q(1)); |
35 assertEquals(3, q(1)); | 39 assertEquals(3, q(1)); |
36 var g = q.toMethod(Derived.prototype); | 40 var g = %ToMethod(q, Derived.prototype); |
37 assertFalse(g === q); | 41 assertFalse(g === q); |
38 assertEquals(4, g(1)); | 42 assertEquals(4, g(1)); |
39 assertEquals(5, q(1)); | 43 assertEquals(5, q(1)); |
40 }()); | 44 }()); |
41 | 45 |
42 | 46 |
43 (function TestErrorCases() { | |
44 var sFun = Function.prototype.toMethod; | |
45 assertThrows(function() { sFun.call({}); }, TypeError); | |
46 assertThrows(function() { sFun.call({}, {}); }, TypeError); | |
47 function f(){}; | |
48 assertThrows(function() { f.toMethod(1); }, TypeError); | |
49 }()); | |
50 | |
51 | |
52 (function TestPrototypeChain() { | 47 (function TestPrototypeChain() { |
53 var o = {}; | 48 var o = {}; |
54 var o1 = {}; | 49 var o1 = {}; |
55 function f() { } | 50 function f() { } |
56 | 51 |
57 function g() { } | 52 function g() { } |
58 | 53 |
59 var fMeth = f.toMethod(o); | 54 var fMeth = %ToMethod(f, o); |
60 assertEquals(o, fMeth[%HomeObjectSymbol()]); | 55 assertEquals(o, fMeth[%HomeObjectSymbol()]); |
61 g.__proto__ = fMeth; | 56 g.__proto__ = fMeth; |
62 assertEquals(undefined, g[%HomeObjectSymbol()]); | 57 assertEquals(undefined, g[%HomeObjectSymbol()]); |
63 var gMeth = g.toMethod(o1); | 58 var gMeth = %ToMethod(g, o1); |
64 assertEquals(fMeth, gMeth.__proto__); | 59 assertEquals(fMeth, gMeth.__proto__); |
65 assertEquals(o, fMeth[%HomeObjectSymbol()]); | 60 assertEquals(o, fMeth[%HomeObjectSymbol()]); |
66 assertEquals(o1, gMeth[%HomeObjectSymbol()]); | 61 assertEquals(o1, gMeth[%HomeObjectSymbol()]); |
67 }()); | 62 }()); |
68 | 63 |
69 | 64 |
70 (function TestBoundFunction() { | 65 (function TestBoundFunction() { |
71 var o = {}; | 66 var o = {}; |
72 var p = {}; | 67 var p = {}; |
73 | 68 |
74 | 69 |
75 function f(x, y, z, w) { | 70 function f(x, y, z, w) { |
76 assertEquals(o, this); | 71 assertEquals(o, this); |
77 assertEquals(1, x); | 72 assertEquals(1, x); |
78 assertEquals(2, y); | 73 assertEquals(2, y); |
79 assertEquals(3, z); | 74 assertEquals(3, z); |
80 assertEquals(4, w); | 75 assertEquals(4, w); |
81 return x+y+z+w; | 76 return x+y+z+w; |
82 } | 77 } |
83 | 78 |
84 var fBound = f.bind(o, 1, 2, 3); | 79 var fBound = f.bind(o, 1, 2, 3); |
85 var fMeth = fBound.toMethod(p); | 80 var fMeth = %ToMethod(fBound, p); |
86 assertEquals(10, fMeth(4)); | 81 assertEquals(10, fMeth(4)); |
87 assertEquals(10, fMeth.call(p, 4)); | 82 assertEquals(10, fMeth.call(p, 4)); |
88 var fBound1 = fBound.bind(o, 4); | 83 var fBound1 = fBound.bind(o, 4); |
89 assertEquals(10, fBound1()); | 84 assertEquals(10, fBound1()); |
90 var fMethBound = fMeth.bind(o, 4); | 85 var fMethBound = fMeth.bind(o, 4); |
91 assertEquals(10, fMethBound()); | 86 assertEquals(10, fMethBound()); |
92 }()); | 87 }()); |
93 | 88 |
94 (function TestOptimized() { | 89 (function TestOptimized() { |
95 function f(o) { | 90 function f(o) { |
96 return o.x; | 91 return o.x; |
97 } | 92 } |
98 var o = {x : 15}; | 93 var o = {x : 15}; |
99 assertEquals(15, f(o)); | 94 assertEquals(15, f(o)); |
100 assertEquals(15, f(o)); | 95 assertEquals(15, f(o)); |
101 %OptimizeFunctionOnNextCall(f); | 96 %OptimizeFunctionOnNextCall(f); |
102 assertEquals(15, f(o)); | 97 assertEquals(15, f(o)); |
103 var g = f.toMethod({}); | 98 var g = %ToMethod(f, {}); |
104 var o1 = {y : 1024, x : "abc"}; | 99 var o1 = {y : 1024, x : "abc"}; |
105 assertEquals("abc", f(o1)); | 100 assertEquals("abc", f(o1)); |
106 assertEquals("abc", g(o1)); | 101 assertEquals("abc", g(o1)); |
107 } ()); | 102 } ()); |
108 | 103 |
109 (function TestExtensibility() { | 104 (function TestExtensibility() { |
110 function f() {} | 105 function f() {} |
111 Object.preventExtensions(f); | 106 Object.preventExtensions(f); |
112 assertFalse(Object.isExtensible(f)); | 107 assertFalse(Object.isExtensible(f)); |
113 var m = f.toMethod({}); | 108 var m = %ToMethod(f, {}); |
114 assertTrue(Object.isExtensible(m)); | 109 assertTrue(Object.isExtensible(m)); |
115 }()); | 110 }()); |
OLD | NEW |