externref.js 11.7 KB
Newer Older
1 2 3 4
// Copyright 2018 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.

5
// Flags: --expose-wasm --expose-gc --allow-natives-syntax
6

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

9
(function testExternRefIdentityFunction() {
10 11 12
  print(arguments.callee.name);
  const builder = new WasmModuleBuilder();
  builder.addFunction('main', kSig_r_r)
13
      .addBody([kExprLocalGet, 0])
14 15 16 17 18 19 20 21 22 23 24 25 26
      .exportFunc();

  const instance = builder.instantiate();

  let obj = {'hello' : 'world'};
  assertEquals(obj, instance.exports.main(obj));
  assertEquals(1234, instance.exports.main(1234));
  assertEquals(123.4, instance.exports.main(123.4));
  assertEquals(undefined, instance.exports.main(undefined));
  assertEquals(null, instance.exports.main(null));
  assertEquals(print, instance.exports.main(print));
})();

27
(function testPassExternRefToImportedFunction() {
28 29 30 31 32
  print(arguments.callee.name);
  const builder = new WasmModuleBuilder();
  const sig_index = builder.addType(kSig_v_r);
  const imp_index = builder.addImport("q", "func", sig_index);
  builder.addFunction('main', sig_index)
33
      .addBody([kExprLocalGet, 0,
34 35 36 37 38 39 40 41 42 43 44 45
      kExprCallFunction, imp_index])
      .exportFunc();

  function checkFunction(value) {
    assertEquals('world', value.hello);
  }

  const instance = builder.instantiate({q: {func: checkFunction}});

  instance.exports.main({hello: 'world'});
})();

46
(function testPassExternRefWithGCWithLocals() {
47 48 49 50 51 52 53 54
  print(arguments.callee.name);
  const builder = new WasmModuleBuilder();
  const ref_sig = builder.addType(kSig_v_r);
  const void_sig = builder.addType(kSig_v_v);
  const imp_index = builder.addImport("q", "func", ref_sig);
  const gc_index = builder.addImport("q", "gc", void_sig);
  // First call the gc, then check if the object still exists.
  builder.addFunction('main', ref_sig)
55
      .addLocals(kWasmExternRef, 10)
56
      .addBody([
57 58 59 60 61 62 63 64 65 66
        kExprLocalGet, 0, kExprLocalSet, 1,             // Set local
        kExprLocalGet, 0, kExprLocalSet, 2,             // Set local
        kExprLocalGet, 0, kExprLocalSet, 3,             // Set local
        kExprLocalGet, 0, kExprLocalSet, 4,             // Set local
        kExprLocalGet, 0, kExprLocalSet, 5,             // Set local
        kExprLocalGet, 0, kExprLocalSet, 6,             // Set local
        kExprLocalGet, 0, kExprLocalSet, 7,             // Set local
        kExprLocalGet, 0, kExprLocalSet, 8,             // Set local
        kExprLocalGet, 0, kExprLocalSet, 9,             // Set local
        kExprLocalGet, 0, kExprLocalSet, 10,            // Set local
67
        kExprCallFunction, gc_index,                    // call gc
68
        kExprLocalGet, 9, kExprCallFunction, imp_index  // call import
69 70 71 72 73 74 75 76 77 78 79 80
      ])
      .exportFunc();

  function checkFunction(value) {
    assertEquals('world', value.hello);
  }

  const instance = builder.instantiate({q: {func: checkFunction, gc: gc}});

  instance.exports.main({hello: 'world'});
})();

81
(function testPassExternRefWithGC() {
82 83 84 85 86 87 88 89 90 91
  print(arguments.callee.name);
  const builder = new WasmModuleBuilder();
  const ref_sig = builder.addType(kSig_v_r);
  const void_sig = builder.addType(kSig_v_v);
  const imp_index = builder.addImport("q", "func", ref_sig);
  const gc_index = builder.addImport("q", "gc", void_sig);
  // First call the gc, then check if the object still exists.
  builder.addFunction('main', ref_sig)
      .addBody([
        kExprCallFunction, gc_index,                    // call gc
92
        kExprLocalGet, 0, kExprCallFunction, imp_index  // call import
93 94 95 96 97 98 99 100 101 102 103
      ])
      .exportFunc();

  function checkFunction(value) {
    assertEquals('world', value.hello);
  }

  const instance = builder.instantiate({q: {func: checkFunction, gc: gc}});

  instance.exports.main({hello: 'world'});
})();
104

105
(function testPassExternRefWithGCWithStackParameters() {
106 107 108 109
  print(arguments.callee.name);
  const num_params = 15;
  for (let index = 0; index < num_params; index++) {
    const builder = new WasmModuleBuilder();
110 111
    // Make a signature with {num_params} many externref parameters.
    const mysig = makeSig(Array(num_params).fill(kWasmExternRef), []);
112 113 114 115 116 117 118 119 120
    const main_sig = builder.addType(mysig);
    const ref_sig = builder.addType(kSig_v_r);
    const void_sig = builder.addType(kSig_v_v);
    const imp_index = builder.addImport('q', 'func', ref_sig);
    const gc_index = builder.addImport('q', 'gc', void_sig);
    // First call the gc, then check if the object still exists.
    builder.addFunction('main', main_sig)
        .addBody([
          kExprCallFunction, gc_index,                        // call gc
121
          kExprLocalGet, index, kExprCallFunction, imp_index  // call import
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        ])
        .exportFunc();

    function checkFunction(value) {
      assertEquals(index, value.hello);
    }

    const instance = builder.instantiate({q: {func: checkFunction, gc: gc}});

    // Pass {num_params} many parameters to main. Note that it is important
    // that no other references to these objects exist. They are kept alive
    // only through references stored in the parameters slots of a stack frame.
    instance.exports.main(
        {hello: 0}, {hello: 1}, {hello: 2}, {hello: 3}, {hello: 4}, {hello: 5},
        {hello: 6}, {hello: 7}, {hello: 8}, {hello: 9}, {hello: 10},
        {hello: 11}, {hello: 12}, {hello: 13}, {hello: 14});
  }
})();

141
(function testPassExternRefWithGCInWrapper() {
142 143
  print(arguments.callee.name);
  const builder = new WasmModuleBuilder();
144
  const kSig_r_iri = makeSig([kWasmI32, kWasmExternRef, kWasmI32], [kWasmExternRef]);
145 146
  const sig_index = builder.addType(kSig_r_iri);
  builder.addFunction('main', sig_index)
147
      .addBody([kExprLocalGet, 1])
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
      .exportFunc();

  const instance = builder.instantiate();

  const triggerGCParam = {
    valueOf: () => {
      gc();
      return 17;
    }
  };

  const result = instance.exports.main(triggerGCParam, {hello: 'world'}, triggerGCParam);
  assertEquals('world', result.hello);
})();

163
(function testExternRefNull() {
164 165 166
  print(arguments.callee.name);
  const builder = new WasmModuleBuilder();
  builder.addFunction('main', kSig_r_v)
167
      .addBody([kExprRefNull, kExternRefCode])
168 169 170 171 172 173
      .exportFunc();

  const instance = builder.instantiate();

  assertEquals(null, instance.exports.main());
})();
174

175
(function testExternRefIsNull() {
176 177 178
  print(arguments.callee.name);
  const builder = new WasmModuleBuilder();
  builder.addFunction('main', kSig_i_r)
179
      .addBody([kExprLocalGet, 0, kExprRefIsNull])
180 181 182 183 184 185 186 187 188 189 190
      .exportFunc();

  const instance = builder.instantiate();

  assertEquals(0, instance.exports.main({'hello' : 'world'}));
  assertEquals(0, instance.exports.main(1234));
  assertEquals(0, instance.exports.main(0));
  assertEquals(0, instance.exports.main(123.4));
  assertEquals(0, instance.exports.main(undefined));
  assertEquals(1, instance.exports.main(null));
  assertEquals(0, instance.exports.main(print));
191
})();
192

193
(function testExternRefNullIsNull() {
194 195 196 197
  print(arguments.callee.name);

  const builder = new WasmModuleBuilder();
  builder.addFunction('main', kSig_i_v)
198
      .addBody([kExprRefNull, kExternRefCode, kExprRefIsNull])
199 200 201 202 203 204 205
      .exportFunc();

  const instance = builder.instantiate();

  assertEquals(1, instance.exports.main());
})();

206
(function testExternRefLocalDefaultValue() {
207
  print(arguments.callee.name);
208 209 210 211 212 213 214
  const numLocals = 3;
  for (let i = 0; i < numLocals; ++i) {
    const builder = new WasmModuleBuilder();
    builder.addFunction('main', kSig_r_v)
        .addBody([kExprLocalGet, i])
        .addLocals(kWasmExternRef, numLocals)
        .exportFunc();
215

216
    const instance = builder.instantiate();
217

218 219
    assertEquals(null, instance.exports.main());
  }
220
})();
221

222
(function testImplicitReturnNullAsExternRef() {
223 224 225 226
  print(arguments.callee.name);
  const builder = new WasmModuleBuilder();
  const sig_index = builder.addType(kSig_r_v);
  builder.addFunction('main', sig_index)
227
      .addBody([kExprRefNull, kExternRefCode])
228 229 230 231 232 233
      .exportFunc();

  const main = builder.instantiate().exports.main;
  assertEquals(null, main());
})();

234
(function testExplicitReturnNullAsExternRef() {
235 236 237 238
  print(arguments.callee.name);
  const builder = new WasmModuleBuilder();
  const sig_index = builder.addType(kSig_r_v);
  builder.addFunction('main', sig_index)
239
      .addBody([kExprRefNull, kExternRefCode, kExprReturn])
240 241 242 243 244
      .exportFunc();

  const main = builder.instantiate().exports.main;
  assertEquals(null, main());
})();
245 246 247 248 249 250

(function testGCInStackCheck() {
  print(arguments.callee.name);
  const builder = new WasmModuleBuilder();

  const gc_sig = builder.addType(kSig_v_v);
251 252 253 254 255 256
  const mysig = makeSig(
      [
        kWasmExternRef, kWasmI32, kWasmExternRef, kWasmExternRef, kWasmExternRef
      ],
      []);
  const func_sig = builder.addType(mysig);
257 258 259
  const triggerGC_index = builder.addImport('q', 'triggerGC', gc_sig);
  const func_index = builder.addImport('q', 'func', func_sig);

260 261 262 263 264 265 266
  const foo = builder.addFunction('foo', func_sig).addBody([
    kExprLocalGet, 0, // --
    kExprLocalGet, 1, // --
    kExprLocalGet, 2, // --
    kExprLocalGet, 3, // --
    kExprLocalGet, 4, // --
    kExprCallFunction, func_index
267 268
  ]);

269
  builder.addFunction('main', func_sig)
270
      .addBody([
271 272 273 274 275 276 277
        kExprCallFunction, triggerGC_index,  // --
        kExprLocalGet, 0,                    // --
        kExprLocalGet, 1,                    // --
        kExprLocalGet, 2,                    // --
        kExprLocalGet, 3,                    // --
        kExprLocalGet, 4,                    // --
        kExprCallFunction, foo.index
278 279 280 281 282 283 284 285 286 287
      ])
      .exportFunc();

  const instance = builder.instantiate({
    q: {
      triggerGC: () => %ScheduleGCInStackCheck(),
      func: (ref) => assertEquals(ref.hello, 4)
    }
  });

288
  instance.exports.main({hello: 4}, 5, {world: 6}, null, {bar: 7});
289
})();
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

(function testGCInStackCheckUnalignedFrameSize() {
  print(arguments.callee.name);
  const builder = new WasmModuleBuilder();

  const gc_sig = builder.addType(kSig_v_v);
  const mysig = makeSig(
      [
        kWasmExternRef, kWasmI32, kWasmExternRef, kWasmExternRef, kWasmExternRef
      ],
      []);
  const func_sig = builder.addType(mysig);
  const triggerGC_index = builder.addImport('q', 'triggerGC', gc_sig);
  const func_index = builder.addImport('q', 'func', func_sig);

  const foo = builder.addFunction('foo', func_sig).addBody([
    kExprLocalGet, 0, // --
    kExprLocalGet, 1, // --
    kExprLocalGet, 2, // --
    kExprLocalGet, 3, // --
    kExprLocalGet, 4, // --
    kExprCallFunction, func_index
  ]).addLocals(kWasmI32, 1);

  builder.addFunction('main', func_sig)
      .addBody([
        kExprCallFunction, triggerGC_index,  // --
        kExprLocalGet, 0,                    // --
        kExprLocalGet, 1,                    // --
        kExprLocalGet, 2,                    // --
        kExprLocalGet, 3,                    // --
        kExprLocalGet, 4,                    // --
        kExprCallFunction, foo.index
      ])
      .exportFunc();

  const instance = builder.instantiate({
    q: {
      triggerGC: () => %ScheduleGCInStackCheck(),
      func: (ref) => assertEquals(ref.hello, 4)
    }
  });

  instance.exports.main({hello: 4}, 5, {world: 6}, null, {bar: 7});
})();
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

(function MultiReturnRefTest() {
  print("MultiReturnTest");
  let builder = new WasmModuleBuilder();
  let sig = makeSig([kWasmExternRef],
      [kWasmExternRef, kWasmExternRef, kWasmExternRef, kWasmExternRef]);

  builder.addFunction("callee", sig)
    .addBody([
      kExprLocalGet, 0,
      kExprLocalGet, 0,
      kExprLocalGet, 0,
      kExprLocalGet, 0,
    ]);
  builder.addFunction("main", sig)
    .addBody([
      kExprLocalGet, 0,
      kExprCallFunction, 0
    ])
    .exportAs("main");

  let module = new WebAssembly.Module(builder.toBuffer());
  let instance = new WebAssembly.Instance(module);
  assertEquals(instance.exports.main(null), [null, null, null, null]);
})();