Skip to content

Commit cf9ddcd

Browse files
joyeecheungtargos
authored andcommittedNov 10, 2023
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 <yagiz@nizipli.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent cc7bf1f commit cf9ddcd

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
@@ -357,6 +357,22 @@ process.emitWarning = emitWarning;
357357
// Note: only after this point are the timers effective
358358
}
359359

360+
{
361+
const {
362+
setSourceMapsEnabled,
363+
maybeCacheGeneratedSourceMap,
364+
} = require('internal/source_map/source_map_cache');
365+
const {
366+
setMaybeCacheGeneratedSourceMap,
367+
} = internalBinding('errors');
368+
369+
process.setSourceMapsEnabled = setSourceMapsEnabled;
370+
// The C++ land calls back to maybeCacheGeneratedSourceMap()
371+
// when code is generated by user with eval() or new Function()
372+
// to cache the source maps from the evaluated code, if any.
373+
setMaybeCacheGeneratedSourceMap(maybeCacheGeneratedSourceMap);
374+
}
375+
360376
function setupProcessObject() {
361377
const EventEmitter = require('events');
362378
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
@@ -564,11 +564,10 @@ function initializeESMLoader() {
564564
}
565565

566566
function initializeSourceMapsHandlers() {
567-
const { setSourceMapsEnabled, getSourceMapsEnabled } =
568-
require('internal/source_map/source_map_cache');
569-
process.setSourceMapsEnabled = setSourceMapsEnabled;
570-
// Initialize the environment flag of source maps.
571-
getSourceMapsEnabled();
567+
const {
568+
setSourceMapsEnabled,
569+
} = require('internal/source_map/source_map_cache');
570+
setSourceMapsEnabled(getOptionValue('--enable-source-maps'));
572571
}
573572

574573
function initializeFrozenIntrinsics() {

‎lib/internal/source_map/source_map_cache.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ const { Buffer } = require('buffer');
2323
let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
2424
debug = fn;
2525
});
26-
const { getOptionValue } = require('internal/options');
2726

2827
const { validateBoolean } = require('internal/validators');
29-
const { setMaybeCacheGeneratedSourceMap } = internalBinding('errors');
28+
const {
29+
setSourceMapsEnabled: setSourceMapsNative,
30+
setPrepareStackTraceCallback,
31+
} = internalBinding('errors');
3032
const { getLazy } = require('internal/util');
3133

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

5052
let SourceMap;
5153

52-
let sourceMapsEnabled;
54+
// This is configured with --enable-source-maps during pre-execution.
55+
let sourceMapsEnabled = false;
5356
function getSourceMapsEnabled() {
54-
if (sourceMapsEnabled === undefined) {
55-
setSourceMapsEnabled(getOptionValue('--enable-source-maps'));
56-
}
5757
return sourceMapsEnabled;
5858
}
5959

6060
function setSourceMapsEnabled(val) {
6161
validateBoolean(val, 'val');
6262

63-
const {
64-
setSourceMapsEnabled,
65-
setPrepareStackTraceCallback,
66-
} = internalBinding('errors');
67-
setSourceMapsEnabled(val);
63+
setSourceMapsNative(val);
6864
if (val) {
6965
const {
7066
prepareStackTrace,
@@ -191,7 +187,6 @@ function maybeCacheGeneratedSourceMap(content) {
191187
debug(err);
192188
}
193189
}
194-
setMaybeCacheGeneratedSourceMap(maybeCacheGeneratedSourceMap);
195190

196191
function dataFromUrl(sourceURL, sourceMappingURL) {
197192
try {
@@ -334,5 +329,6 @@ module.exports = {
334329
getSourceMapsEnabled,
335330
setSourceMapsEnabled,
336331
maybeCacheSourceMap,
332+
maybeCacheGeneratedSourceMap,
337333
sourceMapCacheToObject,
338334
};

0 commit comments

Comments
 (0)
Please sign in to comment.