Skip to content

Commit 3c822ef

Browse files
authored
Start using JS string template in JS code (#19285)
It looks like these have been implements for a very long time in all the engines: https://caniuse.com/template-literals I just chose of libraries to start with, and will continue with others if there are no objections.
1 parent c8f3ea7 commit 3c822ef

File tree

6 files changed

+83
-85
lines changed

6 files changed

+83
-85
lines changed

src/embind/embind.js

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ var LibraryEmbind = {
9393
proto[methodName] = function() {
9494
// TODO This check can be removed in -O3 level "unsafe" optimizations.
9595
if (!proto[methodName].overloadTable.hasOwnProperty(arguments.length)) {
96-
throwBindingError("Function '" + humanName + "' called with an invalid number of arguments (" + arguments.length + ") - expects one of (" + proto[methodName].overloadTable + ")!");
96+
throwBindingError(`Function '${humanName}' called with an invalid number of arguments (${arguments.length}) - expects one of (${proto[methodName].overloadTable})!`);
9797
}
9898
return proto[methodName].overloadTable[arguments.length].apply(this, arguments);
9999
};
@@ -118,14 +118,14 @@ var LibraryEmbind = {
118118
$exposePublicSymbol: function(name, value, numArguments) {
119119
if (Module.hasOwnProperty(name)) {
120120
if (undefined === numArguments || (undefined !== Module[name].overloadTable && undefined !== Module[name].overloadTable[numArguments])) {
121-
throwBindingError("Cannot register public name '" + name + "' twice");
121+
throwBindingError(`Cannot register public name '${name}' twice`);
122122
}
123123

124124
// We are exposing a function with the same name as an existing function. Create an overload table and a function selector
125125
// that routes between the two.
126126
ensureOverloadTable(Module, name, name);
127127
if (Module.hasOwnProperty(numArguments)) {
128-
throwBindingError("Cannot register multiple overloads of a function with the same number of arguments (" + numArguments + ")!");
128+
throwBindingError(`Cannot register multiple overloads of a function with the same number of arguments (${numArguments})!`);
129129
}
130130
// Add the new function into the overload table.
131131
Module[name].overloadTable[numArguments] = value;
@@ -282,19 +282,19 @@ var LibraryEmbind = {
282282
$registerType__docs: '/** @param {Object=} options */',
283283
$registerType: function(rawType, registeredInstance, options = {}) {
284284
if (!('argPackAdvance' in registeredInstance)) {
285-
throw new TypeError('registerType registeredInstance requires argPackAdvance');
285+
throw new TypeError('registerType registeredInstance requires argPackAdvance');
286286
}
287287

288288
var name = registeredInstance.name;
289289
if (!rawType) {
290-
throwBindingError('type "' + name + '" must have a positive integer typeid pointer');
290+
throwBindingError(`type "${name}" must have a positive integer typeid pointer`);
291291
}
292292
if (registeredTypes.hasOwnProperty(rawType)) {
293-
if (options.ignoreDuplicateRegistrations) {
294-
return;
295-
} else {
296-
throwBindingError("Cannot register type '" + name + "' twice");
297-
}
293+
if (options.ignoreDuplicateRegistrations) {
294+
return;
295+
} else {
296+
throwBindingError(`Cannot register type '${name}' twice`);
297+
}
298298
}
299299

300300
registeredTypes[rawType] = registeredInstance;
@@ -546,10 +546,10 @@ var LibraryEmbind = {
546546
var checkAssertions = (value, toTypeName) => {
547547
#if ASSERTIONS
548548
if (typeof value != "number" && typeof value != "boolean") {
549-
throw new TypeError('Cannot convert "' + embindRepr(value) + '" to ' + toTypeName);
549+
throw new TypeError(`Cannot convert "${embindRepr(value)}" to ${toTypeName}`);
550550
}
551551
if (value < minRange || value > maxRange) {
552-
throw new TypeError('Passing a number "' + embindRepr(value) + '" from JS side to C/C++ side to an argument of type "' + name + '", which is outside the valid range [' + minRange + ', ' + maxRange + ']!');
552+
throw new TypeError(`Passing a number "${embindRepr(value)}" from JS side to C/C++ side to an argument of type "${name}", which is outside the valid range [${minRange}, ${maxRange}]!`);
553553
}
554554
#endif
555555
}
@@ -599,10 +599,10 @@ var LibraryEmbind = {
599599
},
600600
'toWireType': function (destructors, value) {
601601
if (typeof value != "bigint" && typeof value != "number") {
602-
throw new TypeError('Cannot convert "' + embindRepr(value) + '" to ' + this.name);
602+
throw new TypeError(`Cannot convert "${embindRepr(value)}" to ${this.name}`);
603603
}
604604
if (value < minRange || value > maxRange) {
605-
throw new TypeError('Passing a number "' + embindRepr(value) + '" from JS side to C/C++ side to an argument of type "' + name + '", which is outside the valid range [' + minRange + ', ' + maxRange + ']!');
605+
throw new TypeError(`Passing a number "${embindRepr(value)}" from JS side to C/C++ side to an argument of type "${name}", which is outside the valid range [${minRange}, ${maxRange}]!`);
606606
}
607607
return value;
608608
},
@@ -630,7 +630,7 @@ var LibraryEmbind = {
630630
'toWireType': function(destructors, value) {
631631
#if ASSERTIONS
632632
if (typeof value != "number" && typeof value != "boolean") {
633-
throw new TypeError('Cannot convert "' + embindRepr(value) + '" to ' + this.name);
633+
throw new TypeError(`Cannot convert ${embindRepr(value)} to ${this.name}`);
634634
}
635635
#endif
636636
// The VM will perform JS to Wasm value conversion, according to the spec:
@@ -902,7 +902,7 @@ var LibraryEmbind = {
902902
$newFunc__deps: ['$createNamedFunction'],
903903
$newFunc: function(constructor, argumentList) {
904904
if (!(constructor instanceof Function)) {
905-
throw new TypeError('new_ called with constructor type ' + typeof(constructor) + " which is not a function");
905+
throw new TypeError(`new_ called with constructor type ${typeof(constructor)} which is not a function`);
906906
}
907907
/*
908908
* Previously, the following line was just:
@@ -990,9 +990,7 @@ var LibraryEmbind = {
990990
var destructors = [];
991991
return function() {
992992
if (arguments.length !== expectedArgCount) {
993-
throwBindingError('function ' + humanName + ' called with ' +
994-
arguments.length + ' arguments, expected ' + expectedArgCount +
995-
' args!');
993+
throwBindingError(`function ${humanName} called with ${arguments.length} arguments, expected ${expectedArgCount} args!`);
996994
}
997995
#if EMSCRIPTEN_TRACING
998996
Module.emscripten_trace_enter_context('embind::' + humanName);
@@ -1060,7 +1058,7 @@ var LibraryEmbind = {
10601058
"}\n";
10611059

10621060
#if EMSCRIPTEN_TRACING
1063-
invokerFnBody += "Module.emscripten_trace_enter_context('embind::" + humanName + "');\n";
1061+
invokerFnBody += `Module.emscripten_trace_enter_context('embind::${humanName}');\n`;
10641062
#endif
10651063

10661064
if (needsDestructorStack) {
@@ -1169,7 +1167,7 @@ var LibraryEmbind = {
11691167

11701168
var fp = makeDynCaller();
11711169
if (typeof fp != "function") {
1172-
throwBindingError("unknown function pointer with signature " + signature + ": " + rawFunction);
1170+
throwBindingError(`unknown function pointer with signature ${signature}: ${rawFunction}`);
11731171
}
11741172
return fp;
11751173
},
@@ -1185,7 +1183,7 @@ var LibraryEmbind = {
11851183
rawInvoker = embind__requireFunction(signature, rawInvoker);
11861184

11871185
exposePublicSymbol(name, function() {
1188-
throwUnboundTypeError('Cannot call ' + name + ' due to unbound types', argTypes);
1186+
throwUnboundTypeError(`Cannot call ${name} due to unbound types`, argTypes);
11891187
}, argCount - 1);
11901188

11911189
whenDependentTypesAreResolved([], argTypes, function(argTypes) {
@@ -1282,7 +1280,7 @@ var LibraryEmbind = {
12821280
},
12831281
'toWireType': function(destructors, o) {
12841282
if (elementsLength !== o.length) {
1285-
throw new TypeError("Incorrect number of tuple elements for " + reg.name + ": expected=" + elementsLength + ", actual=" + o.length);
1283+
throw new TypeError(`Incorrect number of tuple elements for ${reg.name}: expected=${elementsLength}, actual=${o.length}`);
12861284
}
12871285
var ptr = rawConstructor();
12881286
for (var i = 0; i < elementsLength; ++i) {
@@ -1395,7 +1393,7 @@ var LibraryEmbind = {
13951393
// assume all fields are present without checking.
13961394
for (var fieldName in fields) {
13971395
if (!(fieldName in o)) {
1398-
throw new TypeError('Missing field: "' + fieldName + '"');
1396+
throw new TypeError(`Missing field: "${fieldName}"`);
13991397
}
14001398
}
14011399
var ptr = rawConstructor();
@@ -1434,13 +1432,13 @@ var LibraryEmbind = {
14341432
}
14351433

14361434
if (!handle.$$) {
1437-
throwBindingError('Cannot pass "' + embindRepr(handle) + '" as a ' + this.name);
1435+
throwBindingError(`Cannot pass "${embindRepr(handle)}" as a ${this.name}`);
14381436
}
14391437
if (!handle.$$.ptr) {
1440-
throwBindingError('Cannot pass deleted object as a pointer of type ' + this.name);
1438+
throwBindingError(`Cannot pass deleted object as a pointer of type ${this.name}`);
14411439
}
14421440
if (!this.isConst && handle.$$.ptrType.isConst) {
1443-
throwBindingError('Cannot convert argument of type ' + (handle.$$.smartPtrType ? handle.$$.smartPtrType.name : handle.$$.ptrType.name) + ' to parameter type ' + this.name);
1441+
throwBindingError(`Cannot convert argument of type ${(handle.$$.smartPtrType ? handle.$$.smartPtrType.name : handle.$$.ptrType.name)} to parameter type ${this.name}`);
14441442
}
14451443
var handleClass = handle.$$.ptrType.registeredClass;
14461444
ptr = upcastPointer(handle.$$.ptr, handleClass, this.registeredClass);
@@ -1459,7 +1457,7 @@ var LibraryEmbind = {
14591457
if (handle.$$.smartPtrType === this) {
14601458
ptr = handle.$$.smartPtr;
14611459
} else {
1462-
throwBindingError('Cannot convert argument of type ' + (handle.$$.smartPtrType ? handle.$$.smartPtrType.name : handle.$$.ptrType.name) + ' to parameter type ' + this.name);
1460+
throwBindingError(`Cannot convert argument of type ${(handle.$$.smartPtrType ? handle.$$.smartPtrType.name : handle.$$.ptrType.name)} to parameter type ${this.name}`);
14631461
}
14641462
break;
14651463

@@ -1503,7 +1501,7 @@ var LibraryEmbind = {
15031501
}
15041502

15051503
if (!handle.$$) {
1506-
throwBindingError('Cannot pass "' + embindRepr(handle) + '" as a ' + this.name);
1504+
throwBindingError(`Cannot pass "${embindRepr(handle)}" as a ${this.name}`);
15071505
}
15081506
if (!handle.$$.ptr) {
15091507
throwBindingError('Cannot pass deleted object as a pointer of type ' + this.name);
@@ -1525,13 +1523,13 @@ var LibraryEmbind = {
15251523
}
15261524

15271525
if (!handle.$$) {
1528-
throwBindingError('Cannot pass "' + embindRepr(handle) + '" as a ' + this.name);
1526+
throwBindingError(`Cannot pass "${embindRepr(handle)}" as a ${this.name}`);
15291527
}
15301528
if (!handle.$$.ptr) {
1531-
throwBindingError('Cannot pass deleted object as a pointer of type ' + this.name);
1529+
throwBindingError(`Cannot pass deleted object as a pointer of type ${this.name}`);
15321530
}
15331531
if (handle.$$.ptrType.isConst) {
1534-
throwBindingError('Cannot convert argument of type ' + handle.$$.ptrType.name + ' to parameter type ' + this.name);
1532+
throwBindingError(`Cannot convert argument of type ${handle.$$.ptrType.name} to parameter type ${this.name}`);
15351533
}
15361534
var handleClass = handle.$$.ptrType.registeredClass;
15371535
var ptr = upcastPointer(handle.$$.ptr, handleClass, this.registeredClass);
@@ -1768,7 +1766,7 @@ var LibraryEmbind = {
17681766
// This is more useful than the empty stacktrace of `FinalizationRegistry`
17691767
// callback.
17701768
var cls = $$.ptrType.registeredClass;
1771-
info.leakWarning = new Error("Embind found a leaked C++ instance " + cls.name + " <" + ptrToString($$.ptr) + ">.\n" +
1769+
info.leakWarning = new Error(`Embind found a leaked C++ instance ${cls.name} <${ptrToString($$.ptr)}>.\n` +
17721770
"We'll free it automatically in this case, but this functionality is not reliable across various environments.\n" +
17731771
"Make sure to invoke .delete() manually once you're done with the instance instead.\n" +
17741772
"Originally allocated"); // `.stack` will add "at ..." after this sentence
@@ -2002,7 +2000,7 @@ var LibraryEmbind = {
20022000

20032001
exposePublicSymbol(legalFunctionName, function() {
20042002
// this code cannot run if baseClassRawType is zero
2005-
throwUnboundTypeError('Cannot construct ' + name + ' due to unbound types', [baseClassRawType]);
2003+
throwUnboundTypeError(`Cannot construct ${name} due to unbound types`, [baseClassRawType]);
20062004
});
20072005

20082006
whenDependentTypesAreResolved(
@@ -2029,7 +2027,7 @@ var LibraryEmbind = {
20292027
}
20302028
var body = registeredClass.constructor_body[arguments.length];
20312029
if (undefined === body) {
2032-
throw new BindingError("Tried to invoke ctor of " + name + " with invalid number of parameters (" + arguments.length + ") - expected (" + Object.keys(registeredClass.constructor_body).toString() + ") parameters instead!");
2030+
throw new BindingError(`Tried to invoke ctor of ${name} with invalid number of parameters (${arguments.length}) - expected (${Object.keys(registeredClass.constructor_body).toString()}) parameters instead!`);
20332031
}
20342032
return body.apply(this, arguments);
20352033
});
@@ -2105,10 +2103,10 @@ var LibraryEmbind = {
21052103
classType.registeredClass.constructor_body = [];
21062104
}
21072105
if (undefined !== classType.registeredClass.constructor_body[argCount - 1]) {
2108-
throw new BindingError("Cannot register multiple constructors with identical number of parameters (" + (argCount-1) + ") for class '" + classType.name + "'! Overload resolution is currently only performed using the parameter count, not actual type info!");
2106+
throw new BindingError(`Cannot register multiple constructors with identical number of parameters (${argCount-1}) for class '${classType.name}'! Overload resolution is currently only performed using the parameter count, not actual type info!`);
21092107
}
21102108
classType.registeredClass.constructor_body[argCount - 1] = () => {
2111-
throwUnboundTypeError('Cannot construct ' + classType.name + ' due to unbound types', rawArgTypes);
2109+
throwUnboundTypeError(`Cannot construct ${classType.name} due to unbound types`, rawArgTypes);
21122110
};
21132111

21142112
whenDependentTypesAreResolved([], rawArgTypes, function(argTypes) {
@@ -2140,7 +2138,7 @@ var LibraryEmbind = {
21402138
$upcastPointer: function(ptr, ptrClass, desiredClass) {
21412139
while (ptrClass !== desiredClass) {
21422140
if (!ptrClass.upcast) {
2143-
throwBindingError("Expected null or instance of " + desiredClass.name + ", got an instance of " + ptrClass.name);
2141+
throwBindingError(`Expected null or instance of ${desiredClass.name}, got an instance of ${ptrClass.name}`);
21442142
}
21452143
ptr = ptrClass.upcast(ptr);
21462144
ptrClass = ptrClass.baseClass;
@@ -2157,7 +2155,7 @@ var LibraryEmbind = {
21572155
throwBindingError(humanName + ' incompatible with "this" of type ' + this_.constructor.name);
21582156
}
21592157
if (!this_.$$.ptr) {
2160-
throwBindingError('cannot call emscripten binding method ' + humanName + ' on deleted object');
2158+
throwBindingError(`cannot call emscripten binding method ${humanName} on deleted object`);
21612159
}
21622160

21632161
// todo: kill this
@@ -2196,7 +2194,7 @@ var LibraryEmbind = {
21962194
}
21972195

21982196
function unboundTypesHandler() {
2199-
throwUnboundTypeError('Cannot call ' + humanName + ' due to unbound types', rawArgTypes);
2197+
throwUnboundTypeError(`Cannot call ${humanName} due to unbound types`, rawArgTypes);
22002198
}
22012199

22022200
var proto = classType.registeredClass.instancePrototype;
@@ -2255,14 +2253,14 @@ var LibraryEmbind = {
22552253
var humanName = classType.name + '.' + fieldName;
22562254
var desc = {
22572255
get: function() {
2258-
throwUnboundTypeError('Cannot access ' + humanName + ' due to unbound types', [getterReturnType, setterArgumentType]);
2256+
throwUnboundTypeError(`Cannot access ${humanName} due to unbound types`, [getterReturnType, setterArgumentType]);
22592257
},
22602258
enumerable: true,
22612259
configurable: true
22622260
};
22632261
if (setter) {
22642262
desc.set = () => {
2265-
throwUnboundTypeError('Cannot access ' + humanName + ' due to unbound types', [getterReturnType, setterArgumentType]);
2263+
throwUnboundTypeError(`Cannot access ${humanName} due to unbound types`, [getterReturnType, setterArgumentType]);
22662264
};
22672265
} else {
22682266
desc.set = (v) => {
@@ -2324,7 +2322,7 @@ var LibraryEmbind = {
23242322
var humanName = classType.name + '.' + methodName;
23252323

23262324
function unboundTypesHandler() {
2327-
throwUnboundTypeError('Cannot call ' + humanName + ' due to unbound types', rawArgTypes);
2325+
throwUnboundTypeError(`Cannot call ${humanName} due to unbound types`, rawArgTypes);
23282326
}
23292327

23302328
if (methodName.startsWith("@@")) {
@@ -2381,18 +2379,18 @@ var LibraryEmbind = {
23812379
var humanName = classType.name + '.' + fieldName;
23822380
var desc = {
23832381
get: function() {
2384-
throwUnboundTypeError('Cannot access ' + humanName + ' due to unbound types', [rawFieldType]);
2382+
throwUnboundTypeError(`Cannot access ${humanName} due to unbound types`, [rawFieldType]);
23852383
},
23862384
enumerable: true,
23872385
configurable: true
23882386
};
23892387
if (setter) {
23902388
desc.set = () => {
2391-
throwUnboundTypeError('Cannot access ' + humanName + ' due to unbound types', [rawFieldType]);
2389+
throwUnboundTypeError(`Cannot access ${humanName} due to unbound types`, [rawFieldType]);
23922390
};
23932391
} else {
23942392
desc.set = (v) => {
2395-
throwBindingError(humanName + ' is a read-only property');
2393+
throwBindingError(`${humanName} is a read-only property`);
23962394
};
23972395
}
23982396

@@ -2445,7 +2443,7 @@ var LibraryEmbind = {
24452443
var ctor = createNamedFunction(constructorName, function() {
24462444
registeredClass.baseClass.pureVirtualFunctions.forEach(function(name) {
24472445
if (this[name] === baseClassPrototype[name]) {
2448-
throw new PureVirtualError('Pure virtual function ' + name + ' must be implemented in JavaScript');
2446+
throw new PureVirtualError(`Pure virtual function ${name} must be implemented in JavaScript`);
24492447
}
24502448
}.bind(this));
24512449

0 commit comments

Comments
 (0)