Skip to content

Instantly share code, notes, and snippets.

@syg
Created August 22, 2012 17:11
Show Gist options
  • Save syg/3427627 to your computer and use it in GitHub Desktop.
Save syg/3427627 to your computer and use it in GitHub Desktop.
function isIndex(n) {
// This isn't right, but pretend that this checks if n is an index in the
// way that SpiderMonkey checks in js_IdIsIndex.
return Number(n) % 1 == 0;
}
function indices(obj) {
var result = [];
for (var i = 0; i < obj.length; i++)
result.push(i);
return result;
}
function paMaker(backing) {
return {
defineProperty: function(name, desc) {
throw new Error("immutable");
},
delete: function(name) {
throw new Error("immutable");
},
get: function(receiver, name) {
if (isIndex(name)) {
if (({}).hasOwnProperty.call(backing, name))
return backing[name];
return undefined;
}
return backing[name];
},
set: function(receiver, name, val) {
throw new Error("immutable");
},
enumerate: function() {
return indices(obj);
},
keys: function() {
return indices(obj);
}
};
}
Array.prototype[0] = "foo";
Array.prototype[42] = "foo";
var proxy = Proxy.create(paMaker([,2]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment