Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: test/mjsunit/harmony/proxies.js

Issue 8139027: Version 3.6.5 (Closed) Base URL: http://v8.googlecode.com/svn/trunk/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/mjsunit/harmony/block-let-declaration.js ('k') | test/mjsunit/harmony/proxies-hash.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2011 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
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Flags: --harmony-proxies 28 // Flags: --harmony-proxies
29 29
30 30
31 // TODO(rossberg): for-in for proxies not implemented. 31 // TODO(rossberg): for-in not implemented on proxies.
32 // TODO(rossberg): inheritance from proxies not implemented.
33 // TODO(rossberg): function proxies as constructors not implemented.
34 32
35 33
36 // Helper. 34 // Helper.
37 35
38 function TestWithProxies(test, handler) { 36 function TestWithProxies(test, handler) {
39 test(handler, Proxy.create) 37 test(handler, Proxy.create)
40 test(handler, function(h) {return Proxy.createFunction(h, function() {})}) 38 test(handler, function(h) {return Proxy.createFunction(h, function() {})})
41 } 39 }
42 40
43 41
44 // Getters. 42
43 // Getting property descriptors (Object.getOwnPropertyDescriptor).
44
45 var key
46
47 function TestGetOwnProperty(handler) {
48 TestWithProxies(TestGetOwnProperty2, handler)
49 }
50
51 function TestGetOwnProperty2(handler, create) {
52 var p = create(handler)
53 assertEquals(42, Object.getOwnPropertyDescriptor(p, "a").value)
54 assertEquals("a", key)
55 assertEquals(42, Object.getOwnPropertyDescriptor(p, 99).value)
56 assertEquals("99", key)
57 }
58
59 TestGetOwnProperty({
60 getOwnPropertyDescriptor: function(k) {
61 key = k
62 return {value: 42, configurable: true}
63 }
64 })
65
66 TestGetOwnProperty({
67 getOwnPropertyDescriptor: function(k) {
68 return this.getOwnPropertyDescriptor2(k)
69 },
70 getOwnPropertyDescriptor2: function(k) {
71 key = k
72 return {value: 42, configurable: true}
73 }
74 })
75
76 TestGetOwnProperty({
77 getOwnPropertyDescriptor: function(k) {
78 key = k
79 return {get value() { return 42 }, get configurable() { return true }}
80 }
81 })
82
83 TestGetOwnProperty(Proxy.create({
84 get: function(pr, pk) {
85 return function(k) { key = k; return {value: 42, configurable: true} }
86 }
87 }))
88
89
90 function TestGetOwnPropertyThrow(handler) {
91 TestWithProxies(TestGetOwnPropertyThrow2, handler)
92 }
93
94 function TestGetOwnPropertyThrow2(handler, create) {
95 var p = create(handler)
96 assertThrows(function(){ Object.getOwnPropertyDescriptor(p, "a") }, "myexn")
97 assertThrows(function(){ Object.getOwnPropertyDescriptor(p, 77) }, "myexn")
98 }
99
100 TestGetOwnPropertyThrow({
101 getOwnPropertyDescriptor: function(k) { throw "myexn" }
102 })
103
104 TestGetOwnPropertyThrow({
105 getOwnPropertyDescriptor: function(k) {
106 return this.getPropertyDescriptor2(k)
107 },
108 getOwnPropertyDescriptor2: function(k) { throw "myexn" }
109 })
110
111 TestGetOwnPropertyThrow({
112 getOwnPropertyDescriptor: function(k) {
113 return {get value() { throw "myexn" }}
114 }
115 })
116
117 TestGetOwnPropertyThrow(Proxy.create({
118 get: function(pr, pk) {
119 return function(k) { throw "myexn" }
120 }
121 }))
122
123
124
125 // Getters (dot, brackets).
126
127 var key
45 128
46 function TestGet(handler) { 129 function TestGet(handler) {
47 TestWithProxies(TestGet2, handler) 130 TestWithProxies(TestGet2, handler)
48 } 131 }
49 132
50 function TestGet2(handler, create) { 133 function TestGet2(handler, create) {
51 var p = create(handler) 134 var p = create(handler)
52 assertEquals(42, p.a) 135 assertEquals(42, p.a)
136 assertEquals("a", key)
53 assertEquals(42, p["b"]) 137 assertEquals(42, p["b"])
138 assertEquals("b", key)
139 assertEquals(42, p[99])
140 assertEquals("99", key)
54 141
55 // TODO(rossberg): inheritance from proxies not yet implemented. 142 var o = Object.create(p, {x: {value: 88}})
56 // var o = Object.create(p, {x: {value: 88}}) 143 assertEquals(42, o.a)
57 // assertEquals(42, o.a) 144 assertEquals("a", key)
58 // assertEquals(42, o["b"]) 145 assertEquals(42, o["b"])
59 // assertEquals(88, o.x) 146 assertEquals("b", key)
60 // assertEquals(88, o["x"]) 147 assertEquals(42, o[99])
148 assertEquals("99", key)
149 assertEquals(88, o.x)
150 assertEquals(88, o["x"])
61 } 151 }
62 152
63 TestGet({ 153 TestGet({
64 get: function(r, k) { return 42 } 154 get: function(r, k) { key = k; return 42 }
65 }) 155 })
66 156
67 TestGet({ 157 TestGet({
68 get: function(r, k) { return this.get2(r, k) }, 158 get: function(r, k) { return this.get2(r, k) },
69 get2: function(r, k) { return 42 } 159 get2: function(r, k) { key = k; return 42 }
70 }) 160 })
71 161
72 TestGet({ 162 TestGet({
73 getPropertyDescriptor: function(k) { return {value: 42} } 163 getPropertyDescriptor: function(k) { key = k; return {value: 42} }
74 }) 164 })
75 165
76 TestGet({ 166 TestGet({
77 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) }, 167 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) },
78 getPropertyDescriptor2: function(k) { return {value: 42} } 168 getPropertyDescriptor2: function(k) { key = k; return {value: 42} }
79 }) 169 })
80 170
81 TestGet({ 171 TestGet({
82 getPropertyDescriptor: function(k) { 172 getPropertyDescriptor: function(k) {
173 key = k;
83 return {get value() { return 42 }} 174 return {get value() { return 42 }}
84 } 175 }
85 }) 176 })
86 177
87 TestGet({ 178 TestGet({
88 get: undefined, 179 get: undefined,
89 getPropertyDescriptor: function(k) { return {value: 42} } 180 getPropertyDescriptor: function(k) { key = k; return {value: 42} }
90 }) 181 })
91 182
92 TestGet(Proxy.create({ 183 TestGet(Proxy.create({
93 get: function(pr, pk) { 184 get: function(pr, pk) {
94 return function(r, k) { return 42 } 185 return function(r, k) { key = k; return 42 }
95 } 186 }
96 })) 187 }))
97 188
98 189
99 function TestGetCall(handler) { 190 function TestGetCall(handler) {
100 TestWithProxies(TestGetCall2, handler) 191 TestWithProxies(TestGetCall2, handler)
101 } 192 }
102 193
103 function TestGetCall2(handler, create) { 194 function TestGetCall2(handler, create) {
104 var p = create(handler) 195 var p = create(handler)
105 assertEquals(55, p.f()) 196 assertEquals(55, p.f())
197 assertEquals(55, p["f"]())
106 assertEquals(55, p.f("unused", "arguments")) 198 assertEquals(55, p.f("unused", "arguments"))
107 assertEquals(55, p.f.call(p)) 199 assertEquals(55, p.f.call(p))
200 assertEquals(55, p["f"].call(p))
201 assertEquals(55, p[101].call(p))
108 assertEquals(55, p.withargs(45, 5)) 202 assertEquals(55, p.withargs(45, 5))
109 assertEquals(55, p.withargs.call(p, 11, 22)) 203 assertEquals(55, p.withargs.call(p, 11, 22))
110 assertEquals("6655", "66" + p) // calls p.toString 204 assertEquals("6655", "66" + p) // calls p.toString
205
206 var o = Object.create(p, {g: {value: function(x) { return x + 88 }}})
207 assertEquals(55, o.f())
208 assertEquals(55, o["f"]())
209 assertEquals(55, o.f("unused", "arguments"))
210 assertEquals(55, o.f.call(o))
211 assertEquals(55, o.f.call(p))
212 assertEquals(55, o["f"].call(p))
213 assertEquals(55, o[101].call(p))
214 assertEquals(55, o.withargs(45, 5))
215 assertEquals(55, o.withargs.call(p, 11, 22))
216 assertEquals(90, o.g(2))
217 assertEquals(91, o.g.call(o, 3))
218 assertEquals(92, o.g.call(p, 4))
219 assertEquals("6655", "66" + o) // calls o.toString
111 } 220 }
112 221
113 TestGetCall({ 222 TestGetCall({
114 get: function(r, k) { return function() { return 55 } } 223 get: function(r, k) { return function() { return 55 } }
115 }) 224 })
116 225
117 TestGetCall({ 226 TestGetCall({
118 get: function(r, k) { return this.get2(r, k) }, 227 get: function(r, k) { return this.get2(r, k) },
119 get2: function(r, k) { return function() { return 55 } } 228 get2: function(r, k) { return function() { return 55 } }
120 }) 229 })
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 274
166 275
167 function TestGetThrow(handler) { 276 function TestGetThrow(handler) {
168 TestWithProxies(TestGetThrow2, handler) 277 TestWithProxies(TestGetThrow2, handler)
169 } 278 }
170 279
171 function TestGetThrow2(handler, create) { 280 function TestGetThrow2(handler, create) {
172 var p = create(handler) 281 var p = create(handler)
173 assertThrows(function(){ p.a }, "myexn") 282 assertThrows(function(){ p.a }, "myexn")
174 assertThrows(function(){ p["b"] }, "myexn") 283 assertThrows(function(){ p["b"] }, "myexn")
284 assertThrows(function(){ p[3] }, "myexn")
285
286 var o = Object.create(p, {x: {value: 88}, '4': {value: 89}})
287 assertThrows(function(){ o.a }, "myexn")
288 assertThrows(function(){ o["b"] }, "myexn")
289 assertThrows(function(){ o[3] }, "myexn")
290 assertEquals(88, o.x)
291 assertEquals(88, o["x"])
292 assertEquals(89, o[4])
175 } 293 }
176 294
177 TestGetThrow({ 295 TestGetThrow({
178 get: function(r, k) { throw "myexn" } 296 get: function(r, k) { throw "myexn" }
179 }) 297 })
180 298
181 TestGetThrow({ 299 TestGetThrow({
182 get: function(r, k) { return this.get2(r, k) }, 300 get: function(r, k) { return this.get2(r, k) },
183 get2: function(r, k) { throw "myexn" } 301 get2: function(r, k) { throw "myexn" }
184 }) 302 })
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 } 343 }
226 344
227 function TestSet2(handler, create) { 345 function TestSet2(handler, create) {
228 var p = create(handler) 346 var p = create(handler)
229 assertEquals(42, p.a = 42) 347 assertEquals(42, p.a = 42)
230 assertEquals("a", key) 348 assertEquals("a", key)
231 assertEquals(42, val) 349 assertEquals(42, val)
232 assertEquals(43, p["b"] = 43) 350 assertEquals(43, p["b"] = 43)
233 assertEquals("b", key) 351 assertEquals("b", key)
234 assertEquals(43, val) 352 assertEquals(43, val)
353 assertEquals(44, p[77] = 44)
354 assertEquals("77", key)
355 assertEquals(44, val)
235 } 356 }
236 357
237 TestSet({ 358 TestSet({
238 set: function(r, k, v) { key = k; val = v; return true } 359 set: function(r, k, v) { key = k; val = v; return true }
239 }) 360 })
240 361
241 TestSet({ 362 TestSet({
242 set: function(r, k, v) { return this.set2(r, k, v) }, 363 set: function(r, k, v) { return this.set2(r, k, v) },
243 set2: function(r, k, v) { key = k; val = v; return true } 364 set2: function(r, k, v) { key = k; val = v; return true }
244 }) 365 })
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 defineProperty: function(k, desc) { key = k, val = desc.value } 418 defineProperty: function(k, desc) { key = k, val = desc.value }
298 }) 419 })
299 420
300 TestSet(Proxy.create({ 421 TestSet(Proxy.create({
301 get: function(pr, pk) { 422 get: function(pr, pk) {
302 return function(r, k, v) { key = k; val = v; return true } 423 return function(r, k, v) { key = k; val = v; return true }
303 } 424 }
304 })) 425 }))
305 426
306 427
307
308 function TestSetThrow(handler, create) { 428 function TestSetThrow(handler, create) {
309 TestWithProxies(TestSetThrow2, handler) 429 TestWithProxies(TestSetThrow2, handler)
310 } 430 }
311 431
312 function TestSetThrow2(handler, create) { 432 function TestSetThrow2(handler, create) {
313 var p = create(handler) 433 var p = create(handler)
314 assertThrows(function(){ p.a = 42 }, "myexn") 434 assertThrows(function(){ p.a = 42 }, "myexn")
315 assertThrows(function(){ p["b"] = 42 }, "myexn") 435 assertThrows(function(){ p["b"] = 42 }, "myexn")
436 assertThrows(function(){ p[22] = 42 }, "myexn")
316 } 437 }
317 438
318 TestSetThrow({ 439 TestSetThrow({
319 set: function(r, k, v) { throw "myexn" } 440 set: function(r, k, v) { throw "myexn" }
320 }) 441 })
321 442
322 TestSetThrow({ 443 TestSetThrow({
323 set: function(r, k, v) { return this.set2(r, k, v) }, 444 set: function(r, k, v) { return this.set2(r, k, v) },
324 set2: function(r, k, v) { throw "myexn" } 445 set2: function(r, k, v) { throw "myexn" }
325 }) 446 })
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 get: function(pr, pk) { throw "myexn" } 538 get: function(pr, pk) { throw "myexn" }
418 })) 539 }))
419 540
420 TestSetThrow(Proxy.create({ 541 TestSetThrow(Proxy.create({
421 get: function(pr, pk) { 542 get: function(pr, pk) {
422 return function(r, k, v) { throw "myexn" } 543 return function(r, k, v) { throw "myexn" }
423 } 544 }
424 })) 545 }))
425 546
426 547
548 var key
549 var val
550
551 function TestSetForDerived(handler, create) {
552 TestWithProxies(TestSetForDerived2, handler)
553 }
554
555 function TestSetForDerived2(handler, create) {
556 var p = create(handler)
557 var o = Object.create(p, {x: {value: 88, writable: true},
558 '1': {value: 89, writable: true}})
559
560 key = ""
561 assertEquals(48, o.x = 48)
562 assertEquals("", key) // trap not invoked
563 assertEquals(48, o.x)
564
565 assertEquals(47, o[1] = 47)
566 assertEquals("", key) // trap not invoked
567 assertEquals(47, o[1])
568
569 assertEquals(49, o.y = 49)
570 assertEquals("y", key)
571 assertEquals(49, o.y)
572
573 assertEquals(50, o[2] = 50)
574 assertEquals("2", key)
575 assertEquals(50, o[2])
576
577 assertEquals(44, o.p_writable = 44)
578 assertEquals("p_writable", key)
579 assertEquals(44, o.p_writable)
580
581 assertEquals(45, o.p_nonwritable = 45)
582 assertEquals("p_nonwritable", key)
583 assertEquals(45, o.p_nonwritable)
584
585 assertEquals(46, o.p_setter = 46)
586 assertEquals("p_setter", key)
587 assertEquals(46, val) // written to parent
588 assertFalse(Object.prototype.hasOwnProperty.call(o, "p_setter"))
589
590 val = ""
591 assertEquals(47, o.p_nosetter = 47)
592 assertEquals("p_nosetter", key)
593 assertEquals("", val) // not written at all
594 assertFalse(Object.prototype.hasOwnProperty.call(o, "p_nosetter"));
595
596 key = ""
597 assertThrows(function(){ "use strict"; o.p_nosetter = 50 }, TypeError)
598 assertEquals("p_nosetter", key)
599 assertEquals("", val) // not written at all
600
601 assertThrows(function(){ o.p_nonconf = 53 }, TypeError)
602 assertEquals("p_nonconf", key)
603
604 assertThrows(function(){ o.p_throw = 51 }, "myexn")
605 assertEquals("p_throw", key)
606
607 assertThrows(function(){ o.p_setterthrow = 52 }, "myexn")
608 assertEquals("p_setterthrow", key)
609 }
610
611 TestSetForDerived({
612 getOwnPropertyDescriptor: function(k) {
613 key = k;
614 switch (k) {
615 case "p_writable": return {writable: true, configurable: true}
616 case "p_nonwritable": return {writable: false, configurable: true}
617 case "p_setter":return {set: function(x) { val = x }, configurable: true}
618 case "p_nosetter": return {get: function() { return 1 }, configurable: tru e}
619 case "p_nonconf":return {}
620 case "p_throw": throw "myexn"
621 case "p_setterthrow": return {set: function(x) { throw "myexn" }}
622 default: return undefined
623 }
624 }
625 })
626
627
628 // TODO(rossberg): TestSetReject, returning false
629 // TODO(rossberg): TestGetProperty, TestSetProperty
630
631
427 632
428 // Property definition (Object.defineProperty and Object.defineProperties). 633 // Property definition (Object.defineProperty and Object.defineProperties).
429 634
430 var key 635 var key
431 var desc 636 var desc
432 637
433 function TestDefine(handler) { 638 function TestDefine(handler) {
434 TestWithProxies(TestDefine2, handler) 639 TestWithProxies(TestDefine2, handler)
435 } 640 }
436 641
437 function TestDefine2(handler, create) { 642 function TestDefine2(handler, create) {
438 var p = create(handler) 643 var p = create(handler)
439 assertEquals(p, Object.defineProperty(p, "a", {value: 44})) 644 assertEquals(p, Object.defineProperty(p, "a", {value: 44}))
440 assertEquals("a", key) 645 assertEquals("a", key)
441 assertEquals(1, Object.getOwnPropertyNames(desc).length) 646 assertEquals(1, Object.getOwnPropertyNames(desc).length)
442 assertEquals(44, desc.value) 647 assertEquals(44, desc.value)
443 648
444 assertEquals(p, Object.defineProperty(p, "b", {value: 45, writable: false})) 649 assertEquals(p, Object.defineProperty(p, "b", {value: 45, writable: false}))
445 assertEquals("b", key) 650 assertEquals("b", key)
446 assertEquals(2, Object.getOwnPropertyNames(desc).length) 651 assertEquals(2, Object.getOwnPropertyNames(desc).length)
447 assertEquals(45, desc.value) 652 assertEquals(45, desc.value)
448 assertEquals(false, desc.writable) 653 assertEquals(false, desc.writable)
449 654
450 assertEquals(p, Object.defineProperty(p, "c", {value: 46, enumerable: false})) 655 assertEquals(p, Object.defineProperty(p, "c", {value: 46, enumerable: false}))
451 assertEquals("c", key) 656 assertEquals("c", key)
452 assertEquals(2, Object.getOwnPropertyNames(desc).length) 657 assertEquals(2, Object.getOwnPropertyNames(desc).length)
453 assertEquals(46, desc.value) 658 assertEquals(46, desc.value)
454 assertEquals(false, desc.enumerable) 659 assertEquals(false, desc.enumerable)
455 660
661 assertEquals(p, Object.defineProperty(p, 101, {value: 47, enumerable: false}))
662 assertEquals("101", key)
663 assertEquals(2, Object.getOwnPropertyNames(desc).length)
664 assertEquals(47, desc.value)
665 assertEquals(false, desc.enumerable)
666
456 var attributes = {configurable: true, mine: 66, minetoo: 23} 667 var attributes = {configurable: true, mine: 66, minetoo: 23}
457 assertEquals(p, Object.defineProperty(p, "d", attributes)) 668 assertEquals(p, Object.defineProperty(p, "d", attributes))
458 assertEquals("d", key) 669 assertEquals("d", key)
459 // Modifying the attributes object after the fact should have no effect. 670 // Modifying the attributes object after the fact should have no effect.
460 attributes.configurable = false 671 attributes.configurable = false
461 attributes.mine = 77 672 attributes.mine = 77
462 delete attributes.minetoo 673 delete attributes.minetoo
463 assertEquals(3, Object.getOwnPropertyNames(desc).length) 674 assertEquals(3, Object.getOwnPropertyNames(desc).length)
464 assertEquals(true, desc.configurable) 675 assertEquals(true, desc.configurable)
465 assertEquals(66, desc.mine) 676 assertEquals(66, desc.mine)
(...skipping 14 matching lines...) Expand all
480 // getOwnPropertyNames: function() { return ["value"] } 691 // getOwnPropertyNames: function() { return ["value"] }
481 // }) 692 // })
482 // assertEquals(1, Object.getOwnPropertyNames(d).length) 693 // assertEquals(1, Object.getOwnPropertyNames(d).length)
483 // assertEquals(77, d.value) 694 // assertEquals(77, d.value)
484 // assertEquals(p, Object.defineProperty(p, "p", d)) 695 // assertEquals(p, Object.defineProperty(p, "p", d))
485 // assertEquals("p", key) 696 // assertEquals("p", key)
486 // assertEquals(1, Object.getOwnPropertyNames(desc).length) 697 // assertEquals(1, Object.getOwnPropertyNames(desc).length)
487 // assertEquals(77, desc.value) 698 // assertEquals(77, desc.value)
488 699
489 var props = { 700 var props = {
490 'bla': {}, 701 '11': {},
491 blub: {get: function() { return true }}, 702 blub: {get: function() { return true }},
492 '': {get value() { return 20 }}, 703 '': {get value() { return 20 }},
493 last: {value: 21, configurable: true, mine: "eyes"} 704 last: {value: 21, configurable: true, mine: "eyes"}
494 } 705 }
495 Object.defineProperty(props, "hidden", {value: "hidden", enumerable: false}) 706 Object.defineProperty(props, "hidden", {value: "hidden", enumerable: false})
496 assertEquals(p, Object.defineProperties(p, props)) 707 assertEquals(p, Object.defineProperties(p, props))
497 assertEquals("last", key) 708 assertEquals("last", key)
498 assertEquals(2, Object.getOwnPropertyNames(desc).length) 709 assertEquals(2, Object.getOwnPropertyNames(desc).length)
499 assertEquals(21, desc.value) 710 assertEquals(21, desc.value)
500 assertEquals(true, desc.configurable) 711 assertEquals(true, desc.configurable)
(...skipping 19 matching lines...) Expand all
520 })) 731 }))
521 732
522 733
523 function TestDefineThrow(handler) { 734 function TestDefineThrow(handler) {
524 TestWithProxies(TestDefineThrow2, handler) 735 TestWithProxies(TestDefineThrow2, handler)
525 } 736 }
526 737
527 function TestDefineThrow2(handler, create) { 738 function TestDefineThrow2(handler, create) {
528 var p = create(handler) 739 var p = create(handler)
529 assertThrows(function(){ Object.defineProperty(p, "a", {value: 44})}, "myexn") 740 assertThrows(function(){ Object.defineProperty(p, "a", {value: 44})}, "myexn")
741 assertThrows(function(){ Object.defineProperty(p, 0, {value: 44})}, "myexn")
530 742
531 // TODO(rossberg): These tests require for-in on proxies. 743 // TODO(rossberg): These tests require for-in on proxies.
532 // var d1 = create({ 744 // var d1 = create({
533 // get: function(r, k) { throw "myexn" }, 745 // get: function(r, k) { throw "myexn" },
534 // getOwnPropertyNames: function() { return ["value"] } 746 // getOwnPropertyNames: function() { return ["value"] }
535 // }) 747 // })
536 // assertThrows(function(){ Object.defineProperty(p, "p", d1) }, "myexn") 748 // assertThrows(function(){ Object.defineProperty(p, "p", d1) }, "myexn")
537 // var d2 = create({ 749 // var d2 = create({
538 // get: function(r, k) { return 77 }, 750 // get: function(r, k) { return 77 },
539 // getOwnPropertyNames: function() { throw "myexn" } 751 // getOwnPropertyNames: function() { throw "myexn" }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 function TestDelete(handler) { 784 function TestDelete(handler) {
573 TestWithProxies(TestDelete2, handler) 785 TestWithProxies(TestDelete2, handler)
574 } 786 }
575 787
576 function TestDelete2(handler, create) { 788 function TestDelete2(handler, create) {
577 var p = create(handler) 789 var p = create(handler)
578 assertEquals(true, delete p.a) 790 assertEquals(true, delete p.a)
579 assertEquals("a", key) 791 assertEquals("a", key)
580 assertEquals(true, delete p["b"]) 792 assertEquals(true, delete p["b"])
581 assertEquals("b", key) 793 assertEquals("b", key)
794 assertEquals(true, delete p[1])
795 assertEquals("1", key)
582 796
583 assertEquals(false, delete p.z1) 797 assertEquals(false, delete p.z1)
584 assertEquals("z1", key) 798 assertEquals("z1", key)
585 assertEquals(false, delete p["z2"]) 799 assertEquals(false, delete p["z2"])
586 assertEquals("z2", key); 800 assertEquals("z2", key);
587 801
588 (function() { 802 (function() {
589 "use strict" 803 "use strict"
590 assertEquals(true, delete p.c) 804 assertEquals(true, delete p.c)
591 assertEquals("c", key) 805 assertEquals("c", key)
592 assertEquals(true, delete p["d"]) 806 assertEquals(true, delete p["d"])
593 assertEquals("d", key) 807 assertEquals("d", key)
808 assertEquals(true, delete p[2])
809 assertEquals("2", key)
594 810
595 assertThrows(function(){ delete p.z3 }, TypeError) 811 assertThrows(function(){ delete p.z3 }, TypeError)
596 assertEquals("z3", key) 812 assertEquals("z3", key)
597 assertThrows(function(){ delete p["z4"] }, TypeError) 813 assertThrows(function(){ delete p["z4"] }, TypeError)
598 assertEquals("z4", key) 814 assertEquals("z4", key)
599 })() 815 })()
600 } 816 }
601 817
602 TestDelete({ 818 TestDelete({
603 delete: function(k) { key = k; return k < "z" } 819 delete: function(k) { key = k; return k < "z" }
(...skipping 12 matching lines...) Expand all
616 832
617 833
618 function TestDeleteThrow(handler) { 834 function TestDeleteThrow(handler) {
619 TestWithProxies(TestDeleteThrow2, handler) 835 TestWithProxies(TestDeleteThrow2, handler)
620 } 836 }
621 837
622 function TestDeleteThrow2(handler, create) { 838 function TestDeleteThrow2(handler, create) {
623 var p = create(handler) 839 var p = create(handler)
624 assertThrows(function(){ delete p.a }, "myexn") 840 assertThrows(function(){ delete p.a }, "myexn")
625 assertThrows(function(){ delete p["b"] }, "myexn"); 841 assertThrows(function(){ delete p["b"] }, "myexn");
842 assertThrows(function(){ delete p[3] }, "myexn");
626 843
627 (function() { 844 (function() {
628 "use strict" 845 "use strict"
629 assertThrows(function(){ delete p.c }, "myexn") 846 assertThrows(function(){ delete p.c }, "myexn")
630 assertThrows(function(){ delete p["d"] }, "myexn") 847 assertThrows(function(){ delete p["d"] }, "myexn")
848 assertThrows(function(){ delete p[4] }, "myexn");
631 })() 849 })()
632 } 850 }
633 851
634 TestDeleteThrow({ 852 TestDeleteThrow({
635 delete: function(k) { throw "myexn" } 853 delete: function(k) { throw "myexn" }
636 }) 854 })
637 855
638 TestDeleteThrow({ 856 TestDeleteThrow({
639 delete: function(k) { return this.delete2(k) }, 857 delete: function(k) { return this.delete2(k) },
640 delete2: function(k) { throw "myexn" } 858 delete2: function(k) { throw "myexn" }
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 assertTrue(99 in p) 989 assertTrue(99 in p)
772 assertEquals("99", key) 990 assertEquals("99", key)
773 assertFalse("z" in p) 991 assertFalse("z" in p)
774 assertEquals("z", key) 992 assertEquals("z", key)
775 993
776 assertEquals(2, ("a" in p) ? 2 : 0) 994 assertEquals(2, ("a" in p) ? 2 : 0)
777 assertEquals(0, !("a" in p) ? 2 : 0) 995 assertEquals(0, !("a" in p) ? 2 : 0)
778 assertEquals(0, ("zzz" in p) ? 2 : 0) 996 assertEquals(0, ("zzz" in p) ? 2 : 0)
779 assertEquals(2, !("zzz" in p) ? 2 : 0) 997 assertEquals(2, !("zzz" in p) ? 2 : 0)
780 998
999 // Test compilation in conditionals.
781 if ("b" in p) { 1000 if ("b" in p) {
782 } else { 1001 } else {
783 assertTrue(false) 1002 assertTrue(false)
784 } 1003 }
785 assertEquals("b", key) 1004 assertEquals("b", key)
786 1005
787 if ("zz" in p) { 1006 if ("zz" in p) {
788 assertTrue(false) 1007 assertTrue(false)
789 } 1008 }
790 assertEquals("zz", key) 1009 assertEquals("zz", key)
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
823 } 1042 }
824 }) 1043 })
825 1044
826 TestIn({ 1045 TestIn({
827 getPropertyDescriptor: function(k) { 1046 getPropertyDescriptor: function(k) {
828 key = k; return k < "z" ? {get value() { return 42 }} : void 0 1047 key = k; return k < "z" ? {get value() { return 42 }} : void 0
829 } 1048 }
830 }) 1049 })
831 1050
832 TestIn({ 1051 TestIn({
833 get: undefined, 1052 has: undefined,
834 getPropertyDescriptor: function(k) { 1053 getPropertyDescriptor: function(k) {
835 key = k; return k < "z" ? {value: 42} : void 0 1054 key = k; return k < "z" ? {value: 42} : void 0
836 } 1055 }
837 }) 1056 })
838 1057
839 TestIn(Proxy.create({ 1058 TestIn(Proxy.create({
840 get: function(pr, pk) { 1059 get: function(pr, pk) {
841 return function(k) { key = k; return k < "z" } 1060 return function(k) { key = k; return k < "z" }
842 } 1061 }
843 })) 1062 }))
844 1063
845 1064
846 function TestInThrow(handler) { 1065 function TestInThrow(handler) {
847 TestWithProxies(TestInThrow2, handler) 1066 TestWithProxies(TestInThrow2, handler)
848 } 1067 }
849 1068
850 function TestInThrow2(handler, create) { 1069 function TestInThrow2(handler, create) {
851 var p = create(handler) 1070 var p = create(handler)
852 assertThrows(function(){ return "a" in o }, "myexn") 1071 assertThrows(function(){ return "a" in o }, "myexn")
1072 assertThrows(function(){ return 99 in o }, "myexn")
853 assertThrows(function(){ return !("a" in o) }, "myexn") 1073 assertThrows(function(){ return !("a" in o) }, "myexn")
854 assertThrows(function(){ return ("a" in o) ? 2 : 3 }, "myexn") 1074 assertThrows(function(){ return ("a" in o) ? 2 : 3 }, "myexn")
855 assertThrows(function(){ if ("b" in o) {} }, "myexn") 1075 assertThrows(function(){ if ("b" in o) {} }, "myexn")
856 assertThrows(function(){ if (!("b" in o)) {} }, "myexn") 1076 assertThrows(function(){ if (!("b" in o)) {} }, "myexn")
857 assertThrows(function(){ if ("zzz" in o) {} }, "myexn") 1077 assertThrows(function(){ if ("zzz" in o) {} }, "myexn")
858 } 1078 }
859 1079
860 TestInThrow({ 1080 TestInThrow({
861 has: function(k) { throw "myexn" } 1081 has: function(k) { throw "myexn" }
862 }) 1082 })
863 1083
864 TestInThrow({ 1084 TestInThrow({
865 has: function(k) { return this.has2(k) }, 1085 has: function(k) { return this.has2(k) },
866 has2: function(k) { throw "myexn" } 1086 has2: function(k) { throw "myexn" }
867 }) 1087 })
868 1088
869 TestInThrow({ 1089 TestInThrow({
870 getPropertyDescriptor: function(k) { throw "myexn" } 1090 getPropertyDescriptor: function(k) { throw "myexn" }
871 }) 1091 })
872 1092
873 TestInThrow({ 1093 TestInThrow({
874 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) }, 1094 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) },
875 getPropertyDescriptor2: function(k) { throw "myexn" } 1095 getPropertyDescriptor2: function(k) { throw "myexn" }
876 }) 1096 })
877 1097
878 TestInThrow({ 1098 TestInThrow({
879 get: undefined, 1099 has: undefined,
880 getPropertyDescriptor: function(k) { throw "myexn" } 1100 getPropertyDescriptor: function(k) { throw "myexn" }
881 }) 1101 })
882 1102
883 TestInThrow(Proxy.create({ 1103 TestInThrow(Proxy.create({
884 get: function(pr, pk) { throw "myexn" } 1104 get: function(pr, pk) { throw "myexn" }
885 })) 1105 }))
886 1106
887 TestInThrow(Proxy.create({ 1107 TestInThrow(Proxy.create({
888 get: function(pr, pk) { 1108 get: function(pr, pk) {
889 return function(k) { throw "myexn" } 1109 return function(k) { throw "myexn" }
890 } 1110 }
891 })) 1111 }))
892 1112
893 1113
1114 function TestInForDerived(handler) {
1115 TestWithProxies(TestInForDerived2, handler)
1116 }
1117
1118 function TestInForDerived2(handler, create) {
1119 var p = create(handler)
1120 var o = Object.create(p)
1121
1122 assertTrue("a" in o)
1123 assertEquals("a", key)
1124 assertTrue(99 in o)
1125 assertEquals("99", key)
1126 assertFalse("z" in o)
1127 assertEquals("z", key)
1128
1129 assertEquals(2, ("a" in o) ? 2 : 0)
1130 assertEquals(0, !("a" in o) ? 2 : 0)
1131 assertEquals(0, ("zzz" in o) ? 2 : 0)
1132 assertEquals(2, !("zzz" in o) ? 2 : 0)
1133
1134 if ("b" in o) {
1135 } else {
1136 assertTrue(false)
1137 }
1138 assertEquals("b", key)
1139
1140 if ("zz" in o) {
1141 assertTrue(false)
1142 }
1143 assertEquals("zz", key)
1144
1145 if (!("c" in o)) {
1146 assertTrue(false)
1147 }
1148 assertEquals("c", key)
1149
1150 if (!("zzz" in o)) {
1151 } else {
1152 assertTrue(false)
1153 }
1154 assertEquals("zzz", key)
1155 }
1156
1157 TestInForDerived({
1158 getPropertyDescriptor: function(k) {
1159 key = k; return k < "z" ? {value: 42, configurable: true} : void 0
1160 }
1161 })
1162
1163 TestInForDerived({
1164 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) },
1165 getPropertyDescriptor2: function(k) {
1166 key = k; return k < "z" ? {value: 42, configurable: true} : void 0
1167 }
1168 })
1169
1170 TestInForDerived({
1171 getPropertyDescriptor: function(k) {
1172 key = k;
1173 return k < "z" ? {get value() { return 42 }, configurable: true} : void 0
1174 }
1175 })
1176
1177 /* TODO(rossberg): this will work once we implement the newest proposal
1178 * regarding default traps for getPropertyDescriptor.
1179 TestInForDerived({
1180 getOwnPropertyDescriptor: function(k) {
1181 key = k; return k < "z" ? {value: 42, configurable: true} : void 0
1182 }
1183 })
1184
1185 TestInForDerived({
1186 getOwnPropertyDescriptor: function(k) {
1187 return this.getOwnPropertyDescriptor2(k)
1188 },
1189 getOwnPropertyDescriptor2: function(k) {
1190 key = k; return k < "z" ? {value: 42, configurable: true} : void 0
1191 }
1192 })
1193
1194 TestInForDerived({
1195 getOwnPropertyDescriptor: function(k) {
1196 key = k;
1197 return k < "z" ? {get value() { return 42 }, configurable: true} : void 0
1198 }
1199 })
1200 */
1201
1202 TestInForDerived(Proxy.create({
1203 get: function(pr, pk) {
1204 return function(k) {
1205 key = k; return k < "z" ? {value: 42, configurable: true} : void 0
1206 }
1207 }
1208 }))
1209
1210
1211
1212 // Property descriptor conversion.
1213
1214 var descget
1215
1216 function TestDescriptorGetOrder(handler) {
1217 var p = Proxy.create(handler)
1218 var o = Object.create(p, {b: {value: 0}})
1219 TestDescriptorGetOrder2(function(n) { return p[n] }, "vV")
1220 TestDescriptorGetOrder2(function(n) { return n in p }, "")
1221 TestDescriptorGetOrder2(function(n) { return o[n] }, "vV")
1222 TestDescriptorGetOrder2(function(n) { return n in o }, "eEcCvVwWgs")
1223 }
1224
1225 function TestDescriptorGetOrder2(f, access) {
1226 descget = ""
1227 assertTrue(f("a"))
1228 assertEquals(access, descget)
1229 descget = ""
1230 assertTrue(f(99))
1231 assertEquals(access, descget)
1232 descget = ""
1233 assertFalse(!!f("z"))
1234 assertEquals("", descget)
1235 }
1236
1237 TestDescriptorGetOrder({
1238 getPropertyDescriptor: function(k) {
1239 if (k >= "z") return void 0
1240 // Return a proxy as property descriptor, so that we can log accesses.
1241 return Proxy.create({
1242 get: function(r, attr) {
1243 descget += attr[0].toUpperCase()
1244 return true
1245 },
1246 has: function(attr) {
1247 descget += attr[0]
1248 switch (attr) {
1249 case "writable":
1250 case "enumerable":
1251 case "configurable":
1252 case "value":
1253 return true
1254 case "get":
1255 case "set":
1256 return false
1257 default:
1258 assertUnreachable()
1259 }
1260 }
1261 })
1262 }
1263 })
1264
1265
894 1266
895 // Own Properties (Object.prototype.hasOwnProperty). 1267 // Own Properties (Object.prototype.hasOwnProperty).
896 1268
897 var key 1269 var key
898 1270
899 function TestHasOwn(handler) { 1271 function TestHasOwn(handler) {
900 TestWithProxies(TestHasOwn2, handler) 1272 TestWithProxies(TestHasOwn2, handler)
901 } 1273 }
902 1274
903 function TestHasOwn2(handler, create) { 1275 function TestHasOwn2(handler, create) {
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 get: function(pr, pk) { 1371 get: function(pr, pk) {
1000 return function(k) { throw "myexn" } 1372 return function(k) { throw "myexn" }
1001 } 1373 }
1002 })) 1374 }))
1003 1375
1004 1376
1005 1377
1006 // Instanceof (instanceof) 1378 // Instanceof (instanceof)
1007 1379
1008 function TestInstanceof() { 1380 function TestInstanceof() {
1009 var o = {} 1381 var o1 = {}
1010 var p1 = Proxy.create({}) 1382 var p1 = Proxy.create({})
1011 var p2 = Proxy.create({}, o) 1383 var p2 = Proxy.create({}, o1)
1012 var p3 = Proxy.create({}, p2) 1384 var p3 = Proxy.create({}, p2)
1385 var o2 = Object.create(p2)
1013 1386
1014 var f0 = function() {} 1387 var f0 = function() {}
1015 f0.prototype = o 1388 f0.prototype = o1
1016 var f1 = function() {} 1389 var f1 = function() {}
1017 f1.prototype = p1 1390 f1.prototype = p1
1018 var f2 = function() {} 1391 var f2 = function() {}
1019 f2.prototype = p2 1392 f2.prototype = p2
1393 var f3 = function() {}
1394 f3.prototype = o2
1020 1395
1021 assertTrue(o instanceof Object) 1396 assertTrue(o1 instanceof Object)
1022 assertFalse(o instanceof f0) 1397 assertFalse(o1 instanceof f0)
1023 assertFalse(o instanceof f1) 1398 assertFalse(o1 instanceof f1)
1024 assertFalse(o instanceof f2) 1399 assertFalse(o1 instanceof f2)
1400 assertFalse(o1 instanceof f3)
1025 assertFalse(p1 instanceof Object) 1401 assertFalse(p1 instanceof Object)
1026 assertFalse(p1 instanceof f0) 1402 assertFalse(p1 instanceof f0)
1027 assertFalse(p1 instanceof f1) 1403 assertFalse(p1 instanceof f1)
1028 assertFalse(p1 instanceof f2) 1404 assertFalse(p1 instanceof f2)
1405 assertFalse(p1 instanceof f3)
1029 assertTrue(p2 instanceof Object) 1406 assertTrue(p2 instanceof Object)
1030 assertTrue(p2 instanceof f0) 1407 assertTrue(p2 instanceof f0)
1031 assertFalse(p2 instanceof f1) 1408 assertFalse(p2 instanceof f1)
1032 assertFalse(p2 instanceof f2) 1409 assertFalse(p2 instanceof f2)
1410 assertFalse(p2 instanceof f3)
1033 assertTrue(p3 instanceof Object) 1411 assertTrue(p3 instanceof Object)
1034 assertTrue(p3 instanceof f0) 1412 assertTrue(p3 instanceof f0)
1035 assertFalse(p3 instanceof f1) 1413 assertFalse(p3 instanceof f1)
1036 assertTrue(p3 instanceof f2) 1414 assertTrue(p3 instanceof f2)
1415 assertFalse(p3 instanceof f3)
1416 assertTrue(o2 instanceof Object)
1417 assertTrue(o2 instanceof f0)
1418 assertFalse(o2 instanceof f1)
1419 assertTrue(o2 instanceof f2)
1420 assertFalse(o2 instanceof f3)
1037 1421
1038 var f = Proxy.createFunction({}, function() {}) 1422 var f = Proxy.createFunction({}, function() {})
1039 assertTrue(f instanceof Function) 1423 assertTrue(f instanceof Function)
1040 } 1424 }
1041 1425
1042 TestInstanceof() 1426 TestInstanceof()
1043 1427
1044 1428
1045 1429
1046 // Prototype (Object.getPrototypeOf, Object.prototype.isPrototypeOf). 1430 // Prototype (Object.getPrototypeOf, Object.prototype.isPrototypeOf).
1047 1431
1048 function TestPrototype() { 1432 function TestPrototype() {
1049 var o = {} 1433 var o1 = {}
1050 var p1 = Proxy.create({}) 1434 var p1 = Proxy.create({})
1051 var p2 = Proxy.create({}, o) 1435 var p2 = Proxy.create({}, o1)
1052 var p3 = Proxy.create({}, p2) 1436 var p3 = Proxy.create({}, p2)
1053 var p4 = Proxy.create({}, 666) 1437 var p4 = Proxy.create({}, 666)
1438 var o2 = Object.create(p3)
1054 1439
1055 assertSame(Object.getPrototypeOf(o), Object.prototype) 1440 assertSame(Object.getPrototypeOf(o1), Object.prototype)
1056 assertSame(Object.getPrototypeOf(p1), null) 1441 assertSame(Object.getPrototypeOf(p1), null)
1057 assertSame(Object.getPrototypeOf(p2), o) 1442 assertSame(Object.getPrototypeOf(p2), o1)
1058 assertSame(Object.getPrototypeOf(p3), p2) 1443 assertSame(Object.getPrototypeOf(p3), p2)
1059 assertSame(Object.getPrototypeOf(p4), null) 1444 assertSame(Object.getPrototypeOf(p4), null)
1445 assertSame(Object.getPrototypeOf(o2), p3)
1060 1446
1061 assertTrue(Object.prototype.isPrototypeOf(o)) 1447 assertTrue(Object.prototype.isPrototypeOf(o1))
1062 assertFalse(Object.prototype.isPrototypeOf(p1)) 1448 assertFalse(Object.prototype.isPrototypeOf(p1))
1063 assertTrue(Object.prototype.isPrototypeOf(p2)) 1449 assertTrue(Object.prototype.isPrototypeOf(p2))
1064 assertTrue(Object.prototype.isPrototypeOf(p3)) 1450 assertTrue(Object.prototype.isPrototypeOf(p3))
1065 assertFalse(Object.prototype.isPrototypeOf(p4)) 1451 assertFalse(Object.prototype.isPrototypeOf(p4))
1066 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, o)) 1452 assertTrue(Object.prototype.isPrototypeOf(o2))
1453 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, o1))
1067 assertFalse(Object.prototype.isPrototypeOf.call(Object.prototype, p1)) 1454 assertFalse(Object.prototype.isPrototypeOf.call(Object.prototype, p1))
1068 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, p2)) 1455 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, p2))
1069 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, p3)) 1456 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, p3))
1070 assertFalse(Object.prototype.isPrototypeOf.call(Object.prototype, p4)) 1457 assertFalse(Object.prototype.isPrototypeOf.call(Object.prototype, p4))
1071 assertFalse(Object.prototype.isPrototypeOf.call(o, o)) 1458 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, o2))
1072 assertFalse(Object.prototype.isPrototypeOf.call(o, p1)) 1459 assertFalse(Object.prototype.isPrototypeOf.call(o1, o1))
1073 assertTrue(Object.prototype.isPrototypeOf.call(o, p2)) 1460 assertFalse(Object.prototype.isPrototypeOf.call(o1, p1))
1074 assertTrue(Object.prototype.isPrototypeOf.call(o, p3)) 1461 assertTrue(Object.prototype.isPrototypeOf.call(o1, p2))
1075 assertFalse(Object.prototype.isPrototypeOf.call(o, p4)) 1462 assertTrue(Object.prototype.isPrototypeOf.call(o1, p3))
1463 assertFalse(Object.prototype.isPrototypeOf.call(o1, p4))
1464 assertTrue(Object.prototype.isPrototypeOf.call(o1, o2))
1076 assertFalse(Object.prototype.isPrototypeOf.call(p1, p1)) 1465 assertFalse(Object.prototype.isPrototypeOf.call(p1, p1))
1077 assertFalse(Object.prototype.isPrototypeOf.call(p1, o)) 1466 assertFalse(Object.prototype.isPrototypeOf.call(p1, o1))
1078 assertFalse(Object.prototype.isPrototypeOf.call(p1, p2)) 1467 assertFalse(Object.prototype.isPrototypeOf.call(p1, p2))
1079 assertFalse(Object.prototype.isPrototypeOf.call(p1, p3)) 1468 assertFalse(Object.prototype.isPrototypeOf.call(p1, p3))
1080 assertFalse(Object.prototype.isPrototypeOf.call(p1, p4)) 1469 assertFalse(Object.prototype.isPrototypeOf.call(p1, p4))
1470 assertFalse(Object.prototype.isPrototypeOf.call(p1, o2))
1081 assertFalse(Object.prototype.isPrototypeOf.call(p2, p1)) 1471 assertFalse(Object.prototype.isPrototypeOf.call(p2, p1))
1082 assertFalse(Object.prototype.isPrototypeOf.call(p2, p2)) 1472 assertFalse(Object.prototype.isPrototypeOf.call(p2, p2))
1083 assertTrue(Object.prototype.isPrototypeOf.call(p2, p3)) 1473 assertTrue(Object.prototype.isPrototypeOf.call(p2, p3))
1084 assertFalse(Object.prototype.isPrototypeOf.call(p2, p4)) 1474 assertFalse(Object.prototype.isPrototypeOf.call(p2, p4))
1475 assertTrue(Object.prototype.isPrototypeOf.call(p2, o2))
1085 assertFalse(Object.prototype.isPrototypeOf.call(p3, p2)) 1476 assertFalse(Object.prototype.isPrototypeOf.call(p3, p2))
1477 assertTrue(Object.prototype.isPrototypeOf.call(p3, o2))
1478 assertFalse(Object.prototype.isPrototypeOf.call(o2, o1))
1479 assertFalse(Object.prototype.isPrototypeOf.call(o2, p1))
1480 assertFalse(Object.prototype.isPrototypeOf.call(o2, p2))
1481 assertFalse(Object.prototype.isPrototypeOf.call(o2, p3))
1482 assertFalse(Object.prototype.isPrototypeOf.call(o2, p4))
1483 assertFalse(Object.prototype.isPrototypeOf.call(o2, o2))
1086 1484
1087 var f = Proxy.createFunction({}, function() {}) 1485 var f = Proxy.createFunction({}, function() {})
1088 assertSame(Object.getPrototypeOf(f), Function.prototype) 1486 assertSame(Object.getPrototypeOf(f), Function.prototype)
1089 assertTrue(Object.prototype.isPrototypeOf(f)) 1487 assertTrue(Object.prototype.isPrototypeOf(f))
1090 assertTrue(Object.prototype.isPrototypeOf.call(Function.prototype, f)) 1488 assertTrue(Object.prototype.isPrototypeOf.call(Function.prototype, f))
1091 } 1489 }
1092 1490
1093 TestPrototype() 1491 TestPrototype()
1094 1492
1095 1493
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1260 return function() { return [1, 2] } 1658 return function() { return [1, 2] }
1261 }, 1659 },
1262 getOwnPropertyDescriptor: function(k) { throw "myexn" } 1660 getOwnPropertyDescriptor: function(k) { throw "myexn" }
1263 }) 1661 })
1264 1662
1265 1663
1266 1664
1267 // Fixing (Object.freeze, Object.seal, Object.preventExtensions, 1665 // Fixing (Object.freeze, Object.seal, Object.preventExtensions,
1268 // Object.isFrozen, Object.isSealed, Object.isExtensible) 1666 // Object.isFrozen, Object.isSealed, Object.isExtensible)
1269 1667
1270 // TODO(rossberg): use TestWithProxies to include funciton proxies
1271 function TestFix(names, handler) { 1668 function TestFix(names, handler) {
1272 var proto = {p: 77} 1669 var proto = {p: 77}
1273 var assertFixing = function(o, s, f, e) { 1670 var assertFixing = function(o, s, f, e) {
1274 assertEquals(s, Object.isSealed(o)) 1671 assertEquals(s, Object.isSealed(o))
1275 assertEquals(f, Object.isFrozen(o)) 1672 assertEquals(f, Object.isFrozen(o))
1276 assertEquals(e, Object.isExtensible(o)) 1673 assertEquals(e, Object.isExtensible(o))
1277 } 1674 }
1278 1675
1279 var p1 = Proxy.create(handler, proto) 1676 var p1 = Proxy.create(handler, proto)
1280 assertFixing(p1, false, false, true) 1677 assertFixing(p1, false, false, true)
(...skipping 26 matching lines...) Expand all
1307 1704
1308 var p3 = Proxy.create(handler, proto) 1705 var p3 = Proxy.create(handler, proto)
1309 assertFixing(p3, false, false, true) 1706 assertFixing(p3, false, false, true)
1310 Object.preventExtensions(p3) 1707 Object.preventExtensions(p3)
1311 assertFixing(p3, names.length === 0, names.length === 0, false) 1708 assertFixing(p3, names.length === 0, names.length === 0, false)
1312 assertArrayEquals(names.sort(), Object.getOwnPropertyNames(p3).sort()) 1709 assertArrayEquals(names.sort(), Object.getOwnPropertyNames(p3).sort())
1313 assertArrayEquals(names.filter(function(x) {return x < "z"}).sort(), 1710 assertArrayEquals(names.filter(function(x) {return x < "z"}).sort(),
1314 Object.keys(p3).sort()) 1711 Object.keys(p3).sort())
1315 assertEquals(proto, Object.getPrototypeOf(p3)) 1712 assertEquals(proto, Object.getPrototypeOf(p3))
1316 assertEquals(77, p3.p) 1713 assertEquals(77, p3.p)
1714
1715 var p = Proxy.create(handler, proto)
1716 var o = Object.create(p)
1717 assertFixing(p, false, false, true)
1718 assertFixing(o, false, false, true)
1719 Object.freeze(o)
1720 assertFixing(p, false, false, true)
1721 assertFixing(o, true, true, false)
1317 } 1722 }
1318 1723
1319 TestFix([], { 1724 TestFix([], {
1320 fix: function() { return {} } 1725 fix: function() { return {} }
1321 }) 1726 })
1322 1727
1323 TestFix(["a", "b", "c", "d", "zz"], { 1728 TestFix(["a", "b", "c", "3", "zz"], {
1324 fix: function() { 1729 fix: function() {
1325 return { 1730 return {
1326 a: {value: "a", writable: true, configurable: false, enumerable: true}, 1731 a: {value: "a", writable: true, configurable: false, enumerable: true},
1327 b: {value: 33, writable: false, configurable: false, enumerable: true}, 1732 b: {value: 33, writable: false, configurable: false, enumerable: true},
1328 c: {value: 0, writable: true, configurable: true, enumerable: true}, 1733 c: {value: 0, writable: true, configurable: true, enumerable: true},
1329 d: {value: true, writable: false, configurable: true, enumerable: true}, 1734 '3': {value: true, writable: false, configurable: true, enumerable: true},
1330 zz: {value: 0, enumerable: false} 1735 zz: {value: 0, enumerable: false}
1331 } 1736 }
1332 } 1737 }
1333 }) 1738 })
1334 1739
1335 TestFix(["a"], { 1740 TestFix(["a"], {
1336 fix: function() { return this.fix2() }, 1741 fix: function() { return this.fix2() },
1337 fix2: function() { 1742 fix2: function() {
1338 return {a: {value: 4, writable: true, configurable: true, enumerable: true}} 1743 return {a: {value: 4, writable: true, configurable: true, enumerable: true}}
1339 } 1744 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1419 assertEquals("my_proxy", Object.prototype.toLocaleString.call(p)) 1824 assertEquals("my_proxy", Object.prototype.toLocaleString.call(p))
1420 assertEquals("toString", key) 1825 assertEquals("toString", key)
1421 1826
1422 var f = Proxy.createFunction(handler, function() {}) 1827 var f = Proxy.createFunction(handler, function() {})
1423 key = "" 1828 key = ""
1424 assertEquals("[object Function]", Object.prototype.toString.call(f)) 1829 assertEquals("[object Function]", Object.prototype.toString.call(f))
1425 assertEquals("", key) 1830 assertEquals("", key)
1426 assertEquals("my_proxy", Object.prototype.toLocaleString.call(f)) 1831 assertEquals("my_proxy", Object.prototype.toLocaleString.call(f))
1427 assertEquals("toString", key) 1832 assertEquals("toString", key)
1428 assertDoesNotThrow(function(){ Function.prototype.toString.call(f) }) 1833 assertDoesNotThrow(function(){ Function.prototype.toString.call(f) })
1834
1835 var o = Object.create(p)
1836 key = ""
1837 assertEquals("[object Object]", Object.prototype.toString.call(o))
1838 assertEquals("", key)
1839 assertEquals("my_proxy", Object.prototype.toLocaleString.call(o))
1840 assertEquals("toString", key)
1429 } 1841 }
1430 1842
1431 TestToString({ 1843 TestToString({
1432 get: function(r, k) { key = k; return function() { return "my_proxy" } } 1844 get: function(r, k) { key = k; return function() { return "my_proxy" } }
1433 }) 1845 })
1434 1846
1435 TestToString({ 1847 TestToString({
1436 get: function(r, k) { return this.get2(r, k) }, 1848 get: function(r, k) { return this.get2(r, k) },
1437 get2: function(r, k) { key = k; return function() { return "my_proxy" } } 1849 get2: function(r, k) { key = k; return function() { return "my_proxy" } }
1438 }) 1850 })
1439 1851
1440 TestToString(Proxy.create({ 1852 TestToString(Proxy.create({
1441 get: function(pr, pk) { 1853 get: function(pr, pk) {
1442 return function(r, k) { key = k; return function() { return "my_proxy" } } 1854 return function(r, k) { key = k; return function() { return "my_proxy" } }
1443 } 1855 }
1444 })) 1856 }))
1445 1857
1446 1858
1447 function TestToStringThrow(handler) { 1859 function TestToStringThrow(handler) {
1448 var p = Proxy.create(handler) 1860 var p = Proxy.create(handler)
1449 assertEquals("[object Object]", Object.prototype.toString.call(p)) 1861 assertEquals("[object Object]", Object.prototype.toString.call(p))
1450 assertThrows(function(){ Object.prototype.toLocaleString.call(p) }, "myexn") 1862 assertThrows(function(){ Object.prototype.toLocaleString.call(p) }, "myexn")
1451 1863
1452 var f = Proxy.createFunction(handler, function() {}) 1864 var f = Proxy.createFunction(handler, function() {})
1453 assertEquals("[object Function]", Object.prototype.toString.call(f)) 1865 assertEquals("[object Function]", Object.prototype.toString.call(f))
1454 assertThrows(function(){ Object.prototype.toLocaleString.call(f) }, "myexn") 1866 assertThrows(function(){ Object.prototype.toLocaleString.call(f) }, "myexn")
1867
1868 var o = Object.create(p)
1869 assertEquals("[object Object]", Object.prototype.toString.call(o))
1870 assertThrows(function(){ Object.prototype.toLocaleString.call(o) }, "myexn")
1455 } 1871 }
1456 1872
1457 TestToStringThrow({ 1873 TestToStringThrow({
1458 get: function(r, k) { throw "myexn" } 1874 get: function(r, k) { throw "myexn" }
1459 }) 1875 })
1460 1876
1461 TestToStringThrow({ 1877 TestToStringThrow({
1462 get: function(r, k) { return function() { throw "myexn" } } 1878 get: function(r, k) { return function() { throw "myexn" } }
1463 }) 1879 })
1464 1880
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1503 } 1919 }
1504 1920
1505 function TestIsEnumerable2(handler, create) { 1921 function TestIsEnumerable2(handler, create) {
1506 var p = create(handler) 1922 var p = create(handler)
1507 assertTrue(Object.prototype.propertyIsEnumerable.call(p, "a")) 1923 assertTrue(Object.prototype.propertyIsEnumerable.call(p, "a"))
1508 assertEquals("a", key) 1924 assertEquals("a", key)
1509 assertTrue(Object.prototype.propertyIsEnumerable.call(p, 2)) 1925 assertTrue(Object.prototype.propertyIsEnumerable.call(p, 2))
1510 assertEquals("2", key) 1926 assertEquals("2", key)
1511 assertFalse(Object.prototype.propertyIsEnumerable.call(p, "z")) 1927 assertFalse(Object.prototype.propertyIsEnumerable.call(p, "z"))
1512 assertEquals("z", key) 1928 assertEquals("z", key)
1929
1930 var o = Object.create(p)
1931 key = ""
1932 assertFalse(Object.prototype.propertyIsEnumerable.call(o, "a"))
1933 assertEquals("", key) // trap not invoked
1513 } 1934 }
1514 1935
1515 TestIsEnumerable({ 1936 TestIsEnumerable({
1516 getOwnPropertyDescriptor: function(k) { 1937 getOwnPropertyDescriptor: function(k) {
1517 key = k; return {enumerable: k < "z", configurable: true} 1938 key = k; return {enumerable: k < "z", configurable: true}
1518 }, 1939 },
1519 }) 1940 })
1520 1941
1521 TestIsEnumerable({ 1942 TestIsEnumerable({
1522 getOwnPropertyDescriptor: function(k) { 1943 getOwnPropertyDescriptor: function(k) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1579 get: function(pr, pk) { 2000 get: function(pr, pk) {
1580 return function(k) { throw "myexn" } 2001 return function(k) { throw "myexn" }
1581 } 2002 }
1582 })) 2003 }))
1583 2004
1584 2005
1585 2006
1586 // Calling (call, Function.prototype.call, Function.prototype.apply, 2007 // Calling (call, Function.prototype.call, Function.prototype.apply,
1587 // Function.prototype.bind). 2008 // Function.prototype.bind).
1588 2009
1589 var global = this 2010 var global_object = this
1590 var receiver 2011 var receiver
1591 2012
2013 function CreateFrozen(handler, callTrap, constructTrap) {
2014 if (handler.fix === undefined) handler.fix = function() { return {} }
2015 var f = Proxy.createFunction(handler, callTrap, constructTrap)
2016 Object.freeze(f)
2017 return f
2018 }
2019
1592 function TestCall(isStrict, callTrap) { 2020 function TestCall(isStrict, callTrap) {
1593 assertEquals(42, callTrap(5, 37)) 2021 assertEquals(42, callTrap(5, 37))
1594 // TODO(rossberg): unrelated bug: this does not succeed for optimized code. 2022 // TODO(rossberg): unrelated bug: this does not succeed for optimized code:
1595 // assertEquals(isStrict ? undefined : global, receiver) 2023 // assertEquals(isStrict ? undefined : global_object, receiver)
1596 2024
1597 var f = Proxy.createFunction({fix: function() { return {} }}, callTrap) 2025 var f = Proxy.createFunction({}, callTrap)
1598 receiver = 333 2026 receiver = 333
1599 assertEquals(42, f(11, 31)) 2027 assertEquals(42, f(11, 31))
1600 assertEquals(isStrict ? undefined : global, receiver) 2028 assertEquals(isStrict ? undefined : global_object, receiver)
1601 var o = {} 2029 var o = {}
1602 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) 2030 assertEquals(42, Function.prototype.call.call(f, o, 20, 22))
1603 assertEquals(o, receiver) 2031 assertEquals(o, receiver)
1604 assertEquals(43, Function.prototype.call.call(f, null, 20, 23)) 2032 assertEquals(43, Function.prototype.call.call(f, null, 20, 23))
1605 assertEquals(isStrict ? null : global, receiver) 2033 assertEquals(isStrict ? null : global_object, receiver)
1606 assertEquals(44, Function.prototype.call.call(f, 2, 21, 23)) 2034 assertEquals(44, Function.prototype.call.call(f, 2, 21, 23))
1607 assertEquals(2, receiver.valueOf()) 2035 assertEquals(2, receiver.valueOf())
1608 receiver = 333 2036 receiver = 333
1609 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) 2037 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15]))
1610 assertEquals(o, receiver) 2038 assertEquals(o, receiver)
1611 var ff = Function.prototype.bind.call(f, o, 12) 2039 var ff = Function.prototype.bind.call(f, o, 12)
1612 receiver = 333 2040 receiver = 333
1613 assertEquals(42, ff(30)) 2041 assertEquals(42, ff(30))
1614 assertEquals(o, receiver) 2042 assertEquals(o, receiver)
1615 receiver = 333 2043 receiver = 333
1616 assertEquals(32, Function.prototype.apply.call(ff, {}, [20])) 2044 assertEquals(32, Function.prototype.apply.call(ff, {}, [20]))
1617 assertEquals(o, receiver) 2045 assertEquals(o, receiver)
1618 2046
1619 Object.freeze(f) 2047 var f = CreateFrozen({}, callTrap)
1620 receiver = 333 2048 receiver = 333
1621 assertEquals(42, f(11, 31)) 2049 assertEquals(42, f(11, 31))
1622 // TODO(rossberg): unrelated bug: this does not succeed for optimized code. 2050 // TODO(rossberg): unrelated bug: this does not succeed for optimized code.
1623 // assertEquals(isStrict ? undefined : global, receiver) 2051 // assertEquals(isStrict ? undefined : global, receiver)
1624 receiver = 333 2052 receiver = 333
1625 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) 2053 assertEquals(42, Function.prototype.call.call(f, o, 20, 22))
1626 assertEquals(o, receiver) 2054 assertEquals(o, receiver)
1627 receiver = 333 2055 receiver = 333
1628 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) 2056 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15]))
1629 assertEquals(o, receiver) 2057 assertEquals(o, receiver)
1630 receiver = 333 2058 receiver = 333
1631 assertEquals(42, ff(30)) 2059 assertEquals(42, ff(30))
1632 assertEquals(o, receiver) 2060 assertEquals(o, receiver)
1633 receiver = 333 2061 receiver = 333
(...skipping 12 matching lines...) Expand all
1646 2074
1647 TestCall(false, Proxy.createFunction({}, function(x, y) { 2075 TestCall(false, Proxy.createFunction({}, function(x, y) {
1648 receiver = this; return x + y 2076 receiver = this; return x + y
1649 })) 2077 }))
1650 2078
1651 TestCall(true, Proxy.createFunction({}, function(x, y) { 2079 TestCall(true, Proxy.createFunction({}, function(x, y) {
1652 "use strict"; 2080 "use strict";
1653 receiver = this; return x + y 2081 receiver = this; return x + y
1654 })) 2082 }))
1655 2083
1656 var p = Proxy.createFunction({fix: function() {return {}}}, function(x, y) { 2084 TestCall(false, CreateFrozen({}, function(x, y) {
1657 receiver = this; return x + y 2085 receiver = this; return x + y
1658 }) 2086 }))
1659 TestCall(false, p)
1660 Object.freeze(p)
1661 TestCall(false, p)
1662 2087
1663 2088
1664 function TestCallThrow(callTrap) { 2089 function TestCallThrow(callTrap) {
1665 var f = Proxy.createFunction({fix: function() {return {}}}, callTrap) 2090 var f = Proxy.createFunction({}, callTrap)
1666 assertThrows(function(){ f(11) }, "myexn") 2091 assertThrows(function(){ f(11) }, "myexn")
1667 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") 2092 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn")
1668 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") 2093 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn")
1669 2094
1670 Object.freeze(f) 2095 var f = CreateFrozen({}, callTrap)
1671 assertThrows(function(){ f(11) }, "myexn") 2096 assertThrows(function(){ f(11) }, "myexn")
1672 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") 2097 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn")
1673 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") 2098 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn")
1674 } 2099 }
1675 2100
1676 TestCallThrow(function() { throw "myexn" }) 2101 TestCallThrow(function() { throw "myexn" })
1677 TestCallThrow(Proxy.createFunction({}, function() { throw "myexn" })) 2102 TestCallThrow(Proxy.createFunction({}, function() { throw "myexn" }))
1678 2103 TestCallThrow(CreateFrozen({}, function() { throw "myexn" }))
1679 var p = Proxy.createFunction( 2104
1680 {fix: function() {return {}}}, function() { throw "myexn" }) 2105
1681 Object.freeze(p) 2106
1682 TestCallThrow(p) 2107 // Construction (new).
2108
2109 var prototype = {}
2110 var receiver
2111
2112 var handlerWithPrototype = {
2113 fix: function() { return {prototype: prototype} },
2114 get: function(r, n) { assertEquals("prototype", n); return prototype }
2115 }
2116
2117 var handlerSansPrototype = {
2118 fix: function() { return {} },
2119 get: function(r, n) { assertEquals("prototype", n); return undefined }
2120 }
2121
2122 function ReturnUndef(x, y) { "use strict"; receiver = this; this.sum = x + y }
2123 function ReturnThis(x, y) { "use strict"; receiver = this; this.sum = x + y; ret urn this }
2124 function ReturnNew(x, y) { "use strict"; receiver = this; return {sum: x + y} }
2125 function ReturnNewWithProto(x, y) {
2126 "use strict";
2127 receiver = this;
2128 var result = Object.create(prototype)
2129 result.sum = x + y
2130 return result
2131 }
2132
2133 function TestConstruct(proto, constructTrap) {
2134 TestConstruct2(proto, constructTrap, handlerWithPrototype)
2135 TestConstruct2(proto, constructTrap, handlerSansPrototype)
2136 }
2137
2138 function TestConstruct2(proto, constructTrap, handler) {
2139 var f = Proxy.createFunction(handler, function() {}, constructTrap)
2140 var o = new f(11, 31)
2141 // TODO(rossberg): doesn't hold, due to unrelated bug.
2142 // assertEquals(undefined, receiver)
2143 assertEquals(42, o.sum)
2144 assertSame(proto, Object.getPrototypeOf(o))
2145
2146 var f = CreateFrozen(handler, function() {}, constructTrap)
2147 var o = new f(11, 32)
2148 // TODO(rossberg): doesn't hold, due to unrelated bug.
2149 // assertEquals(undefined, receiver)
2150 assertEquals(43, o.sum)
2151 assertSame(proto, Object.getPrototypeOf(o))
2152 }
2153
2154 TestConstruct(Object.prototype, ReturnNew)
2155 TestConstruct(prototype, ReturnNewWithProto)
2156
2157 TestConstruct(Object.prototype, Proxy.createFunction({}, ReturnNew))
2158 TestConstruct(prototype, Proxy.createFunction({}, ReturnNewWithProto))
2159
2160 TestConstruct(Object.prototype, CreateFrozen({}, ReturnNew))
2161 TestConstruct(prototype, CreateFrozen({}, ReturnNewWithProto))
2162
2163
2164 function TestConstructFromCall(proto, returnsThis, callTrap) {
2165 TestConstructFromCall2(proto, returnsThis, callTrap, handlerWithPrototype)
2166 TestConstructFromCall2(proto, returnsThis, callTrap, handlerSansPrototype)
2167 }
2168
2169 function TestConstructFromCall2(proto, returnsThis, callTrap, handler) {
2170 var f = Proxy.createFunction(handler, callTrap)
2171 var o = new f(11, 31)
2172 if (returnsThis) assertEquals(o, receiver)
2173 assertEquals(42, o.sum)
2174 assertSame(proto, Object.getPrototypeOf(o))
2175
2176 var f = CreateFrozen(handler, callTrap)
2177 var o = new f(11, 32)
2178 if (returnsThis) assertEquals(o, receiver)
2179 assertEquals(43, o.sum)
2180 assertSame(proto, Object.getPrototypeOf(o))
2181 }
2182
2183 TestConstructFromCall(Object.prototype, true, ReturnUndef)
2184 TestConstructFromCall(Object.prototype, true, ReturnThis)
2185 TestConstructFromCall(Object.prototype, false, ReturnNew)
2186 TestConstructFromCall(prototype, false, ReturnNewWithProto)
2187
2188 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnUnd ef))
2189 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnThi s))
2190 TestConstructFromCall(Object.prototype, false, Proxy.createFunction({}, ReturnNe w))
2191 TestConstructFromCall(prototype, false, Proxy.createFunction({}, ReturnNewWithPr oto))
2192
2193 TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnUndef))
2194 TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnThis))
2195 TestConstructFromCall(Object.prototype, false, CreateFrozen({}, ReturnNew))
2196 TestConstructFromCall(prototype, false, CreateFrozen({}, ReturnNewWithProto))
2197
2198 ReturnUndef.prototype = prototype
2199 ReturnThis.prototype = prototype
2200 ReturnNew.prototype = prototype
2201 ReturnNewWithProto.prototype = prototype
2202
2203 TestConstructFromCall(prototype, true, ReturnUndef)
2204 TestConstructFromCall(prototype, true, ReturnThis)
2205 TestConstructFromCall(Object.prototype, false, ReturnNew)
2206 TestConstructFromCall(prototype, false, ReturnNewWithProto)
2207
2208 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnUnd ef))
2209 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnThi s))
2210 TestConstructFromCall(Object.prototype, false, Proxy.createFunction({}, ReturnNe w))
2211 TestConstructFromCall(prototype, false, Proxy.createFunction({}, ReturnNewWithPr oto))
2212
2213 TestConstructFromCall(prototype, true, Proxy.createFunction(handlerWithPrototype , ReturnUndef))
2214 TestConstructFromCall(prototype, true, Proxy.createFunction(handlerWithPrototype , ReturnThis))
2215 TestConstructFromCall(Object.prototype, false, Proxy.createFunction(handlerWithP rototype, ReturnNew))
2216 TestConstructFromCall(prototype, false, Proxy.createFunction(handlerWithPrototyp e, ReturnNewWithProto))
2217
2218 TestConstructFromCall(prototype, true, CreateFrozen(handlerWithPrototype, Return Undef))
2219 TestConstructFromCall(prototype, true, CreateFrozen(handlerWithPrototype, Return This))
2220 TestConstructFromCall(Object.prototype, false, CreateFrozen(handlerWithPrototype , ReturnNew))
2221 TestConstructFromCall(prototype, false, CreateFrozen(handlerWithPrototype, Retur nNewWithProto))
2222
2223
2224 function TestConstructThrow(trap) {
2225 TestConstructThrow2(Proxy.createFunction({fix: function() {return {}}}, trap))
2226 TestConstructThrow2(Proxy.createFunction({fix: function() {return {}}},
2227 function() {}, trap))
2228 }
2229
2230 function TestConstructThrow2(f) {
2231 assertThrows(function(){ new f(11) }, "myexn")
2232 Object.freeze(f)
2233 assertThrows(function(){ new f(11) }, "myexn")
2234 }
2235
2236 TestConstructThrow(function() { throw "myexn" })
2237 TestConstructThrow(Proxy.createFunction({}, function() { throw "myexn" }))
2238 TestConstructThrow(CreateFrozen({}, function() { throw "myexn" }))
2239
2240
2241
2242 // Getters and setters.
2243
2244 var value
2245 var receiver
2246
2247 function TestAccessorCall(getterCallTrap, setterCallTrap) {
2248 var handler = {fix: function() { return {} }}
2249 var pgetter = Proxy.createFunction(handler, getterCallTrap)
2250 var psetter = Proxy.createFunction(handler, setterCallTrap)
2251
2252 var o = {}
2253 var oo = Object.create(o)
2254 Object.defineProperty(o, "a", {get: pgetter, set: psetter})
2255 Object.defineProperty(o, "b", {get: pgetter})
2256 Object.defineProperty(o, "c", {set: psetter})
2257 Object.defineProperty(o, "3", {get: pgetter, set: psetter})
2258 Object.defineProperty(oo, "a", {value: 43})
2259
2260 receiver = ""
2261 assertEquals(42, o.a)
2262 assertSame(o, receiver)
2263 receiver = ""
2264 assertEquals(42, o.b)
2265 assertSame(o, receiver)
2266 receiver = ""
2267 assertEquals(undefined, o.c)
2268 assertEquals("", receiver)
2269 receiver = ""
2270 assertEquals(42, o["a"])
2271 assertSame(o, receiver)
2272 receiver = ""
2273 assertEquals(42, o[3])
2274 assertSame(o, receiver)
2275
2276 receiver = ""
2277 assertEquals(43, oo.a)
2278 assertEquals("", receiver)
2279 receiver = ""
2280 assertEquals(42, oo.b)
2281 assertSame(o, receiver)
2282 receiver = ""
2283 assertEquals(undefined, oo.c)
2284 assertEquals("", receiver)
2285 receiver = ""
2286 assertEquals(43, oo["a"])
2287 assertEquals("", receiver)
2288 receiver = ""
2289 assertEquals(42, oo[3])
2290 assertSame(o, receiver)
2291
2292 receiver = ""
2293 assertEquals(50, o.a = 50)
2294 assertSame(o, receiver)
2295 assertEquals(50, value)
2296 receiver = ""
2297 assertEquals(51, o.b = 51)
2298 assertEquals("", receiver)
2299 assertEquals(50, value) // no setter
2300 assertThrows(function() { "use strict"; o.b = 51 }, TypeError)
2301 receiver = ""
2302 assertEquals(52, o.c = 52)
2303 assertSame(o, receiver)
2304 assertEquals(52, value)
2305 receiver = ""
2306 assertEquals(53, o["a"] = 53)
2307 assertSame(o, receiver)
2308 assertEquals(53, value)
2309 receiver = ""
2310 assertEquals(54, o[3] = 54)
2311 assertSame(o, receiver)
2312 assertEquals(54, value)
2313
2314 value = 0
2315 receiver = ""
2316 assertEquals(60, oo.a = 60)
2317 assertEquals("", receiver)
2318 assertEquals(0, value) // oo has own 'a'
2319 assertEquals(61, oo.b = 61)
2320 assertSame("", receiver)
2321 assertEquals(0, value) // no setter
2322 assertThrows(function() { "use strict"; oo.b = 61 }, TypeError)
2323 receiver = ""
2324 assertEquals(62, oo.c = 62)
2325 assertSame(oo, receiver)
2326 assertEquals(62, value)
2327 receiver = ""
2328 assertEquals(63, oo["c"] = 63)
2329 assertSame(oo, receiver)
2330 assertEquals(63, value)
2331 receiver = ""
2332 assertEquals(64, oo[3] = 64)
2333 assertSame(oo, receiver)
2334 assertEquals(64, value)
2335 }
2336
2337 TestAccessorCall(
2338 function() { receiver = this; return 42 },
2339 function(x) { receiver = this; value = x }
2340 )
2341
2342 TestAccessorCall(
2343 function() { "use strict"; receiver = this; return 42 },
2344 function(x) { "use strict"; receiver = this; value = x }
2345 )
2346
2347 TestAccessorCall(
2348 Proxy.createFunction({}, function() { receiver = this; return 42 }),
2349 Proxy.createFunction({}, function(x) { receiver = this; value = x })
2350 )
2351
2352 TestAccessorCall(
2353 CreateFrozen({}, function() { receiver = this; return 42 }),
2354 CreateFrozen({}, function(x) { receiver = this; value = x })
2355 )
OLDNEW
« no previous file with comments | « test/mjsunit/harmony/block-let-declaration.js ('k') | test/mjsunit/harmony/proxies-hash.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698