Skip to content

Commit 5535d0a

Browse files
Bashamegasaschanaz
andauthored
Support typedef in KDL (#2340)
Co-authored-by: saschanaz <saschanaz@users.noreply.github.com>
1 parent a7e5b21 commit 5535d0a

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

inputfiles/addedTypes.jsonc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,6 @@
434434
"additionalTypes": ["Headers"],
435435
"name": "HeadersInit"
436436
},
437-
{
438-
"overrideType": "number | string | Date | BufferSource | IDBValidKey[]",
439-
"name": "IDBValidKey"
440-
},
441437
{
442438
"type": {
443439
"0": {
@@ -477,11 +473,6 @@
477473
}
478474
]
479475
},
480-
{
481-
"name": "ImportValue",
482-
"legacyNamespace": "WebAssembly",
483-
"overrideType": "ExportValue | number"
484-
},
485476
{
486477
"name": "ModuleImports",
487478
"legacyNamespace": "WebAssembly",

inputfiles/patches/indexeddb.kdl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ interface IDBRequest {
77
}
88

99
interface IDBOpenDBRequest extends=IDBRequest<IDBDatabase>
10+
11+
typedef IDBValidKey {
12+
type long
13+
type DOMString
14+
type Date
15+
type BufferSource
16+
type sequence {
17+
type IDBValidKey
18+
}
19+
}

inputfiles/patches/webassembly.kdl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ dictionary MemoryDescriptor {
1616
member shared type=boolean
1717
}
1818

19+
typedef ImportValue legacyNamespace=WebAssembly {
20+
type ExportValue
21+
type long
22+
}
23+
1924
removals {
2025
// Overridden with `keyof ValueTypeMap`
2126
enum ValueType

src/build/patches.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
Dictionary,
1717
Member,
1818
Signature,
19+
TypeDef,
1920
} from "./types.js";
2021
import { readdir, readFile } from "fs/promises";
2122
import { merge } from "./helpers.js";
@@ -120,6 +121,7 @@ function convertKDLNodes(nodes: Node[]): DeepPartial<WebIdl> {
120121
const mixin: Record<string, DeepPartial<Interface>> = {};
121122
const interfaces: Record<string, DeepPartial<Interface>> = {};
122123
const dictionary: Record<string, DeepPartial<Dictionary>> = {};
124+
const typedefs: DeepPartial<TypeDef>[] = [];
123125

124126
for (const node of nodes) {
125127
// Note: no "removals" handling here; caller is responsible for splitting
@@ -143,6 +145,9 @@ function convertKDLNodes(nodes: Node[]): DeepPartial<WebIdl> {
143145
case "dictionary":
144146
dictionary[name] = merge(dictionary[name], handleDictionary(node));
145147
break;
148+
case "typedef":
149+
typedefs.push(handleTypedef(node));
150+
break;
146151
default:
147152
throw new Error(`Unknown node name: ${node.name}`);
148153
}
@@ -155,6 +160,7 @@ function convertKDLNodes(nodes: Node[]): DeepPartial<WebIdl> {
155160
interface: interfaces,
156161
}),
157162
...optionalNestedMember("dictionaries", dictionary, { dictionary }),
163+
...optionalNestedMember("typedefs", typedefs, { typedef: typedefs }),
158164
};
159165
}
160166

@@ -432,6 +438,23 @@ function handleMember(c: Node): DeepPartial<Member> {
432438
};
433439
}
434440

441+
/**
442+
* Handles typedef nodes
443+
* @param node The typedef node to handle.
444+
*/
445+
function handleTypedef(node: Node): DeepPartial<TypeDef> {
446+
const typeNodes = node.children.filter((c) => c.name === "type");
447+
return {
448+
name: string(node.values[0]),
449+
...handleTyped(typeNodes),
450+
...optionalMember(
451+
"legacyNamespace",
452+
"string",
453+
node.properties?.legacyNamespace,
454+
),
455+
};
456+
}
457+
435458
/**
436459
* Collect all file URLs in a directory.
437460
*/

0 commit comments

Comments
 (0)