Node VM Continued

One thing I noticed today is that this works:

var util = require('util');
var vm = require('vm');
 
var contextObject = {
}
contextObject.contextMethod = function(text) {
console.log(text);
}
var myContext = vm.createContext(contextObject);
myContext.contextMethod2 = function(text) {
console.log(text);
}
var scriptText = 'contextMethod("Hello World!"); contextMethod2("Hello Universe!");';
var script = vm.createScript(scriptText);
script.runInContext(myContext);

Which in general makes sense, but it is nice to see that you can modify the context.

This entry was posted in Javascript, Node.js and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*