Skip to content

Commit ad53094

Browse files
authored
Use globalThis with null chaining to save a little code size. NFC (#25381)
Followup to #25375
1 parent c5baec4 commit ad53094

File tree

53 files changed

+180
-180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+180
-180
lines changed

src/lib/libstrings.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ addToLibrary({
1313
#if TEXTDECODER == 2
1414
$UTF8Decoder: "new TextDecoder()",
1515
#else
16-
$UTF8Decoder: "typeof TextDecoder != 'undefined' ? new TextDecoder() : undefined",
16+
$UTF8Decoder: "globalThis.TextDecoder ? new TextDecoder() : undefined",
1717
#endif
1818

1919
$findStringEnd: (heapOrArray, idx, maxBytesToRead, ignoreNul) => {
@@ -302,7 +302,7 @@ addToLibrary({
302302
#if TEXTDECODER == 2
303303
$UTF16Decoder: "new TextDecoder('utf-16le');",
304304
#else
305-
$UTF16Decoder: "typeof TextDecoder != 'undefined' ? new TextDecoder('utf-16le') : undefined;",
305+
$UTF16Decoder: "globalThis.TextDecoder ? new TextDecoder('utf-16le') : undefined;",
306306
#endif
307307

308308
// Given a pointer 'ptr' to a null-terminated UTF16LE-encoded string in the

src/modularize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var {{{ EXPORT_NAME }}} = (() => {
1818
// When MODULARIZE this JS may be executed later,
1919
// after document.currentScript is gone, so we save it.
2020
// In EXPORT_ES6 mode we can just use 'import.meta.url'.
21-
var _scriptName = typeof document != 'undefined' ? document.currentScript?.src : undefined;
21+
var _scriptName = globalThis.document?.currentScript?.src;
2222
return async function(moduleArg = {}) {
2323
var moduleRtn;
2424

src/shell.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ var _scriptName = import.meta.url;
145145
// In MODULARIZE mode _scriptName needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
146146
// before the page load. In non-MODULARIZE modes generate it here.
147147
#if SINGLE_FILE && OUTPUT_FORMAT == 'HTML'
148-
var _scriptName = typeof document != 'undefined' ? URL.createObjectURL(new Blob([document.getElementById('mainScript').textContent], { "type" : "text/javascript" })) : undefined;
148+
var _scriptName = globalThis.document ? URL.createObjectURL(new Blob([document.getElementById('mainScript').textContent], { "type" : "text/javascript" })) : undefined;
149149
#else
150-
var _scriptName = typeof document != 'undefined' ? document.currentScript?.src : undefined;
150+
var _scriptName = globalThis.document?.currentScript?.src;
151151
#endif
152152
#endif // !MODULARIZE
153153
#elif ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_WORKER

src/shell_minimal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ var ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER && self.name?.startsWith('em-
136136
#if !MODULARIZE
137137
// In MODULARIZE mode _scriptName needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
138138
// before the page load. In non-MODULARIZE modes generate it here.
139-
var _scriptName = typeof document != 'undefined' ? document.currentScript?.src : undefined;
139+
var _scriptName = globalThis.document?.currentScript?.src;
140140
#endif
141141

142142
#if ENVIRONMENT_MAY_BE_NODE

test/codesize/audio_worklet_wasm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ var L = [], M = a => {
113113
}, Q = (a, b, c, e) => {
114114
b = P[b];
115115
P[a].connect(b.destination || b, c, e);
116-
}, P = {}, R = 0, S = "undefined" != typeof TextDecoder ? new TextDecoder : void 0, T = (a = 0) => {
116+
}, P = {}, R = 0, S = globalThis.TextDecoder ? new TextDecoder : void 0, T = (a = 0) => {
117117
for (var b = J, c = a, e = c + void 0; b[c] && !(c >= e); ) ++c;
118118
if (16 < c - a && b.buffer && S) return S.decode(b.slice(a, c));
119119
for (e = ""; a < c; ) {

test/codesize/test_codesize_cxx_ctors1.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"a.out.js": 19756,
2+
"a.out.js": 19723,
33
"a.out.js.gz": 8160,
44
"a.out.nodebug.wasm": 129509,
55
"a.out.nodebug.wasm.gz": 49243,
6-
"total": 149265,
6+
"total": 149232,
77
"total_gz": 57403,
88
"sent": [
99
"__cxa_throw",

test/codesize/test_codesize_cxx_ctors2.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 19733,
3-
"a.out.js.gz": 8148,
2+
"a.out.js": 19700,
3+
"a.out.js.gz": 8146,
44
"a.out.nodebug.wasm": 128936,
55
"a.out.nodebug.wasm.gz": 48884,
6-
"total": 148669,
7-
"total_gz": 57032,
6+
"total": 148636,
7+
"total_gz": 57030,
88
"sent": [
99
"__cxa_throw",
1010
"_abort_js",

test/codesize/test_codesize_cxx_except.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 23412,
3-
"a.out.js.gz": 9144,
2+
"a.out.js": 23379,
3+
"a.out.js.gz": 9145,
44
"a.out.nodebug.wasm": 171271,
55
"a.out.nodebug.wasm.gz": 57338,
6-
"total": 194683,
7-
"total_gz": 66482,
6+
"total": 194650,
7+
"total_gz": 66483,
88
"sent": [
99
"__cxa_begin_catch",
1010
"__cxa_end_catch",

test/codesize/test_codesize_cxx_except_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 19641,
3-
"a.out.js.gz": 8111,
2+
"a.out.js": 19608,
3+
"a.out.js.gz": 8109,
44
"a.out.nodebug.wasm": 144630,
55
"a.out.nodebug.wasm.gz": 54894,
6-
"total": 164271,
7-
"total_gz": 63005,
6+
"total": 164238,
7+
"total_gz": 63003,
88
"sent": [
99
"_abort_js",
1010
"_tzset_js",

test/codesize/test_codesize_cxx_except_wasm_legacy.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 19641,
3-
"a.out.js.gz": 8111,
2+
"a.out.js": 19608,
3+
"a.out.js.gz": 8109,
44
"a.out.nodebug.wasm": 142219,
55
"a.out.nodebug.wasm.gz": 54358,
6-
"total": 161860,
7-
"total_gz": 62469,
6+
"total": 161827,
7+
"total_gz": 62467,
88
"sent": [
99
"_abort_js",
1010
"_tzset_js",

0 commit comments

Comments
 (0)