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 --allow-natives-syntax |
| 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 |
| 10 var expect_elison = 0; |
| 11 var expect_no_elison = 1; |
| 12 // function calls stack: first_export -> first_func -> first_import -> |
| 13 // second_export -> second_import |
| 14 // In this case, first_import and second_export have same signature, |
| 15 // So that wrappers will be removed |
| 16 (function TestWasmWrapperElision() { |
| 17 var imported = function (a) { |
| 18 return a; |
| 19 }; |
| 20 |
| 21 var second_module = new WasmModuleBuilder(); |
| 22 var sig_index = second_module.addType(kSig_i_i); |
| 23 second_module |
| 24 .addImportWithModule("import_module_2", "import_name_2", sig_index); |
| 25 second_module |
| 26 .addFunction("second_export", sig_index) |
| 27 .addBody([ |
| 28 kExprGetLocal, 0, |
| 29 kExprCallImport, kArity1, 0, |
| 30 kExprReturn, kArity1 |
| 31 ]) |
| 32 .exportFunc(); |
| 33 |
| 34 var first_module = new WasmModuleBuilder(); |
| 35 var sig_index = first_module.addType(kSig_i_i); |
| 36 first_module |
| 37 .addImportWithModule("import_module_1", "import_name_1", sig_index); |
| 38 first_module |
| 39 .addFunction("first_export", sig_index) |
| 40 .addBody([ |
| 41 kExprGetLocal, 0, |
| 42 kExprCallFunction, kArity1, 1, |
| 43 kExprReturn, kArity1 |
| 44 ]) |
| 45 .exportFunc(); |
| 46 first_module |
| 47 .addFunction("first_func", sig_index) |
| 48 .addBody([ |
| 49 kExprI32Const, 1, |
| 50 kExprGetLocal, 0, |
| 51 kExprI32Add, |
| 52 kExprCallImport, kArity1, 0, |
| 53 kExprReturn, kArity1 |
| 54 ]); |
| 55 |
| 56 var f = second_module |
| 57 .instantiate({import_module_2: {import_name_2: imported}}) |
| 58 .exports.second_export; |
| 59 var the_export = first_module |
| 60 .instantiate({import_module_1: {import_name_1: f}}) |
| 61 .exports.first_export; |
| 62 assertEquals(the_export(2), 3); |
| 63 assertEquals(the_export(-1), 0); |
| 64 assertEquals(the_export(0), 1); |
| 65 assertEquals(the_export(5.5), 6); |
| 66 assertEquals(%CheckWasmWrapperElision(the_export, expect_elison), true); |
| 67 })(); |
| 68 |
| 69 // function calls stack: first_export -> first_func -> first_import -> |
| 70 // second_export -> second_import |
| 71 // In this case, second_export has less params than first_import, |
| 72 // So that wrappers will not be removed |
| 73 (function TestWasmWrapperNoElisionLessParams() { |
| 74 var imported = function (a) { |
| 75 return a; |
| 76 }; |
| 77 |
| 78 var second_module = new WasmModuleBuilder(); |
| 79 var sig_index_1 = second_module.addType(kSig_i_i); |
| 80 second_module |
| 81 .addImportWithModule("import_module_2", "import_name_2", sig_index_1); |
| 82 second_module |
| 83 .addFunction("second_export", sig_index_1) |
| 84 .addBody([ |
| 85 kExprGetLocal, 0, |
| 86 kExprCallImport, kArity1, 0, |
| 87 kExprReturn, kArity1 |
| 88 ]) |
| 89 .exportFunc(); |
| 90 |
| 91 var first_module = new WasmModuleBuilder(); |
| 92 var sig_index_2 = first_module.addType(kSig_i_ii); |
| 93 first_module |
| 94 .addImportWithModule("import_module_1", "import_name_1", sig_index_2); |
| 95 first_module |
| 96 .addFunction("first_export", sig_index_2) |
| 97 .addBody([ |
| 98 kExprGetLocal, 0, |
| 99 kExprGetLocal, 1, |
| 100 kExprCallFunction, kArity2, 1, |
| 101 kExprReturn, kArity1 |
| 102 ]) |
| 103 .exportFunc(); |
| 104 first_module |
| 105 .addFunction("first_func", sig_index_2) |
| 106 .addBody([ |
| 107 kExprGetLocal, 0, |
| 108 kExprGetLocal, 1, |
| 109 kExprCallImport, kArity2, 0, |
| 110 kExprReturn, kArity1 |
| 111 ]); |
| 112 |
| 113 var f = second_module |
| 114 .instantiate({import_module_2: {import_name_2: imported}}) |
| 115 .exports.second_export; |
| 116 var the_export = first_module |
| 117 .instantiate({import_module_1: {import_name_1: f}}) |
| 118 .exports.first_export; |
| 119 assertEquals(the_export(4, 5), 4); |
| 120 assertEquals(the_export(-1, 4), -1); |
| 121 assertEquals(the_export(0, 2), 0); |
| 122 assertEquals(the_export(9.9, 4.3), 9); |
| 123 assertEquals(%CheckWasmWrapperElision(the_export, expect_no_elison), true); |
| 124 })(); |
| 125 |
| 126 // function calls stack: first_export -> first_func -> first_import -> |
| 127 // second_export -> second_import |
| 128 // In this case, second_export has more params than first_import, |
| 129 // So that wrappers will not be removed |
| 130 (function TestWasmWrapperNoElisionMoreParams() { |
| 131 var imported = function (a, b, c) { |
| 132 return a+b+c; |
| 133 }; |
| 134 |
| 135 var second_module = new WasmModuleBuilder(); |
| 136 var sig_index_3 = second_module.addType(kSig_i_iii); |
| 137 second_module |
| 138 .addImportWithModule("import_module_2", "import_name_2", sig_index_3); |
| 139 second_module |
| 140 .addFunction("second_export", sig_index_3) |
| 141 .addBody([ |
| 142 kExprGetLocal, 0, |
| 143 kExprGetLocal, 1, |
| 144 kExprGetLocal, 2, |
| 145 kExprCallImport, kArity3, 0, |
| 146 kExprReturn, kArity1 |
| 147 ]) |
| 148 .exportFunc(); |
| 149 |
| 150 var first_module = new WasmModuleBuilder(); |
| 151 var sig_index_2 = first_module.addType(kSig_i_ii); |
| 152 first_module |
| 153 .addImportWithModule("import_module_1", "import_name_1", sig_index_2); |
| 154 first_module |
| 155 .addFunction("first_export", sig_index_2) |
| 156 .addBody([ |
| 157 kExprGetLocal, 0, |
| 158 kExprGetLocal, 1, |
| 159 kExprCallFunction, kArity2, 1, |
| 160 kExprReturn, kArity1 |
| 161 ]) |
| 162 .exportFunc(); |
| 163 first_module |
| 164 .addFunction("first_func", sig_index_2) |
| 165 .addBody([ |
| 166 kExprGetLocal, 0, |
| 167 kExprGetLocal, 1, |
| 168 kExprCallImport, kArity2, 0, |
| 169 kExprReturn, kArity1 |
| 170 ]); |
| 171 |
| 172 var f = second_module |
| 173 .instantiate({import_module_2: {import_name_2: imported}}) |
| 174 .exports.second_export; |
| 175 var the_export = first_module |
| 176 .instantiate({import_module_1: {import_name_1: f}}) |
| 177 .exports.first_export; |
| 178 assertEquals(the_export(5, 6), 11); |
| 179 assertEquals(the_export(-1, -4), -5); |
| 180 assertEquals(the_export(0, 0), 0); |
| 181 assertEquals(the_export(1.1, 2.7), 3); |
| 182 assertEquals(%CheckWasmWrapperElision(the_export, expect_no_elison), true); |
| 183 })(); |
| 184 |
| 185 // function calls stack: first_export -> first_func -> first_import -> |
| 186 // second_export -> second_import |
| 187 // In this case, second_export has different params type with first_import, |
| 188 // So that wrappers will not be removed |
| 189 (function TestWasmWrapperNoElisionTypeMismatch() { |
| 190 var imported = function (a, b) { |
| 191 return a+b; |
| 192 }; |
| 193 |
| 194 var second_module = new WasmModuleBuilder(); |
| 195 var sig_index_2 = second_module.addType(kSig_d_dd); |
| 196 second_module |
| 197 .addImportWithModule("import_module_2", "import_name_2", sig_index_2); |
| 198 second_module |
| 199 .addFunction("second_export", sig_index_2) |
| 200 .addBody([ |
| 201 kExprGetLocal, 0, |
| 202 kExprGetLocal, 1, |
| 203 kExprCallImport, kArity2, 0, |
| 204 kExprReturn, kArity1 |
| 205 ]) |
| 206 .exportFunc(); |
| 207 |
| 208 var first_module = new WasmModuleBuilder(); |
| 209 var sig_index_2 = first_module.addType(kSig_i_ii); |
| 210 first_module |
| 211 .addImportWithModule("import_module_1", "import_name_1", sig_index_2); |
| 212 first_module |
| 213 .addFunction("first_export", sig_index_2) |
| 214 .addBody([ |
| 215 kExprGetLocal, 0, |
| 216 kExprGetLocal, 1, |
| 217 kExprCallFunction, kArity2, 1, |
| 218 kExprReturn, kArity1 |
| 219 ]) |
| 220 .exportFunc(); |
| 221 first_module |
| 222 .addFunction("first_func", sig_index_2) |
| 223 .addBody([ |
| 224 kExprGetLocal, 0, |
| 225 kExprGetLocal, 1, |
| 226 kExprCallImport, kArity2, 0, |
| 227 kExprReturn, kArity1 |
| 228 ]); |
| 229 |
| 230 var f = second_module |
| 231 .instantiate({import_module_2: {import_name_2: imported}}) |
| 232 .exports.second_export; |
| 233 var the_export = first_module |
| 234 .instantiate({import_module_1: {import_name_1: f}}) |
| 235 .exports.first_export; |
| 236 assertEquals(the_export(2.8, 9.1), 11); |
| 237 assertEquals(the_export(-1.7, -2.5), -3); |
| 238 assertEquals(the_export(0.0, 0.0), 0); |
| 239 assertEquals(the_export(2, -2), 0); |
| 240 assertEquals(%CheckWasmWrapperElision(the_export, expect_no_elison), true); |
| 241 })(); |
OLD | NEW |