-
Notifications
You must be signed in to change notification settings - Fork 30.6k
/
Copy pathModule.js
205 lines (178 loc) Β· 5.87 KB
/
Module.js
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
'use strict';
const { emitExperimentalWarning } = require('internal/util');
const { URL } = require('internal/url');
const { kParsingContext, isContext } = process.binding('contextify');
const errors = require('internal/errors');
const {
getConstructorOf,
customInspectSymbol,
} = require('internal/util');
const { SafePromise } = require('internal/safe_globals');
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();
class Module {
constructor(src, options = {}) {
emitExperimentalWarning('vm.Module');
if (typeof src !== 'string')
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'src', 'string', src);
if (typeof options !== 'object' || options === null)
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'options', 'object', options);
let context;
if (options.context !== undefined) {
if (isContext(options.context)) {
context = options.context;
} else {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'options.context', 'vm.Context');
}
}
let url = options.url;
if (url !== undefined) {
if (typeof url !== 'string') {
throw new errors.TypeError(
'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);
}
const wrap = new ModuleWrap(src, url, {
[kParsingContext]: context,
lineOffset: options.lineOffset,
columnOffset: options.columnOffset
});
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 errors.Error('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 errors.Error('ERR_VM_MODULE_STATUS', 'must be errored');
return wrap.getError();
}
async link(linker) {
if (typeof linker !== 'function')
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'linker', 'function', linker);
if (linkingStatusMap.get(this) !== 'unlinked')
throw new errors.Error('ERR_VM_MODULE_ALREADY_LINKED');
const wrap = wrapMap.get(this);
if (wrap.getStatus() !== kUninstantiated)
throw new errors.Error('ERR_VM_MODULE_STATUS', 'must be uninstantiated');
linkingStatusMap.set(this, 'linking');
const promises = wrap.link(async (specifier) => {
const m = await linker(specifier, this);
if (!m || !wrapMap.has(m))
throw new errors.Error('ERR_VM_MODULE_NOT_MODULE');
if (m.context !== this.context)
throw new errors.Error('ERR_VM_MODULE_DIFFERENT_CONTEXT');
const childLinkingStatus = linkingStatusMap.get(m);
if (childLinkingStatus === 'errored')
throw new errors.Error('ERR_VM_MODULE_LINKING_ERRORED');
if (childLinkingStatus === 'unlinked')
await m.link(linker);
return wrapMap.get(m);
});
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 errors.Error(
'ERR_VM_MODULE_STATUS', 'must not be instantiating or evaluating');
if (linkingStatusMap.get(this) !== 'linked')
throw new errors.Error('ERR_VM_MODULE_NOT_LINKED');
wrap.instantiate();
}
async evaluate(options) {
const wrap = wrapMap.get(this);
const status = wrap.getStatus();
if (status !== kInstantiated &&
status !== kEvaluated &&
status !== kErrored) {
throw new errors.Error(
'ERR_VM_MODULE_STATUS',
'must be one of instantiated, evaluated, and errored');
}
const result = wrap.evaluate(options);
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);
}
}
module.exports = {
Module
};