1'use strict'; 2 3const { 4 ArrayPrototypePush, 5 ArrayPrototypePushApply, 6 ArrayPrototypeSlice, 7 StringPrototypeSlice, 8} = primordials; 9 10const Buffer = require('buffer').Buffer; 11const console = require('internal/console/global'); 12const vm = require('vm'); 13const { SourceTextModule } = require('internal/vm/module'); 14 15const natives = internalBinding('natives'); 16 17async function linker(specifier, referencingModule) { 18 // Transform "./file.mjs" to "file" 19 const file = StringPrototypeSlice(specifier, 2, -4); 20 const code = natives[`internal/deps/v8/tools/${file}`]; 21 return new SourceTextModule(code, { context: referencingModule.context }); 22} 23 24(async () => { 25 const tickArguments = []; 26 if (process.platform === 'darwin') { 27 ArrayPrototypePush(tickArguments, '--mac'); 28 } else if (process.platform === 'win32') { 29 ArrayPrototypePush(tickArguments, '--windows'); 30 } 31 ArrayPrototypePushApply(tickArguments, 32 ArrayPrototypeSlice(process.argv, 1)); 33 34 const context = vm.createContext({ 35 arguments: tickArguments, 36 write(s) { process.stdout.write(s); }, 37 printErr(err) { console.error(err); }, 38 console, 39 process, 40 Buffer, 41 }); 42 43 const polyfill = natives['internal/v8_prof_polyfill']; 44 const script = `(function(module, require) { 45 ${polyfill} 46 })`; 47 48 vm.runInContext(script, context)(module, require); 49 50 const tickProcessor = natives['internal/deps/v8/tools/tickprocessor-driver']; 51 const tickprocessorDriver = new SourceTextModule(tickProcessor, { context }); 52 await tickprocessorDriver.link(linker); 53 await tickprocessorDriver.evaluate(); 54})(); 55