export-mutable-global.js 2.99 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
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
6 7 8 9

(function exportImmutableGlobal() {
  var builder = new WasmModuleBuilder();
  let globals = [
10 11 12
    [kWasmI32, 'i32', WasmInitExpr.I32Const(4711)],
    [kWasmF32, 'f32', WasmInitExpr.F32Const(Math.fround(3.14))],
    [kWasmF64, 'f64', WasmInitExpr.F64Const(1 / 7)]
13 14
  ];
  for (let [type, name, value] of globals) {
15
    builder.addGlobal(type, false, value).exportAs(name);
16
  }
17
  var instance = builder.instantiate();
18 19

  for (let [type, name, value] of globals) {
20
    let obj = instance.exports[name];
21 22
    assertEquals("object", typeof obj, name);
    assertTrue(obj instanceof WebAssembly.Global, name);
23
    assertEquals(value.value || 0, obj.value, name);
24
    assertThrows(() => obj.value = 0);
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  }
})();

(function canExportI64Global() {
  var builder = new WasmModuleBuilder();
  builder.addGlobal(kWasmI64, false).exportAs('g');
  builder.instantiate();
})();

(function canExportAndImportI64() {
  var builder = new WasmModuleBuilder();
  builder.addGlobal(kWasmI64, false).exportAs('g');
  let g = builder.instantiate().exports.g;

  builder = new WasmModuleBuilder();
  builder.addImportedGlobal("mod", "g", kWasmI64);
  builder.instantiate({mod: {g: g}});
})();

(function exportMutableGlobal() {
  var builder = new WasmModuleBuilder();
  let globals = [
47 48 49
    [kWasmI32, 'i32', WasmInitExpr.I32Const(4711)],               // -
    [kWasmF32, 'f32', WasmInitExpr.F32Const(Math.fround(3.14))],  // -
    [kWasmF64, 'f64', WasmInitExpr.F64Const(1 / 7)]               // -
50 51
  ];
  for (let [index, [type, name, value]] of globals.entries()) {
52
    builder.addGlobal(type, true, value).exportAs(name);
53
    builder.addFunction("get " + name, makeSig([], [type]))
54
      .addBody([kExprGlobalGet, index])
55 56
      .exportFunc();
    builder.addFunction("set " + name, makeSig([type], []))
57
      .addBody([kExprLocalGet, 0, kExprGlobalSet, index])
58 59
      .exportFunc();
  }
60
  var instance = builder.instantiate();
61 62

  for (let [type, name, value] of globals) {
63
    let obj = instance.exports[name];
64

65
    assertEquals(value.value || 0, obj.value, name);
66

67
    // Changing the exported global should change the instance's global.
68
    obj.value = 1001;
69
    assertEquals(1001, instance.exports['get ' + name](), name);
70

71 72
    // Changing the instance's global should change the exported global.
    instance.exports['set ' + name](112358);
73 74 75
    assertEquals(112358, obj.value, name);
  }
})();
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

(function exportImportedMutableGlobal() {
  let builder = new WasmModuleBuilder();
  builder.addGlobal(kWasmI32, true).exportAs('g1');
  let g1 = builder.instantiate().exports.g1;

  builder = new WasmModuleBuilder();
  builder.addImportedGlobal("mod", "g1", kWasmI32, true);
  builder.addExportOfKind('g2', kExternalGlobal, 0);
  let g2 = builder.instantiate({mod: {g1: g1}}).exports.g2;

  g1.value = 123;

  assertEquals(g1.value, g2.value);
})();