js/src/tests/js1_8_5/extensions/scripted-proxies.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/licenses/publicdomain/
michael@0 4 * Contributor: Andreas Gal
michael@0 5 */
michael@0 6
michael@0 7 //-----------------------------------------------------------------------------
michael@0 8 var BUGNUMBER = 546590;
michael@0 9 var summary = 'basic scripted proxies tests';
michael@0 10 var actual = '';
michael@0 11 var expect = '';
michael@0 12
michael@0 13 //-----------------------------------------------------------------------------
michael@0 14 test();
michael@0 15 //-----------------------------------------------------------------------------
michael@0 16
michael@0 17 function test() {
michael@0 18 enterFunc ('test');
michael@0 19 printBugNumber(BUGNUMBER);
michael@0 20 printStatus(summary);
michael@0 21
michael@0 22 testObj({ foo: 1, bar: 2 });
michael@0 23 testObj({ 1: 2, 3: 4 });
michael@0 24 testObj([ 1, 2, 3 ]);
michael@0 25 testObj(new Date());
michael@0 26 testObj(new Array());
michael@0 27 testObj(new RegExp());
michael@0 28 testObj(Date);
michael@0 29 testObj(Array);
michael@0 30 testObj(RegExp);
michael@0 31
michael@0 32 /* Test function proxies. */
michael@0 33 var proxy = Proxy.createFunction({
michael@0 34 get: function(obj,name) { return Function.prototype[name]; },
michael@0 35 fix: function() {
michael@0 36 return ({});
michael@0 37 }
michael@0 38 }, function() { return "call"; });
michael@0 39
michael@0 40 assertEq(proxy(), "call");
michael@0 41 assertEq(Function.prototype.bind.call(proxy)(), "call");
michael@0 42 assertEq(typeof proxy, "function");
michael@0 43 if ("isTrapping" in Proxy) {
michael@0 44 assertEq(Proxy.isTrapping(proxy), true);
michael@0 45 assertEq(Proxy.fix(proxy), true);
michael@0 46 assertEq(Proxy.isTrapping(proxy), false);
michael@0 47 assertEq(typeof proxy, "function");
michael@0 48 assertEq(proxy(), "call");
michael@0 49 }
michael@0 50
michael@0 51 /* Test function proxies as constructors. */
michael@0 52 var proxy = Proxy.createFunction({
michael@0 53 get: function(obj, name) { return Function.prototype[name]; },
michael@0 54 fix: function() { return ({}); }
michael@0 55 },
michael@0 56 function() { var x = {}; x.origin = "call"; return x; },
michael@0 57 function() { var x = {}; x.origin = "new"; return x; })
michael@0 58
michael@0 59 assertEq(proxy().origin, "call");
michael@0 60 assertEq(Function.prototype.bind.call(proxy)().origin, "call");
michael@0 61 assertEq((new proxy()).origin, "new");
michael@0 62 assertEq(new (Function.prototype.bind.call(proxy))().origin, "new");
michael@0 63 if ("fix" in Proxy) {
michael@0 64 assertEq(Proxy.fix(proxy), true);
michael@0 65 assertEq(proxy().origin, "call");
michael@0 66 assertEq((new proxy()).origin, "new");
michael@0 67 }
michael@0 68
michael@0 69 /* Test fallback on call if no construct trap was given. */
michael@0 70 var proxy = Proxy.createFunction({
michael@0 71 get: function(obj, name) { return Function.prototype[name]; },
michael@0 72 fix: function() { return ({}); }
michael@0 73 },
michael@0 74 function() { this.origin = "new"; return "new-ret"; });
michael@0 75
michael@0 76 assertEq((new proxy()).origin, "new");
michael@0 77 if ("fix" in Proxy) {
michael@0 78 assertEq(Proxy.fix(proxy), true);
michael@0 79 assertEq((new proxy()).origin, "new");
michael@0 80 }
michael@0 81
michael@0 82 /* Test invoke. */
michael@0 83 var proxy = Proxy.create({ get: function(obj,name) { return function(a,b,c) { return name + uneval([a,b,c]); } }});
michael@0 84 assertEq(proxy.foo(1,2,3), "foo[1, 2, 3]");
michael@0 85
michael@0 86 reportCompare(0, 0, "done.");
michael@0 87
michael@0 88 exitFunc ('test');
michael@0 89 }
michael@0 90
michael@0 91 /* Test object proxies. */
michael@0 92 function noopHandlerMaker(obj) {
michael@0 93 return {
michael@0 94 getOwnPropertyDescriptor: function(name) {
michael@0 95 var desc = Object.getOwnPropertyDescriptor(obj);
michael@0 96 // a trapping proxy's properties must always be configurable
michael@0 97 desc.configurable = true;
michael@0 98 return desc;
michael@0 99 },
michael@0 100 getPropertyDescriptor: function(name) {
michael@0 101 var desc = Object.getPropertyDescriptor(obj); // assumed
michael@0 102 // a trapping proxy's properties must always be configurable
michael@0 103 desc.configurable = true;
michael@0 104 return desc;
michael@0 105 },
michael@0 106 getOwnPropertyNames: function() {
michael@0 107 return Object.getOwnPropertyNames(obj);
michael@0 108 },
michael@0 109 defineProperty: function(name, desc) {
michael@0 110 return Object.defineProperty(obj, name, desc);
michael@0 111 },
michael@0 112 delete: function(name) { return delete obj[name]; },
michael@0 113 fix: function() {
michael@0 114 // As long as obj is not frozen, the proxy won't allow itself to be fixed
michael@0 115 // if (!Object.isFrozen(obj)) [not implemented in SpiderMonkey]
michael@0 116 // return undefined;
michael@0 117 // return Object.getOwnProperties(obj); // assumed [not implemented in SpiderMonkey]
michael@0 118 var props = {};
michael@0 119 for (x in obj)
michael@0 120 props[x] = Object.getOwnPropertyDescriptor(obj, x);
michael@0 121 return props;
michael@0 122 },
michael@0 123 has: function(name) { return name in obj; },
michael@0 124 hasOwn: function(name) { return ({}).hasOwnProperty.call(obj, name); },
michael@0 125 get: function(receiver, name) { return obj[name]; },
michael@0 126 set: function(receiver, name, val) { obj[name] = val; return true; }, // bad behavior when set fails in non-strict mode
michael@0 127 enumerate: function() {
michael@0 128 var result = [];
michael@0 129 for (name in obj) { result.push(name); };
michael@0 130 return result;
michael@0 131 },
michael@0 132 keys: function() { return Object.keys(obj); }
michael@0 133 };
michael@0 134 };
michael@0 135
michael@0 136 function testNoopHandler(obj, proxy) {
michael@0 137 /* Check that both objects see the same properties. */
michael@0 138 for (x in obj)
michael@0 139 assertEq(obj[x], proxy[x]);
michael@0 140 for (x in proxy)
michael@0 141 assertEq(obj[x], proxy[x]);
michael@0 142 /* Check that the iteration order is the same. */
michael@0 143 var a = [], b = [];
michael@0 144 for (x in obj)
michael@0 145 a.push(x);
michael@0 146 for (x in proxy)
michael@0 147 b.push(x);
michael@0 148 assertEq(uneval(a), uneval(b));
michael@0 149 }
michael@0 150
michael@0 151 function testObj(obj) {
michael@0 152 var proxy = Proxy.create(noopHandlerMaker(obj));
michael@0 153 testNoopHandler(obj, proxy);
michael@0 154 assertEq(typeof proxy, "object");
michael@0 155 if ("isTrapping" in Proxy) {
michael@0 156 assertEq(Proxy.isTrapping(proxy), true);
michael@0 157 assertEq(Proxy.fix(proxy), true);
michael@0 158 assertEq(Proxy.isTrapping(proxy), false);
michael@0 159 assertEq(typeof proxy, "object");
michael@0 160 testNoopHandler(obj, proxy);
michael@0 161 }
michael@0 162 }

mercurial