Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: test/mjsunit/wasm/instantiate-module-basic.js

Issue 2345593003: [wasm] Master CL for Binary 0xC changes. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix test failures and TSAN races. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/mjsunit/wasm/instance-gc.js ('k') | test/mjsunit/wasm/module-memory.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 13 matching lines...) Expand all
24 assertFalse(instance === null); 24 assertFalse(instance === null);
25 assertFalse(instance === 0); 25 assertFalse(instance === 0);
26 assertEquals("object", typeof instance); 26 assertEquals("object", typeof instance);
27 27
28 // Check the memory is an ArrayBuffer. 28 // Check the memory is an ArrayBuffer.
29 var mem = instance.exports.memory; 29 var mem = instance.exports.memory;
30 assertFalse(mem === undefined); 30 assertFalse(mem === undefined);
31 assertFalse(mem === null); 31 assertFalse(mem === null);
32 assertFalse(mem === 0); 32 assertFalse(mem === 0);
33 assertEquals("object", typeof mem); 33 assertEquals("object", typeof mem);
34 assertTrue(mem instanceof ArrayBuffer); 34 assertTrue(mem instanceof WebAssembly.Memory);
35 for (let i = 0; i < 4; i++) { 35 var buf = mem.buffer;
36 assertTrue(buf instanceof ArrayBuffer);
37 assertEquals(65536, buf.byteLength);
38 for (var i = 0; i < 4; i++) {
36 instance.exports.memory = 0; // should be ignored 39 instance.exports.memory = 0; // should be ignored
40 mem.buffer = 0; // should be ignored
37 assertSame(mem, instance.exports.memory); 41 assertSame(mem, instance.exports.memory);
42 assertSame(buf, mem.buffer);
38 } 43 }
39 44
40 assertEquals(65536, instance.exports.memory.byteLength);
41
42 // Check the properties of the main function. 45 // Check the properties of the main function.
43 let main = instance.exports.main; 46 let main = instance.exports.main;
44 assertFalse(main === undefined); 47 assertFalse(main === undefined);
45 assertFalse(main === null); 48 assertFalse(main === null);
46 assertFalse(main === 0); 49 assertFalse(main === 0);
47 assertEquals("function", typeof main); 50 assertEquals("function", typeof main);
48 51
49 assertEquals(kReturnValue, main()); 52 assertEquals(kReturnValue, main());
50 } 53 }
51 54
52 // Deprecated experimental API. 55 // Deprecated experimental API.
53 CheckInstance(Wasm.instantiateModule(buffer)); 56 CheckInstance(Wasm.instantiateModule(buffer));
54 57
55 // Official API 58 // Official API
56 let module = new WebAssembly.Module(buffer); 59 let module = new WebAssembly.Module(buffer);
57 CheckInstance(new WebAssembly.Instance(module)); 60 CheckInstance(new WebAssembly.Instance(module));
58 61
59 let promise = WebAssembly.compile(buffer); 62 let promise = WebAssembly.compile(buffer);
60 promise.then(module => CheckInstance(new WebAssembly.Instance(module))); 63 promise.then(module => CheckInstance(new WebAssembly.Instance(module)));
61 64
62 // Negative tests. 65 // Negative tests.
63 (function InvalidModules() { 66 (function InvalidModules() {
67 print("InvalidModules...");
64 let invalid_cases = [undefined, 1, "", "a", {some:1, obj: "b"}]; 68 let invalid_cases = [undefined, 1, "", "a", {some:1, obj: "b"}];
65 let len = invalid_cases.length; 69 let len = invalid_cases.length;
66 for (var i = 0; i < len; ++i) { 70 for (var i = 0; i < len; ++i) {
67 try { 71 try {
68 let instance = new WebAssembly.Instance(1); 72 let instance = new WebAssembly.Instance(1);
69 assertUnreachable("should not be able to instantiate invalid modules."); 73 assertUnreachable("should not be able to instantiate invalid modules.");
70 } catch (e) { 74 } catch (e) {
71 assertContains("Argument 0", e.toString()); 75 assertContains("Argument 0", e.toString());
72 } 76 }
73 } 77 }
74 })(); 78 })();
75 79
76 // Compile async an invalid blob. 80 // Compile async an invalid blob.
77 (function InvalidBinaryAsyncCompilation() { 81 (function InvalidBinaryAsyncCompilation() {
82 print("InvalidBinaryAsyncCompilation...");
78 let builder = new WasmModuleBuilder(); 83 let builder = new WasmModuleBuilder();
79 builder.addFunction("f", kSig_i_i) 84 builder.addFunction("f", kSig_i_i)
80 .addBody([kExprCallImport, kArity0, 0]); 85 .addBody([kExprCallFunction, 0]);
81 let promise = WebAssembly.compile(builder.toBuffer()); 86 let promise = WebAssembly.compile(builder.toBuffer());
82 promise 87 promise
83 .then(compiled => 88 .then(compiled =>
84 assertUnreachable("should not be able to compile invalid blob.")) 89 assertUnreachable("should not be able to compile invalid blob."))
85 .catch(e => assertContains("invalid signature index", e.toString())); 90 .catch(e => assertContains("invalid signature index", e.toString()));
86 })(); 91 })();
87 92
88 // Multiple instances tests. 93 // Multiple instances tests.
89 (function ManyInstances() { 94 (function ManyInstances() {
95 print("ManyInstances...");
90 let compiled_module = new WebAssembly.Module(buffer); 96 let compiled_module = new WebAssembly.Module(buffer);
91 let instance_1 = new WebAssembly.Instance(compiled_module); 97 let instance_1 = new WebAssembly.Instance(compiled_module);
92 let instance_2 = new WebAssembly.Instance(compiled_module); 98 let instance_2 = new WebAssembly.Instance(compiled_module);
93 assertTrue(instance_1 != instance_2); 99 assertTrue(instance_1 != instance_2);
94 })(); 100 })();
95 101
96 (function ManyInstancesAsync() { 102 (function ManyInstancesAsync() {
103 print("ManyInstancesAsync...");
97 let promise = WebAssembly.compile(buffer); 104 let promise = WebAssembly.compile(buffer);
98 promise.then(compiled_module => { 105 promise.then(compiled_module => {
99 let instance_1 = new WebAssembly.Instance(compiled_module); 106 let instance_1 = new WebAssembly.Instance(compiled_module);
100 let instance_2 = new WebAssembly.Instance(compiled_module); 107 let instance_2 = new WebAssembly.Instance(compiled_module);
101 assertTrue(instance_1 != instance_2); 108 assertTrue(instance_1 != instance_2);
102 }); 109 });
103 })(); 110 })();
104 111
105 (function InstancesAreIsolatedFromEachother() { 112 (function InstancesAreIsolatedFromEachother() {
113 print("InstancesAreIsolatedFromEachother...");
106 var builder = new WasmModuleBuilder(); 114 var builder = new WasmModuleBuilder();
107 builder.addMemory(1,1, true); 115 builder.addMemory(1,1, true);
108 var kSig_v_i = makeSig([kAstI32], []); 116 var kSig_v_i = makeSig([kAstI32], []);
109 var signature = builder.addType(kSig_v_i); 117 var signature = builder.addType(kSig_v_i);
110 builder.addImport("some_value", kSig_i); 118 builder.addImport("some_value", kSig_i);
111 builder.addImport("writer", signature); 119 builder.addImport("writer", signature);
112 120
113 builder.addFunction("main", kSig_i_i) 121 builder.addFunction("main", kSig_i_i)
114 .addBody([ 122 .addBody([
115 kExprI32Const, 1,
116 kExprGetLocal, 0, 123 kExprGetLocal, 0,
117 kExprI32LoadMem, 0, 0, 124 kExprI32LoadMem, 0, 0,
118 kExprCallIndirect, kArity1, signature, 125 kExprI32Const, 1,
126 kExprCallIndirect, signature,
119 kExprGetLocal,0, 127 kExprGetLocal,0,
120 kExprI32LoadMem,0, 0, 128 kExprI32LoadMem,0, 0,
121 kExprCallImport, kArity0, 0, 129 kExprCallFunction, 0,
122 kExprI32Add 130 kExprI32Add
123 ]).exportFunc(); 131 ]).exportFunc();
124 132
125 // writer(mem[i]); 133 // writer(mem[i]);
126 // return mem[i] + some_value(); 134 // return mem[i] + some_value();
127 builder.addFunction("_wrap_writer", signature) 135 builder.addFunction("_wrap_writer", signature)
128 .addBody([ 136 .addBody([
129 kExprGetLocal, 0, 137 kExprGetLocal, 0,
130 kExprCallImport, kArity1, 1]); 138 kExprCallFunction, 1]);
131 builder.appendToTable([0, 1]); 139 builder.appendToTable([2, 3]);
132 140
133 141
134 var module = new WebAssembly.Module(builder.toBuffer()); 142 var module = new WebAssembly.Module(builder.toBuffer());
135 var mem_1 = new ArrayBuffer(4); 143 var mem_1 = new ArrayBuffer(4);
136 var mem_2 = new ArrayBuffer(4); 144 var mem_2 = new ArrayBuffer(4);
137 var view_1 = new Int32Array(mem_1); 145 var view_1 = new Int32Array(mem_1);
138 var view_2 = new Int32Array(mem_2); 146 var view_2 = new Int32Array(mem_2);
139 147
140 view_1[0] = 42; 148 view_1[0] = 42;
141 view_2[0] = 1000; 149 view_2[0] = 1000;
142 150
143 var outval_1; 151 var outval_1;
144 var outval_2; 152 var outval_2;
145 var i1 = new WebAssembly.Instance(module, {some_value: () => 1, 153 var i1 = new WebAssembly.Instance(module, {some_value: () => 1,
146 writer: (x)=>outval_1 = x }, mem_1); 154 writer: (x)=>outval_1 = x }, mem_1);
147 var i2 = new WebAssembly.Instance(module, {some_value: () => 2, 155 var i2 = new WebAssembly.Instance(module, {some_value: () => 2,
148 writer: (x)=>outval_2 = x }, mem_2); 156 writer: (x)=>outval_2 = x }, mem_2);
149 157
150 assertEquals(43, i1.exports.main(0)); 158 assertEquals(43, i1.exports.main(0));
151 assertEquals(1002, i2.exports.main(0)); 159 assertEquals(1002, i2.exports.main(0));
152 160
153 assertEquals(42, outval_1); 161 assertEquals(42, outval_1);
154 assertEquals(1000, outval_2); 162 assertEquals(1000, outval_2);
155 })(); 163 })();
156 164
157 (function GlobalsArePrivateToTheInstance() { 165 (function GlobalsArePrivateToTheInstance() {
166 print("GlobalsArePrivateToTheInstance...");
158 var builder = new WasmModuleBuilder(); 167 var builder = new WasmModuleBuilder();
159 builder.addGlobal(kAstI32); 168 builder.addGlobal(kAstI32);
160 builder.addFunction("read", kSig_i_v) 169 builder.addFunction("read", kSig_i_v)
161 .addBody([ 170 .addBody([
162 kExprGetGlobal, 0]) 171 kExprGetGlobal, 0])
163 .exportFunc(); 172 .exportFunc();
164 173
165 builder.addFunction("write", kSig_v_i) 174 builder.addFunction("write", kSig_v_i)
166 .addBody([ 175 .addBody([
167 kExprGetLocal, 0, 176 kExprGetLocal, 0,
168 kExprSetGlobal, 0]) 177 kExprSetGlobal, 0])
169 .exportFunc(); 178 .exportFunc();
170 179
171 var module = new WebAssembly.Module(builder.toBuffer()); 180 var module = new WebAssembly.Module(builder.toBuffer());
172 var i1 = new WebAssembly.Instance(module); 181 var i1 = new WebAssembly.Instance(module);
173 var i2 = new WebAssembly.Instance(module); 182 var i2 = new WebAssembly.Instance(module);
174 i1.exports.write(1); 183 i1.exports.write(1);
175 i2.exports.write(2); 184 i2.exports.write(2);
176 assertEquals(1, i1.exports.read()); 185 assertEquals(1, i1.exports.read());
177 assertEquals(2, i2.exports.read()); 186 assertEquals(2, i2.exports.read());
178 })(); 187 })();
179 188
180 189
181 (function InstanceMemoryIsIsolated() { 190 (function InstanceMemoryIsIsolated() {
191 print("InstanceMemoryIsIsolated...");
182 var builder = new WasmModuleBuilder(); 192 var builder = new WasmModuleBuilder();
183 builder.addMemory(1,1, true); 193 builder.addMemory(1,1, true);
184 194
185 builder.addFunction("f", kSig_i) 195 builder.addFunction("f", kSig_i)
186 .addBody([ 196 .addBody([
187 kExprI32Const, 0, 197 kExprI32Const, 0,
188 kExprI32LoadMem, 0, 0 198 kExprI32LoadMem, 0, 0
189 ]).exportFunc(); 199 ]).exportFunc();
190 200
191 var mem_1 = new ArrayBuffer(65536); 201 var mem_1 = new ArrayBuffer(65536);
192 var mem_2 = new ArrayBuffer(65536); 202 var mem_2 = new ArrayBuffer(65536);
193 var view_1 = new Int32Array(mem_1); 203 var view_1 = new Int32Array(mem_1);
194 var view_2 = new Int32Array(mem_2); 204 var view_2 = new Int32Array(mem_2);
195 view_1[0] = 1; 205 view_1[0] = 1;
196 view_2[0] = 1000; 206 view_2[0] = 1000;
197 207
198 var module = new WebAssembly.Module(builder.toBuffer()); 208 var module = new WebAssembly.Module(builder.toBuffer());
199 var i1 = new WebAssembly.Instance(module, null, mem_1); 209 var i1 = new WebAssembly.Instance(module, null, mem_1);
200 var i2 = new WebAssembly.Instance(module, null, mem_2); 210 var i2 = new WebAssembly.Instance(module, null, mem_2);
201 211
202 assertEquals(1, i1.exports.f()); 212 assertEquals(1, i1.exports.f());
203 assertEquals(1000, i2.exports.f()); 213 assertEquals(1000, i2.exports.f());
204 })(); 214 })();
OLDNEW
« no previous file with comments | « test/mjsunit/wasm/instance-gc.js ('k') | test/mjsunit/wasm/module-memory.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698