Skip to content

Commit c168aba

Browse files
committed
Use nullish coalescing in more places in the JS library. NFC
1 parent 57aeff1 commit c168aba

Some content is hidden

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

46 files changed

+78
-74
lines changed

src/library.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,8 +1551,7 @@ addToLibrary({
15511551
var ret = getCompilerSetting(name);
15521552
if (typeof ret == 'number' || typeof ret == 'boolean') return ret;
15531553

1554-
if (!_emscripten_get_compiler_setting.cache) _emscripten_get_compiler_setting.cache = {};
1555-
var cache = _emscripten_get_compiler_setting.cache;
1554+
var cache = _emscripten_get_compiler_setting.cache ??= {};
15561555
var fullret = cache[name];
15571556
if (fullret) return fullret;
15581557
return cache[name] = stringToNewUTF8(ret);

src/library_fs.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ FS.staticInit();
6666
genericErrors: {},
6767
filesystems: null,
6868
syncFSRequests: 0, // we warn if there are multiple in flight at once
69-
69+
#if expectToReceiveOnModule('logReadFiles')
70+
readFiles: {},
71+
#endif
7072
#if ASSERTIONS
7173
ErrnoError: class extends Error {
7274
#else
@@ -1100,15 +1102,16 @@ FS.staticInit();
11001102
if (stream.stream_ops.open) {
11011103
stream.stream_ops.open(stream);
11021104
}
1105+
#if expectToReceiveOnModule('logReadFiles')
11031106
if (Module['logReadFiles'] && !(flags & {{{ cDefs.O_WRONLY}}})) {
1104-
if (!FS.readFiles) FS.readFiles = {};
11051107
if (!(path in FS.readFiles)) {
11061108
FS.readFiles[path] = 1;
11071109
#if FS_DEBUG
11081110
dbg(`FS.trackingDelegate error on read file: ${path}`);
11091111
#endif
11101112
}
11111113
}
1114+
#endif
11121115
#if FS_DEBUG
11131116
if (FS.trackingDelegate['onOpenFile']) {
11141117
FS.trackingDelegate['onOpenFile'](path, trackingFlags);
@@ -1588,7 +1591,7 @@ FS.staticInit();
15881591
createDevice(parent, name, input, output) {
15891592
var path = PATH.join2(typeof parent == 'string' ? parent : FS.getPath(parent), name);
15901593
var mode = FS_getMode(!!input, !!output);
1591-
if (!FS.createDevice.major) FS.createDevice.major = 64;
1594+
FS.createDevice.major ??= 64;
15921595
var dev = FS.makedev(FS.createDevice.major++, 0);
15931596
// Create a fake device that a set of stream ops to emulate
15941597
// the old behavior.

src/library_html5.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,27 @@ var LibraryHTML5 = {
1212
],
1313
$JSEvents: {
1414

15-
/* We do not depend on the exact initial values of falsey member fields - these
16-
fields can be populated on-demand to save code size.
17-
(but still documented here to keep track of what is supposed to be present)
18-
15+
#if USE_CLOSURE_COMPILER
1916
// pointers to structs malloc()ed to Emscripten HEAP for JS->C interop.
17+
batteryEvent: 0,
18+
gamepadEvent: 0,
2019
keyEvent: 0,
2120
mouseEvent: 0,
2221
wheelEvent: 0,
2322
uiEvent: 0,
2423
focusEvent: 0,
2524
deviceOrientationEvent: 0,
25+
orientationChangeEvent: 0,
2626
deviceMotionEvent: 0,
2727
fullscreenChangeEvent: 0,
2828
pointerlockChangeEvent: 0,
2929
visibilityChangeEvent: 0,
3030
touchEvent: 0,
31+
#endif
32+
33+
/* We do not depend on the exact initial values of falsey member fields - these
34+
fields can be populated on-demand to save code size.
35+
(but still documented here to keep track of what is supposed to be present)
3136
3237
// When we transition from fullscreen to windowed mode, we remember here the
3338
// element that was just in fullscreen mode so that we can report
@@ -253,7 +258,7 @@ var LibraryHTML5 = {
253258
#if PTHREADS
254259
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
255260
#endif
256-
if (!JSEvents.keyEvent) JSEvents.keyEvent = _malloc({{{ C_STRUCTS.EmscriptenKeyboardEvent.__size__ }}});
261+
JSEvents.keyEvent ||= _malloc({{{ C_STRUCTS.EmscriptenKeyboardEvent.__size__ }}});
257262

258263
var keyEventHandlerFunc = (e) => {
259264
#if ASSERTIONS
@@ -507,7 +512,7 @@ var LibraryHTML5 = {
507512
#if PTHREADS
508513
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
509514
#endif
510-
if (!JSEvents.mouseEvent) JSEvents.mouseEvent = _malloc({{{ C_STRUCTS.EmscriptenMouseEvent.__size__ }}});
515+
JSEvents.mouseEvent ||= _malloc({{{ C_STRUCTS.EmscriptenMouseEvent.__size__ }}});
511516
target = findEventTarget(target);
512517

513518
var mouseEventHandlerFunc = (e = event) => {
@@ -598,7 +603,7 @@ var LibraryHTML5 = {
598603
#if PTHREADS
599604
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
600605
#endif
601-
if (!JSEvents.wheelEvent) JSEvents.wheelEvent = _malloc({{{ C_STRUCTS.EmscriptenWheelEvent.__size__ }}});
606+
JSEvents.wheelEvent ||= _malloc({{{ C_STRUCTS.EmscriptenWheelEvent.__size__ }}});
602607

603608
// The DOM Level 3 events spec event 'wheel'
604609
var wheelHandlerFunc = (e = event) => {
@@ -673,7 +678,7 @@ var LibraryHTML5 = {
673678
#if PTHREADS
674679
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
675680
#endif
676-
if (!JSEvents.uiEvent) JSEvents.uiEvent = _malloc({{{ C_STRUCTS.EmscriptenUiEvent.__size__ }}});
681+
JSEvents.uiEvent ||= _malloc({{{ C_STRUCTS.EmscriptenUiEvent.__size__ }}});
677682

678683
#if DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
679684
target = findEventTarget(target);
@@ -745,7 +750,7 @@ var LibraryHTML5 = {
745750
#if PTHREADS
746751
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
747752
#endif
748-
if (!JSEvents.focusEvent) JSEvents.focusEvent = _malloc({{{ C_STRUCTS.EmscriptenFocusEvent.__size__ }}});
753+
JSEvents.focusEvent ||= _malloc({{{ C_STRUCTS.EmscriptenFocusEvent.__size__ }}});
749754

750755
var focusEventHandlerFunc = (e = event) => {
751756
var nodeName = JSEvents.getNodeNameForTarget(e.target);
@@ -809,7 +814,7 @@ var LibraryHTML5 = {
809814
#if PTHREADS
810815
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
811816
#endif
812-
if (!JSEvents.deviceOrientationEvent) JSEvents.deviceOrientationEvent = _malloc({{{ C_STRUCTS.EmscriptenDeviceOrientationEvent.__size__ }}});
817+
JSEvents.deviceOrientationEvent ||= _malloc({{{ C_STRUCTS.EmscriptenDeviceOrientationEvent.__size__ }}});
813818

814819
var deviceOrientationEventHandlerFunc = (e = event) => {
815820
fillDeviceOrientationEventData(JSEvents.deviceOrientationEvent, e, target); // TODO: Thread-safety with respect to emscripten_get_deviceorientation_status()
@@ -879,7 +884,7 @@ var LibraryHTML5 = {
879884
#if PTHREADS
880885
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
881886
#endif
882-
if (!JSEvents.deviceMotionEvent) JSEvents.deviceMotionEvent = _malloc({{{ C_STRUCTS.EmscriptenDeviceMotionEvent.__size__ }}});
887+
JSEvents.deviceMotionEvent ||= _malloc({{{ C_STRUCTS.EmscriptenDeviceMotionEvent.__size__ }}});
883888

884889
var deviceMotionEventHandlerFunc = (e = event) => {
885890
fillDeviceMotionEventData(JSEvents.deviceMotionEvent, e, target); // TODO: Thread-safety with respect to emscripten_get_devicemotion_status()
@@ -962,7 +967,7 @@ var LibraryHTML5 = {
962967
#if PTHREADS
963968
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
964969
#endif
965-
if (!JSEvents.orientationChangeEvent) JSEvents.orientationChangeEvent = _malloc({{{ C_STRUCTS.EmscriptenOrientationChangeEvent.__size__ }}});
970+
JSEvents.orientationChangeEvent ||= _malloc({{{ C_STRUCTS.EmscriptenOrientationChangeEvent.__size__ }}});
966971

967972
var orientationChangeEventHandlerFunc = (e = event) => {
968973
#if PTHREADS
@@ -1074,7 +1079,7 @@ var LibraryHTML5 = {
10741079
#if PTHREADS
10751080
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
10761081
#endif
1077-
if (!JSEvents.fullscreenChangeEvent) JSEvents.fullscreenChangeEvent = _malloc({{{ C_STRUCTS.EmscriptenFullscreenChangeEvent.__size__ }}});
1082+
JSEvents.fullscreenChangeEvent ||= _malloc({{{ C_STRUCTS.EmscriptenFullscreenChangeEvent.__size__ }}});
10781083

10791084
var fullscreenChangeEventhandlerFunc = (e = event) => {
10801085
#if PTHREADS
@@ -1200,9 +1205,9 @@ var LibraryHTML5 = {
12001205

12011206
// If we are adding padding, must choose a background color or otherwise Chrome will give the
12021207
// padding a default white color. Do it only if user has not customized their own background color.
1203-
if (!target.style.backgroundColor) target.style.backgroundColor = 'black';
1208+
target.style.backgroundColor ||= 'black';
12041209
// IE11 does the same, but requires the color to be set in the document body.
1205-
if (!document.body.style.backgroundColor) document.body.style.backgroundColor = 'black'; // IE11
1210+
document.body.style.backgroundColor ||= 'black'; // IE11
12061211
// Firefox always shows black letterboxes independent of style color.
12071212

12081213
target.style.width = cssWidth + 'px';
@@ -1421,7 +1426,7 @@ var LibraryHTML5 = {
14211426
$doRequestFullscreen: (target, strategy) => {
14221427
if (!JSEvents.fullscreenEnabled()) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
14231428
#if !DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
1424-
if (!target) target = '#canvas';
1429+
target ||= '#canvas';
14251430
#endif
14261431
target = findEventTarget(target);
14271432
if (!target) return {{{ cDefs.EMSCRIPTEN_RESULT_UNKNOWN_TARGET }}};
@@ -1604,7 +1609,7 @@ var LibraryHTML5 = {
16041609
#if PTHREADS
16051610
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
16061611
#endif
1607-
if (!JSEvents.pointerlockChangeEvent) JSEvents.pointerlockChangeEvent = _malloc({{{ C_STRUCTS.EmscriptenPointerlockChangeEvent.__size__ }}});
1612+
JSEvents.pointerlockChangeEvent ||= _malloc({{{ C_STRUCTS.EmscriptenPointerlockChangeEvent.__size__ }}});
16081613

16091614
var pointerlockChangeEventHandlerFunc = (e = event) => {
16101615
#if PTHREADS
@@ -1742,7 +1747,7 @@ var LibraryHTML5 = {
17421747
emscripten_request_pointerlock__deps: ['$JSEvents', '$requestPointerLock', '$findEventTarget'],
17431748
emscripten_request_pointerlock: (target, deferUntilInEventHandler) => {
17441749
#if !DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
1745-
if (!target) target = '#canvas';
1750+
target ||= '#canvas';
17461751
#endif
17471752
target = findEventTarget(target);
17481753
if (!target) return {{{ cDefs.EMSCRIPTEN_RESULT_UNKNOWN_TARGET }}};
@@ -1833,7 +1838,7 @@ var LibraryHTML5 = {
18331838
#if PTHREADS
18341839
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
18351840
#endif
1836-
if (!JSEvents.visibilityChangeEvent) JSEvents.visibilityChangeEvent = _malloc({{{ C_STRUCTS.EmscriptenVisibilityChangeEvent.__size__ }}});
1841+
JSEvents.visibilityChangeEvent ||= _malloc({{{ C_STRUCTS.EmscriptenVisibilityChangeEvent.__size__ }}});
18371842

18381843
var visibilityChangeEventHandlerFunc = (e = event) => {
18391844
#if PTHREADS
@@ -1887,7 +1892,7 @@ var LibraryHTML5 = {
18871892
#if PTHREADS
18881893
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
18891894
#endif
1890-
if (!JSEvents.touchEvent) JSEvents.touchEvent = _malloc({{{ C_STRUCTS.EmscriptenTouchEvent.__size__ }}});
1895+
JSEvents.touchEvent ||= _malloc({{{ C_STRUCTS.EmscriptenTouchEvent.__size__ }}});
18911896

18921897
target = findEventTarget(target);
18931898

@@ -2035,7 +2040,7 @@ var LibraryHTML5 = {
20352040
#if PTHREADS
20362041
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
20372042
#endif
2038-
if (!JSEvents.gamepadEvent) JSEvents.gamepadEvent = _malloc({{{ C_STRUCTS.EmscriptenGamepadEvent.__size__ }}});
2043+
JSEvents.gamepadEvent ||= _malloc({{{ C_STRUCTS.EmscriptenGamepadEvent.__size__ }}});
20392044

20402045
var gamepadEventHandlerFunc = (e = event) => {
20412046
#if PTHREADS
@@ -2175,7 +2180,7 @@ var LibraryHTML5 = {
21752180
#if PTHREADS
21762181
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
21772182
#endif
2178-
if (!JSEvents.batteryEvent) JSEvents.batteryEvent = _malloc({{{ C_STRUCTS.EmscriptenBatteryEvent.__size__ }}});
2183+
JSEvents.batteryEvent ||= _malloc({{{ C_STRUCTS.EmscriptenBatteryEvent.__size__ }}});
21792184

21802185
var batteryEventHandlerFunc = (e = event) => {
21812186
#if PTHREADS

src/library_html5_webgl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ var LibraryHtml5WebGL = {
382382
#endif
383383

384384
#if !DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
385-
if (!target) target = Module['canvas'];
385+
target ||= Module['canvas'];
386386
#endif
387387

388388
var webGlEventHandlerFunc = (e = event) => {

src/library_lz4.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ addToLibrary({
2222
},
2323
loadPackage(pack, preloadPlugin) {
2424
LZ4.init();
25-
var compressedData = pack['compressedData'];
26-
if (!compressedData) compressedData = LZ4.codec.compressPackage(pack['data']);
25+
var compressedData = pack['compressedData'] || LZ4.codec.compressPackage(pack['data']);
2726
assert(compressedData['cachedIndexes'].length === compressedData['cachedChunks'].length);
2827
for (var i = 0; i < compressedData['cachedIndexes'].length; i++) {
2928
compressedData['cachedIndexes'][i] = -1;

src/library_sdl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,7 @@ var LibrarySDL = {
20422042
SDL.eventHandlerContext = userdata;
20432043

20442044
// All SDLEvents take the same amount of memory
2045-
if (!SDL.eventHandlerTemp) SDL.eventHandlerTemp = _malloc({{{ C_STRUCTS.SDL_KeyboardEvent.__size__ }}});
2045+
SDL.eventHandlerTemp ||= _malloc({{{ C_STRUCTS.SDL_KeyboardEvent.__size__ }}});
20462046
},
20472047

20482048
SDL_SetColors__proxy: 'sync',

src/library_stack_trace.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ var LibraryStackTrace = {
5858
lineno = parts[3];
5959
column = parts[4];
6060
} else {
61-
parts = newFirefoxRe.exec(line);
62-
if (!parts) parts = firefoxRe.exec(line);
61+
parts = newFirefoxRe.exec(line) || firefoxRe.exec(line);
6362
if (parts && parts.length >= 4) {
6463
symbolName = parts[1];
6564
file = parts[2];

src/library_wasmfs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ FS.init();
409409
}
410410
var path = PATH.join2(parent, name);
411411
var mode = FS_getMode(!!input, !!output);
412-
if (!FS.createDevice.major) FS.createDevice.major = 64;
412+
FS.createDevice.major ??= 64;
413413
var dev = FS.makedev(FS.createDevice.major++, 0);
414414
// Create a fake device with a set of stream ops to emulate
415415
// the old API's createDevice().

src/library_webgl.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
669669
},
670670

671671
hookWebGL: function(glCtx) {
672-
if (!glCtx) glCtx = this.detectWebGLContext();
672+
glCtx ??= this.detectWebGLContext();
673673
if (!glCtx) return;
674674
if (!((typeof WebGLRenderingContext != 'undefined' && glCtx instanceof WebGLRenderingContext)
675675
|| (typeof WebGL2RenderingContext != 'undefined' && glCtx instanceof WebGL2RenderingContext))) {
@@ -4161,8 +4161,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
41614161
var mem = _malloc(length), binding = emscriptenWebGLGetBufferBinding(target);
41624162
if (!mem) return 0;
41634163

4164-
if (!GL.mappedBuffers[binding]) GL.mappedBuffers[binding] = {};
4165-
binding = GL.mappedBuffers[binding];
4164+
binding = GL.mappedBuffers[binding] ??= {};
41664165
binding.offset = offset;
41674166
binding.length = length;
41684167
binding.mem = mem;

src/library_workerfs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ addToLibrary({
1212
reader: null,
1313
mount(mount) {
1414
assert(ENVIRONMENT_IS_WORKER);
15-
if (!WORKERFS.reader) WORKERFS.reader = new FileReaderSync();
15+
WORKERFS.reader ??= new FileReaderSync();
1616
var root = WORKERFS.createNode(null, '/', WORKERFS.DIR_MODE, 0);
1717
var createdParents = {};
1818
function ensureParent(path) {

0 commit comments

Comments
 (0)