chiark / gitweb /
WebAssembly declaration file for TypeScript and a dummy test.
authorNidin Vinayakan <NVinayakan@chip.de>
Fri, 31 Mar 2017 15:24:28 +0000 (17:24 +0200)
committerNidin Vinayakan <NVinayakan@chip.de>
Fri, 31 Mar 2017 15:24:28 +0000 (17:24 +0200)
README.md
test/tsconfig.json [new file with mode: 0644]
test/wasm.js [new file with mode: 0644]
test/wasm.ts [new file with mode: 0644]
webassembly.d.ts [new file with mode: 0644]

index 739add462bd64a79e0f1a3db49b9c7e5f2a335ab..ee3b7afe8eb15e621e399315701569a17147d8d8 100644 (file)
--- 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 (file)
index 0000000..5406175
--- /dev/null
@@ -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 (file)
index 0000000..1868b3b
--- /dev/null
@@ -0,0 +1,55 @@
+/**
+ * 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);
+});
diff --git a/test/wasm.ts b/test/wasm.ts
new file mode 100644 (file)
index 0000000..9b956fc
--- /dev/null
@@ -0,0 +1,64 @@
+/**
+ * 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);
+});
diff --git a/webassembly.d.ts b/webassembly.d.ts
new file mode 100644 (file)
index 0000000..0fbf2e8
--- /dev/null
@@ -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<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