From 70ed25cbefa6c1840a7cead9d2d3cfb7241803ba Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 24 Oct 2025 10:27:11 -0700 Subject: [PATCH] Remove redundant null chaining. NFC This null chaining operator is not only redundant (because it happens only after a typeof check), but simply doesn't work. One cannot do `foo?.bar` if `foo` does not exist. One would need to instead do `globalThis?.foo?.bar`. --- src/minimum_runtime_check.js | 10 ++++++---- test/codesize/test_codesize_hello_O0.json | 8 ++++---- test/codesize/test_codesize_minimal_O0.expected.js | 10 ++++++---- test/codesize/test_codesize_minimal_O0.json | 8 ++++---- test/codesize/test_unoptimized_code_size.json | 12 ++++++------ 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/minimum_runtime_check.js b/src/minimum_runtime_check.js index 31061d00c74d3..313b8cab532cf 100644 --- a/src/minimum_runtime_check.js +++ b/src/minimum_runtime_check.js @@ -20,7 +20,9 @@ var TARGET_NOT_SUPPORTED = {{{ TARGET_NOT_SUPPORTED }}}; - var currentNodeVersion = typeof process !== 'undefined' && process?.versions?.node ? humanReadableVersionToPacked(process.versions.node) : TARGET_NOT_SUPPORTED; + // Note: We use a typeof check here instead of optional chaining using + // globalThis because older browsers might not have globalThis defined. + var currentNodeVersion = typeof process !== 'undefined' && process.versions?.node ? humanReadableVersionToPacked(process.versions.node) : TARGET_NOT_SUPPORTED; #if MIN_NODE_VERSION == TARGET_NOT_SUPPORTED if (currentNodeVersion < TARGET_NOT_SUPPORTED) { throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)'); @@ -30,7 +32,7 @@ throw new Error(`This emscripten-generated code requires node v${ packedVersionToHumanReadable({{{ MIN_NODE_VERSION }}}) } (detected v${packedVersionToHumanReadable(currentNodeVersion)})`); } - var currentSafariVersion = typeof navigator !== 'undefined' && navigator?.userAgent?.includes("Safari/") && navigator.userAgent.match(/Version\/(\d+\.?\d*\.?\d*)/) ? humanReadableVersionToPacked(navigator.userAgent.match(/Version\/(\d+\.?\d*\.?\d*)/)[1]) : TARGET_NOT_SUPPORTED; + var currentSafariVersion = typeof navigator !== 'undefined' && navigator.userAgent?.includes("Safari/") && navigator.userAgent.match(/Version\/(\d+\.?\d*\.?\d*)/) ? humanReadableVersionToPacked(navigator.userAgent.match(/Version\/(\d+\.?\d*\.?\d*)/)[1]) : TARGET_NOT_SUPPORTED; #if MIN_SAFARI_VERSION == TARGET_NOT_SUPPORTED if (currentSafariVersion < TARGET_NOT_SUPPORTED) { throw new Error(`This page was compiled without support for Safari browser. Pass -sMIN_SAFARI_VERSION=${currentSafariVersion} or lower to enable support for this browser.`); @@ -40,7 +42,7 @@ throw new Error(`This emscripten-generated code requires Safari v${ packedVersionToHumanReadable({{{ MIN_SAFARI_VERSION }}}) } (detected v${currentSafariVersion})`); } - var currentFirefoxVersion = typeof navigator !== 'undefined' && navigator?.userAgent?.match(/Firefox\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Firefox\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED; + var currentFirefoxVersion = typeof navigator !== 'undefined' && navigator.userAgent?.match(/Firefox\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Firefox\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED; #if MIN_FIREFOX_VERSION == TARGET_NOT_SUPPORTED if (currentFirefoxVersion < TARGET_NOT_SUPPORTED) { throw new Error(`This page was compiled without support for Firefox browser. Pass -sMIN_FIREFOX_VERSION=${currentFirefoxVersion} or lower to enable support for this browser.`); @@ -50,7 +52,7 @@ throw new Error(`This emscripten-generated code requires Firefox v{{{ MIN_FIREFOX_VERSION }}} (detected v${currentFirefoxVersion})`); } - var currentChromeVersion = typeof navigator !== 'undefined' && navigator?.userAgent?.match(/Chrome\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Chrome\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED; + var currentChromeVersion = typeof navigator !== 'undefined' && navigator.userAgent?.match(/Chrome\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Chrome\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED; #if MIN_CHROME_VERSION == TARGET_NOT_SUPPORTED if (currentChromeVersion < TARGET_NOT_SUPPORTED) { throw new Error(`This page was compiled without support for Chrome browser. Pass -sMIN_CHROME_VERSION=${currentChromeVersion} or lower to enable support for this browser.`); diff --git a/test/codesize/test_codesize_hello_O0.json b/test/codesize/test_codesize_hello_O0.json index 2ec27991f5115..cf2cc428a80a0 100644 --- a/test/codesize/test_codesize_hello_O0.json +++ b/test/codesize/test_codesize_hello_O0.json @@ -1,10 +1,10 @@ { - "a.out.js": 24334, - "a.out.js.gz": 8676, + "a.out.js": 24330, + "a.out.js.gz": 8675, "a.out.nodebug.wasm": 15119, "a.out.nodebug.wasm.gz": 7444, - "total": 39453, - "total_gz": 16120, + "total": 39449, + "total_gz": 16119, "sent": [ "fd_write" ], diff --git a/test/codesize/test_codesize_minimal_O0.expected.js b/test/codesize/test_codesize_minimal_O0.expected.js index 16279522e87cb..4aee550e060a5 100644 --- a/test/codesize/test_codesize_minimal_O0.expected.js +++ b/test/codesize/test_codesize_minimal_O0.expected.js @@ -16,22 +16,24 @@ var TARGET_NOT_SUPPORTED = 2147483647; - var currentNodeVersion = typeof process !== 'undefined' && process?.versions?.node ? humanReadableVersionToPacked(process.versions.node) : TARGET_NOT_SUPPORTED; + // Note: We use a typeof check here instead of optional chaining using + // globalThis because older browsers might not have globalThis defined. + var currentNodeVersion = typeof process !== 'undefined' && process.versions?.node ? humanReadableVersionToPacked(process.versions.node) : TARGET_NOT_SUPPORTED; if (currentNodeVersion < 160000) { throw new Error(`This emscripten-generated code requires node v${ packedVersionToHumanReadable(160000) } (detected v${packedVersionToHumanReadable(currentNodeVersion)})`); } - var currentSafariVersion = typeof navigator !== 'undefined' && navigator?.userAgent?.includes("Safari/") && navigator.userAgent.match(/Version\/(\d+\.?\d*\.?\d*)/) ? humanReadableVersionToPacked(navigator.userAgent.match(/Version\/(\d+\.?\d*\.?\d*)/)[1]) : TARGET_NOT_SUPPORTED; + var currentSafariVersion = typeof navigator !== 'undefined' && navigator.userAgent?.includes("Safari/") && navigator.userAgent.match(/Version\/(\d+\.?\d*\.?\d*)/) ? humanReadableVersionToPacked(navigator.userAgent.match(/Version\/(\d+\.?\d*\.?\d*)/)[1]) : TARGET_NOT_SUPPORTED; if (currentSafariVersion < 150000) { throw new Error(`This emscripten-generated code requires Safari v${ packedVersionToHumanReadable(150000) } (detected v${currentSafariVersion})`); } - var currentFirefoxVersion = typeof navigator !== 'undefined' && navigator?.userAgent?.match(/Firefox\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Firefox\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED; + var currentFirefoxVersion = typeof navigator !== 'undefined' && navigator.userAgent?.match(/Firefox\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Firefox\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED; if (currentFirefoxVersion < 79) { throw new Error(`This emscripten-generated code requires Firefox v79 (detected v${currentFirefoxVersion})`); } - var currentChromeVersion = typeof navigator !== 'undefined' && navigator?.userAgent?.match(/Chrome\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Chrome\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED; + var currentChromeVersion = typeof navigator !== 'undefined' && navigator.userAgent?.match(/Chrome\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Chrome\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED; if (currentChromeVersion < 85) { throw new Error(`This emscripten-generated code requires Chrome v85 (detected v${currentChromeVersion})`); } diff --git a/test/codesize/test_codesize_minimal_O0.json b/test/codesize/test_codesize_minimal_O0.json index 323b19e853832..f5419c8c593eb 100644 --- a/test/codesize/test_codesize_minimal_O0.json +++ b/test/codesize/test_codesize_minimal_O0.json @@ -1,10 +1,10 @@ { - "a.out.js": 19504, - "a.out.js.gz": 6976, + "a.out.js": 19500, + "a.out.js.gz": 6975, "a.out.nodebug.wasm": 1136, "a.out.nodebug.wasm.gz": 659, - "total": 20640, - "total_gz": 7635, + "total": 20636, + "total_gz": 7634, "sent": [], "imports": [], "exports": [ diff --git a/test/codesize/test_unoptimized_code_size.json b/test/codesize/test_unoptimized_code_size.json index 64aaf7fad2324..419757ce1192b 100644 --- a/test/codesize/test_unoptimized_code_size.json +++ b/test/codesize/test_unoptimized_code_size.json @@ -1,16 +1,16 @@ { - "hello_world.js": 56854, - "hello_world.js.gz": 17626, + "hello_world.js": 56997, + "hello_world.js.gz": 17686, "hello_world.wasm": 15119, "hello_world.wasm.gz": 7444, "no_asserts.js": 26632, "no_asserts.js.gz": 8884, "no_asserts.wasm": 12219, "no_asserts.wasm.gz": 6005, - "strict.js": 54869, - "strict.js.gz": 16968, + "strict.js": 55012, + "strict.js.gz": 17030, "strict.wasm": 15119, "strict.wasm.gz": 7442, - "total": 180812, - "total_gz": 64369 + "total": 181098, + "total_gz": 64491 }