| 1 |
"use strict" |
| 2 |
// Helpers. |
| 3 |
function makeAccessorProp(obj, propName, initialValue, hiddenName) { |
| 4 |
if (!hiddenName) |
| 5 |
hiddenName = propName + '_'; |
| 6 |
if (initialValue) |
| 7 |
Object.defineProperty(obj, hiddenName, { value: initialValue, writable: true, enumerable: false }); |
| 8 |
Object.defineProperty(obj, propName, |
| 9 |
{ configurable: true, |
| 10 |
enumerable: true, |
| 11 |
get: function() { return this[hiddenName]; }, |
| 12 |
set: function(x) { |
| 13 |
Object.defineProperty(this, hiddenName, { value: x, writable: true, enumerable: false }); |
| 14 |
} |
| 15 |
}); |
| 16 |
} |
| 17 |
|
| 18 |
// Set up a prototype with 4 properties. |
| 19 |
var proto = {valueProp: 11, valuePropShadowed: 22}; |
| 20 |
makeAccessorProp(proto, 'accessorProp', 33); |
| 21 |
makeAccessorProp(proto, 'accessorPropShadowed', 44); |
| 22 |
|
| 23 |
// Set up a proxy that uses |proto| as a prototype. |
| 24 |
var proxyTarget = {valuePropShadowed: 21}; |
| 25 |
makeAccessorProp(proxyTarget, 'accessorPropShadowed', -44, 'accessorPropShadowed__'); |
| 26 |
var proxy = wrapWithProto(proxyTarget, proto); |
| 27 |
|
| 28 |
// Value getters. |
| 29 |
assertEq(proxy.valueProp, 11); |
| 30 |
assertEq(proxy.valuePropShadowed, 21); |
| 31 |
// Iteration, enumeration, etc. |
| 32 |
var propNames = []; |
| 33 |
for (var i in proxy) |
| 34 |
propNames.push(i); |
| 35 |
assertEq(propNames.length, 4); |
| 36 |
assertEq('valueProp' in proxy, true); |
| 37 |
assertEq(proxy.hasOwnProperty('valueProp'), false); |
| 38 |
assertEq(Object.getOwnPropertyNames(proxy).indexOf('valueProp'), -1); |
| 39 |
assertEq('valuePropShadowed' in proxy, true); |
| 40 |
assertEq(Object.getOwnPropertyNames(proxy).indexOf('valuePropShadowed') == -1, false); |
| 41 |
assertEq(proxy.hasOwnProperty('valuePropShadowed'), true); |
| 42 |
// Value setters. |
| 43 |
proxy.valuePropShadowed = 20; |
| 44 |
proxy.valueProp = 10; |
| 45 |
assertEq(proxyTarget.valuePropShadowed, 20); |
| 46 |
assertEq(proxyTarget.valueProp, 10); |
| 47 |
// Accessor getters. |
| 48 |
assertEq(proxy.accessorProp, 33); |
| 49 |
assertEq(proxy.accessorPropShadowed, -44); |
| 50 |
// Accessor setters. |
| 51 |
proxy.accessorProp = 32; |
| 52 |
proxy.accessorPropShadowed = -43; |
| 53 |
assertEq(proxy.accessorProp, 32); |
| 54 |
assertEq(proxy.accessorPropShadowed, -43); |
| 55 |
// Make sure the underlying objects look right. |
| 56 |
assertEq(proto.accessorProp_, 33); |
| 57 |
assertEq(proto.accessorPropShadowed_, 44); |
| 58 |
assertEq(proto.hasOwnProperty('accessorPropShadowed__'), false); |
| 59 |
assertEq(proxyTarget.accessorProp_, 32); |
| 60 |
assertEq(proxyTarget.hasOwnProperty('accessorPropShadowed_'), false); |
| 61 |
assertEq(proxyTarget.accessorPropShadowed__, -43); |
| 62 |
|
| 63 |
// Now, create a new object prototyped to |proxy| and make sure |proxy| behaves |
| 64 |
// well on the prototype chain. |
| 65 |
function Constructor() { |
| 66 |
this.foo = 2; |
| 67 |
} |
| 68 |
Constructor.prototype = proxy; |
| 69 |
var child = new Constructor(); |
| 70 |
assertEq(child.valueProp, 10); |
| 71 |
assertEq(child.valuePropShadowed, 20); |
| 72 |
var childPropNames = []; |
| 73 |
for (var i in child) |
| 74 |
childPropNames.push(i); |
| 75 |
assertEq(childPropNames.length, 5); |
| 76 |
child.accessorProp = 5; |
| 77 |
child.accessorPropShadowed = 6; |
| 78 |
assertEq(child.accessorProp, 5); |
| 79 |
assertEq(child.accessorPropShadowed, 6); |
| 80 |
assertEq(child.accessorProp_, 5); |
| 81 |
assertEq(child.accessorPropShadowed__, 6); |