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
|
'use strict';
// Flags: --experimental-vm-modules
const common = require('../common');
const assert = require('assert');
const { SourceTextModule, createContext } = require('vm');
async function createEmptyLinkedModule() {
const m = new SourceTextModule('');
await m.link(common.mustNotCall());
return m;
}
async function checkArgType() {
common.expectsError(() => {
new SourceTextModule();
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
});
for (const invalidOptions of [
0, 1, null, true, 'str', () => {}, { identifier: 0 }, Symbol.iterator,
{ context: null }, { context: 'hucairz' }, { context: {} }
]) {
common.expectsError(() => {
new SourceTextModule('', invalidOptions);
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
});
}
for (const invalidLinker of [
0, 1, undefined, null, true, 'str', {}, Symbol.iterator
]) {
await assert.rejects(async () => {
const m = new SourceTextModule('');
await m.link(invalidLinker);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
});
}
}
// Check methods/properties can only be used under a specific state.
async function checkModuleState() {
await assert.rejects(async () => {
const m = new SourceTextModule('');
await m.link(common.mustNotCall());
assert.strictEqual(m.status, 'linked');
await m.link(common.mustNotCall());
}, {
code: 'ERR_VM_MODULE_ALREADY_LINKED'
});
await assert.rejects(async () => {
const m = new SourceTextModule('');
m.link(common.mustNotCall());
assert.strictEqual(m.status, 'linking');
await m.link(common.mustNotCall());
}, {
code: 'ERR_VM_MODULE_STATUS'
});
await assert.rejects(async () => {
const m = new SourceTextModule('');
await m.evaluate();
}, {
code: 'ERR_VM_MODULE_STATUS',
message: 'Module status must be one of linked, evaluated, or errored'
});
await assert.rejects(async () => {
const m = new SourceTextModule('');
await m.evaluate(false);
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type Object. ' +
'Received type boolean'
});
common.expectsError(() => {
const m = new SourceTextModule('');
m.error;
}, {
code: 'ERR_VM_MODULE_STATUS',
message: 'Module status must be errored'
});
await assert.rejects(async () => {
const m = await createEmptyLinkedModule();
await m.evaluate();
m.error;
}, {
code: 'ERR_VM_MODULE_STATUS',
message: 'Module status must be errored'
});
common.expectsError(() => {
const m = new SourceTextModule('');
m.namespace;
}, {
code: 'ERR_VM_MODULE_STATUS',
message: 'Module status must not be unlinked or linking'
});
}
// Check link() fails when the returned module is not valid.
async function checkLinking() {
await assert.rejects(async () => {
const m = new SourceTextModule('import "foo";');
try {
await m.link(common.mustCall(() => ({})));
} catch (err) {
assert.strictEqual(m.status, 'errored');
throw err;
}
}, {
code: 'ERR_VM_MODULE_NOT_MODULE'
});
await assert.rejects(async () => {
const c = createContext({ a: 1 });
const foo = new SourceTextModule('', { context: c });
await foo.link(common.mustNotCall());
const bar = new SourceTextModule('import "foo";');
try {
await bar.link(common.mustCall(() => foo));
} catch (err) {
assert.strictEqual(bar.status, 'errored');
throw err;
}
}, {
code: 'ERR_VM_MODULE_DIFFERENT_CONTEXT'
});
await assert.rejects(async () => {
const erroredModule = new SourceTextModule('import "foo";');
try {
await erroredModule.link(common.mustCall(() => ({})));
} catch {
// ignored
} finally {
assert.strictEqual(erroredModule.status, 'errored');
}
const rootModule = new SourceTextModule('import "errored";');
await rootModule.link(common.mustCall(() => erroredModule));
}, {
code: 'ERR_VM_MODULE_LINKING_ERRORED'
});
}
common.expectsError(() => {
new SourceTextModule('', {
importModuleDynamically: 'hucairz'
});
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.importModuleDynamically"' +
' property must be of type function. Received type string'
});
// Check the JavaScript engine deals with exceptions correctly
async function checkExecution() {
await (async () => {
const m = new SourceTextModule('import { nonexistent } from "module";');
// There is no code for this exception since it is thrown by the JavaScript
// engine.
await assert.rejects(() => {
return m.link(common.mustCall(() => new SourceTextModule('')));
}, SyntaxError);
})();
await (async () => {
const m = new SourceTextModule('throw new Error();');
await m.link(common.mustNotCall());
const evaluatePromise = m.evaluate();
await evaluatePromise.catch(() => {});
assert.strictEqual(m.status, 'errored');
try {
await evaluatePromise;
} catch (err) {
assert.strictEqual(m.error, err);
return;
}
assert.fail('Missing expected exception');
})();
}
// Check for error thrown when breakOnSigint is not a boolean for evaluate()
async function checkInvalidOptionForEvaluate() {
await assert.rejects(async () => {
const m = new SourceTextModule('export const a = 1; export let b = 2');
await m.evaluate({ breakOnSigint: 'a-string' });
}, {
name: 'TypeError',
message:
'The "options.breakOnSigint" property must be of type boolean. ' +
'Received type string',
code: 'ERR_INVALID_ARG_TYPE'
});
}
const finished = common.mustCall();
(async function main() {
await checkArgType();
await checkModuleState();
await checkLinking();
await checkExecution();
await checkInvalidOptionForEvaluate();
finished();
})();
|