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
|
'use strict';
// Flags: --experimental-vm-modules
const common = require('../common');
const assert = require('assert');
const { SourceTextModule, createContext } = require('vm');
const util = require('util');
(async function test1() {
const context = createContext({
foo: 'bar',
baz: undefined,
typeofProcess: undefined,
});
const m = new SourceTextModule(
'baz = foo; typeofProcess = typeof process; typeof Object;',
{ context }
);
assert.strictEqual(m.status, 'uninstantiated');
await m.link(common.mustNotCall());
m.instantiate();
assert.strictEqual(m.status, 'instantiated');
const result = await m.evaluate();
assert.strictEqual(m.status, 'evaluated');
assert.strictEqual(Object.getPrototypeOf(result), null);
assert.deepStrictEqual(context, {
foo: 'bar',
baz: 'bar',
typeofProcess: 'undefined'
});
assert.strictEqual(result.result, 'function');
}());
(async () => {
const m = new SourceTextModule(
'global.vmResult = "foo"; Object.prototype.toString.call(process);'
);
await m.link(common.mustNotCall());
m.instantiate();
const { result } = await m.evaluate();
assert.strictEqual(global.vmResult, 'foo');
assert.strictEqual(result, '[object process]');
delete global.vmResult;
})();
(async () => {
const m = new SourceTextModule('while (true) {}');
await m.link(common.mustNotCall());
m.instantiate();
await m.evaluate({ timeout: 500 })
.then(() => assert(false), () => {});
})();
// Check the generated url for each module
(async () => {
const context1 = createContext({ });
const context2 = createContext({ });
const m1 = new SourceTextModule('1', { context: context1 });
assert.strictEqual(m1.url, 'vm:module(0)');
const m2 = new SourceTextModule('2', { context: context1 });
assert.strictEqual(m2.url, 'vm:module(1)');
const m3 = new SourceTextModule('3', { context: context2 });
assert.strictEqual(m3.url, 'vm:module(0)');
})();
// Check inspection of the instance
{
const context = createContext({ foo: 'bar' });
const m = new SourceTextModule('1', { context });
assert.strictEqual(
util.inspect(m),
"SourceTextModule {\n status: 'uninstantiated',\n linkingStatus:" +
" 'unlinked',\n url: 'vm:module(0)',\n context: { foo: 'bar' }\n}"
);
assert.strictEqual(
m[util.inspect.custom].call(Object.create(null)),
'SourceTextModule {\n status: undefined,\n linkingStatus: undefined,' +
'\n url: undefined,\n context: undefined\n}'
);
assert.strictEqual(util.inspect(m, { depth: -1 }), '[SourceTextModule]');
}
// Check dependencies getter returns same object every time
{
const m = new SourceTextModule('');
const dep = m.dependencySpecifiers;
assert.notStrictEqual(dep, undefined);
assert.strictEqual(dep, m.dependencySpecifiers);
}
|