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

Avoid useless ifelse check during fallback type conversion #232

Closed
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
41 changes: 25 additions & 16 deletions lib/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,32 +273,41 @@ module.exports.generateOverloadConversions = function (ctx, typeOfOp, name, pare
}

const booleans = S.filter(o => isOrIncludes(ctx, o.typeList[d], t => t.idlType === "boolean"));
if (booleans.length) {
possibilities.push(`
if (typeof curArg === "boolean") {
${continued(booleans[0], i)}
}
`);
}

const numerics = S.filter(o => isOrIncludes(ctx, o.typeList[d], t => Types.numericTypes.has(t.idlType)));
if (numerics.length) {
possibilities.push(`
if (typeof curArg === "number") {
${continued(numerics[0], i)}
}
`);
}

const strings = S.filter(o => {
return isOrIncludes(ctx, o.typeList[d], t => {
return Types.stringTypes.has(t.idlType) || ctx.enumerations.has(t.idlType);
});
});

const any = S.filter(o => isOrIncludes(ctx, o.typeList[d], t => t.idlType === "any"));
if (strings.length) {
if (booleans.length) {
possibilities.push(`
if (typeof curArg === "boolean") {
${continued(booleans[0], i)}
}
`);
}

if (numerics.length) {
possibilities.push(`
if (typeof curArg === "number") {
${continued(numerics[0], i)}
}
`);
}

possibilities.push(`{ ${continued(strings[0], i)} }`);
} else if (numerics.length) {
if (booleans.length) {
possibilities.push(`
if (typeof curArg === "boolean") {
${continued(booleans[0], i)}
}
`);
}

possibilities.push(`{ ${continued(numerics[0], i)} }`);
} else if (booleans.length) {
possibilities.push(`{ ${continued(booleans[0], i)} }`);
Expand Down
36 changes: 19 additions & 17 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
if (union.ArrayBufferViews.size > 0 || union.object) {
let condition = `ArrayBuffer.isView(${name})`;
// Skip specific type check if all ArrayBufferView member types are allowed.
if (union.ArrayBufferViews.size !== arrayBufferViewTypes.size) {
if (union.ArrayBufferViews.size !== 0 &&
union.ArrayBufferViews.size !== arrayBufferViewTypes.size) {
const exprs = [...union.ArrayBufferViews].map(a => `${name}.constructor.name === "${a}"`);
condition += ` && (${exprs.join(" || ")})`;
}
Expand Down Expand Up @@ -272,25 +273,26 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
output.push(code);
}

if (union.boolean) {
output.push(`
if (typeof ${name} === "boolean") {
${generateTypeConversion(ctx, name, union.boolean, [], parentName, errPrefix).body}
}
`);
}
{
const { string, numeric, boolean } = union;
if (boolean && (string || numeric)) {
output.push(`
if (typeof ${name} === "boolean") {
${generateTypeConversion(ctx, name, boolean, [], parentName, errPrefix).body}
}
`);
}

if (union.numeric) {
output.push(`
if (typeof ${name} === "number") {
${generateTypeConversion(ctx, name, union.numeric, [], parentName, errPrefix).body}
}
`);
}
if (numeric && string) {
output.push(`
if (typeof ${name} === "number") {
${generateTypeConversion(ctx, name, numeric, [], parentName, errPrefix).body}
}
`);
}

{
let code = "{";
const type = union.string || union.numeric || union.boolean;
const type = string || numeric || boolean;
if (type) {
const conv = generateTypeConversion(ctx, name, type, [], parentName, errPrefix);
code += conv.body;
Expand Down
Loading