Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types for WebAssembly #1675

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ ts_sources = [
"js/url.ts",
"js/url_search_params.ts",
"js/util.ts",
"js/wasm.d.ts",
"js/workers.ts",
"js/write_file.ts",
"js/performance.ts",
Expand Down
7 changes: 6 additions & 1 deletion js/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
// is evaluated in. We use this file to automatically build the runtime type
// library.

// TODO This doesn't work.
/// <reference path="./wasm.d.ts" />

// Modules which will make up part of the global public API surface should be
// imported as namespaces, so when the runtime tpye library is generated they
// imported as namespaces, so when the runtime type library is generated they
// can be expressed as a namespace in the type library.
import * as blob from "./blob";
import * as consoleTypes from "./console";
Expand Down Expand Up @@ -97,3 +100,5 @@ export type TextDecoder = textEncoding.TextDecoder;
window.performance = new performanceUtil.Performance();

window.workerMain = workers.workerMain;

//window.WebAssembly = _WebAssembly;
125 changes: 125 additions & 0 deletions js/wasm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// MIT License
// Copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/webassembly-js-api/index.d.ts
// Type definitions for WebAssembly v1 (MVP)
// Project: https://github.com/winksaville/test-webassembly-js-ts
// Definitions by: 01alchemist <https://twitter.com/01alchemist>
// Wink Saville <[email protected]>
// Periklis Tsirakidis <https://github.com/periklis>
// Sergey Rubanov <https://github.com/chicoxyzzy>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2

/**
* The WebAssembly namespace, see [WebAssembly](https://github.com/webassembly)
* and [WebAssembly JS API](http://webassembly.org/getting-started/js-api/)
* for more information.
*/
declare namespace WebAssembly {
type Imports = Array<{
name: string;
kind: string;
}>;

type Exports = Array<{
module: string;
name: string;
kind: string;
}>;

type BufferSource = ArrayBufferView | ArrayBuffer;

/**
* WebAssembly.Module
*/
class Module {
constructor(bufferSource: BufferSource);
static customSections(module: Module, sectionName: string): ArrayBuffer[];
static exports(module: Module): Imports;
static imports(module: Module): Exports;
}

/**
* WebAssembly.Instance
*/
class Instance {
readonly exports: any;
constructor(module: Module, importObject?: any);
}

/**
* WebAssembly.Memory
* Note: A WebAssembly page has a constant size of 65,536 bytes, i.e., 64KiB.
*/
interface MemoryDescriptor {
initial: number;
maximum?: number;
}

class Memory {
readonly buffer: ArrayBuffer;
constructor(memoryDescriptor: MemoryDescriptor);
grow(numPages: number): number;
}

/**
* WebAssembly.Table
*/
interface TableDescriptor {
element: "anyfunc";
initial: number;
maximum?: number;
}

class Table {
readonly length: number;
constructor(tableDescriptor: TableDescriptor);
get(index: number): (args: any[]) => any;
grow(numElements: number): number;
set(index: number, value: (args: any[]) => any): void;
}

/**
* Errors
*/
class CompileError extends Error {
readonly fileName: string;
readonly lineNumber: string;
readonly columnNumber: string;
constructor(message?: string, fileName?: string, lineNumber?: number);
toString(): string;
}

class LinkError extends Error {
readonly fileName: string;
readonly lineNumber: string;
readonly columnNumber: string;
constructor(message?: string, fileName?: string, lineNumber?: number);
toString(): string;
}

class RuntimeError extends Error {
readonly fileName: string;
readonly lineNumber: string;
readonly columnNumber: string;
constructor(message?: string, fileName?: string, lineNumber?: number);
toString(): string;
}

function compile(bufferSource: BufferSource): Promise<Module>;

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

function instantiate(
bufferSource: BufferSource,
importObject?: object
): Promise<ResultObject>;
function instantiate(
module: Module,
importObject?: object
): Promise<Instance>;

function validate(bufferSource: BufferSource): boolean;
}
15 changes: 15 additions & 0 deletions tests/wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// prettier-ignore
const wasmCode = new Uint8Array([
0, 97, 115, 109, 1, 0, 0, 0, 1, 133, 128, 128, 128, 0, 1, 96, 0, 1, 127,
3, 130, 128, 128, 128, 0, 1, 0, 4, 132, 128, 128, 128, 0, 1, 112, 0, 0,
5, 131, 128, 128, 128, 0, 1, 0, 1, 6, 129, 128, 128, 128, 0, 0, 7, 145,
128, 128, 128, 0, 2, 6, 109, 101, 109, 111, 114, 121, 2, 0, 4, 109, 97,
105, 110, 0, 0, 10, 138, 128, 128, 128, 0, 1, 132, 128, 128, 128, 0, 0,
65, 42, 11
]);

const wasmModule = new WebAssembly.Module(wasmCode);

const wasmInstance = new WebAssembly.Instance(wasmModule, {});

console.log(wasmInstance.exports.main().toString());
1 change: 1 addition & 0 deletions tests/wasm.js.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
2 changes: 2 additions & 0 deletions tests/wasm.js.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
args: tests/wasm.js
output: tests/wasm.js.out
15 changes: 15 additions & 0 deletions tests/wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// prettier-ignore
const wasmCode = new Uint8Array([
0, 97, 115, 109, 1, 0, 0, 0, 1, 133, 128, 128, 128, 0, 1, 96, 0, 1, 127,
3, 130, 128, 128, 128, 0, 1, 0, 4, 132, 128, 128, 128, 0, 1, 112, 0, 0,
5, 131, 128, 128, 128, 0, 1, 0, 1, 6, 129, 128, 128, 128, 0, 0, 7, 145,
128, 128, 128, 0, 2, 6, 109, 101, 109, 111, 114, 121, 2, 0, 4, 109, 97,
105, 110, 0, 0, 10, 138, 128, 128, 128, 0, 1, 132, 128, 128, 128, 0, 0,
65, 42, 11
]);

const wasmModule = new WebAssembly.Module(wasmCode);

const wasmInstance = new WebAssembly.Instance(wasmModule, {});

console.log(wasmInstance.exports.main().toString());
1 change: 1 addition & 0 deletions tests/wasm.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
2 changes: 2 additions & 0 deletions tests/wasm.ts.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
args: tests/wasm.ts
output: tests/wasm.ts.out