Skip to content

Commit 071bb2c

Browse files
committed
feat: Emit WebAssembly types separately from DOM types
1 parent c7f8137 commit 071bb2c

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

baselines/webassembly.generated.d.ts

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/////////////////////////////
2+
/// WebAssembly APIs
3+
/////////////////////////////
4+
5+
declare namespace WebAssembly {
6+
interface CompileError extends Error {
7+
}
8+
9+
var CompileError: {
10+
prototype: CompileError;
11+
new(message?: string): CompileError;
12+
(message?: string): CompileError;
13+
};
14+
15+
interface Global {
16+
value: any;
17+
valueOf(): any;
18+
}
19+
20+
var Global: {
21+
prototype: Global;
22+
new(descriptor: GlobalDescriptor, v?: any): Global;
23+
};
24+
25+
interface Instance {
26+
readonly exports: Exports;
27+
}
28+
29+
var Instance: {
30+
prototype: Instance;
31+
new(module: Module, importObject?: Imports): Instance;
32+
};
33+
34+
interface LinkError extends Error {
35+
}
36+
37+
var LinkError: {
38+
prototype: LinkError;
39+
new(message?: string): LinkError;
40+
(message?: string): LinkError;
41+
};
42+
43+
interface Memory {
44+
readonly buffer: ArrayBuffer;
45+
grow(delta: number): number;
46+
}
47+
48+
var Memory: {
49+
prototype: Memory;
50+
new(descriptor: MemoryDescriptor): Memory;
51+
};
52+
53+
interface Module {
54+
}
55+
56+
var Module: {
57+
prototype: Module;
58+
new(bytes: BufferSource): Module;
59+
customSections(moduleObject: Module, sectionName: string): ArrayBuffer[];
60+
exports(moduleObject: Module): ModuleExportDescriptor[];
61+
imports(moduleObject: Module): ModuleImportDescriptor[];
62+
};
63+
64+
interface RuntimeError extends Error {
65+
}
66+
67+
var RuntimeError: {
68+
prototype: RuntimeError;
69+
new(message?: string): RuntimeError;
70+
(message?: string): RuntimeError;
71+
};
72+
73+
interface Table {
74+
readonly length: number;
75+
get(index: number): any;
76+
grow(delta: number, value?: any): number;
77+
set(index: number, value?: any): void;
78+
}
79+
80+
var Table: {
81+
prototype: Table;
82+
new(descriptor: TableDescriptor, value?: any): Table;
83+
};
84+
85+
interface GlobalDescriptor {
86+
mutable?: boolean;
87+
value: ValueType;
88+
}
89+
90+
interface MemoryDescriptor {
91+
initial: number;
92+
maximum?: number;
93+
shared?: boolean;
94+
}
95+
96+
interface ModuleExportDescriptor {
97+
kind: ImportExportKind;
98+
name: string;
99+
}
100+
101+
interface ModuleImportDescriptor {
102+
kind: ImportExportKind;
103+
module: string;
104+
name: string;
105+
}
106+
107+
interface TableDescriptor {
108+
element: TableKind;
109+
initial: number;
110+
maximum?: number;
111+
}
112+
113+
interface WebAssemblyInstantiatedSource {
114+
instance: Instance;
115+
module: Module;
116+
}
117+
118+
type ImportExportKind = "function" | "global" | "memory" | "table";
119+
type TableKind = "anyfunc" | "externref";
120+
type ValueType = "anyfunc" | "externref" | "f32" | "f64" | "i32" | "i64";
121+
type ExportValue = Function | Global | Memory | Table;
122+
type Exports = Record<string, ExportValue>;
123+
type ImportValue = ExportValue | number;
124+
type Imports = Record<string, ModuleImports>;
125+
type ModuleImports = Record<string, ImportValue>;
126+
function compile(bytes: BufferSource): Promise<Module>;
127+
function instantiate(bytes: BufferSource, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
128+
function instantiate(moduleObject: Module, importObject?: Imports): Promise<Instance>;
129+
function validate(bytes: BufferSource): boolean;
130+
}
131+
132+
type BufferSource = ArrayBufferView | ArrayBuffer;

inputfiles/addedTypes.jsonc

+2
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@
596596
"EventListener": {
597597
"name": "EventListener",
598598
"noInterfaceObject": true,
599+
"exposed": "Window Worker Worklet",
599600
"methods": {
600601
"method": {
601602
// This is a hack to add a call signature, but I think it's reasonable
@@ -612,6 +613,7 @@
612613
"EventListenerObject": {
613614
"name": "EventListenerObject",
614615
"noInterfaceObject": true,
616+
"exposed": "Window Worker Worklet",
615617
"methods": {
616618
"method": {
617619
"handleEvent": {

inputfiles/knownTypes.json

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
"WebAssembly": [
3+
],
24
"Window": [
35
"AesCbcParams",
46
"AesCtrParams",

inputfiles/overridingTypes.jsonc

+21
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,7 @@
26982698
}
26992699
},
27002700
"Instance": {
2701+
"overrideExposed": "Window Worker Worklet WebAssembly",
27012702
"constructor": {
27022703
"signature": {
27032704
"0": {
@@ -2802,6 +2803,18 @@
28022803
}
28032804
}
28042805
},
2806+
"Global": {
2807+
"overrideExposed": "Window Worker Worklet WebAssembly"
2808+
},
2809+
"Module": {
2810+
"overrideExposed": "Window Worker Worklet WebAssembly"
2811+
},
2812+
"Memory": {
2813+
"overrideExposed": "Window Worker Worklet WebAssembly"
2814+
},
2815+
"Table": {
2816+
"overrideExposed": "Window Worker Worklet WebAssembly"
2817+
},
28052818

28062819
// https://github.com/w3c/media-source/pull/282, but not supported anywhere yet
28072820
"MediaSource": {
@@ -3132,9 +3145,17 @@
31323145
},
31333146
{
31343147
"name": "WebAssembly",
3148+
"exposed": "Window Worker Worklet WebAssembly",
31353149
"methods": {
31363150
"method": {
3151+
"validate": {
3152+
"exposed": "Window Worker Worklet WebAssembly"
3153+
},
3154+
"compile": {
3155+
"exposed": "Window Worker Worklet WebAssembly"
3156+
},
31373157
"instantiate": {
3158+
"exposed": "Window Worker Worklet WebAssembly",
31383159
"signature": {
31393160
"0": {
31403161
"param": [

src/build.ts

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ interface EmitOptions {
3232
global: string[];
3333
name: string;
3434
outputFolder: URL;
35+
emitIterators?: boolean;
3536
}
3637

3738
async function emitFlavor(
@@ -48,6 +49,10 @@ async function emitFlavor(
4849
result
4950
);
5051

52+
if (options.emitIterators === false) {
53+
return;
54+
}
55+
5156
const iterators = emitWebIdl(exposed, options.global[0], true);
5257
await fs.writeFile(
5358
new URL(`${options.name}.iterable.generated.d.ts`, options.outputFolder),
@@ -270,6 +275,13 @@ async function emitDom() {
270275
global: ["AudioWorklet", "Worklet"],
271276
outputFolder,
272277
});
278+
emitFlavor(webidl, new Set(knownTypes.WebAssembly), {
279+
name: "webassembly",
280+
global: ["WebAssembly"],
281+
outputFolder,
282+
// WebAssembly has no iterable interfaces.
283+
emitIterators: false,
284+
});
273285

274286
function prune(
275287
obj: Browser.WebIdl,

0 commit comments

Comments
 (0)