• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Flags: --experimental-vm-modules
4
5const common = require('../common');
6const assert = require('assert');
7const {
8  Module,
9  SourceTextModule,
10  SyntheticModule,
11  createContext
12} = require('vm');
13const util = require('util');
14
15(async function test1() {
16  const context = createContext({
17    foo: 'bar',
18    baz: undefined,
19    typeofProcess: undefined,
20  });
21  const m = new SourceTextModule(
22    'baz = foo; typeofProcess = typeof process; typeof Object;',
23    { context }
24  );
25  assert.strictEqual(m.status, 'unlinked');
26  await m.link(common.mustNotCall());
27  assert.strictEqual(m.status, 'linked');
28  const result = await m.evaluate();
29  assert.strictEqual(m.status, 'evaluated');
30  assert.strictEqual(Object.getPrototypeOf(result), null);
31  assert.deepStrictEqual(context, {
32    foo: 'bar',
33    baz: 'bar',
34    typeofProcess: 'undefined'
35  });
36  assert.strictEqual(result.result, 'function');
37}());
38
39(async () => {
40  const m = new SourceTextModule(
41    'global.vmResult = "foo"; Object.prototype.toString.call(process);'
42  );
43  await m.link(common.mustNotCall());
44  const { result } = await m.evaluate();
45  assert.strictEqual(global.vmResult, 'foo');
46  assert.strictEqual(result, '[object process]');
47  delete global.vmResult;
48})();
49
50(async () => {
51  const m = new SourceTextModule('while (true) {}');
52  await m.link(common.mustNotCall());
53  await m.evaluate({ timeout: 500 })
54    .then(() => assert(false), () => {});
55})();
56
57// Check the generated identifier for each module
58(async () => {
59  const context1 = createContext({ });
60  const context2 = createContext({ });
61
62  const m1 = new SourceTextModule('1', { context: context1 });
63  assert.strictEqual(m1.identifier, 'vm:module(0)');
64  const m2 = new SourceTextModule('2', { context: context1 });
65  assert.strictEqual(m2.identifier, 'vm:module(1)');
66  const m3 = new SourceTextModule('3', { context: context2 });
67  assert.strictEqual(m3.identifier, 'vm:module(0)');
68})();
69
70// Check inspection of the instance
71{
72  const context = createContext({ foo: 'bar' });
73  const m = new SourceTextModule('1', { context });
74
75  assert.strictEqual(
76    util.inspect(m),
77    `SourceTextModule {
78  status: 'unlinked',
79  identifier: 'vm:module(0)',
80  context: { foo: 'bar' }
81}`
82  );
83
84  assert.strictEqual(util.inspect(m, { depth: -1 }), '[SourceTextModule]');
85
86  assert.strictEqual(
87    m[util.inspect.custom].call(Object.create(null)),
88    'Module { status: undefined, identifier: undefined, context: undefined }',
89  );
90}
91
92{
93  const context = createContext({ foo: 'bar' });
94  const m = new SyntheticModule([], () => {}, { context });
95
96  assert.strictEqual(
97    util.inspect(m),
98    `SyntheticModule {
99  status: 'unlinked',
100  identifier: 'vm:module(0)',
101  context: { foo: 'bar' }
102}`
103  );
104
105  assert.strictEqual(util.inspect(m, { depth: -1 }), '[SyntheticModule]');
106}
107
108// Check dependencies getter returns same object every time
109{
110  const m = new SourceTextModule('');
111  const dep = m.dependencySpecifiers;
112  assert.notStrictEqual(dep, undefined);
113  assert.strictEqual(dep, m.dependencySpecifiers);
114}
115
116// Check the impossibility of creating an abstract instance of the Module.
117{
118  assert.throws(() => new Module(), {
119    message: 'Module is not a constructor',
120    name: 'TypeError'
121  });
122}
123
124// Check to throws invalid exportNames
125{
126  assert.throws(() => new SyntheticModule(undefined, () => {}, {}), {
127    message: 'The "exportNames" argument must be an ' +
128        'Array of unique strings.' +
129        ' Received undefined',
130    name: 'TypeError'
131  });
132}
133
134// Check to throws duplicated exportNames
135// https://github.com/nodejs/node/issues/32806
136{
137  assert.throws(() => new SyntheticModule(['x', 'x'], () => {}, {}), {
138    message: 'The argument \'exportNames.x\' is duplicated. Received \'x\'',
139    name: 'TypeError',
140  });
141}
142
143// Check to throws invalid evaluateCallback
144{
145  assert.throws(() => new SyntheticModule([], undefined, {}), {
146    message: 'The "evaluateCallback" argument must be of type function.' +
147      ' Received undefined',
148    name: 'TypeError'
149  });
150}
151
152// Check to throws invalid options
153{
154  assert.throws(() => new SyntheticModule([], () => {}, null), {
155    message: 'The "options" argument must be of type object.' +
156      ' Received null',
157    name: 'TypeError'
158  });
159}
160