Skip to content

Commit cda16e7

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 8d9c908 commit cda16e7

37 files changed

+217
-208
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
@@ -1430,12 +1430,10 @@ addToLibrary({
14301430

14311431
// We never free the return values of this function so we need to allocate
14321432
// using builtin_malloc to avoid LSan reporting these as leaks.
1433-
emscripten_get_compiler_setting__noleakcheck: true,
14341433
#if RETAIN_COMPILER_SETTINGS
1434+
emscripten_get_compiler_setting__noleakcheck: true,
14351435
emscripten_get_compiler_setting__deps: ['$stringToNewUTF8'],
1436-
#endif
14371436
emscripten_get_compiler_setting: (name) => {
1438-
#if RETAIN_COMPILER_SETTINGS
14391437
name = UTF8ToString(name);
14401438

14411439
var ret = getCompilerSetting(name);
@@ -1445,10 +1443,10 @@ addToLibrary({
14451443
var fullret = cache[name];
14461444
if (fullret) return fullret;
14471445
return cache[name] = stringToNewUTF8(ret);
1446+
},
14481447
#else
1449-
throw 'You must build with -sRETAIN_COMPILER_SETTINGS for getCompilerSetting or emscripten_get_compiler_setting to work';
1448+
emscripten_get_compiler_setting: (name) => abort('You must build with -sRETAIN_COMPILER_SETTINGS for getCompilerSetting or emscripten_get_compiler_setting to work'),
14501449
#endif
1451-
},
14521450

14531451
emscripten_has_asyncify: () => {{{ ASYNCIFY }}},
14541452

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
};
@@ -3058,7 +3058,7 @@ var LibraryGLEmulation = {
30583058
emulatedElementArrayBuffer = true;
30593059
}
30603060
} else if (GLImmediate.mode > 6) { // above GL_TRIANGLE_FAN are the non-GL ES modes
3061-
if (GLImmediate.mode != 7) throw 'unsupported immediate mode ' + GLImmediate.mode; // GL_QUADS
3061+
if (GLImmediate.mode != 7) abort('unsupported immediate mode ' + GLImmediate.mode); // GL_QUADS
30623062
// GLImmediate.firstVertex is the first vertex we want. Quad indexes are
30633063
// in the pattern 0 1 2, 0 2 3, 4 5 6, 4 6 7, so we need to look at
30643064
// index firstVertex * 1.5 to see it. Then since indexes are 2 bytes
@@ -3214,7 +3214,7 @@ var LibraryGLEmulation = {
32143214
glTexCoord2fv: (v) =>
32153215
_glTexCoord2i({{{ makeGetValue('v', '0', 'float') }}}, {{{ makeGetValue('v', '4', 'float') }}}),
32163216

3217-
glTexCoord4f: () => { throw 'glTexCoord4f: TODO' },
3217+
glTexCoord4f: () => { abort('glTexCoord4f: TODO') },
32183218

32193219
glColor4f: (r, g, b, a) => {
32203220
r = Math.max(Math.min(r, 1), 0);
@@ -3746,9 +3746,9 @@ var LibraryGLEmulation = {
37463746
},
37473747
glRotatef: 'glRotated',
37483748

3749-
glDrawBuffer: () => { throw 'glDrawBuffer: TODO' },
3749+
glDrawBuffer: () => { abort('glDrawBuffer: TODO') },
37503750
#if MAX_WEBGL_VERSION < 2
3751-
glReadBuffer: () => { throw 'glReadBuffer: TODO' },
3751+
glReadBuffer: () => { abort('glReadBuffer: TODO') },
37523752
#endif
37533753

37543754
glClipPlane: (pname, param) => {
@@ -3796,7 +3796,7 @@ var LibraryGLEmulation = {
37963796
// multiply position with current modelviewmatrix
37973797
GLImmediate.matrixLib.mat4.multiplyVec4(GLImmediate.matrix[0], GLEmulation.lightPosition[lightId]);
37983798
} else {
3799-
throw 'glLightfv: TODO: ' + pname;
3799+
abort('glLightfv: TODO: ' + pname);
38003800
}
38013801
}
38023802
},
@@ -3805,7 +3805,7 @@ var LibraryGLEmulation = {
38053805
if (pname == 0x0B52) { // GL_LIGHT_MODEL_TWO_SIDE
38063806
GLEmulation.lightModelTwoSide = (param != 0) ? true : false;
38073807
} else {
3808-
throw 'glLightModelf: TODO: ' + pname;
3808+
abort('glLightModelf: TODO: ' + pname);
38093809
}
38103810
},
38113811

@@ -3816,12 +3816,12 @@ var LibraryGLEmulation = {
38163816
GLEmulation.lightModelAmbient[2] = {{{ makeGetValue('param', '8', 'float') }}};
38173817
GLEmulation.lightModelAmbient[3] = {{{ makeGetValue('param', '12', 'float') }}};
38183818
} else {
3819-
throw 'glLightModelfv: TODO: ' + pname;
3819+
abort('glLightModelfv: TODO: ' + pname);
38203820
}
38213821
},
38223822

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

38263826
if (pname == 0x1200) { // GL_AMBIENT
38273827
GLEmulation.materialAmbient[0] = {{{ makeGetValue('param', '0', 'float') }}};
@@ -3841,22 +3841,22 @@ var LibraryGLEmulation = {
38413841
} else if (pname == 0x1601) { // GL_SHININESS
38423842
GLEmulation.materialShininess[0] = {{{ makeGetValue('param', '0', 'float') }}};
38433843
} else {
3844-
throw 'glMaterialfv: TODO: ' + pname;
3844+
abort('glMaterialfv: TODO: ' + pname);
38453845
}
38463846
},
38473847

3848-
glTexGeni: (coord, pname, param) => { throw 'glTexGeni: TODO' },
3849-
glTexGenfv: (coord, pname, param) => { throw 'glTexGenfv: TODO' },
3848+
glTexGeni: (coord, pname, param) => abort('glTexGeni: TODO'),
3849+
glTexGenfv: (coord, pname, param) => abort('glTexGenfv: TODO'),
38503850
glTexEnvi: (target, pname, params) => warnOnce('glTexEnvi: TODO'),
38513851
glTexEnvf: (target, pname, params) => warnOnce('glTexEnvf: TODO'),
38523852
glTexEnvfv: (target, pname, params) => warnOnce('glTexEnvfv: TODO'),
38533853

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

3857-
glTexImage1D: (target, level, internalformat, width, border, format, type, data) => { throw 'glTexImage1D: TODO' },
3858-
glTexCoord3f: (target, level, internalformat, width, border, format, type, data) => { throw 'glTexCoord3f: TODO' },
3859-
glGetTexLevelParameteriv: (target, level, pname, params) => { throw 'glGetTexLevelParameteriv: TODO' },
3857+
glTexImage1D: (target, level, internalformat, width, border, format, type, data) => abort('glTexImage1D: TODO'),
3858+
glTexCoord3f: (target, level, internalformat, width, border, format, type, data) => abort('glTexCoord3f: TODO'),
3859+
glGetTexLevelParameteriv: (target, level, pname, params) => abort('glGetTexLevelParameteriv: TODO'),
38603860

38613861
glShadeModel: () => warnOnce('TODO: glShadeModel'),
38623862

0 commit comments

Comments
 (0)