Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: clean up and streamline vm.md examples #31474

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions doc/api/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,8 @@ for (let i = 0; i < 10; ++i) {
script.runInContext(context);
}

console.log(util.inspect(context));

// { animal: 'cat', count: 12, name: 'kitty' }
console.log(context);
// Prints: { animal: 'cat', count: 12, name: 'kitty' }
```

Using the `timeout` or `breakOnSigint` options will result in new event loops
Expand Down Expand Up @@ -246,9 +245,8 @@ contexts.forEach((context) => {
script.runInNewContext(context);
});

console.log(util.inspect(contexts));

// [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
console.log(contexts);
// Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
```

### `script.runInThisContext([options])`
Expand Down Expand Up @@ -804,9 +802,11 @@ vm.createContext(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,
Expand Down Expand Up @@ -906,9 +906,8 @@ vm.createContext(contextObject);
for (let i = 0; i < 10; ++i) {
vm.runInContext('globalVar *= 2;', contextObject);
}
console.log(util.inspect(contextObject));

// { globalVar: 1024 }
console.log(contextObject);
// Prints: { globalVar: 1024 }
```

## `vm.runInNewContext(code[, contextObject[, options]])`
Expand Down Expand Up @@ -1003,9 +1002,8 @@ const contextObject = {
};

vm.runInNewContext('count += 1; name = "kitty"', contextObject);
console.log(util.inspect(contextObject));

// { animal: 'cat', count: 3, name: 'kitty' }
console.log(contextObject);
// Prints: { animal: 'cat', count: 3, name: 'kitty' }
```

## `vm.runInThisContext(code[, options])`
Expand Down Expand Up @@ -1075,15 +1073,12 @@ const vm = require('vm');
let localVar = 'initial value';

const vmResult = vm.runInThisContext('localVar = "vm";');
console.log('vmResult:', vmResult);
console.log('localVar:', localVar);
console.log(`vmResult: '${vmResult}', localVar: '${localVar}'`);
// Prints: vmResult: 'vm', localVar: 'initial value'

const evalResult = eval('localVar = "eval";');
console.log('evalResult:', evalResult);
console.log('localVar:', localVar);

// vmResult: 'vm', localVar: 'initial value'
// evalResult: 'eval', localVar: 'eval'
console.log(`evalResult: '${evalResult}', localVar: '${localVar}'`);
// Prints: evalResult: 'eval', localVar: 'eval'
```

Because `vm.runInThisContext()` does not have access to the local scope,
Expand Down