From 7cde3de28ccb3ecc60d65c51332891ac7ec0c9f4 Mon Sep 17 00:00:00 2001 From: Sergey Chernyshev Date: Thu, 25 Apr 2024 01:53:46 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/48304 Reviewed-By: Yagiz Nizipli Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: James M Snell Reviewed-By: Chengzhong Wu --- graal-nodejs/lib/internal/bootstrap/node.js | 16 +++++++++++++++ .../bootstrap/switches/is_main_thread.js | 2 -- .../lib/internal/process/pre_execution.js | 9 ++++----- .../internal/source_map/source_map_cache.js | 20 ++++++++----------- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/graal-nodejs/lib/internal/bootstrap/node.js b/graal-nodejs/lib/internal/bootstrap/node.js index 3d4d8719e51..83ffbd2c76c 100644 --- a/graal-nodejs/lib/internal/bootstrap/node.js +++ b/graal-nodejs/lib/internal/bootstrap/node.js @@ -363,6 +363,22 @@ process.emitWarning = emitWarning; // Note: only after this point are the timers effective } +{ + const { + setSourceMapsEnabled, + maybeCacheGeneratedSourceMap, + } = require('internal/source_map/source_map_cache'); + const { + setMaybeCacheGeneratedSourceMap, + } = internalBinding('errors'); + + process.setSourceMapsEnabled = setSourceMapsEnabled; + // The C++ land calls back to maybeCacheGeneratedSourceMap() + // when code is generated by user with eval() or new Function() + // to cache the source maps from the evaluated code, if any. + setMaybeCacheGeneratedSourceMap(maybeCacheGeneratedSourceMap); +} + function setupProcessObject() { const EventEmitter = require('events'); const origProcProto = ObjectGetPrototypeOf(process); diff --git a/graal-nodejs/lib/internal/bootstrap/switches/is_main_thread.js b/graal-nodejs/lib/internal/bootstrap/switches/is_main_thread.js index 212a067e3a7..2767ee12584 100644 --- a/graal-nodejs/lib/internal/bootstrap/switches/is_main_thread.js +++ b/graal-nodejs/lib/internal/bootstrap/switches/is_main_thread.js @@ -305,8 +305,6 @@ if (internalBinding('config').hasInspector) { internalBinding('wasm_web_api'); // Needed to detect whether it's on main thread. internalBinding('worker'); -// Needed to setup source maps. -require('internal/source_map/source_map_cache'); // Needed by most execution modes. require('internal/modules/run_main'); // Needed to refresh DNS configurations. diff --git a/graal-nodejs/lib/internal/process/pre_execution.js b/graal-nodejs/lib/internal/process/pre_execution.js index 61f831c84ac..cb2e53200b6 100644 --- a/graal-nodejs/lib/internal/process/pre_execution.js +++ b/graal-nodejs/lib/internal/process/pre_execution.js @@ -574,11 +574,10 @@ function initializeESMLoader() { } function initializeSourceMapsHandlers() { - const { setSourceMapsEnabled, getSourceMapsEnabled } = - require('internal/source_map/source_map_cache'); - process.setSourceMapsEnabled = setSourceMapsEnabled; - // Initialize the environment flag of source maps. - getSourceMapsEnabled(); + const { + setSourceMapsEnabled, + } = require('internal/source_map/source_map_cache'); + setSourceMapsEnabled(getOptionValue('--enable-source-maps')); } function initializeFrozenIntrinsics() { diff --git a/graal-nodejs/lib/internal/source_map/source_map_cache.js b/graal-nodejs/lib/internal/source_map/source_map_cache.js index d86f43290ab..560b51eb6d6 100644 --- a/graal-nodejs/lib/internal/source_map/source_map_cache.js +++ b/graal-nodejs/lib/internal/source_map/source_map_cache.js @@ -23,10 +23,12 @@ const { Buffer } = require('buffer'); let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => { debug = fn; }); -const { getOptionValue } = require('internal/options'); const { validateBoolean } = require('internal/validators'); -const { setMaybeCacheGeneratedSourceMap } = internalBinding('errors'); +const { + setSourceMapsEnabled: setSourceMapsNative, + setPrepareStackTraceCallback, +} = internalBinding('errors'); const { getLazy } = require('internal/util'); // Since the CJS module cache is mutable, which leads to memory leaks when @@ -49,22 +51,16 @@ const { fileURLToPath, pathToFileURL, URL } = require('internal/url'); let SourceMap; -let sourceMapsEnabled; +// This is configured with --enable-source-maps during pre-execution. +let sourceMapsEnabled = false; function getSourceMapsEnabled() { - if (sourceMapsEnabled === undefined) { - setSourceMapsEnabled(getOptionValue('--enable-source-maps')); - } return sourceMapsEnabled; } function setSourceMapsEnabled(val) { validateBoolean(val, 'val'); - const { - setSourceMapsEnabled, - setPrepareStackTraceCallback, - } = internalBinding('errors'); - setSourceMapsEnabled(val); + setSourceMapsNative(val); if (val) { const { prepareStackTrace, @@ -191,7 +187,6 @@ function maybeCacheGeneratedSourceMap(content) { debug(err); } } -setMaybeCacheGeneratedSourceMap(maybeCacheGeneratedSourceMap); function dataFromUrl(sourceURL, sourceMappingURL) { try { @@ -334,5 +329,6 @@ module.exports = { getSourceMapsEnabled, setSourceMapsEnabled, maybeCacheSourceMap, + maybeCacheGeneratedSourceMap, sourceMapCacheToObject, };