Skip to content

Generate Worker iterable types #877

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

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
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
139 changes: 139 additions & 0 deletions baselines/webworker.iterable.generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/////////////////////////////
/// Worker Iterable APIs
/////////////////////////////

interface Cache {
addAll(requests: Iterable<RequestInfo>): Promise<void>;
}

interface CanvasPathDrawingStyles {
setLineDash(segments: Iterable<number>): void;
}

interface DOMStringList {
[Symbol.iterator](): IterableIterator<string>;
}

interface FileList {
[Symbol.iterator](): IterableIterator<File>;
}

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<string>;
/**
* Returns a list of values in the list.
*/
values(): IterableIterator<FormDataEntryValue>;
}

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<string>;
/**
* Returns an iterator allowing to go through all values of the key/value pairs contained in this object.
*/
values(): IterableIterator<string>;
}

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<string>, 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<string>;
/**
* Returns a list of values in the search params.
*/
values(): IterableIterator<string>;
}

interface WEBGL_draw_buffers {
drawBuffersWEBGL(buffers: Iterable<GLenum>): void;
}

interface WebGL2RenderingContextBase {
clearBufferfv(buffer: GLenum, drawbuffer: GLint, values: Iterable<GLfloat>, srcOffset?: GLuint): void;
clearBufferiv(buffer: GLenum, drawbuffer: GLint, values: Iterable<GLint>, srcOffset?: GLuint): void;
clearBufferuiv(buffer: GLenum, drawbuffer: GLint, values: Iterable<GLuint>, srcOffset?: GLuint): void;
drawBuffers(buffers: Iterable<GLenum>): void;
getActiveUniforms(program: WebGLProgram, uniformIndices: Iterable<GLuint>, pname: GLenum): any;
getUniformIndices(program: WebGLProgram, uniformNames: Iterable<string>): Iterable<GLuint> | null;
invalidateFramebuffer(target: GLenum, attachments: Iterable<GLenum>): void;
invalidateSubFramebuffer(target: GLenum, attachments: Iterable<GLenum>, x: GLint, y: GLint, width: GLsizei, height: GLsizei): void;
transformFeedbackVaryings(program: WebGLProgram, varyings: Iterable<string>, bufferMode: GLenum): void;
uniform1uiv(location: WebGLUniformLocation | null, data: Iterable<GLuint>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniform2uiv(location: WebGLUniformLocation | null, data: Iterable<GLuint>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniform3uiv(location: WebGLUniformLocation | null, data: Iterable<GLuint>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniform4uiv(location: WebGLUniformLocation | null, data: Iterable<GLuint>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniformMatrix2x3fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniformMatrix2x4fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniformMatrix3x2fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniformMatrix3x4fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniformMatrix4x2fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniformMatrix4x3fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
vertexAttribI4iv(index: GLuint, values: Iterable<GLint>): void;
vertexAttribI4uiv(index: GLuint, values: Iterable<GLuint>): void;
}

interface WebGL2RenderingContextOverloads {
uniform1fv(location: WebGLUniformLocation | null, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniform1iv(location: WebGLUniformLocation | null, data: Iterable<GLint>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniform2fv(location: WebGLUniformLocation | null, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniform2iv(location: WebGLUniformLocation | null, data: Iterable<GLint>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniform3fv(location: WebGLUniformLocation | null, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniform3iv(location: WebGLUniformLocation | null, data: Iterable<GLint>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniform4fv(location: WebGLUniformLocation | null, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniform4iv(location: WebGLUniformLocation | null, data: Iterable<GLint>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniformMatrix2fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniformMatrix3fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
uniformMatrix4fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
}

interface WebGLRenderingContextBase {
vertexAttrib1fv(index: GLuint, values: Iterable<GLfloat>): void;
vertexAttrib2fv(index: GLuint, values: Iterable<GLfloat>): void;
vertexAttrib3fv(index: GLuint, values: Iterable<GLfloat>): void;
vertexAttrib4fv(index: GLuint, values: Iterable<GLfloat>): void;
}

interface WebGLRenderingContextOverloads {
uniform1fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
uniform1iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
uniform2fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
uniform2iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
uniform3fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
uniform3iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
uniform4fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
uniform4iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
uniformMatrix2fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable<GLfloat>): void;
uniformMatrix3fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable<GLfloat>): void;
uniformMatrix4fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable<GLfloat>): void;
}
24 changes: 14 additions & 10 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<string, string> = {};
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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};`);
Expand Down Expand Up @@ -1174,7 +1173,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {

emitCallBackFunctions();

if (flavor !== Flavor.Worker) {
if (flavor === Flavor.Window) {
emitHTMLElementTagNameMap();
emitHTMLElementDeprecatedTagNameMap();
emitSVGElementTagNameMap();
Expand Down Expand Up @@ -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
Expand Down
37 changes: 15 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,22 @@ function mergeNamesakes(filtered: Browser.WebIdl) {
}
}

function emitDomWorker(webidl: Browser.WebIdl, tsWorkerOutput: string, forceKnownWorkerTypes: Set<string>) {
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<string>) {
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<string>, 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() {
Expand All @@ -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"));
Expand Down Expand Up @@ -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>): Browser.WebIdl {
return filterByNull(obj, template);
Expand Down