summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/wasm/wasm-array-js-interop.js
blob: 7f7c411c345a0bddbc7a513cd15d8b9b4816bde7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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

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

const kIterationsCountForICProgression = 20;

// TODO(ishell): remove once leaked maps could keep NativeModule alive.
let instances = [];

function createArray_i() {
  let builder = new WasmModuleBuilder();

  const type_index = builder.addArray(kWasmI32, true);

  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();
  instances.push(instance);
  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) {
    assertEquals(10, o.length);
    for (let i = 0; i < o.length; i++) {
      let len = o.length;
      assertEquals(10, len);
      let v = o[i];
      assertEquals(i, v);
    }
  }

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

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