doc: clean up and streamline vm.md examples

PR-URL: https://github.com/nodejs/node/pull/31474
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Denys Otrishko 2020-01-23 16:02:40 +02:00 committed by Rich Trott
parent 05d350a360
commit f2d5173b9d

View File

@ -175,9 +175,8 @@ for (let i = 0; i < 10; ++i) {
script.runInContext(context); script.runInContext(context);
} }
console.log(util.inspect(context)); console.log(context);
// Prints: { animal: 'cat', count: 12, name: 'kitty' }
// { animal: 'cat', count: 12, name: 'kitty' }
``` ```
Using the `timeout` or `breakOnSigint` options will result in new event loops Using the `timeout` or `breakOnSigint` options will result in new event loops
@ -246,9 +245,8 @@ contexts.forEach((context) => {
script.runInNewContext(context); script.runInNewContext(context);
}); });
console.log(util.inspect(contexts)); console.log(contexts);
// Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
// [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
``` ```
### `script.runInThisContext([options])` ### `script.runInThisContext([options])`
@ -804,9 +802,11 @@ vm.createContext(context);
vm.runInContext('globalVar *= 2;', context); vm.runInContext('globalVar *= 2;', context);
console.log(util.inspect(context)); // { globalVar: 2 } console.log(context);
// Prints: { globalVar: 2 }
console.log(util.inspect(globalVar)); // 3 console.log(global.globalVar);
// Prints: 3
``` ```
If `contextObject` is omitted (or passed explicitly as `undefined`), a new, If `contextObject` is omitted (or passed explicitly as `undefined`), a new,
@ -906,9 +906,8 @@ vm.createContext(contextObject);
for (let i = 0; i < 10; ++i) { for (let i = 0; i < 10; ++i) {
vm.runInContext('globalVar *= 2;', contextObject); vm.runInContext('globalVar *= 2;', contextObject);
} }
console.log(util.inspect(contextObject)); console.log(contextObject);
// Prints: { globalVar: 1024 }
// { globalVar: 1024 }
``` ```
## `vm.runInNewContext(code[, contextObject[, options]])` ## `vm.runInNewContext(code[, contextObject[, options]])`
@ -1003,9 +1002,8 @@ const contextObject = {
}; };
vm.runInNewContext('count += 1; name = "kitty"', contextObject); vm.runInNewContext('count += 1; name = "kitty"', contextObject);
console.log(util.inspect(contextObject)); console.log(contextObject);
// Prints: { animal: 'cat', count: 3, name: 'kitty' }
// { animal: 'cat', count: 3, name: 'kitty' }
``` ```
## `vm.runInThisContext(code[, options])` ## `vm.runInThisContext(code[, options])`
@ -1075,15 +1073,12 @@ const vm = require('vm');
let localVar = 'initial value'; let localVar = 'initial value';
const vmResult = vm.runInThisContext('localVar = "vm";'); const vmResult = vm.runInThisContext('localVar = "vm";');
console.log('vmResult:', vmResult); console.log(`vmResult: '${vmResult}', localVar: '${localVar}'`);
console.log('localVar:', localVar); // Prints: vmResult: 'vm', localVar: 'initial value'
const evalResult = eval('localVar = "eval";'); const evalResult = eval('localVar = "eval";');
console.log('evalResult:', evalResult); console.log(`evalResult: '${evalResult}', localVar: '${localVar}'`);
console.log('localVar:', localVar); // Prints: evalResult: 'eval', localVar: 'eval'
// vmResult: 'vm', localVar: 'initial value'
// evalResult: 'eval', localVar: 'eval'
``` ```
Because `vm.runInThisContext()` does not have access to the local scope, Because `vm.runInThisContext()` does not have access to the local scope,