Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 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 | (function testExportedMain() { |
| 11 | var kReturnValue = 88; |
| 12 | var builder = new WasmModuleBuilder(); |
| 13 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 14 | builder.addFunction("main", kSig_i) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 15 | .addBody([ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 16 | kExprI8Const, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 17 | kReturnValue, |
| 18 | kExprReturn, kArity1 |
| 19 | ]) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 20 | .exportFunc(); |
| 21 | |
| 22 | var module = builder.instantiate(); |
| 23 | |
| 24 | assertEquals("object", typeof module.exports); |
| 25 | assertEquals("function", typeof module.exports.main); |
| 26 | |
| 27 | assertEquals(kReturnValue, module.exports.main()); |
| 28 | })(); |
| 29 | |
| 30 | (function testExportedTwice() { |
| 31 | var kReturnValue = 99; |
| 32 | |
| 33 | var builder = new WasmModuleBuilder(); |
| 34 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 35 | builder.addFunction("main", kSig_i) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 36 | .addBody([ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 37 | kExprI8Const, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 38 | kReturnValue, |
| 39 | kExprReturn, kArity1 |
| 40 | ]) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 41 | .exportAs("blah") |
| 42 | .exportAs("foo"); |
| 43 | |
| 44 | var module = builder.instantiate(); |
| 45 | |
| 46 | assertEquals("object", typeof module.exports); |
| 47 | assertEquals("function", typeof module.exports.blah); |
| 48 | assertEquals("function", typeof module.exports.foo); |
| 49 | |
| 50 | assertEquals(kReturnValue, module.exports.foo()); |
| 51 | assertEquals(kReturnValue, module.exports.blah()); |
| 52 | })(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 53 | |
| 54 | |
| 55 | (function testNumericName() { |
| 56 | var kReturnValue = 93; |
| 57 | |
| 58 | var builder = new WasmModuleBuilder(); |
| 59 | |
| 60 | builder.addFunction("main", kSig_i) |
| 61 | .addBody([ |
| 62 | kExprI8Const, |
| 63 | kReturnValue, |
| 64 | kExprReturn, kArity1 |
| 65 | ]) |
| 66 | .exportAs("0"); |
| 67 | |
| 68 | var module = builder.instantiate(); |
| 69 | |
| 70 | assertEquals("object", typeof module.exports); |
| 71 | assertEquals("function", typeof module.exports["0"]); |
| 72 | |
| 73 | assertEquals(kReturnValue, module.exports["0"]()); |
| 74 | })(); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 75 | |
| 76 | (function testExportNameClash() { |
| 77 | var builder = new WasmModuleBuilder(); |
| 78 | |
| 79 | builder.addFunction("one", kSig_v_v).addBody([kExprNop]).exportAs("main"); |
| 80 | builder.addFunction("two", kSig_v_v).addBody([kExprNop]).exportAs("other"); |
| 81 | builder.addFunction("three", kSig_v_v).addBody([kExprNop]).exportAs("main"); |
| 82 | |
| 83 | try { |
| 84 | builder.instantiate(); |
| 85 | assertUnreachable("should have thrown an exception"); |
| 86 | } catch (e) { |
| 87 | assertContains("Duplicate export", e.toString()); |
| 88 | } |
| 89 | })(); |