Skip to content

Commit cafece8

Browse files
sbc100pull[bot]
authored andcommitted
More use of string templates in parseTools.js. NFC (#20431)
1 parent 224aaca commit cafece8

2 files changed

Lines changed: 30 additions & 31 deletions

File tree

src/parseTools.js

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function processMacros(text) {
2323
const ret = eval(str);
2424
return ret !== null ? ret.toString() : '';
2525
} catch (ex) {
26-
ex.stack = 'In the following macro:\n\n' + str + '\n\n' + ex.stack;
26+
ex.stack = `In the following macro:\n\n${str}\n\n${ex.stack}`;
2727
throw ex;
2828
}
2929
});
@@ -178,7 +178,7 @@ function needsQuoting(ident) {
178178
global.POINTER_SIZE = MEMORY64 ? 8 : 4;
179179
global.STACK_ALIGN = 16;
180180
const POINTER_BITS = POINTER_SIZE * 8;
181-
const POINTER_TYPE = 'u' + POINTER_BITS;
181+
const POINTER_TYPE = `u${POINTER_BITS}`;
182182
const POINTER_JS_TYPE = MEMORY64 ? "'bigint'" : "'number'";
183183
const POINTER_SHIFT = MEMORY64 ? '3' : '2';
184184
const POINTER_HEAP = MEMORY64 ? 'HEAP64' : 'HEAP32';
@@ -190,7 +190,7 @@ const SIZE_TYPE = POINTER_TYPE;
190190
// used in practice, while POINTER_TYPE is the more refined internal
191191
// type (that is unsigned, where as core wasm does not have unsigned
192192
// types).
193-
const POINTER_WASM_TYPE = 'i' + POINTER_BITS;
193+
const POINTER_WASM_TYPE = `i${POINTER_BITS}`;
194194

195195
function isPointerType(type) {
196196
return type[type.length - 1] == '*';
@@ -201,10 +201,10 @@ function isPointerType(type) {
201201
// value will be replaced with tempVar.
202202
function makeInlineCalculation(expression, value, tempVar) {
203203
if (!isNiceIdent(value)) {
204-
expression = tempVar + '=' + value + ',' + expression;
204+
expression = `${tempVar} = ${value},${expression}`;
205205
value = tempVar;
206206
}
207-
return '(' + expression.replace(/VALUE/g, value) + ')';
207+
return `(${expression.replace(/VALUE/g, value)})`;
208208
}
209209

210210
// XXX Make all i64 parts signed
@@ -264,7 +264,7 @@ function indentify(text, indent) {
264264
indent += ' ';
265265
}
266266
}
267-
return text.replace(/\n/g, '\n' + indent);
267+
return text.replace(/\n/g, `\n${indent}`);
268268
}
269269

270270
// Correction tools
@@ -283,7 +283,7 @@ function getNativeTypeSize(type) {
283283
}
284284
if (type[0] === 'i') {
285285
const bits = Number(type.substr(1));
286-
assert(bits % 8 === 0, 'getNativeTypeSize invalid bits ' + bits + ', type ' + type);
286+
assert(bits % 8 === 0, `getNativeTypeSize invalid bits ${bits}, ${type} type`);
287287
return bits / 8;
288288
}
289289
return 0;
@@ -322,7 +322,7 @@ function asmEnsureFloat(value, type) {
322322
// may need a .0 (if it can't fit in an int)
323323
if (value == 0) return 'Math.fround(0)';
324324
value = ensureDot(value);
325-
return 'Math.fround(' + value + ')';
325+
return `Math.fround(${value})`;
326326
}
327327
if (FLOAT_TYPES.has(type)) {
328328
return ensureDot(value);
@@ -340,15 +340,15 @@ function asmCoercion(value, type) {
340340
return asmEnsureFloat(value, type);
341341
}
342342
if (type === 'float') {
343-
return 'Math.fround(' + value + ')';
343+
return `Math.fround(${value})`;
344344
}
345-
return '(+(' + value + '))';
345+
return `(+(${value}))`;
346346
}
347-
return '((' + value + ')|0)';
347+
return `((${value})|0)`;
348348
}
349349

350350
function asmFloatToInt(x) {
351-
return '(~~(' + x + '))';
351+
return `(~~(${x}))`;
352352
}
353353

354354
// See makeSetValue
@@ -359,7 +359,7 @@ function makeGetValue(ptr, pos, type, noNeedFirst, unsigned, ignore, align) {
359359
// TODO(sbc): make this into an error at some point.
360360
printErr('makeGetValue: Please use u8/u16/u32/u64 unsigned types in favor of additional argument');
361361
if (unsigned && type.startsWith('i')) {
362-
type = 'u' + type.slice(1);
362+
type = `u${type.slice(1)}`;
363363
}
364364
} else if (type.startsWith('u')) {
365365
// Set `unsigned` based on the type name.
@@ -368,11 +368,11 @@ function makeGetValue(ptr, pos, type, noNeedFirst, unsigned, ignore, align) {
368368

369369
const offset = calcFastOffset(ptr, pos);
370370
if (type === 'i53' || type === 'u53') {
371-
return 'readI53From' + (unsigned ? 'U' : 'I') + '64(' + offset + ')';
371+
return `readI53From${unsigned ? 'U' : 'I'}64(${offset})`;
372372
}
373373

374374
const slab = getHeapForType(type);
375-
let ret = slab + '[' + getHeapOffset(offset, type) + ']';
375+
let ret = `${slab}[${getHeapOffset(offset, type)}]`;
376376
if (MEMORY64 && isPointerType(type)) {
377377
ret = `Number(${ret})`;
378378
}
@@ -395,7 +395,7 @@ function makeSetValue(ptr, pos, value, type) {
395395
if (ASSERTIONS == 2 && (type.startsWith('i') || type.startsWith('u'))) {
396396
const width = getBitWidth(type);
397397
const assertion = `checkInt${width}(${value})`;
398-
rtn += ';' + assertion
398+
rtn += `;${assertion}`
399399
}
400400
return rtn;
401401
}
@@ -419,7 +419,7 @@ function makeSetValueImpl(ptr, pos, value, type) {
419419
if (slab == 'HEAPU64' || slab == 'HEAP64') {
420420
value = `BigInt(${value})`;
421421
}
422-
return slab + '[' + getHeapOffset(offset, type) + '] = ' + value;
422+
return `${slab}[${getHeapOffset(offset, type)}] = ${value}`;
423423
}
424424

425425
function makeHEAPView(which, start, end) {
@@ -517,7 +517,7 @@ function getHeapForType(type) {
517517
case 'i64': // fallthrough
518518
case 'u64': error('use i53/u53, or avoid i64/u64 without WASM_BIGINT');
519519
}
520-
assert(false, 'bad heap type: ' + type);
520+
assert(false, `bad heap type: ${type}`);
521521
}
522522

523523
function makeReturn64(value) {
@@ -726,20 +726,16 @@ function modifyJSFunction(text, func) {
726726

727727
function runIfMainThread(text) {
728728
if (WASM_WORKERS && PTHREADS) {
729-
return 'if (!ENVIRONMENT_IS_WASM_WORKER && !ENVIRONMENT_IS_PTHREAD) { ' + text + ' }';
729+
return `if (!ENVIRONMENT_IS_WASM_WORKER && !ENVIRONMENT_IS_PTHREAD) { ${text} }`;
730730
} else if (WASM_WORKERS) {
731-
return 'if (!ENVIRONMENT_IS_WASM_WORKER) { ' + text + ' }';
731+
return `if (!ENVIRONMENT_IS_WASM_WORKER) { ${text} }`;
732732
} else if (PTHREADS) {
733-
return 'if (!ENVIRONMENT_IS_PTHREAD) { ' + text + ' }';
733+
return `if (!ENVIRONMENT_IS_PTHREAD) { ${text} }`;
734734
} else {
735735
return text;
736736
}
737737
}
738738

739-
// Legacy name for runIfMainThread.
740-
// TODO(remove).
741-
const runOnMainThread = runIfMainThread;
742-
743739
function expectToReceiveOnModule(name) {
744740
return INCOMING_MODULE_JS_API.has(name);
745741
}
@@ -794,17 +790,17 @@ function makeModuleReceiveExpr(name, defaultValue) {
794790
function makeModuleReceiveWithVar(localName, moduleName, defaultValue, noAssert) {
795791
if (!moduleName) moduleName = localName;
796792
checkReceiving(moduleName);
797-
let ret = 'var ' + localName;
793+
let ret = `var ${localName}`;
798794
if (!expectToReceiveOnModule(moduleName)) {
799795
if (defaultValue) {
800-
ret += ' = ' + defaultValue;
796+
ret += ` = ${defaultValue}`;
801797
}
802798
ret += ';';
803799
} else {
804800
if (defaultValue) {
805801
ret += ` = Module['${moduleName}'] || ${defaultValue};`;
806802
} else {
807-
ret += ';' + makeModuleReceive(localName, moduleName);
803+
ret += `; ${makeModuleReceive(localName, moduleName)}`;
808804
return ret;
809805
}
810806
}
@@ -817,7 +813,7 @@ function makeModuleReceiveWithVar(localName, moduleName, defaultValue, noAssert)
817813
function makeRemovedFSAssert(fsName) {
818814
assert(ASSERTIONS);
819815
const lower = fsName.toLowerCase();
820-
if (JS_LIBRARIES.includes('library_' + lower + '.js')) return '';
816+
if (JS_LIBRARIES.includes(`library_${lower}.js`)) return '';
821817
return `var ${fsName} = '${fsName} is no longer included by default; build with -l${lower}.js';`;
822818
}
823819

@@ -853,7 +849,7 @@ function _asmjsDemangle(symbol) {
853849
return symbol;
854850
}
855851
// Strip leading "_"
856-
assert(symbol.startsWith('_'), 'expected mangled symbol: ' + symbol);
852+
assert(symbol.startsWith('_'), `expected mangled symbol: ${symbol}`);
857853
return symbol.substr(1);
858854
}
859855

@@ -1010,7 +1006,7 @@ function getEntryFunction() {
10101006
if (MAIN_MODULE) {
10111007
return `resolveGlobalSymbol('${entryFunction}').sym;`
10121008
}
1013-
return '_' + entryFunction;
1009+
return `_${entryFunction}`;
10141010
}
10151011

10161012
function preJS() {

src/parseTools_legacy.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,6 @@ function asmFFICoercion(value, type) {
132132
if (type === 'float') value = asmCoercion(value, 'float');
133133
return value;
134134
}
135+
136+
// Legacy name for runIfMainThread.
137+
const runOnMainThread = runIfMainThread;

0 commit comments

Comments
 (0)