mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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: https://github.com/nodejs/node/issues/64076
Signed-off-by: Muhammad Zeeshan <61280174+zeeshan56656@users.noreply.github.com>
PR-URL: https://github.com/nodejs/node/pull/64112
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
1 parent
c879f912bf
commit
2a2effadf6
1 file changed
+4
-2
+4
-2
@@ -919,6 +919,7 @@ const contextifiedObject = vm.createContext({ secret: 42 });
|
||||
const module = new vm.SourceTextModule(
|
||||
'Object.getPrototypeOf(import.meta.prop).secret = secret;',
|
||||
{
|
||||
context: contextifiedObject,
|
||||
initializeImportMeta(meta) {
|
||||
// Note: this object is created in the top context. As such,
|
||||
// Object.getPrototypeOf(import.meta.prop) points to the
|
||||
@@ -937,7 +938,7 @@ await module.evaluate();
|
||||
// To fix this problem, replace
|
||||
// meta.prop = {};
|
||||
// above with
|
||||
// meta.prop = vm.runInContext('{}', contextifiedObject);
|
||||
// meta.prop = vm.runInContext('({})', contextifiedObject);
|
||||
```
|
||||
|
||||
```cjs
|
||||
@@ -947,6 +948,7 @@ const contextifiedObject = vm.createContext({ secret: 42 });
|
||||
const module = new vm.SourceTextModule(
|
||||
'Object.getPrototypeOf(import.meta.prop).secret = secret;',
|
||||
{
|
||||
context: contextifiedObject,
|
||||
initializeImportMeta(meta) {
|
||||
// Note: this object is created in the top context. As such,
|
||||
// Object.getPrototypeOf(import.meta.prop) points to the
|
||||
@@ -964,7 +966,7 @@ const contextifiedObject = vm.createContext({ secret: 42 });
|
||||
// To fix this problem, replace
|
||||
// meta.prop = {};
|
||||
// above with
|
||||
// meta.prop = vm.runInContext('{}', contextifiedObject);
|
||||
// meta.prop = vm.runInContext('({})', contextifiedObject);
|
||||
})();
|
||||
```
|
||||
|
||||
|
||||
Reference in new issue
Block a user