Skip to content

Commit b62683d

Browse files
committed
Leverage coalescing operators. NFC
Note that this bumps minimum Chrome & Firefox versions, as well as JS version from ES2020 to ES2021. I believe this is not blocked on #11984 as it doesn't require Closure to emit runtime helpers. This is mostly automated with ast-grep, bunch of custom rules, and a few manual fixups. Best viewed with "ignore whitespace changes" (https://github.com/emscripten-core/emscripten/pull/20531/files) due to indentation changes and due to first commit being #20530. Care was taken to ensure that false-y conditions still use `||` where it might matter and only null-ish conditions use `??`.
1 parent 06561ee commit b62683d

File tree

96 files changed

+321
-394
lines changed

Some content is hidden

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

96 files changed

+321
-394
lines changed

src/Fetch.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function fetchDeleteCachedData(db, fetch, onsuccess, onerror) {
8484

8585
var fetch_attr = fetch + {{{ C_STRUCTS.emscripten_fetch_t.__attributes }}};
8686
var path = HEAPU32[fetch_attr + {{{ C_STRUCTS.emscripten_fetch_attr_t.destinationPath }}} >> 2];
87-
if (!path) path = HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
87+
path ||= HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
8888
var pathStr = UTF8ToString(path);
8989

9090
try {
@@ -133,7 +133,7 @@ function fetchLoadCachedData(db, fetch, onsuccess, onerror) {
133133

134134
var fetch_attr = fetch + {{{ C_STRUCTS.emscripten_fetch_t.__attributes }}};
135135
var path = HEAPU32[fetch_attr + {{{ C_STRUCTS.emscripten_fetch_attr_t.destinationPath }}} >> 2];
136-
if (!path) path = HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
136+
path ||= HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
137137
var pathStr = UTF8ToString(path);
138138

139139
try {
@@ -198,7 +198,7 @@ function fetchCacheData(/** @type {IDBDatabase} */ db, fetch, data, onsuccess, o
198198

199199
var fetch_attr = fetch + {{{ C_STRUCTS.emscripten_fetch_t.__attributes }}};
200200
var destinationPath = HEAPU32[fetch_attr + {{{ C_STRUCTS.emscripten_fetch_attr_t.destinationPath }}} >> 2];
201-
if (!destinationPath) destinationPath = HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
201+
destinationPath ||= HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
202202
var destinationPathStr = UTF8ToString(destinationPath);
203203

204204
try {
@@ -248,7 +248,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
248248

249249
var fetch_attr = fetch + {{{ C_STRUCTS.emscripten_fetch_t.__attributes }}};
250250
var requestMethod = UTF8ToString(fetch_attr + {{{ C_STRUCTS.emscripten_fetch_attr_t.requestMethod }}});
251-
if (!requestMethod) requestMethod = 'GET';
251+
requestMethod ||= 'GET';
252252
var timeoutMsecs = {{{ makeGetValue('fetch_attr', C_STRUCTS.emscripten_fetch_attr_t.timeoutMSecs, 'u32') }}};
253253
var userName = {{{ makeGetValue('fetch_attr', C_STRUCTS.emscripten_fetch_attr_t.userName, '*') }}};
254254
var password = {{{ makeGetValue('fetch_attr', C_STRUCTS.emscripten_fetch_attr_t.password, '*') }}};
@@ -354,12 +354,12 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
354354
#if FETCH_DEBUG
355355
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" succeeded with status ${xhr.status}`);
356356
#endif
357-
if (onsuccess) onsuccess(fetch, xhr, e);
357+
onsuccess?.(fetch, xhr, e);
358358
} else {
359359
#if FETCH_DEBUG
360360
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" failed with status ${xhr.status}`);
361361
#endif
362-
if (onerror) onerror(fetch, xhr, e);
362+
onerror?.(fetch, xhr, e);
363363
}
364364
};
365365
xhr.onerror = (e) => {
@@ -371,7 +371,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
371371
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" finished with error, readyState ${xhr.readyState} and status ${xhr.status}`);
372372
#endif
373373
saveResponseAndStatus();
374-
if (onerror) onerror(fetch, xhr, e);
374+
onerror?.(fetch, xhr, e);
375375
};
376376
xhr.ontimeout = (e) => {
377377
// check if xhr was aborted by user and don't try to call back
@@ -381,7 +381,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
381381
#if FETCH_DEBUG
382382
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" timed out, readyState ${xhr.readyState} and status ${xhr.status}`);
383383
#endif
384-
if (onerror) onerror(fetch, xhr, e);
384+
onerror?.(fetch, xhr, e);
385385
};
386386
xhr.onprogress = (e) => {
387387
// check if xhr was aborted by user and don't try to call back
@@ -410,7 +410,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
410410
if (xhr.readyState >= 3 && xhr.status === 0 && e.loaded > 0) xhr.status = 200;
411411
HEAPU16[fetch + {{{ C_STRUCTS.emscripten_fetch_t.status }}} >> 1] = xhr.status;
412412
if (xhr.statusText) stringToUTF8(xhr.statusText, fetch + {{{ C_STRUCTS.emscripten_fetch_t.statusText }}}, 64);
413-
if (onprogress) onprogress(fetch, xhr, e);
413+
onprogress?.(fetch, xhr, e);
414414
if (ptr) {
415415
_free(ptr);
416416
}
@@ -425,7 +425,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
425425
if (xhr.readyState >= 2) {
426426
HEAPU16[fetch + {{{ C_STRUCTS.emscripten_fetch_t.status }}} >> 1] = xhr.status;
427427
}
428-
if (onreadystatechange) onreadystatechange(fetch, xhr, e);
428+
onreadystatechange?.(fetch, xhr, e);
429429
};
430430
#if FETCH_DEBUG
431431
dbg(`fetch: xhr.send(data=${data})`);
@@ -436,7 +436,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
436436
#if FETCH_DEBUG
437437
dbg(`fetch: xhr failed with exception: ${e}`);
438438
#endif
439-
if (onerror) onerror(fetch, xhr, e);
439+
onerror?.(fetch, xhr, e);
440440
}
441441
}
442442

@@ -468,14 +468,14 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
468468
{{{ runtimeKeepalivePop() }}}
469469
doCallback(() => {
470470
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
471-
else if (successcb) successcb(fetch);
471+
else successcb?.(fetch);
472472
});
473473
};
474474

475475
var reportProgress = (fetch, xhr, e) => {
476476
doCallback(() => {
477477
if (onprogress) {{{ makeDynCall('vp', 'onprogress') }}}(fetch);
478-
else if (progresscb) progresscb(fetch);
478+
else progresscb?.(fetch);
479479
});
480480
};
481481

@@ -486,7 +486,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
486486
{{{ runtimeKeepalivePop() }}}
487487
doCallback(() => {
488488
if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(fetch);
489-
else if (errorcb) errorcb(fetch);
489+
else errorcb?.(fetch);
490490
});
491491
};
492492

@@ -496,7 +496,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
496496
#endif
497497
doCallback(() => {
498498
if (onreadystatechange) {{{ makeDynCall('vp', 'onreadystatechange') }}}(fetch);
499-
else if (readystatechangecb) readystatechangecb(fetch);
499+
else readystatechangecb?.(fetch);
500500
});
501501
};
502502

@@ -519,7 +519,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
519519
{{{ runtimeKeepalivePop() }}}
520520
doCallback(() => {
521521
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
522-
else if (successcb) successcb(fetch);
522+
else successcb?.(fetch);
523523
});
524524
};
525525
var storeError = (fetch, xhr, e) => {
@@ -529,7 +529,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
529529
{{{ runtimeKeepalivePop() }}}
530530
doCallback(() => {
531531
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
532-
else if (successcb) successcb(fetch);
532+
else successcb?.(fetch);
533533
});
534534
};
535535
fetchCacheData(Fetch.dbInstance, fetch, xhr.response, storeSuccess, storeError);

src/cpuprofiler.js

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -148,37 +148,35 @@ var emscriptenCpuProfiler = {
148148
createSection: function createSection(number, name, drawColor, traceable) {
149149
while (this.sections.length <= number) this.sections.push(null); // Keep an array structure.
150150
var sect = this.sections[number];
151-
if (!sect) {
152-
sect = {
153-
count: 0,
154-
name,
155-
startTick: 0,
156-
accumulatedTimeInsideMainLoop: 0,
157-
accumulatedTimeOutsideMainLoop: 0,
158-
frametimesInsideMainLoop: [],
159-
frametimesOutsideMainLoop: [],
160-
drawColor,
161-
traceable,
162-
accumulatedFrameTimeInsideMainLoop: function(startX, numSamples) {
163-
var total = 0;
164-
numSamples = Math.min(numSamples, this.frametimesInsideMainLoop.length);
165-
for (var i = 0; i < numSamples; ++i) {
166-
var x = (startX + i) % this.frametimesInsideMainLoop.length;
167-
if (this.frametimesInsideMainLoop[x]) total += this.frametimesInsideMainLoop[x];
168-
}
169-
return total;
170-
},
171-
accumulatedFrameTimeOutsideMainLoop: function(startX, numSamples) {
172-
var total = 0;
173-
numSamples = Math.min(numSamples, this.frametimesInsideMainLoop.length);
174-
for (var i = 0; i < numSamples; ++i) {
175-
var x = (startX + i) % this.frametimesInsideMainLoop.length;
176-
if (this.frametimesOutsideMainLoop[x]) total += this.frametimesOutsideMainLoop[x];
177-
}
178-
return total;
151+
sect ||= {
152+
count: 0,
153+
name,
154+
startTick: 0,
155+
accumulatedTimeInsideMainLoop: 0,
156+
accumulatedTimeOutsideMainLoop: 0,
157+
frametimesInsideMainLoop: [],
158+
frametimesOutsideMainLoop: [],
159+
drawColor,
160+
traceable,
161+
accumulatedFrameTimeInsideMainLoop: function(startX, numSamples) {
162+
var total = 0;
163+
numSamples = Math.min(numSamples, this.frametimesInsideMainLoop.length);
164+
for (var i = 0; i < numSamples; ++i) {
165+
var x = (startX + i) % this.frametimesInsideMainLoop.length;
166+
if (this.frametimesInsideMainLoop[x]) total += this.frametimesInsideMainLoop[x];
179167
}
180-
};
181-
}
168+
return total;
169+
},
170+
accumulatedFrameTimeOutsideMainLoop: function(startX, numSamples) {
171+
var total = 0;
172+
numSamples = Math.min(numSamples, this.frametimesInsideMainLoop.length);
173+
for (var i = 0; i < numSamples; ++i) {
174+
var x = (startX + i) % this.frametimesInsideMainLoop.length;
175+
if (this.frametimesOutsideMainLoop[x]) total += this.frametimesOutsideMainLoop[x];
176+
}
177+
return total;
178+
}
179+
};
182180
sect.name = name;
183181
this.sections[number] = sect;
184182
},
@@ -368,7 +366,7 @@ var emscriptenCpuProfiler = {
368366
fpsOverlay.style = 'position: fixed; font-weight: bold; padding: 3px; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; cursor: pointer;';
369367
fpsOverlay.onclick = () => {
370368
var view = document.getElementById('cpuprofiler_canvas');
371-
if (view) view.scrollIntoView();
369+
view?.scrollIntoView();
372370
};
373371
fpsOverlay.oncontextmenu = (e) => e.preventDefault();
374372
document.body.appendChild(fpsOverlay);
@@ -532,14 +530,14 @@ var emscriptenCpuProfiler = {
532530
},
533531

534532
detectWebGLContext: function() {
535-
if (Module['canvas'] && Module['canvas'].GLctxObject && Module['canvas'].GLctxObject.GLctx) return Module['canvas'].GLctxObject.GLctx;
533+
if (Module['canvas']?.GLctxObject?.GLctx) return Module['canvas'].GLctxObject.GLctx;
536534
else if (typeof GLctx != 'undefined') return GLctx;
537535
else if (Module.ctx) return Module.ctx;
538536
return null;
539537
},
540538

541539
toggleHookWebGL: function(glCtx) {
542-
if (!glCtx) glCtx = this.detectWebGLContext();
540+
glCtx ||= this.detectWebGLContext();
543541
if (this.hookedWebGLContexts.includes(glCtx)) this.unhookWebGL(glCtx);
544542
else this.hookWebGL(glCtx);
545543
},
@@ -563,7 +561,7 @@ var emscriptenCpuProfiler = {
563561
},
564562

565563
unhookWebGL: function(glCtx) {
566-
if (!glCtx) glCtx = this.detectWebGLContext();
564+
glCtx ||= this.detectWebGLContext();
567565
if (!glCtx.cpuprofilerAlreadyHooked) return;
568566
glCtx.cpuprofilerAlreadyHooked = false;
569567
this.hookedWebGLContexts.splice(this.hookedWebGLContexts.indexOf(glCtx), 1);
@@ -601,7 +599,7 @@ var emscriptenCpuProfiler = {
601599
},
602600

603601
hookWebGL: function(glCtx) {
604-
if (!glCtx) glCtx = this.detectWebGLContext();
602+
glCtx ||= this.detectWebGLContext();
605603
if (!glCtx) return;
606604
if (!((typeof WebGLRenderingContext != 'undefined' && glCtx instanceof WebGLRenderingContext)
607605
|| (typeof WebGL2RenderingContext != 'undefined' && glCtx instanceof WebGL2RenderingContext))) {

src/embind/embind.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,9 +1371,7 @@ var LibraryEmbind = {
13711371
return ptr;
13721372
},
13731373
destructor(ptr) {
1374-
if (this.rawDestructor) {
1375-
this.rawDestructor(ptr);
1376-
}
1374+
this.rawDestructor?.(ptr);
13771375
},
13781376
'argPackAdvance': GenericWireTypeSize,
13791377
'readValueFromPointer': readPointer,
@@ -1807,12 +1805,8 @@ var LibraryEmbind = {
18071805
rawDestructor) => {
18081806
name = readLatin1String(name);
18091807
getActualType = embind__requireFunction(getActualTypeSignature, getActualType);
1810-
if (upcast) {
1811-
upcast = embind__requireFunction(upcastSignature, upcast);
1812-
}
1813-
if (downcast) {
1814-
downcast = embind__requireFunction(downcastSignature, downcast);
1815-
}
1808+
upcast &&= embind__requireFunction(upcastSignature, upcast);
1809+
downcast &&= embind__requireFunction(downcastSignature, downcast);
18161810
rawDestructor = embind__requireFunction(destructorSignature, rawDestructor);
18171811
var legalFunctionName = makeLegalFunctionName(name);
18181812

@@ -1867,9 +1861,7 @@ var LibraryEmbind = {
18671861

18681862
if (registeredClass.baseClass) {
18691863
// Keep track of class hierarchy. Used to allow sub-classes to inherit class functions.
1870-
if (registeredClass.baseClass.__derivedClasses === undefined) {
1871-
registeredClass.baseClass.__derivedClasses = [];
1872-
}
1864+
registeredClass.baseClass.__derivedClasses ??= [];
18731865

18741866
registeredClass.baseClass.__derivedClasses.push(registeredClass);
18751867
}

src/embind/emval.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,7 @@ var LibraryEmVal = {
336336
}
337337
var rv = kind === /* CONSTRUCTOR */ 1 ? reflectConstruct(func, argN) : func.apply(obj, argN);
338338
for (var i = 0; i < argCount; ++i) {
339-
if (types[i].deleteObject) {
340-
types[i].deleteObject(argN[i]);
341-
}
339+
types[i].deleteObject?.(argN[i]);
342340
}
343341
return emval_returnValue(retType, destructorsRef, rv);
344342
};

src/headless.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ var window = {
8383
eventListeners: {},
8484
addEventListener(id, func) {
8585
var listeners = this.eventListeners[id];
86-
if (!listeners) {
87-
listeners = this.eventListeners[id] = [];
88-
}
86+
listeners ||= this.eventListeners[id] = [];
8987
listeners.push(func);
9088
},
9189
removeEventListener(id, func) {
@@ -100,9 +98,7 @@ var window = {
10098
},
10199
callEventListeners(id) {
102100
var listeners = this.eventListeners[id];
103-
if (listeners) {
104-
listeners.forEach((listener) => listener());
105-
}
101+
listeners?.forEach((listener) => listener());
106102
},
107103
URL: {
108104
createObjectURL(x) {
@@ -161,17 +157,15 @@ var document = {
161157
},
162158
elements: {},
163159
querySelector(id) {
164-
if (!document.elements[id]) {
165-
document.elements[id] = {
166-
classList: {
167-
add() {},
168-
remove() {},
169-
},
170-
eventListeners: {},
171-
addEventListener: document.addEventListener,
172-
removeEventListener: document.removeEventListener,
173-
callEventListeners: document.callEventListeners,
174-
};
160+
document.elements[id] ||= {
161+
classList: {
162+
add() {},
163+
remove() {},
164+
},
165+
eventListeners: {},
166+
addEventListener: document.addEventListener,
167+
removeEventListener: document.removeEventListener,
168+
callEventListeners: document.callEventListeners,
175169
};
176170
return document.elements[id];
177171
},
@@ -214,7 +208,7 @@ var XMLHttpRequest = function() {
214208
} else {
215209
window.setTimeout(() => {
216210
this.doSend();
217-
if (this.onload) this.onload();
211+
this.onload?.();
218212
}, 0);
219213
}
220214
},
@@ -239,7 +233,7 @@ var Image = () => {
239233
this.complete = true;
240234
this.width = 64;
241235
this.height = 64;
242-
if (this.onload) this.onload();
236+
this.onload?.();
243237
});
244238
};
245239
var Worker = (workerPath) => {
@@ -252,7 +246,7 @@ var Worker = (workerPath) => {
252246

253247
function duplicateJSON(json) {
254248
function handleTypedArrays(key, value) {
255-
if (value && value.toString && value.toString().substring(0, 8) == '[object ' && value.length && value.byteLength) {
249+
if (value?.toString && value.toString().substring(0, 8) == '[object ' && value.length && value.byteLength) {
256250
return Array.prototype.slice.call(value);
257251
}
258252
return value;
@@ -293,6 +287,4 @@ if (typeof console == 'undefined') {
293287
}
294288

295289
// additional setup
296-
if (!Module['canvas']) {
297-
Module['canvas'] = document.getElementById('canvas');
298-
}
290+
Module['canvas'] ||= document.getElementById('canvas');

0 commit comments

Comments
 (0)