• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-gc --experimental-vm-modules
2
3'use strict';
4
5// This tests that vm.Script would not get GC'ed while the script can still
6// initiate dynamic import.
7// See https://github.com/nodejs/node/issues/43205.
8
9require('../common');
10const vm = require('vm');
11
12const code = `
13new Promise(resolve => {
14  setTimeout(() => {
15    gc();  // vm.Script should not be GC'ed while the script is alive.
16    resolve();
17  }, 1);
18}).then(() => import('foo'));`;
19
20// vm.runInThisContext creates a vm.Script underneath, which should not be GC'ed
21// while import() can still be initiated.
22vm.runInThisContext(code, {
23  async importModuleDynamically() {
24    const m = new vm.SyntheticModule(['bar'], () => {
25      m.setExport('bar', 1);
26    });
27
28    await m.link(() => {});
29    await m.evaluate();
30    return m;
31  }
32});
33