diff --git a/baselines/webworker.iterable.generated.d.ts b/baselines/webworker.iterable.generated.d.ts new file mode 100644 index 000000000..61b4e2e74 --- /dev/null +++ b/baselines/webworker.iterable.generated.d.ts @@ -0,0 +1,139 @@ +///////////////////////////// +/// Worker Iterable APIs +///////////////////////////// + +interface Cache { + addAll(requests: Iterable): Promise; +} + +interface CanvasPathDrawingStyles { + setLineDash(segments: Iterable): void; +} + +interface DOMStringList { + [Symbol.iterator](): IterableIterator; +} + +interface FileList { + [Symbol.iterator](): IterableIterator; +} + +interface FormData { + [Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>; + /** + * Returns an array of key, value pairs for every entry in the list. + */ + entries(): IterableIterator<[string, FormDataEntryValue]>; + /** + * Returns a list of keys in the list. + */ + keys(): IterableIterator; + /** + * Returns a list of values in the list. + */ + values(): IterableIterator; +} + +interface Headers { + [Symbol.iterator](): IterableIterator<[string, string]>; + /** + * Returns an iterator allowing to go through all key/value pairs contained in this object. + */ + entries(): IterableIterator<[string, string]>; + /** + * Returns an iterator allowing to go through all keys of the key/value pairs contained in this object. + */ + keys(): IterableIterator; + /** + * Returns an iterator allowing to go through all values of the key/value pairs contained in this object. + */ + values(): IterableIterator; +} + +interface IDBObjectStore { + /** + * 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. + * + * Throws an "InvalidStateError" DOMException if not called within an upgrade transaction. + */ + createIndex(name: string, keyPath: string | Iterable, options?: IDBIndexParameters): IDBIndex; +} + +interface URLSearchParams { + [Symbol.iterator](): IterableIterator<[string, string]>; + /** + * Returns an array of key, value pairs for every entry in the search params. + */ + entries(): IterableIterator<[string, string]>; + /** + * Returns a list of keys in the search params. + */ + keys(): IterableIterator; + /** + * Returns a list of values in the search params. + */ + values(): IterableIterator; +} + +interface WEBGL_draw_buffers { + drawBuffersWEBGL(buffers: Iterable): void; +} + +interface WebGL2RenderingContextBase { + clearBufferfv(buffer: GLenum, drawbuffer: GLint, values: Iterable, srcOffset?: GLuint): void; + clearBufferiv(buffer: GLenum, drawbuffer: GLint, values: Iterable, srcOffset?: GLuint): void; + clearBufferuiv(buffer: GLenum, drawbuffer: GLint, values: Iterable, srcOffset?: GLuint): void; + drawBuffers(buffers: Iterable): void; + getActiveUniforms(program: WebGLProgram, uniformIndices: Iterable, pname: GLenum): any; + getUniformIndices(program: WebGLProgram, uniformNames: Iterable): Iterable | null; + invalidateFramebuffer(target: GLenum, attachments: Iterable): void; + invalidateSubFramebuffer(target: GLenum, attachments: Iterable, x: GLint, y: GLint, width: GLsizei, height: GLsizei): void; + transformFeedbackVaryings(program: WebGLProgram, varyings: Iterable, bufferMode: GLenum): void; + uniform1uiv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniform2uiv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniform3uiv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniform4uiv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniformMatrix2x3fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniformMatrix2x4fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniformMatrix3x2fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniformMatrix3x4fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniformMatrix4x2fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniformMatrix4x3fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + vertexAttribI4iv(index: GLuint, values: Iterable): void; + vertexAttribI4uiv(index: GLuint, values: Iterable): void; +} + +interface WebGL2RenderingContextOverloads { + uniform1fv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniform1iv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniform2fv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniform2iv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniform3fv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniform3iv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniform4fv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniform4iv(location: WebGLUniformLocation | null, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniformMatrix2fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniformMatrix3fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; + uniformMatrix4fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable, srcOffset?: GLuint, srcLength?: GLuint): void; +} + +interface WebGLRenderingContextBase { + vertexAttrib1fv(index: GLuint, values: Iterable): void; + vertexAttrib2fv(index: GLuint, values: Iterable): void; + vertexAttrib3fv(index: GLuint, values: Iterable): void; + vertexAttrib4fv(index: GLuint, values: Iterable): void; +} + +interface WebGLRenderingContextOverloads { + uniform1fv(location: WebGLUniformLocation | null, v: Iterable): void; + uniform1iv(location: WebGLUniformLocation | null, v: Iterable): void; + uniform2fv(location: WebGLUniformLocation | null, v: Iterable): void; + uniform2iv(location: WebGLUniformLocation | null, v: Iterable): void; + uniform3fv(location: WebGLUniformLocation | null, v: Iterable): void; + uniform3iv(location: WebGLUniformLocation | null, v: Iterable): void; + uniform4fv(location: WebGLUniformLocation | null, v: Iterable): void; + uniform4iv(location: WebGLUniformLocation | null, v: Iterable): void; + uniformMatrix2fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable): void; + uniformMatrix3fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable): void; + uniformMatrix4fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable): void; +} diff --git a/src/emitter.ts b/src/emitter.ts index 6c6080b8f..9ceadb9af 100644 --- a/src/emitter.ts +++ b/src/emitter.ts @@ -3,9 +3,8 @@ import { mapToArray, distinct, map, toNameMap, mapDefined, arrayToMap, flatMap, import { collectLegacyNamespaceTypes } from "./legacy-namespace"; export const enum Flavor { - Web, - Worker, - ES6Iterators + Window, + Worker } // Note: @@ -120,11 +119,11 @@ function isEventHandler(p: Browser.Property) { return typeof p["event-handler"] === "string"; } -export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) { +export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boolean) { // Global print target const printer = createTextWriter("\n"); - const pollutor = getElements(webidl.interfaces, "interface").find(i => flavor === Flavor.Web ? !!i["primary-global"] : !!i.global); + const pollutor = getElements(webidl.interfaces, "interface").find(i => flavor === Flavor.Window ? !!i["primary-global"] : !!i.global); const allNonCallbackInterfaces = getElements(webidl.interfaces, "interface").concat(getElements(webidl.mixins, "mixin")); const allInterfaces = getElements(webidl.interfaces, "interface").concat( @@ -182,7 +181,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) { const iNameToConstParents = arrayToMap(allInterfaces, i => i.name, getParentsWithConstant); - return flavor === Flavor.ES6Iterators ? emitES6DomIterators() : emit(); + return iterator ? emitES6DomIterators() : emit(); function getTagNameToElementNameMap() { const htmlResult: Record = {}; @@ -340,7 +339,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) { } function convertDomTypeToTsTypeSimple(objDomType: string): string { - if (objDomType === "sequence" && flavor === Flavor.ES6Iterators) { + if (objDomType === "sequence" && iterator) { return "Iterable"; } if (baseTypeConversionMap.has(objDomType)) { @@ -830,7 +829,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) { printer.printLine("};"); printer.printLine(""); - if (flavor === Flavor.Web && i["legacy-window-alias"]) { + if (flavor === Flavor.Window && i["legacy-window-alias"]) { for (const alias of i["legacy-window-alias"]!) { printer.printLine(`type ${alias} = ${i.name};`); printer.printLine(`declare var ${alias}: typeof ${i.name};`); @@ -1174,7 +1173,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) { emitCallBackFunctions(); - if (flavor !== Flavor.Worker) { + if (flavor === Flavor.Window) { emitHTMLElementTagNameMap(); emitHTMLElementDeprecatedTagNameMap(); emitSVGElementTagNameMap(); @@ -1345,7 +1344,12 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) { function emitES6DomIterators() { printer.reset(); printer.printLine("/////////////////////////////"); - printer.printLine("/// DOM Iterable APIs"); + if (flavor === Flavor.Worker) { + printer.printLine("/// Worker Iterable APIs"); + } + else { + printer.printLine("/// DOM Iterable APIs"); + } printer.printLine("/////////////////////////////"); allInterfaces diff --git a/src/index.ts b/src/index.ts index 2af14aacd..6af231e5c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,24 +25,22 @@ function mergeNamesakes(filtered: Browser.WebIdl) { } } -function emitDomWorker(webidl: Browser.WebIdl, tsWorkerOutput: string, forceKnownWorkerTypes: Set) { - const worker = getExposedTypes(webidl, "Worker", forceKnownWorkerTypes); - mergeNamesakes(worker); - const result = emitWebIdl(worker, Flavor.Worker); - fs.writeFileSync(tsWorkerOutput, result); - return; +interface EmitOptions { + flavor: Flavor; + global: string; + name: string; + outputFolder: string; } -function emitDomWeb(webidl: Browser.WebIdl, tsWebOutput: string, forceKnownWindowTypes: Set) { - const browser = getExposedTypes(webidl, "Window", forceKnownWindowTypes); - mergeNamesakes(browser); - const result = emitWebIdl(browser, Flavor.Web); - fs.writeFileSync(tsWebOutput, result); - return; -} +function emitFlavor(webidl: Browser.WebIdl, forceKnownTypes: Set, options: EmitOptions) { + const exposed = getExposedTypes(webidl, options.global, forceKnownTypes); + mergeNamesakes(exposed); + + const result = emitWebIdl(exposed, options.flavor, false); + fs.writeFileSync(`${options.outputFolder}/${options.name}.generated.d.ts`, result); -function emitES6DomIterators(webidl: Browser.WebIdl, tsWebIteratorsOutput: string) { - fs.writeFileSync(tsWebIteratorsOutput, emitWebIdl(webidl, Flavor.ES6Iterators)); + const iterators = emitWebIdl(exposed, options.flavor, true); + fs.writeFileSync(`${options.outputFolder}/${options.name}.iterable.generated.d.ts`, iterators); } function emitDom() { @@ -65,10 +63,6 @@ function emitDom() { fs.mkdirSync(outputFolder); } - const tsWebOutput = path.join(outputFolder, "dom.generated.d.ts"); - const tsWebIteratorsOutput = path.join(outputFolder, "dom.iterable.generated.d.ts"); - const tsWorkerOutput = path.join(outputFolder, "webworker.generated.d.ts"); - const overriddenItems = require(path.join(inputFolder, "overridingTypes.json")); const addedItems = require(path.join(inputFolder, "addedTypes.json")); const comments = require(path.join(inputFolder, "comments.json")); @@ -184,9 +178,8 @@ function emitDom() { } } - emitDomWeb(webidl, tsWebOutput, new Set(knownTypes.Window)); - emitDomWorker(webidl, tsWorkerOutput, new Set(knownTypes.Worker)); - emitES6DomIterators(webidl, tsWebIteratorsOutput); + emitFlavor(webidl, new Set(knownTypes.Window), { name: "dom", flavor: Flavor.Window, global: "Window", outputFolder }); + emitFlavor(webidl, new Set(knownTypes.Worker), { name: "webworker", flavor: Flavor.Worker, global: "Worker", outputFolder }); function prune(obj: Browser.WebIdl, template: Partial): Browser.WebIdl { return filterByNull(obj, template);