Skip to content

Commit 4ff55ec

Browse files
committed
bootstrap: simplify initialization of source map handlers
- Move the initialization of process.setSourceMapsEnabled and the maybeCacheGeneratedSourceMap callback to bootstrap/node.js so they are included in the snapshot. - Simplify the handling of --enable-source-maps by explicitly calling setSourceMapsEnabled() during pre-execution. PR-URL: #48304 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent d102f18 commit 4ff55ec

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

lib/internal/bootstrap/node.js

+16
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,22 @@ process.emitWarning = emitWarning;
335335
// Note: only after this point are the timers effective
336336
}
337337

338+
{
339+
const {
340+
setSourceMapsEnabled,
341+
maybeCacheGeneratedSourceMap,
342+
} = require('internal/source_map/source_map_cache');
343+
const {
344+
setMaybeCacheGeneratedSourceMap,
345+
} = internalBinding('errors');
346+
347+
process.setSourceMapsEnabled = setSourceMapsEnabled;
348+
// The C++ land calls back to maybeCacheGeneratedSourceMap()
349+
// when code is generated by user with eval() or new Function()
350+
// to cache the source maps from the evaluated code, if any.
351+
setMaybeCacheGeneratedSourceMap(maybeCacheGeneratedSourceMap);
352+
}
353+
338354
function setupProcessObject() {
339355
const EventEmitter = require('events');
340356
const origProcProto = ObjectGetPrototypeOf(process);

lib/internal/bootstrap/switches/is_main_thread.js

-2
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,6 @@ if (internalBinding('config').hasInspector) {
305305
internalBinding('wasm_web_api');
306306
// Needed to detect whether it's on main thread.
307307
internalBinding('worker');
308-
// Needed to setup source maps.
309-
require('internal/source_map/source_map_cache');
310308
// Needed by most execution modes.
311309
require('internal/modules/run_main');
312310
// Needed to refresh DNS configurations.

lib/internal/process/pre_execution.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -618,11 +618,10 @@ function initializeESMLoader(isLoaderWorker) {
618618
}
619619

620620
function initializeSourceMapsHandlers() {
621-
const { setSourceMapsEnabled, getSourceMapsEnabled } =
622-
require('internal/source_map/source_map_cache');
623-
process.setSourceMapsEnabled = setSourceMapsEnabled;
624-
// Initialize the environment flag of source maps.
625-
getSourceMapsEnabled();
621+
const {
622+
setSourceMapsEnabled,
623+
} = require('internal/source_map/source_map_cache');
624+
setSourceMapsEnabled(getOptionValue('--enable-source-maps'));
626625
}
627626

628627
function initializeFrozenIntrinsics() {

lib/internal/source_map/source_map_cache.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ const { Buffer } = require('buffer');
1515
let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
1616
debug = fn;
1717
});
18-
const { getOptionValue } = require('internal/options');
1918

2019
const { validateBoolean } = require('internal/validators');
21-
const { setMaybeCacheGeneratedSourceMap } = internalBinding('errors');
20+
const {
21+
setSourceMapsEnabled: setSourceMapsNative,
22+
setPrepareStackTraceCallback,
23+
} = internalBinding('errors');
2224
const { getLazy } = require('internal/util');
2325

2426
// Since the CJS module cache is mutable, which leads to memory leaks when
@@ -41,22 +43,16 @@ const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
4143

4244
let SourceMap;
4345

44-
let sourceMapsEnabled;
46+
// This is configured with --enable-source-maps during pre-execution.
47+
let sourceMapsEnabled = false;
4548
function getSourceMapsEnabled() {
46-
if (sourceMapsEnabled === undefined) {
47-
setSourceMapsEnabled(getOptionValue('--enable-source-maps'));
48-
}
4949
return sourceMapsEnabled;
5050
}
5151

5252
function setSourceMapsEnabled(val) {
5353
validateBoolean(val, 'val');
5454

55-
const {
56-
setSourceMapsEnabled,
57-
setPrepareStackTraceCallback,
58-
} = internalBinding('errors');
59-
setSourceMapsEnabled(val);
55+
setSourceMapsNative(val);
6056
if (val) {
6157
const {
6258
prepareStackTrace,
@@ -186,7 +182,6 @@ function maybeCacheGeneratedSourceMap(content) {
186182
debug(err);
187183
}
188184
}
189-
setMaybeCacheGeneratedSourceMap(maybeCacheGeneratedSourceMap);
190185

191186
function dataFromUrl(sourceURL, sourceMappingURL) {
192187
try {
@@ -333,5 +328,6 @@ module.exports = {
333328
getSourceMapsEnabled,
334329
setSourceMapsEnabled,
335330
maybeCacheSourceMap,
331+
maybeCacheGeneratedSourceMap,
336332
sourceMapCacheToObject,
337333
};

0 commit comments

Comments
 (0)