mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-11 12:30:18 +00:00
Merge pull request #15662 from winksaville/add-webassembly-js-api
Typescript declaration file for WebAssembly JS API
This commit is contained in:
Vendored
+107
@@ -0,0 +1,107 @@
|
||||
// Type definitions for WebAssembly v1 (MVP)
|
||||
// Project: https://github.com/winksaville/test-webassembly-js-ts
|
||||
// Definitions by: 01alchemist <https://twitter.com/01alchemist>, Wink Saville <wink@saville.com>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
/**
|
||||
* WebAssembly.Module
|
||||
*/
|
||||
class Module {
|
||||
constructor(bufferSource: ArrayBuffer | Uint8Array);
|
||||
static customSections(module: Module, sectionName: string): ArrayBuffer[];
|
||||
static exports(module: Module): Array<{
|
||||
name: string;
|
||||
kind: string;
|
||||
}>;
|
||||
static imports(module: Module): Array<{
|
||||
module: string;
|
||||
name: string;
|
||||
kind: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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: ArrayBuffer | Uint8Array): Promise<Module>;
|
||||
|
||||
interface ResultObject {
|
||||
module: Module;
|
||||
instance: Instance;
|
||||
}
|
||||
|
||||
function instantiate(bufferSource: ArrayBuffer | Uint8Array, importObject?: any): Promise<ResultObject>;
|
||||
function instantiate(module: Module, importObject?: any): Promise<Instance>;
|
||||
|
||||
function validate(bufferSource: ArrayBuffer | Uint8Array): boolean;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"webassembly-js-api-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "../tslint.json" }
|
||||
@@ -0,0 +1,75 @@
|
||||
function debug(s: string) {
|
||||
// debug(s)
|
||||
}
|
||||
|
||||
// Table
|
||||
let table = new WebAssembly.Table({element: "anyfunc", initial: 1, maximum: 10});
|
||||
debug(`table.length=${table.length}`);
|
||||
debug(`table.get(0)=${table.get(0)}`);
|
||||
table.grow(1);
|
||||
|
||||
// Memory
|
||||
let memory = new WebAssembly.Memory({initial: 2, maximum: 8});
|
||||
debug(`memory.grow(6)=${memory.grow(6)}`);
|
||||
let u8 = new Uint8Array(memory.buffer);
|
||||
u8[0] = 1;
|
||||
u8[1] = 2;
|
||||
debug(`u8[0]=${u8[0]}`);
|
||||
debug(`u8[1]=${u8[1]}`);
|
||||
|
||||
let wasmDataU8 = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
|
||||
debug(`wasmDataU8.length=${wasmDataU8.length}`);
|
||||
debug(`wasmDataU8[0]=${wasmDataU8[0].toString(16)}`);
|
||||
debug(`wasmDataU8[1]=${wasmDataU8[1].toString(16)}`);
|
||||
debug(`wasmDataU8[2]=${wasmDataU8[2].toString(16)}`);
|
||||
debug(`wasmDataU8[3]=${wasmDataU8[3].toString(16)}`);
|
||||
debug(`wasmDataU8[4]=${wasmDataU8[4].toString(16)}`);
|
||||
debug(`wasmDataU8[5]=${wasmDataU8[5].toString(16)}`);
|
||||
debug(`wasmDataU8[6]=${wasmDataU8[6].toString(16)}`);
|
||||
debug(`wasmDataU8[7]=${wasmDataU8[7].toString(16)}`);
|
||||
|
||||
// Validate
|
||||
let valid = WebAssembly.validate(wasmDataU8);
|
||||
debug("wasmDataU8 is " + (valid ? "" : "not ") + "a valid wasm wasmModule");
|
||||
|
||||
// Module
|
||||
let wasmModule = new WebAssembly.Module(wasmDataU8);
|
||||
debug(`wasmModule=${wasmModule}`);
|
||||
|
||||
// CustomSections
|
||||
let nameSections = WebAssembly.Module.customSections(wasmModule, "name");
|
||||
debug(`Module contains ${nameSections.length} name sections`);
|
||||
if (nameSections.length !== 0) {
|
||||
debug("Module contains a name section");
|
||||
debug(nameSections[0].toString());
|
||||
}
|
||||
|
||||
// Instance
|
||||
let instance = new WebAssembly.Instance(wasmModule);
|
||||
debug(`instance=${instance}`);
|
||||
debug(`instance.exports=${instance.exports}`);
|
||||
|
||||
// Compile Asynchronously
|
||||
debug("instantiateAsync compile:");
|
||||
WebAssembly.compile(wasmDataU8).then((mod: WebAssembly.Module) => {
|
||||
debug(`instantiateAsync compiled: ${JSON.stringify(mod)}`);
|
||||
});
|
||||
|
||||
// Instantiate
|
||||
// Primary overload — taking wasm binary code
|
||||
WebAssembly.instantiate(wasmDataU8).then((result: WebAssembly.ResultObject) => {
|
||||
debug(`Primary overload mod=${result.module}`);
|
||||
debug(`Primary overload inst=${result.instance}`);
|
||||
debug(`Primary exec instance.exports.addTwo1(-1, 1)=${result.instance.exports.addTwo1(-1, 1)}`);
|
||||
});
|
||||
|
||||
// Instantiate
|
||||
// Secondary overload — taking a Module object
|
||||
WebAssembly.instantiate(wasmModule).then((instance: WebAssembly.Instance) => {
|
||||
debug(`Secondary overload instance=${instance}`);
|
||||
debug(`Secondary exec instance.exports.addTwo1(0, -1)=${instance.exports.addTwo1(0, -1)}`);
|
||||
});
|
||||
|
||||
// Validate
|
||||
WebAssembly.validate(new ArrayBuffer(8));
|
||||
WebAssembly.validate(wasmDataU8);
|
||||
Reference in New Issue
Block a user