11cb0ef41Sopenharmony_ci// Flags: --expose-gc --experimental-vm-modules
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// This tests that vm.Script would not get GC'ed while the script can still
61cb0ef41Sopenharmony_ci// initiate dynamic import.
71cb0ef41Sopenharmony_ci// See https://github.com/nodejs/node/issues/43205.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cirequire('../common');
101cb0ef41Sopenharmony_ciconst vm = require('vm');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst code = `
131cb0ef41Sopenharmony_cinew Promise(resolve => {
141cb0ef41Sopenharmony_ci  setTimeout(() => {
151cb0ef41Sopenharmony_ci    gc();  // vm.Script should not be GC'ed while the script is alive.
161cb0ef41Sopenharmony_ci    resolve();
171cb0ef41Sopenharmony_ci  }, 1);
181cb0ef41Sopenharmony_ci}).then(() => import('foo'));`;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci// vm.runInThisContext creates a vm.Script underneath, which should not be GC'ed
211cb0ef41Sopenharmony_ci// while import() can still be initiated.
221cb0ef41Sopenharmony_civm.runInThisContext(code, {
231cb0ef41Sopenharmony_ci  async importModuleDynamically() {
241cb0ef41Sopenharmony_ci    const m = new vm.SyntheticModule(['bar'], () => {
251cb0ef41Sopenharmony_ci      m.setExport('bar', 1);
261cb0ef41Sopenharmony_ci    });
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci    await m.link(() => {});
291cb0ef41Sopenharmony_ci    await m.evaluate();
301cb0ef41Sopenharmony_ci    return m;
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci});
33