OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project 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 // Flags: --expose-wasm | 5 // Flags: --expose-wasm |
6 | 6 |
7 load("test/mjsunit/wasm/wasm-constants.js"); | 7 load("test/mjsunit/wasm/wasm-constants.js"); |
8 load("test/mjsunit/wasm/wasm-module-builder.js"); | 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
9 | 9 |
10 let kReturnValue = 117; | 10 let kReturnValue = 117; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 | 51 |
52 // Deprecated experimental API. | 52 // Deprecated experimental API. |
53 CheckInstance(Wasm.instantiateModule(buffer)); | 53 CheckInstance(Wasm.instantiateModule(buffer)); |
54 | 54 |
55 // Official API | 55 // Official API |
56 let module = new WebAssembly.Module(buffer); | 56 let module = new WebAssembly.Module(buffer); |
57 CheckInstance(new WebAssembly.Instance(module)); | 57 CheckInstance(new WebAssembly.Instance(module)); |
58 | 58 |
59 let promise = WebAssembly.compile(buffer); | 59 let promise = WebAssembly.compile(buffer); |
60 promise.then(module => CheckInstance(new WebAssembly.Instance(module))); | 60 promise.then(module => CheckInstance(new WebAssembly.Instance(module))); |
| 61 |
| 62 // Negative tests. |
| 63 (function InvalidModules() { |
| 64 let invalid_cases = [undefined, 1, "", "a", {some:1, obj: "b"}]; |
| 65 let len = invalid_cases.length; |
| 66 for (var i = 0; i < len; ++i) { |
| 67 try { |
| 68 let instance = new WebAssembly.Instance(1); |
| 69 assertUnreachable("should not be able to instantiate invalid modules."); |
| 70 } catch (e) { |
| 71 assertContains("Argument 0", e.toString()); |
| 72 } |
| 73 } |
| 74 })(); |
| 75 |
| 76 // Compile async an invalid blob. |
| 77 (function InvalidBinaryAsyncCompilation() { |
| 78 let builder = new WasmModuleBuilder(); |
| 79 builder.addFunction("f", kSig_i_i) |
| 80 .addBody([kExprCallImport, kArity0, 0]); |
| 81 let promise = WebAssembly.compile(builder.toBuffer()); |
| 82 promise |
| 83 .then(compiled => |
| 84 assertUnreachable("should not be able to compile invalid blob.")) |
| 85 .catch(e => assertContains("invalid signature index", e.toString())); |
| 86 })(); |
| 87 |
| 88 // Multiple instances tests. |
| 89 (function ManyInstances() { |
| 90 let compiled_module = new WebAssembly.Module(buffer); |
| 91 let instance_1 = new WebAssembly.Instance(compiled_module); |
| 92 let instance_2 = new WebAssembly.Instance(compiled_module); |
| 93 assertTrue(instance_1 != instance_2); |
| 94 })(); |
| 95 |
| 96 (function ManyInstancesAsync() { |
| 97 let promise = WebAssembly.compile(buffer); |
| 98 promise.then(compiled_module => { |
| 99 let instance_1 = new WebAssembly.Instance(compiled_module); |
| 100 let instance_2 = new WebAssembly.Instance(compiled_module); |
| 101 assertTrue(instance_1 != instance_2); |
| 102 }); |
| 103 })(); |
| 104 |
| 105 (function InstancesAreIsolatedFromEachother() { |
| 106 var builder = new WasmModuleBuilder(); |
| 107 builder.addMemory(1,1, true); |
| 108 var kSig_v_i = makeSig([kAstI32], []); |
| 109 var signature = builder.addType(kSig_v_i); |
| 110 builder.addImport("some_value", kSig_i); |
| 111 builder.addImport("writer", signature); |
| 112 |
| 113 builder.addFunction("main", kSig_i_i) |
| 114 .addBody([ |
| 115 kExprI32Const, 1, |
| 116 kExprGetLocal, 0, |
| 117 kExprI32LoadMem, 0, 0, |
| 118 kExprCallIndirect, kArity1, signature, |
| 119 kExprGetLocal,0, |
| 120 kExprI32LoadMem,0, 0, |
| 121 kExprCallImport, kArity0, 0, |
| 122 kExprI32Add |
| 123 ]).exportFunc(); |
| 124 |
| 125 // writer(mem[i]); |
| 126 // return mem[i] + some_value(); |
| 127 builder.addFunction("_wrap_writer", signature) |
| 128 .addBody([ |
| 129 kExprGetLocal, 0, |
| 130 kExprCallImport, kArity1, 1]); |
| 131 builder.appendToTable([0, 1]); |
| 132 |
| 133 |
| 134 var module = new WebAssembly.Module(builder.toBuffer()); |
| 135 var mem_1 = new ArrayBuffer(4); |
| 136 var mem_2 = new ArrayBuffer(4); |
| 137 var view_1 = new Int32Array(mem_1); |
| 138 var view_2 = new Int32Array(mem_2); |
| 139 |
| 140 view_1[0] = 42; |
| 141 view_2[0] = 1000; |
| 142 |
| 143 var outval_1; |
| 144 var outval_2; |
| 145 var i1 = new WebAssembly.Instance(module, {some_value: () => 1, |
| 146 writer: (x)=>outval_1 = x }, mem_1); |
| 147 var i2 = new WebAssembly.Instance(module, {some_value: () => 2, |
| 148 writer: (x)=>outval_2 = x }, mem_2); |
| 149 |
| 150 assertEquals(43, i1.exports.main(0)); |
| 151 assertEquals(1002, i2.exports.main(0)); |
| 152 |
| 153 assertEquals(42, outval_1); |
| 154 assertEquals(1000, outval_2); |
| 155 })(); |
OLD | NEW |