From b0333516fd75a0a18ec6102708c6593dbee8b39a Mon Sep 17 00:00:00 2001 From: Nidin Vinayakan Date: Fri, 31 Mar 2017 17:24:28 +0200 Subject: [PATCH] WebAssembly declaration file for TypeScript and a dummy test. --- README.md | 2 +- test/tsconfig.json | 7 +++ test/wasm.js | 55 +++++++++++++++++++++++ test/wasm.ts | 64 +++++++++++++++++++++++++++ webassembly.d.ts | 108 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 235 insertions(+), 1 deletion(-) create mode 100644 test/tsconfig.json create mode 100644 test/wasm.js create mode 100644 test/wasm.ts create mode 100644 webassembly.d.ts diff --git a/README.md b/README.md index 739add46..ee3b7afe 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# webassembly-types +# WebAssembly Types Typescript declaration for WebAssembly JS API diff --git a/test/tsconfig.json b/test/tsconfig.json new file mode 100644 index 00000000..54061752 --- /dev/null +++ b/test/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "commonjs" + }, + "files": ["wasm"] +} \ No newline at end of file diff --git a/test/wasm.js b/test/wasm.js new file mode 100644 index 00000000..1868b3bf --- /dev/null +++ b/test/wasm.js @@ -0,0 +1,55 @@ +/** + * Created by Nidin Vinayakan on 31/03/17. + */ +/// +let source = new Uint8Array(1); +//Table +let table = new WebAssembly.Table({ element: "anyfunc", initial: 1, maximum: 10 }); +console.log(table.length); +table.get(0)(); +table.set(0, function () { +}); +table.grow(1); +//Memory +let memory = new WebAssembly.Memory({ initial: 2, maximum: 256 }); +console.log(memory.grow(512)); +let u8 = new Uint8Array(memory.buffer); +console.log(u8); +//Module +let module = new WebAssembly.Module(source); +//customSections +let nameSections = WebAssembly.Module.customSections(module, "name"); +if (nameSections.length != 0) { + console.log("Module contains a name section"); + console.log(nameSections[0]); +} +//exports +console.log(WebAssembly.Module.exports(module).length); +console.log(WebAssembly.Module.exports(module)[0].name); +console.log(WebAssembly.Module.exports(module)[0].kind); +//imports +console.log(WebAssembly.Module.imports(module).length); +console.log(WebAssembly.Module.imports(module)[0].module); +console.log(WebAssembly.Module.imports(module)[0].name); +console.log(WebAssembly.Module.imports(module)[0].kind); +//Instance +let instance = new WebAssembly.Instance(module); +console.log(instance.exports.exported_func()); +let bytes = new ArrayBuffer(1); //dummy bytes +//validate +let valid = WebAssembly.validate(bytes); +console.log("The given bytes are " + (valid ? "" : "not ") + "a valid wasm module"); +//compile +WebAssembly.compile(bytes).then((module) => { + console.log(module); +}); +//instantiate +//Primary overload — taking wasm binary code +WebAssembly.instantiate(bytes).then((result) => { + console.log(result.module); + console.log(result.instance); +}); +//Secondary overload — taking a module object instance +WebAssembly.instantiate(module).then((instance) => { + console.log(instance); +}); diff --git a/test/wasm.ts b/test/wasm.ts new file mode 100644 index 00000000..9b956fc6 --- /dev/null +++ b/test/wasm.ts @@ -0,0 +1,64 @@ +/** + * Created by Nidin Vinayakan on 31/03/17. + */ +/// + +let source: Uint8Array = new Uint8Array(1); + +//Table +let table = new WebAssembly.Table({element: "anyfunc", initial: 1, maximum: 10}); +console.log(table.length); +table.get(0)(); +table.set(0, function () { +}); +table.grow(1); + +//Memory +let memory = new WebAssembly.Memory({initial: 2, maximum: 256}); +console.log(memory.grow(512)); +let u8 = new Uint8Array(memory.buffer); +console.log(u8); + +//Module +let module = new WebAssembly.Module(source); +//customSections +let nameSections = WebAssembly.Module.customSections(module, "name"); +if (nameSections.length != 0) { + console.log("Module contains a name section"); + console.log(nameSections[0]); +} +//exports +console.log(WebAssembly.Module.exports(module).length); +console.log(WebAssembly.Module.exports(module)[0].name); +console.log(WebAssembly.Module.exports(module)[0].kind); +//imports +console.log(WebAssembly.Module.imports(module).length); +console.log(WebAssembly.Module.imports(module)[0].module); +console.log(WebAssembly.Module.imports(module)[0].name); +console.log(WebAssembly.Module.imports(module)[0].kind); + +//Instance +let instance = new WebAssembly.Instance(module); +console.log(instance.exports.exported_func()); + + +let bytes = new ArrayBuffer(1);//dummy bytes +//validate +let valid = WebAssembly.validate(bytes); +console.log("The given bytes are " + (valid ? "" : "not ") + "a valid wasm module"); + +//compile +WebAssembly.compile(bytes).then((module: WebAssembly.Module) => { + console.log(module); +}); + +//instantiate +//Primary overload — taking wasm binary code +WebAssembly.instantiate(bytes).then((result: WebAssembly.ResultObject) => { + console.log(result.module); + console.log(result.instance); +}); +//Secondary overload — taking a module object instance +WebAssembly.instantiate(module).then((instance: WebAssembly.Instance) => { + console.log(instance); +}); diff --git a/webassembly.d.ts b/webassembly.d.ts new file mode 100644 index 00000000..0fbf2e87 --- /dev/null +++ b/webassembly.d.ts @@ -0,0 +1,108 @@ +interface NativeError { + +} + +/** + * WebAssembly v1 (MVP) declaration file for TypeScript + * Author : 01alchemist (https://twitter.com/01alchemist) + */ +declare namespace WebAssembly { + + /** + * WebAssembly.Module + **/ + interface Module { + readonly [Symbol.toStringTag]: "Module"; + } + + interface ModuleConstructor { + readonly prototype: Module; + new(bufferSource: ArrayBuffer | Uint8Array): Module; + customSections(module: Module, sectionName: string): ArrayBuffer[]; + exports(module: Module): { + name: string; + kind: string; + }[]; + imports(module: Module): { + module: string; + name: string; + kind: string; + }[]; + } + const Module: ModuleConstructor; + + /** + * WebAssembly.Instance + **/ + interface Instance { + readonly exports: any; + readonly [Symbol.toStringTag]: "Instance"; + } + + interface InstanceConstructor { + readonly prototype: Instance; + new(module: Module, importObject?: any): Instance; + } + const Instance: InstanceConstructor; + + /** + * WebAssembly.Memory + * Note: A WebAssembly page has a constant size of 65,536 bytes, i.e., 64KiB. + **/ + interface MemoryDescriptor { + initial: number; + maximum?: number; + } + + interface Memory { + readonly buffer: ArrayBuffer; + grow(numPages: number): number; + readonly [Symbol.toStringTag]: "Memory"; + } + + interface MemoryConstructor { + readonly prototype: Memory; + new (memoryDescriptor: MemoryDescriptor): Memory; + } + const Memory: MemoryConstructor; + + /** + * WebAssembly.Table + **/ + interface TableDescriptor { + element: "anyfunc", + initial: number; + maximum?: number; + } + + interface Table { + readonly length: number; + get(index: number): Function; + grow(numElements: number): number; + set(index: number, value: Function): void; + readonly [Symbol.toStringTag]: "Table"; + } + + interface TableConstructor { + readonly prototype: Table; + new (tableDescriptor: TableDescriptor): Table; + } + const Table: TableConstructor; + + + const CompileError: NativeError; + const LinkError: NativeError; + const RuntimeError: NativeError; + + function compile(bufferSource: ArrayBuffer | Uint8Array): Promise; + + interface ResultObject { + module:Module; + instance:Instance; + } + + function instantiate(bufferSource: ArrayBuffer | Uint8Array, importObject?: any): Promise; + function instantiate(module: Module, importObject?: any): Promise; + + function validate(bufferSource: ArrayBuffer | Uint8Array):boolean; +} \ No newline at end of file -- 2.30.2