From de61f5f699eb55058ddd34e0efaf2bb40c8633d3 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 27 Jul 2026 08:46:42 -0700 Subject: [PATCH] Refactor src/lib/libopenal.js. NFC - Use `for .. of` in a few more places. - Remove duplication in setSourceParam(AL_BUFFER) --- src/lib/libopenal.js | 114 ++++++++---------- .../test_codesize_hello_dylink_all.json | 4 +- tools/maint/sync_deps.py | 2 +- 3 files changed, 54 insertions(+), 66 deletions(-) diff --git a/src/lib/libopenal.js b/src/lib/libopenal.js index 1961aeebcf447..a245d85d8e7ad 100644 --- a/src/lib/libopenal.js +++ b/src/lib/libopenal.js @@ -143,7 +143,7 @@ var LibraryOpenAL = { var buf = src.bufQueue[bufCursor % src.bufQueue.length]; // If the buffer contains no data, skip it - if (buf.length === 0) { + if (!buf.length) { skipCount++; // If we've gone through the whole queue and everything is 0 length, just give up if (skipCount === src.bufQueue.length) { @@ -249,7 +249,7 @@ var LibraryOpenAL = { } else if (src.type === {{{ cDefs.AL_STATIC }}} && src.looping) { // If the source is a looping static buffer, determine the buffer offset based on the loop points var buf = src.bufQueue[0]; - if (buf.length === 0) { + if (!buf.length) { src.bufOffset = 0.0; } else { var delta = (currentTime - src.bufStartTime) * src.playbackRate; @@ -331,8 +331,8 @@ var LibraryOpenAL = { }, stopSourceAudio: (src) => { - for (var i = 0; i < src.audioQueue.length; i++) { - src.audioQueue[i].stop(); + for (var audioSrc of src.audioQueue) { + audioSrc.stop(); } src.audioQueue.length = 0; }, @@ -399,9 +399,9 @@ var LibraryOpenAL = { // Find the first non-zero buffer in the queue to determine the proper format var templateBuf = AL.buffers[0]; - for (var i = 0; i < src.bufQueue.length; i++) { - if (src.bufQueue[i].id !== 0) { - templateBuf = src.bufQueue[i]; + for (var buf of src.bufQueue) { + if (buf.id) { + templateBuf = buf; break; } } @@ -697,9 +697,8 @@ var LibraryOpenAL = { sourceDuration: (src) => { var length = 0.0; - for (var i = 0; i < src.bufQueue.length; i++) { - var audioBuf = src.bufQueue[i].audioBuf; - length += audioBuf ? audioBuf.duration : 0.0; + for (var buf of src.bufQueue) { + length += buf.audioBuf?.duration ?? 0.0; } return length; }, @@ -709,9 +708,7 @@ var LibraryOpenAL = { var offset = 0.0; for (var i = 0; i < src.bufsProcessed; i++) { - if (src.bufQueue[i].audioBuf) { - offset += src.bufQueue[i].audioBuf.duration; - } + offset += src.bufQueue[i].audioBuf?.duration ?? 0.0; } offset += src.bufOffset; @@ -947,7 +944,7 @@ var LibraryOpenAL = { return; } var buf = AL.buffers[bufferId]; - if (!buf || bufferId === 0) { + if (!buf || !bufferId) { #if OPENAL_DEBUG dbg(`${funcname}() called with an invalid buffer`); #endif @@ -965,7 +962,7 @@ var LibraryOpenAL = { case 0x2004 /* AL_SIZE */: return buf.length * buf.bytesPerSample * buf.channels; case 0x2015 /* AL_LOOP_POINTS_SOFT */: - if (buf.length === 0) { + if (!buf.length) { return [0, 0]; } return [ @@ -989,7 +986,7 @@ var LibraryOpenAL = { return; } var buf = AL.buffers[bufferId]; - if (!buf || bufferId === 0) { + if (!buf || !bufferId) { #if OPENAL_DEBUG dbg(`${funcname}() called with an invalid buffer`); #endif @@ -1006,7 +1003,7 @@ var LibraryOpenAL = { switch (param) { case 0x2004 /* AL_SIZE */: - if (value !== 0) { + if (value) { #if OPENAL_DEBUG dbg(`${funcname}() param AL_SIZE value ${value} is out of range`); #endif @@ -1093,12 +1090,12 @@ var LibraryOpenAL = { case 0x1010 /* AL_SOURCE_STATE */: return src.state; case 0x1015 /* AL_BUFFERS_QUEUED */: - if (src.bufQueue.length === 1 && src.bufQueue[0].id === 0) { + if (src.bufQueue.length === 1 && !src.bufQueue[0].id) { return 0; } return src.bufQueue.length; case 0x1016 /* AL_BUFFERS_PROCESSED */: - if ((src.bufQueue.length === 1 && src.bufQueue[0].id === 0) || src.looping) { + if ((src.bufQueue.length === 1 && !src.bufQueue[0].id) || src.looping) { return 0; } return src.bufsProcessed; @@ -1131,17 +1128,17 @@ var LibraryOpenAL = { case 0x2009 /* AL_BYTE_LENGTH_SOFT */: var length = 0; var bytesPerFrame = 0; - for (var i = 0; i < src.bufQueue.length; i++) { - length += src.bufQueue[i].length; - if (src.bufQueue[i].id !== 0) { - bytesPerFrame = src.bufQueue[i].bytesPerSample * src.bufQueue[i].channels; + for (var buf of src.bufQueue) { + length += buf.length; + if (buf.id) { + bytesPerFrame = buf.bytesPerSample * buf.channels; } } return length * bytesPerFrame; case 0x200A /* AL_SAMPLE_LENGTH_SOFT */: var length = 0; - for (var i = 0; i < src.bufQueue.length; i++) { - length += src.bufQueue[i].length; + for (var buf of src.bufQueue) { + length += buf.length; } return length; case 0x200B /* AL_SEC_LENGTH_SOFT */: @@ -1318,8 +1315,8 @@ var LibraryOpenAL = { } if (value === 0) { - for (var i in src.bufQueue) { - src.bufQueue[i].refCount--; + for (var b of src.bufQueue) { + b.refCount--; } src.bufQueue.length = 1; src.bufQueue[0] = AL.buffers[0]; @@ -1336,8 +1333,8 @@ var LibraryOpenAL = { return; } - for (var i in src.bufQueue) { - src.bufQueue[i].refCount--; + for (var b of src.bufQueue) { + b.refCount--; } src.bufQueue.length = 0; @@ -1454,7 +1451,7 @@ var LibraryOpenAL = { if (srcLen > 0.0) { var frequency; for (var buf of src.bufQueue) { - if (buf.id !== 0) { + if (buf.id) { frequency = buf.frequency; break; } @@ -1476,7 +1473,7 @@ var LibraryOpenAL = { if (srcLen > 0.0) { var bytesPerSec; for (var buf of src.bufQueue) { - if (buf.id !== 0) { + if (buf.id) { bytesPerSec = buf.frequency * buf.bytesPerSample * buf.channels; break; } @@ -1561,7 +1558,7 @@ var LibraryOpenAL = { // people might assume that most alcCapture functions // accept NULL as a 'use the default' device. requireValidCaptureDevice: (deviceId, funcname) => { - if (deviceId === 0) { + if (!deviceId) { #if OPENAL_DEBUG dbg(`${funcname}() on a NULL device is an error`); #endif @@ -1616,7 +1613,7 @@ var LibraryOpenAL = { var resolvedDeviceName = AL.CAPTURE_DEVICE_NAME; // NULL is a valid device name here (resolves to default); - if (pDeviceName !== 0) { + if (pDeviceName) { resolvedDeviceName = UTF8ToString(pDeviceName); if (resolvedDeviceName !== AL.CAPTURE_DEVICE_NAME) { #if OPENAL_DEBUG @@ -2088,7 +2085,7 @@ var LibraryOpenAL = { while (true) { attr = HEAP32[pAttrList++]; attrs.push(attr); - if (attr === 0) { + if (!attr) { break; } val = HEAP32[pAttrList++]; @@ -2125,7 +2122,7 @@ var LibraryOpenAL = { } break; case 0x1996 /* ALC_HRTF_ID_SOFT */: - if (val !== 0) { + if (val) { #if OPENAL_DEBUG dbg(`Invalid ALC_HRTF_ID_SOFT index ${val}`); #endif @@ -2254,20 +2251,11 @@ var LibraryOpenAL = { }, alcGetCurrentContext__proxy: 'sync', - alcGetCurrentContext: () => { - if (AL.currentCtx !== null) { - return AL.currentCtx.id; - } - return 0; - }, + alcGetCurrentContext: () => AL.currentCtx ? AL.currentCtx.id : 0, alcMakeContextCurrent__proxy: 'sync', alcMakeContextCurrent: (contextId) => { - if (contextId === 0) { - AL.currentCtx = null; - } else { - AL.currentCtx = AL.contexts[contextId]; - } + AL.currentCtx = AL.contexts[contextId]; return {{{ cDefs.ALC_TRUE }}}; }, @@ -2295,7 +2283,7 @@ var LibraryOpenAL = { // Spec says : // Using a NULL handle is legal, but only the // tokens defined by the AL core are guaranteed. - if (deviceId !== 0 && !(deviceId in AL.deviceRefCounts)) { + if (deviceId && !(deviceId in AL.deviceRefCounts)) { #if OPENAL_DEBUG dbg('alcGetEnumValue() called with an invalid device'); #endif @@ -2399,7 +2387,7 @@ var LibraryOpenAL = { ret = AL.CAPTURE_DEVICE_NAME; break; case 0x310 /* ALC_CAPTURE_DEVICE_SPECIFIER */: - if (deviceId === 0) { + if (!deviceId) { ret = AL.CAPTURE_DEVICE_NAME + '\0'; } else { var c = AL.requireValidCaptureDevice(deviceId, 'alcGetString'); @@ -2429,7 +2417,7 @@ var LibraryOpenAL = { alcGetIntegerv__proxy: 'sync', alcGetIntegerv: (deviceId, param, size, pValues) => { - if (size === 0 || !pValues) { + if (!size || !pValues) { // Ignore the query, per the spec return; } @@ -2620,7 +2608,7 @@ var LibraryOpenAL = { var ret; switch (param) { case 0x1995 /* ALC_HRTF_SPECIFIER_SOFT */: - if (index === 0) { + if (!index) { ret = 'Web Audio HRTF'; } else { #if OPENAL_DEBUG @@ -2631,7 +2619,7 @@ var LibraryOpenAL = { } break; default: - if (index !== 0) { + if (index) { #if OPENAL_DEBUG dbg(`alcGetStringiSOFT() with param ${ptrToString(param)} not implemented yet`); #endif @@ -2664,7 +2652,7 @@ var LibraryOpenAL = { var val = 0; while (true) { attr = HEAP32[pAttrList++]; - if (attr === 0) { + if (!attr) { break; } val = HEAP32[pAttrList++]; @@ -2740,8 +2728,8 @@ var LibraryOpenAL = { for (var i = 0; i < count; ++i) { var bufId = {{{ makeGetValue('pBufferIds', 'i*4', 'i32') }}}; - /// Deleting the zero buffer is a legal NOP, so ignore it - if (bufId === 0) { + // Deleting the zero buffer is a legal NOP, so ignore it + if (!bufId) { continue; } @@ -2766,7 +2754,7 @@ var LibraryOpenAL = { for (var i = 0; i < count; ++i) { var bufId = {{{ makeGetValue('pBufferIds', 'i*4', 'i32') }}}; - if (bufId === 0) { + if (!bufId) { continue; } @@ -3250,7 +3238,7 @@ var LibraryOpenAL = { case {{{ cDefs.AL_DOPPLER_FACTOR }}}: case {{{ cDefs.AL_SPEED_OF_SOUND }}}: case {{{ cDefs.AL_DISTANCE_MODEL }}}: - return val !== 0 ? {{{ cDefs.AL_TRUE }}} : {{{ cDefs.AL_FALSE }}}; + return val ? {{{ cDefs.AL_TRUE }}} : {{{ cDefs.AL_FALSE }}}; default: #if OPENAL_DEBUG dbg(`alGetBoolean(): param ${ptrToString(param)} has wrong signature`); @@ -4032,14 +4020,14 @@ var LibraryOpenAL = { return; } - if (count === 0) { + if (!count) { return; } // Find the first non-zero buffer in the queue to determine the proper format var templateBuf = AL.buffers[0]; for (var buf of src.bufQueue) { - if (buf.id !== 0) { + if (buf.id) { templateBuf = buf; break; } @@ -4057,7 +4045,7 @@ var LibraryOpenAL = { } // Check that the added buffer has the correct format. If the template is the zero buffer, any format is valid. - if (templateBuf.id !== 0 && ( + if (templateBuf.id && ( buf.frequency !== templateBuf.frequency || buf.bytesPerSample !== templateBuf.bytesPerSample || buf.channels !== templateBuf.channels) @@ -4070,7 +4058,7 @@ var LibraryOpenAL = { } // If the only buffer in the queue is the zero buffer, clear the queue before we add anything. - if (src.bufQueue.length === 1 && src.bufQueue[0].id === 0) { + if (src.bufQueue.length === 1 && !src.bufQueue[0].id) { src.bufQueue.length = 0; } @@ -4107,12 +4095,12 @@ var LibraryOpenAL = { AL.currentCtx.err = {{{ cDefs.AL_INVALID_NAME }}}; return; } - if (count > (src.bufQueue.length === 1 && src.bufQueue[0].id === 0 ? 0 : src.bufsProcessed)) { + if (count > (src.bufQueue.length === 1 && !src.bufQueue[0].id ? 0 : src.bufsProcessed)) { AL.currentCtx.err = {{{ cDefs.AL_INVALID_VALUE }}}; return; } - if (count === 0) { + if (!count) { return; } @@ -4125,7 +4113,7 @@ var LibraryOpenAL = { } /// If the queue is empty, put the zero buffer back in - if (src.bufQueue.length === 0) { + if (!src.bufQueue.length) { src.bufQueue.push(AL.buffers[0]); } diff --git a/test/codesize/test_codesize_hello_dylink_all.json b/test/codesize/test_codesize_hello_dylink_all.json index 77d18f7fbfcfd..d7becc68c6fb7 100644 --- a/test/codesize/test_codesize_hello_dylink_all.json +++ b/test/codesize/test_codesize_hello_dylink_all.json @@ -1,7 +1,7 @@ { - "a.out.js": 268215, + "a.out.js": 267949, "a.out.nodebug.wasm": 588309, - "total": 856524, + "total": 856258, "sent": [ "IMG_Init", "IMG_Load", diff --git a/tools/maint/sync_deps.py b/tools/maint/sync_deps.py index ff418b3118396..9cf82f3a6dbb7 100755 --- a/tools/maint/sync_deps.py +++ b/tools/maint/sync_deps.py @@ -98,7 +98,7 @@ def sync_repo(name, repo_dir, revision, url): return if is_dirty(repo_dir): - utils.exit_with_error("Directory for {name} is dirty: '{repo_dir}'") + utils.exit_with_error(f"Directory for {name} is dirty: '{repo_dir}'") if not has_revision(repo_dir, revision): print(' Fetching revision')