Skip to content

Commit ff48336

Browse files
Fix unions with callback functions and callback interfaces
1 parent 173441c commit ff48336

File tree

3 files changed

+166
-3
lines changed

3 files changed

+166
-3
lines changed

lib/types.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ function extractUnionInfo(ctx, idlType, errPrefix) {
594594
if (seen.dictionaryLike) {
595595
error("Callback functions are not distinguishable with dictionary-like types");
596596
}
597-
seen.callbackFunction = item.idlType;
597+
seen.callbackFunction = item;
598598
} else if (ctx.dictionaries.has(item.idlType)) {
599599
if (seen.object) {
600600
error("Dictionary-like types are not distinguishable with object type");
@@ -616,7 +616,7 @@ function extractUnionInfo(ctx, idlType, errPrefix) {
616616
if (seen.dictionaryLike) {
617617
error("There can only be one dictionary-like type in a union type");
618618
}
619-
seen.callbackInterface = item.idlType;
619+
seen.callbackInterface = item;
620620
} else if (ctx.interfaces.has(item.idlType)) {
621621
if (seen.object) {
622622
error("Interface types are not distinguishable with object type");
@@ -710,9 +710,10 @@ function sameType(ctx, type1, type2) {
710710
sameType(ctx, extracted1.numeric, extracted2.numeric) &&
711711
sameType(ctx, extracted1.boolean, extracted2.boolean) &&
712712
extracted1.callback === extracted2.callback &&
713+
sameType(ctx, extracted1.callbackFunction, extracted2.callbackFunction) &&
713714
sameType(ctx, extracted1.dictionary, extracted2.dictionary) &&
714715
sameArray([...extracted1.interfaces].sort(), [...extracted2.interfaces].sort()) &&
715-
extracted1.callbackInterface === extracted2.callbackInterface &&
716+
sameType(ctx, extracted1.callbackInterface, extracted2.callbackInterface) &&
716717
extracted1.unknown === extracted2.unknown;
717718
}
718719

test/__snapshots__/test.js.snap

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8407,6 +8407,8 @@ const utils = require("./utils.js");
84078407

84088408
const RequestDestination = require("./RequestDestination.js");
84098409
const URL = require("./URL.js");
8410+
const AsyncCallbackFunction = require("./AsyncCallbackFunction.js");
8411+
const AsyncCallbackInterface = require("./AsyncCallbackInterface.js");
84108412
const implSymbol = utils.implSymbol;
84118413
const ctorRegistrySymbol = utils.ctorRegistrySymbol;
84128414

@@ -8923,6 +8925,82 @@ exports.install = (globalObject, globalNames) => {
89238925
return esValue[implSymbol].arrayBufferOrSharedArrayBufferConsumer(...args);
89248926
}
89258927

8928+
callbackFunctionOrNumConsumer(cb) {
8929+
const esValue = this !== null && this !== undefined ? this : globalObject;
8930+
if (!exports.is(esValue)) {
8931+
throw new globalObject.TypeError(
8932+
"'callbackFunctionOrNumConsumer' called on an object that is not a valid instance of TypedefsAndUnions."
8933+
);
8934+
}
8935+
8936+
if (arguments.length < 1) {
8937+
throw new globalObject.TypeError(
8938+
\`Failed to execute 'callbackFunctionOrNumConsumer' on 'TypedefsAndUnions': 1 argument required, but only \${arguments.length} present.\`
8939+
);
8940+
}
8941+
const args = [];
8942+
{
8943+
let curArg = arguments[0];
8944+
if (typeof curArg === "function") {
8945+
curArg = AsyncCallbackFunction.convert(globalObject, curArg, {
8946+
context:
8947+
"Failed to execute 'callbackFunctionOrNumConsumer' on 'TypedefsAndUnions': parameter 1" +
8948+
" callback function"
8949+
});
8950+
} else if (typeof curArg === "number") {
8951+
curArg = conversions["double"](curArg, {
8952+
context: "Failed to execute 'callbackFunctionOrNumConsumer' on 'TypedefsAndUnions': parameter 1",
8953+
globals: globalObject
8954+
});
8955+
} else {
8956+
curArg = conversions["double"](curArg, {
8957+
context: "Failed to execute 'callbackFunctionOrNumConsumer' on 'TypedefsAndUnions': parameter 1",
8958+
globals: globalObject
8959+
});
8960+
}
8961+
args.push(curArg);
8962+
}
8963+
return esValue[implSymbol].callbackFunctionOrNumConsumer(...args);
8964+
}
8965+
8966+
callbackInterfaceOrNumConsumer(cb) {
8967+
const esValue = this !== null && this !== undefined ? this : globalObject;
8968+
if (!exports.is(esValue)) {
8969+
throw new globalObject.TypeError(
8970+
"'callbackInterfaceOrNumConsumer' called on an object that is not a valid instance of TypedefsAndUnions."
8971+
);
8972+
}
8973+
8974+
if (arguments.length < 1) {
8975+
throw new globalObject.TypeError(
8976+
\`Failed to execute 'callbackInterfaceOrNumConsumer' on 'TypedefsAndUnions': 1 argument required, but only \${arguments.length} present.\`
8977+
);
8978+
}
8979+
const args = [];
8980+
{
8981+
let curArg = arguments[0];
8982+
if (utils.isObject(curArg)) {
8983+
curArg = AsyncCallbackInterface.convert(globalObject, curArg, {
8984+
context:
8985+
"Failed to execute 'callbackInterfaceOrNumConsumer' on 'TypedefsAndUnions': parameter 1" +
8986+
" callback interface"
8987+
});
8988+
} else if (typeof curArg === "number") {
8989+
curArg = conversions["double"](curArg, {
8990+
context: "Failed to execute 'callbackInterfaceOrNumConsumer' on 'TypedefsAndUnions': parameter 1",
8991+
globals: globalObject
8992+
});
8993+
} else {
8994+
curArg = conversions["double"](curArg, {
8995+
context: "Failed to execute 'callbackInterfaceOrNumConsumer' on 'TypedefsAndUnions': parameter 1",
8996+
globals: globalObject
8997+
});
8998+
}
8999+
args.push(curArg);
9000+
}
9001+
return esValue[implSymbol].callbackInterfaceOrNumConsumer(...args);
9002+
}
9003+
89269004
get buf() {
89279005
const esValue = this !== null && this !== undefined ? this : globalObject;
89289006

@@ -9008,6 +9086,8 @@ exports.install = (globalObject, globalNames) => {
90089086
arrayBufferViewOrURLMapConsumer: { enumerable: true },
90099087
arrayBufferViewDupConsumer: { enumerable: true },
90109088
arrayBufferOrSharedArrayBufferConsumer: { enumerable: true },
9089+
callbackFunctionOrNumConsumer: { enumerable: true },
9090+
callbackInterfaceOrNumConsumer: { enumerable: true },
90119091
buf: { enumerable: true },
90129092
time: { enumerable: true },
90139093
[Symbol.toStringTag]: { value: "TypedefsAndUnions", configurable: true }
@@ -20061,6 +20141,8 @@ const utils = require("./utils.js");
2006120141

2006220142
const RequestDestination = require("./RequestDestination.js");
2006320143
const URL = require("./URL.js");
20144+
const AsyncCallbackFunction = require("./AsyncCallbackFunction.js");
20145+
const AsyncCallbackInterface = require("./AsyncCallbackInterface.js");
2006420146
const implSymbol = utils.implSymbol;
2006520147
const ctorRegistrySymbol = utils.ctorRegistrySymbol;
2006620148

@@ -20577,6 +20659,82 @@ exports.install = (globalObject, globalNames) => {
2057720659
return esValue[implSymbol].arrayBufferOrSharedArrayBufferConsumer(...args);
2057820660
}
2057920661

20662+
callbackFunctionOrNumConsumer(cb) {
20663+
const esValue = this !== null && this !== undefined ? this : globalObject;
20664+
if (!exports.is(esValue)) {
20665+
throw new globalObject.TypeError(
20666+
"'callbackFunctionOrNumConsumer' called on an object that is not a valid instance of TypedefsAndUnions."
20667+
);
20668+
}
20669+
20670+
if (arguments.length < 1) {
20671+
throw new globalObject.TypeError(
20672+
\`Failed to execute 'callbackFunctionOrNumConsumer' on 'TypedefsAndUnions': 1 argument required, but only \${arguments.length} present.\`
20673+
);
20674+
}
20675+
const args = [];
20676+
{
20677+
let curArg = arguments[0];
20678+
if (typeof curArg === "function") {
20679+
curArg = AsyncCallbackFunction.convert(globalObject, curArg, {
20680+
context:
20681+
"Failed to execute 'callbackFunctionOrNumConsumer' on 'TypedefsAndUnions': parameter 1" +
20682+
" callback function"
20683+
});
20684+
} else if (typeof curArg === "number") {
20685+
curArg = conversions["double"](curArg, {
20686+
context: "Failed to execute 'callbackFunctionOrNumConsumer' on 'TypedefsAndUnions': parameter 1",
20687+
globals: globalObject
20688+
});
20689+
} else {
20690+
curArg = conversions["double"](curArg, {
20691+
context: "Failed to execute 'callbackFunctionOrNumConsumer' on 'TypedefsAndUnions': parameter 1",
20692+
globals: globalObject
20693+
});
20694+
}
20695+
args.push(curArg);
20696+
}
20697+
return esValue[implSymbol].callbackFunctionOrNumConsumer(...args);
20698+
}
20699+
20700+
callbackInterfaceOrNumConsumer(cb) {
20701+
const esValue = this !== null && this !== undefined ? this : globalObject;
20702+
if (!exports.is(esValue)) {
20703+
throw new globalObject.TypeError(
20704+
"'callbackInterfaceOrNumConsumer' called on an object that is not a valid instance of TypedefsAndUnions."
20705+
);
20706+
}
20707+
20708+
if (arguments.length < 1) {
20709+
throw new globalObject.TypeError(
20710+
\`Failed to execute 'callbackInterfaceOrNumConsumer' on 'TypedefsAndUnions': 1 argument required, but only \${arguments.length} present.\`
20711+
);
20712+
}
20713+
const args = [];
20714+
{
20715+
let curArg = arguments[0];
20716+
if (utils.isObject(curArg)) {
20717+
curArg = AsyncCallbackInterface.convert(globalObject, curArg, {
20718+
context:
20719+
"Failed to execute 'callbackInterfaceOrNumConsumer' on 'TypedefsAndUnions': parameter 1" +
20720+
" callback interface"
20721+
});
20722+
} else if (typeof curArg === "number") {
20723+
curArg = conversions["double"](curArg, {
20724+
context: "Failed to execute 'callbackInterfaceOrNumConsumer' on 'TypedefsAndUnions': parameter 1",
20725+
globals: globalObject
20726+
});
20727+
} else {
20728+
curArg = conversions["double"](curArg, {
20729+
context: "Failed to execute 'callbackInterfaceOrNumConsumer' on 'TypedefsAndUnions': parameter 1",
20730+
globals: globalObject
20731+
});
20732+
}
20733+
args.push(curArg);
20734+
}
20735+
return esValue[implSymbol].callbackInterfaceOrNumConsumer(...args);
20736+
}
20737+
2058020738
get buf() {
2058120739
const esValue = this !== null && this !== undefined ? this : globalObject;
2058220740

@@ -20662,6 +20820,8 @@ exports.install = (globalObject, globalNames) => {
2066220820
arrayBufferViewOrURLMapConsumer: { enumerable: true },
2066320821
arrayBufferViewDupConsumer: { enumerable: true },
2066420822
arrayBufferOrSharedArrayBufferConsumer: { enumerable: true },
20823+
callbackFunctionOrNumConsumer: { enumerable: true },
20824+
callbackInterfaceOrNumConsumer: { enumerable: true },
2066520825
buf: { enumerable: true },
2066620826
time: { enumerable: true },
2066720827
[Symbol.toStringTag]: { value: "TypedefsAndUnions", configurable: true }

test/cases/TypedefsAndUnions.webidl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ interface TypedefsAndUnions {
1010
undefined arrayBufferViewOrURLMapConsumer((ArrayBufferView or URLMap) b);
1111
undefined arrayBufferViewDupConsumer((ArrayBufferView or Uint8ClampedArray) b);
1212
undefined arrayBufferOrSharedArrayBufferConsumer((ArrayBuffer or SharedArrayBuffer) b);
13+
undefined callbackFunctionOrNumConsumer((AsyncCallbackFunction or double) cb);
14+
undefined callbackInterfaceOrNumConsumer((AsyncCallbackInterface or double) cb);
1315

1416
attribute (ArrayBuffer or Uint8Array or Uint16Array) buf;
1517
attribute DOMTimeStamp time;

0 commit comments

Comments
 (0)