|
| 1 | +describe("Test WebAssembly ", () => { |
| 2 | + // https://wasdk.github.io/WasmFiddle/?15acre |
| 3 | + // |
| 4 | + // #include <stdio.h> |
| 5 | + // #include <sys/uio.h> |
| 6 | + // |
| 7 | + // #define WASM_EXPORT __attribute__((visibility("default"))) |
| 8 | + // |
| 9 | + // extern double logarithm(double value); |
| 10 | + // |
| 11 | + // WASM_EXPORT int log(double value) { |
| 12 | + // return logarithm(value); |
| 13 | + // } |
| 14 | + let wasmCode = new Uint8Array([ |
| 15 | + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x8b, 0x80, 0x80, 0x80, 0x00, 0x02, |
| 16 | + 0x60, 0x01, 0x7c, 0x01, 0x7c, 0x60, 0x01, 0x7c, 0x01, 0x7f, 0x02, 0x91, 0x80, 0x80, 0x80, |
| 17 | + 0x00, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x09, 0x6c, 0x6f, 0x67, 0x61, 0x72, 0x69, 0x74, 0x68, |
| 18 | + 0x6d, 0x00, 0x00, 0x03, 0x82, 0x80, 0x80, 0x80, 0x00, 0x01, 0x01, 0x04, 0x84, 0x80, 0x80, |
| 19 | + 0x80, 0x00, 0x01, 0x70, 0x00, 0x00, 0x05, 0x83, 0x80, 0x80, 0x80, 0x00, 0x01, 0x00, 0x01, |
| 20 | + 0x06, 0x81, 0x80, 0x80, 0x80, 0x00, 0x00, 0x07, 0x90, 0x80, 0x80, 0x80, 0x00, 0x02, 0x06, |
| 21 | + 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x03, 0x6c, 0x6f, 0x67, 0x00, 0x01, 0x0a, |
| 22 | + 0x8d, 0x80, 0x80, 0x80, 0x00, 0x01, 0x87, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x00, 0x10, |
| 23 | + 0x00, 0xaa, 0x0b |
| 24 | + ]); |
| 25 | + |
| 26 | + it("Handle compilation failures", done => { |
| 27 | + WebAssembly.compile(new Uint8Array([ 1, 2, 3, 4 ])).then(moduleInstance => { |
| 28 | + expect(true).toBe(false, "The success callback of the compilation promise was called"); |
| 29 | + done(); |
| 30 | + }).catch(e => { |
| 31 | + expect(e.name).toEqual("CompileError"); |
| 32 | + expect(e.message).toEqual("WebAssembly.compile(): expected magic word 00 61 73 6d, found 01 02 03 04 @+0"); |
| 33 | + done(); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it("Compile and instantiate a WebAssembly module asynchronously", done => { |
| 38 | + let importsObj = { |
| 39 | + env: { |
| 40 | + logarithm: Math.log |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + WebAssembly.compile(wasmCode).then(wasmModule => { |
| 45 | + WebAssembly.instantiate(wasmModule, importsObj).then(moduleInstance => { |
| 46 | + expect(moduleInstance).toBeDefined(); |
| 47 | + expect(moduleInstance.exports).toBeDefined(); |
| 48 | + expect(moduleInstance.exports.log).toEqual(jasmine.any(Function)); |
| 49 | + let actual = moduleInstance.exports.log(Math.E); |
| 50 | + expect(actual).toEqual(1); |
| 51 | + done(); |
| 52 | + }).catch(e => { |
| 53 | + expect(true).toBe(false, "An unexpected error occurred while instantiating the WebAssembly module: " + e.toString()); |
| 54 | + done(); |
| 55 | + }); |
| 56 | + }).catch(e => { |
| 57 | + expect(true).toBe(false, "An unexpected error occurred while compiling the WebAssembly module: " + e.toString()); |
| 58 | + done(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it("Compile and instantiate a WebAssembly module inside a worker", done => { |
| 63 | + let worker = new Worker("./testWebAssemblyWorker"); |
| 64 | + |
| 65 | + worker.onmessage = msg => { |
| 66 | + expect(msg.data).toEqual(1); |
| 67 | + worker.terminate(); |
| 68 | + done(); |
| 69 | + }; |
| 70 | + |
| 71 | + worker.postMessage(Array.from(wasmCode)); |
| 72 | + }); |
| 73 | +}); |
0 commit comments