michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: * Contributor: Andreas Gal michael@0: */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 546590; michael@0: var summary = 'basic scripted proxies tests'; michael@0: var actual = ''; michael@0: var expect = ''; michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: function test() { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus(summary); michael@0: michael@0: testObj({ foo: 1, bar: 2 }); michael@0: testObj({ 1: 2, 3: 4 }); michael@0: testObj([ 1, 2, 3 ]); michael@0: testObj(new Date()); michael@0: testObj(new Array()); michael@0: testObj(new RegExp()); michael@0: testObj(Date); michael@0: testObj(Array); michael@0: testObj(RegExp); michael@0: michael@0: /* Test function proxies. */ michael@0: var proxy = Proxy.createFunction({ michael@0: get: function(obj,name) { return Function.prototype[name]; }, michael@0: fix: function() { michael@0: return ({}); michael@0: } michael@0: }, function() { return "call"; }); michael@0: michael@0: assertEq(proxy(), "call"); michael@0: assertEq(Function.prototype.bind.call(proxy)(), "call"); michael@0: assertEq(typeof proxy, "function"); michael@0: if ("isTrapping" in Proxy) { michael@0: assertEq(Proxy.isTrapping(proxy), true); michael@0: assertEq(Proxy.fix(proxy), true); michael@0: assertEq(Proxy.isTrapping(proxy), false); michael@0: assertEq(typeof proxy, "function"); michael@0: assertEq(proxy(), "call"); michael@0: } michael@0: michael@0: /* Test function proxies as constructors. */ michael@0: var proxy = Proxy.createFunction({ michael@0: get: function(obj, name) { return Function.prototype[name]; }, michael@0: fix: function() { return ({}); } michael@0: }, michael@0: function() { var x = {}; x.origin = "call"; return x; }, michael@0: function() { var x = {}; x.origin = "new"; return x; }) michael@0: michael@0: assertEq(proxy().origin, "call"); michael@0: assertEq(Function.prototype.bind.call(proxy)().origin, "call"); michael@0: assertEq((new proxy()).origin, "new"); michael@0: assertEq(new (Function.prototype.bind.call(proxy))().origin, "new"); michael@0: if ("fix" in Proxy) { michael@0: assertEq(Proxy.fix(proxy), true); michael@0: assertEq(proxy().origin, "call"); michael@0: assertEq((new proxy()).origin, "new"); michael@0: } michael@0: michael@0: /* Test fallback on call if no construct trap was given. */ michael@0: var proxy = Proxy.createFunction({ michael@0: get: function(obj, name) { return Function.prototype[name]; }, michael@0: fix: function() { return ({}); } michael@0: }, michael@0: function() { this.origin = "new"; return "new-ret"; }); michael@0: michael@0: assertEq((new proxy()).origin, "new"); michael@0: if ("fix" in Proxy) { michael@0: assertEq(Proxy.fix(proxy), true); michael@0: assertEq((new proxy()).origin, "new"); michael@0: } michael@0: michael@0: /* Test invoke. */ michael@0: var proxy = Proxy.create({ get: function(obj,name) { return function(a,b,c) { return name + uneval([a,b,c]); } }}); michael@0: assertEq(proxy.foo(1,2,3), "foo[1, 2, 3]"); michael@0: michael@0: reportCompare(0, 0, "done."); michael@0: michael@0: exitFunc ('test'); michael@0: } michael@0: michael@0: /* Test object proxies. */ michael@0: function noopHandlerMaker(obj) { michael@0: return { michael@0: getOwnPropertyDescriptor: function(name) { michael@0: var desc = Object.getOwnPropertyDescriptor(obj); michael@0: // a trapping proxy's properties must always be configurable michael@0: desc.configurable = true; michael@0: return desc; michael@0: }, michael@0: getPropertyDescriptor: function(name) { michael@0: var desc = Object.getPropertyDescriptor(obj); // assumed michael@0: // a trapping proxy's properties must always be configurable michael@0: desc.configurable = true; michael@0: return desc; michael@0: }, michael@0: getOwnPropertyNames: function() { michael@0: return Object.getOwnPropertyNames(obj); michael@0: }, michael@0: defineProperty: function(name, desc) { michael@0: return Object.defineProperty(obj, name, desc); michael@0: }, michael@0: delete: function(name) { return delete obj[name]; }, michael@0: fix: function() { michael@0: // As long as obj is not frozen, the proxy won't allow itself to be fixed michael@0: // if (!Object.isFrozen(obj)) [not implemented in SpiderMonkey] michael@0: // return undefined; michael@0: // return Object.getOwnProperties(obj); // assumed [not implemented in SpiderMonkey] michael@0: var props = {}; michael@0: for (x in obj) michael@0: props[x] = Object.getOwnPropertyDescriptor(obj, x); michael@0: return props; michael@0: }, michael@0: has: function(name) { return name in obj; }, michael@0: hasOwn: function(name) { return ({}).hasOwnProperty.call(obj, name); }, michael@0: get: function(receiver, name) { return obj[name]; }, michael@0: set: function(receiver, name, val) { obj[name] = val; return true; }, // bad behavior when set fails in non-strict mode michael@0: enumerate: function() { michael@0: var result = []; michael@0: for (name in obj) { result.push(name); }; michael@0: return result; michael@0: }, michael@0: keys: function() { return Object.keys(obj); } michael@0: }; michael@0: }; michael@0: michael@0: function testNoopHandler(obj, proxy) { michael@0: /* Check that both objects see the same properties. */ michael@0: for (x in obj) michael@0: assertEq(obj[x], proxy[x]); michael@0: for (x in proxy) michael@0: assertEq(obj[x], proxy[x]); michael@0: /* Check that the iteration order is the same. */ michael@0: var a = [], b = []; michael@0: for (x in obj) michael@0: a.push(x); michael@0: for (x in proxy) michael@0: b.push(x); michael@0: assertEq(uneval(a), uneval(b)); michael@0: } michael@0: michael@0: function testObj(obj) { michael@0: var proxy = Proxy.create(noopHandlerMaker(obj)); michael@0: testNoopHandler(obj, proxy); michael@0: assertEq(typeof proxy, "object"); michael@0: if ("isTrapping" in Proxy) { michael@0: assertEq(Proxy.isTrapping(proxy), true); michael@0: assertEq(Proxy.fix(proxy), true); michael@0: assertEq(Proxy.isTrapping(proxy), false); michael@0: assertEq(typeof proxy, "object"); michael@0: testNoopHandler(obj, proxy); michael@0: } michael@0: }