Skip to content

Instantly share code, notes, and snippets.

@isaacs
Last active December 26, 2015 14:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/7163071 to your computer and use it in GitHub Desktop.
Save isaacs/7163071 to your computer and use it in GitHub Desktop.
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