Skip to content

Commit

Permalink
Precompute concatenated strings where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Apr 2, 2020
1 parent 6b27842 commit 5fc3c6b
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 430 deletions.
10 changes: 6 additions & 4 deletions lib/constructs/iterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ class Iterable {
throw new TypeError("Illegal invocation");
}
if (arguments.length < 1) {
throw new TypeError("Failed to execute 'forEach' on '${this.name}': 1 argument required, " +
"but only 0 present.");
throw new TypeError(
"Failed to execute 'forEach' on '${this.name}': 1 argument required, but only 0 present."
);
}
if (typeof callback !== "function") {
throw new TypeError("Failed to execute 'forEach' on '${this.name}': The callback provided " +
"as parameter 1 is not a function.");
throw new TypeError(
"Failed to execute 'forEach' on '${this.name}': The callback provided as parameter 1 is not a function."
);
}
const thisArg = arguments[1];
let pairs = Array.from(this[implSymbol]);
Expand Down
9 changes: 5 additions & 4 deletions lib/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function generateVarConversion(ctx, overload, i, parent, errPrefix, targetIdx =
`;
}
const msg = typeof targetIdx === "string" ?
`"${errPrefix}parameter " + (${targetIdx} + 1)` : `"${errPrefix}parameter ${i + 1}"`;
`\`${errPrefix}parameter \${${targetIdx} + 1}\`` : `"${errPrefix}parameter ${i + 1}"`;
const conv = Types.generateTypeConversion(
ctx, "curArg", idlType, [], parent.name, msg);
requires.merge(conv.requires);
Expand Down Expand Up @@ -58,8 +58,9 @@ module.exports.generateOverloadConversions = function (ctx, typeOfOp, name, pare
const plural = minArgs > 1 ? "s" : "";
str += `
if (arguments.length < ${minArgs}) {
throw new TypeError("${errPrefix}${minArgs} argument${plural} required, but only " + arguments.length +
" present.");
throw new TypeError(
\`${errPrefix}${minArgs} argument${plural} required, but only \${arguments.length} present.\`
);
}
`;
}
Expand All @@ -72,7 +73,7 @@ module.exports.generateOverloadConversions = function (ctx, typeOfOp, name, pare
.filter(o => o.typeList.length === numArgs);
if (S.length === 0) {
switchCases.push(`
throw new TypeError("${errPrefix}only " + arguments.length + " arguments present.");
throw new TypeError(\`${errPrefix}only \${arguments.length} arguments present.\`);
`);
continue;
}
Expand Down
40 changes: 28 additions & 12 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}

/**
* @param {string} errPrefix
* @param {string} appended
*/
function appendToErrorPrefix(errPrefix, appended) {
if (errPrefix.endsWith('"')) {
return errPrefix.slice(0, -1) + appended + '"';
} else if (errPrefix.endsWith("'")) {
return errPrefix.slice(0, -1) + appended + "'";
} else if (errPrefix.endsWith("`")) {
return errPrefix.slice(0, -1) + appended + "`";
}

return `\`\${${errPrefix}}${appended}\``;
}

function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, errPrefix = '"The provided value"') {
const requires = new utils.RequiresMap(ctx);
let str = "";
Expand Down Expand Up @@ -131,7 +147,7 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
// We do not save the callback context yet.
str += `
if (!utils.isObject(${name})) {
throw new TypeError(${errPrefix} + " is not an object");
throw new TypeError(${appendToErrorPrefix(errPrefix, " is not an object")});
}
`;
} else {
Expand Down Expand Up @@ -218,25 +234,25 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
if (union.sequenceLike) {
code += `if (${name}[Symbol.iterator] !== undefined) {`;
const conv = generateTypeConversion(ctx, name, union.sequenceLike, [], parentName,
`${errPrefix} + " sequence"`);
appendToErrorPrefix(errPrefix, " sequence"));
requires.merge(conv.requires);
code += conv.body;
code += `} else {`;
}

if (union.dictionary) {
const conv = generateTypeConversion(ctx, name, union.dictionary, [], parentName,
`${errPrefix} + " dictionary"`);
appendToErrorPrefix(errPrefix, " dictionary"));
requires.merge(conv.requires);
code += conv.body;
} else if (union.record) {
const conv = generateTypeConversion(ctx, name, union.record, [], parentName,
`${errPrefix} + " record"`);
appendToErrorPrefix(errPrefix, " record"));
requires.merge(conv.requires);
code += conv.body;
} else if (union.callbackInterface) {
const conv = generateTypeConversion(ctx, name, union.callbackInterface, [], parentName,
`${errPrefix} + " callback interface"`);
appendToErrorPrefix(errPrefix, " callback interface"));
requires.merge(conv.requires);
code += conv.body;
} else if (union.object) {
Expand Down Expand Up @@ -276,7 +292,7 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
code += conv.body;
requires.merge(conv.requires);
} else {
code += `throw new TypeError(${errPrefix} + " is not of any supported type.")`;
code += `throw new TypeError(${appendToErrorPrefix(errPrefix, " is not of any supported type.")})`;
}
code += "}";
output.push(code);
Expand All @@ -287,12 +303,12 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e

function generateSequence() {
const conv = generateTypeConversion(ctx, "nextItem", idlType.idlType[0], [], parentName,
`${errPrefix} + "'s element"`);
appendToErrorPrefix(errPrefix, "'s element"));
requires.merge(conv.requires);

str += `
if (!utils.isObject(${name})) {
throw new TypeError(${errPrefix} + " is not an iterable object.");
throw new TypeError(${appendToErrorPrefix(errPrefix, " is not an iterable object.")});
} else {
const V = [];
const tmp = ${name};
Expand All @@ -307,15 +323,15 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e

function generateRecord() {
const keyConv = generateTypeConversion(ctx, "typedKey", idlType.idlType[0], [], parentName,
`${errPrefix} + "'s key"`);
appendToErrorPrefix(errPrefix, "'s key"));
requires.merge(keyConv.requires);
const valConv = generateTypeConversion(ctx, "typedValue", idlType.idlType[1], [], parentName,
`${errPrefix} + "'s value"`);
appendToErrorPrefix(errPrefix, "'s value"));
requires.merge(valConv.requires);

str += `
if (!utils.isObject(${name})) {
throw new TypeError(${errPrefix} + " is not an object.");
throw new TypeError(${appendToErrorPrefix(errPrefix, " is not an object.")});
} else {
const result = Object.create(null);
for (const key of Reflect.ownKeys(${name})) {
Expand All @@ -341,7 +357,7 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
handler = "";
} else {
const conv = generateTypeConversion(ctx, "value", idlType.idlType[0], [], parentName,
`${errPrefix} + " promise value"`);
appendToErrorPrefix(errPrefix, " promise value"));
requires.merge(conv.requires);
handler = `
${conv.body}
Expand Down
Loading

0 comments on commit 5fc3c6b

Please sign in to comment.