Skip to content

Commit 499a2e9

Browse files
saschanazsandersn
authored andcommitted
Emit iterable arguments for ES2015+ (#699)
* fix compiler error * emit iterable arguments * apply renamings
1 parent ab3d46e commit 499a2e9

File tree

2 files changed

+115
-5
lines changed

2 files changed

+115
-5
lines changed

baselines/dom.iterable.generated.d.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22
/// DOM Iterable APIs
33
/////////////////////////////
44

5+
interface AudioParam {
6+
setValueCurveAtTime(values: Iterable<number>, startTime: number, duration: number): AudioParam;
7+
}
8+
59
interface AudioParamMap extends ReadonlyMap<string, AudioParam> {
610
}
711

812
interface AudioTrackList {
913
[Symbol.iterator](): IterableIterator<AudioTrack>;
1014
}
1115

16+
interface BaseAudioContext {
17+
createIIRFilter(feedforward: Iterable<number>, feedback: Iterable<number>): IIRFilterNode;
18+
createPeriodicWave(real: Iterable<number>, imag: Iterable<number>, constraints?: PeriodicWaveConstraints): PeriodicWave;
19+
}
20+
1221
interface CSSRuleList {
1322
[Symbol.iterator](): IterableIterator<CSSRule>;
1423
}
@@ -17,6 +26,14 @@ interface CSSStyleDeclaration {
1726
[Symbol.iterator](): IterableIterator<string>;
1827
}
1928

29+
interface Cache {
30+
addAll(requests: Iterable<RequestInfo>): Promise<void>;
31+
}
32+
33+
interface CanvasPathDrawingStyles {
34+
setLineDash(segments: Iterable<number>): void;
35+
}
36+
2037
interface ClientRectList {
2138
[Symbol.iterator](): IterableIterator<ClientRect>;
2239
}
@@ -96,6 +113,15 @@ interface Headers {
96113
values(): IterableIterator<string>;
97114
}
98115

116+
interface IDBObjectStore {
117+
/**
118+
* Creates a new index in store with the given name, keyPath and options and returns a new IDBIndex. If the keyPath and options define constraints that cannot be satisfied with the data already in store the upgrade transaction will abort with a "ConstraintError" DOMException.
119+
*
120+
* Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
121+
*/
122+
createIndex(name: string, keyPath: string | Iterable<string>, options?: IDBIndexParameters): IDBIndex;
123+
}
124+
99125
interface MediaKeyStatusMap {
100126
[Symbol.iterator](): IterableIterator<[BufferSource, MediaKeyStatus]>;
101127
entries(): IterableIterator<[BufferSource, MediaKeyStatus]>;
@@ -115,6 +141,10 @@ interface NamedNodeMap {
115141
[Symbol.iterator](): IterableIterator<Attr>;
116142
}
117143

144+
interface Navigator {
145+
requestMediaKeySystemAccess(keySystem: string, supportedConfigurations: Iterable<MediaKeySystemConfiguration>): Promise<MediaKeySystemAccess>;
146+
}
147+
118148
interface NodeList {
119149
[Symbol.iterator](): IterableIterator<Node>;
120150
/**
@@ -155,6 +185,10 @@ interface PluginArray {
155185
[Symbol.iterator](): IterableIterator<Plugin>;
156186
}
157187

188+
interface RTCRtpTransceiver {
189+
setCodecPreferences(codecs: Iterable<RTCRtpCodecCapability>): void;
190+
}
191+
158192
interface RTCStatsReport extends ReadonlyMap<string, any> {
159193
}
160194

@@ -222,6 +256,36 @@ interface URLSearchParams {
222256
values(): IterableIterator<string>;
223257
}
224258

259+
interface VRDisplay {
260+
requestPresent(layers: Iterable<VRLayer>): Promise<void>;
261+
}
262+
225263
interface VideoTrackList {
226264
[Symbol.iterator](): IterableIterator<VideoTrack>;
227265
}
266+
267+
interface WEBGL_draw_buffers {
268+
drawBuffersWEBGL(buffers: Iterable<GLenum>): void;
269+
}
270+
271+
interface WebAuthentication {
272+
makeCredential(accountInformation: Account, cryptoParameters: Iterable<ScopedCredentialParameters>, attestationChallenge: Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | DataView | ArrayBuffer | null, options?: ScopedCredentialOptions): Promise<ScopedCredentialInfo>;
273+
}
274+
275+
interface WebGLRenderingContextBase {
276+
uniform1fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
277+
uniform2fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
278+
uniform3fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
279+
uniform4fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
280+
uniform1iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
281+
uniform2iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
282+
uniform3iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
283+
uniform4iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
284+
uniformMatrix2fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable<GLfloat>): void;
285+
uniformMatrix3fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable<GLfloat>): void;
286+
uniformMatrix4fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable<GLfloat>): void;
287+
vertexAttrib1fv(index: GLuint, values: Iterable<GLfloat>): void;
288+
vertexAttrib2fv(index: GLuint, values: Iterable<GLfloat>): void;
289+
vertexAttrib3fv(index: GLuint, values: Iterable<GLfloat>): void;
290+
vertexAttrib4fv(index: GLuint, values: Iterable<GLfloat>): void;
291+
}

src/emitter.ts

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
325325
}
326326

327327
function convertDomTypeToTsTypeSimple(objDomType: string): string {
328+
if (objDomType === "sequence" && flavor === Flavor.ES6Iterators) {
329+
return "Iterable";
330+
}
328331
if (baseTypeConversionMap.has(objDomType)) {
329332
return baseTypeConversionMap.get(objDomType)!;
330333
}
@@ -1207,8 +1210,8 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
12071210
});
12081211
}
12091212

1210-
function getIteratorExtends(iterator: Browser.Iterator | undefined, subtypes: string[]) {
1211-
if (!iterator) {
1213+
function getIteratorExtends(iterator?: Browser.Iterator, subtypes?: string[]) {
1214+
if (!iterator || !subtypes) {
12121215
return "";
12131216
}
12141217
const base = iterator.kind === "maplike" ? `Map<${subtypes[0]}, ${subtypes[1]}>` :
@@ -1220,17 +1223,60 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
12201223
return `extends ${result} `;
12211224
}
12221225

1226+
function hasSequenceArgument(s: Browser.Signature) {
1227+
function typeIncludesSequence(type: string | Browser.Typed[]): boolean {
1228+
if (Array.isArray(type)) {
1229+
return type.some(t => typeIncludesSequence(t.type))
1230+
}
1231+
return type === "sequence" || !!sequenceTypedefMap[type];
1232+
}
1233+
return !!s.param && s.param.some(p => typeIncludesSequence(p.type));
1234+
}
1235+
1236+
function replaceTypedefsInSignatures(signatures: Browser.Signature[]): Browser.Signature[] {
1237+
return signatures.map(s => {
1238+
const params = s.param!.map(p => {
1239+
const typedef = typeof p.type === "string" ? sequenceTypedefMap[p.type] : undefined;
1240+
if (!typedef) {
1241+
return p;
1242+
}
1243+
return { ...p, type: typedef.type };
1244+
})
1245+
return { ...s, param: params };
1246+
});
1247+
}
1248+
1249+
const sequenceTypedefs = !webidl.typedefs ? [] :
1250+
webidl.typedefs.typedef
1251+
.filter(typedef => Array.isArray(typedef.type))
1252+
.map(typedef => ({ ...typedef, type: (typedef.type as Browser.Typed[]).filter(t => t.type === "sequence") }))
1253+
.filter(typedef => typedef.type.length)
1254+
const sequenceTypedefMap = arrayToMap(sequenceTypedefs, t => t["new-type"], t => t);
1255+
12231256
const subtypes = getIteratorSubtypes();
1224-
if (subtypes) {
1257+
const methodsWithSequence: Browser.Method[] =
1258+
mapToArray(i.methods ? i.methods.method : {})
1259+
.filter(m => m.signature && !m["override-signatures"])
1260+
.map(m => ({
1261+
...m,
1262+
signature: replaceTypedefsInSignatures(m.signature.filter(hasSequenceArgument))
1263+
}))
1264+
.filter(m => m.signature.length)
1265+
.sort();
1266+
1267+
if (subtypes || methodsWithSequence.length) {
12251268
const iteratorExtends = getIteratorExtends(i.iterator, subtypes);
12261269
const name = extendConflictsBaseTypes[i.name] ? `${i.name}Base` : i.name;
12271270
printer.printLine("");
12281271
printer.printLine(`interface ${name} ${iteratorExtends}{`);
12291272
printer.increaseIndent();
1230-
if (!iteratorExtends) {
1273+
1274+
methodsWithSequence.forEach(m => emitMethod("", m, new Set()));
1275+
1276+
if (subtypes && !iteratorExtends) {
12311277
printer.printLine(`[Symbol.iterator](): IterableIterator<${stringifySingleOrTupleTypes(subtypes)}>;`);
12321278
}
1233-
if (i.iterator && i.iterator.kind === "iterable") {
1279+
if (i.iterator && i.iterator.kind === "iterable" && subtypes) {
12341280
emitIterableDeclarationMethods(i, subtypes);
12351281
}
12361282
printer.decreaseIndent();

0 commit comments

Comments
 (0)