Skip to content

Commit 3734b95

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: nodejs/node#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 c537f3c commit 3734b95

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

graal-nodejs/lib/internal/bootstrap/node.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,22 @@ process.emitWarning = emitWarning;
363363
// Note: only after this point are the timers effective
364364
}
365365

366+
{
367+
const {
368+
setSourceMapsEnabled,
369+
maybeCacheGeneratedSourceMap,
370+
} = require('internal/source_map/source_map_cache');
371+
const {
372+
setMaybeCacheGeneratedSourceMap,
373+
} = internalBinding('errors');
374+
375+
process.setSourceMapsEnabled = setSourceMapsEnabled;
376+
// The C++ land calls back to maybeCacheGeneratedSourceMap()
377+
// when code is generated by user with eval() or new Function()
378+
// to cache the source maps from the evaluated code, if any.
379+
setMaybeCacheGeneratedSourceMap(maybeCacheGeneratedSourceMap);
380+
}
381+
366382
function setupProcessObject() {
367383
const EventEmitter = require('events');
368384
const origProcProto = ObjectGetPrototypeOf(process);

graal-nodejs/lib/internal/bootstrap/switches/is_main_thread.js

Lines changed: 0 additions & 2 deletions
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.

graal-nodejs/lib/internal/process/pre_execution.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,10 @@ function initializeESMLoader() {
573573
}
574574

575575
function initializeSourceMapsHandlers() {
576-
const { setSourceMapsEnabled, getSourceMapsEnabled } =
577-
require('internal/source_map/source_map_cache');
578-
process.setSourceMapsEnabled = setSourceMapsEnabled;
579-
// Initialize the environment flag of source maps.
580-
getSourceMapsEnabled();
576+
const {
577+
setSourceMapsEnabled,
578+
} = require('internal/source_map/source_map_cache');
579+
setSourceMapsEnabled(getOptionValue('--enable-source-maps'));
581580
}
582581

583582
function initializeFrozenIntrinsics() {

graal-nodejs/lib/internal/source_map/source_map_cache.js

Lines changed: 8 additions & 12 deletions
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)