diff --git a/node/node-tests.ts b/node/node-tests.ts index 18c345118..e5a20fa78 100644 --- a/node/node-tests.ts +++ b/node/node-tests.ts @@ -16,6 +16,7 @@ import * as path from "path"; import * as readline from "readline"; import * as childProcess from "child_process"; import * as os from "os"; +import * as vm from "vm"; // Specifically test buffer module regression. import {Buffer as ImportedBuffer, SlowBuffer as ImportedSlowBuffer} from "buffer"; @@ -679,3 +680,63 @@ namespace os_tests { result = os.networkInterfaces(); } } + +//////////////////////////////////////////////////// +/// vm tests : https://nodejs.org/api/vm.html +//////////////////////////////////////////////////// + +namespace vm_tests { + { + const sandbox = { + animal: 'cat', + count: 2 + }; + + const context = new vm.createContext(sandbox); + console.log(vm.isContext(context)); + const script = new vm.Script('count += 1; name = "kitty"'); + + for (let i = 0; i < 10; ++i) { + script.runInContext(context); + } + + console.log(util.inspect(sandbox)); + + vm.runInNewContext('count += 1; name = "kitty"', sandbox); + console.log(util.inspect(sandbox)); + } + + { + const sandboxes = [{}, {}, {}]; + + const script = new vm.Script('globalVar = "set"'); + + sandboxes.forEach((sandbox) => { + script.runInNewContext(sandbox); + }); + + console.log(util.inspect(sandboxes)); + } + + { + global.globalVar = 0; + + const script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' }); + + for (var i = 0; i < 1000; ++i) { + script.runInThisContext(); + } + + console.log(globalVar); + + var localVar = 'initial value'; + vm.runInThisContext('localVar = "vm";'); + + console.log(localVar); + } + + { + const Debug = vm.runInDebugContext('Debug'); + Debug.scripts().forEach(function(script) { console.log(script.name); }); + } +}