OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 // | |
5 // Flags: --harmony-classes --allow-natives-syntax | |
6 | |
7 | |
8 (function TestSingleClass() { | |
9 function f(x) { | |
10 var a = [0, 1, 2] | |
11 return a[x]; | |
12 } | |
13 | |
14 function ClassD() { } | |
15 | |
16 assertEquals(1, f(1)); | |
17 var g = %ToMethod(f, ClassD.prototype); | |
18 assertEquals(1, g(1)); | |
19 assertEquals(undefined, f[%HomeObjectSymbol()]); | |
20 assertEquals(ClassD.prototype, g[%HomeObjectSymbol()]); | |
21 }()); | |
22 | |
23 | |
24 (function TestClassHierarchy() { | |
25 function f(x) { | |
26 return function g(y) { x++; return x + y; }; | |
27 } | |
28 | |
29 function Base() {} | |
30 function Derived() { } | |
31 Derived.prototype = Object.create(Base.prototype); | |
32 | |
33 var q = f(0); | |
34 assertEquals(2, q(1)); | |
35 assertEquals(3, q(1)); | |
36 var g = %ToMethod(q, Derived.prototype); | |
37 assertFalse(g === q); | |
38 assertEquals(4, g(1)); | |
39 assertEquals(5, q(1)); | |
40 }()); | |
41 | |
42 | |
43 (function TestPrototypeChain() { | |
44 var o = {}; | |
45 var o1 = {}; | |
46 function f() { } | |
47 | |
48 function g() { } | |
49 | |
50 var fMeth = %ToMethod(f, o); | |
51 assertEquals(o, fMeth[%HomeObjectSymbol()]); | |
52 g.__proto__ = fMeth; | |
53 assertEquals(undefined, g[%HomeObjectSymbol()]); | |
54 var gMeth = %ToMethod(g, o1); | |
55 assertEquals(fMeth, gMeth.__proto__); | |
56 assertEquals(o, fMeth[%HomeObjectSymbol()]); | |
57 assertEquals(o1, gMeth[%HomeObjectSymbol()]); | |
58 }()); | |
59 | |
60 | |
61 (function TestBoundFunction() { | |
62 var o = {}; | |
63 var p = {}; | |
64 | |
65 | |
66 function f(x, y, z, w) { | |
67 assertEquals(o, this); | |
68 assertEquals(1, x); | |
69 assertEquals(2, y); | |
70 assertEquals(3, z); | |
71 assertEquals(4, w); | |
72 return x+y+z+w; | |
73 } | |
74 | |
75 var fBound = f.bind(o, 1, 2, 3); | |
76 var fMeth = %ToMethod(fBound, p); | |
77 assertEquals(10, fMeth(4)); | |
78 assertEquals(10, fMeth.call(p, 4)); | |
79 var fBound1 = fBound.bind(o, 4); | |
80 assertEquals(10, fBound1()); | |
81 var fMethBound = fMeth.bind(o, 4); | |
82 assertEquals(10, fMethBound()); | |
83 }()); | |
84 | |
85 (function TestOptimized() { | |
86 function f(o) { | |
87 return o.x; | |
88 } | |
89 var o = {x : 15}; | |
90 assertEquals(15, f(o)); | |
91 assertEquals(15, f(o)); | |
92 %OptimizeFunctionOnNextCall(f); | |
93 assertEquals(15, f(o)); | |
94 var g = %ToMethod(f, {}); | |
95 var o1 = {y : 1024, x : "abc"}; | |
96 assertEquals("abc", f(o1)); | |
97 assertEquals("abc", g(o1)); | |
98 } ()); | |
99 | |
100 (function TestExtensibility() { | |
101 function f() {} | |
102 Object.preventExtensions(f); | |
103 assertFalse(Object.isExtensible(f)); | |
104 var m = %ToMethod(f, {}); | |
105 assertTrue(Object.isExtensible(m)); | |
106 }()); | |
OLD | NEW |