Skip to content

Commit

Permalink
feat: Emit WebAssembly types separately from DOM types
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Sep 27, 2021
1 parent c7f8137 commit 071bb2c
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
132 changes: 132 additions & 0 deletions baselines/webassembly.generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/////////////////////////////
/// WebAssembly APIs
/////////////////////////////

declare namespace WebAssembly {
interface CompileError extends Error {
}

var CompileError: {
prototype: CompileError;
new(message?: string): CompileError;
(message?: string): CompileError;
};

interface Global {
value: any;
valueOf(): any;
}

var Global: {
prototype: Global;
new(descriptor: GlobalDescriptor, v?: any): Global;
};

interface Instance {
readonly exports: Exports;
}

var Instance: {
prototype: Instance;
new(module: Module, importObject?: Imports): Instance;
};

interface LinkError extends Error {
}

var LinkError: {
prototype: LinkError;
new(message?: string): LinkError;
(message?: string): LinkError;
};

interface Memory {
readonly buffer: ArrayBuffer;
grow(delta: number): number;
}

var Memory: {
prototype: Memory;
new(descriptor: MemoryDescriptor): Memory;
};

interface Module {
}

var Module: {
prototype: Module;
new(bytes: BufferSource): Module;
customSections(moduleObject: Module, sectionName: string): ArrayBuffer[];
exports(moduleObject: Module): ModuleExportDescriptor[];
imports(moduleObject: Module): ModuleImportDescriptor[];
};

interface RuntimeError extends Error {
}

var RuntimeError: {
prototype: RuntimeError;
new(message?: string): RuntimeError;
(message?: string): RuntimeError;
};

interface Table {
readonly length: number;
get(index: number): any;
grow(delta: number, value?: any): number;
set(index: number, value?: any): void;
}

var Table: {
prototype: Table;
new(descriptor: TableDescriptor, value?: any): Table;
};

interface GlobalDescriptor {
mutable?: boolean;
value: ValueType;
}

interface MemoryDescriptor {
initial: number;
maximum?: number;
shared?: boolean;
}

interface ModuleExportDescriptor {
kind: ImportExportKind;
name: string;
}

interface ModuleImportDescriptor {
kind: ImportExportKind;
module: string;
name: string;
}

interface TableDescriptor {
element: TableKind;
initial: number;
maximum?: number;
}

interface WebAssemblyInstantiatedSource {
instance: Instance;
module: Module;
}

type ImportExportKind = "function" | "global" | "memory" | "table";
type TableKind = "anyfunc" | "externref";
type ValueType = "anyfunc" | "externref" | "f32" | "f64" | "i32" | "i64";
type ExportValue = Function | Global | Memory | Table;
type Exports = Record<string, ExportValue>;
type ImportValue = ExportValue | number;
type Imports = Record<string, ModuleImports>;
type ModuleImports = Record<string, ImportValue>;
function compile(bytes: BufferSource): Promise<Module>;
function instantiate(bytes: BufferSource, importObject?: Imports): Promise<WebAssemblyInstantiatedSource>;
function instantiate(moduleObject: Module, importObject?: Imports): Promise<Instance>;
function validate(bytes: BufferSource): boolean;
}

type BufferSource = ArrayBufferView | ArrayBuffer;
2 changes: 2 additions & 0 deletions inputfiles/addedTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@
"EventListener": {
"name": "EventListener",
"noInterfaceObject": true,
"exposed": "Window Worker Worklet",
"methods": {
"method": {
// This is a hack to add a call signature, but I think it's reasonable
Expand All @@ -612,6 +613,7 @@
"EventListenerObject": {
"name": "EventListenerObject",
"noInterfaceObject": true,
"exposed": "Window Worker Worklet",
"methods": {
"method": {
"handleEvent": {
Expand Down
2 changes: 2 additions & 0 deletions inputfiles/knownTypes.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"WebAssembly": [
],
"Window": [
"AesCbcParams",
"AesCtrParams",
Expand Down
21 changes: 21 additions & 0 deletions inputfiles/overridingTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2698,6 +2698,7 @@
}
},
"Instance": {
"overrideExposed": "Window Worker Worklet WebAssembly",
"constructor": {
"signature": {
"0": {
Expand Down Expand Up @@ -2802,6 +2803,18 @@
}
}
},
"Global": {
"overrideExposed": "Window Worker Worklet WebAssembly"
},
"Module": {
"overrideExposed": "Window Worker Worklet WebAssembly"
},
"Memory": {
"overrideExposed": "Window Worker Worklet WebAssembly"
},
"Table": {
"overrideExposed": "Window Worker Worklet WebAssembly"
},

// https://github.com/w3c/media-source/pull/282, but not supported anywhere yet
"MediaSource": {
Expand Down Expand Up @@ -3132,9 +3145,17 @@
},
{
"name": "WebAssembly",
"exposed": "Window Worker Worklet WebAssembly",
"methods": {
"method": {
"validate": {
"exposed": "Window Worker Worklet WebAssembly"
},
"compile": {
"exposed": "Window Worker Worklet WebAssembly"
},
"instantiate": {
"exposed": "Window Worker Worklet WebAssembly",
"signature": {
"0": {
"param": [
Expand Down
12 changes: 12 additions & 0 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface EmitOptions {
global: string[];
name: string;
outputFolder: URL;
emitIterators?: boolean;
}

async function emitFlavor(
Expand All @@ -48,6 +49,10 @@ async function emitFlavor(
result
);

if (options.emitIterators === false) {
return;
}

const iterators = emitWebIdl(exposed, options.global[0], true);
await fs.writeFile(
new URL(`${options.name}.iterable.generated.d.ts`, options.outputFolder),
Expand Down Expand Up @@ -270,6 +275,13 @@ async function emitDom() {
global: ["AudioWorklet", "Worklet"],
outputFolder,
});
emitFlavor(webidl, new Set(knownTypes.WebAssembly), {
name: "webassembly",
global: ["WebAssembly"],
outputFolder,
// WebAssembly has no iterable interfaces.
emitIterators: false,
});

function prune(
obj: Browser.WebIdl,
Expand Down

0 comments on commit 071bb2c

Please sign in to comment.