Skip to content

Commit 9a88fe4

Browse files
devsnektargos
authored andcommitted
vm: rename vm.Module to vm.SourceTextModule
At the last TC39 meeting, a new type of Module Records backed by JavaScript source called Dynamic Module Records was discussed, and it is now at Stage 1. Regardless of whether that proposal makes it all the way into the spec, SourceTextModule is indeed a more descriptive and accurate name for what this class represents. PR-URL: #22007 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: John-David Dalton <[email protected]> Reviewed-By: Bradley Farias <[email protected]>
1 parent 02e665c commit 9a88fe4

14 files changed

+86
-81
lines changed

doc/api/errors.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ The module must be successfully linked before instantiation.
18111811
<a id="ERR_VM_MODULE_NOT_MODULE"></a>
18121812
### ERR_VM_MODULE_NOT_MODULE
18131813

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.
18151815

18161816
<a id="ERR_VM_MODULE_STATUS"></a>
18171817
### ERR_VM_MODULE_STATUS

doc/api/vm.md

+22-22
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ console.log(x); // 1; y is not defined.
4343
**The vm module is not a security mechanism. Do not use it to run untrusted
4444
code**.
4545

46-
## Class: vm.Module
46+
## Class: vm.SourceTextModule
4747
<!-- YAML
4848
added: v9.6.0
4949
-->
@@ -53,20 +53,20 @@ added: v9.6.0
5353
*This feature is only available with the `--experimental-vm-modules` command
5454
flag enabled.*
5555

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.
6060

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.
6666

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.
7070

7171
This implementation lies at a lower level than the [ECMAScript Module
7272
loader][]. There is also currently no way to interact with the Loader, though
@@ -80,15 +80,15 @@ const contextifiedSandbox = vm.createContext({ secret: 42 });
8080
(async () => {
8181
// Step 1
8282
//
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.
8787
//
8888
// Here, we attempt to obtain the default export from the module "foo", and
8989
// put it into local binding "secret".
9090

91-
const bar = new vm.Module(`
91+
const bar = new vm.SourceTextModule(`
9292
import s from 'foo';
9393
s;
9494
`, { context: contextifiedSandbox });
@@ -118,7 +118,7 @@ const contextifiedSandbox = vm.createContext({ secret: 42 });
118118

119119
async function linker(specifier, referencingModule) {
120120
if (specifier === 'foo') {
121-
return new vm.Module(`
121+
return new vm.SourceTextModule(`
122122
// The "secret" variable refers to the global variable we added to
123123
// "contextifiedSandbox" when creating the context.
124124
export default secret;
@@ -155,7 +155,7 @@ const contextifiedSandbox = vm.createContext({ secret: 42 });
155155
})();
156156
```
157157

158-
### Constructor: new vm.Module(code[, options])
158+
### Constructor: new vm.SourceTextModule(code[, options])
159159

160160
* `code` {string} JavaScript Module code to parse
161161
* `options`
@@ -170,7 +170,7 @@ const contextifiedSandbox = vm.createContext({ secret: 42 });
170170
* `initalizeImportMeta` {Function} Called during evaluation of this `Module`
171171
to initialize the `import.meta`. This function has the signature `(meta,
172172
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.
174174

175175
Creates a new ES `Module` object.
176176

@@ -185,7 +185,7 @@ const vm = require('vm');
185185
const contextifiedSandbox = vm.createContext({ secret: 42 });
186186

187187
(async () => {
188-
const module = new vm.Module(
188+
const module = new vm.SourceTextModule(
189189
'Object.getPrototypeOf(import.meta.prop).secret = secret;',
190190
{
191191
initializeImportMeta(meta) {

lib/internal/process/esm_loader.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { URL } = require('url');
1313
const {
1414
initImportMetaMap,
1515
wrapToModuleMap
16-
} = require('internal/vm/module');
16+
} = require('internal/vm/source_text_module');
1717

1818
function normalizeReferrerURL(referrer) {
1919
if (typeof referrer === 'string' && path.isAbsolute(referrer)) {
@@ -30,8 +30,8 @@ function initializeImportMetaObject(wrap, meta) {
3030
} else {
3131
const initializeImportMeta = initImportMetaMap.get(vmModule);
3232
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.
3535
initializeImportMeta(meta, vmModule);
3636
}
3737
}

lib/internal/vm/module.js lib/internal/vm/source_text_module.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ const perContextModuleId = new WeakMap();
4444
const wrapMap = new WeakMap();
4545
const dependencyCacheMap = new WeakMap();
4646
const linkingStatusMap = new WeakMap();
47-
// vm.Module -> function
47+
// vm.SourceTextModule -> function
4848
const initImportMetaMap = new WeakMap();
49-
// ModuleWrap -> vm.Module
49+
// ModuleWrap -> vm.SourceTextModule
5050
const wrapToModuleMap = new WeakMap();
5151
const defaultModuleName = 'vm:module';
5252

53-
class Module {
53+
// TODO(devsnek): figure out AbstractModule class or protocol
54+
class SourceTextModule {
5455
constructor(src, options = {}) {
55-
emitExperimentalWarning('vm.Module');
56+
emitExperimentalWarning('vm.SourceTextModule');
5657

5758
if (typeof src !== 'string')
5859
throw new ERR_INVALID_ARG_TYPE('src', 'string', src);
@@ -230,7 +231,7 @@ class Module {
230231

231232
[customInspectSymbol](depth, options) {
232233
let ctor = getConstructorOf(this);
233-
ctor = ctor === null ? Module : ctor;
234+
ctor = ctor === null ? SourceTextModule : ctor;
234235

235236
if (typeof depth === 'number' && depth < 0)
236237
return options.stylize(`[${ctor.name}]`, 'special');
@@ -254,7 +255,7 @@ function validateInteger(prop, propName) {
254255
}
255256

256257
module.exports = {
257-
Module,
258+
SourceTextModule,
258259
initImportMetaMap,
259260
wrapToModuleMap
260261
};

lib/vm.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,7 @@ module.exports = {
308308
isContext,
309309
};
310310

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+
}

node.gyp

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
'lib/internal/v8_prof_processor.js',
167167
'lib/internal/validators.js',
168168
'lib/internal/stream_base_commons.js',
169-
'lib/internal/vm/module.js',
169+
'lib/internal/vm/source_text_module.js',
170170
'lib/internal/worker.js',
171171
'lib/internal/streams/lazy_transform.js',
172172
'lib/internal/streams/async_iterator.js',

test/parallel/test-util-inspect-namespace.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
require('../common');
66
const assert = require('assert');
77

8-
const { Module } = require('vm');
8+
const { SourceTextModule } = require('vm');
99
const { inspect } = require('util');
1010

1111
(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');
1313
await m.link(() => 0);
1414
m.instantiate();
1515
assert.strictEqual(

test/parallel/test-util-types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ for (const [ value, _method ] of [
126126
}
127127

128128
(async () => {
129-
const m = new vm.Module('');
129+
const m = new vm.SourceTextModule('');
130130
await m.link(() => 0);
131131
m.instantiate();
132132
await m.evaluate();

test/parallel/test-vm-module-basic.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
const common = require('../common');
66
const assert = require('assert');
7-
const { Module, createContext } = require('vm');
7+
const { SourceTextModule, createContext } = require('vm');
88

99
(async function test1() {
1010
const context = createContext({
1111
foo: 'bar',
1212
baz: undefined,
1313
typeofProcess: undefined,
1414
});
15-
const m = new Module(
15+
const m = new SourceTextModule(
1616
'baz = foo; typeofProcess = typeof process; typeof Object;',
1717
{ context }
1818
);
@@ -32,7 +32,7 @@ const { Module, createContext } = require('vm');
3232
}());
3333

3434
(async () => {
35-
const m = new Module(
35+
const m = new SourceTextModule(
3636
'global.vmResult = "foo"; Object.prototype.toString.call(process);'
3737
);
3838
await m.link(common.mustNotCall());
@@ -44,7 +44,7 @@ const { Module, createContext } = require('vm');
4444
})();
4545

4646
(async () => {
47-
const m = new Module('while (true) {}');
47+
const m = new SourceTextModule('while (true) {}');
4848
await m.link(common.mustNotCall());
4949
m.instantiate();
5050
await m.evaluate({ timeout: 500 })

test/parallel/test-vm-module-dynamic-import.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
const common = require('../common');
66

77
const assert = require('assert');
8-
const { Module, createContext } = require('vm');
8+
const { SourceTextModule, createContext } = require('vm');
99

1010
const finished = common.mustCall();
1111

1212
(async function() {
13-
const m = new Module('import("foo")', { context: createContext() });
13+
const m = new SourceTextModule('import("foo")', { context: createContext() });
1414
await m.link(common.mustNotCall());
1515
m.instantiate();
1616
const { result } = await m.evaluate();

0 commit comments

Comments
 (0)