Skip to content

Commit 4d0dbad

Browse files
author
Orta
authored
Merge pull request #755 from RReverser/improve-wasm-imports-exports
Improve WebAssembly import/export types
2 parents d17c9e1 + 8b1139f commit 4d0dbad

File tree

7 files changed

+115
-12
lines changed

7 files changed

+115
-12
lines changed

baselines/dom.generated.d.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19043,7 +19043,7 @@ declare namespace WebAssembly {
1904319043
};
1904419044

1904519045
interface Instance {
19046-
readonly exports: any;
19046+
readonly exports: Exports;
1904719047
}
1904819048

1904919049
var Instance: {
@@ -19134,10 +19134,16 @@ declare namespace WebAssembly {
1913419134

1913519135
type ImportExportKind = "function" | "table" | "memory" | "global";
1913619136
type TableKind = "anyfunc";
19137+
type ValueType = "i32" | "i64" | "f32" | "f64";
19138+
type ExportValue = Function | Global | Memory | Table;
19139+
type Exports = Record<string, ExportValue>;
19140+
type ImportValue = ExportValue | number;
19141+
type ModuleImports = Record<string, ImportValue>;
19142+
type Imports = Record<string, ModuleImports>;
1913719143
function compile(bytes: BufferSource): Promise<Module>;
1913819144
function compileStreaming(source: Response | Promise<Response>): Promise<Module>;
19139-
function instantiate(bytes: BufferSource, importObject?: any): Promise<WebAssemblyInstantiatedSource>;
19140-
function instantiate(moduleObject: Module, importObject?: any): Promise<Instance>;
19145+
function instantiate(bytes: BufferSource, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
19146+
function instantiate(moduleObject: Module, importObject?: Imports): Promise<Instance>;
1914119147
function instantiateStreaming(source: Response | Promise<Response>, importObject?: any): Promise<WebAssemblyInstantiatedSource>;
1914219148
function validate(bytes: BufferSource): boolean;
1914319149
}
@@ -20097,7 +20103,6 @@ type TouchType = "direct" | "stylus";
2009720103
type Transport = "usb" | "nfc" | "ble";
2009820104
type UserVerificationRequirement = "required" | "preferred" | "discouraged";
2009920105
type VRDisplayEventReason = "mounted" | "navigation" | "requested" | "unmounted";
20100-
type ValueType = "i32" | "i64" | "f32" | "f64";
2010120106
type VideoFacingModeEnum = "user" | "environment" | "left" | "right";
2010220107
type VisibilityState = "hidden" | "visible" | "prerender";
2010320108
type WebGLPowerPreference = "default" | "low-power" | "high-performance";

baselines/webworker.generated.d.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5604,7 +5604,7 @@ declare namespace WebAssembly {
56045604
};
56055605

56065606
interface Instance {
5607-
readonly exports: any;
5607+
readonly exports: Exports;
56085608
}
56095609

56105610
var Instance: {
@@ -5679,10 +5679,16 @@ declare namespace WebAssembly {
56795679

56805680
type ImportExportKind = "function" | "table" | "memory" | "global";
56815681
type TableKind = "anyfunc";
5682+
type ValueType = "i32" | "i64" | "f32" | "f64";
5683+
type ExportValue = Function | Global | Memory | Table;
5684+
type Exports = Record<string, ExportValue>;
5685+
type ImportValue = ExportValue | number;
5686+
type ModuleImports = Record<string, ImportValue>;
5687+
type Imports = Record<string, ModuleImports>;
56825688
function compile(bytes: BufferSource): Promise<Module>;
56835689
function compileStreaming(source: Response | Promise<Response>): Promise<Module>;
5684-
function instantiate(bytes: BufferSource, importObject?: any): Promise<WebAssemblyInstantiatedSource>;
5685-
function instantiate(moduleObject: Module, importObject?: any): Promise<Instance>;
5690+
function instantiate(bytes: BufferSource, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
5691+
function instantiate(moduleObject: Module, importObject?: Imports): Promise<Instance>;
56865692
function instantiateStreaming(source: Response | Promise<Response>, importObject?: any): Promise<WebAssemblyInstantiatedSource>;
56875693
function validate(bytes: BufferSource): boolean;
56885694
}
@@ -5857,7 +5863,6 @@ type RequestRedirect = "follow" | "error" | "manual";
58575863
type ResponseType = "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect";
58585864
type ServiceWorkerState = "installing" | "installed" | "activating" | "activated" | "redundant";
58595865
type ServiceWorkerUpdateViaCache = "imports" | "all" | "none";
5860-
type ValueType = "i32" | "i64" | "f32" | "f64";
58615866
type VisibilityState = "hidden" | "visible" | "prerender";
58625867
type WebGLPowerPreference = "default" | "low-power" | "high-performance";
58635868
type WorkerType = "classic" | "module";

inputfiles/addedTypes.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,6 +2264,55 @@
22642264
{
22652265
"new-type": "WindowProxy",
22662266
"type": "Window"
2267+
},
2268+
{
2269+
"new-type": "ExportValue",
2270+
"legacy-namespace": "WebAssembly",
2271+
"override-type": "Function | Global | Memory | Table"
2272+
},
2273+
{
2274+
"new-type": "Exports",
2275+
"legacy-namespace": "WebAssembly",
2276+
"type": "record",
2277+
"subtype": [
2278+
{
2279+
"type": "DOMString"
2280+
},
2281+
{
2282+
"type": "ExportValue"
2283+
}
2284+
]
2285+
},
2286+
{
2287+
"new-type": "ImportValue",
2288+
"legacy-namespace": "WebAssembly",
2289+
"override-type": "ExportValue | number"
2290+
},
2291+
{
2292+
"new-type": "ModuleImports",
2293+
"legacy-namespace": "WebAssembly",
2294+
"type": "record",
2295+
"subtype": [
2296+
{
2297+
"type": "DOMString"
2298+
},
2299+
{
2300+
"type": "ImportValue"
2301+
}
2302+
]
2303+
},
2304+
{
2305+
"new-type": "Imports",
2306+
"legacy-namespace": "WebAssembly",
2307+
"type": "record",
2308+
"subtype": [
2309+
{
2310+
"type": "DOMString"
2311+
},
2312+
{
2313+
"type": "ModuleImports"
2314+
}
2315+
]
22672316
}
22682317
]
22692318
}

inputfiles/overridingTypes.json

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@
312312
},
313313
"TableKind": {
314314
"legacy-namespace": "WebAssembly"
315+
},
316+
"ValueType": {
317+
"legacy-namespace": "WebAssembly"
315318
}
316319
}
317320
},
@@ -2802,6 +2805,15 @@
28022805
}
28032806
}
28042807
}
2808+
},
2809+
"Instance": {
2810+
"properties": {
2811+
"property": {
2812+
"exports": {
2813+
"type": "Exports"
2814+
}
2815+
}
2816+
}
28052817
}
28062818
}
28072819
},
@@ -3190,5 +3202,25 @@
31903202
"new-type": "BodyInit"
31913203
}
31923204
]
3193-
}
3205+
},
3206+
"namespaces": [
3207+
{
3208+
"name": "WebAssembly",
3209+
"methods": {
3210+
"method": {
3211+
"instantiate": {
3212+
"override-signatures": [
3213+
"instantiate(bytes: BufferSource, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>",
3214+
"instantiate(moduleObject: Module, importObject?: Imports): Promise<Instance>"
3215+
],
3216+
"force-references": [
3217+
{
3218+
"type": "Imports"
3219+
}
3220+
]
3221+
}
3222+
}
3223+
}
3224+
}
3225+
]
31943226
}

src/emitter.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,8 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
10491049
namespace.nested.enums
10501050
.sort(compareName)
10511051
.forEach(emitEnum);
1052+
namespace.nested.typedefs
1053+
.forEach(emitTypeDef);
10521054
}
10531055

10541056
emitProperties("var ", EmitScope.InstanceOnly, namespace);
@@ -1097,7 +1099,9 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
10971099

10981100
function emitTypeDefs() {
10991101
if (webidl.typedefs) {
1100-
webidl.typedefs.typedef.forEach(emitTypeDef);
1102+
webidl.typedefs.typedef
1103+
.filter(i => !i["legacy-namespace"])
1104+
.forEach(emitTypeDef);
11011105
}
11021106
}
11031107

@@ -1272,7 +1276,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
12721276
mapToArray(i.methods ? i.methods.method : {})
12731277
.filter(m => m.signature && !m["override-signatures"])
12741278
.map(m => ({
1275-
...m,
1279+
...m,
12761280
signature: replaceTypedefsInSignatures(m.signature.filter(hasSequenceArgument))
12771281
}))
12781282
.filter(m => m.signature.length)

src/legacy-namespace.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export function collectLegacyNamespaceTypes(webidl: Browser.WebIdl): Browser.Int
2222
getNamespace(i["legacy-namespace"]).nested!.enums.push(i);
2323
}
2424
}
25+
for (const i of webidl.typedefs!.typedef) {
26+
if (i["legacy-namespace"]) {
27+
getNamespace(i["legacy-namespace"]).nested!.typedefs.push(i);
28+
}
29+
}
2530

2631
return mapToArray(namespaceMap);
2732

@@ -44,7 +49,8 @@ function addEmptyNested(namespace: Browser.Interface): Browser.Interface {
4449
nested: {
4550
interfaces: [],
4651
enums: [],
47-
dictionaries: []
52+
dictionaries: [],
53+
typedefs: [],
4854
}
4955
};
5056
}

src/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export interface Interface {
200200
interfaces: Interface[];
201201
enums: Enum[];
202202
dictionaries: Dictionary[];
203+
typedefs: TypeDef[];
203204
}
204205
}
205206

@@ -222,6 +223,7 @@ export interface Enum {
222223
export interface TypeDef extends Typed {
223224
"new-type": string;
224225
deprecated?: 1;
226+
"legacy-namespace"?: string;
225227
}
226228

227229
export interface Dictionary {

0 commit comments

Comments
 (0)