Skip to content

Commit 6634b9a

Browse files
committed
Avoid throwing of plain JS strings. NFC
Replaced with `assert`, `abort` or `throw new Error` accordingly. We almost never want to be throwing plain values because we don't end up with a stack trace in that case.
1 parent 74a4aa7 commit 6634b9a

File tree

76 files changed

+364
-365
lines changed

Some content is hidden

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

76 files changed

+364
-365
lines changed

src/cpuprofiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ var emscriptenCpuProfiler = {
655655
case 9: glCtx[f] = (a1, a2, a3, a4, a5, a6, a7, a8, a9) => { this.enterSection(section); var ret = glCtx[realf](a1, a2, a3, a4, a5, a6, a7, a8, a9); this.endSection(section); return ret; }; break;
656656
case 10: glCtx[f] = (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) => { this.enterSection(section); var ret = glCtx[realf](a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); this.endSection(section); return ret; }; break;
657657
case 11: glCtx[f] = (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) => { this.enterSection(section); var ret = glCtx[realf](a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11); this.endSection(section); return ret; }; break;
658-
default: throw 'hookWebGL failed! Unexpected length ' + glCtx[realf].length;
658+
default: throw new Error('hookWebGL failed! Unexpected length ' + glCtx[realf].length);
659659
}
660660
},
661661

src/jsifier.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ function handleI64Signatures(symbol, snippet, sig, i53abi) {
316316
// For functions that where we need to mutate the return value, we
317317
// also need to wrap the body in an inner function.
318318
if (oneliner) {
319+
// Special case for abort(), this a noreturn function and but closure
320+
// compiler doesn't have a way to express that, so it complains if we
321+
// do `BigInt(abort(..))`.
322+
if (body.startsWith('abort(')) {
323+
return snippet;
324+
}
319325
if (argConversions) {
320326
return `${async_}(${args}) => {
321327
${argConversions}

src/lib/libaddfunction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ addToLibrary({
159159
if (!(err instanceof RangeError)) {
160160
throw err;
161161
}
162-
throw 'Unable to grow wasm table. Set ALLOW_TABLE_GROWTH.';
162+
abort('Unable to grow wasm table. Set ALLOW_TABLE_GROWTH.');
163163
}
164164
#endif
165165
},

src/lib/libasync.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ addToLibrary({
8484
!isAsyncifyImport &&
8585
!changedToDisabled &&
8686
!ignoredInvoke) {
87-
throw new Error(`import ${x} was not in ASYNCIFY_IMPORTS, but changed the state`);
87+
abort(`import ${x} was not in ASYNCIFY_IMPORTS, but changed the state`);
8888
}
8989
}
9090
};
@@ -664,19 +664,19 @@ addToLibrary({
664664
},
665665
#else // ASYNCIFY
666666
emscripten_sleep: () => {
667-
throw 'Please compile your program with async support in order to use asynchronous operations like emscripten_sleep';
667+
abort('Please compile your program with async support in order to use asynchronous operations like emscripten_sleep');
668668
},
669669
emscripten_wget: (url, file) => {
670-
throw 'Please compile your program with async support in order to use asynchronous operations like emscripten_wget';
670+
abort('Please compile your program with async support in order to use asynchronous operations like emscripten_wget');
671671
},
672672
emscripten_wget_data: (url, pbuffer, pnum, perror) => {
673-
throw 'Please compile your program with async support in order to use asynchronous operations like emscripten_wget_data';
673+
abort('Please compile your program with async support in order to use asynchronous operations like emscripten_wget_data');
674674
},
675675
emscripten_scan_registers: (func) => {
676-
throw 'Please compile your program with async support in order to use asynchronous operations like emscripten_scan_registers';
676+
abort('Please compile your program with async support in order to use asynchronous operations like emscripten_scan_registers');
677677
},
678678
emscripten_fiber_swap: (oldFiber, newFiber) => {
679-
throw 'Please compile your program with async support in order to use asynchronous operations like emscripten_fiber_swap';
679+
abort('Please compile your program with async support in order to use asynchronous operations like emscripten_fiber_swap');
680680
},
681681
#endif // ASYNCIFY
682682
});

src/lib/libbrowser.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,11 @@ var LibraryBrowser = {
374374
delta *= 80;
375375
break;
376376
default:
377-
throw 'unrecognized mouse wheel delta mode: ' + event.deltaMode;
377+
abort('unrecognized mouse wheel delta mode: ' + event.deltaMode);
378378
}
379379
break;
380380
default:
381-
throw 'unrecognized mouse wheel event: ' + event.type;
381+
abort('unrecognized mouse wheel event: ' + event.type);
382382
}
383383
return delta;
384384
},
@@ -804,7 +804,7 @@ var LibraryBrowser = {
804804
#if BUILD_AS_WORKER
805805
emscripten_worker_respond_provisionally__proxy: 'sync',
806806
emscripten_worker_respond_provisionally: (data, size) => {
807-
if (workerResponded) throw 'already responded with final response!';
807+
if (workerResponded) abort('already responded with final response!');
808808
var transferObject = {
809809
'callbackId': workerCallbackId,
810810
'finalResponse': false,
@@ -819,7 +819,7 @@ var LibraryBrowser = {
819819

820820
emscripten_worker_respond__proxy: 'sync',
821821
emscripten_worker_respond: (data, size) => {
822-
if (workerResponded) throw 'already responded with final response!';
822+
if (workerResponded) abort('already responded with final response!');
823823
workerResponded = true;
824824
var transferObject = {
825825
'callbackId': workerCallbackId,

src/lib/libc_preprocessor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ addToLibrary({
204204
}
205205
// else a number:
206206
#if ASSERTIONS
207-
if (tokens[i] == ')') throw 'Parsing failure, mismatched parentheses in parsing!' + tokens.toString();
207+
assert(tokens[i] !== ')', 'Parsing failure, mismatched parentheses in parsing!' + tokens.toString());
208208
assert(operatorAndPriority == -1);
209209
#endif
210210
var num = Number(tokens[i]);

src/lib/libcore.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,12 +1426,10 @@ addToLibrary({
14261426

14271427
// We never free the return values of this function so we need to allocate
14281428
// using builtin_malloc to avoid LSan reporting these as leaks.
1429-
emscripten_get_compiler_setting__noleakcheck: true,
14301429
#if RETAIN_COMPILER_SETTINGS
1430+
emscripten_get_compiler_setting__noleakcheck: true,
14311431
emscripten_get_compiler_setting__deps: ['$stringToNewUTF8'],
1432-
#endif
14331432
emscripten_get_compiler_setting: (name) => {
1434-
#if RETAIN_COMPILER_SETTINGS
14351433
name = UTF8ToString(name);
14361434

14371435
var ret = getCompilerSetting(name);
@@ -1441,10 +1439,10 @@ addToLibrary({
14411439
var fullret = cache[name];
14421440
if (fullret) return fullret;
14431441
return cache[name] = stringToNewUTF8(ret);
1442+
},
14441443
#else
1445-
throw 'You must build with -sRETAIN_COMPILER_SETTINGS for getCompilerSetting or emscripten_get_compiler_setting to work';
1444+
emscripten_get_compiler_setting: (name) => abort('You must build with -sRETAIN_COMPILER_SETTINGS for getCompilerSetting or emscripten_get_compiler_setting to work'),
14461445
#endif
1447-
},
14481446

14491447
emscripten_has_asyncify: () => {{{ ASYNCIFY }}},
14501448

src/lib/libfs.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ FS.staticInit();`;
13511351
opts.flags = opts.flags || {{{ cDefs.O_RDONLY }}};
13521352
opts.encoding = opts.encoding || 'binary';
13531353
if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') {
1354-
throw new Error(`Invalid encoding type "${opts.encoding}"`);
1354+
abort(`Invalid encoding type "${opts.encoding}"`);
13551355
}
13561356
var stream = FS.open(path, opts.flags);
13571357
var stat = FS.stat(path);
@@ -1373,7 +1373,7 @@ FS.staticInit();`;
13731373
if (ArrayBuffer.isView(data)) {
13741374
FS.write(stream, data, 0, data.byteLength, undefined, opts.canOwn);
13751375
} else {
1376-
throw new Error('Unsupported data type');
1376+
abort('Unsupported data type');
13771377
}
13781378
FS.close(stream);
13791379
},
@@ -1706,7 +1706,7 @@ FS.staticInit();`;
17061706
dbg(`forceLoadFile: ${obj.url}`)
17071707
#endif
17081708
if (typeof XMLHttpRequest != 'undefined') {
1709-
throw new Error("Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.");
1709+
abort("Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.");
17101710
} else { // Command-line.
17111711
try {
17121712
obj.contents = readBinary(obj.url);
@@ -1749,7 +1749,7 @@ FS.staticInit();`;
17491749
var xhr = new XMLHttpRequest();
17501750
xhr.open('HEAD', url, false);
17511751
xhr.send(null);
1752-
if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) throw new Error("Couldn't load " + url + ". Status: " + xhr.status);
1752+
if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) abort("Couldn't load " + url + ". Status: " + xhr.status);
17531753
var datalength = Number(xhr.getResponseHeader("Content-length"));
17541754
var header;
17551755
var hasByteServing = (header = xhr.getResponseHeader("Accept-Ranges")) && header === "bytes";
@@ -1765,8 +1765,8 @@ FS.staticInit();`;
17651765

17661766
// Function to get a range from the remote URL.
17671767
var doXHR = (from, to) => {
1768-
if (from > to) throw new Error("invalid range (" + from + ", " + to + ") or no bytes requested!");
1769-
if (to > datalength-1) throw new Error("only " + datalength + " bytes available! programmer error!");
1768+
if (from > to) abort("invalid range (" + from + ", " + to + ") or no bytes requested!");
1769+
if (to > datalength-1) abort("only " + datalength + " bytes available! programmer error!");
17701770

17711771
// TODO: Use mozResponseArrayBuffer, responseStream, etc. if available.
17721772
var xhr = new XMLHttpRequest();
@@ -1780,7 +1780,7 @@ FS.staticInit();`;
17801780
}
17811781

17821782
xhr.send(null);
1783-
if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) throw new Error("Couldn't load " + url + ". Status: " + xhr.status);
1783+
if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) abort("Couldn't load " + url + ". Status: " + xhr.status);
17841784
if (xhr.response !== undefined) {
17851785
return new Uint8Array(/** @type{Array<number>} */(xhr.response || []));
17861786
}
@@ -1794,7 +1794,7 @@ FS.staticInit();`;
17941794
if (typeof lazyArray.chunks[chunkNum] == 'undefined') {
17951795
lazyArray.chunks[chunkNum] = doXHR(start, end);
17961796
}
1797-
if (typeof lazyArray.chunks[chunkNum] == 'undefined') throw new Error('doXHR failed!');
1797+
if (typeof lazyArray.chunks[chunkNum] == 'undefined') abort('doXHR failed!');
17981798
return lazyArray.chunks[chunkNum];
17991799
});
18001800

@@ -1825,7 +1825,7 @@ FS.staticInit();`;
18251825
}
18261826

18271827
if (typeof XMLHttpRequest != 'undefined') {
1828-
if (!ENVIRONMENT_IS_WORKER) throw 'Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc';
1828+
if (!ENVIRONMENT_IS_WORKER) abort('Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc');
18291829
var lazyArray = new LazyUint8Array();
18301830
var properties = { isDevice: false, contents: lazyArray };
18311831
} else {

src/lib/libglemu.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ var LibraryGLEmulation = {
592592
dbg(`Info: ${JSON.stringify(GL.shaderInfos[shader])}`);
593593
dbg(`Original source: ${GL.shaderOriginalSources[shader]}`);
594594
dbg(`Source: ${GL.shaderSources[shader]}`);
595-
throw 'Shader compilation halt';
595+
abort('Shader compilation halt');
596596
}
597597
#endif
598598
};
@@ -3062,7 +3062,7 @@ var LibraryGLEmulation = {
30623062
emulatedElementArrayBuffer = true;
30633063
}
30643064
} else if (GLImmediate.mode > 6) { // above GL_TRIANGLE_FAN are the non-GL ES modes
3065-
if (GLImmediate.mode != 7) throw 'unsupported immediate mode ' + GLImmediate.mode; // GL_QUADS
3065+
if (GLImmediate.mode != 7) abort('unsupported immediate mode ' + GLImmediate.mode); // GL_QUADS
30663066
// GLImmediate.firstVertex is the first vertex we want. Quad indexes are
30673067
// in the pattern 0 1 2, 0 2 3, 4 5 6, 4 6 7, so we need to look at
30683068
// index firstVertex * 1.5 to see it. Then since indexes are 2 bytes
@@ -3218,7 +3218,7 @@ var LibraryGLEmulation = {
32183218
glTexCoord2fv: (v) =>
32193219
_glTexCoord2i({{{ makeGetValue('v', '0', 'float') }}}, {{{ makeGetValue('v', '4', 'float') }}}),
32203220

3221-
glTexCoord4f: () => { throw 'glTexCoord4f: TODO' },
3221+
glTexCoord4f: () => { abort('glTexCoord4f: TODO') },
32223222

32233223
glColor4f: (r, g, b, a) => {
32243224
r = Math.max(Math.min(r, 1), 0);
@@ -3750,9 +3750,9 @@ var LibraryGLEmulation = {
37503750
},
37513751
glRotatef: 'glRotated',
37523752

3753-
glDrawBuffer: () => { throw 'glDrawBuffer: TODO' },
3753+
glDrawBuffer: () => { abort('glDrawBuffer: TODO') },
37543754
#if MAX_WEBGL_VERSION < 2
3755-
glReadBuffer: () => { throw 'glReadBuffer: TODO' },
3755+
glReadBuffer: () => { abort('glReadBuffer: TODO') },
37563756
#endif
37573757

37583758
glClipPlane: (pname, param) => {
@@ -3800,7 +3800,7 @@ var LibraryGLEmulation = {
38003800
// multiply position with current modelviewmatrix
38013801
GLImmediate.matrixLib.mat4.multiplyVec4(GLImmediate.matrix[0], GLEmulation.lightPosition[lightId]);
38023802
} else {
3803-
throw 'glLightfv: TODO: ' + pname;
3803+
abort('glLightfv: TODO: ' + pname);
38043804
}
38053805
}
38063806
},
@@ -3809,7 +3809,7 @@ var LibraryGLEmulation = {
38093809
if (pname == 0x0B52) { // GL_LIGHT_MODEL_TWO_SIDE
38103810
GLEmulation.lightModelTwoSide = (param != 0) ? true : false;
38113811
} else {
3812-
throw 'glLightModelf: TODO: ' + pname;
3812+
abort('glLightModelf: TODO: ' + pname);
38133813
}
38143814
},
38153815

@@ -3820,12 +3820,12 @@ var LibraryGLEmulation = {
38203820
GLEmulation.lightModelAmbient[2] = {{{ makeGetValue('param', '8', 'float') }}};
38213821
GLEmulation.lightModelAmbient[3] = {{{ makeGetValue('param', '12', 'float') }}};
38223822
} else {
3823-
throw 'glLightModelfv: TODO: ' + pname;
3823+
abort('glLightModelfv: TODO: ' + pname);
38243824
}
38253825
},
38263826

38273827
glMaterialfv: (face, pname, param) => {
3828-
if ((face != 0x0404) && (face != 0x0408)) { throw 'glMaterialfv: TODO' + face; } // only GL_FRONT and GL_FRONT_AND_BACK supported
3828+
if ((face != 0x0404) && (face != 0x0408)) { abort('glMaterialfv: TODO' + face); } // only GL_FRONT and GL_FRONT_AND_BACK supported
38293829

38303830
if (pname == 0x1200) { // GL_AMBIENT
38313831
GLEmulation.materialAmbient[0] = {{{ makeGetValue('param', '0', 'float') }}};
@@ -3845,22 +3845,22 @@ var LibraryGLEmulation = {
38453845
} else if (pname == 0x1601) { // GL_SHININESS
38463846
GLEmulation.materialShininess[0] = {{{ makeGetValue('param', '0', 'float') }}};
38473847
} else {
3848-
throw 'glMaterialfv: TODO: ' + pname;
3848+
abort('glMaterialfv: TODO: ' + pname);
38493849
}
38503850
},
38513851

3852-
glTexGeni: (coord, pname, param) => { throw 'glTexGeni: TODO' },
3853-
glTexGenfv: (coord, pname, param) => { throw 'glTexGenfv: TODO' },
3852+
glTexGeni: (coord, pname, param) => abort('glTexGeni: TODO'),
3853+
glTexGenfv: (coord, pname, param) => abort('glTexGenfv: TODO'),
38543854
glTexEnvi: (target, pname, params) => warnOnce('glTexEnvi: TODO'),
38553855
glTexEnvf: (target, pname, params) => warnOnce('glTexEnvf: TODO'),
38563856
glTexEnvfv: (target, pname, params) => warnOnce('glTexEnvfv: TODO'),
38573857

3858-
glGetTexEnviv: (target, pname, param) => { throw 'GL emulation not initialized!'; },
3859-
glGetTexEnvfv: (target, pname, param) => { throw 'GL emulation not initialized!'; },
3858+
glGetTexEnviv: (target, pname, param) => abort('GL emulation not initialized!'),
3859+
glGetTexEnvfv: (target, pname, param) => abort('GL emulation not initialized!'),
38603860

3861-
glTexImage1D: (target, level, internalformat, width, border, format, type, data) => { throw 'glTexImage1D: TODO' },
3862-
glTexCoord3f: (target, level, internalformat, width, border, format, type, data) => { throw 'glTexCoord3f: TODO' },
3863-
glGetTexLevelParameteriv: (target, level, pname, params) => { throw 'glGetTexLevelParameteriv: TODO' },
3861+
glTexImage1D: (target, level, internalformat, width, border, format, type, data) => abort('glTexImage1D: TODO'),
3862+
glTexCoord3f: (target, level, internalformat, width, border, format, type, data) => abort('glTexCoord3f: TODO'),
3863+
glGetTexLevelParameteriv: (target, level, pname, params) => abort('glGetTexLevelParameteriv: TODO'),
38643864

38653865
glShadeModel: () => warnOnce('TODO: glShadeModel'),
38663866

0 commit comments

Comments
 (0)