{"action":"closed","issue":{"url":"https://api.github.com/repos/joyent/node/issues/9240","labels_url":"https://api.github.com/repos/joyent/node/issues/9240/labels{/name}","comments_url":"https://api.github.com/repos/joyent/node/issues/9240/comments","events_url":"https://api.github.com/repos/joyent/node/issues/9240/events","html_url":"https://github.com/joyent/node/issues/9240","id":58038752,"number":9240,"title":"documentation sample needs to be improved for vm","user":{"login":"smartmouse","id":4519627,"avatar_url":"https://avatars.githubusercontent.com/u/4519627?v=3","gravatar_id":"","url":"https://api.github.com/users/smartmouse","html_url":"https://github.com/smartmouse","followers_url":"https://api.github.com/users/smartmouse/followers","following_url":"https://api.github.com/users/smartmouse/following{/other_user}","gists_url":"https://api.github.com/users/smartmouse/gists{/gist_id}","starred_url":"https://api.github.com/users/smartmouse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smartmouse/subscriptions","organizations_url":"https://api.github.com/users/smartmouse/orgs","repos_url":"https://api.github.com/users/smartmouse/repos","events_url":"https://api.github.com/users/smartmouse/events{/privacy}","received_events_url":"https://api.github.com/users/smartmouse/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-02-18T08:14:44Z","updated_at":"2015-02-18T18:57:51Z","closed_at":"2015-02-18T18:57:51Z","body":"The following sample needs to be improved:\r\nIf we just execute it -- it will give an error -- \r\nC:\\Users\\IBM_ADMIN\\VMTest\\vmincr.js:28\r\n  script.runInContext(sandbox);\r\n         ^\r\nTypeError: sandbox argument must have been converted to a context.\r\n    at TypeError (native)\r\n    at Object.<anonymous> (C:\\Users\\IBM_ADMIN\\VMTest\\vmincr.js:28:10)\r\n    at Module._compile (module.js:460:26)\r\n    at Object.Module._extensions..js (module.js:478:10)\r\n    at Module.load (module.js:355:32)\r\n    at Function.Module._load (module.js:310:12)\r\n    at Function.Module.runMain (module.js:501:10)\r\n    at startup (node.js:129:16)\r\n    at node.js:814:3\r\n\r\n\r\n\r\n============================================\r\nvar util = require('util');\r\nvar vm = require('vm');\r\n\r\nvar sandbox = {\r\n  animal: 'cat',\r\n  count: 2\r\n};\r\n\r\nvar script = new vm.Script('count += 1; name = \"kitty\"');\r\n\r\nfor (var i = 0; i < 10; ++i) {\r\n  script.runInContext(sandbox);\r\n}\r\n\r\nconsole.log(util.inspect(sandbox));\r\n\r\n// { animal: 'cat', count: 12, name: 'kitty' }\r\n\r\n\r\n======================================================\r\n\r\nThanks. I can resolve this by adding\r\nvm.runInNewContext('count += 1; name = \"kitty\"', sandbox);\r\nconsole.log(util.inspect(sandbox));\r\n\r\n"}}