Skip to content

Commit 834eb88

Browse files
authored
More use of globalThis to save on code size. NFC (#25417)
Followup to #25381
1 parent 967008f commit 834eb88

File tree

72 files changed

+248
-251
lines changed

Some content is hidden

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

72 files changed

+248
-251
lines changed

src/cpuprofiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ function cpuprofiler_add_hooks() {
756756
emscriptenCpuProfiler.initialize();
757757
}
758758

759-
if (typeof document != 'undefined') {
759+
if (globalThis.document) {
760760
emscriptenCpuProfiler.initialize();
761761
}
762762

src/deterministic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Date.now = deterministicNow;
2020
// Setting performance.now to deterministicNow doesn't work so we instead
2121
// use a helper function in parseTools (getPerformanceNow()) to call it
2222
// directly.
23-
// if (typeof performance == 'object') performance.now = Date.now;
23+
// if (globalThis.performance) performance.now = Date.now;
2424

2525
Module['thisProgram'] = 'thisProgram'; // for consistency between different builds than between runs of the same build
2626

src/emrun_postjs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* emcc is run with `--emrun`
88
*/
99

10-
if (typeof window == "object" && (typeof ENVIRONMENT_IS_PTHREAD == 'undefined' || !ENVIRONMENT_IS_PTHREAD)) {
10+
if (globalThis.window && (typeof ENVIRONMENT_IS_PTHREAD == 'undefined' || !ENVIRONMENT_IS_PTHREAD)) {
1111
var emrun_register_handlers = () => {
1212
// When C code exit()s, we may still have remaining stdout and stderr
1313
// messages in flight. In that case, we can't close the browser until all
@@ -89,7 +89,7 @@ if (typeof window == "object" && (typeof ENVIRONMENT_IS_PTHREAD == 'undefined' |
8989
http.send(data); // XXX this does not work in workers, for some odd reason (issue #2681)
9090
};
9191

92-
if (typeof document != 'undefined') {
92+
if (globalThis.document) {
9393
emrun_register_handlers();
9494
}
9595
}

src/emrun_prejs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
// Route URL GET parameters to argc+argv
11-
if (typeof window == 'object') {
11+
if (globalThis.window) {
1212
Module['arguments'] = window.location.search.slice(1).trim().split('&');
1313
for (let i = 0; i < Module['arguments'].length; ++i) {
1414
Module['arguments'][i] = decodeURI(Module['arguments'][i]);

src/lib/libaddfunction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ addToLibrary({
9595
// "WebAssembly.Function" constructor.
9696
// Otherwise, construct a minimal wasm module importing the JS function and
9797
// re-exporting it.
98-
if (typeof WebAssembly.Function == "function") {
98+
if (WebAssembly.Function) {
9999
return new WebAssembly.Function(sigToWasmTypes(sig), func);
100100
}
101101
#endif

src/lib/libatomic.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ addToLibrary({
2222
// https://github.com/tc39/proposal-atomics-wait-async/blob/master/PROPOSAL.md
2323
// This polyfill performs polling with setTimeout() to observe a change in the
2424
// target memory location.
25-
$polyfillWaitAsync__postset: `if (!Atomics.waitAsync || (typeof navigator != 'undefined' && navigator.userAgent && Number((navigator.userAgent.match(/Chrom(e|ium)\\/([0-9]+)\\./)||[])[2]) < 91)) {
25+
$polyfillWaitAsync__postset: `if (!Atomics.waitAsync || (globalThis.navigator?.userAgent && Number((navigator.userAgent.match(/Chrom(e|ium)\\/([0-9]+)\\./)||[])[2]) < 91)) {
2626
let __Atomics_waitAsyncAddresses = [/*[i32a, index, value, maxWaitMilliseconds, promiseResolve]*/];
2727
function __Atomics_pollWaitAsyncAddresses() {
2828
let now = performance.now();
@@ -148,7 +148,7 @@ addToLibrary({
148148
return numCancelled;
149149
},
150150

151-
emscripten_has_threading_support: () => typeof SharedArrayBuffer != 'undefined',
151+
emscripten_has_threading_support: () => !!globalThis.SharedArrayBuffer,
152152

153153
emscripten_num_logical_cores: () =>
154154
#if ENVIRONMENT_MAY_BE_NODE

src/lib/libcore.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,7 @@ addToLibrary({
13441344
// (https://github.com/WebAudio/web-audio-api/issues/2527), so if building
13451345
// with
13461346
// Audio Worklets enabled, do a dynamic check for its presence.
1347-
if (typeof performance != 'undefined' && {{{ getPerformanceNow() }}}) {
1347+
if (globalThis.performance && {{{ getPerformanceNow() }}}) {
13481348
#if PTHREADS
13491349
_emscripten_get_now = () => performance.timeOrigin + {{{ getPerformanceNow() }}}();
13501350
#else
@@ -1369,7 +1369,7 @@ addToLibrary({
13691369
}
13701370
#endif
13711371
#if AUDIO_WORKLET // https://github.com/WebAudio/web-audio-api/issues/2413
1372-
if (typeof performance == 'object' && performance && typeof performance['now'] == 'function') {
1372+
if (globalThis.performance?.now == 'function') {
13731373
return 1000; // microseconds (1/1000 of a millisecond)
13741374
}
13751375
return 1000*1000; // milliseconds
@@ -1383,7 +1383,7 @@ addToLibrary({
13831383
// implementation is not :(
13841384
$nowIsMonotonic__internal: true,
13851385
#if AUDIO_WORKLET // // https://github.com/WebAudio/web-audio-api/issues/2413
1386-
$nowIsMonotonic: `((typeof performance == 'object' && performance && typeof performance['now'] == 'function'));`,
1386+
$nowIsMonotonic: `!!globalThis.performance?.now;`,
13871387
#else
13881388
// Modern environment where performance.now() is supported
13891389
$nowIsMonotonic: 1,
@@ -2345,7 +2345,7 @@ addToLibrary({
23452345
assert(id, 'addRunDependency requires an ID')
23462346
assert(!runDependencyTracking[id]);
23472347
runDependencyTracking[id] = 1;
2348-
if (runDependencyWatcher === null && typeof setInterval != 'undefined') {
2348+
if (runDependencyWatcher === null && globalThis.setInterval) {
23492349
// Check for missing dependencies every few seconds
23502350
runDependencyWatcher = setInterval(() => {
23512351
if (ABORT) {

src/lib/libembind.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ var LibraryEmbind = {
13691369
#endif
13701370
],
13711371
$attachFinalizer: (handle) => {
1372-
if ('undefined' === typeof FinalizationRegistry) {
1372+
if (!globalThis.FinalizationRegistry) {
13731373
attachFinalizer = (handle) => handle;
13741374
return handle;
13751375
}

src/lib/libeventloop.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ LibraryJSEventLoop = {
5454

5555
$emSetImmediate__deps: ['$setImmediateWrapped', '$clearImmediateWrapped', '$emClearImmediate'],
5656
$emSetImmediate__postset: `
57-
if (typeof setImmediate != "undefined") {
57+
if (globalThis.setImmediate) {
5858
emSetImmediate = setImmediateWrapped;
5959
emClearImmediate = clearImmediateWrapped;
60-
} else if (typeof addEventListener == "function") {
60+
} else if (globalThis.addEventListener) {
6161
var __setImmediate_id_counter = 0;
6262
var __setImmediate_queue = [];
6363
var __setImmediate_message_id = "_si";
@@ -298,7 +298,7 @@ LibraryJSEventLoop = {
298298
},
299299

300300
requestAnimationFrame(func) {
301-
if (typeof requestAnimationFrame == 'function') {
301+
if (globalThis.requestAnimationFrame) {
302302
requestAnimationFrame(func);
303303
} else {
304304
MainLoop.fakeRequestAnimationFrame(func);
@@ -340,8 +340,10 @@ LibraryJSEventLoop = {
340340
};
341341
MainLoop.method = 'rAF';
342342
} else if (mode == {{{ cDefs.EM_TIMING_SETIMMEDIATE}}}) {
343-
if (typeof MainLoop.setImmediate == 'undefined') {
344-
if (typeof setImmediate == 'undefined') {
343+
if (!MainLoop.setImmediate) {
344+
if (globalThis.setImmediate) {
345+
MainLoop.setImmediate = setImmediate;
346+
} else {
345347
// Emulate setImmediate. (note: not a complete polyfill, we don't emulate clearImmediate() to keep code size to minimum, since not needed)
346348
var setImmediates = [];
347349
var emscriptenMainLoopMessageId = 'setimmediate';
@@ -363,8 +365,6 @@ LibraryJSEventLoop = {
363365
postMessage({target: emscriptenMainLoopMessageId}); // In --proxy-to-worker, route the message via proxyClient.js
364366
} else postMessage(emscriptenMainLoopMessageId, "*"); // On the main thread, can just send the message to itself.
365367
});
366-
} else {
367-
MainLoop.setImmediate = setImmediate;
368368
}
369369
}
370370
MainLoop.scheduler = function MainLoop_scheduler_setImmediate() {

src/lib/libfs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ FS.staticInit();`;
17051705
#if FS_DEBUG
17061706
dbg(`forceLoadFile: ${obj.url}`)
17071707
#endif
1708-
if (typeof XMLHttpRequest != 'undefined') {
1708+
if (globalThis.XMLHttpRequest) {
17091709
abort("Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.");
17101710
} else { // Command-line.
17111711
try {
@@ -1824,7 +1824,7 @@ FS.staticInit();`;
18241824
}
18251825
}
18261826

1827-
if (typeof XMLHttpRequest != 'undefined') {
1827+
if (globalThis.XMLHttpRequest) {
18281828
if (!ENVIRONMENT_IS_WORKER) abort('Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc');
18291829
var lazyArray = new LazyUint8Array();
18301830
var properties = { isDevice: false, contents: lazyArray };

0 commit comments

Comments
 (0)