OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 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 |
(...skipping 14 matching lines...) Expand all Loading... |
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 for proxies not implemented. |
32 // TODO(rossberg): inheritance from proxies not implemented. | 32 // TODO(rossberg): inheritance from proxies not implemented. |
33 // TODO(rossberg): function proxies as constructors not implemented. | 33 // TODO(rossberg): function proxies as constructors not implemented. |
34 | 34 |
35 | |
36 // Helper. | 35 // Helper. |
37 | 36 |
38 function TestWithProxies(test, handler) { | 37 function TestWithProxies(test, handler) { |
39 test(handler, Proxy.create) | 38 test(handler, Proxy.create) |
40 test(handler, function(h) {return Proxy.createFunction(h, function() {})}) | 39 test(handler, function(h) {return Proxy.createFunction(h, function() {})}) |
41 } | 40 } |
42 | 41 |
43 | 42 |
44 // Getters. | 43 // Getters. |
45 | 44 |
(...skipping 1530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1576 get: function(pr, pk) { | 1575 get: function(pr, pk) { |
1577 return function(k) { throw "myexn" } | 1576 return function(k) { throw "myexn" } |
1578 } | 1577 } |
1579 })) | 1578 })) |
1580 | 1579 |
1581 | 1580 |
1582 | 1581 |
1583 // Calling (call, Function.prototype.call, Function.prototype.apply, | 1582 // Calling (call, Function.prototype.call, Function.prototype.apply, |
1584 // Function.prototype.bind). | 1583 // Function.prototype.bind). |
1585 | 1584 |
1586 var global = this | 1585 var global_object = this |
1587 var receiver | 1586 var receiver |
1588 | 1587 |
| 1588 function CreateFrozen(handler, callTrap, constructTrap) { |
| 1589 if (handler.fix === undefined) handler.fix = function() { return {} } |
| 1590 var f = Proxy.createFunction(handler, callTrap, constructTrap) |
| 1591 Object.freeze(f) |
| 1592 return f |
| 1593 } |
| 1594 |
1589 function TestCall(isStrict, callTrap) { | 1595 function TestCall(isStrict, callTrap) { |
1590 assertEquals(42, callTrap(5, 37)) | 1596 assertEquals(42, callTrap(5, 37)) |
1591 // TODO(rossberg): unrelated bug: this does not succeed for optimized code. | 1597 // TODO(rossberg): unrelated bug: this does not succeed for optimized code. |
1592 // assertEquals(isStrict ? undefined : global, receiver) | 1598 // assertEquals(isStrict ? undefined : global_object, receiver) |
1593 | 1599 |
1594 var f = Proxy.createFunction({fix: function() { return {} }}, callTrap) | 1600 var f = Proxy.createFunction({}, callTrap) |
1595 receiver = 333 | 1601 receiver = 333 |
1596 assertEquals(42, f(11, 31)) | 1602 assertEquals(42, f(11, 31)) |
1597 assertEquals(isStrict ? undefined : global, receiver) | 1603 assertEquals(isStrict ? undefined : global_object, receiver) |
1598 var o = {} | 1604 var o = {} |
1599 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) | 1605 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) |
1600 assertEquals(o, receiver) | 1606 assertEquals(o, receiver) |
1601 receiver = 333 | 1607 receiver = 333 |
1602 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) | 1608 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) |
1603 assertEquals(o, receiver) | 1609 assertEquals(o, receiver) |
1604 var ff = Function.prototype.bind.call(f, o, 12) | 1610 var ff = Function.prototype.bind.call(f, o, 12) |
1605 receiver = 333 | 1611 receiver = 333 |
1606 assertEquals(42, ff(30)) | 1612 assertEquals(42, ff(30)) |
1607 assertEquals(o, receiver) | 1613 assertEquals(o, receiver) |
1608 receiver = 333 | 1614 receiver = 333 |
1609 assertEquals(32, Function.prototype.apply.call(ff, {}, [20])) | 1615 assertEquals(32, Function.prototype.apply.call(ff, {}, [20])) |
1610 assertEquals(o, receiver) | 1616 assertEquals(o, receiver) |
1611 | 1617 |
1612 Object.freeze(f) | 1618 var f = CreateFrozen({}, callTrap) |
1613 receiver = 333 | 1619 receiver = 333 |
1614 assertEquals(42, f(11, 31)) | 1620 assertEquals(42, f(11, 31)) |
1615 // TODO(rossberg): unrelated bug: this does not succeed for optimized code. | 1621 // TODO(rossberg): unrelated bug: this does not succeed for optimized code. |
1616 // assertEquals(isStrict ? undefined : global, receiver) | 1622 // assertEquals(isStrict ? undefined : global, receiver) |
1617 receiver = 333 | 1623 receiver = 333 |
1618 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) | 1624 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) |
1619 assertEquals(o, receiver) | 1625 assertEquals(o, receiver) |
1620 receiver = 333 | 1626 receiver = 333 |
1621 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) | 1627 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) |
1622 assertEquals(o, receiver) | 1628 assertEquals(o, receiver) |
(...skipping 16 matching lines...) Expand all Loading... |
1639 | 1645 |
1640 TestCall(false, Proxy.createFunction({}, function(x, y) { | 1646 TestCall(false, Proxy.createFunction({}, function(x, y) { |
1641 receiver = this; return x + y | 1647 receiver = this; return x + y |
1642 })) | 1648 })) |
1643 | 1649 |
1644 TestCall(true, Proxy.createFunction({}, function(x, y) { | 1650 TestCall(true, Proxy.createFunction({}, function(x, y) { |
1645 "use strict"; | 1651 "use strict"; |
1646 receiver = this; return x + y | 1652 receiver = this; return x + y |
1647 })) | 1653 })) |
1648 | 1654 |
1649 var p = Proxy.createFunction({fix: function() {return {}}}, function(x, y) { | 1655 TestCall(false, CreateFrozen({}, function(x, y) { |
1650 receiver = this; return x + y | 1656 receiver = this; return x + y |
1651 }) | 1657 })) |
1652 Object.freeze(p) | |
1653 TestCall(false, p) | |
1654 | 1658 |
1655 | 1659 |
1656 function TestCallThrow(callTrap) { | 1660 function TestCallThrow(callTrap) { |
1657 var f = Proxy.createFunction({fix: function() {return {}}}, callTrap) | 1661 var f = Proxy.createFunction({}, callTrap) |
1658 assertThrows(function(){ f(11) }, "myexn") | 1662 assertThrows(function(){ f(11) }, "myexn") |
1659 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") | 1663 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") |
1660 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") | 1664 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") |
1661 | 1665 |
1662 Object.freeze(f) | 1666 var f = CreateFrozen({}, callTrap) |
1663 assertThrows(function(){ f(11) }, "myexn") | 1667 assertThrows(function(){ f(11) }, "myexn") |
1664 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") | 1668 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") |
1665 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") | 1669 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") |
1666 } | 1670 } |
1667 | 1671 |
1668 TestCallThrow(function() { throw "myexn" }) | 1672 TestCallThrow(function() { throw "myexn" }) |
1669 TestCallThrow(Proxy.createFunction({}, function() { throw "myexn" })) | 1673 TestCallThrow(Proxy.createFunction({}, function() { throw "myexn" })) |
1670 | 1674 |
1671 var p = Proxy.createFunction( | 1675 var p = Proxy.createFunction( |
1672 {fix: function() {return {}}}, function() { throw "myexn" }) | 1676 {fix: function() {return {}}}, function() { throw "myexn" }) |
1673 Object.freeze(p) | 1677 Object.freeze(p) |
1674 TestCallThrow(p) | 1678 TestCallThrow(p) |
| 1679 |
| 1680 |
| 1681 |
| 1682 // Construction (new). |
| 1683 |
| 1684 var prototype = {} |
| 1685 var receiver |
| 1686 |
| 1687 var handlerWithPrototype = { |
| 1688 fix: function() { return {prototype: prototype} }, |
| 1689 get: function(r, n) { assertEquals("prototype", n); return prototype } |
| 1690 } |
| 1691 |
| 1692 var handlerSansPrototype = { |
| 1693 fix: function() { return {} }, |
| 1694 get: function(r, n) { assertEquals("prototype", n); return undefined } |
| 1695 } |
| 1696 |
| 1697 function ReturnUndef(x, y) { "use strict"; receiver = this; this.sum = x + y } |
| 1698 function ReturnThis(x, y) { "use strict"; receiver = this; this.sum = x + y; ret
urn this } |
| 1699 function ReturnNew(x, y) { "use strict"; receiver = this; return {sum: x + y} } |
| 1700 function ReturnNewWithProto(x, y) { |
| 1701 "use strict"; |
| 1702 receiver = this; |
| 1703 var result = Object.create(prototype) |
| 1704 result.sum = x + y |
| 1705 return result |
| 1706 } |
| 1707 |
| 1708 function TestConstruct(proto, constructTrap) { |
| 1709 TestConstruct2(proto, constructTrap, handlerWithPrototype) |
| 1710 TestConstruct2(proto, constructTrap, handlerSansPrototype) |
| 1711 } |
| 1712 |
| 1713 function TestConstruct2(proto, constructTrap, handler) { |
| 1714 var f = Proxy.createFunction(handler, function() {}, constructTrap) |
| 1715 var o = new f(11, 31) |
| 1716 // TODO(rossberg): doesn't hold, due to unrelated bug. |
| 1717 // assertEquals(undefined, receiver) |
| 1718 assertEquals(42, o.sum) |
| 1719 assertSame(proto, Object.getPrototypeOf(o)) |
| 1720 |
| 1721 var f = CreateFrozen(handler, function() {}, constructTrap) |
| 1722 var o = new f(11, 32) |
| 1723 // TODO(rossberg): doesn't hold, due to unrelated bug. |
| 1724 // assertEquals(undefined, receiver) |
| 1725 assertEquals(43, o.sum) |
| 1726 assertSame(proto, Object.getPrototypeOf(o)) |
| 1727 } |
| 1728 |
| 1729 TestConstruct(Object.prototype, ReturnNew) |
| 1730 TestConstruct(prototype, ReturnNewWithProto) |
| 1731 |
| 1732 TestConstruct(Object.prototype, Proxy.createFunction({}, ReturnNew)) |
| 1733 TestConstruct(prototype, Proxy.createFunction({}, ReturnNewWithProto)) |
| 1734 |
| 1735 TestConstruct(Object.prototype, CreateFrozen({}, ReturnNew)) |
| 1736 TestConstruct(prototype, CreateFrozen({}, ReturnNewWithProto)) |
| 1737 |
| 1738 |
| 1739 function TestConstructFromCall(proto, returnsThis, callTrap) { |
| 1740 TestConstructFromCall2(proto, returnsThis, callTrap, handlerWithPrototype) |
| 1741 TestConstructFromCall2(proto, returnsThis, callTrap, handlerSansPrototype) |
| 1742 } |
| 1743 |
| 1744 function TestConstructFromCall2(proto, returnsThis, callTrap, handler) { |
| 1745 var f = Proxy.createFunction(handler, callTrap) |
| 1746 var o = new f(11, 31) |
| 1747 if (returnsThis) assertEquals(o, receiver) |
| 1748 assertEquals(42, o.sum) |
| 1749 assertSame(proto, Object.getPrototypeOf(o)) |
| 1750 |
| 1751 var f = CreateFrozen(handler, callTrap) |
| 1752 var o = new f(11, 32) |
| 1753 if (returnsThis) assertEquals(o, receiver) |
| 1754 assertEquals(43, o.sum) |
| 1755 assertSame(proto, Object.getPrototypeOf(o)) |
| 1756 } |
| 1757 |
| 1758 TestConstructFromCall(Object.prototype, true, ReturnUndef) |
| 1759 TestConstructFromCall(Object.prototype, true, ReturnThis) |
| 1760 TestConstructFromCall(Object.prototype, false, ReturnNew) |
| 1761 TestConstructFromCall(prototype, false, ReturnNewWithProto) |
| 1762 |
| 1763 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnUnd
ef)) |
| 1764 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnThi
s)) |
| 1765 TestConstructFromCall(Object.prototype, false, Proxy.createFunction({}, ReturnNe
w)) |
| 1766 TestConstructFromCall(prototype, false, Proxy.createFunction({}, ReturnNewWithPr
oto)) |
| 1767 |
| 1768 TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnUndef)) |
| 1769 TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnThis)) |
| 1770 TestConstructFromCall(Object.prototype, false, CreateFrozen({}, ReturnNew)) |
| 1771 TestConstructFromCall(prototype, false, CreateFrozen({}, ReturnNewWithProto)) |
| 1772 |
| 1773 ReturnUndef.prototype = prototype |
| 1774 ReturnThis.prototype = prototype |
| 1775 ReturnNew.prototype = prototype |
| 1776 ReturnNewWithProto.prototype = prototype |
| 1777 |
| 1778 TestConstructFromCall(prototype, true, ReturnUndef) |
| 1779 TestConstructFromCall(prototype, true, ReturnThis) |
| 1780 TestConstructFromCall(Object.prototype, false, ReturnNew) |
| 1781 TestConstructFromCall(prototype, false, ReturnNewWithProto) |
| 1782 |
| 1783 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnUnd
ef)) |
| 1784 TestConstructFromCall(Object.prototype, true, Proxy.createFunction({}, ReturnThi
s)) |
| 1785 TestConstructFromCall(Object.prototype, false, Proxy.createFunction({}, ReturnNe
w)) |
| 1786 TestConstructFromCall(prototype, false, Proxy.createFunction({}, ReturnNewWithPr
oto)) |
| 1787 |
| 1788 TestConstructFromCall(prototype, true, Proxy.createFunction(handlerWithPrototype
, ReturnUndef)) |
| 1789 TestConstructFromCall(prototype, true, Proxy.createFunction(handlerWithPrototype
, ReturnThis)) |
| 1790 TestConstructFromCall(Object.prototype, false, Proxy.createFunction(handlerWithP
rototype, ReturnNew)) |
| 1791 TestConstructFromCall(prototype, false, Proxy.createFunction(handlerWithPrototyp
e, ReturnNewWithProto)) |
| 1792 |
| 1793 TestConstructFromCall(prototype, true, CreateFrozen(handlerWithPrototype, Return
Undef)) |
| 1794 TestConstructFromCall(prototype, true, CreateFrozen(handlerWithPrototype, Return
This)) |
| 1795 TestConstructFromCall(Object.prototype, false, CreateFrozen(handlerWithPrototype
, ReturnNew)) |
| 1796 TestConstructFromCall(prototype, false, CreateFrozen(handlerWithPrototype, Retur
nNewWithProto)) |
| 1797 |
| 1798 |
| 1799 function TestConstructThrow(trap) { |
| 1800 TestConstructThrow2(Proxy.createFunction({fix: function() {return {}}}, trap)) |
| 1801 TestConstructThrow2(Proxy.createFunction({fix: function() {return {}}}, |
| 1802 function() {}, trap)) |
| 1803 } |
| 1804 |
| 1805 function TestConstructThrow2(f) { |
| 1806 assertThrows(function(){ new f(11) }, "myexn") |
| 1807 Object.freeze(f) |
| 1808 assertThrows(function(){ new f(11) }, "myexn") |
| 1809 } |
| 1810 |
| 1811 TestConstructThrow(function() { throw "myexn" }) |
| 1812 TestConstructThrow(Proxy.createFunction({}, function() { throw "myexn" })) |
| 1813 TestConstructThrow(CreateFrozen({}, function() { throw "myexn" })) |
OLD | NEW |