OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --expose-wasm |
| 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 |
| 10 var debug = false; |
| 11 |
| 12 function SimpleDataSegmentTest(offset) { |
| 13 print("SimpleDataSegmentTest(" + offset + ")..."); |
| 14 var builder = new WasmModuleBuilder(); |
| 15 builder.addMemory(1, 1, false); |
| 16 builder.addFunction("load", kSig_i_i) |
| 17 .addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0]) |
| 18 .exportAs("load"); |
| 19 builder.addDataSegment(offset, [9, 9, 9, 9]); |
| 20 |
| 21 var buffer = builder.toBuffer(debug); |
| 22 var instance = Wasm.instantiateModule(buffer); |
| 23 for (var i = offset - 20; i < offset + 20; i += 4) { |
| 24 if (i < 0) continue; |
| 25 var expected = (i == offset) ? 151587081 : 0; |
| 26 assertEquals(expected, instance.exports.load(i)); |
| 27 } |
| 28 } |
| 29 |
| 30 SimpleDataSegmentTest(0); |
| 31 SimpleDataSegmentTest(4); |
| 32 SimpleDataSegmentTest(12); |
| 33 SimpleDataSegmentTest(1064); |
| 34 |
| 35 function GlobalInitTest(offset) { |
| 36 print("GlobalInitTest(" + offset + ")..."); |
| 37 var builder = new WasmModuleBuilder(); |
| 38 builder.addMemory(1, 1, false); |
| 39 var g = builder.addGlobal(kAstI32, false); |
| 40 g.init = offset; |
| 41 builder.addFunction("load", kSig_i_i) |
| 42 .addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0]) |
| 43 .exportAs("load"); |
| 44 builder.addDataSegment(g.index, [7, 7, 7, 7], true); |
| 45 |
| 46 var buffer = builder.toBuffer(debug); |
| 47 var instance = Wasm.instantiateModule(buffer); |
| 48 for (var i = offset - 20; i < offset + 20; i += 4) { |
| 49 if (i < 0) continue; |
| 50 var expected = i == offset ? 117901063 : 0; |
| 51 assertEquals(expected, instance.exports.load(i)); |
| 52 } |
| 53 } |
| 54 |
| 55 GlobalInitTest(0); |
| 56 GlobalInitTest(12); |
| 57 GlobalInitTest(3040); |
| 58 |
| 59 function GlobalImportedInitTest(pad) { |
| 60 print("GlobaleImportedInitTest(" + pad + ")..."); |
| 61 var builder = new WasmModuleBuilder(); |
| 62 builder.addMemory(1, 1, false); |
| 63 |
| 64 while (pad-- > 0) builder.addGlobal(kAstI32); // pad |
| 65 |
| 66 var g = builder.addImportedGlobal("offset", undefined, kAstI32); |
| 67 |
| 68 while (pad-- > 0) builder.addGlobal(kAstI32); // pad |
| 69 |
| 70 builder.addFunction("load", kSig_i_i) |
| 71 .addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0]) |
| 72 .exportAs("load"); |
| 73 builder.addDataSegment(g.index, [5, 5, 5, 5], true); |
| 74 |
| 75 var buffer = builder.toBuffer(debug); |
| 76 var module = new WebAssembly.Module(buffer); |
| 77 |
| 78 for (var offset of [0, 12, 192, 1024]) { |
| 79 var instance = new WebAssembly.Instance(module, {offset: offset}); |
| 80 for (var i = offset - 20; i < offset + 20; i += 4) { |
| 81 if (i < 0) continue; |
| 82 var expected = i == offset ? 84215045 : 0; |
| 83 assertEquals(expected, instance.exports.load(i)); |
| 84 } |
| 85 } |
| 86 } |
| 87 |
| 88 GlobalImportedInitTest(0); |
| 89 GlobalImportedInitTest(1); |
| 90 GlobalImportedInitTest(4); |
OLD | NEW |