export-table.js 4.31 KB
Newer Older
titzer's avatar
titzer committed
1 2 3 4 5 6
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --expose-wasm

7
load("test/mjsunit/wasm/wasm-module-builder.js");
titzer's avatar
titzer committed
8 9

(function testExportedMain() {
10
  print("TestExportedMain...");
11
  var kReturnValue = 44;
12
  var builder = new WasmModuleBuilder();
titzer's avatar
titzer committed
13

14
  builder.addFunction("main", kSig_i_v)
15
    .addBody([
16
      kExprI32Const,
17
      kReturnValue,
18
      kExprReturn
19
    ])
20
    .exportFunc();
titzer's avatar
titzer committed
21

22
  var module = builder.instantiate();
titzer's avatar
titzer committed
23 24 25 26 27 28 29 30

  assertEquals("object", typeof module.exports);
  assertEquals("function", typeof module.exports.main);

  assertEquals(kReturnValue, module.exports.main());
})();

(function testExportedTwice() {
31
  print("TestExportedTwice...");
32
  var kReturnValue = 45;
titzer's avatar
titzer committed
33

34 35
  var builder = new WasmModuleBuilder();

36
  builder.addFunction("main", kSig_i_v)
37
    .addBody([
38
      kExprI32Const,
39
      kReturnValue,
40
      kExprReturn
41
    ])
42 43
    .exportAs("blah")
    .exportAs("foo");
titzer's avatar
titzer committed
44

45
  var module = builder.instantiate();
titzer's avatar
titzer committed
46 47 48 49 50 51

  assertEquals("object", typeof module.exports);
  assertEquals("function", typeof module.exports.blah);
  assertEquals("function", typeof module.exports.foo);

  assertEquals(kReturnValue, module.exports.foo());
52
  assertEquals(kReturnValue, module.exports.blah());
53
  assertSame(module.exports.blah, module.exports.foo);
titzer's avatar
titzer committed
54
})();
55

56 57
(function testEmptyName() {
  print("TestEmptyName...");
58
  var kReturnValue = 46;
59 60 61 62 63

  var builder = new WasmModuleBuilder();

  builder.addFunction("main", kSig_i_v)
    .addBody([
64
      kExprI32Const,
65 66 67 68 69 70 71 72 73 74 75 76
      kReturnValue,
      kExprReturn
    ])
    .exportAs("");

  var module = builder.instantiate();

  assertEquals("object", typeof module.exports);
  assertEquals("function", typeof module.exports[""]);

  assertEquals(kReturnValue, module.exports[""]());
})();
77 78

(function testNumericName() {
79
  print("TestNumericName...");
80
  var kReturnValue = 47;
81 82 83

  var builder = new WasmModuleBuilder();

84
  builder.addFunction("main", kSig_i_v)
85
    .addBody([
86
      kExprI32Const,
87
      kReturnValue,
88
      kExprReturn
89 90 91 92 93 94 95 96 97 98
    ])
    .exportAs("0");

  var module = builder.instantiate();

  assertEquals("object", typeof module.exports);
  assertEquals("function", typeof module.exports["0"]);

  assertEquals(kReturnValue, module.exports["0"]());
})();
99 100

(function testExportNameClash() {
101
  print("TestExportNameClash...");
102 103 104 105 106 107
  var builder = new WasmModuleBuilder();

  builder.addFunction("one",   kSig_v_v).addBody([kExprNop]).exportAs("main");
  builder.addFunction("two",   kSig_v_v).addBody([kExprNop]).exportAs("other");
  builder.addFunction("three", kSig_v_v).addBody([kExprNop]).exportAs("main");

108 109
  assertThrows(() => builder.instantiate(), WebAssembly.CompileError,
    /Duplicate export name 'main' for function 0 and function 2/);
110
})();
111 112 113 114 115 116


(function testExportMultipleIdentity() {
  print("TestExportMultipleIdentity...");
  var builder = new WasmModuleBuilder();

117
  var f = builder.addFunction("one", kSig_v_v).addBody([kExprNop])
118 119 120 121 122 123 124 125 126 127 128
    .exportAs("a")
    .exportAs("b")
    .exportAs("c");

  let instance = builder.instantiate();
  let e = instance.exports;
  assertEquals("function", typeof e.a);
  assertEquals("function", typeof e.b);
  assertEquals("function", typeof e.c);
  assertSame(e.a, e.b);
  assertSame(e.a, e.c);
129
  assertEquals(String(f.index), e.a.name);
130
})();
131 132 133 134 135 136 137 138


(function testReexportJSMultipleIdentity() {
  print("TestReexportMultipleIdentity...");
  var builder = new WasmModuleBuilder();

  function js() {}

139
  var a = builder.addImport("m", "a", kSig_v_v);
140 141 142
  builder.addExport("f", a);
  builder.addExport("g", a);

143
  let instance = builder.instantiate({m: {a: js}});
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  let e = instance.exports;
  assertEquals("function", typeof e.f);
  assertEquals("function", typeof e.g);
  assertFalse(e.f == js);
  assertFalse(e.g == js);
  assertTrue(e.f == e.g);
})();


(function testReexportJSMultiple() {
  print("TestReexportMultiple...");
  var builder = new WasmModuleBuilder();

  function js() {}

159 160
  var a = builder.addImport("q", "a", kSig_v_v);
  var b = builder.addImport("q", "b", kSig_v_v);
161 162 163
  builder.addExport("f", a);
  builder.addExport("g", b);

164
  let instance = builder.instantiate({q: {a: js, b: js}});
165 166 167 168 169 170 171
  let e = instance.exports;
  assertEquals("function", typeof e.f);
  assertEquals("function", typeof e.g);
  assertFalse(e.f == js);
  assertFalse(e.g == js);
  assertFalse(e.f == e.g);
})();