Skip to content

Commit

Permalink
Use nullish coalescing in more places in the JS library. NFC (#22601)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Sep 23, 2024
1 parent d286f12 commit 6e41736
Show file tree
Hide file tree
Showing 46 changed files with 78 additions and 74 deletions.
3 changes: 1 addition & 2 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -1551,8 +1551,7 @@ addToLibrary({
var ret = getCompilerSetting(name);
if (typeof ret == 'number' || typeof ret == 'boolean') return ret;
if (!_emscripten_get_compiler_setting.cache) _emscripten_get_compiler_setting.cache = {};
var cache = _emscripten_get_compiler_setting.cache;
var cache = _emscripten_get_compiler_setting.cache ??= {};
var fullret = cache[name];
if (fullret) return fullret;
return cache[name] = stringToNewUTF8(ret);
Expand Down
9 changes: 6 additions & 3 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ FS.staticInit();
genericErrors: {},
filesystems: null,
syncFSRequests: 0, // we warn if there are multiple in flight at once

#if expectToReceiveOnModule('logReadFiles')
readFiles: {},
#endif
#if ASSERTIONS
ErrnoError: class extends Error {
#else
Expand Down Expand Up @@ -1100,15 +1102,16 @@ FS.staticInit();
if (stream.stream_ops.open) {
stream.stream_ops.open(stream);
}
#if expectToReceiveOnModule('logReadFiles')
if (Module['logReadFiles'] && !(flags & {{{ cDefs.O_WRONLY}}})) {
if (!FS.readFiles) FS.readFiles = {};
if (!(path in FS.readFiles)) {
FS.readFiles[path] = 1;
#if FS_DEBUG
dbg(`FS.trackingDelegate error on read file: ${path}`);
#endif
}
}
#endif
#if FS_DEBUG
if (FS.trackingDelegate['onOpenFile']) {
FS.trackingDelegate['onOpenFile'](path, trackingFlags);
Expand Down Expand Up @@ -1588,7 +1591,7 @@ FS.staticInit();
createDevice(parent, name, input, output) {
var path = PATH.join2(typeof parent == 'string' ? parent : FS.getPath(parent), name);
var mode = FS_getMode(!!input, !!output);
if (!FS.createDevice.major) FS.createDevice.major = 64;
FS.createDevice.major ??= 64;
var dev = FS.makedev(FS.createDevice.major++, 0);
// Create a fake device that a set of stream ops to emulate
// the old behavior.
Expand Down
49 changes: 27 additions & 22 deletions src/library_html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@ var LibraryHTML5 = {
],
$JSEvents: {

/* We do not depend on the exact initial values of falsey member fields - these
fields can be populated on-demand to save code size.
(but still documented here to keep track of what is supposed to be present)
#if USE_CLOSURE_COMPILER
// pointers to structs malloc()ed to Emscripten HEAP for JS->C interop.
batteryEvent: 0,
gamepadEvent: 0,
keyEvent: 0,
mouseEvent: 0,
wheelEvent: 0,
uiEvent: 0,
focusEvent: 0,
deviceOrientationEvent: 0,
orientationChangeEvent: 0,
deviceMotionEvent: 0,
fullscreenChangeEvent: 0,
pointerlockChangeEvent: 0,
visibilityChangeEvent: 0,
touchEvent: 0,
#endif

/* We do not depend on the exact initial values of falsey member fields - these
fields can be populated on-demand to save code size.
(but still documented here to keep track of what is supposed to be present)
// When we transition from fullscreen to windowed mode, we remember here the
// element that was just in fullscreen mode so that we can report
Expand Down Expand Up @@ -253,7 +258,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.keyEvent) JSEvents.keyEvent = _malloc({{{ C_STRUCTS.EmscriptenKeyboardEvent.__size__ }}});
JSEvents.keyEvent ||= _malloc({{{ C_STRUCTS.EmscriptenKeyboardEvent.__size__ }}});
var keyEventHandlerFunc = (e) => {
#if ASSERTIONS
Expand Down Expand Up @@ -507,7 +512,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.mouseEvent) JSEvents.mouseEvent = _malloc({{{ C_STRUCTS.EmscriptenMouseEvent.__size__ }}});
JSEvents.mouseEvent ||= _malloc({{{ C_STRUCTS.EmscriptenMouseEvent.__size__ }}});
target = findEventTarget(target);

var mouseEventHandlerFunc = (e = event) => {
Expand Down Expand Up @@ -598,7 +603,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.wheelEvent) JSEvents.wheelEvent = _malloc({{{ C_STRUCTS.EmscriptenWheelEvent.__size__ }}});
JSEvents.wheelEvent ||= _malloc({{{ C_STRUCTS.EmscriptenWheelEvent.__size__ }}});

// The DOM Level 3 events spec event 'wheel'
var wheelHandlerFunc = (e = event) => {
Expand Down Expand Up @@ -673,7 +678,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.uiEvent) JSEvents.uiEvent = _malloc({{{ C_STRUCTS.EmscriptenUiEvent.__size__ }}});
JSEvents.uiEvent ||= _malloc({{{ C_STRUCTS.EmscriptenUiEvent.__size__ }}});

#if DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
target = findEventTarget(target);
Expand Down Expand Up @@ -745,7 +750,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.focusEvent) JSEvents.focusEvent = _malloc({{{ C_STRUCTS.EmscriptenFocusEvent.__size__ }}});
JSEvents.focusEvent ||= _malloc({{{ C_STRUCTS.EmscriptenFocusEvent.__size__ }}});

var focusEventHandlerFunc = (e = event) => {
var nodeName = JSEvents.getNodeNameForTarget(e.target);
Expand Down Expand Up @@ -809,7 +814,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.deviceOrientationEvent) JSEvents.deviceOrientationEvent = _malloc({{{ C_STRUCTS.EmscriptenDeviceOrientationEvent.__size__ }}});
JSEvents.deviceOrientationEvent ||= _malloc({{{ C_STRUCTS.EmscriptenDeviceOrientationEvent.__size__ }}});

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

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

var orientationChangeEventHandlerFunc = (e = event) => {
#if PTHREADS
Expand Down Expand Up @@ -1074,7 +1079,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.fullscreenChangeEvent) JSEvents.fullscreenChangeEvent = _malloc({{{ C_STRUCTS.EmscriptenFullscreenChangeEvent.__size__ }}});
JSEvents.fullscreenChangeEvent ||= _malloc({{{ C_STRUCTS.EmscriptenFullscreenChangeEvent.__size__ }}});

var fullscreenChangeEventhandlerFunc = (e = event) => {
#if PTHREADS
Expand Down Expand Up @@ -1200,9 +1205,9 @@ var LibraryHTML5 = {

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

target.style.width = cssWidth + 'px';
Expand Down Expand Up @@ -1421,7 +1426,7 @@ var LibraryHTML5 = {
$doRequestFullscreen: (target, strategy) => {
if (!JSEvents.fullscreenEnabled()) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
#if !DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
if (!target) target = '#canvas';
target ||= '#canvas';
#endif
target = findEventTarget(target);
if (!target) return {{{ cDefs.EMSCRIPTEN_RESULT_UNKNOWN_TARGET }}};
Expand Down Expand Up @@ -1604,7 +1609,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.pointerlockChangeEvent) JSEvents.pointerlockChangeEvent = _malloc({{{ C_STRUCTS.EmscriptenPointerlockChangeEvent.__size__ }}});
JSEvents.pointerlockChangeEvent ||= _malloc({{{ C_STRUCTS.EmscriptenPointerlockChangeEvent.__size__ }}});

var pointerlockChangeEventHandlerFunc = (e = event) => {
#if PTHREADS
Expand Down Expand Up @@ -1742,7 +1747,7 @@ var LibraryHTML5 = {
emscripten_request_pointerlock__deps: ['$JSEvents', '$requestPointerLock', '$findEventTarget'],
emscripten_request_pointerlock: (target, deferUntilInEventHandler) => {
#if !DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
if (!target) target = '#canvas';
target ||= '#canvas';
#endif
target = findEventTarget(target);
if (!target) return {{{ cDefs.EMSCRIPTEN_RESULT_UNKNOWN_TARGET }}};
Expand Down Expand Up @@ -1833,7 +1838,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.visibilityChangeEvent) JSEvents.visibilityChangeEvent = _malloc({{{ C_STRUCTS.EmscriptenVisibilityChangeEvent.__size__ }}});
JSEvents.visibilityChangeEvent ||= _malloc({{{ C_STRUCTS.EmscriptenVisibilityChangeEvent.__size__ }}});

var visibilityChangeEventHandlerFunc = (e = event) => {
#if PTHREADS
Expand Down Expand Up @@ -1887,7 +1892,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.touchEvent) JSEvents.touchEvent = _malloc({{{ C_STRUCTS.EmscriptenTouchEvent.__size__ }}});
JSEvents.touchEvent ||= _malloc({{{ C_STRUCTS.EmscriptenTouchEvent.__size__ }}});

target = findEventTarget(target);

Expand Down Expand Up @@ -2035,7 +2040,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.gamepadEvent) JSEvents.gamepadEvent = _malloc({{{ C_STRUCTS.EmscriptenGamepadEvent.__size__ }}});
JSEvents.gamepadEvent ||= _malloc({{{ C_STRUCTS.EmscriptenGamepadEvent.__size__ }}});

var gamepadEventHandlerFunc = (e = event) => {
#if PTHREADS
Expand Down Expand Up @@ -2175,7 +2180,7 @@ var LibraryHTML5 = {
#if PTHREADS
targetThread = JSEvents.getTargetThreadForEventCallback(targetThread);
#endif
if (!JSEvents.batteryEvent) JSEvents.batteryEvent = _malloc({{{ C_STRUCTS.EmscriptenBatteryEvent.__size__ }}});
JSEvents.batteryEvent ||= _malloc({{{ C_STRUCTS.EmscriptenBatteryEvent.__size__ }}});

var batteryEventHandlerFunc = (e = event) => {
#if PTHREADS
Expand Down
2 changes: 1 addition & 1 deletion src/library_html5_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ var LibraryHtml5WebGL = {
#endif
#if !DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
if (!target) target = Module['canvas'];
target ||= Module['canvas'];
#endif
var webGlEventHandlerFunc = (e = event) => {
Expand Down
3 changes: 1 addition & 2 deletions src/library_lz4.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ addToLibrary({
},
loadPackage(pack, preloadPlugin) {
LZ4.init();
var compressedData = pack['compressedData'];
if (!compressedData) compressedData = LZ4.codec.compressPackage(pack['data']);
var compressedData = pack['compressedData'] || LZ4.codec.compressPackage(pack['data']);
assert(compressedData['cachedIndexes'].length === compressedData['cachedChunks'].length);
for (var i = 0; i < compressedData['cachedIndexes'].length; i++) {
compressedData['cachedIndexes'][i] = -1;
Expand Down
2 changes: 1 addition & 1 deletion src/library_sdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,7 @@ var LibrarySDL = {
SDL.eventHandlerContext = userdata;

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

SDL_SetColors__proxy: 'sync',
Expand Down
3 changes: 1 addition & 2 deletions src/library_stack_trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ var LibraryStackTrace = {
lineno = parts[3];
column = parts[4];
} else {
parts = newFirefoxRe.exec(line);
if (!parts) parts = firefoxRe.exec(line);
parts = newFirefoxRe.exec(line) || firefoxRe.exec(line);
if (parts && parts.length >= 4) {
symbolName = parts[1];
file = parts[2];
Expand Down
2 changes: 1 addition & 1 deletion src/library_wasmfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ FS.init();
}
var path = PATH.join2(parent, name);
var mode = FS_getMode(!!input, !!output);
if (!FS.createDevice.major) FS.createDevice.major = 64;
FS.createDevice.major ??= 64;
var dev = FS.makedev(FS.createDevice.major++, 0);
// Create a fake device with a set of stream ops to emulate
// the old API's createDevice().
Expand Down
5 changes: 2 additions & 3 deletions src/library_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
},

hookWebGL: function(glCtx) {
if (!glCtx) glCtx = this.detectWebGLContext();
glCtx ??= this.detectWebGLContext();
if (!glCtx) return;
if (!((typeof WebGLRenderingContext != 'undefined' && glCtx instanceof WebGLRenderingContext)
|| (typeof WebGL2RenderingContext != 'undefined' && glCtx instanceof WebGL2RenderingContext))) {
Expand Down Expand Up @@ -4161,8 +4161,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
var mem = _malloc(length), binding = emscriptenWebGLGetBufferBinding(target);
if (!mem) return 0;
if (!GL.mappedBuffers[binding]) GL.mappedBuffers[binding] = {};
binding = GL.mappedBuffers[binding];
binding = GL.mappedBuffers[binding] ??= {};
binding.offset = offset;
binding.length = length;
binding.mem = mem;
Expand Down
2 changes: 1 addition & 1 deletion src/library_workerfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ addToLibrary({
reader: null,
mount(mount) {
assert(ENVIRONMENT_IS_WORKER);
if (!WORKERFS.reader) WORKERFS.reader = new FileReaderSync();
WORKERFS.reader ??= new FileReaderSync();
var root = WORKERFS.createNode(null, '/', WORKERFS.DIR_MODE, 0);
var createdParents = {};
function ensureParent(path) {
Expand Down
2 changes: 1 addition & 1 deletion src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ function createWasm() {
}
#endif

if (!wasmBinaryFile) wasmBinaryFile = findWasmBinary();
wasmBinaryFile ??= findWasmBinary();

#if WASM_ASYNC_COMPILATION
#if RUNTIME_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/shell.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
return canvas;
})(),
setStatus: (text) => {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
Module.setStatus.last ??= { time: Date.now(), text: '' };
if (text === Module.setStatus.last.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
Expand Down
2 changes: 1 addition & 1 deletion src/shell_minimal.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
return canvas;
})(),
setStatus: (text) => {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
Module.setStatus.last ??= { time: Date.now(), text: '' };
if (text === Module.setStatus.last.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8455
8451
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20831
20829
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8438
8435
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20799
20797
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9476
9473
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24675
24673
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8415
8411
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20725
20723
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8415
8411
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20725
20723
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8347
8342
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20408
20407
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9480
9475
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24675
24673
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8455
8451
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20831
20829
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7581
7584
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18732
18730
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O0.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7983
7985
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2788
2790
Loading

0 comments on commit 6e41736

Please sign in to comment.