summaryrefslogtreecommitdiff
path: root/test/parallel/test-vm-module-basic.js
blob: 1f699ddf5b43343c76a4480402fa8726cb1cb3b7 (plain)
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
'use strict';

// Flags: --experimental-vm-modules

const common = require('../common');
const assert = require('assert');
const { Module, createContext } = require('vm');

(async function test1() {
  const context = createContext({
    foo: 'bar',
    baz: undefined,
    typeofProcess: undefined,
  });
  const m = new Module(
    '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 Module(
    '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 Module('while (true) {}');
  await m.link(common.mustNotCall());
  m.instantiate();
  await m.evaluate({ timeout: 500 })
    .then(() => assert(false), () => {});
})();