Skip to content

Commit 1ebd4c8

Browse files
authored
Consistent use of typeof operator in JS library code. NFC (#16261)
I noticed that in some places we were using equality checks (==/!=) and in others we were using identity checks (===/!==). In general identity checks are safer, but since typeof is known to always returns a string and the RHS of these checks is also always a string we can choose to always prefer `!=` and `==` over `!==` and `===` for the sake of code size (sad but true, we care that much about each byte of output). Perhaps a better solution would be to teach closure compiler how to remove the extra `=` when its know that both sides of the operator are the same type already.. but that would be a much larger change.
1 parent 4fbff61 commit 1ebd4c8

File tree

96 files changed

+378
-378
lines changed

Some content is hidden

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

96 files changed

+378
-378
lines changed

src/Fetch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var Fetch = {
7171
#endif // ~FETCH_SUPPORT_INDEXEDDB
7272

7373
#if FETCH_SUPPORT_INDEXEDDB
74-
if (typeof ENVIRONMENT_IS_FETCH_WORKER === 'undefined' || !ENVIRONMENT_IS_FETCH_WORKER) addRunDependency('library_fetch_init');
74+
if (typeof ENVIRONMENT_IS_FETCH_WORKER == 'undefined' || !ENVIRONMENT_IS_FETCH_WORKER) addRunDependency('library_fetch_init');
7575
#endif
7676
}
7777
}

src/IDBStore.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
{
88
indexedDB: function() {
9-
if (typeof indexedDB !== 'undefined') return indexedDB;
9+
if (typeof indexedDB != 'undefined') return indexedDB;
1010
var ret = null;
11-
if (typeof window === 'object') ret = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
11+
if (typeof window == 'object') ret = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
1212
assert(ret, 'IDBStore used, but indexedDB not supported');
1313
return ret;
1414
},

src/base64Decode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ base64ReverseLookup[47] = 63; // '/'
3737
/** @noinline */
3838
function base64Decode(b64) {
3939
#if ENVIRONMENT_MAY_BE_NODE
40-
if (typeof ENVIRONMENT_IS_NODE !== 'undefined' && ENVIRONMENT_IS_NODE) {
40+
if (typeof ENVIRONMENT_IS_NODE != 'undefined' && ENVIRONMENT_IS_NODE) {
4141
var buf = Buffer.from(b64, 'base64');
4242
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
4343
}

src/base64Utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Decodes a base64 string.
99
* @param {string} input The string to decode.
1010
*/
11-
var decodeBase64 = typeof atob === 'function' ? atob : function (input) {
11+
var decodeBase64 = typeof atob == 'function' ? atob : function (input) {
1212
var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
1313

1414
var output = '';
@@ -43,7 +43,7 @@ var decodeBase64 = typeof atob === 'function' ? atob : function (input) {
4343
// Throws error on invalid input.
4444
function intArrayFromBase64(s) {
4545
#if ENVIRONMENT_MAY_BE_NODE
46-
if (typeof ENVIRONMENT_IS_NODE === 'boolean' && ENVIRONMENT_IS_NODE) {
46+
if (typeof ENVIRONMENT_IS_NODE == 'boolean' && ENVIRONMENT_IS_NODE) {
4747
var buf = Buffer.from(s, 'base64');
4848
return new Uint8Array(buf['buffer'], buf['byteOffset'], buf['byteLength']);
4949
}

src/cpuprofiler.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ var emscriptenCpuProfiler = {
533533

534534
detectWebGLContext: function() {
535535
if (Module['canvas'] && Module['canvas'].GLctxObject && Module['canvas'].GLctxObject.GLctx) return Module['canvas'].GLctxObject.GLctx;
536-
else if (typeof GLctx !== 'undefined') return GLctx;
536+
else if (typeof GLctx != 'undefined') return GLctx;
537537
else if (Module.ctx) return Module.ctx;
538538
return null;
539539
},
@@ -570,7 +570,7 @@ var emscriptenCpuProfiler = {
570570
document.getElementById("toggle_webgl_profile").style.background = '#E1E1E1';
571571

572572
for (var f in glCtx) {
573-
if (typeof glCtx[f] !== 'function' || f.startsWith('real_')) continue;
573+
if (typeof glCtx[f] != 'function' || f.startsWith('real_')) continue;
574574
var realf = 'real_' + f;
575575
glCtx[f] = glCtx[realf];
576576
delete glCtx[realf];
@@ -603,8 +603,8 @@ var emscriptenCpuProfiler = {
603603
hookWebGL: function(glCtx) {
604604
if (!glCtx) glCtx = this.detectWebGLContext();
605605
if (!glCtx) return;
606-
if (!((typeof WebGLRenderingContext !== 'undefined' && glCtx instanceof WebGLRenderingContext)
607-
|| (typeof WebGL2RenderingContext !== 'undefined' && glCtx instanceof WebGL2RenderingContext))) {
606+
if (!((typeof WebGLRenderingContext != 'undefined' && glCtx instanceof WebGLRenderingContext)
607+
|| (typeof WebGL2RenderingContext != 'undefined' && glCtx instanceof WebGL2RenderingContext))) {
608608
document.getElementById("toggle_webgl_profile").disabled = true;
609609
return;
610610
}
@@ -619,7 +619,7 @@ var emscriptenCpuProfiler = {
619619
this.createSection(0, 'Hot GL', this.colorHotGLFunction, /*traceable=*/true);
620620
this.createSection(1, 'Cold GL', this.colorColdGLFunction, /*traceable=*/true);
621621
for (var f in glCtx) {
622-
if (typeof glCtx[f] !== 'function' || f.startsWith('real_')) continue;
622+
if (typeof glCtx[f] != 'function' || f.startsWith('real_')) continue;
623623
this.hookWebGLFunction(f, glCtx);
624624
}
625625
// The above injection won't work for texImage2D and texSubImage2D, which have multiple overloads.
@@ -671,4 +671,4 @@ setTimeout = (fn, delay) => {
671671
// Backwards compatibility with previously compiled code. Don't call this anymore!
672672
function cpuprofiler_add_hooks() { emscriptenCpuProfiler.initialize(); }
673673

674-
if (typeof Module !== 'undefined' && typeof document !== 'undefined') emscriptenCpuProfiler.initialize();
674+
if (typeof Module != 'undefined' && typeof document != 'undefined') emscriptenCpuProfiler.initialize();

src/deterministic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Math.random = () => {
1111
};
1212
var TIME = 10000;
1313
Date.now = () => TIME++;
14-
if (typeof performance === 'object') performance.now = Date.now;
14+
if (typeof performance == 'object') performance.now = Date.now;
1515
if (ENVIRONMENT_IS_NODE) process['hrtime'] = Date.now;
1616

1717
if (!Module) Module = {};

src/embind/embind.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ var LibraryEmbind = {
568568
var isUnsignedType = (name.includes('unsigned'));
569569
var checkAssertions = (value, toTypeName) => {
570570
#if ASSERTIONS
571-
if (typeof value !== "number" && typeof value !== "boolean") {
571+
if (typeof value != "number" && typeof value != "boolean") {
572572
throw new TypeError('Cannot convert "' + _embind_repr(value) + '" to ' + toTypeName);
573573
}
574574
if (value < minRange || value > maxRange) {
@@ -622,7 +622,7 @@ var LibraryEmbind = {
622622
return value;
623623
},
624624
'toWireType': function (destructors, value) {
625-
if (typeof value !== "bigint") {
625+
if (typeof value != "bigint") {
626626
throw new TypeError('Cannot convert "' + _embind_repr(value) + '" to ' + this.name);
627627
}
628628
if (value < minRange || value > maxRange) {
@@ -653,7 +653,7 @@ var LibraryEmbind = {
653653
},
654654
'toWireType': function(destructors, value) {
655655
#if ASSERTIONS
656-
if (typeof value !== "number" && typeof value !== "boolean") {
656+
if (typeof value != "number" && typeof value != "boolean") {
657657
throw new TypeError('Cannot convert "' + _embind_repr(value) + '" to ' + this.name);
658658
}
659659
#endif
@@ -726,7 +726,7 @@ var LibraryEmbind = {
726726
}
727727

728728
var getLength;
729-
var valueIsOfTypeString = (typeof value === 'string');
729+
var valueIsOfTypeString = (typeof value == 'string');
730730

731731
if (!(valueIsOfTypeString || value instanceof Uint8Array || value instanceof Uint8ClampedArray || value instanceof Int8Array)) {
732732
throwBindingError('Cannot pass non-string to std::string');
@@ -827,7 +827,7 @@ var LibraryEmbind = {
827827
return str;
828828
},
829829
'toWireType': function(destructors, value) {
830-
if (!(typeof value === 'string')) {
830+
if (!(typeof value == 'string')) {
831831
throwBindingError('Cannot pass non-string to C++ string type ' + name);
832832
}
833833

@@ -1175,7 +1175,7 @@ var LibraryEmbind = {
11751175
}
11761176

11771177
var fp = makeDynCaller();
1178-
if (typeof fp !== "function") {
1178+
if (typeof fp != "function") {
11791179
throwBindingError("unknown function pointer with signature " + signature + ": " + rawFunction);
11801180
}
11811181
return fp;

src/embind/emval.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,34 +209,34 @@ var LibraryEmVal = {
209209

210210
#if DYNAMIC_EXECUTION == 0
211211
$emval_get_global: function() {
212-
if (typeof globalThis === 'object') {
212+
if (typeof globalThis == 'object') {
213213
return globalThis;
214214
}
215215
function testGlobal(obj) {
216216
obj['$$$embind_global$$$'] = obj;
217-
var success = typeof $$$embind_global$$$ === 'object' && obj['$$$embind_global$$$'] === obj;
217+
var success = typeof $$$embind_global$$$ == 'object' && obj['$$$embind_global$$$'] == obj;
218218
if (!success) {
219219
delete obj['$$$embind_global$$$'];
220220
}
221221
return success;
222222
}
223-
if (typeof $$$embind_global$$$ === 'object') {
223+
if (typeof $$$embind_global$$$ == 'object') {
224224
return $$$embind_global$$$;
225225
}
226-
if (typeof global === 'object' && testGlobal(global)) {
226+
if (typeof global == 'object' && testGlobal(global)) {
227227
$$$embind_global$$$ = global;
228-
} else if (typeof self === 'object' && testGlobal(self)) {
228+
} else if (typeof self == 'object' && testGlobal(self)) {
229229
$$$embind_global$$$ = self; // This works for both "window" and "self" (Web Workers) global objects
230230
}
231-
if (typeof $$$embind_global$$$ === 'object') {
231+
if (typeof $$$embind_global$$$ == 'object') {
232232
return $$$embind_global$$$;
233233
}
234234
throw Error('unable to get global object.');
235235
},
236236
#else
237237
// appease jshint (technically this code uses eval)
238238
$emval_get_global: function() {
239-
if (typeof globalThis === 'object') {
239+
if (typeof globalThis == 'object') {
240240
return globalThis;
241241
}
242242
return (function(){
@@ -491,13 +491,13 @@ var LibraryEmVal = {
491491
_emval_is_number__deps: ['$Emval'],
492492
_emval_is_number: function(handle) {
493493
handle = Emval.toValue(handle);
494-
return typeof handle === 'number';
494+
return typeof handle == 'number';
495495
},
496496

497497
_emval_is_string__deps: ['$Emval'],
498498
_emval_is_string: function(handle) {
499499
handle = Emval.toValue(handle);
500-
return typeof handle === 'string';
500+
return typeof handle == 'string';
501501
},
502502

503503
_emval_in__deps: ['$Emval'],

src/emrun_postjs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7-
if (typeof window === "object" && (typeof ENVIRONMENT_IS_PTHREAD === 'undefined' || !ENVIRONMENT_IS_PTHREAD)) {
7+
if (typeof window == "object" && (typeof ENVIRONMENT_IS_PTHREAD == 'undefined' || !ENVIRONMENT_IS_PTHREAD)) {
88
var emrun_register_handlers = function() {
99
// When C code exit()s, we may still have remaining stdout and stderr messages in flight. In that case, we can't close
1010
// the browser until all those XHRs have finished, so the following state variables track that all communication is done,
@@ -66,5 +66,5 @@ if (typeof window === "object" && (typeof ENVIRONMENT_IS_PTHREAD === 'undefined'
6666
http.send(data); // XXX this does not work in workers, for some odd reason (issue #2681)
6767
};
6868

69-
if (typeof Module !== 'undefined' && typeof document !== 'undefined') emrun_register_handlers();
69+
if (typeof Module != 'undefined' && typeof document != 'undefined') emrun_register_handlers();
7070
}

src/emrun_prejs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
// Route URL GET parameters to argc+argv
8-
if (typeof window === 'object') {
8+
if (typeof window == 'object') {
99
Module['arguments'] = window.location.search.substr(1).trim().split('&');
1010
// If no args were passed arguments = [''], in which case kill the single empty string.
1111
if (!Module['arguments'][0]) {

0 commit comments

Comments
 (0)