Skip to content

Commit a3d00e5

Browse files
committed
Leverage coalescing operators. NFC
Note that this bumps minimum Chrome & Firefox versions. 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. Care was taken to ensure that false-y conditions still use `||` where it might matter and only null-ish conditions use `??`.
1 parent 710266a commit a3d00e5

Some content is hidden

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

49 files changed

+260
-330
lines changed

src/Fetch.js

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

8383
var fetch_attr = fetch + {{{ C_STRUCTS.emscripten_fetch_t.__attributes }}};
8484
var path = HEAPU32[fetch_attr + {{{ C_STRUCTS.emscripten_fetch_attr_t.destinationPath }}} >> 2];
85-
if (!path) path = HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
85+
path ||= HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
8686
var pathStr = UTF8ToString(path);
8787

8888
try {
@@ -131,7 +131,7 @@ function fetchLoadCachedData(db, fetch, onsuccess, onerror) {
131131

132132
var fetch_attr = fetch + {{{ C_STRUCTS.emscripten_fetch_t.__attributes }}};
133133
var path = HEAPU32[fetch_attr + {{{ C_STRUCTS.emscripten_fetch_attr_t.destinationPath }}} >> 2];
134-
if (!path) path = HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
134+
path ||= HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
135135
var pathStr = UTF8ToString(path);
136136

137137
try {
@@ -196,7 +196,7 @@ function fetchCacheData(/** @type {IDBDatabase} */ db, fetch, data, onsuccess, o
196196

197197
var fetch_attr = fetch + {{{ C_STRUCTS.emscripten_fetch_t.__attributes }}};
198198
var destinationPath = HEAPU32[fetch_attr + {{{ C_STRUCTS.emscripten_fetch_attr_t.destinationPath }}} >> 2];
199-
if (!destinationPath) destinationPath = HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
199+
destinationPath ||= HEAPU32[fetch + {{{ C_STRUCTS.emscripten_fetch_t.url }}} >> 2];
200200
var destinationPathStr = UTF8ToString(destinationPath);
201201

202202
try {
@@ -246,7 +246,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
246246

247247
var fetch_attr = fetch + {{{ C_STRUCTS.emscripten_fetch_t.__attributes }}};
248248
var requestMethod = UTF8ToString(fetch_attr + {{{ C_STRUCTS.emscripten_fetch_attr_t.requestMethod }}});
249-
if (!requestMethod) requestMethod = 'GET';
249+
requestMethod ||= 'GET';
250250
var timeoutMsecs = {{{ makeGetValue('fetch_attr', C_STRUCTS.emscripten_fetch_attr_t.timeoutMSecs, 'u32') }}};
251251
var userName = {{{ makeGetValue('fetch_attr', C_STRUCTS.emscripten_fetch_attr_t.userName, '*') }}};
252252
var password = {{{ makeGetValue('fetch_attr', C_STRUCTS.emscripten_fetch_attr_t.password, '*') }}};
@@ -352,12 +352,12 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
352352
#if FETCH_DEBUG
353353
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" succeeded with status ${xhr.status}`);
354354
#endif
355-
if (onsuccess) onsuccess(fetch, xhr, e);
355+
onsuccess?.(fetch, xhr, e);
356356
} else {
357357
#if FETCH_DEBUG
358358
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" failed with status ${xhr.status}`);
359359
#endif
360-
if (onerror) onerror(fetch, xhr, e);
360+
onerror?.(fetch, xhr, e);
361361
}
362362
};
363363
xhr.onerror = (e) => {
@@ -369,7 +369,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
369369
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" finished with error, readyState ${xhr.readyState} and status ${xhr.status}`);
370370
#endif
371371
saveResponseAndStatus();
372-
if (onerror) onerror(fetch, xhr, e);
372+
onerror?.(fetch, xhr, e);
373373
};
374374
xhr.ontimeout = (e) => {
375375
// check if xhr was aborted by user and don't try to call back
@@ -379,7 +379,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
379379
#if FETCH_DEBUG
380380
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" timed out, readyState ${xhr.readyState} and status ${xhr.status}`);
381381
#endif
382-
if (onerror) onerror(fetch, xhr, e);
382+
onerror?.(fetch, xhr, e);
383383
};
384384
xhr.onprogress = (e) => {
385385
// check if xhr was aborted by user and don't try to call back
@@ -408,7 +408,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
408408
if (xhr.readyState >= 3 && xhr.status === 0 && e.loaded > 0) xhr.status = 200;
409409
HEAPU16[fetch + {{{ C_STRUCTS.emscripten_fetch_t.status }}} >> 1] = xhr.status;
410410
if (xhr.statusText) stringToUTF8(xhr.statusText, fetch + {{{ C_STRUCTS.emscripten_fetch_t.statusText }}}, 64);
411-
if (onprogress) onprogress(fetch, xhr, e);
411+
onprogress?.(fetch, xhr, e);
412412
if (ptr) {
413413
_free(ptr);
414414
}
@@ -423,7 +423,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
423423
if (xhr.readyState >= 2) {
424424
HEAPU16[fetch + {{{ C_STRUCTS.emscripten_fetch_t.status }}} >> 1] = xhr.status;
425425
}
426-
if (onreadystatechange) onreadystatechange(fetch, xhr, e);
426+
onreadystatechange?.(fetch, xhr, e);
427427
};
428428
#if FETCH_DEBUG
429429
dbg(`fetch: xhr.send(data=${data})`);
@@ -434,7 +434,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
434434
#if FETCH_DEBUG
435435
dbg(`fetch: xhr failed with exception: ${e}`);
436436
#endif
437-
if (onerror) onerror(fetch, xhr, e);
437+
onerror?.(fetch, xhr, e);
438438
}
439439
}
440440

@@ -466,14 +466,14 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
466466
{{{ runtimeKeepalivePop() }}}
467467
doCallback(() => {
468468
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
469-
else if (successcb) successcb(fetch);
469+
else successcb?.(fetch);
470470
});
471471
};
472472

473473
var reportProgress = (fetch, xhr, e) => {
474474
doCallback(() => {
475475
if (onprogress) {{{ makeDynCall('vp', 'onprogress') }}}(fetch);
476-
else if (progresscb) progresscb(fetch);
476+
else progresscb?.(fetch);
477477
});
478478
};
479479

@@ -484,7 +484,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
484484
{{{ runtimeKeepalivePop() }}}
485485
doCallback(() => {
486486
if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(fetch);
487-
else if (errorcb) errorcb(fetch);
487+
else errorcb?.(fetch);
488488
});
489489
};
490490

@@ -494,7 +494,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
494494
#endif
495495
doCallback(() => {
496496
if (onreadystatechange) {{{ makeDynCall('vp', 'onreadystatechange') }}}(fetch);
497-
else if (readystatechangecb) readystatechangecb(fetch);
497+
else readystatechangecb?.(fetch);
498498
});
499499
};
500500

@@ -517,7 +517,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
517517
{{{ runtimeKeepalivePop() }}}
518518
doCallback(() => {
519519
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
520-
else if (successcb) successcb(fetch);
520+
else successcb?.(fetch);
521521
});
522522
};
523523
var storeError = (fetch, xhr, e) => {
@@ -527,7 +527,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
527527
{{{ runtimeKeepalivePop() }}}
528528
doCallback(() => {
529529
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
530-
else if (successcb) successcb(fetch);
530+
else successcb?.(fetch);
531531
});
532532
};
533533
fetchCacheData(Fetch.dbInstance, fetch, xhr.response, storeSuccess, storeError);

src/cpuprofiler.js

Lines changed: 32 additions & 34 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
},
@@ -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
@@ -1370,9 +1370,7 @@ var LibraryEmbind = {
13701370
return ptr;
13711371
},
13721372
destructor(ptr) {
1373-
if (this.rawDestructor) {
1374-
this.rawDestructor(ptr);
1375-
}
1373+
this.rawDestructor?.(ptr);
13761374
},
13771375
'argPackAdvance': GenericWireTypeSize,
13781376
'readValueFromPointer': readPointer,
@@ -1806,12 +1804,8 @@ var LibraryEmbind = {
18061804
rawDestructor) => {
18071805
name = readLatin1String(name);
18081806
getActualType = embind__requireFunction(getActualTypeSignature, getActualType);
1809-
if (upcast) {
1810-
upcast = embind__requireFunction(upcastSignature, upcast);
1811-
}
1812-
if (downcast) {
1813-
downcast = embind__requireFunction(downcastSignature, downcast);
1814-
}
1807+
upcast &&= embind__requireFunction(upcastSignature, upcast);
1808+
downcast &&= embind__requireFunction(downcastSignature, downcast);
18151809
rawDestructor = embind__requireFunction(destructorSignature, rawDestructor);
18161810
var legalFunctionName = makeLegalFunctionName(name);
18171811

@@ -1866,9 +1860,7 @@ var LibraryEmbind = {
18661860

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

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

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
};

0 commit comments

Comments
 (0)