| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 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 | 9 |
| 9 (function testExportedMain() { | 10 (function testExportedMain() { |
| 10 var kBodySize = 3; | 11 var kReturnValue = 88; |
| 11 var kReturnValue = 99; | 12 var builder = new WasmModuleBuilder(); |
| 12 var kNameMainOffset = kHeaderSize + 4 + 7 + kBodySize + 8 + 1; | |
| 13 | 13 |
| 14 var data = bytesWithHeader( | 14 builder.addFunction("main", [kAstI32]) |
| 15 // signatures | 15 .addBody([ |
| 16 kDeclSignatures, 1, | 16 kExprReturn, |
| 17 0, kAstI32, // void -> i32 | 17 kExprI8Const, |
| 18 // -- main function | 18 kReturnValue]) |
| 19 kDeclFunctions, | 19 .exportFunc(); |
| 20 1, | |
| 21 0, // decl flags | |
| 22 0, 0, // signature index | |
| 23 kBodySize, 0, | |
| 24 // main body | |
| 25 kExprReturn, | |
| 26 kExprI8Const, | |
| 27 kReturnValue, | |
| 28 // exports | |
| 29 kDeclExportTable, | |
| 30 1, | |
| 31 0, 0, // func index index | |
| 32 kNameMainOffset, 0, 0, 0, // function name offset | |
| 33 // names | |
| 34 kDeclEnd, | |
| 35 'm', 'a', 'i', 'n', 0 // -- | |
| 36 ); | |
| 37 | 20 |
| 38 var ffi = new Object(); | 21 var module = builder.instantiate(); |
| 39 var module = _WASMEXP_.instantiateModule(data, ffi); | |
| 40 | 22 |
| 41 assertEquals("object", typeof module.exports); | 23 assertEquals("object", typeof module.exports); |
| 42 assertEquals("function", typeof module.exports.main); | 24 assertEquals("function", typeof module.exports.main); |
| 43 | 25 |
| 44 assertEquals(kReturnValue, module.exports.main()); | 26 assertEquals(kReturnValue, module.exports.main()); |
| 45 })(); | 27 })(); |
| 46 | 28 |
| 47 (function testExportedTwice() { | 29 (function testExportedTwice() { |
| 48 var kBodySize = 3; | |
| 49 var kReturnValue = 99; | 30 var kReturnValue = 99; |
| 50 var kNameMainOffset = kHeaderSize + 4 + 7 + kBodySize + 14 + 1; | |
| 51 var kNameFooOffset = kNameMainOffset + 5; | |
| 52 | 31 |
| 53 var data = bytesWithHeader( | 32 var builder = new WasmModuleBuilder(); |
| 54 // signatures | |
| 55 kDeclSignatures, 1, | |
| 56 0, kAstI32, // void -> i32 | |
| 57 // -- main function | |
| 58 kDeclFunctions, | |
| 59 1, | |
| 60 0, // decl flags | |
| 61 0, 0, // signature index | |
| 62 kBodySize, 0, | |
| 63 // main body | |
| 64 kExprReturn, | |
| 65 kExprI8Const, | |
| 66 kReturnValue, | |
| 67 // exports | |
| 68 kDeclExportTable, | |
| 69 2, | |
| 70 0, 0, // func index index | |
| 71 kNameMainOffset, 0, 0, 0, // function name offset | |
| 72 0, 0, // func index index | |
| 73 kNameFooOffset, 0, 0, 0, // function name offset | |
| 74 // names | |
| 75 kDeclEnd, | |
| 76 'b', 'l', 'a', 'h', 0, // -- | |
| 77 'f', 'o', 'o', 0 // -- | |
| 78 ); | |
| 79 | 33 |
| 80 var ffi = new Object(); | 34 builder.addFunction("main", [kAstI32]) |
| 81 var module = _WASMEXP_.instantiateModule(data, ffi); | 35 .addBody([ |
| 36 kExprReturn, |
| 37 kExprI8Const, |
| 38 kReturnValue]) |
| 39 .exportAs("blah") |
| 40 .exportAs("foo"); |
| 41 |
| 42 var module = builder.instantiate(); |
| 82 | 43 |
| 83 assertEquals("object", typeof module.exports); | 44 assertEquals("object", typeof module.exports); |
| 84 assertEquals("function", typeof module.exports.blah); | 45 assertEquals("function", typeof module.exports.blah); |
| 85 assertEquals("function", typeof module.exports.foo); | 46 assertEquals("function", typeof module.exports.foo); |
| 86 | 47 |
| 48 assertEquals(kReturnValue, module.exports.foo()); |
| 87 assertEquals(kReturnValue, module.exports.blah()); | 49 assertEquals(kReturnValue, module.exports.blah()); |
| 88 assertEquals(kReturnValue, module.exports.foo()); | |
| 89 })(); | 50 })(); |
| OLD | NEW |