Skip to content

Commit

Permalink
Leverage coalescing operators. NFC
Browse files Browse the repository at this point in the history
Note that this bumps minimum Chrome & Firefox versions.

I believe this is not blocked on emscripten-core#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 `??`.
  • Loading branch information
RReverser committed Oct 24, 2023
1 parent 3630336 commit cc67cbd
Show file tree
Hide file tree
Showing 49 changed files with 260 additions and 330 deletions.
34 changes: 17 additions & 17 deletions src/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function fetchDeleteCachedData(db, fetch, onsuccess, onerror) {

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

try {
Expand Down Expand Up @@ -131,7 +131,7 @@ function fetchLoadCachedData(db, fetch, onsuccess, onerror) {

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

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

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

try {
Expand Down Expand Up @@ -246,7 +246,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {

var fetch_attr = fetch + {{{ C_STRUCTS.emscripten_fetch_t.__attributes }}};
var requestMethod = UTF8ToString(fetch_attr + {{{ C_STRUCTS.emscripten_fetch_attr_t.requestMethod }}});
if (!requestMethod) requestMethod = 'GET';
requestMethod ||= 'GET';
var timeoutMsecs = {{{ makeGetValue('fetch_attr', C_STRUCTS.emscripten_fetch_attr_t.timeoutMSecs, 'u32') }}};
var userName = {{{ makeGetValue('fetch_attr', C_STRUCTS.emscripten_fetch_attr_t.userName, '*') }}};
var password = {{{ makeGetValue('fetch_attr', C_STRUCTS.emscripten_fetch_attr_t.password, '*') }}};
Expand Down Expand Up @@ -352,12 +352,12 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
#if FETCH_DEBUG
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" succeeded with status ${xhr.status}`);
#endif
if (onsuccess) onsuccess(fetch, xhr, e);
onsuccess?.(fetch, xhr, e);
} else {
#if FETCH_DEBUG
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" failed with status ${xhr.status}`);
#endif
if (onerror) onerror(fetch, xhr, e);
onerror?.(fetch, xhr, e);
}
};
xhr.onerror = (e) => {
Expand All @@ -369,7 +369,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" finished with error, readyState ${xhr.readyState} and status ${xhr.status}`);
#endif
saveResponseAndStatus();
if (onerror) onerror(fetch, xhr, e);
onerror?.(fetch, xhr, e);
};
xhr.ontimeout = (e) => {
// check if xhr was aborted by user and don't try to call back
Expand All @@ -379,7 +379,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
#if FETCH_DEBUG
dbg(`fetch: xhr of URL "${xhr.url_}" / responseURL "${xhr.responseURL}" timed out, readyState ${xhr.readyState} and status ${xhr.status}`);
#endif
if (onerror) onerror(fetch, xhr, e);
onerror?.(fetch, xhr, e);
};
xhr.onprogress = (e) => {
// check if xhr was aborted by user and don't try to call back
Expand Down Expand Up @@ -408,7 +408,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
if (xhr.readyState >= 3 && xhr.status === 0 && e.loaded > 0) xhr.status = 200;
HEAPU16[fetch + {{{ C_STRUCTS.emscripten_fetch_t.status }}} >> 1] = xhr.status;
if (xhr.statusText) stringToUTF8(xhr.statusText, fetch + {{{ C_STRUCTS.emscripten_fetch_t.statusText }}}, 64);
if (onprogress) onprogress(fetch, xhr, e);
onprogress?.(fetch, xhr, e);
if (ptr) {
_free(ptr);
}
Expand All @@ -423,7 +423,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
if (xhr.readyState >= 2) {
HEAPU16[fetch + {{{ C_STRUCTS.emscripten_fetch_t.status }}} >> 1] = xhr.status;
}
if (onreadystatechange) onreadystatechange(fetch, xhr, e);
onreadystatechange?.(fetch, xhr, e);
};
#if FETCH_DEBUG
dbg(`fetch: xhr.send(data=${data})`);
Expand All @@ -434,7 +434,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
#if FETCH_DEBUG
dbg(`fetch: xhr failed with exception: ${e}`);
#endif
if (onerror) onerror(fetch, xhr, e);
onerror?.(fetch, xhr, e);
}
}

Expand Down Expand Up @@ -466,14 +466,14 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
{{{ runtimeKeepalivePop() }}}
doCallback(() => {
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
else if (successcb) successcb(fetch);
else successcb?.(fetch);
});
};

var reportProgress = (fetch, xhr, e) => {
doCallback(() => {
if (onprogress) {{{ makeDynCall('vp', 'onprogress') }}}(fetch);
else if (progresscb) progresscb(fetch);
else progresscb?.(fetch);
});
};

Expand All @@ -484,7 +484,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
{{{ runtimeKeepalivePop() }}}
doCallback(() => {
if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(fetch);
else if (errorcb) errorcb(fetch);
else errorcb?.(fetch);
});
};

Expand All @@ -494,7 +494,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
#endif
doCallback(() => {
if (onreadystatechange) {{{ makeDynCall('vp', 'onreadystatechange') }}}(fetch);
else if (readystatechangecb) readystatechangecb(fetch);
else readystatechangecb?.(fetch);
});
};

Expand All @@ -517,7 +517,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
{{{ runtimeKeepalivePop() }}}
doCallback(() => {
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
else if (successcb) successcb(fetch);
else successcb?.(fetch);
});
};
var storeError = (fetch, xhr, e) => {
Expand All @@ -527,7 +527,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
{{{ runtimeKeepalivePop() }}}
doCallback(() => {
if (onsuccess) {{{ makeDynCall('vp', 'onsuccess') }}}(fetch);
else if (successcb) successcb(fetch);
else successcb?.(fetch);
});
};
fetchCacheData(Fetch.dbInstance, fetch, xhr.response, storeSuccess, storeError);
Expand Down
66 changes: 32 additions & 34 deletions src/cpuprofiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,37 +148,35 @@ var emscriptenCpuProfiler = {
createSection: function createSection(number, name, drawColor, traceable) {
while (this.sections.length <= number) this.sections.push(null); // Keep an array structure.
var sect = this.sections[number];
if (!sect) {
sect = {
count: 0,
name,
startTick: 0,
accumulatedTimeInsideMainLoop: 0,
accumulatedTimeOutsideMainLoop: 0,
frametimesInsideMainLoop: [],
frametimesOutsideMainLoop: [],
drawColor,
traceable,
accumulatedFrameTimeInsideMainLoop: function(startX, numSamples) {
var total = 0;
numSamples = Math.min(numSamples, this.frametimesInsideMainLoop.length);
for (var i = 0; i < numSamples; ++i) {
var x = (startX + i) % this.frametimesInsideMainLoop.length;
if (this.frametimesInsideMainLoop[x]) total += this.frametimesInsideMainLoop[x];
}
return total;
},
accumulatedFrameTimeOutsideMainLoop: function(startX, numSamples) {
var total = 0;
numSamples = Math.min(numSamples, this.frametimesInsideMainLoop.length);
for (var i = 0; i < numSamples; ++i) {
var x = (startX + i) % this.frametimesInsideMainLoop.length;
if (this.frametimesOutsideMainLoop[x]) total += this.frametimesOutsideMainLoop[x];
}
return total;
sect ||= {
count: 0,
name,
startTick: 0,
accumulatedTimeInsideMainLoop: 0,
accumulatedTimeOutsideMainLoop: 0,
frametimesInsideMainLoop: [],
frametimesOutsideMainLoop: [],
drawColor,
traceable,
accumulatedFrameTimeInsideMainLoop: function(startX, numSamples) {
var total = 0;
numSamples = Math.min(numSamples, this.frametimesInsideMainLoop.length);
for (var i = 0; i < numSamples; ++i) {
var x = (startX + i) % this.frametimesInsideMainLoop.length;
if (this.frametimesInsideMainLoop[x]) total += this.frametimesInsideMainLoop[x];
}
};
}
return total;
},
accumulatedFrameTimeOutsideMainLoop: function(startX, numSamples) {
var total = 0;
numSamples = Math.min(numSamples, this.frametimesInsideMainLoop.length);
for (var i = 0; i < numSamples; ++i) {
var x = (startX + i) % this.frametimesInsideMainLoop.length;
if (this.frametimesOutsideMainLoop[x]) total += this.frametimesOutsideMainLoop[x];
}
return total;
}
};
sect.name = name;
this.sections[number] = sect;
},
Expand Down Expand Up @@ -532,14 +530,14 @@ var emscriptenCpuProfiler = {
},

detectWebGLContext: function() {
if (Module['canvas'] && Module['canvas'].GLctxObject && Module['canvas'].GLctxObject.GLctx) return Module['canvas'].GLctxObject.GLctx;
if (Module['canvas']?.GLctxObject?.GLctx) return Module['canvas'].GLctxObject.GLctx;
else if (typeof GLctx != 'undefined') return GLctx;
else if (Module.ctx) return Module.ctx;
return null;
},

toggleHookWebGL: function(glCtx) {
if (!glCtx) glCtx = this.detectWebGLContext();
glCtx ||= this.detectWebGLContext();
if (this.hookedWebGLContexts.includes(glCtx)) this.unhookWebGL(glCtx);
else this.hookWebGL(glCtx);
},
Expand All @@ -563,7 +561,7 @@ var emscriptenCpuProfiler = {
},

unhookWebGL: function(glCtx) {
if (!glCtx) glCtx = this.detectWebGLContext();
glCtx ||= this.detectWebGLContext();
if (!glCtx.cpuprofilerAlreadyHooked) return;
glCtx.cpuprofilerAlreadyHooked = false;
this.hookedWebGLContexts.splice(this.hookedWebGLContexts.indexOf(glCtx), 1);
Expand Down Expand Up @@ -601,7 +599,7 @@ var emscriptenCpuProfiler = {
},

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
16 changes: 4 additions & 12 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
Expand Up @@ -1370,9 +1370,7 @@ var LibraryEmbind = {
return ptr;
},
destructor(ptr) {
if (this.rawDestructor) {
this.rawDestructor(ptr);
}
this.rawDestructor?.(ptr);
},
'argPackAdvance': GenericWireTypeSize,
'readValueFromPointer': readPointer,
Expand Down Expand Up @@ -1806,12 +1804,8 @@ var LibraryEmbind = {
rawDestructor) => {
name = readLatin1String(name);
getActualType = embind__requireFunction(getActualTypeSignature, getActualType);
if (upcast) {
upcast = embind__requireFunction(upcastSignature, upcast);
}
if (downcast) {
downcast = embind__requireFunction(downcastSignature, downcast);
}
upcast &&= embind__requireFunction(upcastSignature, upcast);
downcast &&= embind__requireFunction(downcastSignature, downcast);
rawDestructor = embind__requireFunction(destructorSignature, rawDestructor);
var legalFunctionName = makeLegalFunctionName(name);

Expand Down Expand Up @@ -1866,9 +1860,7 @@ var LibraryEmbind = {

if (registeredClass.baseClass) {
// Keep track of class hierarchy. Used to allow sub-classes to inherit class functions.
if (registeredClass.baseClass.__derivedClasses === undefined) {
registeredClass.baseClass.__derivedClasses = [];
}
registeredClass.baseClass.__derivedClasses ??= [];

registeredClass.baseClass.__derivedClasses.push(registeredClass);
}
Expand Down
4 changes: 1 addition & 3 deletions src/embind/emval.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,7 @@ var LibraryEmVal = {
}
var rv = kind === /* CONSTRUCTOR */ 1 ? reflectConstruct(func, argN) : func.apply(obj, argN);
for (var i = 0; i < argCount; ++i) {
if (types[i].deleteObject) {
types[i].deleteObject(argN[i]);
}
types[i].deleteObject?.(argN[i]);
}
return emval_returnValue(retType, destructorsRef, rv);
};
Expand Down
Loading

0 comments on commit cc67cbd

Please sign in to comment.