Skip to content

Commit b1072be

Browse files
authored
Remove redundant null chaining. NFC (#25643)
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`.
1 parent 3b5d9ec commit b1072be

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

src/minimum_runtime_check.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
var TARGET_NOT_SUPPORTED = {{{ TARGET_NOT_SUPPORTED }}};
2222

23-
var currentNodeVersion = typeof process !== 'undefined' && process?.versions?.node ? humanReadableVersionToPacked(process.versions.node) : TARGET_NOT_SUPPORTED;
23+
// Note: We use a typeof check here instead of optional chaining using
24+
// globalThis because older browsers might not have globalThis defined.
25+
var currentNodeVersion = typeof process !== 'undefined' && process.versions?.node ? humanReadableVersionToPacked(process.versions.node) : TARGET_NOT_SUPPORTED;
2426
#if MIN_NODE_VERSION == TARGET_NOT_SUPPORTED
2527
if (currentNodeVersion < TARGET_NOT_SUPPORTED) {
2628
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 @@
3032
throw new Error(`This emscripten-generated code requires node v${ packedVersionToHumanReadable({{{ MIN_NODE_VERSION }}}) } (detected v${packedVersionToHumanReadable(currentNodeVersion)})`);
3133
}
3234

33-
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;
35+
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;
3436
#if MIN_SAFARI_VERSION == TARGET_NOT_SUPPORTED
3537
if (currentSafariVersion < TARGET_NOT_SUPPORTED) {
3638
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 @@
4042
throw new Error(`This emscripten-generated code requires Safari v${ packedVersionToHumanReadable({{{ MIN_SAFARI_VERSION }}}) } (detected v${currentSafariVersion})`);
4143
}
4244

43-
var currentFirefoxVersion = typeof navigator !== 'undefined' && navigator?.userAgent?.match(/Firefox\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Firefox\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED;
45+
var currentFirefoxVersion = typeof navigator !== 'undefined' && navigator.userAgent?.match(/Firefox\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Firefox\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED;
4446
#if MIN_FIREFOX_VERSION == TARGET_NOT_SUPPORTED
4547
if (currentFirefoxVersion < TARGET_NOT_SUPPORTED) {
4648
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 @@
5052
throw new Error(`This emscripten-generated code requires Firefox v{{{ MIN_FIREFOX_VERSION }}} (detected v${currentFirefoxVersion})`);
5153
}
5254

53-
var currentChromeVersion = typeof navigator !== 'undefined' && navigator?.userAgent?.match(/Chrome\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Chrome\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED;
55+
var currentChromeVersion = typeof navigator !== 'undefined' && navigator.userAgent?.match(/Chrome\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Chrome\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED;
5456
#if MIN_CHROME_VERSION == TARGET_NOT_SUPPORTED
5557
if (currentChromeVersion < TARGET_NOT_SUPPORTED) {
5658
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.`);

test/codesize/test_codesize_hello_O0.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 24334,
3-
"a.out.js.gz": 8676,
2+
"a.out.js": 24330,
3+
"a.out.js.gz": 8675,
44
"a.out.nodebug.wasm": 15119,
55
"a.out.nodebug.wasm.gz": 7444,
6-
"total": 39453,
7-
"total_gz": 16120,
6+
"total": 39449,
7+
"total_gz": 16119,
88
"sent": [
99
"fd_write"
1010
],

test/codesize/test_codesize_minimal_O0.expected.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@
1616

1717
var TARGET_NOT_SUPPORTED = 2147483647;
1818

19-
var currentNodeVersion = typeof process !== 'undefined' && process?.versions?.node ? humanReadableVersionToPacked(process.versions.node) : TARGET_NOT_SUPPORTED;
19+
// Note: We use a typeof check here instead of optional chaining using
20+
// globalThis because older browsers might not have globalThis defined.
21+
var currentNodeVersion = typeof process !== 'undefined' && process.versions?.node ? humanReadableVersionToPacked(process.versions.node) : TARGET_NOT_SUPPORTED;
2022
if (currentNodeVersion < 160000) {
2123
throw new Error(`This emscripten-generated code requires node v${ packedVersionToHumanReadable(160000) } (detected v${packedVersionToHumanReadable(currentNodeVersion)})`);
2224
}
2325

24-
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;
26+
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;
2527
if (currentSafariVersion < 150000) {
2628
throw new Error(`This emscripten-generated code requires Safari v${ packedVersionToHumanReadable(150000) } (detected v${currentSafariVersion})`);
2729
}
2830

29-
var currentFirefoxVersion = typeof navigator !== 'undefined' && navigator?.userAgent?.match(/Firefox\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Firefox\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED;
31+
var currentFirefoxVersion = typeof navigator !== 'undefined' && navigator.userAgent?.match(/Firefox\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Firefox\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED;
3032
if (currentFirefoxVersion < 79) {
3133
throw new Error(`This emscripten-generated code requires Firefox v79 (detected v${currentFirefoxVersion})`);
3234
}
3335

34-
var currentChromeVersion = typeof navigator !== 'undefined' && navigator?.userAgent?.match(/Chrome\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Chrome\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED;
36+
var currentChromeVersion = typeof navigator !== 'undefined' && navigator.userAgent?.match(/Chrome\/(\d+(?:\.\d+)?)/) ? parseFloat(navigator.userAgent.match(/Chrome\/(\d+(?:\.\d+)?)/)[1]) : TARGET_NOT_SUPPORTED;
3537
if (currentChromeVersion < 85) {
3638
throw new Error(`This emscripten-generated code requires Chrome v85 (detected v${currentChromeVersion})`);
3739
}

test/codesize/test_codesize_minimal_O0.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 19504,
3-
"a.out.js.gz": 6976,
2+
"a.out.js": 19500,
3+
"a.out.js.gz": 6975,
44
"a.out.nodebug.wasm": 1136,
55
"a.out.nodebug.wasm.gz": 659,
6-
"total": 20640,
7-
"total_gz": 7635,
6+
"total": 20636,
7+
"total_gz": 7634,
88
"sent": [],
99
"imports": [],
1010
"exports": [
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"hello_world.js": 56854,
3-
"hello_world.js.gz": 17626,
2+
"hello_world.js": 56997,
3+
"hello_world.js.gz": 17686,
44
"hello_world.wasm": 15119,
55
"hello_world.wasm.gz": 7444,
66
"no_asserts.js": 26632,
77
"no_asserts.js.gz": 8884,
88
"no_asserts.wasm": 12219,
99
"no_asserts.wasm.gz": 6005,
10-
"strict.js": 54869,
11-
"strict.js.gz": 16968,
10+
"strict.js": 55012,
11+
"strict.js.gz": 17030,
1212
"strict.wasm": 15119,
1313
"strict.wasm.gz": 7442,
14-
"total": 180812,
15-
"total_gz": 64369
14+
"total": 181098,
15+
"total_gz": 64491
1616
}

0 commit comments

Comments
 (0)