Skip to content

Commit 95ef06b

Browse files
authored
Use JS string interpolation in src/modules.js. NFC (#15288)
1 parent ff23b8c commit 95ef06b

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

src/modules.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ var LibraryManager = {
179179
}
180180

181181
// Deduplicate libraries to avoid processing any library file multiple times
182-
libraries = libraries.filter(function(item, pos) {
183-
return libraries.indexOf(item) == pos;
184-
});
182+
libraries = libraries.filter((item, pos) => libraries.indexOf(item) == pos);
185183

186184
// Save the list for has() queries later.
187185
this.libraries = libraries;
@@ -196,21 +194,21 @@ var LibraryManager = {
196194
processed = processMacros(preprocess(src, filename));
197195
eval(processed);
198196
} catch(e) {
199-
var details = [e, e.lineNumber ? 'line number: ' + e.lineNumber : ''];
197+
var details = [e, e.lineNumber ? `line number: ${e.lineNumber}` : ''];
200198
if (VERBOSE) {
201199
details.push((e.stack || "").toString().replace('Object.<anonymous>', filename));
202200
}
203201
if (processed) {
204-
error('failure to execute js library "' + filename + '": ' + details);
202+
error(`failure to execute js library "${filename}": ${details}`);
205203
if (VERBOSE) {
206-
error('preprocessed source (you can run a js engine on this to get a clearer error message sometimes):\n=============\n' + processed + '\n=============');
204+
error(`preprocessed source (you can run a js engine on this to get a clearer error message sometimes):\n=============\n${processed}\n=============`);
207205
} else {
208206
error('use -s VERBOSE to see more details')
209207
}
210208
} else {
211-
error('failure to process js library "' + filename + '": ' + details);
209+
error(`failure to process js library "${filename}": ${details}`);
212210
if (VERBOSE) {
213-
error('original source:\n=============\n' + src + '\n=============');
211+
error(`original source:\n=============\n${src}\n=============`);
214212
} else {
215213
error('use -s VERBOSE to see more details')
216214
}
@@ -222,13 +220,13 @@ var LibraryManager = {
222220
// apply synonyms. these are typically not speed-sensitive, and doing it
223221
// this way makes it possible to not include hacks in the compiler
224222
// (and makes it simpler to switch between SDL versions, fastcomp and non-fastcomp, etc.).
225-
var lib = LibraryManager.library;
223+
var lib = this.library;
226224
libloop: for (var x in lib) {
227225
if (isJsLibraryConfigIdentifier(x)) {
228226
var index = x.lastIndexOf('__');
229227
var basename = x.slice(0, index);
230228
if (!(basename in lib)) {
231-
error('Missing library element `' + basename + '` for library config `' + x + '`');
229+
error(`Missing library element '${basename}' for library config '${x}'`);
232230
}
233231
continue;
234232
}
@@ -246,20 +244,20 @@ var LibraryManager = {
246244
// implemented.
247245
function testStringType(sig) {
248246
if (typeof lib[sig] !== 'undefined' && typeof typeof lib[sig] !== 'string') {
249-
error(sig + ' should be a string! (was ' + typeof lib[sig]);
247+
error(`${sig} should be a string! (was ${typeof lib[sig]})`);
250248
}
251249
}
252250
var aliasSig = x + '__sig';
253251
var targetSig = target + '__sig';
254252
testStringType(aliasSig);
255253
testStringType(targetSig);
256254
if (typeof lib[aliasSig] === 'string' && typeof lib[targetSig] === 'string' && lib[aliasSig] != lib[targetSig]) {
257-
error(aliasSig + ' (' + lib[aliasSig] + ') differs from ' + targetSig + ' (' + lib[targetSig] + ')');
255+
error(`${aliasSig} (${lib[aliasSig]}) differs from ${targetSig} (${lib[targetSig]})`);
258256
}
259257

260258
var sig = lib[aliasSig] || lib[targetSig];
261259
if (typeof sig !== 'string') {
262-
error('Function ' + x + ' aliases to target function ' + target + ', but neither the alias or the target provide a signature. Please add a ' + targetSig + ": 'vifj...' annotation or a " + aliasSig + ": 'vifj...' annotation to describe the type of function forwarding that is needed!");
260+
error(`Function ${x} aliases to target function ${target}, but neither the alias or the target provide a signature. Please add a ${targetSig}: 'vifj...' annotation or a ${aliasSig}: 'vifj...' annotation to describe the type of function forwarding that is needed!`);
263261
}
264262

265263
// If only one of the target or the alias specifies a sig then copy
@@ -280,7 +278,7 @@ var LibraryManager = {
280278
}
281279
var ret = sig == 'v' ? '' : 'return ';
282280
var args = genArgSequence(argCount).join(',');
283-
lib[x] = new Function(args, ret + '_' + target + '(' + args + ');');
281+
lib[x] = new Function(args, `${ret}_${target}(${args});`);
284282

285283
if (!lib[x + '__deps']) lib[x + '__deps'] = [];
286284
lib[x + '__deps'].push(target);
@@ -304,8 +302,8 @@ if (!BOOTSTRAPPING_STRUCT_INFO) {
304302

305303
// Safe way to access a C define. We check that we don't add library functions with missing defines.
306304
function cDefine(key) {
307-
if (key in C_DEFINES) return C_DEFINES[key];
308-
throw 'Missing C define ' + key + '! If you just added it to struct_info.json, you need to ./emcc --clear-cache';
305+
if (key in C_DEFINES) return C_DEFINES[key];
306+
throw `Missing C define ${key}! If you just added it to struct_info.json, you need to ./emcc --clear-cache`;
309307
}
310308

311309
var EXPORTED_RUNTIME_METHODS_SET = set(EXPORTED_RUNTIME_METHODS);
@@ -345,7 +343,7 @@ function exportRuntime() {
345343
} else if (exported === 'printErr') {
346344
exported = 'err';
347345
}
348-
return 'Module["' + name + '"] = ' + exported + ';';
346+
return `Module["${name}"] = ${exported};`;
349347
}
350348
// do not export it. but if ASSERTIONS, emit a
351349
// stub with an error, so the user gets a message
@@ -359,9 +357,9 @@ function exportRuntime() {
359357
extra = '. Alternatively, forcing filesystem support (-s FORCE_FILESYSTEM=1) can export this for you';
360358
}
361359
if (!isNumber) {
362-
return 'if (!Object.getOwnPropertyDescriptor(Module, "' + name + '")) Module["' + name + '"] = function() { abort("\'' + name + '\' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the FAQ)' + extra + '") };';
360+
return `if (!Object.getOwnPropertyDescriptor(Module, "${name}")) Module["${name}"] = function() { abort("'${name}' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the FAQ)${extra}") };`;
363361
} else {
364-
return 'if (!Object.getOwnPropertyDescriptor(Module, "' + name + '")) Object.defineProperty(Module, "' + name + '", { configurable: true, get: function() { abort("\'' + name + '\' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the FAQ)' + extra + '") } });';
362+
return `if (!Object.getOwnPropertyDescriptor(Module, "${name}")) Object.defineProperty(Module, "${name}", { configurable: true, get: function() { abort("'${name}' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the FAQ)${extra}") } });`;
365363
}
366364
}
367365
return '';
@@ -474,7 +472,7 @@ function exportRuntime() {
474472
threadExports.push('ExitStatus');
475473
}
476474

477-
threadExports.forEach(function(x) {
475+
threadExports.forEach(x => {
478476
EXPORTED_RUNTIME_METHODS_SET[x] = 1;
479477
runtimeElements.push(x);
480478
});
@@ -501,12 +499,12 @@ function exportRuntime() {
501499
// check all exported things exist, warn about typos
502500
for (var name in EXPORTED_RUNTIME_METHODS_SET) {
503501
if (!runtimeElements.includes(name) && !runtimeNumbers.includes(name)) {
504-
printErr('warning: invalid item (maybe a typo?) in EXPORTED_RUNTIME_METHODS: ' + name);
502+
printErr(`warning: invalid item (maybe a typo?) in EXPORTED_RUNTIME_METHODS: ${name}`);
505503
}
506504
}
507505
}
508-
var exports = runtimeElements.map(function(name) { return maybeExport(name); });
509-
exports = exports.concat(runtimeNumbers.map(function(name) { return maybeExportNumber(name); }));
510-
exports = exports.filter(function(name) { return name != '' });
506+
var exports = runtimeElements.map(name => maybeExport(name));
507+
exports = exports.concat(runtimeNumbers.map(name => maybeExportNumber(name)));
508+
exports = exports.filter(name => name != '');
511509
return exports.join('\n');
512510
}

tests/test_other.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10383,7 +10383,7 @@ def test_jslib_bad_config(self):
1038310383
});
1038410384
''')
1038510385
err = self.expect_fail([EMCC, test_file('hello_world.c'), '--js-library=lib.js'])
10386-
self.assertContained('error: Missing library element `foo` for library config `foo__sig`', err)
10386+
self.assertContained("error: Missing library element 'foo' for library config 'foo__sig'", err)
1038710387

1038810388
def test_jslib_ifdef(self):
1038910389
create_file('lib.js', '''

0 commit comments

Comments
 (0)