| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 02e665c commit 9a88fe4
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1811,7 +1811,7 @@ The module must be successfully linked before instantiation. | |||
| 1811 | 1811 | <a id="ERR_VM_MODULE_NOT_MODULE"></a> | |
| 1812 | 1812 | ### ERR_VM_MODULE_NOT_MODULE | |
| 1813 | 1813 | ||
| 1814 | - The fulfilled value of a linking promise is not a `vm.Module` object. | ||
| 1814 | + The fulfilled value of a linking promise is not a `vm.SourceTextModule` object. | ||
| 1815 | 1815 | ||
| 1816 | 1816 | <a id="ERR_VM_MODULE_STATUS"></a> | |
| 1817 | 1817 | ### ERR_VM_MODULE_STATUS | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,7 +43,7 @@ console.log(x); // 1; y is not defined. | |||
| 43 | 43 | **The vm module is not a security mechanism. Do not use it to run untrusted | |
| 44 | 44 | code**. | |
| 45 | 45 | ||
| 46 | - ## Class: vm.Module | ||
| 46 | + ## Class: vm.SourceTextModule | ||
| 47 | 47 | <!-- YAML | |
| 48 | 48 | added: v9.6.0 | |
| 49 | 49 | --> | |
@@ -53,20 +53,20 @@ added: v9.6.0 | |||
| 53 | 53 | *This feature is only available with the `--experimental-vm-modules` command | |
| 54 | 54 | flag enabled.* | |
| 55 | 55 | ||
| 56 | - The `vm.Module` class provides a low-level interface for using ECMAScript | ||
| 57 | - modules in VM contexts. It is the counterpart of the `vm.Script` class that | ||
| 58 | - closely mirrors [Source Text Module Record][]s as defined in the ECMAScript | ||
| 59 | - specification. | ||
| 56 | + The `vm.SourceTextModule` class provides a low-level interface for using | ||
| 57 | + ECMAScript modules in VM contexts. It is the counterpart of the `vm.Script` | ||
| 58 | + class that closely mirrors [Source Text Module Record][]s as defined in the | ||
| 59 | + ECMAScript specification. | ||
| 60 | 60 | ||
| 61 | - Unlike `vm.Script` however, every `vm.Module` object is bound to a context from | ||
| 62 | - its creation. Operations on `vm.Module` objects are intrinsically asynchronous, | ||
| 63 | - in contrast with the synchronous nature of `vm.Script` objects. With the help | ||
| 64 | - of async functions, however, manipulating `vm.Module` objects is fairly | ||
| 65 | - straightforward. | ||
| 61 | + Unlike `vm.Script` however, every `vm.SourceTextModule` object is bound to a | ||
| 62 | + context from its creation. Operations on `vm.SourceTextModule` objects are | ||
| 63 | + intrinsically asynchronous, in contrast with the synchronous nature of | ||
| 64 | + `vm.Script` objects. With the help of async functions, however, manipulating | ||
| 65 | + `vm.SourceTextModule` objects is fairly straightforward. | ||
| 66 | 66 | ||
| 67 | - Using a `vm.Module` object requires four distinct steps: creation/parsing, | ||
| 68 | - linking, instantiation, and evaluation. These four steps are illustrated in the | ||
| 69 | - following example. | ||
| 67 | + Using a `vm.SourceTextModule` object requires four distinct steps: | ||
| 68 | + creation/parsing, linking, instantiation, and evaluation. These four steps are | ||
| 69 | + illustrated in the following example. | ||
| 70 | 70 | ||
| 71 | 71 | This implementation lies at a lower level than the [ECMAScript Module | |
| 72 | 72 | loader][]. There is also currently no way to interact with the Loader, though | |
@@ -80,15 +80,15 @@ const contextifiedSandbox = vm.createContext({ secret: 42 }); | |||
| 80 | 80 | (async () => { | |
| 81 | 81 | // Step 1 | |
| 82 | 82 | // | |
| 83 | - // Create a Module by constructing a new `vm.Module` object. This parses the | ||
| 84 | - // provided source text, throwing a `SyntaxError` if anything goes wrong. By | ||
| 85 | - // default, a Module is created in the top context. But here, we specify | ||
| 86 | - // `contextifiedSandbox` as the context this Module belongs to. | ||
| 83 | + // Create a Module by constructing a new `vm.SourceTextModule` object. This | ||
| 84 | + // parses the provided source text, throwing a `SyntaxError` if anything goes | ||
| 85 | + // wrong. By default, a Module is created in the top context. But here, we | ||
| 86 | + // specify `contextifiedSandbox` as the context this Module belongs to. | ||
| 87 | 87 | // | |
| 88 | 88 | // Here, we attempt to obtain the default export from the module "foo", and | |
| 89 | 89 | // put it into local binding "secret". | |
| 90 | 90 | ||
| 91 | - const bar = new vm.Module(` | ||
| 91 | + const bar = new vm.SourceTextModule(` | ||
| 92 | 92 | import s from 'foo'; | |
| 93 | 93 | s; | |
| 94 | 94 | `, { context: contextifiedSandbox }); | |
@@ -118,7 +118,7 @@ const contextifiedSandbox = vm.createContext({ secret: 42 }); | |||
| 118 | 118 | ||
| 119 | 119 | async function linker(specifier, referencingModule) { | |
| 120 | 120 | if (specifier === 'foo') { | |
| 121 | - return new vm.Module(` | ||
| 121 | + return new vm.SourceTextModule(` | ||
| 122 | 122 | // The "secret" variable refers to the global variable we added to | |
| 123 | 123 | // "contextifiedSandbox" when creating the context. | |
| 124 | 124 | export default secret; | |
@@ -155,7 +155,7 @@ const contextifiedSandbox = vm.createContext({ secret: 42 }); | |||
| 155 | 155 | })(); | |
| 156 | 156 | ``` | |
| 157 | 157 | ||
| 158 | - ### Constructor: new vm.Module(code[, options]) | ||
| 158 | + ### Constructor: new vm.SourceTextModule(code[, options]) | ||
| 159 | 159 | ||
| 160 | 160 | * `code` {string} JavaScript Module code to parse | |
| 161 | 161 | * `options` | |
@@ -170,7 +170,7 @@ const contextifiedSandbox = vm.createContext({ secret: 42 }); | |||
| 170 | 170 | * `initalizeImportMeta` {Function} Called during evaluation of this `Module` | |
| 171 | 171 | to initialize the `import.meta`. This function has the signature `(meta, | |
| 172 | 172 | module)`, where `meta` is the `import.meta` object in the `Module`, and | |
| 173 | - `module` is this `vm.Module` object. | ||
| 173 | + `module` is this `vm.SourceTextModule` object. | ||
| 174 | 174 | ||
| 175 | 175 | Creates a new ES `Module` object. | |
| 176 | 176 | ||
@@ -185,7 +185,7 @@ const vm = require('vm'); | |||
| 185 | 185 | const contextifiedSandbox = vm.createContext({ secret: 42 }); | |
| 186 | 186 | ||
| 187 | 187 | (async () => { | |
| 188 | - const module = new vm.Module( | ||
| 188 | + const module = new vm.SourceTextModule( | ||
| 189 | 189 | 'Object.getPrototypeOf(import.meta.prop).secret = secret;', | |
| 190 | 190 | { | |
| 191 | 191 | initializeImportMeta(meta) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ const { URL } = require('url'); | |||
| 13 | 13 | const { | |
| 14 | 14 | initImportMetaMap, | |
| 15 | 15 | wrapToModuleMap | |
| 16 | - } = require('internal/vm/module'); | ||
| 16 | + } = require('internal/vm/source_text_module'); | ||
| 17 | 17 | ||
| 18 | 18 | function normalizeReferrerURL(referrer) { | |
| 19 | 19 | if (typeof referrer === 'string' && path.isAbsolute(referrer)) { | |
@@ -30,8 +30,8 @@ function initializeImportMetaObject(wrap, meta) { | |||
| 30 | 30 | } else { | |
| 31 | 31 | const initializeImportMeta = initImportMetaMap.get(vmModule); | |
| 32 | 32 | if (initializeImportMeta !== undefined) { | |
| 33 | - // This ModuleWrap belongs to vm.Module, initializer callback was | ||
| 34 | - // provided. | ||
| 33 | + // This ModuleWrap belongs to vm.SourceTextModule, | ||
| 34 | + // initializer callback was provided. | ||
| 35 | 35 | initializeImportMeta(meta, vmModule); | |
| 36 | 36 | } | |
| 37 | 37 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,15 +44,16 @@ const perContextModuleId = new WeakMap(); | |||
| 44 | 44 | const wrapMap = new WeakMap(); | |
| 45 | 45 | const dependencyCacheMap = new WeakMap(); | |
| 46 | 46 | const linkingStatusMap = new WeakMap(); | |
| 47 | - // vm.Module -> function | ||
| 47 | + // vm.SourceTextModule -> function | ||
| 48 | 48 | const initImportMetaMap = new WeakMap(); | |
| 49 | - // ModuleWrap -> vm.Module | ||
| 49 | + // ModuleWrap -> vm.SourceTextModule | ||
| 50 | 50 | const wrapToModuleMap = new WeakMap(); | |
| 51 | 51 | const defaultModuleName = 'vm:module'; | |
| 52 | 52 | ||
| 53 | - class Module { | ||
| 53 | + // TODO(devsnek): figure out AbstractModule class or protocol | ||
| 54 | + class SourceTextModule { | ||
| 54 | 55 | constructor(src, options = {}) { | |
| 55 | - emitExperimentalWarning('vm.Module'); | ||
| 56 | + emitExperimentalWarning('vm.SourceTextModule'); | ||
| 56 | 57 | ||
| 57 | 58 | if (typeof src !== 'string') | |
| 58 | 59 | throw new ERR_INVALID_ARG_TYPE('src', 'string', src); | |
@@ -230,7 +231,7 @@ class Module { | |||
| 230 | 231 | ||
| 231 | 232 | [customInspectSymbol](depth, options) { | |
| 232 | 233 | let ctor = getConstructorOf(this); | |
| 233 | - ctor = ctor === null ? Module : ctor; | ||
| 234 | + ctor = ctor === null ? SourceTextModule : ctor; | ||
| 234 | 235 | ||
| 235 | 236 | if (typeof depth === 'number' && depth < 0) | |
| 236 | 237 | return options.stylize(`[${ctor.name}]`, 'special'); | |
@@ -254,7 +255,7 @@ function validateInteger(prop, propName) { | |||
| 254 | 255 | } | |
| 255 | 256 | ||
| 256 | 257 | module.exports = { | |
| 257 | - Module, | ||
| 258 | + SourceTextModule, | ||
| 258 | 259 | initImportMetaMap, | |
| 259 | 260 | wrapToModuleMap | |
| 260 | 261 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -308,5 +308,7 @@ module.exports = { | |||
| 308 | 308 | isContext, | |
| 309 | 309 | }; | |
| 310 | 310 | ||
| 311 | - if (process.binding('config').experimentalVMModules) | ||
| 312 | - module.exports.Module = require('internal/vm/module').Module; | ||
| 311 | + if (process.binding('config').experimentalVMModules) { | ||
| 312 | + const { SourceTextModule } = require('internal/vm/source_text_module'); | ||
| 313 | + module.exports.SourceTextModule = SourceTextModule; | ||
| 314 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -166,7 +166,7 @@ | |||
| 166 | 166 | 'lib/internal/v8_prof_processor.js', | |
| 167 | 167 | 'lib/internal/validators.js', | |
| 168 | 168 | 'lib/internal/stream_base_commons.js', | |
| 169 | - 'lib/internal/vm/module.js', | ||
| 169 | + 'lib/internal/vm/source_text_module.js', | ||
| 170 | 170 | 'lib/internal/worker.js', | |
| 171 | 171 | 'lib/internal/streams/lazy_transform.js', | |
| 172 | 172 | 'lib/internal/streams/async_iterator.js', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,11 +5,11 @@ | |||
| 5 | 5 | require('../common'); | |
| 6 | 6 | const assert = require('assert'); | |
| 7 | 7 | ||
| 8 | - const { Module } = require('vm'); | ||
| 8 | + const { SourceTextModule } = require('vm'); | ||
| 9 | 9 | const { inspect } = require('util'); | |
| 10 | 10 | ||
| 11 | 11 | (async () => { | |
| 12 | - const m = new Module('export const a = 1; export var b = 2'); | ||
| 12 | + const m = new SourceTextModule('export const a = 1; export var b = 2'); | ||
| 13 | 13 | await m.link(() => 0); | |
| 14 | 14 | m.instantiate(); | |
| 15 | 15 | assert.strictEqual( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -126,7 +126,7 @@ for (const [ value, _method ] of [ | |||
| 126 | 126 | } | |
| 127 | 127 | ||
| 128 | 128 | (async () => { | |
| 129 | - const m = new vm.Module(''); | ||
| 129 | + const m = new vm.SourceTextModule(''); | ||
| 130 | 130 | await m.link(() => 0); | |
| 131 | 131 | m.instantiate(); | |
| 132 | 132 | await m.evaluate(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,15 +4,15 @@ | |||
| 4 | 4 | ||
| 5 | 5 | const common = require('../common'); | |
| 6 | 6 | const assert = require('assert'); | |
| 7 | - const { Module, createContext } = require('vm'); | ||
| 7 | + const { SourceTextModule, createContext } = require('vm'); | ||
| 8 | 8 | ||
| 9 | 9 | (async function test1() { | |
| 10 | 10 | const context = createContext({ | |
| 11 | 11 | foo: 'bar', | |
| 12 | 12 | baz: undefined, | |
| 13 | 13 | typeofProcess: undefined, | |
| 14 | 14 | }); | |
| 15 | - const m = new Module( | ||
| 15 | + const m = new SourceTextModule( | ||
| 16 | 16 | 'baz = foo; typeofProcess = typeof process; typeof Object;', | |
| 17 | 17 | { context } | |
| 18 | 18 | ); | |
@@ -32,7 +32,7 @@ const { Module, createContext } = require('vm'); | |||
| 32 | 32 | }()); | |
| 33 | 33 | ||
| 34 | 34 | (async () => { | |
| 35 | - const m = new Module( | ||
| 35 | + const m = new SourceTextModule( | ||
| 36 | 36 | 'global.vmResult = "foo"; Object.prototype.toString.call(process);' | |
| 37 | 37 | ); | |
| 38 | 38 | await m.link(common.mustNotCall()); | |
@@ -44,7 +44,7 @@ const { Module, createContext } = require('vm'); | |||
| 44 | 44 | })(); | |
| 45 | 45 | ||
| 46 | 46 | (async () => { | |
| 47 | - const m = new Module('while (true) {}'); | ||
| 47 | + const m = new SourceTextModule('while (true) {}'); | ||
| 48 | 48 | await m.link(common.mustNotCall()); | |
| 49 | 49 | m.instantiate(); | |
| 50 | 50 | await m.evaluate({ timeout: 500 }) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,12 +5,12 @@ | |||
| 5 | 5 | const common = require('../common'); | |
| 6 | 6 | ||
| 7 | 7 | const assert = require('assert'); | |
| 8 | - const { Module, createContext } = require('vm'); | ||
| 8 | + const { SourceTextModule, createContext } = require('vm'); | ||
| 9 | 9 | ||
| 10 | 10 | const finished = common.mustCall(); | |
| 11 | 11 | ||
| 12 | 12 | (async function() { | |
| 13 | - const m = new Module('import("foo")', { context: createContext() }); | ||
| 13 | + const m = new SourceTextModule('import("foo")', { context: createContext() }); | ||
| 14 | 14 | await m.link(common.mustNotCall()); | |
| 15 | 15 | m.instantiate(); | |
| 16 | 16 | const { result } = await m.evaluate(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments