Skip to content

Commit 8cb1cdf

Browse files
author
Orta Therox
authored
Merge pull request #877 from saschanaz/worker-iterable
Generate Worker iterable types
2 parents e7cc431 + 0675fb8 commit 8cb1cdf

File tree

3 files changed

+168
-32
lines changed

3 files changed

+168
-32
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/////////////////////////////
2+
/// Worker Iterable APIs
3+
/////////////////////////////
4+
5+
interface Cache {
6+
addAll(requests: Iterable<RequestInfo>): Promise<void>;
7+
}
8+
9+
interface CanvasPathDrawingStyles {
10+
setLineDash(segments: Iterable<number>): void;
11+
}
12+
13+
interface DOMStringList {
14+
[Symbol.iterator](): IterableIterator<string>;
15+
}
16+
17+
interface FileList {
18+
[Symbol.iterator](): IterableIterator<File>;
19+
}
20+
21+
interface FormData {
22+
[Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>;
23+
/**
24+
* Returns an array of key, value pairs for every entry in the list.
25+
*/
26+
entries(): IterableIterator<[string, FormDataEntryValue]>;
27+
/**
28+
* Returns a list of keys in the list.
29+
*/
30+
keys(): IterableIterator<string>;
31+
/**
32+
* Returns a list of values in the list.
33+
*/
34+
values(): IterableIterator<FormDataEntryValue>;
35+
}
36+
37+
interface Headers {
38+
[Symbol.iterator](): IterableIterator<[string, string]>;
39+
/**
40+
* Returns an iterator allowing to go through all key/value pairs contained in this object.
41+
*/
42+
entries(): IterableIterator<[string, string]>;
43+
/**
44+
* Returns an iterator allowing to go through all keys of the key/value pairs contained in this object.
45+
*/
46+
keys(): IterableIterator<string>;
47+
/**
48+
* Returns an iterator allowing to go through all values of the key/value pairs contained in this object.
49+
*/
50+
values(): IterableIterator<string>;
51+
}
52+
53+
interface IDBObjectStore {
54+
/**
55+
* 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.
56+
*
57+
* Throws an "InvalidStateError" DOMException if not called within an upgrade transaction.
58+
*/
59+
createIndex(name: string, keyPath: string | Iterable<string>, options?: IDBIndexParameters): IDBIndex;
60+
}
61+
62+
interface URLSearchParams {
63+
[Symbol.iterator](): IterableIterator<[string, string]>;
64+
/**
65+
* Returns an array of key, value pairs for every entry in the search params.
66+
*/
67+
entries(): IterableIterator<[string, string]>;
68+
/**
69+
* Returns a list of keys in the search params.
70+
*/
71+
keys(): IterableIterator<string>;
72+
/**
73+
* Returns a list of values in the search params.
74+
*/
75+
values(): IterableIterator<string>;
76+
}
77+
78+
interface WEBGL_draw_buffers {
79+
drawBuffersWEBGL(buffers: Iterable<GLenum>): void;
80+
}
81+
82+
interface WebGL2RenderingContextBase {
83+
clearBufferfv(buffer: GLenum, drawbuffer: GLint, values: Iterable<GLfloat>, srcOffset?: GLuint): void;
84+
clearBufferiv(buffer: GLenum, drawbuffer: GLint, values: Iterable<GLint>, srcOffset?: GLuint): void;
85+
clearBufferuiv(buffer: GLenum, drawbuffer: GLint, values: Iterable<GLuint>, srcOffset?: GLuint): void;
86+
drawBuffers(buffers: Iterable<GLenum>): void;
87+
getActiveUniforms(program: WebGLProgram, uniformIndices: Iterable<GLuint>, pname: GLenum): any;
88+
getUniformIndices(program: WebGLProgram, uniformNames: Iterable<string>): Iterable<GLuint> | null;
89+
invalidateFramebuffer(target: GLenum, attachments: Iterable<GLenum>): void;
90+
invalidateSubFramebuffer(target: GLenum, attachments: Iterable<GLenum>, x: GLint, y: GLint, width: GLsizei, height: GLsizei): void;
91+
transformFeedbackVaryings(program: WebGLProgram, varyings: Iterable<string>, bufferMode: GLenum): void;
92+
uniform1uiv(location: WebGLUniformLocation | null, data: Iterable<GLuint>, srcOffset?: GLuint, srcLength?: GLuint): void;
93+
uniform2uiv(location: WebGLUniformLocation | null, data: Iterable<GLuint>, srcOffset?: GLuint, srcLength?: GLuint): void;
94+
uniform3uiv(location: WebGLUniformLocation | null, data: Iterable<GLuint>, srcOffset?: GLuint, srcLength?: GLuint): void;
95+
uniform4uiv(location: WebGLUniformLocation | null, data: Iterable<GLuint>, srcOffset?: GLuint, srcLength?: GLuint): void;
96+
uniformMatrix2x3fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
97+
uniformMatrix2x4fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
98+
uniformMatrix3x2fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
99+
uniformMatrix3x4fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
100+
uniformMatrix4x2fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
101+
uniformMatrix4x3fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
102+
vertexAttribI4iv(index: GLuint, values: Iterable<GLint>): void;
103+
vertexAttribI4uiv(index: GLuint, values: Iterable<GLuint>): void;
104+
}
105+
106+
interface WebGL2RenderingContextOverloads {
107+
uniform1fv(location: WebGLUniformLocation | null, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
108+
uniform1iv(location: WebGLUniformLocation | null, data: Iterable<GLint>, srcOffset?: GLuint, srcLength?: GLuint): void;
109+
uniform2fv(location: WebGLUniformLocation | null, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
110+
uniform2iv(location: WebGLUniformLocation | null, data: Iterable<GLint>, srcOffset?: GLuint, srcLength?: GLuint): void;
111+
uniform3fv(location: WebGLUniformLocation | null, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
112+
uniform3iv(location: WebGLUniformLocation | null, data: Iterable<GLint>, srcOffset?: GLuint, srcLength?: GLuint): void;
113+
uniform4fv(location: WebGLUniformLocation | null, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
114+
uniform4iv(location: WebGLUniformLocation | null, data: Iterable<GLint>, srcOffset?: GLuint, srcLength?: GLuint): void;
115+
uniformMatrix2fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
116+
uniformMatrix3fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
117+
uniformMatrix4fv(location: WebGLUniformLocation | null, transpose: GLboolean, data: Iterable<GLfloat>, srcOffset?: GLuint, srcLength?: GLuint): void;
118+
}
119+
120+
interface WebGLRenderingContextBase {
121+
vertexAttrib1fv(index: GLuint, values: Iterable<GLfloat>): void;
122+
vertexAttrib2fv(index: GLuint, values: Iterable<GLfloat>): void;
123+
vertexAttrib3fv(index: GLuint, values: Iterable<GLfloat>): void;
124+
vertexAttrib4fv(index: GLuint, values: Iterable<GLfloat>): void;
125+
}
126+
127+
interface WebGLRenderingContextOverloads {
128+
uniform1fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
129+
uniform1iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
130+
uniform2fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
131+
uniform2iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
132+
uniform3fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
133+
uniform3iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
134+
uniform4fv(location: WebGLUniformLocation | null, v: Iterable<GLfloat>): void;
135+
uniform4iv(location: WebGLUniformLocation | null, v: Iterable<GLint>): void;
136+
uniformMatrix2fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable<GLfloat>): void;
137+
uniformMatrix3fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable<GLfloat>): void;
138+
uniformMatrix4fv(location: WebGLUniformLocation | null, transpose: GLboolean, value: Iterable<GLfloat>): void;
139+
}

src/emitter.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { mapToArray, distinct, map, toNameMap, mapDefined, arrayToMap, flatMap,
33
import { collectLegacyNamespaceTypes } from "./legacy-namespace";
44

55
export const enum Flavor {
6-
Web,
7-
Worker,
8-
ES6Iterators
6+
Window,
7+
Worker
98
}
109

1110
// Note:
@@ -120,11 +119,11 @@ function isEventHandler(p: Browser.Property) {
120119
return typeof p["event-handler"] === "string";
121120
}
122121

123-
export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
122+
export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boolean) {
124123
// Global print target
125124
const printer = createTextWriter("\n");
126125

127-
const pollutor = getElements(webidl.interfaces, "interface").find(i => flavor === Flavor.Web ? !!i["primary-global"] : !!i.global);
126+
const pollutor = getElements(webidl.interfaces, "interface").find(i => flavor === Flavor.Window ? !!i["primary-global"] : !!i.global);
128127

129128
const allNonCallbackInterfaces = getElements(webidl.interfaces, "interface").concat(getElements(webidl.mixins, "mixin"));
130129
const allInterfaces = getElements(webidl.interfaces, "interface").concat(
@@ -182,7 +181,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
182181

183182
const iNameToConstParents = arrayToMap(allInterfaces, i => i.name, getParentsWithConstant);
184183

185-
return flavor === Flavor.ES6Iterators ? emitES6DomIterators() : emit();
184+
return iterator ? emitES6DomIterators() : emit();
186185

187186
function getTagNameToElementNameMap() {
188187
const htmlResult: Record<string, string> = {};
@@ -340,7 +339,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
340339
}
341340

342341
function convertDomTypeToTsTypeSimple(objDomType: string): string {
343-
if (objDomType === "sequence" && flavor === Flavor.ES6Iterators) {
342+
if (objDomType === "sequence" && iterator) {
344343
return "Iterable";
345344
}
346345
if (baseTypeConversionMap.has(objDomType)) {
@@ -830,7 +829,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
830829
printer.printLine("};");
831830
printer.printLine("");
832831

833-
if (flavor === Flavor.Web && i["legacy-window-alias"]) {
832+
if (flavor === Flavor.Window && i["legacy-window-alias"]) {
834833
for (const alias of i["legacy-window-alias"]!) {
835834
printer.printLine(`type ${alias} = ${i.name};`);
836835
printer.printLine(`declare var ${alias}: typeof ${i.name};`);
@@ -1174,7 +1173,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
11741173

11751174
emitCallBackFunctions();
11761175

1177-
if (flavor !== Flavor.Worker) {
1176+
if (flavor === Flavor.Window) {
11781177
emitHTMLElementTagNameMap();
11791178
emitHTMLElementDeprecatedTagNameMap();
11801179
emitSVGElementTagNameMap();
@@ -1345,7 +1344,12 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
13451344
function emitES6DomIterators() {
13461345
printer.reset();
13471346
printer.printLine("/////////////////////////////");
1348-
printer.printLine("/// DOM Iterable APIs");
1347+
if (flavor === Flavor.Worker) {
1348+
printer.printLine("/// Worker Iterable APIs");
1349+
}
1350+
else {
1351+
printer.printLine("/// DOM Iterable APIs");
1352+
}
13491353
printer.printLine("/////////////////////////////");
13501354

13511355
allInterfaces

src/index.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,22 @@ function mergeNamesakes(filtered: Browser.WebIdl) {
2525
}
2626
}
2727

28-
function emitDomWorker(webidl: Browser.WebIdl, tsWorkerOutput: string, forceKnownWorkerTypes: Set<string>) {
29-
const worker = getExposedTypes(webidl, "Worker", forceKnownWorkerTypes);
30-
mergeNamesakes(worker);
31-
const result = emitWebIdl(worker, Flavor.Worker);
32-
fs.writeFileSync(tsWorkerOutput, result);
33-
return;
28+
interface EmitOptions {
29+
flavor: Flavor;
30+
global: string;
31+
name: string;
32+
outputFolder: string;
3433
}
3534

36-
function emitDomWeb(webidl: Browser.WebIdl, tsWebOutput: string, forceKnownWindowTypes: Set<string>) {
37-
const browser = getExposedTypes(webidl, "Window", forceKnownWindowTypes);
38-
mergeNamesakes(browser);
39-
const result = emitWebIdl(browser, Flavor.Web);
40-
fs.writeFileSync(tsWebOutput, result);
41-
return;
42-
}
35+
function emitFlavor(webidl: Browser.WebIdl, forceKnownTypes: Set<string>, options: EmitOptions) {
36+
const exposed = getExposedTypes(webidl, options.global, forceKnownTypes);
37+
mergeNamesakes(exposed);
38+
39+
const result = emitWebIdl(exposed, options.flavor, false);
40+
fs.writeFileSync(`${options.outputFolder}/${options.name}.generated.d.ts`, result);
4341

44-
function emitES6DomIterators(webidl: Browser.WebIdl, tsWebIteratorsOutput: string) {
45-
fs.writeFileSync(tsWebIteratorsOutput, emitWebIdl(webidl, Flavor.ES6Iterators));
42+
const iterators = emitWebIdl(exposed, options.flavor, true);
43+
fs.writeFileSync(`${options.outputFolder}/${options.name}.iterable.generated.d.ts`, iterators);
4644
}
4745

4846
function emitDom() {
@@ -65,10 +63,6 @@ function emitDom() {
6563
fs.mkdirSync(outputFolder);
6664
}
6765

68-
const tsWebOutput = path.join(outputFolder, "dom.generated.d.ts");
69-
const tsWebIteratorsOutput = path.join(outputFolder, "dom.iterable.generated.d.ts");
70-
const tsWorkerOutput = path.join(outputFolder, "webworker.generated.d.ts");
71-
7266
const overriddenItems = require(path.join(inputFolder, "overridingTypes.json"));
7367
const addedItems = require(path.join(inputFolder, "addedTypes.json"));
7468
const comments = require(path.join(inputFolder, "comments.json"));
@@ -184,9 +178,8 @@ function emitDom() {
184178
}
185179
}
186180

187-
emitDomWeb(webidl, tsWebOutput, new Set(knownTypes.Window));
188-
emitDomWorker(webidl, tsWorkerOutput, new Set(knownTypes.Worker));
189-
emitES6DomIterators(webidl, tsWebIteratorsOutput);
181+
emitFlavor(webidl, new Set(knownTypes.Window), { name: "dom", flavor: Flavor.Window, global: "Window", outputFolder });
182+
emitFlavor(webidl, new Set(knownTypes.Worker), { name: "webworker", flavor: Flavor.Worker, global: "Worker", outputFolder });
190183

191184
function prune(obj: Browser.WebIdl, template: Partial<Browser.WebIdl>): Browser.WebIdl {
192185
return filterByNull(obj, template);

0 commit comments

Comments
 (0)