Skip to content

Use JS string interpolation in src/modules.js. NFC #15288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions src/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ var LibraryManager = {
}

// Deduplicate libraries to avoid processing any library file multiple times
libraries = libraries.filter(function(item, pos) {
return libraries.indexOf(item) == pos;
});
libraries = libraries.filter((item, pos) => libraries.indexOf(item) == pos);

// Save the list for has() queries later.
this.libraries = libraries;
Expand All @@ -196,21 +194,21 @@ var LibraryManager = {
processed = processMacros(preprocess(src, filename));
eval(processed);
} catch(e) {
var details = [e, e.lineNumber ? 'line number: ' + e.lineNumber : ''];
var details = [e, e.lineNumber ? `line number: ${e.lineNumber}` : ''];
if (VERBOSE) {
details.push((e.stack || "").toString().replace('Object.<anonymous>', filename));
}
if (processed) {
error('failure to execute js library "' + filename + '": ' + details);
error(`failure to execute js library "${filename}": ${details}`);
if (VERBOSE) {
error('preprocessed source (you can run a js engine on this to get a clearer error message sometimes):\n=============\n' + processed + '\n=============');
error(`preprocessed source (you can run a js engine on this to get a clearer error message sometimes):\n=============\n${processed}\n=============`);
} else {
error('use -s VERBOSE to see more details')
}
} else {
error('failure to process js library "' + filename + '": ' + details);
error(`failure to process js library "${filename}": ${details}`);
if (VERBOSE) {
error('original source:\n=============\n' + src + '\n=============');
error(`original source:\n=============\n${src}\n=============`);
} else {
error('use -s VERBOSE to see more details')
}
Expand All @@ -222,13 +220,13 @@ var LibraryManager = {
// apply synonyms. these are typically not speed-sensitive, and doing it
// this way makes it possible to not include hacks in the compiler
// (and makes it simpler to switch between SDL versions, fastcomp and non-fastcomp, etc.).
var lib = LibraryManager.library;
var lib = this.library;
libloop: for (var x in lib) {
if (isJsLibraryConfigIdentifier(x)) {
var index = x.lastIndexOf('__');
var basename = x.slice(0, index);
if (!(basename in lib)) {
error('Missing library element `' + basename + '` for library config `' + x + '`');
error(`Missing library element '${basename}' for library config '${x}'`);
}
continue;
}
Expand All @@ -246,20 +244,20 @@ var LibraryManager = {
// implemented.
function testStringType(sig) {
if (typeof lib[sig] !== 'undefined' && typeof typeof lib[sig] !== 'string') {
error(sig + ' should be a string! (was ' + typeof lib[sig]);
error(`${sig} should be a string! (was ${typeof lib[sig]})`);
}
}
var aliasSig = x + '__sig';
var targetSig = target + '__sig';
testStringType(aliasSig);
testStringType(targetSig);
if (typeof lib[aliasSig] === 'string' && typeof lib[targetSig] === 'string' && lib[aliasSig] != lib[targetSig]) {
error(aliasSig + ' (' + lib[aliasSig] + ') differs from ' + targetSig + ' (' + lib[targetSig] + ')');
error(`${aliasSig} (${lib[aliasSig]}) differs from ${targetSig} (${lib[targetSig]})`);
}

var sig = lib[aliasSig] || lib[targetSig];
if (typeof sig !== 'string') {
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!");
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!`);
}

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

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

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

var EXPORTED_RUNTIME_METHODS_SET = set(EXPORTED_RUNTIME_METHODS);
Expand Down Expand Up @@ -345,7 +343,7 @@ function exportRuntime() {
} else if (exported === 'printErr') {
exported = 'err';
}
return 'Module["' + name + '"] = ' + exported + ';';
return `Module["${name}"] = ${exported};`;
}
// do not export it. but if ASSERTIONS, emit a
// stub with an error, so the user gets a message
Expand All @@ -359,9 +357,9 @@ function exportRuntime() {
extra = '. Alternatively, forcing filesystem support (-s FORCE_FILESYSTEM=1) can export this for you';
}
if (!isNumber) {
return 'if (!Object.getOwnPropertyDescriptor(Module, "' + name + '")) Module["' + name + '"] = function() { abort("\'' + name + '\' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the FAQ)' + extra + '") };';
return `if (!Object.getOwnPropertyDescriptor(Module, "${name}")) Module["${name}"] = function() { abort("'${name}' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the FAQ)${extra}") };`;
} else {
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 + '") } });';
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}") } });`;
}
}
return '';
Expand Down Expand Up @@ -474,7 +472,7 @@ function exportRuntime() {
threadExports.push('ExitStatus');
}

threadExports.forEach(function(x) {
threadExports.forEach(x => {
EXPORTED_RUNTIME_METHODS_SET[x] = 1;
runtimeElements.push(x);
});
Expand All @@ -501,12 +499,12 @@ function exportRuntime() {
// check all exported things exist, warn about typos
for (var name in EXPORTED_RUNTIME_METHODS_SET) {
if (!runtimeElements.includes(name) && !runtimeNumbers.includes(name)) {
printErr('warning: invalid item (maybe a typo?) in EXPORTED_RUNTIME_METHODS: ' + name);
printErr(`warning: invalid item (maybe a typo?) in EXPORTED_RUNTIME_METHODS: ${name}`);
}
}
}
var exports = runtimeElements.map(function(name) { return maybeExport(name); });
exports = exports.concat(runtimeNumbers.map(function(name) { return maybeExportNumber(name); }));
exports = exports.filter(function(name) { return name != '' });
var exports = runtimeElements.map(name => maybeExport(name));
exports = exports.concat(runtimeNumbers.map(name => maybeExportNumber(name)));
exports = exports.filter(name => name != '');
return exports.join('\n');
}
2 changes: 1 addition & 1 deletion tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -10383,7 +10383,7 @@ def test_jslib_bad_config(self):
});
''')
err = self.expect_fail([EMCC, test_file('hello_world.c'), '--js-library=lib.js'])
self.assertContained('error: Missing library element `foo` for library config `foo__sig`', err)
self.assertContained("error: Missing library element 'foo' for library config 'foo__sig'", err)

def test_jslib_ifdef(self):
create_file('lib.js', '''
Expand Down