forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.js
More file actions
295 lines (251 loc) · 8.51 KB
/
Copy pathmodule.js
File metadata and controls
295 lines (251 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
'use strict';
const { internalBinding } = require('internal/bootstrap/loaders');
const { emitExperimentalWarning } = require('internal/util');
const { URL } = require('internal/url');
const { isContext } = process.binding('contextify');
const {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE,
ERR_VM_MODULE_ALREADY_LINKED,
ERR_VM_MODULE_DIFFERENT_CONTEXT,
ERR_VM_MODULE_LINKING_ERRORED,
ERR_VM_MODULE_NOT_LINKED,
ERR_VM_MODULE_NOT_MODULE,
ERR_VM_MODULE_STATUS,
} = require('internal/errors').codes;
const {
getConstructorOf,
customInspectSymbol,
} = require('internal/util');
const { SafePromise } = require('internal/safe_globals');
const { isModuleNamespaceObject } = require('util').types;
const {
ModuleWrap,
kUninstantiated,
kInstantiating,
kInstantiated,
kEvaluating,
kEvaluated,
kErrored,
} = internalBinding('module_wrap');
const STATUS_MAP = {
[kUninstantiated]: 'uninstantiated',
[kInstantiating]: 'instantiating',
[kInstantiated]: 'instantiated',
[kEvaluating]: 'evaluating',
[kEvaluated]: 'evaluated',
[kErrored]: 'errored',
};
let globalModuleId = 0;
const perContextModuleId = new WeakMap();
const wrapMap = new WeakMap();
const dependencyCacheMap = new WeakMap();
const linkingStatusMap = new WeakMap();
const linkerFnMap = new WeakMap();
class Module {
constructor(src, options = {}) {
emitExperimentalWarning('vm.Module');
if (typeof src !== 'string')
throw new ERR_INVALID_ARG_TYPE('src', 'string', src);
if (typeof options !== 'object' || options === null)
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
const {
context,
lineOffset = 0,
columnOffset = 0,
} = options;
if (context !== undefined) {
if (typeof context !== 'object' || context === null) {
throw new ERR_INVALID_ARG_TYPE('options.context', 'Object', context);
}
if (!isContext(context)) {
throw new ERR_INVALID_ARG_TYPE('options.context', 'vm.Context',
context);
}
}
let { url } = options;
if (url !== undefined) {
if (typeof url !== 'string') {
throw new ERR_INVALID_ARG_TYPE('options.url', 'string', url);
}
url = new URL(url).href;
} else if (context === undefined) {
url = `vm:module(${globalModuleId++})`;
} else if (perContextModuleId.has(context)) {
const curId = perContextModuleId.get(context);
url = `vm:module(${curId})`;
perContextModuleId.set(context, curId + 1);
} else {
url = 'vm:module(0)';
perContextModuleId.set(context, 1);
}
validateInteger(lineOffset, 'options.lineOffset');
validateInteger(columnOffset, 'options.columnOffset');
let { initializeImportMeta } = options;
if (initializeImportMeta !== undefined) {
if (typeof initializeImportMeta === 'function') {
const fn = initializeImportMeta;
initializeImportMeta = (meta) => fn(meta, this);
} else {
throw new ERR_INVALID_ARG_TYPE(
'options.initializeImportMeta', 'function', initializeImportMeta);
}
}
const wrap = new ModuleWrap(src, url, context, lineOffset, columnOffset);
const {
initializeImportMetaMap,
importModuleDynamicallyMap,
} = require('internal/process/esm_loader');
if (initializeImportMeta)
initializeImportMetaMap.set(wrap, initializeImportMeta);
importModuleDynamicallyMap.set(wrap, async (specifier) => {
const linker = linkerFnMap.get(this);
return callLinkerForNamespace(linker, specifier, this);
});
wrapMap.set(this, wrap);
linkingStatusMap.set(this, 'unlinked');
Object.defineProperties(this, {
url: { value: url, enumerable: true },
context: { value: context, enumerable: true },
});
}
get linkingStatus() {
return linkingStatusMap.get(this);
}
get status() {
return STATUS_MAP[wrapMap.get(this).getStatus()];
}
get namespace() {
const wrap = wrapMap.get(this);
if (wrap.getStatus() < kInstantiated)
throw new ERR_VM_MODULE_STATUS(
'must not be uninstantiated or instantiating'
);
return wrap.namespace();
}
get dependencySpecifiers() {
let deps = dependencyCacheMap.get(this);
if (deps !== undefined)
return deps;
deps = wrapMap.get(this).getStaticDependencySpecifiers();
Object.freeze(deps);
dependencyCacheMap.set(this, deps);
return deps;
}
get error() {
const wrap = wrapMap.get(this);
if (wrap.getStatus() !== kErrored)
throw new ERR_VM_MODULE_STATUS('must be errored');
return wrap.getError();
}
async link(linker) {
if (typeof linker !== 'function')
throw new ERR_INVALID_ARG_TYPE('linker', 'function', linker);
if (linkingStatusMap.get(this) !== 'unlinked')
throw new ERR_VM_MODULE_ALREADY_LINKED();
const wrap = wrapMap.get(this);
if (wrap.getStatus() !== kUninstantiated)
throw new ERR_VM_MODULE_STATUS('must be uninstantiated');
linkingStatusMap.set(this, 'linking');
linkerFnMap.set(this, linker);
const promises = wrap.link((s) => callLinkerForModuleWrap(linker, s, this));
try {
if (promises !== undefined)
await SafePromise.all(promises);
linkingStatusMap.set(this, 'linked');
} catch (err) {
linkingStatusMap.set(this, 'errored');
throw err;
}
}
instantiate() {
const wrap = wrapMap.get(this);
const status = wrap.getStatus();
if (status === kInstantiating || status === kEvaluating)
throw new ERR_VM_MODULE_STATUS('must not be instantiating or evaluating');
if (linkingStatusMap.get(this) !== 'linked')
throw new ERR_VM_MODULE_NOT_LINKED();
wrap.instantiate();
}
async evaluate(options = {}) {
if (typeof options !== 'object' || options === null) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
let timeout = options.timeout;
if (timeout === undefined) {
timeout = -1;
} else if (!Number.isInteger(timeout) || timeout <= 0) {
throw new ERR_INVALID_ARG_TYPE('options.timeout', 'a positive integer',
timeout);
}
const { breakOnSigint = false } = options;
if (typeof breakOnSigint !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE('options.breakOnSigint', 'boolean',
breakOnSigint);
}
const wrap = wrapMap.get(this);
const status = wrap.getStatus();
if (status !== kInstantiated &&
status !== kEvaluated &&
status !== kErrored) {
throw new ERR_VM_MODULE_STATUS(
'must be one of instantiated, evaluated, and errored'
);
}
const result = wrap.evaluate(timeout, breakOnSigint);
return { result, __proto__: null };
}
[customInspectSymbol](depth, options) {
let ctor = getConstructorOf(this);
ctor = ctor === null ? Module : ctor;
if (typeof depth === 'number' && depth < 0)
return options.stylize(`[${ctor.name}]`, 'special');
const o = Object.create({ constructor: ctor });
o.status = this.status;
o.linkingStatus = this.linkingStatus;
o.url = this.url;
o.context = this.context;
return require('util').inspect(o, options);
}
}
function validateInteger(prop, propName) {
if (!Number.isInteger(prop)) {
throw new ERR_INVALID_ARG_TYPE(propName, 'integer', prop);
}
if ((prop >> 0) !== prop) {
throw new ERR_OUT_OF_RANGE(propName, '32-bit integer', prop);
}
}
async function getWrapFromModule(m, scriptOrModule, linker) {
if (!m || !wrapMap.has(m))
throw new ERR_VM_MODULE_NOT_MODULE();
if (scriptOrModule instanceof Module &&
(m.context !== scriptOrModule.context))
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();
const childLinkingStatus = linkingStatusMap.get(m);
if (childLinkingStatus === 'errored')
throw new ERR_VM_MODULE_LINKING_ERRORED();
if (childLinkingStatus === 'unlinked')
await m.link(linker);
return wrapMap.get(m);
}
async function callLinkerForModuleWrap(linker, specifier, scriptOrModule) {
const m = await linker(specifier, scriptOrModule);
return getWrapFromModule(m, scriptOrModule, linker);
}
async function callLinkerForNamespace(linker, specifier, scriptOrModule) {
const m = await linker(specifier, scriptOrModule);
if (isModuleNamespaceObject(m))
return m;
const wrap = await getWrapFromModule(m, scriptOrModule, linker);
const status = wrap.getStatus();
if (status < kInstantiated)
wrap.instantiate();
if (status < kEvaluated)
await wrap.evaluate(-1, false);
return wrap.namespace();
}
module.exports = {
Module,
callLinkerForNamespace,
};