| 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 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 | 9 |
| 10 (function testExportedMain() { | 10 (function testExportedMain() { |
| 11 print("TestExportedMain..."); |
| 11 var kReturnValue = 88; | 12 var kReturnValue = 88; |
| 12 var builder = new WasmModuleBuilder(); | 13 var builder = new WasmModuleBuilder(); |
| 13 | 14 |
| 14 builder.addFunction("main", kSig_i_v) | 15 builder.addFunction("main", kSig_i_v) |
| 15 .addBody([ | 16 .addBody([ |
| 16 kExprI8Const, | 17 kExprI8Const, |
| 17 kReturnValue, | 18 kReturnValue, |
| 18 kExprReturn | 19 kExprReturn |
| 19 ]) | 20 ]) |
| 20 .exportFunc(); | 21 .exportFunc(); |
| 21 | 22 |
| 22 var module = builder.instantiate(); | 23 var module = builder.instantiate(); |
| 23 | 24 |
| 24 assertEquals("object", typeof module.exports); | 25 assertEquals("object", typeof module.exports); |
| 25 assertEquals("function", typeof module.exports.main); | 26 assertEquals("function", typeof module.exports.main); |
| 26 | 27 |
| 27 assertEquals(kReturnValue, module.exports.main()); | 28 assertEquals(kReturnValue, module.exports.main()); |
| 28 })(); | 29 })(); |
| 29 | 30 |
| 30 (function testExportedTwice() { | 31 (function testExportedTwice() { |
| 32 print("TestExportedTwice..."); |
| 31 var kReturnValue = 99; | 33 var kReturnValue = 99; |
| 32 | 34 |
| 33 var builder = new WasmModuleBuilder(); | 35 var builder = new WasmModuleBuilder(); |
| 34 | 36 |
| 35 builder.addFunction("main", kSig_i_v) | 37 builder.addFunction("main", kSig_i_v) |
| 36 .addBody([ | 38 .addBody([ |
| 37 kExprI8Const, | 39 kExprI8Const, |
| 38 kReturnValue, | 40 kReturnValue, |
| 39 kExprReturn | 41 kExprReturn |
| 40 ]) | 42 ]) |
| 41 .exportAs("blah") | 43 .exportAs("blah") |
| 42 .exportAs("foo"); | 44 .exportAs("foo"); |
| 43 | 45 |
| 44 var module = builder.instantiate(); | 46 var module = builder.instantiate(); |
| 45 | 47 |
| 46 assertEquals("object", typeof module.exports); | 48 assertEquals("object", typeof module.exports); |
| 47 assertEquals("function", typeof module.exports.blah); | 49 assertEquals("function", typeof module.exports.blah); |
| 48 assertEquals("function", typeof module.exports.foo); | 50 assertEquals("function", typeof module.exports.foo); |
| 49 | 51 |
| 50 assertEquals(kReturnValue, module.exports.foo()); | 52 assertEquals(kReturnValue, module.exports.foo()); |
| 51 assertEquals(kReturnValue, module.exports.blah()); | 53 assertEquals(kReturnValue, module.exports.blah()); |
| 54 assertSame(module.exports.blah, module.exports.foo); |
| 52 })(); | 55 })(); |
| 53 | 56 |
| 54 | 57 |
| 55 (function testNumericName() { | 58 (function testNumericName() { |
| 59 print("TestNumericName..."); |
| 56 var kReturnValue = 93; | 60 var kReturnValue = 93; |
| 57 | 61 |
| 58 var builder = new WasmModuleBuilder(); | 62 var builder = new WasmModuleBuilder(); |
| 59 | 63 |
| 60 builder.addFunction("main", kSig_i_v) | 64 builder.addFunction("main", kSig_i_v) |
| 61 .addBody([ | 65 .addBody([ |
| 62 kExprI8Const, | 66 kExprI8Const, |
| 63 kReturnValue, | 67 kReturnValue, |
| 64 kExprReturn | 68 kExprReturn |
| 65 ]) | 69 ]) |
| 66 .exportAs("0"); | 70 .exportAs("0"); |
| 67 | 71 |
| 68 var module = builder.instantiate(); | 72 var module = builder.instantiate(); |
| 69 | 73 |
| 70 assertEquals("object", typeof module.exports); | 74 assertEquals("object", typeof module.exports); |
| 71 assertEquals("function", typeof module.exports["0"]); | 75 assertEquals("function", typeof module.exports["0"]); |
| 72 | 76 |
| 73 assertEquals(kReturnValue, module.exports["0"]()); | 77 assertEquals(kReturnValue, module.exports["0"]()); |
| 74 })(); | 78 })(); |
| 75 | 79 |
| 76 (function testExportNameClash() { | 80 (function testExportNameClash() { |
| 81 print("TestExportNameClash..."); |
| 77 var builder = new WasmModuleBuilder(); | 82 var builder = new WasmModuleBuilder(); |
| 78 | 83 |
| 79 builder.addFunction("one", kSig_v_v).addBody([kExprNop]).exportAs("main"); | 84 builder.addFunction("one", kSig_v_v).addBody([kExprNop]).exportAs("main"); |
| 80 builder.addFunction("two", kSig_v_v).addBody([kExprNop]).exportAs("other"); | 85 builder.addFunction("two", kSig_v_v).addBody([kExprNop]).exportAs("other"); |
| 81 builder.addFunction("three", kSig_v_v).addBody([kExprNop]).exportAs("main"); | 86 builder.addFunction("three", kSig_v_v).addBody([kExprNop]).exportAs("main"); |
| 82 | 87 |
| 83 try { | 88 try { |
| 84 builder.instantiate(); | 89 builder.instantiate(); |
| 85 assertUnreachable("should have thrown an exception"); | 90 assertUnreachable("should have thrown an exception"); |
| 86 } catch (e) { | 91 } catch (e) { |
| 87 assertContains("Duplicate export", e.toString()); | 92 assertContains("Duplicate export", e.toString()); |
| 88 } | 93 } |
| 89 })(); | 94 })(); |
| 95 |
| 96 |
| 97 (function testExportMultipleIdentity() { |
| 98 print("TestExportMultipleIdentity..."); |
| 99 var builder = new WasmModuleBuilder(); |
| 100 |
| 101 builder.addFunction("one", kSig_v_v).addBody([kExprNop]) |
| 102 .exportAs("a") |
| 103 .exportAs("b") |
| 104 .exportAs("c"); |
| 105 |
| 106 let instance = builder.instantiate(); |
| 107 let e = instance.exports; |
| 108 assertEquals("function", typeof e.a); |
| 109 assertEquals("function", typeof e.b); |
| 110 assertEquals("function", typeof e.c); |
| 111 assertSame(e.a, e.b); |
| 112 assertSame(e.a, e.c); |
| 113 assertEquals("a", e.a.name); |
| 114 })(); |
| OLD | NEW |