indirect-tables.js 27.9 KB
Newer Older
1 2 3 4
// 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.

5
// Flags: --expose-wasm --expose-gc
6 7 8 9 10 11 12

load("test/mjsunit/wasm/wasm-module-builder.js");

function AddFunctions(builder) {
  let sig_index = builder.addType(kSig_i_ii);
  let mul = builder.addFunction("mul", sig_index)
    .addBody([
13 14
      kExprLocalGet, 0,  // --
      kExprLocalGet, 1,  // --
15 16 17 18
      kExprI32Mul        // --
    ]);
  let add = builder.addFunction("add", sig_index)
    .addBody([
19 20
      kExprLocalGet, 0,  // --
      kExprLocalGet, 1,  // --
21 22 23 24
      kExprI32Add        // --
    ]);
  let sub = builder.addFunction("sub", sig_index)
    .addBody([
25 26
      kExprLocalGet, 0,  // --
      kExprLocalGet, 1,  // --
27 28 29 30 31
      kExprI32Sub        // --
    ]);
  return {mul: mul, add: add, sub: sub};
}

32 33
function js_div(a, b) { return (a / b) | 0; }

34
(function ExportedTableTest() {
35
  print(arguments.callee.name);
36 37 38

  let builder = new WasmModuleBuilder();

39
  let d = builder.addImport("q", "js_div", kSig_i_ii);
40 41 42 43
  let f = AddFunctions(builder);
  builder.addFunction("main", kSig_i_ii)
    .addBody([
      kExprI32Const, 33,  // --
44 45
      kExprLocalGet, 0,   // --
      kExprLocalGet, 1,   // --
46
      kExprCallIndirect, 0, kTableZero])  // --
47 48 49 50
    .exportAs("main");

  f.add.exportAs("blarg");

51
  builder.setTableBounds(10, 10);
52
  let g = builder.addImportedGlobal("q", "base", kWasmI32);
53
  builder.addElementSegment(
54
      0, g, true, [f.mul.index, f.add.index, f.sub.index, d]);
55 56 57 58 59 60
  builder.addExportOfKind("table", kExternalTable, 0);

  let module = new WebAssembly.Module(builder.toBuffer());

  for (let i = 0; i < 5; i++) {
    print(" base = " + i);
61
    let instance = new WebAssembly.Instance(module, {q: {base: i, js_div: js_div}});
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    main = instance.exports.main;
    let table = instance.exports.table;
    assertTrue(table instanceof WebAssembly.Table);
    assertEquals(10, table.length);
    for (let j = 0; j < i; j++) {
      assertSame(null, table.get(j));
    }
    let mul = table.get(i+0);
    let add = table.get(i+1);
    let sub = table.get(i+2);

    print("  mul=" + mul);
    print("  add=" + add);
    print("  sub=" + sub);
    assertEquals("function", typeof mul);
    assertEquals("function", typeof add);
    assertEquals("function", typeof sub);
    assertEquals(2, mul.length);
    assertEquals(2, add.length);
    assertEquals(2, sub.length);
82
    assertEquals(String(f.add.index), add.name);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    let exp_div = table.get(i+3);
    assertEquals("function", typeof exp_div);
    print("  js_div=" + exp_div);
    // Should have a new, wrapped version of the import.
    assertFalse(js_div == exp_div);


    for (let j = i + 4; j < 10; j++) {
      assertSame(null, table.get(j));
    }

    assertEquals(-33, mul(-11, 3));
    assertEquals(4444444, add(3333333, 1111111));
    assertEquals(-9999, sub(1, 10000));
    assertEquals(-44, exp_div(-88.1, 2));
  }
})();
101 102


103
(function ImportedTableTest1() {
104
  let kTableSize = 10;
105
  print(arguments.callee.name);
106 107
  var builder = new WasmModuleBuilder();

108
  let d = builder.addImport("q", "js_div", kSig_i_ii);
109
  let f = AddFunctions(builder);
110
  builder.setTableBounds(kTableSize, kTableSize);
111
  let g = builder.addImportedGlobal("q", "base", kWasmI32);
112
  builder.addElementSegment(
113
      0, g, true, [f.mul.index, f.add.index, f.sub.index, d]);
114 115 116 117 118 119
  builder.addExportOfKind("table", kExternalTable, 0);

  let m1 = new WebAssembly.Module(builder.toBuffer());

  var builder = new WasmModuleBuilder();

120
  builder.addImportedTable("r", "table", kTableSize, kTableSize);
121 122 123
  builder.addFunction("main", kSig_i_ii)
    .addBody([
      kExprI32Const, 33,  // --
124 125
      kExprLocalGet, 0,   // --
      kExprLocalGet, 1,   // --
126 127 128 129 130 131 132 133
      kExprCallIndirect, 0, kTableZero])  // --
    .exportAs("main");

  let m2 = new WebAssembly.Module(builder.toBuffer());

  // Run 5 trials at different table bases.
  for (let i = 0; i < 5; i++) {
    print(" base = " + i);
134
    let i1 = new WebAssembly.Instance(m1, {q: {base: i, js_div: js_div}});
135 136
    let table = i1.exports.table;
    assertEquals(10, table.length);
137
    let i2 = new WebAssembly.Instance(m2, {r: {table: table}});
138 139 140
    let main = i2.exports.main;

    for (var j = 0; j < i; j++) {
141
      assertTraps(kTrapFuncSigMismatch, () => main(0, j));
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
      assertSame(null, table.get(j));
    }

    // mul
    assertEquals("function", typeof table.get(i+0));
    assertEquals(0, main(0, i+0));
    assertEquals(66, main(2, i+0));

    // add
    assertEquals("function", typeof table.get(i+1));
    assertEquals(33, main(0, i+1));
    assertEquals(38, main(5, i+1));

    // sub
    assertEquals("function", typeof table.get(i+2));
    assertEquals(32, main(1, i+2));
    assertEquals(28, main(5, i+2));

    // div
    assertEquals("function", typeof table.get(i+3));
    assertEquals(8, main(4, i+3));
    assertEquals(3, main(11, i+3));

    for (var j = i + 4; j < (kTableSize + 5); j++) {
      assertThrows(x => main(0, j));
      if (j < kTableSize) assertSame(null, table.get(j));
    }
  }
})();

172
(function ImportedTableTest2() {
173
  let kTableSize = 10;
174
  print(arguments.callee.name);
175 176 177

  var builder = new WasmModuleBuilder();

178 179
  let d = builder.addImport("q", "js_div", kSig_i_ii);
  builder.addImportedTable("q", "table", kTableSize, kTableSize);
180
  let g = builder.addImportedGlobal("q", "base", kWasmI32);
181
  let f = AddFunctions(builder);
182
  builder.addElementSegment(
183
      0, g, true, [f.mul.index, f.add.index, f.sub.index, d]);
184 185 186
  builder.addFunction("main", kSig_i_ii)
    .addBody([
      kExprI32Const, 55,  // --
187 188
      kExprLocalGet, 0,   // --
      kExprLocalGet, 1,   // --
189 190 191 192 193 194 195 196 197
      kExprCallIndirect, 0, kTableZero])  // --
    .exportAs("main");

  let m2 = new WebAssembly.Module(builder.toBuffer());

  // Run 5 trials at different table bases.
  for (let i = 0; i < 5; i++) {
    print(" base = " + i);
    let table = new WebAssembly.Table({element: "anyfunc",
198 199
                                       initial: kTableSize,
                                       maximum: kTableSize});
200
    assertEquals(10, table.length);
201 202
    let i2 = new WebAssembly.Instance(m2, {q: {base: i, table: table,
                                               js_div: js_div}});
203 204 205
    let main = i2.exports.main;

    for (var j = 0; j < i; j++) {
206
      assertTraps(kTrapFuncSigMismatch, () => main(0, j));
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
      assertSame(null, table.get(j));
    }

    // mul
    assertEquals("function", typeof table.get(i+0));
    assertEquals(0, main(0, i+0));
    assertEquals(110, main(2, i+0));

    // add
    assertEquals("function", typeof table.get(i+1));
    assertEquals(55, main(0, i+1));
    assertEquals(60, main(5, i+1));

    // sub
    assertEquals("function", typeof table.get(i+2));
    assertEquals(54, main(1, i+2));
    assertEquals(50, main(5, i+2));

    // div
    assertEquals("function", typeof table.get(i+3));
    assertEquals(13, main(4, i+3));
    assertEquals(5, main(11, i+3));

    for (var j = i + 4; j < (kTableSize + 5); j++) {
      assertThrows(x => main(0, j));
      if (j < kTableSize) assertSame(null, table.get(j));
    }
  }
})();


(function CumulativeTest() {
239
  print(arguments.callee.name);
240 241

  let kTableSize = 10;
242 243
  let table = new WebAssembly.Table(
    {element: "anyfunc", initial: kTableSize, maximum: kTableSize});
244 245 246

  var builder = new WasmModuleBuilder();

247
  builder.addImportedTable("x", "table", kTableSize, kTableSize);
248
  let g = builder.addImportedGlobal("x", "base", kWasmI32);
249
  let sig_index = builder.addType(kSig_i_v);
250
  let f = builder.addFunction("f", sig_index)
251
    .addBody([
252
      kExprGlobalGet, g
253 254 255
    ]);
  builder.addFunction("main", kSig_i_ii)
    .addBody([
256
      kExprLocalGet, 0,
257 258
      kExprCallIndirect, sig_index, kTableZero])  // --
    .exportAs("main");
259
  builder.addElementSegment(0, g, true, [f.index]);
260 261 262 263 264

  let module = new WebAssembly.Module(builder.toBuffer());

  for (var i = 0; i < kTableSize; i++) {
    print(" base = " + i);
265
    let instance = new WebAssembly.Instance(module, {x: {base: i, table: table}});
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

    for (var j = 0; j < kTableSize; j++) {
      let func = table.get(j);
      if (j > i) {
        assertSame(null, func);
        assertTraps(kTrapFuncSigMismatch, () => instance.exports.main(j));
      } else {
        assertEquals("function", typeof func);
        assertEquals(j, func());
        assertEquals(j, instance.exports.main(j));
      }
    }
  }
})();

(function TwoWayTest() {
282
  print(arguments.callee.name);
283 284 285 286 287 288 289 290 291 292 293 294
  let kTableSize = 3;

  // Module {m1} defines the table and exports it.
  var builder = new WasmModuleBuilder();
  builder.addType(kSig_i_i);
  builder.addType(kSig_i_ii);
  var sig_index1 = builder.addType(kSig_i_v);
  var f1 = builder.addFunction("f1", sig_index1)
    .addBody([kExprI32Const, 11]);

  builder.addFunction("main", kSig_i_ii)
    .addBody([
295
      kExprLocalGet, 0,   // --
296 297 298
      kExprCallIndirect, sig_index1, kTableZero])  // --
    .exportAs("main");

299
  builder.setTableBounds(kTableSize, kTableSize);
300
  builder.addElementSegment(0, 0, false, [f1.index]);
301 302 303 304 305 306 307 308 309 310 311 312 313
  builder.addExportOfKind("table", kExternalTable, 0);

  var m1 = new WebAssembly.Module(builder.toBuffer());

  // Module {m2} imports the table and adds {f2}.
  var builder = new WasmModuleBuilder();
  builder.addType(kSig_i_ii);
  var sig_index2 = builder.addType(kSig_i_v);
  var f2 = builder.addFunction("f2", sig_index2)
    .addBody([kExprI32Const, 22]);

  builder.addFunction("main", kSig_i_ii)
    .addBody([
314
      kExprLocalGet, 0,   // --
315 316 317
      kExprCallIndirect, sig_index2, kTableZero])  // --
    .exportAs("main");

318
  builder.addImportedTable("z", "table", kTableSize, kTableSize);
319
  builder.addElementSegment(0, 1, false, [f2.index]);
320 321 322 323 324 325

  var m2 = new WebAssembly.Module(builder.toBuffer());

  assertFalse(sig_index1 == sig_index2);

  var i1 = new WebAssembly.Instance(m1);
326
  var i2 = new WebAssembly.Instance(m2, {z: {table: i1.exports.table}});
327 328 329 330 331 332 333

  assertEquals(11, i1.exports.main(0));
  assertEquals(11, i2.exports.main(0));

  assertEquals(22, i1.exports.main(1));
  assertEquals(22, i2.exports.main(1));

334 335 336 337
  assertTraps(kTrapFuncSigMismatch, () => i1.exports.main(2));
  assertTraps(kTrapFuncSigMismatch, () => i2.exports.main(2));
  assertTraps(kTrapFuncInvalid, () => i1.exports.main(3));
  assertTraps(kTrapFuncInvalid, () => i2.exports.main(3));
338 339 340
})();

(function MismatchedTableSize() {
341
  print(arguments.callee.name);
342 343 344 345 346 347
  let kTableSize = 5;

  for (var expsize = 1; expsize < 4; expsize++) {
    for (var impsize = 1; impsize < 4; impsize++) {
      print(" expsize = " + expsize + ", impsize = " + impsize);
      var builder = new WasmModuleBuilder();
348
      builder.setTableBounds(expsize, expsize);
349 350 351 352 353
      builder.addExportOfKind("expfoo", kExternalTable, 0);

      let m1 = new WebAssembly.Module(builder.toBuffer());

      var builder = new WasmModuleBuilder();
354
      builder.addImportedTable("y", "impfoo", impsize, impsize);
355 356 357 358 359 360 361

      let m2 = new WebAssembly.Module(builder.toBuffer());

      var i1 = new WebAssembly.Instance(m1);

      // TODO(titzer): v8 currently requires import table size to match
      // export table size.
362
      var ffi = {y: {impfoo: i1.exports.expfoo}};
363 364 365 366 367 368 369
      if (expsize == impsize) {
        var i2 = new WebAssembly.Instance(m2, ffi);
      } else {
        assertThrows(() => new WebAssembly.Instance(m2, ffi));
      }
    }
  }
370
})();
371

372
(function TableGrowBoundsCheck() {
373
  print(arguments.callee.name);
374 375 376 377 378 379 380 381 382 383 384 385
  var kMaxSize = 30, kInitSize = 5;
  let table = new WebAssembly.Table({element: "anyfunc",
    initial: kInitSize, maximum: kMaxSize});
  var builder = new WasmModuleBuilder();
  builder.addImportedTable("x", "table", kInitSize, kMaxSize);
  let module = new WebAssembly.Module(builder.toBuffer());
  let instance = new WebAssembly.Instance(module, {x: {base: 1, table: table}});

  for(var i = kInitSize; i < kMaxSize; i+=5) {
    assertEquals(i, table.length);
    for (var j = 0; j < i; j++) table.set(j, null);
    for (var j = 0; j < i; j++) assertEquals(null, table.get(j));
386 387
    assertThrows(() => table.set(i, null), RangeError);
    assertThrows(() => table.get(i), RangeError);
388 389 390
    assertEquals(i, table.grow(5));
  }
  assertEquals(30, table.length);
391 392 393
  assertThrows(() => table.grow(1), RangeError);
  assertThrows(() => table.set(kMaxSize, null), RangeError);
  assertThrows(() => table.get(kMaxSize), RangeError);
394
})();
395

396
(function CumulativeGrowTest() {
397
  print(arguments.callee.name);
398 399 400 401
  let table = new WebAssembly.Table({
    element: "anyfunc", initial: 10, maximum: 30});
  var builder = new WasmModuleBuilder();
  builder.addImportedTable("x", "table", 10, 30);
402

403 404 405 406
  let g = builder.addImportedGlobal("x", "base", kWasmI32);
  let sig_index = builder.addType(kSig_i_v);
  builder.addFunction("g", sig_index)
    .addBody([
407
      kExprGlobalGet, g
408 409 410
    ]);
  builder.addFunction("main", kSig_i_ii)
    .addBody([
411
      kExprLocalGet, 0,
412 413
      kExprCallIndirect, sig_index, kTableZero])  // --
    .exportAs("main");
414
  builder.addElementSegment(0, g, true, [g]);
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
  let module = new WebAssembly.Module(builder.toBuffer());

  var instances = [];
  for (var i = 0; i < 10; i++) {
    print(" base = " + i);
    instances.push(new WebAssembly.Instance(
        module, {x: {base: i, table: table}}));
  }

  for (var j = 0; j < 10; j++) {
    let func = table.get(j);
    assertEquals("function", typeof func);
    assertEquals(j, func());
    assertEquals(j, instances[j].exports.main(j));
  }

  assertEquals(10, table.grow(10));

  // Verify that grow does not alter function behaviors
  for (var j = 0; j < 10; j++) {
    let func = table.get(j);
    assertEquals("function", typeof func);
    assertEquals(j, func());
    assertEquals(j, instances[j].exports.main(j));
  }

  let new_builder = new WasmModuleBuilder();
442
  new_builder.addExport("wasm", new_builder.addFunction("", kSig_v_v).addBody([]));
443 444 445 446 447 448 449 450 451 452 453
  new_builder.addImportedTable("x", "table", 20, 30);
  let new_module = new WebAssembly.Module(new_builder.toBuffer());
  let instance = new WebAssembly.Instance(new_module, {x: {table: table}});
  let new_func = instance.exports.wasm;

  for (var j = 10; j < 20; j++) {
    table.set(j, new_func);
    let func = table.get(j);
    assertEquals("function", typeof func);
    assertSame(new_func, table.get(j));
  }
454
  assertThrows(() => table.grow(11), RangeError);
455
})();
456 457 458


(function TestImportTooLarge() {
459
  print(arguments.callee.name);
460 461 462 463 464
  let builder = new WasmModuleBuilder();
  builder.addImportedTable("t", "t", 1, 2);

  // initial size is too large
  assertThrows(() => builder.instantiate({t: {t: new WebAssembly.Table(
465
    {element: "anyfunc", initial: 3, maximum: 3})}}), WebAssembly.LinkError);
466 467 468

  // maximum size is too large
  assertThrows(() => builder.instantiate({t: {t: new WebAssembly.Table(
469
    {element: "anyfunc", initial: 1, maximum: 4})}}), WebAssembly.LinkError);
470 471 472

  // no maximum
  assertThrows(() => builder.instantiate({t: {t: new WebAssembly.Table(
473
    {element: "anyfunc", initial: 1})}}), WebAssembly.LinkError);
474
})();
475 476

(function TableImportLargerThanCompiled() {
477
  print(arguments.callee.name);
478 479 480 481 482 483 484 485 486
  var kMaxSize = 30, kInitSize = 5;
  var builder = new WasmModuleBuilder();
  builder.addImportedTable("x", "table", 1, 35);
  let table = new WebAssembly.Table({element: "anyfunc",
    initial: kInitSize, maximum: kMaxSize});
  let module = new WebAssembly.Module(builder.toBuffer());
  let instance = new WebAssembly.Instance(module, {x: {base: 1, table: table}});
  for (var i = 0; i < kInitSize; ++i) table.set(i, null);
  for (var i = 0; i < kInitSize; ++i) assertEquals(null, table.get(i));
487
  assertThrows(() => table.set(kInitSize, null), RangeError);
488 489 490
})();

(function ModulesShareTableAndGrow() {
491
  print(arguments.callee.name);
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
  let module1 = (() => {
    let builder = new WasmModuleBuilder();
    builder.addImportedTable("x", "table", 1, 35);
    return new WebAssembly.Module(builder.toBuffer());
  })();
  let module2 = (() => {
    let builder = new WasmModuleBuilder();
    builder.addImportedTable("x", "table", 2, 40);
    return new WebAssembly.Module(builder.toBuffer());
  })();

  var kMaxSize = 30, kInitSize = 5;
  let table = new WebAssembly.Table({element: "anyfunc",
    initial: kInitSize, maximum: kMaxSize});
  let instance1 = new WebAssembly.Instance(
      module1, {x: {base: 1, table: table}});
  let instance2 = new WebAssembly.Instance(
      module2, {x: {base: 1, table: table}});

  for (var i = 0; i < kInitSize; ++i) table.set(i, null);
  for (var i = 0; i < kInitSize; ++i) assertEquals(null, table.get(i));
513
  assertThrows(() => table.set(kInitSize, null), RangeError);
514 515 516
  assertEquals(kInitSize, table.grow(5));
  for (var i = 0; i < 2*kInitSize; ++i) table.set(i, null);
  for (var i = 0; i < 2*kInitSize; ++i) assertEquals(null, table.get(i));
517
  assertThrows(() => table.set(2*kInitSize, null), RangeError);
518
  // Try to grow past imported maximum
519
  assertThrows(() => table.grow(21), RangeError);
520
})();
521 522 523

(function MultipleElementSegments() {
  let kTableSize = 10;
524
  print(arguments.callee.name);
525 526 527 528 529 530 531 532 533 534 535 536

  let mul = (a, b) => a * b;
  let add = (a, b) => a + b;
  let sub = (a, b) => a - b;

  // Test 1 to 3 segments in the elements section.
  // segment 1 sets [1, 2] to mul,
  // segment 2 sets [2, 3, 4] to add,
  // segment 3 sets [3, 4, 5, 6] to sub.
  for (let num_segments = 1; num_segments < 4; ++num_segments) {
    var builder = new WasmModuleBuilder();

537
    builder.setTableBounds(kTableSize, kTableSize);
538 539 540 541 542 543 544
    builder.addExportOfKind("table", kExternalTable, 0);
    let f = AddFunctions(builder);
    let indexes = [f.mul.index, f.add.index, f.sub.index];
    for (let i = 0; i < num_segments; ++i) {
      let offset = i + 1;
      let len = i + 2;
      let index = indexes[i];
545
      builder.addElementSegment(0, offset, false, new Array(len).fill(index));
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    }

    let instance = builder.instantiate();

    let table = instance.exports.table;
    assertEquals(kTableSize, table.length);

    for (let i = 0; i < num_segments; ++i) {
      let exp = i < 1 || i > 2 ? null : mul;
      if (num_segments > 1 && i >= 2 && i <= 4) exp = add;
      if (num_segments > 2 && i >= 3 && i <= 6) exp = sub;
      if (!exp) {
        assertSame(null, table.get(i));
      } else {
        assertEquals("function", typeof table.get(i));
    assertEquals(exp(6, 3), table.get(i)(6, 3));
      }
    }
  }
})();
566 567 568 569 570 571 572 573 574

(function InitImportedTableSignatureMismatch() {
  // instance0 exports a function table and a main function which indirectly
  // calls a function from the table.
  let builder0 = new WasmModuleBuilder();
  builder0.setName('module_0');
  let sig_index = builder0.addType(kSig_i_v);
  builder0.addFunction('main', kSig_i_i)
      .addBody([
575
        kExprLocalGet, 0,  // -
576 577 578
        kExprCallIndirect, sig_index, kTableZero
      ])
      .exportAs('main');
579
  builder0.setTableBounds(3, 3);
580 581 582 583 584 585 586
  builder0.addExportOfKind('table', kExternalTable);
  let module0 = new WebAssembly.Module(builder0.toBuffer());
  let instance0 = new WebAssembly.Instance(module0);

  // instance1 imports the table and adds a function to it.
  let builder1 = new WasmModuleBuilder();
  builder1.setName('module_1');
587
  builder1.addFunction('f', kSig_i_i).addBody([kExprLocalGet, 0]);
588
  builder1.addImportedTable('z', 'table');
589
  builder1.addElementSegment(0, 0, false, [0]);
590 591 592 593 594 595 596 597 598 599
  let module1 = new WebAssembly.Module(builder1.toBuffer());
  let instance1 =
      new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}});

  // Calling the main method on instance0 should fail, because the signature of
  // the added function does not match.
  assertThrows(
      () => instance0.exports.main(0), WebAssembly.RuntimeError,
      /signature mismatch/);
})();
600 601

(function IndirectCallIntoOtherInstance() {
602 603
  print(arguments.callee.name);

604 605 606 607 608 609 610 611 612 613
  var mem_1 = new WebAssembly.Memory({initial: 1});
  var mem_2 = new WebAssembly.Memory({initial: 1});
  var view_1 = new Int32Array(mem_1.buffer);
  var view_2 = new Int32Array(mem_2.buffer);
  view_1[0] = 1;
  view_2[0] = 1000;

  let builder = new WasmModuleBuilder();
  let sig = builder.addType(kSig_i_v);
  builder.addFunction('main', kSig_i_i)
614
    .addBody([kExprLocalGet, 0, kExprCallIndirect, sig, kTableZero])
615 616 617
    .exportAs('main');
  builder.addImportedMemory('', 'memory', 1);

618
  builder.setTableBounds(1, 1);
619 620 621 622 623 624 625 626
  builder.addExportOfKind('table', kExternalTable);

  let module1 = new WebAssembly.Module(builder.toBuffer());
  let instance1 = new WebAssembly.Instance(module1, {'':{memory:mem_1}});

  builder = new WasmModuleBuilder();
  builder.addFunction('main', kSig_i_v).addBody([kExprI32Const, 0, kExprI32LoadMem, 0, 0]);
  builder.addImportedTable('', 'table');
627
  builder.addElementSegment(0, 0, false, [0]);
628 629 630 631 632 633 634 635 636 637 638 639 640
  builder.addImportedMemory('', 'memory', 1);


  let module2 = new WebAssembly.Module(builder.toBuffer());
  let instance2 = new WebAssembly.Instance(module2, {
    '': {
      table: instance1.exports.table,
      memory: mem_2
    }
  });

  assertEquals(instance1.exports.main(0), 1000);
})();
641 642 643


(function ImportedFreestandingTable() {
644
  print(arguments.callee.name);
645 646 647 648 649 650 651 652 653 654 655

  function forceGc() {
    gc();
    gc();
    gc();
  }

  function setup() {
    let builder = new WasmModuleBuilder();
    let sig = builder.addType(kSig_i_v);
    builder.addFunction('main', kSig_i_i)
656
      .addBody([kExprLocalGet, 0, kExprCallIndirect, sig, kTableZero])
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
      .exportAs('main');

    builder.addImportedTable('', 'table');

    let module1 = new WebAssembly.Module(builder.toBuffer());
    let table = new WebAssembly.Table({initial:2, element:'anyfunc'});
    let instance1 = new WebAssembly.Instance(module1, {'':{table: table}});

    builder = new WasmModuleBuilder();
    builder.addExport('theImport', builder.addImport('', 'callout', kSig_i_v));
    builder.addImportedMemory('', 'memory', 1);
    builder.addFunction('main', kSig_i_v)
      .addBody([
        kExprCallFunction, 0,
        kExprI32Const, 0, kExprI32LoadMem, 0, 0,
        kExprI32Add
      ]).exportAs('main');

    let mem = new WebAssembly.Memory({initial:1});
    let view = new Int32Array(mem.buffer);
    view[0] = 4;

    let module2 = new WebAssembly.Module(builder.toBuffer());
    let instance2 = new WebAssembly.Instance(module2, {
      '': {
        callout: () => {
          forceGc();
          return 3;
        },
        'memory': mem
      }
    });
    table.set(0, instance2.exports.main);
    table.set(1, instance2.exports.theImport);
    return instance1;
  }

  function test(variant, expectation) {
    var instance = setup();
    forceGc();
    assertEquals(expectation, instance.exports.main(variant));
  }

  // 0 indirectly calls the wasm function that calls the import,
  // 1 does the same but for the exported import.
  test(0, 7);
  test(1, 3);
})();
705 706


707 708 709 710 711 712 713
(function ImportedWasmFunctionPutIntoTable() {
  print(arguments.callee.name);

  let wasm_mul = (() => {
    let builder = new WasmModuleBuilder();
    builder.addFunction("mul", kSig_i_ii)
      .addBody(
714 715
        [kExprLocalGet, 0,
         kExprLocalGet, 1,
716 717 718 719 720 721 722 723 724 725 726 727
         kExprI32Mul])
      .exportFunc();
    return builder.instantiate().exports.mul;
  })();

  let builder = new WasmModuleBuilder();

  let j = builder.addImport("q", "js_div", kSig_i_ii);
  let w = builder.addImport("q", "wasm_mul", kSig_i_ii);
  builder.addFunction("main", kSig_i_ii)
    .addBody([
      kExprI32Const, 33,  // --
728 729
      kExprLocalGet, 0,   // --
      kExprLocalGet, 1,   // --
730 731 732
      kExprCallIndirect, 0, kTableZero])  // --
    .exportAs("main");

733
  builder.setTableBounds(10, 10);
734
  let g = builder.addImportedGlobal("q", "base", kWasmI32);
735
  builder.addElementSegment(0, g, true, [j, w]);
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758

  let module = new WebAssembly.Module(builder.toBuffer());
  for (var i = 0; i < 5; i++) {
    let instance = new WebAssembly.Instance(module, {q: {base: i, js_div: js_div, wasm_mul: wasm_mul}});
    let j = i + 1;

    assertThrows(() => {instance.exports.main(j, i-1)});
    assertEquals((33/j)|0, instance.exports.main(j, i+0));
    assertEquals((33*j)|0, instance.exports.main(j, i+1));
    assertThrows(() => {instance.exports.main(j, i+2)});
  }

})();

(function ImportedWasmFunctionPutIntoImportedTable() {
  print(arguments.callee.name);

  let kTableSize = 10;

  let wasm_mul = (() => {
    let builder = new WasmModuleBuilder();
    builder.addFunction("mul", kSig_i_ii)
      .addBody(
759 760
        [kExprLocalGet, 0,
         kExprLocalGet, 1,
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
         kExprI32Mul])
      .exportFunc();
    return builder.instantiate().exports.mul;
  })();

  let table = new WebAssembly.Table({element: "anyfunc",
                                     initial: kTableSize,
                                     maximum: kTableSize});

  let builder = new WasmModuleBuilder();

  let j = builder.addImport("q", "js_div", kSig_i_ii);
  let w = builder.addImport("q", "wasm_mul", kSig_i_ii);
  builder.addImportedTable("q", "table", kTableSize, kTableSize);
  builder.addFunction("main", kSig_i_ii)
    .addBody([
      kExprI32Const, 44,  // --
778 779
      kExprLocalGet, 0,   // --
      kExprLocalGet, 1,   // --
780 781 782 783
      kExprCallIndirect, 0, kTableZero])  // --
    .exportAs("main");

  let g = builder.addImportedGlobal("q", "base", kWasmI32);
784
  builder.addElementSegment(0, g, true, [j, w]);
785 786 787 788 789 790 791 792 793 794 795

  let module = new WebAssembly.Module(builder.toBuffer());
  for (var i = 0; i < 5; i++) {
    let instance = new WebAssembly.Instance(module, {q: {base: i, js_div: js_div, wasm_mul: wasm_mul, table: table}});
    let j = i + 1;

    assertEquals((44/j)|0, instance.exports.main(j, i+0));
    assertEquals((44*j)|0, instance.exports.main(j, i+1));
    assertThrows(() => {instance.exports.main(j, i+2)});
  }
})();
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

(function ExportedFunctionsImportedOrder() {
  print(arguments.callee.name);

  let i1 = (() => {
    let builder = new WasmModuleBuilder();
    builder.addFunction("f1", kSig_i_v)
      .addBody(
        [kExprI32Const, 1])
      .exportFunc();
    builder.addFunction("f2", kSig_i_v)
      .addBody(
        [kExprI32Const, 2])
      .exportFunc();
    return builder.instantiate();
  })();

  let i2 = (() => {
    let builder = new WasmModuleBuilder();
815
    builder.addTable(kWasmAnyFunc, 4);
816 817 818 819
    builder.addImport("q", "f2", kSig_i_v);
    builder.addImport("q", "f1", kSig_i_v);
    builder.addFunction("main", kSig_i_i)
      .addBody([
820
        kExprLocalGet, 0,
821 822 823
        kExprCallIndirect, 0, kTableZero
      ])
      .exportFunc();
824
    builder.addElementSegment(0, 0, false, [0, 1, 1, 0]);
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

    return builder.instantiate({q: {f2: i1.exports.f2, f1: i1.exports.f1}});
  })();

  assertEquals(2, i2.exports.main(0));
  assertEquals(1, i2.exports.main(1));
  assertEquals(1, i2.exports.main(2));
  assertEquals(2, i2.exports.main(3));
})();

(function IndirectCallsToImportedFunctions() {
  print(arguments.callee.name);

  let module = (() => {
    let builder = new WasmModuleBuilder();
    builder.addMemory(1, 1, false);
    builder.addFunction("f", kSig_i_v)
      .addBody([
        kExprI32Const, 0,
        kExprI32LoadMem, 0, 4,
      ])
      .exportFunc();
    builder.exportMemoryAs("memory");
    return new WebAssembly.Module(builder.toBuffer());
  })();

  function setMemI32(instance, offset, val) {
    var array = new Int32Array(instance.exports.memory.buffer);
    array[offset/4] = val;
  }

  function makeFun(val) {
    let instance = new WebAssembly.Instance(module);
    setMemI32(instance, 0, 2000000);
    setMemI32(instance, 4, val);
    setMemI32(instance, 8, 3000000);
    return instance.exports.f;
  }

  let f300 = makeFun(300);
  let f100 = makeFun(100);
  let f200 = makeFun(200);

  let main = (() => {
    let builder = new WasmModuleBuilder();
    builder.addMemory(1, 1, false);
871
    builder.addTable(kWasmAnyFunc, 4);
872 873 874 875 876 877 878 879 880 881
    builder.addImport("q", "f1", kSig_i_v);
    builder.addImport("q", "f2", kSig_i_v);
    builder.addImport("q", "f3", kSig_i_v);
    builder.addFunction("f", kSig_i_v)
      .addBody([
        kExprI32Const, 8,
        kExprI32LoadMem, 0, 0,
      ]);
    builder.addFunction("main", kSig_i_i)
      .addBody([
882
        kExprLocalGet, 0,
883 884 885 886
        kExprCallIndirect, 0, kTableZero
      ])
      .exportFunc();
    builder.exportMemoryAs("memory");
887
    builder.addElementSegment(0, 0, false, [0, 1, 2, 3]);
888 889 890 891 892 893 894 895 896 897 898 899
    var instance = builder.instantiate({q: {f1: f100, f2: f200, f3: f300}});
    setMemI32(instance, 0, 5000000);
    setMemI32(instance, 4, 6000000);
    setMemI32(instance, 8, 400);
    return instance.exports.main;
  })();

  assertEquals(100, main(0));
  assertEquals(200, main(1));
  assertEquals(300, main(2));
  assertEquals(400, main(3));
})();