1// Flags: --experimental-vm-modules 2'use strict'; 3 4// https://github.com/nodejs/node/issues/3020 5// Promises used to allow code to escape the timeout 6// set for runInContext, runInNewContext, and runInThisContext. 7 8const common = require('../common'); 9const assert = require('assert'); 10const vm = require('vm'); 11 12const NS_PER_MS = 1000000n; 13 14const hrtime = process.hrtime.bigint; 15 16function loop() { 17 const start = hrtime(); 18 while (1) { 19 const current = hrtime(); 20 const span = (current - start) / NS_PER_MS; 21 if (span >= 100n) { 22 throw new Error( 23 `escaped timeout at ${span} milliseconds!`); 24 } 25 } 26} 27 28assert.rejects(async () => { 29 const module = new vm.SourceTextModule( 30 'Promise.resolve().then(() => loop()); loop();', 31 { 32 context: vm.createContext({ 33 hrtime, 34 loop 35 }, { microtaskMode: 'afterEvaluate' }) 36 }); 37 await module.link(common.mustNotCall()); 38 await module.evaluate({ timeout: 5 }); 39}, { 40 code: 'ERR_SCRIPT_EXECUTION_TIMEOUT', 41 message: 'Script execution timed out after 5ms' 42}); 43