| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 36ba9d9 commit 413db7c
15 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,10 +1,37 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | + // This main script is used by --prof-process to process logs generated by --prof. | ||
| 4 | + // The tick processor implementation is vendor-in from V8, placed in deps/v8/tools, | ||
| 5 | + // and they have different resolution rules compared to normal Node.js internal builtins, | ||
| 6 | + // despite being embedded into the binary as a built-in as well. | ||
| 7 | + | ||
| 8 | + // TODO(joyeecheung): put the context locals in import.meta. | ||
| 9 | + const { | ||
| 10 | + ObjectAssign, | ||
| 11 | + PromisePrototypeThen, | ||
| 12 | + globalThis, | ||
| 13 | + } = primordials; | ||
| 14 | + | ||
| 3 | 15 | const { | |
| 4 | 16 | prepareMainThreadExecution, | |
| 5 | 17 | markBootstrapComplete, | |
| 6 | 18 | } = require('internal/process/pre_execution'); | |
| 7 | 19 | ||
| 20 | + const { importBuiltinSourceTextModule } = internalBinding('builtins'); | ||
| 21 | + const { triggerUncaughtException } = internalBinding('errors'); | ||
| 22 | + | ||
| 8 | 23 | prepareMainThreadExecution(); | |
| 9 | 24 | markBootstrapComplete(); | |
| 10 | - require('internal/v8_prof_processor'); | ||
| 25 | + | ||
| 26 | + // Polyfill the context with globals needed by the tick processor. | ||
| 27 | + const { globals, openFile } = require('internal/v8_prof_polyfill'); | ||
| 28 | + ObjectAssign(globalThis, globals); | ||
| 29 | + openFile(process.argv[process.argv.length - 1]); | ||
| 30 | + | ||
| 31 | + // Load and evaluate the V8 tick processor as a source text module. | ||
| 32 | + const { promise } = importBuiltinSourceTextModule('internal/deps/v8/tools/tickprocessor-driver'); | ||
| 33 | + // We must catch the rejection asynchronously to print it out properly since | ||
| 34 | + // the tick processor contains top level await. | ||
| 35 | + PromisePrototypeThen(promise, undefined, (err) => { | ||
| 36 | + triggerUncaughtException(err, true /* fromPromise */); | ||
| 37 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,17 +30,17 @@ | |||
| 30 | 30 | /* eslint-disable node-core/prefer-primordials, no-restricted-globals */ | |
| 31 | 31 | /* global console */ | |
| 32 | 32 | ||
| 33 | - module.exports = { versionCheck }; | ||
| 33 | + const { | ||
| 34 | + ArrayPrototypePush, | ||
| 35 | + ArrayPrototypePushApply, | ||
| 36 | + ArrayPrototypeSlice, | ||
| 37 | + } = primordials; | ||
| 34 | 38 | ||
| 35 | - // Don't execute when required directly instead of being eval'd from | ||
| 36 | - // lib/internal/v8_prof_processor.js. This way we can test functions | ||
| 37 | - // from this file in isolation. | ||
| 38 | - if (module.id === 'internal/v8_prof_polyfill') return; | ||
| 39 | - | ||
| 40 | - // Node polyfill | ||
| 41 | 39 | const fs = require('fs'); | |
| 42 | 40 | const cp = require('child_process'); | |
| 43 | 41 | const { Buffer } = require('buffer'); | |
| 42 | + const { StringDecoder } = require('string_decoder'); | ||
| 43 | + | ||
| 44 | 44 | const os = { | |
| 45 | 45 | system: function(name, args) { | |
| 46 | 46 | if (process.platform === 'linux' && name === 'nm') { | |
@@ -68,24 +68,40 @@ const os = { | |||
| 68 | 68 | }, | |
| 69 | 69 | }; | |
| 70 | 70 | const print = console.log; | |
| 71 | + | ||
| 71 | 72 | function read(fileName) { | |
| 72 | 73 | return fs.readFileSync(fileName, 'utf8'); | |
| 73 | 74 | } | |
| 74 | 75 | const quit = process.exit; | |
| 75 | - // Polyfill "readline()". | ||
| 76 | - const logFile = globalThis.arguments[globalThis.arguments.length - 1]; | ||
| 77 | - try { | ||
| 78 | - fs.accessSync(logFile); | ||
| 79 | - } catch { | ||
| 80 | - console.error('Please provide a valid isolate file as the final argument.'); | ||
| 81 | - process.exit(1); | ||
| 76 | + | ||
| 77 | + const tickArguments = []; | ||
| 78 | + if (process.platform === 'darwin') { | ||
| 79 | + ArrayPrototypePush(tickArguments, '--mac'); | ||
| 80 | + } else if (process.platform === 'win32') { | ||
| 81 | + ArrayPrototypePush(tickArguments, '--windows'); | ||
| 82 | 82 | } | |
| 83 | - const fd = fs.openSync(logFile, 'r'); | ||
| 83 | + ArrayPrototypePushApply(tickArguments, | ||
| 84 | + ArrayPrototypeSlice(process.argv, 1)); | ||
| 85 | + | ||
| 86 | + function write(str) { process.stdout.write(str); }; | ||
| 87 | + function printErr(str) { process.stderr.write(str); }; | ||
| 88 | + | ||
| 89 | + let logFile; | ||
| 90 | + let fd; | ||
| 84 | 91 | const buf = Buffer.allocUnsafe(4096); | |
| 85 | - const dec = new (require('string_decoder').StringDecoder)('utf-8'); | ||
| 92 | + const dec = new StringDecoder('utf-8'); | ||
| 86 | 93 | let line = ''; | |
| 87 | 94 | ||
| 88 | - { | ||
| 95 | + function openFile(filename) { | ||
| 96 | + logFile = filename; | ||
| 97 | + try { | ||
| 98 | + fd = fs.openSync(logFile, 'r'); | ||
| 99 | + } catch (e) { | ||
| 100 | + console.error(`Cannot access log file: ${logFile}`); | ||
| 101 | + console.error('Please provide a valid isolate file as the final argument.'); | ||
| 102 | + throw e; | ||
| 103 | + } | ||
| 104 | + | ||
| 89 | 105 | const message = versionCheck(peekline(), process.versions.v8); | |
| 90 | 106 | if (message) console.log(message); | |
| 91 | 107 | } | |
@@ -162,10 +178,17 @@ function macCppfiltNm(out) { | |||
| 162 | 178 | }); | |
| 163 | 179 | } | |
| 164 | 180 | ||
| 165 | - Object.assign(globalThis, { | ||
| 166 | - os, | ||
| 167 | - print, | ||
| 168 | - read, | ||
| 169 | - quit, | ||
| 170 | - readline, | ||
| 171 | - }); | ||
| 181 | + module.exports = { | ||
| 182 | + globals: { | ||
| 183 | + arguments: tickArguments, | ||
| 184 | + write, | ||
| 185 | + printErr, | ||
| 186 | + os, | ||
| 187 | + print, | ||
| 188 | + read, | ||
| 189 | + quit, | ||
| 190 | + readline, | ||
| 191 | + }, | ||
| 192 | + openFile, | ||
| 193 | + versionCheck, | ||
| 194 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,6 +73,7 @@ | |||
| 73 | 73 | 'src/async_context_frame.cc', | |
| 74 | 74 | 'src/async_wrap.cc', | |
| 75 | 75 | 'src/base_object.cc', | |
| 76 | + 'src/builtin_info.cc', | ||
| 76 | 77 | 'src/cares_wrap.cc', | |
| 77 | 78 | 'src/cleanup_queue.cc', | |
| 78 | 79 | 'src/compile_cache.cc', | |
@@ -214,6 +215,7 @@ | |||
| 214 | 215 | 'src/base_object_types.h', | |
| 215 | 216 | 'src/blob_serializer_deserializer.h', | |
| 216 | 217 | 'src/blob_serializer_deserializer-inl.h', | |
| 218 | + "src/builtin_info.h", | ||
| 217 | 219 | 'src/callback_queue.h', | |
| 218 | 220 | 'src/callback_queue-inl.h', | |
| 219 | 221 | 'src/cleanup_queue.h', | |
@@ -1381,6 +1383,8 @@ | |||
| 1381 | 1383 | 'tools/executable_wrapper.h', | |
| 1382 | 1384 | 'src/embedded_data.h', | |
| 1383 | 1385 | 'src/embedded_data.cc', | |
| 1386 | + 'src/builtin_info.h', | ||
| 1387 | + 'src/builtin_info.cc', | ||
| 1384 | 1388 | ], | |
| 1385 | 1389 | 'conditions': [ | |
| 1386 | 1390 | [ 'node_shared_simdutf=="false"', { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -909,7 +909,7 @@ Maybe<void> InitializePrimordials(Local<Context> context, | |||
| 909 | 909 | exports, primordials, private_symbols, per_isolate_symbols}; | |
| 910 | 910 | ||
| 911 | 911 | if (builtin_loader | |
| 912 | - .CompileAndCall( | ||
| 912 | + .CompileAndCallWith( | ||
| 913 | 913 | context, *module, arraysize(arguments), arguments, nullptr) | |
| 914 | 914 | .IsEmpty()) { | |
| 915 | 915 | // Execution failed during context creation. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,48 @@ | |||
| 1 | + #include "builtin_info.h" | ||
| 2 | + | ||
| 3 | + namespace node { | ||
| 4 | + namespace builtins { | ||
| 5 | + | ||
| 6 | + BuiltinSourceType GetBuiltinSourceType(const std::string& id, | ||
| 7 | + const std::string& filename) { | ||
| 8 | + if (filename.ends_with(".mjs")) { | ||
| 9 | + return BuiltinSourceType::kSourceTextModule; | ||
| 10 | + } | ||
| 11 | + if (id.starts_with("internal/bootstrap/realm")) { | ||
| 12 | + return BuiltinSourceType::kBootstrapRealm; | ||
| 13 | + } | ||
| 14 | + if (id.starts_with("internal/bootstrap/")) { | ||
| 15 | + return BuiltinSourceType::kBootstrapScript; | ||
| 16 | + } | ||
| 17 | + if (id.starts_with("internal/per_context/")) { | ||
| 18 | + return BuiltinSourceType::kPerContextScript; | ||
| 19 | + } | ||
| 20 | + if (id.starts_with("internal/main/")) { | ||
| 21 | + return BuiltinSourceType::kMainScript; | ||
| 22 | + } | ||
| 23 | + if (id.starts_with("internal/deps/v8/tools/")) { | ||
| 24 | + return BuiltinSourceType::kSourceTextModule; | ||
| 25 | + } | ||
| 26 | + return BuiltinSourceType::kFunction; | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + std::string GetBuiltinSourceTypeName(BuiltinSourceType type) { | ||
| 30 | + switch (type) { | ||
| 31 | + case BuiltinSourceType::kBootstrapRealm: | ||
| 32 | + return "kBootstrapRealm"; | ||
| 33 | + case BuiltinSourceType::kBootstrapScript: | ||
| 34 | + return "kBootstrapScript"; | ||
| 35 | + case BuiltinSourceType::kPerContextScript: | ||
| 36 | + return "kPerContextScript"; | ||
| 37 | + case BuiltinSourceType::kMainScript: | ||
| 38 | + return "kMainScript"; | ||
| 39 | + case BuiltinSourceType::kFunction: | ||
| 40 | + return "kFunction"; | ||
| 41 | + case BuiltinSourceType::kSourceTextModule: | ||
| 42 | + return "kSourceTextModule"; | ||
| 43 | + } | ||
| 44 | + abort(); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + } // namespace builtins | ||
| 48 | + } // namespace node | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,57 @@ | |||
| 1 | + #ifndef SRC_BUILTIN_INFO_H_ | ||
| 2 | + #define SRC_BUILTIN_INFO_H_ | ||
| 3 | + | ||
| 4 | + #include <cinttypes> | ||
| 5 | + #include <string> | ||
| 6 | + #include <unordered_map> | ||
| 7 | + #include <vector> | ||
| 8 | + | ||
| 9 | + // This file must not depend on node.h or other code that depends on | ||
| 10 | + // the full Node.js implementation because it is used during the | ||
| 11 | + // compilation of the Node.js implementation itself (especially js2c). | ||
| 12 | + | ||
| 13 | + namespace node { | ||
| 14 | + namespace builtins { | ||
| 15 | + | ||
| 16 | + enum class BuiltinSourceType { | ||
| 17 | + kBootstrapRealm, // internal/bootstrap/realm | ||
| 18 | + kBootstrapScript, // internal/bootstrap/* | ||
| 19 | + kPerContextScript, // internal/per_context/* | ||
| 20 | + kMainScript, // internal/main/* | ||
| 21 | + kFunction, // others | ||
| 22 | + kSourceTextModule, // selected modules, ends with .mjs in source | ||
| 23 | + }; | ||
| 24 | + | ||
| 25 | + struct BuiltinInfo { | ||
| 26 | + // C++17 inline static | ||
| 27 | + inline static const std::unordered_map<BuiltinSourceType, | ||
| 28 | + std::vector<std::string>> | ||
| 29 | + parameter_map{ | ||
| 30 | + {BuiltinSourceType::kBootstrapRealm, | ||
| 31 | + {"process", | ||
| 32 | + "getLinkedBinding", | ||
| 33 | + "getInternalBinding", | ||
| 34 | + "primordials"}}, | ||
| 35 | + {BuiltinSourceType::kBootstrapScript, | ||
| 36 | + {"process", "require", "internalBinding", "primordials"}}, | ||
| 37 | + {BuiltinSourceType::kPerContextScript, | ||
| 38 | + {"exports", "primordials", "privateSymbols", "perIsolateSymbols"}}, | ||
| 39 | + {BuiltinSourceType::kMainScript, | ||
| 40 | + {"process", "require", "internalBinding", "primordials"}}, | ||
| 41 | + {BuiltinSourceType::kFunction, | ||
| 42 | + {"exports", | ||
| 43 | + "require", | ||
| 44 | + "module", | ||
| 45 | + "process", | ||
| 46 | + "internalBinding", | ||
| 47 | + "primordials"}}, | ||
| 48 | + }; | ||
| 49 | + }; | ||
| 50 | + | ||
| 51 | + std::string GetBuiltinSourceTypeName(BuiltinSourceType type); | ||
| 52 | + BuiltinSourceType GetBuiltinSourceType(const std::string& id, | ||
| 53 | + const std::string& filename); | ||
| 54 | + } // namespace builtins | ||
| 55 | + } // namespace node | ||
| 56 | + | ||
| 57 | + #endif // SRC_BUILTIN_INFO_H_ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,6 +57,7 @@ | |||
| 57 | 57 | V(onpskexchange_symbol, "onpskexchange") \ | |
| 58 | 58 | V(resource_symbol, "resource_symbol") \ | |
| 59 | 59 | V(trigger_async_id_symbol, "trigger_async_id_symbol") \ | |
| 60 | + V(builtin_source_text_module_hdo, "builtin_source_text_module_hdo") \ | ||
| 60 | 61 | V(source_text_module_default_hdo, "source_text_module_default_hdo") \ | |
| 61 | 62 | V(vm_context_no_contextify, "vm_context_no_contextify") \ | |
| 62 | 63 | V(vm_dynamic_import_default_internal, "vm_dynamic_import_default_internal") \ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments