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