Skip to content

Commit

Permalink
Use nullish coalescing in more places in the JS library. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Sep 20, 2024
1 parent 57aeff1 commit 3a4cea5
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 36 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
4 changes: 2 additions & 2 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ FS.staticInit();
stream.stream_ops.open(stream);
}
if (Module['logReadFiles'] && !(flags & {{{ cDefs.O_WRONLY}}})) {
if (!FS.readFiles) FS.readFiles = {};
FS.readFiles ??= {};
if (!(path in FS.readFiles)) {
FS.readFiles[path] = 1;
#if FS_DEBUG
Expand Down Expand Up @@ -1588,7 +1588,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
36 changes: 18 additions & 18 deletions src/library_html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,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 +507,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 +598,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 +673,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 +745,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 +809,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 +879,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 +962,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 +1074,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 +1200,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 +1421,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 +1604,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 +1742,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 +1833,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 +1887,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 +2035,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 +2175,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

0 comments on commit 3a4cea5

Please sign in to comment.