Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage coalescing operators. NFC #20531

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,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 @@ -133,7 +133,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 @@ -198,7 +198,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 @@ -248,7 +248,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 @@ -354,12 +354,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 @@ -371,7 +371,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 @@ -381,7 +381,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 @@ -410,7 +410,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 @@ -425,7 +425,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 @@ -436,7 +436,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 @@ -468,14 +468,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 @@ -486,7 +486,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 @@ -496,7 +496,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 @@ -519,7 +519,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 @@ -529,7 +529,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
68 changes: 33 additions & 35 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 @@ -368,7 +366,7 @@ var emscriptenCpuProfiler = {
fpsOverlay.style = 'position: fixed; font-weight: bold; padding: 3px; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; cursor: pointer;';
fpsOverlay.onclick = () => {
var view = document.getElementById('cpuprofiler_canvas');
if (view) view.scrollIntoView();
view?.scrollIntoView();
};
fpsOverlay.oncontextmenu = (e) => e.preventDefault();
document.body.appendChild(fpsOverlay);
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 @@ -1307,9 +1307,7 @@ var LibraryEmbind = {
return ptr;
},
destructor(ptr) {
if (this.rawDestructor) {
this.rawDestructor(ptr);
}
this.rawDestructor?.(ptr);
},
'argPackAdvance': GenericWireTypeSize,
'readValueFromPointer': readPointer,
Expand Down Expand Up @@ -1743,12 +1741,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 @@ -1803,9 +1797,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
38 changes: 15 additions & 23 deletions src/headless.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ var window = {
eventListeners: {},
addEventListener(id, func) {
var listeners = this.eventListeners[id];
if (!listeners) {
listeners = this.eventListeners[id] = [];
}
listeners ||= this.eventListeners[id] = [];
listeners.push(func);
},
removeEventListener(id, func) {
Expand All @@ -100,9 +98,7 @@ var window = {
},
callEventListeners(id) {
var listeners = this.eventListeners[id];
if (listeners) {
listeners.forEach((listener) => listener());
}
listeners?.forEach((listener) => listener());
},
URL: {
createObjectURL(x) {
Expand Down Expand Up @@ -161,17 +157,15 @@ var document = {
},
elements: {},
querySelector(id) {
if (!document.elements[id]) {
document.elements[id] = {
classList: {
add() {},
remove() {},
},
eventListeners: {},
addEventListener: document.addEventListener,
removeEventListener: document.removeEventListener,
callEventListeners: document.callEventListeners,
};
document.elements[id] ||= {
classList: {
add() {},
remove() {},
},
eventListeners: {},
addEventListener: document.addEventListener,
removeEventListener: document.removeEventListener,
callEventListeners: document.callEventListeners,
};
return document.elements[id];
},
Expand Down Expand Up @@ -214,7 +208,7 @@ var XMLHttpRequest = function() {
} else {
window.setTimeout(() => {
this.doSend();
if (this.onload) this.onload();
this.onload?.();
}, 0);
}
},
Expand All @@ -239,7 +233,7 @@ var Image = () => {
this.complete = true;
this.width = 64;
this.height = 64;
if (this.onload) this.onload();
this.onload?.();
});
};
var Worker = (workerPath) => {
Expand All @@ -252,7 +246,7 @@ var Worker = (workerPath) => {

function duplicateJSON(json) {
function handleTypedArrays(key, value) {
if (value && value.toString && value.toString().substring(0, 8) == '[object ' && value.length && value.byteLength) {
if (value?.toString && value.toString().substring(0, 8) == '[object ' && value.length && value.byteLength) {
return Array.prototype.slice.call(value);
}
return value;
Expand Down Expand Up @@ -293,6 +287,4 @@ if (typeof console == 'undefined') {
}

// additional setup
if (!Module['canvas']) {
Module['canvas'] = document.getElementById('canvas');
}
Module['canvas'] ||= document.getElementById('canvas');
Loading