summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/gc/casting.js
blob: 3b550e641588cd59eb6bb74e03da82888b19f71e (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// |jit-test| skip-if: !wasmGcEnabled()

// Test all possible casting combinations of the following graph:
//
//  A1       A2
//  |        |
//  B1       B2
//  | \      |
//  C1  C2   C3
//  | \      |
//  D1  D2   D3
//  | \      |
//  E1  E2   E3
//  | \      |
//  F1  F2   F3
//  | \      |
//  G1  G2   G3
//  | \      |
//  H1  H2   H3
//  | \      |
//  I1  I2   I3
//  | \      |
//  J1  J2   J3
//
// NOTE: this object will be mutated and needs to be ordered such that parent
// definitions come before children.  Note also, to be properly effective,
// these trees need to have a depth of at least MinSuperTypeVectorLength as
// defined in wasm/WasmCodegenConstants.h; keep it in sync with that.
const TYPES = {
  'A1': { super: null },
  'A2': { super: null },
  'B1': { super: 'A1' },
  'B2': { super: 'A2' },
  'C1': { super: 'B1' },
  'C2': { super: 'B1' },
  'C3': { super: 'B2' },
  'D1': { super: 'C1' },
  'D2': { super: 'C1' },
  'D3': { super: 'C3' },
  'E1': { super: 'D1' },
  'E2': { super: 'D1' },
  'E3': { super: 'D3' },
  'F1': { super: 'E1' },
  'F2': { super: 'E1' },
  'F3': { super: 'E3' },
  'G1': { super: 'F1' },
  'G2': { super: 'F1' },
  'G3': { super: 'F3' },
  'H1': { super: 'G1' },
  'H2': { super: 'G1' },
  'H3': { super: 'G3' },
  'I1': { super: 'H1' },
  'I2': { super: 'H1' },
  'I3': { super: 'H3' },
  'J1': { super: 'I1' },
  'J2': { super: 'I1' },
  'J3': { super: 'I3' },
};

// The oracle method for testing the declared subtype relationship.
function manualIsSubtype(types, subType, superType) {
  while (subType !== superType && subType.super !== null) {
    subType = types[subType.super];
  }
  return subType === superType;
}

function testAllCasts(types) {
  let typeSection = ``;
  let funcSection = ``;
  for (let name in types) {
    let type = types[name];
    if (type.super === null) {
      typeSection += `(type \$${name} (sub (struct)))\n`;
    } else {
      typeSection += `(type \$${name} (sub \$${type.super} (struct)))\n`;
    }
    funcSection += `
      (func (export "new${name}") (result externref)
        struct.new_default \$${name}
        extern.convert_any
      )
      (func (export "is${name}") (param externref) (result i32)
        local.get 0
        any.convert_extern
        ref.test (ref \$${name})
      )`;
  }
  // NOTE: we place all types in a single recursion group to prevent
  // canonicalization from 
  let moduleText = `(module
    (rec ${typeSection})
    ${funcSection}
  )`;

  // Instantiate the module and acquire the testing methods
  let exports = wasmEvalText(moduleText).exports;
  for (let name in types) {
    let type = types[name];
    type['new'] = exports[`new${name}`];
    type['is'] = exports[`is${name}`];
  }

  // Test every combination of types, comparing the oracle method against the
  // JIT'ed method.
  for (let subTypeName in types) {
    let subType = types[subTypeName];
    for (let superTypeName in types) {
      let superType = types[subTypeName];
      assertEq(
        manualIsSubtype(types, subType, superType) ? 1 : 0,
        superType['is'](subType['new']()));
    }
  }
}
testAllCasts(TYPES);

// Test that combinations of ref.test and ref.cast compile correctly.
// (These can be optimized together.)
{
  const { make, test1, test2, test3, test4 } = wasmEvalText(`(module
    (type $a (array i32))
    (func (export "make") (param i32) (result anyref)
      local.get 0
      local.get 0
      array.new_fixed $a 2
    )
    (func (export "test1") (param anyref) (result i32)
      (if (ref.test (ref $a) (local.get 0))
        (then
          (ref.cast (ref $a) (local.get 0))
          (array.get $a (i32.const 0))
          return
        )
      )
      i32.const -1
    )
    (func (export "test2") (param anyref) (result i32)
      (if (ref.test (ref $a) (local.get 0))
        (then)
        (else
          (ref.cast (ref $a) (local.get 0))
          (array.get $a (i32.const 0))
          return
        )
      )
      i32.const -1
    )
    (func (export "test3") (param anyref) (result i32)
      (if (ref.test (ref $a) (local.get 0))
        (then
          (if (ref.test (ref $a) (local.get 0))
            (then)
            (else
              (ref.cast (ref $a) (local.get 0))
              (array.get $a (i32.const 0))
              return
            )
          )
        )
      )
      i32.const -1
    )
    (func (export "test4") (param anyref) (result i32)
      (if (ref.test (ref $a) (local.get 0))
        (then
          (if (ref.test (ref $a) (local.get 0))
            (then
              local.get 0
              ref.cast (ref $a)
              ref.cast (ref $a)
              (array.get $a (i32.const 0))
              return
            )
          )
        )
      )
      i32.const -1
    )
  )`).exports;
  assertEq(test1(make(99)), 99);
  assertEq(test2(make(99)), -1);
  assertEq(test3(make(99)), -1);
  assertEq(test4(make(99)), 99);
}