Skip to content

Commit b43a012

Browse files
committed
doc: fix import.meta example for vm.SourceTextModule
The import.meta example for new vm.SourceTextModule() in vm.md does not run as written. It fails in two separate ways. First, the constructor is missing the context: contextifiedObject option, so the module evaluates in the top context where secret is not defined, and the snippet throws ReferenceError: secret is not defined. Second, the trailing note suggests replacing meta.prop = {} with vm.runInContext('{}', contextifiedObject), but '{}' is parsed as an empty block and evaluates to undefined. That makes the following Object.getPrototypeOf(import.meta.prop) throw TypeError. Wrapping it as '({})' returns an object, which is what the note intends. This adds the context option and corrects the suggested replacement to '({})' in both the mjs and cjs variants. Fixes: #64076 Signed-off-by: Muhammad Zeeshan <61280174+zeeshan56656@users.noreply.github.com>
1 parent bfb2fa7 commit b43a012

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

doc/api/vm.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ const contextifiedObject = vm.createContext({ secret: 42 });
919919
const module = new vm.SourceTextModule(
920920
'Object.getPrototypeOf(import.meta.prop).secret = secret;',
921921
{
922+
context: contextifiedObject,
922923
initializeImportMeta(meta) {
923924
// Note: this object is created in the top context. As such,
924925
// Object.getPrototypeOf(import.meta.prop) points to the
@@ -937,7 +938,7 @@ await module.evaluate();
937938
// To fix this problem, replace
938939
// meta.prop = {};
939940
// above with
940-
// meta.prop = vm.runInContext('{}', contextifiedObject);
941+
// meta.prop = vm.runInContext('({})', contextifiedObject);
941942
```
942943
943944
```cjs
@@ -947,6 +948,7 @@ const contextifiedObject = vm.createContext({ secret: 42 });
947948
const module = new vm.SourceTextModule(
948949
'Object.getPrototypeOf(import.meta.prop).secret = secret;',
949950
{
951+
context: contextifiedObject,
950952
initializeImportMeta(meta) {
951953
// Note: this object is created in the top context. As such,
952954
// Object.getPrototypeOf(import.meta.prop) points to the
@@ -964,7 +966,7 @@ const contextifiedObject = vm.createContext({ secret: 42 });
964966
// To fix this problem, replace
965967
// meta.prop = {};
966968
// above with
967-
// meta.prop = vm.runInContext('{}', contextifiedObject);
969+
// meta.prop = vm.runInContext('({})', contextifiedObject);
968970
})();
969971
```
970972

0 commit comments

Comments
 (0)