externref-table.js 2.75 KB
Newer Older
1 2 3 4
// Copyright 2019 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: --experimental-wasm-reftypes --experimental-wasm-bulk-memory
6 7 8

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

9
(function TestExternRefTableSetWithMultipleTypes() {
10
  print(arguments.callee.name);
11
  let table = new WebAssembly.Table({element: "externref", initial: 10});
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

  // Table should be initialized with null.
  assertEquals(null, table.get(1));
  let obj = {'hello' : 'world'};
  table.set(2, obj);
  assertSame(obj, table.get(2));
  table.set(3, 1234);
  assertEquals(1234, table.get(3));
  table.set(4, 123.5);
  assertEquals(123.5, table.get(4));
  table.set(5, undefined);
  assertEquals(undefined, table.get(5));
  // Overwrite entry 4, because null would otherwise be the default value.
  table.set(4, null);
  assertEquals(null, table.get(4));
  table.set(7, print);
  assertEquals(print, table.get(7));

  assertThrows(() => table.set(12), RangeError);
})();
32

33
(function TestImportExternRefTable() {
34 35 36
  print(arguments.callee.name);

  const builder = new WasmModuleBuilder();
37
  const table_index = builder.addImportedTable("imp", "table", 3, 10, kWasmExternRef);
38
  builder.addFunction('get', kSig_r_v)
39
  .addBody([kExprI32Const, 0, kExprTableGet, table_index]);
40

41
  let table_ref = new WebAssembly.Table({element: "externref", initial: 3, maximum: 10});
42 43 44 45 46 47
  builder.instantiate({imp:{table: table_ref}});

  let table_func = new WebAssembly.Table({ element: "anyfunc", initial: 3, maximum: 10 });
  assertThrows(() => builder.instantiate({ imp: { table: table_func } }),
    WebAssembly.LinkError, /imported table does not match the expected type/);
})();
48

49
(function TestExternRefDropDeclarativeElementSegment() {
50 51 52 53 54 55 56 57 58 59 60 61 62 63
  print(arguments.callee.name);

  const builder = new WasmModuleBuilder();
  builder.addDeclarativeElementSegment([null]);
  builder.addFunction('drop', kSig_v_v)
      .addBody([kNumericPrefix, kExprElemDrop, 0])
      .exportFunc();
  const instance = builder.instantiate();

  // Counts as double-drop because declarative segments are dropped on
  // initialization and is therefore not expected to throw.
  instance.exports.drop();
})();

64
(function TestExternRefTableInitFromDeclarativeElementSegment() {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  print(arguments.callee.name);

  const builder = new WasmModuleBuilder();
  const table = builder.addTable(kWasmAnyFunc, 10);
  builder.addDeclarativeElementSegment([null]);
  builder.addFunction('init', kSig_v_v)
      .addBody([
        kExprI32Const, 0, kExprI32Const, 0, kExprI32Const, 1, kNumericPrefix,
        kExprTableInit, table.index, 0
      ])
      .exportFunc();
  const instance = builder.instantiate();

  assertTraps(kTrapTableOutOfBounds, () => instance.exports.init());
})();