Skip to content
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

Precompute concatenated strings where possible #221

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 2 additions & 4 deletions lib/constructs/iterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ 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.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep this the way it is.

}
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
7 changes: 3 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,7 @@ 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 +71,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
34 changes: 23 additions & 11 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}

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 @@ -211,25 +223,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 @@ -269,7 +281,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 @@ -280,12 +292,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 @@ -300,15 +312,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 @@ -334,7 +346,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