wasm-array-js-interop.js 2.47 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2021 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: --allow-natives-syntax --experimental-wasm-gc --wasm-gc-js-interop
// Flags: --expose-gc

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

10 11
const kIterationsCountForICProgression = 20;

12 13 14
function createArray_i() {
  let builder = new WasmModuleBuilder();

15
  const type_index = builder.addArray(kWasmI32, true);
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

  let sig_a_i = makeSig_r_x(kWasmDataRef, kWasmI32);
  let sig_i_ai = makeSig([kWasmDataRef, kWasmI32], [kWasmI32]);
  let sig_v_aii = makeSig([kWasmDataRef, kWasmI32, kWasmI32], []);

  builder.addFunction("new_array", sig_a_i)
    .addBody([
      kExprLocalGet, 0,                             // --
      kExprI32Const, 10,                            // --
      kGCPrefix, kExprRttCanon, type_index,         // --
      kGCPrefix, kExprArrayNewWithRtt, type_index]) // --
    .exportAs("new_array");

  builder.addFunction("array_get", sig_i_ai)
    .addBody([
      kExprLocalGet, 0,                      // --
      kGCPrefix, kExprRttCanon, type_index,  // --
      kGCPrefix, kExprRefCast,               // --
      kExprLocalGet, 1,                      // --
      kGCPrefix, kExprArrayGet, type_index]) // --
    .exportAs("array_get");

  builder.addFunction("array_set", sig_v_aii)
    .addBody([
      kExprLocalGet, 0,                      // --
      kGCPrefix, kExprRttCanon, type_index,  // --
      kGCPrefix, kExprRefCast,               // --
      kExprLocalGet, 1,                      // --
      kExprLocalGet, 2,                      // --
      kGCPrefix, kExprArraySet, type_index]) // --
    .exportAs("array_set");

  let instance = builder.instantiate();
  let new_array = instance.exports.new_array;
  let array_get = instance.exports.array_get;
  let array_set = instance.exports.array_set;

  let value = 42;
  let o = new_array(value);
  %DebugPrint(o);
  assertEquals(10, o.length);
  for (let i = 0; i < o.length; i++) {
    let res;
    res = array_get(o, i);
    assertEquals(value, res);

    array_set(o, i, i);
    res = array_get(o, i);
    assertEquals(i, res);
  }
  return o;
}

(function TestSimpleArrayInterop() {
  function f(o) {
71 72
    assertEquals(10, o.length);
    for (let i = 0; i < o.length; i++) {
73
      let len = o.length;
74
      assertEquals(10, len);
75 76
      let v = o[i];
      assertEquals(i, v);
77 78 79 80 81 82 83 84 85
    }
  }

  let o = createArray_i();
  %DebugPrint(o);

  f(o);
  gc();
})();