| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 const incrementer_url = '../wasm/incrementer.wasm'; | 5 const incrementer_url = '../wasm/incrementer.wasm'; |
| 6 | 6 |
| 7 function TestStreamedCompile() { | 7 function TestStreamedCompile() { |
| 8 return fetch(incrementer_url) | 8 return fetch(incrementer_url) |
| 9 .then(WebAssembly.compile) | 9 .then(WebAssembly.compile) |
| 10 .then(m => new WebAssembly.Instance(m)) | 10 .then(m => new WebAssembly.Instance(m)) |
| 11 .then(i => assert_equals(5, i.exports.increment(4))); | 11 .then(i => assert_equals(5, i.exports.increment(4))); |
| 12 } | 12 } |
| 13 | 13 |
| 14 function TestShortFormStreamedCompile() { | 14 function TestShortFormStreamedCompile() { |
| 15 return WebAssembly.compile(fetch(incrementer_url)) | 15 return WebAssembly.compile(fetch(incrementer_url)) |
| 16 .then(m => new WebAssembly.Instance(m)) | 16 .then(m => new WebAssembly.Instance(m)) |
| 17 .then(i => assert_equals(5, i.exports.increment(4))); | 17 .then(i => assert_equals(5, i.exports.increment(4))); |
| 18 } | 18 } |
| 19 | 19 |
| 20 function NegativeTestStreamedCompilePromise() { | 20 function NegativeTestStreamedCompilePromise() { |
| 21 return WebAssembly.compile(new Promise((resolve, reject)=>{resolve(5);})) | 21 return WebAssembly.compile(new Promise((resolve, reject)=>{resolve(5);})) |
| 22 .then(assert_unreached, | 22 .then(assert_unreached, |
| 23 e => assert_true(e instanceof TypeError)); | 23 e => assert_true(e instanceof TypeError)); |
| 24 } | 24 } |
| 25 | 25 |
| 26 function BlankResponse() { | 26 function CompileBlankResponse() { |
| 27 return WebAssembly.compile(new Response()) | 27 return WebAssembly.compile(new Response()) |
| 28 .then(assert_unreached, | 28 .then(assert_unreached, |
| 29 e => assert_true(e instanceof TypeError)); | 29 e => assert_true(e instanceof TypeError)); |
| 30 } | 30 } |
| 31 | 31 |
| 32 function FromArrayBuffer() { | 32 function InstantiateBlankResponse() { |
| 33 return WebAssembly.instantiate(new Response()) |
| 34 .then(assert_unreached, |
| 35 e => assert_true(e instanceof TypeError)); |
| 36 } |
| 37 |
| 38 function CompileFromArrayBuffer() { |
| 33 return fetch(incrementer_url) | 39 return fetch(incrementer_url) |
| 34 .then(r => r.arrayBuffer()) | 40 .then(r => r.arrayBuffer()) |
| 35 .then(arr => new Response(arr)) | 41 .then(arr => new Response(arr)) |
| 36 .then(WebAssembly.compile) | 42 .then(WebAssembly.compile) |
| 37 .then(m => new WebAssembly.Instance(m)) | 43 .then(m => new WebAssembly.Instance(m)) |
| 38 .then(i => assert_equals(6, i.exports.increment(5))); | 44 .then(i => assert_equals(6, i.exports.increment(5))); |
| 39 } | 45 } |
| 40 | 46 |
| 41 function FromInvalidArrayBuffer() { | 47 function CompileFromInvalidArrayBuffer() { |
| 42 var arr = new ArrayBuffer(10); | 48 var arr = new ArrayBuffer(10); |
| 43 var view = new Uint8Array(arr); | 49 var view = new Uint8Array(arr); |
| 44 for (var i = 0; i < view.length; ++i) view[i] = i; | 50 for (var i = 0; i < view.length; ++i) view[i] = i; |
| 45 | 51 |
| 46 return WebAssembly.compile(new Response(arr)) | 52 return WebAssembly.compile(new Response(arr)) |
| 47 .then(assert_unreached, | 53 .then(assert_unreached, |
| 48 e => assert_true(e instanceof Error)); | 54 e => assert_true(e instanceof Error)); |
| 49 } | 55 } |
| 56 |
| 57 function InstantiateFromInvalidArrayBuffer() { |
| 58 var arr = new ArrayBuffer(10); |
| 59 var view = new Uint8Array(arr); |
| 60 for (var i = 0; i < view.length; ++i) view[i] = i; |
| 61 |
| 62 return WebAssembly.instantiate(new Response(arr)) |
| 63 .then(assert_unreached, |
| 64 e => assert_true(e instanceof Error)); |
| 65 } |
| 66 |
| 67 function TestStreamedInstantiate() { |
| 68 return fetch(incrementer_url) |
| 69 .then(WebAssembly.instantiate) |
| 70 .then(pair => assert_equals(5, pair.instance.exports.increment(4))); |
| 71 } |
| 72 |
| 73 function InstantiateFromArrayBuffer() { |
| 74 return fetch(incrementer_url) |
| 75 .then(response => response.arrayBuffer()) |
| 76 .then(WebAssembly.instantiate) |
| 77 .then(pair => assert_equals(5, pair.instance.exports.increment(4))); |
| 78 } |
| 79 |
| 80 function TestShortFormStreamedInstantiate() { |
| 81 return WebAssembly.instantiate(fetch(incrementer_url)) |
| 82 .then(pair => assert_equals(5, pair.instance.exports.increment(4))); |
| 83 } |
| 84 |
| 85 function InstantiateFromInvalidArrayBuffer() { |
| 86 var arr = new ArrayBuffer(10); |
| 87 var view = new Uint8Array(arr); |
| 88 for (var i = 0; i < view.length; ++i) view[i] = i; |
| 89 |
| 90 return WebAssembly.compile(new Response(arr)) |
| 91 .then(assert_unreached, |
| 92 e => assert_true(e instanceof Error)); |
| 93 } |
| 94 |
| 95 function buildImportingModuleBytes() { |
| 96 var builder = new WasmModuleBuilder(); |
| 97 builder.addImportedMemory("", "memory", 1); |
| 98 var kSig_v_i = makeSig([kWasmI32], []); |
| 99 var signature = builder.addType(kSig_v_i); |
| 100 builder.addImport("", "some_value", kSig_i_v); |
| 101 builder.addImport("", "writer", signature); |
| 102 |
| 103 builder.addFunction("main", kSig_i_i) |
| 104 .addBody([ |
| 105 kExprGetLocal, 0, |
| 106 kExprI32LoadMem, 0, 0, |
| 107 kExprI32Const, 1, |
| 108 kExprCallIndirect, signature, kTableZero, |
| 109 kExprGetLocal,0, |
| 110 kExprI32LoadMem,0, 0, |
| 111 kExprCallFunction, 0, |
| 112 kExprI32Add |
| 113 ]).exportFunc(); |
| 114 |
| 115 // writer(mem[i]); |
| 116 // return mem[i] + some_value(); |
| 117 builder.addFunction("_wrap_writer", signature) |
| 118 .addBody([ |
| 119 kExprGetLocal, 0, |
| 120 kExprCallFunction, 1]); |
| 121 builder.appendToTable([2, 3]); |
| 122 |
| 123 var wire_bytes = builder.toBuffer(); |
| 124 return wire_bytes; |
| 125 } |
| 126 |
| 127 function TestInstantiateComplexModule() { |
| 128 var mem_1 = new WebAssembly.Memory({initial: 1}); |
| 129 var view_1 = new Int32Array(mem_1.buffer); |
| 130 view_1[0] = 42; |
| 131 var outval_1; |
| 132 |
| 133 var ffi = {"": |
| 134 {some_value: () => 1, |
| 135 writer: (x) => outval_1 = x , |
| 136 memory: mem_1} |
| 137 }; |
| 138 return Promise.resolve(buildImportingModuleBytes()) |
| 139 .then(b => WebAssembly.instantiate(b, ffi)) |
| 140 .then(pair => assert_true(pair.instance instanceof WebAssembly.Instance)); |
| 141 } |
| OLD | NEW |