Browser polyfill for Node.js VM
Works in all modern browsers(Internet Explorer 10+)
You can use it in 2 ways:
Make sure to check examples
-
vm.Script
-
new vm.Script(code)
var code = 'console.log("Hello");'; var script = new vm.Script(code);
-
script.runInContext(context)
var code = 'console.log(a);'; var sandbox = { a: 1 }; var context = vm.createContext(sandbox); var script = new vm.Script(code); script.runInContext(context);
-
script.runInNewContext(sandbox)
var code = 'console.log(a);'; var sandbox = { a: 1 }; var script = new vm.Script(code); script.runInNewContext(sandbox);
-
script.runInThisContext()
var code = 'console.log(someVar);'; var script = new vm.Script(code); window.someVar = 1; script.runInThisContext();
-
-
vm.createContext(sandbox)
var sandbox = { a: 5, b: 10, c: 'something' }; var context = vm.createContext(sandbox);
-
vm.isContext(sandbox)
var sandbox = { a: 1 }; var context = vm.createContext(sandbox); vm.isContext(sandbox); // => true vm.isContext({}); // => false
-
vm.runInContext(code, context)
var code = 'console.log(a);'; var sandbox = { a: 1 }; var context = vm.createContext(sandbox); vm.runInContext(code, context);
-
vm.runInDebugContext(code)
Does not work in browsers. Throws Error.
-
vm.runInNewContext(code, sandbox)
var code = 'console.log(a)'; var sandbox = { a: 1 }; vm.runInNewContext(code, sandbox);
-
vm.runInThisContext(code)
var code = 'console.log(someVar)'; window.someVar = 1; vm.runInThisContext(code);
-
Install
$ npm install
-
Lint
$ npm run lint
-
Build
$ npm run build
ISC © Sergey Nanovsky