v8/test/mjsunit/wasm/anyref-table.js
Emanuel Ziegler de17316ad2 [wasm][reference-types] Implement declarative segments
Implement the latest spec changes:
  - Allow declarative segments to behave like passive & dropped segments.
  - Enforce that only declared functions may be returned or used in globals
    as funcref.
  - Ensure that table fill does not modify any entries if OOB.

Spec tests for select and br_table are still failing due to proposal issue

Bug: v8:10156

R=ahaas@chromium.org

Change-Id: I5b95be36a67bc7482a84b848908cc4cbdf94af03
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2027458
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Commit-Queue: Emanuel Ziegler <ecmziegler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66297}
2020-02-17 17:38:27 +00:00

80 lines
2.7 KiB
JavaScript

// 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.
// Flags: --experimental-wasm-anyref --experimental-wasm-bulk-memory
load("test/mjsunit/wasm/wasm-module-builder.js");
(function TestAnyRefTableSetWithMultipleTypes() {
print(arguments.callee.name);
let table = new WebAssembly.Table({element: "anyref", initial: 10});
// 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);
})();
(function TestImportAnyRefTable() {
print(arguments.callee.name);
const builder = new WasmModuleBuilder();
const table_index = builder.addImportedTable("imp", "table", 3, 10, kWasmAnyRef);
builder.addFunction('get', kSig_r_v)
.addBody([kExprI32Const, 0, kExprTableGet, table_index]);
let table_ref = new WebAssembly.Table({element: "anyref", initial: 3, maximum: 10});
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/);
})();
(function TestAnyRefDropDeclarativeElementSegment() {
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();
})();
(function TestAnyRefTableInitFromDeclarativeElementSegment() {
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());
})();