1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_8_5/extensions/scripted-proxies.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,162 @@ 1.4 +/* 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/licenses/publicdomain/ 1.7 + * Contributor: Andreas Gal 1.8 + */ 1.9 + 1.10 +//----------------------------------------------------------------------------- 1.11 +var BUGNUMBER = 546590; 1.12 +var summary = 'basic scripted proxies tests'; 1.13 +var actual = ''; 1.14 +var expect = ''; 1.15 + 1.16 +//----------------------------------------------------------------------------- 1.17 +test(); 1.18 +//----------------------------------------------------------------------------- 1.19 + 1.20 +function test() { 1.21 + enterFunc ('test'); 1.22 + printBugNumber(BUGNUMBER); 1.23 + printStatus(summary); 1.24 + 1.25 + testObj({ foo: 1, bar: 2 }); 1.26 + testObj({ 1: 2, 3: 4 }); 1.27 + testObj([ 1, 2, 3 ]); 1.28 + testObj(new Date()); 1.29 + testObj(new Array()); 1.30 + testObj(new RegExp()); 1.31 + testObj(Date); 1.32 + testObj(Array); 1.33 + testObj(RegExp); 1.34 + 1.35 + /* Test function proxies. */ 1.36 + var proxy = Proxy.createFunction({ 1.37 + get: function(obj,name) { return Function.prototype[name]; }, 1.38 + fix: function() { 1.39 + return ({}); 1.40 + } 1.41 + }, function() { return "call"; }); 1.42 + 1.43 + assertEq(proxy(), "call"); 1.44 + assertEq(Function.prototype.bind.call(proxy)(), "call"); 1.45 + assertEq(typeof proxy, "function"); 1.46 + if ("isTrapping" in Proxy) { 1.47 + assertEq(Proxy.isTrapping(proxy), true); 1.48 + assertEq(Proxy.fix(proxy), true); 1.49 + assertEq(Proxy.isTrapping(proxy), false); 1.50 + assertEq(typeof proxy, "function"); 1.51 + assertEq(proxy(), "call"); 1.52 + } 1.53 + 1.54 + /* Test function proxies as constructors. */ 1.55 + var proxy = Proxy.createFunction({ 1.56 + get: function(obj, name) { return Function.prototype[name]; }, 1.57 + fix: function() { return ({}); } 1.58 + }, 1.59 + function() { var x = {}; x.origin = "call"; return x; }, 1.60 + function() { var x = {}; x.origin = "new"; return x; }) 1.61 + 1.62 + assertEq(proxy().origin, "call"); 1.63 + assertEq(Function.prototype.bind.call(proxy)().origin, "call"); 1.64 + assertEq((new proxy()).origin, "new"); 1.65 + assertEq(new (Function.prototype.bind.call(proxy))().origin, "new"); 1.66 + if ("fix" in Proxy) { 1.67 + assertEq(Proxy.fix(proxy), true); 1.68 + assertEq(proxy().origin, "call"); 1.69 + assertEq((new proxy()).origin, "new"); 1.70 + } 1.71 + 1.72 + /* Test fallback on call if no construct trap was given. */ 1.73 + var proxy = Proxy.createFunction({ 1.74 + get: function(obj, name) { return Function.prototype[name]; }, 1.75 + fix: function() { return ({}); } 1.76 + }, 1.77 + function() { this.origin = "new"; return "new-ret"; }); 1.78 + 1.79 + assertEq((new proxy()).origin, "new"); 1.80 + if ("fix" in Proxy) { 1.81 + assertEq(Proxy.fix(proxy), true); 1.82 + assertEq((new proxy()).origin, "new"); 1.83 + } 1.84 + 1.85 + /* Test invoke. */ 1.86 + var proxy = Proxy.create({ get: function(obj,name) { return function(a,b,c) { return name + uneval([a,b,c]); } }}); 1.87 + assertEq(proxy.foo(1,2,3), "foo[1, 2, 3]"); 1.88 + 1.89 + reportCompare(0, 0, "done."); 1.90 + 1.91 + exitFunc ('test'); 1.92 +} 1.93 + 1.94 +/* Test object proxies. */ 1.95 +function noopHandlerMaker(obj) { 1.96 + return { 1.97 + getOwnPropertyDescriptor: function(name) { 1.98 + var desc = Object.getOwnPropertyDescriptor(obj); 1.99 + // a trapping proxy's properties must always be configurable 1.100 + desc.configurable = true; 1.101 + return desc; 1.102 + }, 1.103 + getPropertyDescriptor: function(name) { 1.104 + var desc = Object.getPropertyDescriptor(obj); // assumed 1.105 + // a trapping proxy's properties must always be configurable 1.106 + desc.configurable = true; 1.107 + return desc; 1.108 + }, 1.109 + getOwnPropertyNames: function() { 1.110 + return Object.getOwnPropertyNames(obj); 1.111 + }, 1.112 + defineProperty: function(name, desc) { 1.113 + return Object.defineProperty(obj, name, desc); 1.114 + }, 1.115 + delete: function(name) { return delete obj[name]; }, 1.116 + fix: function() { 1.117 + // As long as obj is not frozen, the proxy won't allow itself to be fixed 1.118 + // if (!Object.isFrozen(obj)) [not implemented in SpiderMonkey] 1.119 + // return undefined; 1.120 + // return Object.getOwnProperties(obj); // assumed [not implemented in SpiderMonkey] 1.121 + var props = {}; 1.122 + for (x in obj) 1.123 + props[x] = Object.getOwnPropertyDescriptor(obj, x); 1.124 + return props; 1.125 + }, 1.126 + has: function(name) { return name in obj; }, 1.127 + hasOwn: function(name) { return ({}).hasOwnProperty.call(obj, name); }, 1.128 + get: function(receiver, name) { return obj[name]; }, 1.129 + set: function(receiver, name, val) { obj[name] = val; return true; }, // bad behavior when set fails in non-strict mode 1.130 + enumerate: function() { 1.131 + var result = []; 1.132 + for (name in obj) { result.push(name); }; 1.133 + return result; 1.134 + }, 1.135 + keys: function() { return Object.keys(obj); } 1.136 + }; 1.137 +}; 1.138 + 1.139 +function testNoopHandler(obj, proxy) { 1.140 + /* Check that both objects see the same properties. */ 1.141 + for (x in obj) 1.142 + assertEq(obj[x], proxy[x]); 1.143 + for (x in proxy) 1.144 + assertEq(obj[x], proxy[x]); 1.145 + /* Check that the iteration order is the same. */ 1.146 + var a = [], b = []; 1.147 + for (x in obj) 1.148 + a.push(x); 1.149 + for (x in proxy) 1.150 + b.push(x); 1.151 + assertEq(uneval(a), uneval(b)); 1.152 +} 1.153 + 1.154 +function testObj(obj) { 1.155 + var proxy = Proxy.create(noopHandlerMaker(obj)); 1.156 + testNoopHandler(obj, proxy); 1.157 + assertEq(typeof proxy, "object"); 1.158 + if ("isTrapping" in Proxy) { 1.159 + assertEq(Proxy.isTrapping(proxy), true); 1.160 + assertEq(Proxy.fix(proxy), true); 1.161 + assertEq(Proxy.isTrapping(proxy), false); 1.162 + assertEq(typeof proxy, "object"); 1.163 + testNoopHandler(obj, proxy); 1.164 + } 1.165 +}