OLD | NEW |
1 // Flags: --harmony-proxies | 1 // Flags: --harmony-proxies |
2 | 2 |
3 // Copyright 2008 the V8 project authors. All rights reserved. | 3 // Copyright 2008 the V8 project authors. All rights reserved. |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 // Getters. | 32 // Getters. |
33 | 33 |
34 function TestGet(handler) { | 34 function TestGet(handler) { |
35 var o = Proxy.create(handler) | 35 var o = Proxy.create(handler) |
36 assertEquals(42, o.a) | 36 assertEquals(42, o.a) |
37 assertEquals(42, o["b"]) | 37 assertEquals(42, o["b"]) |
38 // assertEquals(Object.getOwnPropertyDescriptor(o, "b").value, 42) | 38 // assertEquals(Object.getOwnPropertyDescriptor(o, "b").value, 42) |
39 } | 39 } |
40 | 40 |
41 TestGet({ | 41 TestGet({get: function(r, k) { return 42 }}) |
42 get: function(r, k) { return 42 } | 42 TestGet({getPropertyDescriptor: function(k) { return {value: 42} }}) |
43 }) | 43 TestGet({getPropertyDescriptor: function(k) { return {get value() { return 42 }}
}}) |
44 TestGet({ | 44 TestGet({get: undefined, getPropertyDescriptor: function(k) { return {value: 42}
}}) |
45 get: function(r, k) { return this.get2(r, k) }, | |
46 get2: function(r, k) { return 42 } | |
47 }) | |
48 TestGet({ | |
49 getPropertyDescriptor: function(k) { return {value: 42} } | |
50 }) | |
51 TestGet({ | |
52 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) }, | |
53 getPropertyDescriptor2: function(k) { return {value: 42} } | |
54 }) | |
55 TestGet({ | |
56 getPropertyDescriptor: function(k) { | |
57 return {get value() { return 42 }} | |
58 } | |
59 }) | |
60 TestGet({ | |
61 get: undefined, | |
62 getPropertyDescriptor: function(k) { return {value: 42} } | |
63 }) | |
64 | 45 |
65 TestGet(Proxy.create({ | 46 TestGet(Proxy.create({get: function(pr, pk) { return function(r, k) { return 42
} }})) |
66 get: function(pr, pk) { | |
67 return function(r, k) { return 42 } | |
68 } | |
69 })) | |
70 | 47 |
71 | 48 |
72 | 49 |
73 // Setters. | 50 // Setters. |
74 | 51 |
75 var key | 52 var key |
76 var val | 53 var val |
77 function TestSet(handler) { | 54 function TestSet(handler) { |
78 var o = Proxy.create(handler) | 55 var o = Proxy.create(handler) |
79 assertEquals(42, o.a = 42) | 56 assertEquals(42, o.a = 42) |
80 assertEquals("a", key) | 57 assertEquals("a", key) |
81 assertEquals(42, val) | 58 assertEquals(42, val) |
82 assertEquals(43, o["b"] = 43) | 59 assertEquals(43, o["b"] = 43) |
83 assertEquals("b", key) | 60 assertEquals("b", key) |
84 assertEquals(43, val) | 61 assertEquals(43, val) |
85 // assertTrue(Object.defineProperty(o, "c", {value: 44})) | 62 // assertTrue(Object.defineProperty(o, "c", {value: 44})) |
86 // assertEquals("c", key) | 63 // assertEquals("c", key) |
87 // assertEquals(44, val) | 64 // assertEquals(44, val) |
88 } | 65 } |
89 | 66 |
90 TestSet({ | 67 TestSet({set: function(r, k, v) { key = k; val = v; return true }}) |
91 set: function(r, k, v) { key = k; val = v; return true } | 68 TestSet({getOwnPropertyDescriptor: function(k) { return {writable: true} }, |
92 }) | 69 defineProperty: function(k, desc) { key = k, val = desc.value }}) |
93 TestSet({ | 70 TestSet({getOwnPropertyDescriptor: function(k) { return {get writable() { return
true }} }, |
94 set: function(r, k, v) { return this.set2(r, k, v) }, | 71 defineProperty: function(k, desc) { key = k, val = desc.value }}) |
95 set2: function(r, k, v) { key = k; val = v; return true } | 72 TestSet({getOwnPropertyDescriptor: function(k) { return {set: function(v) { key
= k, val = v }} }}) |
96 }) | 73 TestSet({getOwnPropertyDescriptor: function(k) { return null }, |
97 TestSet({ | 74 getPropertyDescriptor: function(k) { return {writable: true} }, |
98 getOwnPropertyDescriptor: function(k) { return {writable: true} }, | 75 defineProperty: function(k, desc) { key = k, val = desc.value }}) |
99 defineProperty: function(k, desc) { key = k; val = desc.value } | 76 TestSet({getOwnPropertyDescriptor: function(k) { return null }, |
100 }) | 77 getPropertyDescriptor: function(k) { return {get writable() { return tr
ue }} }, |
101 TestSet({ | 78 defineProperty: function(k, desc) { key = k, val = desc.value }}) |
102 getOwnPropertyDescriptor: function(k) { | 79 TestSet({getOwnPropertyDescriptor: function(k) { return null }, |
103 return this.getOwnPropertyDescriptor2(k) | 80 getPropertyDescriptor: function(k) { return {set: function(v) { key = k
, val = v }} }}) |
104 }, | 81 TestSet({getOwnPropertyDescriptor: function(k) { return null }, |
105 getOwnPropertyDescriptor2: function(k) { return {writable: true} }, | 82 getPropertyDescriptor: function(k) { return null }, |
106 defineProperty: function(k, desc) { this.defineProperty2(k, desc) }, | 83 defineProperty: function(k, desc) { key = k, val = desc.value }}) |
107 defineProperty2: function(k, desc) { key = k; val = desc.value } | |
108 }) | |
109 TestSet({ | |
110 getOwnPropertyDescriptor: function(k) { | |
111 return {get writable() { return true }} | |
112 }, | |
113 defineProperty: function(k, desc) { key = k; val = desc.value } | |
114 }) | |
115 TestSet({ | |
116 getOwnPropertyDescriptor: function(k) { | |
117 return {set: function(v) { key = k; val = v }} | |
118 } | |
119 }) | |
120 TestSet({ | |
121 getOwnPropertyDescriptor: function(k) { return null }, | |
122 getPropertyDescriptor: function(k) { return {writable: true} }, | |
123 defineProperty: function(k, desc) { key = k; val = desc.value } | |
124 }) | |
125 TestSet({ | |
126 getOwnPropertyDescriptor: function(k) { return null }, | |
127 getPropertyDescriptor: function(k) { | |
128 return {get writable() { return true }} | |
129 }, | |
130 defineProperty: function(k, desc) { key = k; val = desc.value } | |
131 }) | |
132 TestSet({ | |
133 getOwnPropertyDescriptor: function(k) { return null }, | |
134 getPropertyDescriptor: function(k) { | |
135 return {set: function(v) { key = k; val = v }} | |
136 } | |
137 }) | |
138 TestSet({ | |
139 getOwnPropertyDescriptor: function(k) { return null }, | |
140 getPropertyDescriptor: function(k) { return null }, | |
141 defineProperty: function(k, desc) { key = k, val = desc.value } | |
142 }) | |
143 | 84 |
144 TestSet(Proxy.create({ | 85 TestSet(Proxy.create({get: function(pr, pk) { return function(r, k, v) { key = k
; val = v; return true } }})) |
145 get: function(pr, pk) { | |
146 return function(r, k, v) { key = k; val = v; return true } | |
147 } | |
148 })) | |
149 | 86 |
150 | 87 |
151 | 88 |
152 // Comparison. | 89 // Comparison. |
153 | 90 |
154 function TestComparison(eq) { | 91 var o1 = Proxy.create({}) |
155 var o1 = Proxy.create({}) | 92 var o2 = Proxy.create({}) |
156 var o2 = Proxy.create({}) | |
157 | 93 |
158 assertTrue(eq(o1, o1)) | 94 assertTrue(o1 == o1) |
159 assertTrue(eq(o2, o2)) | 95 assertTrue(o2 == o2) |
160 assertTrue(!eq(o1, o2)) | 96 assertTrue(!(o1 == o2)) |
161 assertTrue(!eq(o1, {})) | 97 assertTrue(!(o1 == {})) |
162 assertTrue(!eq({}, o2)) | 98 assertTrue(!({} == o2)) |
163 assertTrue(!eq({}, {})) | 99 assertTrue(!({} == {})) |
164 } | |
165 | 100 |
166 TestComparison(function(o1, o2) { return o1 == o2 }) | 101 assertTrue(o1 === o1) |
167 TestComparison(function(o1, o2) { return o1 === o2 }) | 102 assertTrue(o2 === o2) |
168 TestComparison(function(o1, o2) { return !(o1 != o2) }) | 103 assertTrue(!(o1 === o2)) |
169 TestComparison(function(o1, o2) { return !(o1 !== o2) }) | 104 assertTrue(!(o1 === {})) |
| 105 assertTrue(!({} === o2)) |
| 106 assertTrue(!({} === {})) |
170 | 107 |
171 | 108 |
172 | 109 |
173 // Type. | 110 // Type. |
174 | 111 |
175 assertEquals("object", typeof Proxy.create({})) | 112 assertEquals("object", typeof Proxy.create({})) |
176 assertTrue(typeof Proxy.create({}) == "object") | 113 assertTrue(typeof Proxy.create({}) == "object") |
177 assertTrue("object" == typeof Proxy.create({})) | 114 assertTrue("object" == typeof Proxy.create({})) |
178 | 115 |
179 // No function proxies yet. | 116 // No function proxies yet. |
180 | |
181 | |
182 | |
183 // Instanceof (instanceof). | |
184 | |
185 function TestInstanceof() { | |
186 var o = {} | |
187 var p1 = Proxy.create({}) | |
188 var p2 = Proxy.create({}, o) | |
189 var p3 = Proxy.create({}, p2) | |
190 | |
191 var f = function() {} | |
192 f.prototype = o | |
193 var f1 = function() {} | |
194 f1.prototype = p1 | |
195 var f2 = function() {} | |
196 f2.prototype = p2 | |
197 | |
198 assertTrue(o instanceof Object) | |
199 assertFalse(o instanceof f) | |
200 assertFalse(o instanceof f1) | |
201 assertFalse(o instanceof f2) | |
202 assertFalse(p1 instanceof Object) | |
203 assertFalse(p1 instanceof f) | |
204 assertFalse(p1 instanceof f1) | |
205 assertFalse(p1 instanceof f2) | |
206 assertTrue(p2 instanceof Object) | |
207 assertTrue(p2 instanceof f) | |
208 assertFalse(p2 instanceof f1) | |
209 assertFalse(p2 instanceof f2) | |
210 assertTrue(p3 instanceof Object) | |
211 assertTrue(p3 instanceof f) | |
212 assertFalse(p3 instanceof f1) | |
213 assertTrue(p3 instanceof f2) | |
214 } | |
215 | |
216 TestInstanceof() | |
217 | |
218 | |
219 | |
220 // Prototype (Object.getPrototypeOf). | |
221 | |
222 function TestPrototype() { | |
223 var o = {} | |
224 var p1 = Proxy.create({}) | |
225 var p2 = Proxy.create({}, o) | |
226 var p3 = Proxy.create({}, p2) | |
227 var p4 = Proxy.create({}, 666) | |
228 | |
229 assertSame(Object.getPrototypeOf(o), Object.prototype) | |
230 assertSame(Object.getPrototypeOf(p1), null) | |
231 assertSame(Object.getPrototypeOf(p2), o) | |
232 assertSame(Object.getPrototypeOf(p3), p2) | |
233 assertSame(Object.getPrototypeOf(p4), null) | |
234 } | |
235 | |
236 TestPrototype() | |
237 | |
238 | |
239 | |
240 // Property names (Object.getOwnPropertyNames). | |
241 | |
242 function TestPropertyNames(names, handler) { | |
243 var p = Proxy.create(handler) | |
244 assertArrayEquals(names, Object.getOwnPropertyNames(p)) | |
245 } | |
246 | |
247 TestPropertyNames([], { | |
248 getOwnPropertyNames: function() { return [] } | |
249 }) | |
250 TestPropertyNames(["a", "zz", " ", "0"], { | |
251 getOwnPropertyNames: function() { return ["a", "zz", " ", 0] } | |
252 }) | |
253 TestPropertyNames(["throw", "function "], { | |
254 getOwnPropertyNames: function() { return this.getOwnPropertyNames2() }, | |
255 getOwnPropertyNames2: function() { return ["throw", "function "] } | |
256 }) | |
257 TestPropertyNames(["[object Object]"], { | |
258 get getOwnPropertyNames() { | |
259 return function() { return [{}] } | |
260 } | |
261 }) | |
OLD | NEW |