Created
April 26, 2012 14:59
-
-
Save jorendorff/2500176 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This is a JavaScript Scratchpad. | |
* | |
* Enter some JavaScript, then Right Click or choose from the Execute Menu: | |
* 1. Run to evaluate the selected text, | |
* 2. Inspect to bring up an Object Inspector on the result, or, | |
* 3. Display to insert the result in a comment after the selection. | |
*/ | |
function wrap(val) { | |
var type = typeof val; | |
if (type == 'string' || type == 'number' || type == 'boolean' | |
|| val == undefined || val === null) | |
{ | |
return val; | |
} | |
function getOwnPropertyDescriptor(name) { | |
var desc = Object.getOwnPropertyDescriptor(val, name); | |
if (desc === undefined) | |
return desc; | |
if ('value' in desc) { | |
desc.value = wrap(desc.value); | |
desc.writable = false; | |
} else { | |
desc.setter = null; | |
desc.getter = wrap(desc.getter); | |
} | |
return desc; | |
} | |
var handler = { | |
getOwnPropertyDescriptor: getOwnPropertyDescriptor, | |
getPropertyDescriptor: getOwnPropertyDescriptor | |
}; | |
return Proxy.create(handler, null); | |
} | |
"use strict"; | |
var p = wrap({x:1}); | |
p.x | |
p.x = 2; | |
var s = new Components.utils.Sandbox("http://www.example.com/", | |
{sandboxPrototype: wrap(this)}); | |
var x; | |
var q; | |
Object.defineProperty(this, "foo", {set: function (value) { q = value; }}); | |
s.evalInSandbox("x = 2;"); | |
Components.utils.evalInSandbox("x=2", s); | |
x; | |
Components.utils.evalInSandbox("x", s); | |
s.x; | |
s.eval("foo = 3;"); | |
q; | |
s.eval("Object.getPrototypeOf(this).xyz = 5;") | |
xyz; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment