-
-
Save isaacs/7163071 to your computer and use it in GitHub Desktop.
This file contains 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
var vm = require('vm'); | |
var code = | |
'Object.defineProperty(this, "f", {\n' + | |
' get: function() { return x; },\n' + | |
' set: function(k) { x = k; },\n' + | |
' configurable: true,\n' + | |
' enumerable: true\n' + | |
'});\n' + | |
'g = f;\n' + | |
// This line makes it work, by triggering the setter callback: | |
// 'Object.keys(this).forEach(function(k){this[k]=this[k]});\n' + | |
'f;\n'; | |
var x = {}; | |
var o = vm.createContext({ console: console, x: x }); | |
var res = vm.runInContext(code, o, 'test'); | |
console.error('res=%j f=%j res==x?%j res===f?%j keys=%j', | |
res, o.f, res === x, res === o.f, Object.keys(o)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment