-# webassembly-types
+# WebAssembly Types
Typescript declaration for WebAssembly JS API
--- /dev/null
+{
+ "compilerOptions": {
+ "target": "es6",
+ "module": "commonjs"
+ },
+ "files": ["wasm"]
+}
\ No newline at end of file
--- /dev/null
+/**
+ * Created by Nidin Vinayakan on 31/03/17.
+ */
+///<reference path="../webassembly.d.ts" />
+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);
+});
--- /dev/null
+/**
+ * Created by Nidin Vinayakan on 31/03/17.
+ */
+///<reference path="../webassembly.d.ts" />
+
+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);
+});
--- /dev/null
+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<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;
+}
\ No newline at end of file