Skip to content

Commit 306c32f

Browse files
authored
[jslib] Minor cleanups to JS library files. NFC (#27441)
Similar to #27425 but applied to the whole JS library - Replace explicit zero checks (`=== 0`, `!== 0`) with implicit truthy/falsy checks (`!x`, `x`). - Replace `.length === 0` checks with `!arr.length`. - Simplify null/undefined/zero check in `src/lib/libccall.js` to `if (str)`. - Use `for (var byte of data)` loop in `src/lib/libbrowser.js`.
1 parent eb0aefe commit 306c32f

53 files changed

Lines changed: 167 additions & 164 deletions

Some content is hidden

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

src/lib/libasync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ addToLibrary({
232232
#endif
233233
if (Asyncify.currData &&
234234
Asyncify.state === Asyncify.State.Unwinding &&
235-
Asyncify.exportCallStack.length === 0) {
235+
!Asyncify.exportCallStack.length) {
236236
// We just finished unwinding.
237237
// Be sure to set the state before calling any other functions to avoid
238238
// possible infinite recursion here (For example in debug pthread builds
@@ -548,7 +548,7 @@ addToLibrary({
548548

549549
var entryPoint = {{{ makeGetValue('newFiber', C_STRUCTS.emscripten_fiber_s.entry, '*') }}};
550550

551-
if (entryPoint !== 0) {
551+
if (entryPoint) {
552552
#if STACK_OVERFLOW_CHECK
553553
writeStackCookie();
554554
#endif

src/lib/libbootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
assert(false, 'libbootstrap.js only designed for use with BOOTSTRAPPING_STRUCT_INFO')
1212
#endif
1313

14-
assert(Object.keys(LibraryManager.library).length === 0);
14+
assert(!Object.keys(LibraryManager.library).length);
1515
addToLibrary({
1616
$callRuntimeCallbacks: () => {},
1717

src/lib/libbrowser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ var LibraryBrowser = {
107107
var ret = '';
108108
var leftchar = 0;
109109
var leftbits = 0;
110-
for (var i = 0; i < data.length; i++) {
111-
leftchar = (leftchar << 8) | data[i];
110+
for (var byte of data) {
111+
leftchar = (leftchar << 8) | byte;
112112
leftbits += 8;
113113
while (leftbits >= 6) {
114114
var curr = (leftchar >> (leftbits-6)) & 0x3f;
@@ -601,7 +601,7 @@ var LibraryBrowser = {
601601
}
602602
#endif
603603
#if ASSERTIONS
604-
assert(runDependencies === 0, 'async_load_script must be run when no other dependencies are active');
604+
assert(!runDependencies, 'async_load_script must be run when no other dependencies are active');
605605
#endif
606606
{{{ runtimeKeepalivePush() }}}
607607

src/lib/libccall.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ addToLibrary({
7070
for (var i = 0; i < args.length; i++) {
7171
var converter = toC[argTypes[i]];
7272
if (converter) {
73-
if (stack === 0) stack = stackSave();
73+
if (!stack) stack = stackSave();
7474
cArgs[i] = converter(args[i]);
7575
} else {
7676
cArgs[i] = args[i];
@@ -86,7 +86,7 @@ addToLibrary({
8686
#if ASYNCIFY == 1
8787
runtimeKeepalivePop();
8888
#endif
89-
if (stack !== 0) stackRestore(stack);
89+
if (stack) stackRestore(stack);
9090
return convertReturnValue(ret);
9191
}
9292
#if ASYNCIFY

src/lib/libcore.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,10 @@ addToLibrary({
764764
var v4part = '';
765765
// check if the 10 high-order bytes are all zeros (first 5 words)
766766
for (i = 0; i < 5; i++) {
767-
if (parts[i] !== 0) { hasipv4 = false; break; }
767+
if (parts[i]) {
768+
hasipv4 = false;
769+
break;
770+
}
768771
}
769772

770773
if (hasipv4) {
@@ -777,7 +780,7 @@ addToLibrary({
777780
return str;
778781
}
779782
// IPv4-compatible IPv6 address if 16-bit value (bytes 11 and 12) == 0x0000 (6th word)
780-
if (parts[5] === 0) {
783+
if (!parts[5]) {
781784
str = '::';
782785
// special case IPv6 addresses
783786
if (v4part === '0.0.0.0') v4part = ''; // any/unspecified address
@@ -791,7 +794,7 @@ addToLibrary({
791794

792795
// first run to find the longest contiguous zero words
793796
for (word = 0; word < 8; word++) {
794-
if (parts[word] === 0) {
797+
if (!parts[word]) {
795798
if (word - lastzero > 1) {
796799
len = 0;
797800
}
@@ -807,10 +810,10 @@ addToLibrary({
807810
for (word = 0; word < 8; word++) {
808811
if (longest > 1) {
809812
// compress contiguous zeros - to produce '::'
810-
if (parts[word] === 0 && word >= zstart && word < (zstart + longest) ) {
813+
if (!parts[word] && word >= zstart && word < (zstart + longest) ) {
811814
if (word === zstart) {
812815
str += ':';
813-
if (zstart === 0) str += ':'; //leading zeros case
816+
if (!zstart) str += ':'; //leading zeros case
814817
}
815818
continue;
816819
}
@@ -844,7 +847,7 @@ addToLibrary({
844847
var pathLen = salen - {{{ C_STRUCTS.sockaddr_un.sun_path }}};
845848
var path = '';
846849
if (pathLen > 0) {
847-
if (HEAPU8[pathStart] === 0) {
850+
if (!HEAPU8[pathStart]) {
848851
path = '\0' + UTF8ToString(pathStart + 1, pathLen - 1, /*ignoreNul=*/true);
849852
} else {
850853
// A pathname address is NUL-terminated; stop at the first NUL.
@@ -894,7 +897,7 @@ addToLibrary({
894897
// terminator), any other path is written NUL-terminated. An empty path
895898
// is the unnamed address (family only).
896899
addr ||= '';
897-
var abstract = addr.charCodeAt(0) === 0;
900+
var abstract = !addr.charCodeAt(0);
898901
// Pathname addresses include the trailing NUL in the reported length;
899902
// abstract addresses do not; an empty address is family-only (unnamed).
900903
var bytes = addr ? lengthBytesUTF8(addr) + (abstract ? 0 : 1) : 0;
@@ -1067,10 +1070,10 @@ addToLibrary({
10671070

10681071
// If type or proto are set to zero in hints we should really be returning multiple addrinfo values, but for
10691072
// now default to a TCP STREAM socket so we can at least return a sensible addrinfo given NULL hints.
1070-
if (proto === 0) {
1073+
if (!proto) {
10711074
proto = {{{ cDefs.IPPROTO_TCP }}};
10721075
}
1073-
if (type === 0) {
1076+
if (!type) {
10741077
type = {{{ cDefs.SOCK_STREAM }}};
10751078
}
10761079

@@ -1081,14 +1084,14 @@ addToLibrary({
10811084
{{{ cDefs.AI_NUMERICSERV }}}|{{{ cDefs.AI_V4MAPPED }}}|{{{ cDefs.AI_ALL }}}|{{{ cDefs.AI_ADDRCONFIG }}})) {
10821085
return {{{ cDefs.EAI_BADFLAGS }}};
10831086
}
1084-
if (hint !== 0 && ({{{ makeGetValue('hint', C_STRUCTS.addrinfo.ai_flags, 'i32') }}} & {{{ cDefs.AI_CANONNAME }}}) && !node) {
1087+
if (hint && ({{{ makeGetValue('hint', C_STRUCTS.addrinfo.ai_flags, 'i32') }}} & {{{ cDefs.AI_CANONNAME }}}) && !node) {
10851088
return {{{ cDefs.EAI_BADFLAGS }}};
10861089
}
10871090
if (flags & {{{ cDefs.AI_ADDRCONFIG }}}) {
10881091
// TODO
10891092
return {{{ cDefs.EAI_NONAME }}};
10901093
}
1091-
if (type !== 0 && type !== {{{ cDefs.SOCK_STREAM }}} && type !== {{{ cDefs.SOCK_DGRAM }}}) {
1094+
if (type && type !== {{{ cDefs.SOCK_STREAM }}} && type !== {{{ cDefs.SOCK_DGRAM }}}) {
10921095
return {{{ cDefs.EAI_SOCKTYPE }}};
10931096
}
10941097
if (family !== {{{ cDefs.AF_UNSPEC }}} && family !== {{{ cDefs.AF_INET }}} && family !== {{{ cDefs.AF_INET6 }}}) {
@@ -1113,7 +1116,7 @@ addToLibrary({
11131116
if (family === {{{ cDefs.AF_UNSPEC }}}) {
11141117
family = {{{ cDefs.AF_INET }}};
11151118
}
1116-
if ((flags & {{{ cDefs.AI_PASSIVE }}}) === 0) {
1119+
if (!(flags & {{{ cDefs.AI_PASSIVE }}})) {
11171120
if (family === {{{ cDefs.AF_INET }}}) {
11181121
addr = _htonl({{{ cDefs.INADDR_LOOPBACK }}});
11191122
} else {
@@ -1263,7 +1266,7 @@ addToLibrary({
12631266
// to add extra entries from /etc/protocols if desired - though not sure if that'd actually be useful.
12641267
var list = Protocols.list;
12651268
var map = Protocols.map;
1266-
if (list.length === 0) {
1269+
if (!list.length) {
12671270
var entry = allocprotoent('tcp', 6, ['TCP']);
12681271
list.push(entry);
12691272
map['tcp'] = map['6'] = entry;
@@ -1739,7 +1742,7 @@ addToLibrary({
17391742
var parts = trace.split('\n');
17401743
for (var i = 0; i < parts.length; i++) {
17411744
var ret = {{{ makeDynCall('iii', 'func') }}}(0, arg);
1742-
if (ret !== 0) return;
1745+
if (ret) return;
17431746
}
17441747
},
17451748

@@ -2364,7 +2367,7 @@ addToLibrary({
23642367
assert(id, 'addRunDependency requires an ID')
23652368
assert(!runDependencyTracking[id]);
23662369
runDependencyTracking[id] = 1;
2367-
if (runDependencyWatcher === null && globalThis.setInterval) {
2370+
if (!runDependencyWatcher && globalThis.setInterval) {
23682371
// Check for missing dependencies every few seconds
23692372
runDependencyWatcher = setInterval(() => {
23702373
if (ABORT) {

src/lib/libdylink.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ var LibraryDylink = {
478478

479479
if (binary instanceof WebAssembly.Module) {
480480
var dylinkSection = WebAssembly.Module.customSections(binary, 'dylink.0');
481-
failIf(dylinkSection.length === 0, 'need dylink section');
481+
failIf(!dylinkSection.length, 'need dylink section');
482482
binary = new Uint8Array(dylinkSection[0]);
483483
end = binary.length
484484
} else {
@@ -490,7 +490,7 @@ var LibraryDylink = {
490490
#endif
491491
failIf(!magicNumberFound, 'need to see wasm magic number'); // \0asm
492492
// we should see the dylink custom section right after the magic number and wasm version
493-
failIf(binary[8] !== 0, 'need the dylink section to be first')
493+
failIf(binary[8], 'need the dylink section to be first')
494494
offset = 9;
495495
var section_size = getLEB(); // section size
496496
end = offset + section_size;

src/lib/libembind_gen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ var LibraryEmbind = {
311311
out.push(' value: T;\n}\n');
312312
}
313313
out.push(`export type ${this.name} = `);
314-
if (this.items.length === 0) {
314+
if (!this.items.length) {
315315
out.push('never/* Empty Enumerator */');
316316
} else {
317317
const outItems = [];
@@ -715,7 +715,7 @@ var LibraryEmbind = {
715715
setter,
716716
setterContext) {
717717
fieldName = AsciiToString(fieldName);
718-
const readonly = setter === 0;
718+
const readonly = !setter;
719719
if (!(readonly || getterReturnType === setterArgumentType)) {
720720
throw new error('Mismatched getter and setter types are not supported.');
721721
}

src/lib/libembind_shared.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ var LibraryEmbindShared = {
195195

196196
$getEnumValueType(rawValueType) {
197197
// This must match the values of enum_value_type in wire.h
198-
return rawValueType === 0 ? 'object' : (rawValueType === 1 ? 'number' : 'string');
198+
return !rawValueType ? 'object' : (rawValueType === 1 ? 'number' : 'string');
199199
},
200200

201201
$getRequiredArgCount(argTypes) {

src/lib/libeventloop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ LibraryJSEventLoop = {
290290
fakeRequestAnimationFrame(func) {
291291
// try to keep 60fps between calls to here
292292
var now = Date.now();
293-
if (MainLoop.nextRAF === 0) {
293+
if (!MainLoop.nextRAF) {
294294
MainLoop.nextRAF = now + 1000/60;
295295
} else {
296296
while (now + 2 >= MainLoop.nextRAF) { // fudge a little, to avoid timer jitter causing us to do lots of delay:0

src/lib/libexceptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ var LibraryExceptions = {
275275
// type of the thrown object. Find one which matches, and
276276
// return the type of the catch block which should be called.
277277
for (var caughtType of args) {
278-
if (caughtType === 0 || caughtType === thrownType) {
278+
if (!caughtType || caughtType === thrownType) {
279279
// Catch all clause matched or exactly the same type is caught
280280
break;
281281
}

0 commit comments

Comments
 (0)