Skip to content

Commit 47447ef

Browse files
Simplify generated code for object type in unions
This also fixes the crash reported in #231.
1 parent ff48336 commit 47447ef

File tree

3 files changed

+101
-30
lines changed

3 files changed

+101
-30
lines changed

lib/types.js

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,13 @@ function generateTypeConversion(
202202
}
203203

204204
if (union.object) {
205-
output.push(`if (utils.isObject(${name}) && ${name}[utils.implSymbol]) {
206-
${name} = utils.implForWrapper(${name});
207-
}`);
205+
output.push(`
206+
if (utils.isObject(${name})) {
207+
if (${name}[utils.implSymbol]) {
208+
${name} = utils.implForWrapper(${name});
209+
}
210+
}
211+
`);
208212
} else if (union.interfaces.size > 0) {
209213
const exprs = [...union.interfaces].map(iface => {
210214
let fn;
@@ -224,17 +228,17 @@ function generateTypeConversion(
224228
`);
225229
}
226230

227-
if (union.ArrayBuffer || union.object) {
231+
if (union.ArrayBuffer) {
228232
output.push(`if (utils.isArrayBuffer(${name})) {
229233
${generateTypeConversion(ctx, name, union.ArrayBuffer, [], parentName, errPrefix).body}
230234
}`);
231235
}
232-
if (union.SharedArrayBuffer || union.object) {
236+
if (union.SharedArrayBuffer) {
233237
output.push(`if (utils.isSharedArrayBuffer(${name})) {
234238
${generateTypeConversion(ctx, name, union.SharedArrayBuffer, [], parentName, errPrefix).body}
235239
}`);
236240
}
237-
if (union.ArrayBufferViews.size > 0 || union.object) {
241+
if (union.ArrayBufferViews.size > 0) {
238242
const viewIdlTypes = new Set([...union.ArrayBufferViews].map(item => item.idlType));
239243
// Skip specific type check if all ArrayBufferView member types are allowed.
240244
if (viewIdlTypes.size === arrayBufferViewTypes.size) {
@@ -253,30 +257,24 @@ function generateTypeConversion(
253257
}
254258
}
255259

256-
if (union.callbackFunction || union.object) {
257-
let code = `if (typeof ${name} === "function") {`;
258-
259-
if (union.callbackFunction) {
260-
const conv = generateTypeConversion(
261-
ctx,
262-
name,
263-
union.callbackFunction,
264-
[],
265-
parentName,
266-
`${errPrefix} + " callback function"`
267-
);
268-
requires.merge(conv.requires);
269-
code += conv.body;
270-
} else if (union.object) {
271-
// noop
272-
}
273-
274-
code += "}";
275-
276-
output.push(code);
260+
if (union.callbackFunction) {
261+
const conv = generateTypeConversion(
262+
ctx,
263+
name,
264+
union.callbackFunction,
265+
[],
266+
parentName,
267+
`${errPrefix} + " callback function"`
268+
);
269+
requires.merge(conv.requires);
270+
output.push(`
271+
if (typeof ${name} === "function") {
272+
${conv.body}
273+
}
274+
`);
277275
}
278276

279-
if (union.sequenceLike || union.dictionary || union.record || union.object || union.callbackInterface) {
277+
if (union.sequenceLike || union.dictionary || union.record || union.callbackInterface) {
280278
let code = `if (utils.isObject(${name})) {`;
281279

282280
if (union.sequenceLike) {
@@ -320,8 +318,6 @@ function generateTypeConversion(
320318
);
321319
requires.merge(conv.requires);
322320
code += conv.body;
323-
} else if (union.object) {
324-
// noop
325321
}
326322

327323
if (union.sequenceLike) {

test/__snapshots__/test.js.snap

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8645,6 +8645,42 @@ exports.install = (globalObject, globalNames) => {
86458645
return esValue[implSymbol].numOrStrOrURLOrNullConsumer(...args);
86468646
}
86478647

8648+
numOrObjConsumer(a) {
8649+
const esValue = this !== null && this !== undefined ? this : globalObject;
8650+
if (!exports.is(esValue)) {
8651+
throw new globalObject.TypeError(
8652+
"'numOrObjConsumer' called on an object that is not a valid instance of TypedefsAndUnions."
8653+
);
8654+
}
8655+
8656+
if (arguments.length < 1) {
8657+
throw new globalObject.TypeError(
8658+
\`Failed to execute 'numOrObjConsumer' on 'TypedefsAndUnions': 1 argument required, but only \${arguments.length} present.\`
8659+
);
8660+
}
8661+
const args = [];
8662+
{
8663+
let curArg = arguments[0];
8664+
if (utils.isObject(curArg)) {
8665+
if (curArg[utils.implSymbol]) {
8666+
curArg = utils.implForWrapper(curArg);
8667+
}
8668+
} else if (typeof curArg === "number") {
8669+
curArg = conversions["double"](curArg, {
8670+
context: "Failed to execute 'numOrObjConsumer' on 'TypedefsAndUnions': parameter 1",
8671+
globals: globalObject
8672+
});
8673+
} else {
8674+
curArg = conversions["double"](curArg, {
8675+
context: "Failed to execute 'numOrObjConsumer' on 'TypedefsAndUnions': parameter 1",
8676+
globals: globalObject
8677+
});
8678+
}
8679+
args.push(curArg);
8680+
}
8681+
return esValue[implSymbol].numOrObjConsumer(...args);
8682+
}
8683+
86488684
urlMapInnerConsumer(a) {
86498685
const esValue = this !== null && this !== undefined ? this : globalObject;
86508686
if (!exports.is(esValue)) {
@@ -9080,6 +9116,7 @@ exports.install = (globalObject, globalNames) => {
90809116
numOrEnumConsumer: { enumerable: true },
90819117
numOrStrOrNullConsumer: { enumerable: true },
90829118
numOrStrOrURLOrNullConsumer: { enumerable: true },
9119+
numOrObjConsumer: { enumerable: true },
90839120
urlMapInnerConsumer: { enumerable: true },
90849121
urlMapConsumer: { enumerable: true },
90859122
bufferSourceOrURLConsumer: { enumerable: true },
@@ -20379,6 +20416,42 @@ exports.install = (globalObject, globalNames) => {
2037920416
return esValue[implSymbol].numOrStrOrURLOrNullConsumer(...args);
2038020417
}
2038120418

20419+
numOrObjConsumer(a) {
20420+
const esValue = this !== null && this !== undefined ? this : globalObject;
20421+
if (!exports.is(esValue)) {
20422+
throw new globalObject.TypeError(
20423+
"'numOrObjConsumer' called on an object that is not a valid instance of TypedefsAndUnions."
20424+
);
20425+
}
20426+
20427+
if (arguments.length < 1) {
20428+
throw new globalObject.TypeError(
20429+
\`Failed to execute 'numOrObjConsumer' on 'TypedefsAndUnions': 1 argument required, but only \${arguments.length} present.\`
20430+
);
20431+
}
20432+
const args = [];
20433+
{
20434+
let curArg = arguments[0];
20435+
if (utils.isObject(curArg)) {
20436+
if (curArg[utils.implSymbol]) {
20437+
curArg = utils.implForWrapper(curArg);
20438+
}
20439+
} else if (typeof curArg === "number") {
20440+
curArg = conversions["double"](curArg, {
20441+
context: "Failed to execute 'numOrObjConsumer' on 'TypedefsAndUnions': parameter 1",
20442+
globals: globalObject
20443+
});
20444+
} else {
20445+
curArg = conversions["double"](curArg, {
20446+
context: "Failed to execute 'numOrObjConsumer' on 'TypedefsAndUnions': parameter 1",
20447+
globals: globalObject
20448+
});
20449+
}
20450+
args.push(curArg);
20451+
}
20452+
return esValue[implSymbol].numOrObjConsumer(...args);
20453+
}
20454+
2038220455
urlMapInnerConsumer(a) {
2038320456
const esValue = this !== null && this !== undefined ? this : globalObject;
2038420457
if (!exports.is(esValue)) {
@@ -20814,6 +20887,7 @@ exports.install = (globalObject, globalNames) => {
2081420887
numOrEnumConsumer: { enumerable: true },
2081520888
numOrStrOrNullConsumer: { enumerable: true },
2081620889
numOrStrOrURLOrNullConsumer: { enumerable: true },
20890+
numOrObjConsumer: { enumerable: true },
2081720891
urlMapInnerConsumer: { enumerable: true },
2081820892
urlMapConsumer: { enumerable: true },
2081920893
bufferSourceOrURLConsumer: { enumerable: true },

test/cases/TypedefsAndUnions.webidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ interface TypedefsAndUnions {
44
undefined numOrEnumConsumer((double or RequestDestination)? a);
55
undefined numOrStrOrNullConsumer(NumOrStrOrNull a);
66
undefined numOrStrOrURLOrNullConsumer(NumOrStrOrURLOrNull? a);
7+
undefined numOrObjConsumer((double or object) a);
78
undefined urlMapInnerConsumer(URLMapInner a);
89
undefined urlMapConsumer(URLMap a);
910
undefined bufferSourceOrURLConsumer((BufferSource or URL) b);

0 commit comments

Comments
 (0)