OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 --expose-gc --verify-heap |
| 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 |
| 10 const verifyHeap = gc; |
| 11 let globalCounter = 10000000; |
| 12 |
| 13 function testProperties(obj) { |
| 14 for (let i = 0; i < 3; i++) { |
| 15 obj.x = 1001; |
| 16 assertEquals(1001, obj.x); |
| 17 |
| 18 obj.y = "old"; |
| 19 assertEquals("old", obj.y); |
| 20 |
| 21 delete obj.y; |
| 22 assertEquals("undefined", typeof obj.y); |
| 23 |
| 24 let uid = globalCounter++; |
| 25 let fresh = "f_" + uid; |
| 26 |
| 27 obj.z = fresh; |
| 28 assertEquals(fresh, obj.z); |
| 29 |
| 30 obj[fresh] = uid; |
| 31 assertEquals(uid, obj[fresh]); |
| 32 |
| 33 verifyHeap(); |
| 34 |
| 35 assertEquals(1001, obj.x); |
| 36 assertEquals(fresh, obj.z); |
| 37 assertEquals(uid, obj[fresh]); |
| 38 } |
| 39 |
| 40 // These properties are special for JSFunctions. |
| 41 Object.defineProperty(obj, 'name', {value: "crazy"}); |
| 42 Object.defineProperty(obj, 'length', {value: 999}); |
| 43 } |
| 44 |
| 45 function minus18(x) { return x - 18; } |
| 46 function id(x) { return x; } |
| 47 |
| 48 function printName(when, f) { |
| 49 print(" " + when + ": name=" + f.name + ", length=" + f.length); |
| 50 } |
| 51 |
| 52 (function ExportedFunctionTest() { |
| 53 print("ExportedFunctionTest"); |
| 54 |
| 55 print(" instance 1, exporting"); |
| 56 var builder = new WasmModuleBuilder(); |
| 57 builder.addFunction("exp", kSig_i_i) |
| 58 .addBody([ |
| 59 kExprGetLocal, 0, |
| 60 kExprCallFunction, 0]) |
| 61 .exportAs("exp"); |
| 62 let module1 = builder.toModule(); |
| 63 let instance1 = new WebAssembly.Instance(module1); |
| 64 let g = instance1.exports.exp; |
| 65 |
| 66 testProperties(g); |
| 67 |
| 68 // The WASM-internal fields of {g} are only inspected when {g} is |
| 69 // used as an import into another instance. |
| 70 print(" instance 2, importing"); |
| 71 var builder = new WasmModuleBuilder(); |
| 72 builder.addImport("imp", "func", kSig_i_i); |
| 73 let module2 = builder.toModule(); |
| 74 let instance2 = new WebAssembly.Instance(module2, {imp: {func: g}}); |
| 75 |
| 76 testProperties(g); |
| 77 })(); |
| 78 |
| 79 (function ImportReexportChain() { |
| 80 print("ImportReexportChain"); |
| 81 |
| 82 var f = id; |
| 83 |
| 84 for (let i = 0; i < 5; i++) { |
| 85 let builder = new WasmModuleBuilder(); |
| 86 builder.addImport("imp", "func", kSig_i_i); |
| 87 builder.addExport("exp", 0); |
| 88 let module = builder.toModule(); |
| 89 let instance = new WebAssembly.Instance(module, {imp: {func: f}}); |
| 90 let g = instance.exports.exp; |
| 91 assertInstanceof(g, Function); |
| 92 printName("before", g); |
| 93 testProperties(g); |
| 94 printName(" after", g); |
| 95 |
| 96 // The WASM-internal fields of {g} are only inspected when {g} is |
| 97 // used as an import into another instance. Use {g} as the import |
| 98 // the next time through the loop. |
| 99 f = g; |
| 100 } |
| 101 })(); |
| 102 |
| 103 |
| 104 (function ModuleTest() { |
| 105 for (f of [x => (x + 19 + globalCounter), minus18]) { |
| 106 print("ModuleTest"); |
| 107 |
| 108 let builder = new WasmModuleBuilder(); |
| 109 builder.addImport("m", "f", kSig_i_i); |
| 110 builder.addFunction("main", kSig_i_i) |
| 111 .addBody([ |
| 112 kExprGetLocal, 0, |
| 113 kExprCallFunction, 0]) |
| 114 .exportAs("main"); |
| 115 builder.addMemory(1, 1, false) |
| 116 .exportMemoryAs("memory") |
| 117 |
| 118 let module = builder.toModule(); |
| 119 testProperties(module); |
| 120 |
| 121 for (let i = 0; i < 3; i++) { |
| 122 print(" instance " + i); |
| 123 let instance = new WebAssembly.Instance(module, {m: {f: f}}); |
| 124 testProperties(instance); |
| 125 |
| 126 print(" memory " + i); |
| 127 let m = instance.exports.memory; |
| 128 assertInstanceof(m, WebAssembly.Memory); |
| 129 testProperties(m); |
| 130 |
| 131 print(" function " + i); |
| 132 let g = instance.exports.main; |
| 133 assertInstanceof(g, Function); |
| 134 printName("before", g); |
| 135 testProperties(g); |
| 136 printName(" after", g); |
| 137 assertInstanceof(g, Function); |
| 138 testProperties(g); |
| 139 for (let j = 10; j < 15; j++) { |
| 140 assertEquals(f(j), g(j)); |
| 141 } |
| 142 verifyHeap(); |
| 143 // The WASM-internal fields of {g} are only inspected when {g} is |
| 144 // used as an import into another instance. Use {g} as the import |
| 145 // the next time through the loop. |
| 146 f = g; |
| 147 } |
| 148 } |
| 149 |
| 150 })(); |
| 151 |
| 152 (function ConstructedTest() { |
| 153 print("ConstructedTest"); |
| 154 |
| 155 var memory = undefined, table = undefined; |
| 156 for (let i = 0; i < 4; i++) { |
| 157 print(" iteration " + i); |
| 158 |
| 159 let m = new WebAssembly.Memory({initial: 1}); |
| 160 let t = new WebAssembly.Table({element: "anyfunc", initial: 1}); |
| 161 m.old = memory; |
| 162 t.old = table; |
| 163 |
| 164 memory = m; |
| 165 table = t; |
| 166 testProperties(memory); |
| 167 testProperties(table); |
| 168 } |
| 169 })(); |
OLD | NEW |