diff options
author | punteek <prateek1@ucla.edu> | 2018-03-06 19:46:39 -0800 |
---|---|---|
committer | Gus Caplan <me@gus.host> | 2018-03-31 19:55:50 -0500 |
commit | 07ba9141e475ec63f6ef56b67ec5f98077cd3446 (patch) | |
tree | 99f36abde8cd6fd8b4a074fdfe8b1d8fe7fa6388 /doc/api/vm.md | |
parent | 28b622cb08602d77512fa3d659451cd317dfcc41 (diff) | |
download | node-new-07ba9141e475ec63f6ef56b67ec5f98077cd3446.tar.gz |
vm: add support for import.meta to Module
Fixes: https://github.com/nodejs/node/issues/18570
PR-URL: https://github.com/nodejs/node/pull/19277
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Diffstat (limited to 'doc/api/vm.md')
-rw-r--r-- | doc/api/vm.md | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/doc/api/vm.md b/doc/api/vm.md index c729ef5999..ed7be81776 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -167,9 +167,49 @@ const contextifiedSandbox = vm.createContext({ secret: 42 }); in stack traces produced by this Module. * `columnOffset` {integer} Spcifies the column number offset that is displayed in stack traces produced by this Module. + * `initalizeImportMeta` {Function} Called during evaluation of this Module to + initialize the `import.meta`. This function has the signature `(meta, + module)`, where `meta` is the `import.meta` object in the Module, and + `module` is this `vm.Module` object. Creates a new ES `Module` object. +*Note*: Properties assigned to the `import.meta` object that are objects may +allow the Module to access information outside the specified `context`, if the +object is created in the top level context. Use `vm.runInContext()` to create +objects in a specific context. + +```js +const vm = require('vm'); + +const contextifiedSandbox = vm.createContext({ secret: 42 }); + +(async () => { + const module = new vm.Module( + 'Object.getPrototypeOf(import.meta.prop).secret = secret;', + { + initializeImportMeta(meta) { + // Note: this object is created in the top context. As such, + // Object.getPrototypeOf(import.meta.prop) points to the + // Object.prototype in the top context rather than that in + // the sandbox. + meta.prop = {}; + } + }); + // Since module has no dependencies, the linker function will never be called. + await module.link(() => {}); + module.initialize(); + await module.evaluate(); + + // Now, Object.prototype.secret will be equal to 42. + // + // To fix this problem, replace + // meta.prop = {}; + // above with + // meta.prop = vm.runInContext('{}', contextifiedSandbox); +})(); +``` + ### module.dependencySpecifiers * {string[]} |