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({get: function(r, k) { return 42 }}) | 41 TestGet({ |
42 TestGet({getPropertyDescriptor: function(k) { return {value: 42} }}) | 42 get: function(r, k) { return 42 } |
43 TestGet({getPropertyDescriptor: function(k) { return {get value() { return 42 }}
}}) | 43 }) |
44 TestGet({get: undefined, getPropertyDescriptor: function(k) { return {value: 42}
}}) | 44 TestGet({ |
| 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 }) |
45 | 64 |
46 TestGet(Proxy.create({get: function(pr, pk) { return function(r, k) { return 42
} }})) | 65 TestGet(Proxy.create({ |
| 66 get: function(pr, pk) { |
| 67 return function(r, k) { return 42 } |
| 68 } |
| 69 })) |
47 | 70 |
48 | 71 |
49 | 72 |
50 // Setters. | 73 // Setters. |
51 | 74 |
52 var key | 75 var key |
53 var val | 76 var val |
54 function TestSet(handler) { | 77 function TestSet(handler) { |
55 var o = Proxy.create(handler) | 78 var o = Proxy.create(handler) |
56 assertEquals(42, o.a = 42) | 79 assertEquals(42, o.a = 42) |
57 assertEquals("a", key) | 80 assertEquals("a", key) |
58 assertEquals(42, val) | 81 assertEquals(42, val) |
59 assertEquals(43, o["b"] = 43) | 82 assertEquals(43, o["b"] = 43) |
60 assertEquals("b", key) | 83 assertEquals("b", key) |
61 assertEquals(43, val) | 84 assertEquals(43, val) |
62 // assertTrue(Object.defineProperty(o, "c", {value: 44})) | 85 // assertTrue(Object.defineProperty(o, "c", {value: 44})) |
63 // assertEquals("c", key) | 86 // assertEquals("c", key) |
64 // assertEquals(44, val) | 87 // assertEquals(44, val) |
65 } | 88 } |
66 | 89 |
67 TestSet({set: function(r, k, v) { key = k; val = v; return true }}) | 90 TestSet({ |
68 TestSet({getOwnPropertyDescriptor: function(k) { return {writable: true} }, | 91 set: function(r, k, v) { key = k; val = v; return true } |
69 defineProperty: function(k, desc) { key = k, val = desc.value }}) | 92 }) |
70 TestSet({getOwnPropertyDescriptor: function(k) { return {get writable() { return
true }} }, | 93 TestSet({ |
71 defineProperty: function(k, desc) { key = k, val = desc.value }}) | 94 set: function(r, k, v) { return this.set2(r, k, v) }, |
72 TestSet({getOwnPropertyDescriptor: function(k) { return {set: function(v) { key
= k, val = v }} }}) | 95 set2: function(r, k, v) { key = k; val = v; return true } |
73 TestSet({getOwnPropertyDescriptor: function(k) { return null }, | 96 }) |
74 getPropertyDescriptor: function(k) { return {writable: true} }, | 97 TestSet({ |
75 defineProperty: function(k, desc) { key = k, val = desc.value }}) | 98 getOwnPropertyDescriptor: function(k) { return {writable: true} }, |
76 TestSet({getOwnPropertyDescriptor: function(k) { return null }, | 99 defineProperty: function(k, desc) { key = k; val = desc.value } |
77 getPropertyDescriptor: function(k) { return {get writable() { return tr
ue }} }, | 100 }) |
78 defineProperty: function(k, desc) { key = k, val = desc.value }}) | 101 TestSet({ |
79 TestSet({getOwnPropertyDescriptor: function(k) { return null }, | 102 getOwnPropertyDescriptor: function(k) { |
80 getPropertyDescriptor: function(k) { return {set: function(v) { key = k
, val = v }} }}) | 103 return this.getOwnPropertyDescriptor2(k) |
81 TestSet({getOwnPropertyDescriptor: function(k) { return null }, | 104 }, |
82 getPropertyDescriptor: function(k) { return null }, | 105 getOwnPropertyDescriptor2: function(k) { return {writable: true} }, |
83 defineProperty: function(k, desc) { key = k, val = desc.value }}) | 106 defineProperty: function(k, desc) { this.defineProperty2(k, desc) }, |
| 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 }) |
84 | 143 |
85 TestSet(Proxy.create({get: function(pr, pk) { return function(r, k, v) { key = k
; val = v; return true } }})) | 144 TestSet(Proxy.create({ |
| 145 get: function(pr, pk) { |
| 146 return function(r, k, v) { key = k; val = v; return true } |
| 147 } |
| 148 })) |
86 | 149 |
87 | 150 |
88 | 151 |
89 // Comparison. | 152 // Comparison. |
90 | 153 |
91 var o1 = Proxy.create({}) | 154 function TestComparison(eq) { |
92 var o2 = Proxy.create({}) | 155 var o1 = Proxy.create({}) |
| 156 var o2 = Proxy.create({}) |
93 | 157 |
94 assertTrue(o1 == o1) | 158 assertTrue(eq(o1, o1)) |
95 assertTrue(o2 == o2) | 159 assertTrue(eq(o2, o2)) |
96 assertTrue(!(o1 == o2)) | 160 assertTrue(!eq(o1, o2)) |
97 assertTrue(!(o1 == {})) | 161 assertTrue(!eq(o1, {})) |
98 assertTrue(!({} == o2)) | 162 assertTrue(!eq({}, o2)) |
99 assertTrue(!({} == {})) | 163 assertTrue(!eq({}, {})) |
| 164 } |
100 | 165 |
101 assertTrue(o1 === o1) | 166 TestComparison(function(o1, o2) { return o1 == o2 }) |
102 assertTrue(o2 === o2) | 167 TestComparison(function(o1, o2) { return o1 === o2 }) |
103 assertTrue(!(o1 === o2)) | 168 TestComparison(function(o1, o2) { return !(o1 != o2) }) |
104 assertTrue(!(o1 === {})) | 169 TestComparison(function(o1, o2) { return !(o1 !== o2) }) |
105 assertTrue(!({} === o2)) | |
106 assertTrue(!({} === {})) | |
107 | 170 |
108 | 171 |
109 | 172 |
110 // Type. | 173 // Type. |
111 | 174 |
112 assertEquals("object", typeof Proxy.create({})) | 175 assertEquals("object", typeof Proxy.create({})) |
113 assertTrue(typeof Proxy.create({}) == "object") | 176 assertTrue(typeof Proxy.create({}) == "object") |
114 assertTrue("object" == typeof Proxy.create({})) | 177 assertTrue("object" == typeof Proxy.create({})) |
115 | 178 |
116 // No function proxies yet. | 179 // 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 |