Skip to content

Commit fd02d44

Browse files
committed
Extend usage of ES6 arrow functions and default params. NFC
See #11984
1 parent f8b200a commit fd02d44

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+192
-223
lines changed

src/Fetch.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var Fetch = {
2828
var openRequest = indexedDB.open(dbname, dbversion);
2929
} catch (e) { return onerror(e); }
3030

31-
openRequest.onupgradeneeded = function(event) {
31+
openRequest.onupgradeneeded = (event) => {
3232
#if FETCH_DEBUG
3333
console.log('fetch: IndexedDB upgrade needed. Clearing database.');
3434
#endif
@@ -38,16 +38,16 @@ var Fetch = {
3838
}
3939
db.createObjectStore('FILES');
4040
};
41-
openRequest.onsuccess = function(event) { onsuccess(event.target.result); };
42-
openRequest.onerror = function(error) { onerror(error); };
41+
openRequest.onsuccess = (event) => onsuccess(event.target.result);
42+
openRequest.onerror = (error) => onerror(error);
4343
},
4444
#endif
4545

4646
staticInit: function() {
4747
var isMainThread = true;
4848

4949
#if FETCH_SUPPORT_INDEXEDDB
50-
var onsuccess = function(db) {
50+
var onsuccess = (db) => {
5151
#if FETCH_DEBUG
5252
console.log('fetch: IndexedDB successfully opened.');
5353
#endif
@@ -57,7 +57,7 @@ var Fetch = {
5757
removeRunDependency('library_fetch_init');
5858
}
5959
};
60-
var onerror = function() {
60+
var onerror = () => {
6161
#if FETCH_DEBUG
6262
console.error('fetch: IndexedDB open failed.');
6363
#endif
@@ -95,7 +95,7 @@ function fetchDeleteCachedData(db, fetch, onsuccess, onerror) {
9595
var transaction = db.transaction(['FILES'], 'readwrite');
9696
var packages = transaction.objectStore('FILES');
9797
var request = packages.delete(pathStr);
98-
request.onsuccess = function(event) {
98+
request.onsuccess = (event) => {
9999
var value = event.target.result;
100100
#if FETCH_DEBUG
101101
console.log('fetch: Deleted file ' + pathStr + ' from IndexedDB');
@@ -109,7 +109,7 @@ function fetchDeleteCachedData(db, fetch, onsuccess, onerror) {
109109
stringToUTF8("OK", fetch + {{{ C_STRUCTS.emscripten_fetch_t.statusText }}}, 64);
110110
onsuccess(fetch, 0, value);
111111
};
112-
request.onerror = function(error) {
112+
request.onerror = (error) => {
113113
#if FETCH_DEBUG
114114
console.error('fetch: Failed to delete file ' + pathStr + ' from IndexedDB! error: ' + error);
115115
#endif
@@ -144,7 +144,7 @@ function fetchLoadCachedData(db, fetch, onsuccess, onerror) {
144144
var transaction = db.transaction(['FILES'], 'readonly');
145145
var packages = transaction.objectStore('FILES');
146146
var getRequest = packages.get(pathStr);
147-
getRequest.onsuccess = function(event) {
147+
getRequest.onsuccess = (event) => {
148148
if (event.target.result) {
149149
var value = event.target.result;
150150
var len = value.byteLength || value.length;
@@ -174,7 +174,7 @@ function fetchLoadCachedData(db, fetch, onsuccess, onerror) {
174174
onerror(fetch, 0, 'no data');
175175
}
176176
};
177-
getRequest.onerror = function(error) {
177+
getRequest.onerror = (error) => {
178178
#if FETCH_DEBUG
179179
console.error('fetch: Failed to load file ' + pathStr + ' from IndexedDB!');
180180
#endif
@@ -209,7 +209,7 @@ function fetchCacheData(db, fetch, data, onsuccess, onerror) {
209209
var transaction = db.transaction(['FILES'], 'readwrite');
210210
var packages = transaction.objectStore('FILES');
211211
var putRequest = packages.put(data, destinationPathStr);
212-
putRequest.onsuccess = function(event) {
212+
putRequest.onsuccess = (event) => {
213213
#if FETCH_DEBUG
214214
console.log('fetch: Stored file "' + destinationPathStr + '" to IndexedDB cache.');
215215
#endif
@@ -218,7 +218,7 @@ function fetchCacheData(db, fetch, data, onsuccess, onerror) {
218218
stringToUTF8("OK", fetch + {{{ C_STRUCTS.emscripten_fetch_t.statusText }}}, 64);
219219
onsuccess(fetch, 0, destinationPathStr);
220220
};
221-
putRequest.onerror = function(error) {
221+
putRequest.onerror = (error) => {
222222
#if FETCH_DEBUG
223223
console.error('fetch: Failed to store file "' + destinationPathStr + '" to IndexedDB cache!');
224224
#endif
@@ -341,7 +341,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
341341
Fetch.setu64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.numBytes }}}, ptrLen);
342342
}
343343

344-
xhr.onload = function(e) {
344+
xhr.onload = (e) => {
345345
saveResponse(fetchAttrLoadToMemory && !fetchAttrStreamData);
346346
var len = xhr.response ? xhr.response.byteLength : 0;
347347
Fetch.setu64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.dataOffset }}}, 0);
@@ -366,7 +366,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
366366
if (onerror) onerror(fetch, xhr, e);
367367
}
368368
};
369-
xhr.onerror = function(e) {
369+
xhr.onerror = (e) => {
370370
saveResponse(fetchAttrLoadToMemory);
371371
var status = xhr.status; // XXX TODO: Overwriting xhr.status doesn't work here, so don't override anywhere else either.
372372
#if FETCH_DEBUG
@@ -378,13 +378,13 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
378378
HEAPU16[fetch + {{{ C_STRUCTS.emscripten_fetch_t.status }}} >> 1] = status;
379379
if (onerror) onerror(fetch, xhr, e);
380380
};
381-
xhr.ontimeout = function(e) {
381+
xhr.ontimeout = (e) => {
382382
#if FETCH_DEBUG
383383
console.error('fetch: xhr of URL "' + xhr.url_ + '" / responseURL "' + xhr.responseURL + '" timed out, readyState ' + xhr.readyState + ' and status ' + xhr.status);
384384
#endif
385385
if (onerror) onerror(fetch, xhr, e);
386386
};
387-
xhr.onprogress = function(e) {
387+
xhr.onprogress = (e) => {
388388
var ptrLen = (fetchAttrLoadToMemory && fetchAttrStreamData && xhr.response) ? xhr.response.byteLength : 0;
389389
var ptr = 0;
390390
if (fetchAttrLoadToMemory && fetchAttrStreamData) {
@@ -411,7 +411,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
411411
_free(ptr);
412412
}
413413
};
414-
xhr.onreadystatechange = function(e) {
414+
xhr.onreadystatechange = (e) => {
415415
HEAPU16[fetch + {{{ C_STRUCTS.emscripten_fetch_t.readyState }}} >> 1] = xhr.readyState;
416416
if (xhr.readyState >= 2) {
417417
HEAPU16[fetch + {{{ C_STRUCTS.emscripten_fetch_t.status }}} >> 1] = xhr.status;
@@ -453,7 +453,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
453453
var fetchAttrReplace = !!(fetchAttributes & {{{ cDefine('EMSCRIPTEN_FETCH_REPLACE') }}});
454454
var fetchAttrSynchronous = !!(fetchAttributes & {{{ cDefine('EMSCRIPTEN_FETCH_SYNCHRONOUS') }}});
455455

456-
var reportSuccess = function(fetch, xhr, e) {
456+
var reportSuccess = (fetch, xhr, e) => {
457457
#if FETCH_DEBUG
458458
console.log('fetch: operation success. e: ' + e);
459459
#endif
@@ -464,14 +464,14 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
464464
}, fetchAttrSynchronous);
465465
};
466466

467-
var reportProgress = function(fetch, xhr, e) {
467+
var reportProgress = (fetch, xhr, e) => {
468468
callUserCallback(function() {
469469
if (onprogress) {{{ makeDynCall('vi', 'onprogress') }}}(fetch);
470470
else if (progresscb) progresscb(fetch);
471471
}, fetchAttrSynchronous);
472472
};
473473

474-
var reportError = function(fetch, xhr, e) {
474+
var reportError = (fetch, xhr, e) => {
475475
#if FETCH_DEBUG
476476
console.error('fetch: operation failed: ' + e);
477477
#endif
@@ -482,7 +482,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
482482
}, fetchAttrSynchronous);
483483
};
484484

485-
var reportReadyStateChange = function(fetch, xhr, e) {
485+
var reportReadyStateChange = (fetch, xhr, e) => {
486486
#if FETCH_DEBUG
487487
console.log('fetch: ready state change. e: ' + e);
488488
#endif
@@ -492,19 +492,19 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
492492
}, fetchAttrSynchronous);
493493
};
494494

495-
var performUncachedXhr = function(fetch, xhr, e) {
495+
var performUncachedXhr = (fetch, xhr, e) => {
496496
#if FETCH_DEBUG
497497
console.error('fetch: starting (uncached) XHR: ' + e);
498498
#endif
499499
fetchXHR(fetch, reportSuccess, reportError, reportProgress, reportReadyStateChange);
500500
};
501501

502502
#if FETCH_SUPPORT_INDEXEDDB
503-
var cacheResultAndReportSuccess = function(fetch, xhr, e) {
503+
var cacheResultAndReportSuccess = (fetch, xhr, e) => {
504504
#if FETCH_DEBUG
505505
console.log('fetch: operation success. Caching result.. e: ' + e);
506506
#endif
507-
var storeSuccess = function(fetch, xhr, e) {
507+
var storeSuccess = (fetch, xhr, e) => {
508508
#if FETCH_DEBUG
509509
console.log('fetch: IndexedDB store succeeded.');
510510
#endif
@@ -514,7 +514,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
514514
else if (successcb) successcb(fetch);
515515
}, fetchAttrSynchronous);
516516
};
517-
var storeError = function(fetch, xhr, e) {
517+
var storeError = (fetch, xhr, e) => {
518518
#if FETCH_DEBUG
519519
console.error('fetch: IndexedDB store failed.');
520520
#endif
@@ -527,7 +527,7 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
527527
fetchCacheData(Fetch.dbInstance, fetch, xhr.response, storeSuccess, storeError);
528528
};
529529

530-
var performCachedXhr = function(fetch, xhr, e) {
530+
var performCachedXhr = (fetch, xhr, e) => {
531531
#if FETCH_DEBUG
532532
console.error('fetch: starting (cached) XHR: ' + e);
533533
#endif

src/cpuprofiler.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ if (!performance.realNow) {
1515
if (isSafari) {
1616
var realPerformance = performance;
1717
performance = {
18-
realNow: function() { return realPerformance.now(); },
19-
now: function() { return realPerformance.now(); }
18+
realNow: () => realPerformance.now(),
19+
now: () => realPerformance.now()
2020
};
2121
} else {
2222
performance.realNow = performance.now;
@@ -275,7 +275,7 @@ var emscriptenCpuProfiler = {
275275
hookedWebGLContexts: [],
276276
logWebGLCallsSlowerThan: Infinity,
277277

278-
toggleHelpTextVisible: function() {
278+
toggleHelpTextVisible: () => {
279279
var help = document.getElementById('cpuprofiler_help_text');
280280
if (help.style) help.style.display = (help.style.display == 'none') ? 'block' : 'none';
281281
},
@@ -285,7 +285,7 @@ var emscriptenCpuProfiler = {
285285
// Hook into requestAnimationFrame function to grab animation even if application did not use emscripten_set_main_loop() to drive animation, but e.g. used its own function that performs requestAnimationFrame().
286286
if (!window.realRequestAnimationFrame) {
287287
window.realRequestAnimationFrame = window.requestAnimationFrame;
288-
window.requestAnimationFrame = function(cb) {
288+
window.requestAnimationFrame = (cb) => {
289289
function hookedCb(p) {
290290
emscriptenCpuProfiler.frameStart();
291291
cb(performance.now());
@@ -348,7 +348,7 @@ var emscriptenCpuProfiler = {
348348
helpText += "</div>";
349349

350350
div.innerHTML = "<div style='color: black; border: 2px solid black; padding: 2px; margin-bottom: 10px; margin-left: 5px; margin-right: 5px; margin-top: 5px; background-color: #F0F0FF;'><span style='margin-left: 10px;'><b>Cpu Profiler</b><sup style='cursor: pointer;' onclick='emscriptenCpuProfiler.toggleHelpTextVisible();'>[?]</sup></span> <button style='display:inline; border: solid 1px #ADADAD; margin: 2px; background-color: #E1E1E1;' onclick='noExitRuntime=false;Module.exit();'>Halt</button><button id='toggle_webgl_profile' style='display:inline; border: solid 1px #ADADAD; margin: 2px; background-color: #E1E1E1;' onclick='emscriptenCpuProfiler.toggleHookWebGL()'>Profile WebGL</button><button id='toggle_webgl_trace' style='display:inline; border: solid 1px #ADADAD; margin: 2px; background-color: #E1E1E1;' onclick='emscriptenCpuProfiler.toggleTraceWebGL()'>Trace Calls</button> slower than <input id='trace_limit' oninput='emscriptenCpuProfiler.disableTraceWebGL();' style='width:40px;' value='100'></input> msecs. <span id='fpsResult' style='margin-left: 5px;'></span><canvas style='border: 1px solid black; margin-left:auto; margin-right:auto; display: block;' id='cpuprofiler_canvas' width='800px' height='200'></canvas><div id='cpuprofiler'></div>" + helpText;
351-
document.getElementById('trace_limit').onkeydown = function(e) { if (e.which == 13 || e.keycode == 13) emscriptenCpuProfiler.enableTraceWebGL(); else emscriptenCpuProfiler.disableTraceWebGL(); };
351+
document.getElementById('trace_limit').onkeydown = (e) => { if (e.which == 13 || e.keycode == 13) emscriptenCpuProfiler.enableTraceWebGL(); else emscriptenCpuProfiler.disableTraceWebGL(); };
352352
cpuprofiler = document.getElementById('cpuprofiler');
353353

354354
if (location.search.includes('expandhelp')) this.toggleHelpTextVisible();
@@ -366,8 +366,11 @@ var emscriptenCpuProfiler = {
366366
fpsOverlay.classList.add("hastooltip");
367367
fpsOverlay.innerHTML = '<div id="fpsOverlay1" style="font-size: 1.5em; color: lightgreen; text-shadow: 3px 3px black;"></div><div id="fpsOverlay2" style="font-size: 1em; color: lightgrey; text-shadow: 3px 3px black;"></div> <span class="tooltip">FPS (CPU usage %)<br>Min/Avg/Max frame times (msecs)</span>';
368368
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;';
369-
fpsOverlay.onclick = function() { var view = document.getElementById('cpuprofiler_canvas'); if (view) view.scrollIntoView(); }
370-
fpsOverlay.oncontextmenu = function(e) { e.preventDefault(); };
369+
fpsOverlay.onclick = () => {
370+
var view = document.getElementById('cpuprofiler_canvas');
371+
if (view) view.scrollIntoView();
372+
};
373+
fpsOverlay.oncontextmenu = (e) => e.preventDefault();
371374
document.body.appendChild(fpsOverlay);
372375
this.fpsOverlay1 = document.getElementById('fpsOverlay1');
373376
this.fpsOverlay2 = document.getElementById('fpsOverlay2');
@@ -387,8 +390,8 @@ var emscriptenCpuProfiler = {
387390
this.clearUi(0, this.canvas.width);
388391
this.drawGraphLabels();
389392
this.updateUi();
390-
Module['preMainLoop'] = function cpuprofiler_frameStart() { emscriptenCpuProfiler.frameStart(); }
391-
Module['postMainLoop'] = function cpuprofiler_frameEnd() { emscriptenCpuProfiler.frameEnd(); }
393+
Module['preMainLoop'] = () => emscriptenCpuProfiler.frameStart();
394+
Module['postMainLoop'] = () => emscriptenCpuProfiler.frameEnd();
392395
},
393396

394397
drawHorizontalLine: function drawHorizontalLine(startX, endX, pixelThickness, msecs) {
@@ -620,24 +623,23 @@ var emscriptenCpuProfiler = {
620623
if (typeof glCtx[f] !== 'function' || f.startsWith('real_')) continue;
621624
this.hookWebGLFunction(f, glCtx);
622625
}
623-
var this_ = this;
624626
// The above injection won't work for texImage2D and texSubImage2D, which have multiple overloads.
625-
glCtx['texImage2D'] = function(a1, a2, a3, a4, a5, a6, a7, a8, a9) {
626-
this_.enterSection(1);
627+
glCtx['texImage2D'] = (a1, a2, a3, a4, a5, a6, a7, a8, a9) => {
628+
this.enterSection(1);
627629
var ret = (a7 !== undefined) ? glCtx['real_texImage2D'](a1, a2, a3, a4, a5, a6, a7, a8, a9) : glCtx['real_texImage2D'](a1, a2, a3, a4, a5, a6);
628-
this_.endSection(1);
630+
this.endSection(1);
629631
return ret;
630632
};
631-
glCtx['texSubImage2D'] = function(a1, a2, a3, a4, a5, a6, a7, a8, a9) {
632-
this_.enterSection(0);
633+
glCtx['texSubImage2D'] = (a1, a2, a3, a4, a5, a6, a7, a8, a9) => {
634+
this.enterSection(0);
633635
var ret = (a8 !== undefined) ? glCtx['real_texSubImage2D'](a1, a2, a3, a4, a5, a6, a7, a8, a9) : glCtx['real_texSubImage2D'](a1, a2, a3, a4, a5, a6, a7);
634-
this_.endSection(0);
636+
this.endSection(0);
635637
return ret;
636638
};
637-
glCtx['texSubImage3D'] = function(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) {
638-
this_.enterSection(0);
639+
glCtx['texSubImage3D'] = (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) => {
640+
this.enterSection(0);
639641
var ret = (a9 !== undefined) ? glCtx['real_texSubImage3D'](a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) : glCtx['real_texSubImage2D'](a1, a2, a3, a4, a5, a6, a7, a8);
640-
this_.endSection(0);
642+
this.endSection(0);
641643
return ret;
642644
};
643645
}
@@ -646,7 +648,7 @@ var emscriptenCpuProfiler = {
646648
// Hook into setInterval to be able to capture the time spent executing them.
647649
emscriptenCpuProfiler.createSection(2, 'setInterval', emscriptenCpuProfiler.colorSetIntervalSection, /*traceable=*/true);
648650
var realSetInterval = setInterval;
649-
setInterval = function(fn, delay) {
651+
setInterval = (fn, delay) => {
650652
function wrappedSetInterval() {
651653
emscriptenCpuProfiler.enterSection(2);
652654
fn();
@@ -658,7 +660,7 @@ setInterval = function(fn, delay) {
658660
// Hook into setTimeout to be able to capture the time spent executing them.
659661
emscriptenCpuProfiler.createSection(3, 'setTimeout', emscriptenCpuProfiler.colorSetTimeoutSection, /*traceable=*/true);
660662
var realSetTimeout = setTimeout;
661-
setTimeout = function(fn, delay) {
663+
setTimeout = (fn, delay) => {
662664
function wrappedSetTimeout() {
663665
emscriptenCpuProfiler.enterSection(3);
664666
fn();

src/deterministic.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
*/
66

77
var MAGIC = 0;
8-
Math.random = function() {
8+
Math.random = () => {
99
MAGIC = Math.pow(MAGIC + 1.8912, 3) % 1;
1010
return MAGIC;
1111
};
1212
var TIME = 10000;
13-
Date.now = function() {
14-
return TIME++;
15-
};
13+
Date.now = () => TIME++;
1614
if (typeof performance === 'object') performance.now = Date.now;
1715
if (ENVIRONMENT_IS_NODE) process['hrtime'] = Date.now;
1816

0 commit comments

Comments
 (0)