stringrefs-invalid.js 1.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright 2022 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 --experimental-wasm-eh --experimental-wasm-gc

d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");

function assertInvalid(fn, message) {
  let builder = new WasmModuleBuilder();
  fn(builder);
  assertThrows(() => builder.toModule(), WebAssembly.CompileError,
               `WebAssembly.Module(): ${message}`);
}

16
let enableMessage = 'enable with --experimental-wasm-stringref'
17
assertInvalid(builder => builder.addLiteralStringRef("foo"),
18
              `unexpected section <StringRef> (${enableMessage}) @+10`);
19 20 21 22 23

for (let [name, code] of [['string', kWasmStringRef],
                          ['stringview_wtf8', kWasmStringViewWtf8],
                          ['stringview_wtf16', kWasmStringViewWtf16],
                          ['stringview_iter', kWasmStringViewIter]]) {
24
  let message = `invalid value type '${name}ref', ${enableMessage}`;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  let default_init = WasmInitExpr.RefNull(code);

  assertInvalid(b => b.addType(makeSig([code], [])),
                `${message} @+13`);
  assertInvalid(b => b.addStruct([makeField(code, true)]),
                `${message} @+13`);
  assertInvalid(b => b.addArray(code, true),
                `${message} @+12`);
  assertInvalid(b => b.addType(makeSig([], [code])),
                `${message} @+14`);
  assertInvalid(b => b.addGlobal(code, true, default_init),
                `${message} @+11`);
  assertInvalid(b => b.addTable(code, 0),
                `${message} @+11`);
  assertInvalid(b => b.addPassiveElementSegment([default_init], code),
                `${message} @+12`);
  assertInvalid(b => b.addTag(makeSig([code], [])),
                `${message} @+13`);
43 44 45
  assertInvalid(
    b => b.addFunction(undefined, kSig_v_v).addLocals(code, 1).addBody([]),
    `Compiling function #0 failed: ${message} @+24`);
46
}