Skip to content

Avoid useless ifelse check during fallback type conversion #232

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

Closed
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
@@ -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)} }`);
36 changes: 19 additions & 17 deletions lib/types.js
Original file line number Diff line number Diff line change
@@ -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(" || ")})`;
}
@@ -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;
2,042 changes: 1,800 additions & 242 deletions test/__snapshots__/test.js.snap

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions test/cases/NoUselessIfElse.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[Exposed=Window]
interface NoUselessIfElse {
// # Overloads:
undefined overloadsObjectOrBoolean(object arg1);
undefined overloadsObjectOrBoolean(boolean arg1);

undefined overloadsObjectOrBooleanOrNumeric(object arg1);
undefined overloadsObjectOrBooleanOrNumeric(boolean arg1);
undefined overloadsObjectOrBooleanOrNumeric(long arg1);

undefined overloadsObjectOrNumeric(object arg1);
undefined overloadsObjectOrNumeric(long arg1);

undefined overloadsBooleanOrNumeric(boolean arg1);
undefined overloadsBooleanOrNumeric(long arg1);

// ## Avoid regressions:
undefined overloadsObjectOrBooleanOrString(object arg1);
undefined overloadsObjectOrBooleanOrString(boolean arg1);
undefined overloadsObjectOrBooleanOrString(DOMString arg1);

undefined overloadsObjectOrBooleanOrNumericOrString(object arg1);
undefined overloadsObjectOrBooleanOrNumericOrString(boolean arg1);
undefined overloadsObjectOrBooleanOrNumericOrString(long arg1);
undefined overloadsObjectOrBooleanOrNumericOrString(DOMString arg1);

undefined overloadsObjectOrNumericOrString(object arg1);
undefined overloadsObjectOrNumericOrString(long arg1);
undefined overloadsObjectOrNumericOrString(DOMString arg1);

undefined overloadsBooleanOrNumericOrString(boolean arg1);
undefined overloadsBooleanOrNumericOrString(long arg1);
undefined overloadsBooleanOrNumericOrString(DOMString arg1);

// # Unions:
undefined unionObjectOrBoolean((object or boolean) arg1);
undefined unionObjectOrBooleanOrNumeric((object or boolean or long) arg1);
undefined unionObjectOrNumeric((object or long) arg1);
undefined unionBooleanOrNumeric((boolean or long) arg1);

// ## Avoid regressions:
undefined unionObjectOrBooleanOrString((object or boolean or DOMString) arg1);
undefined unionObjectOrBooleanOrNumericOrString((object or boolean or long or DOMString) arg1);
undefined unionObjectOrNumericOrString((object or long or DOMString) arg1);
undefined unionBooleanOrNumericOrString((boolean or long or DOMString) arg1);
};