globals.js 6.56 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 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: --expose-wasm

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

9 10 11 12 13 14 15 16 17
(function TestMultipleInstances() {
  print("TestMultipleInstances");

  var builder = new WasmModuleBuilder();

  let g = builder.addGlobal(kWasmI32, true);
  let sig_index = builder.addType(kSig_i_v);
  builder.addFunction("get", sig_index)
    .addBody([
18
      kExprGlobalGet, g.index])
19 20 21
    .exportAs("get");
  builder.addFunction("set", kSig_v_i)
    .addBody([
22
      kExprLocalGet, 0,
23
      kExprGlobalSet, g.index])
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
    .exportAs("set");

  let module = new WebAssembly.Module(builder.toBuffer());

  let a = new WebAssembly.Instance(module);
  let b = new WebAssembly.Instance(module);

  assertEquals(0, a.exports.get());
  assertEquals(0, b.exports.get());

  a.exports.set(1);

  assertEquals(1, a.exports.get());
  assertEquals(0, b.exports.get());

  b.exports.set(6);

  assertEquals(1, a.exports.get());
  assertEquals(6, b.exports.get());

  a.exports.set(7);

  assertEquals(7, a.exports.get());
  assertEquals(6, b.exports.get());

})();

51 52 53 54
function TestImported(type, val, expected) {
  print("TestImported " + type + "(" + val +")" + " = " + expected);
  var builder = new WasmModuleBuilder();
  var sig = makeSig([], [type]);
55
  var g = builder.addImportedGlobal("uuu", "foo", type);
56
  builder.addFunction("main", sig)
57
    .addBody([kExprGlobalGet, g])
58
    .exportAs("main");
59
  builder.addGlobal(kWasmI32);  // pad
60

61
  var instance = builder.instantiate({uuu: {foo: val}});
62 63 64
  assertEquals(expected, instance.exports.main());
}

65 66 67
TestImported(kWasmI32, 300.1, 300);
TestImported(kWasmF32, 87234.87238, Math.fround(87234.87238));
TestImported(kWasmF64, 77777.88888, 77777.88888);
68 69


70 71 72 73 74 75 76 77 78
(function TestImportedMultipleInstances() {
  print("TestImportedMultipleInstances");

  var builder = new WasmModuleBuilder();

  let g = builder.addImportedGlobal("mod", "g", kWasmI32);
  let sig_index = builder.addType(kSig_i_v);
  builder.addFunction("main", sig_index)
    .addBody([
79
      kExprGlobalGet, g])
80 81 82 83 84 85 86 87 88 89 90 91 92
    .exportAs("main");

  let module = new WebAssembly.Module(builder.toBuffer());

  print("  i 100...");
  let i100 = new WebAssembly.Instance(module, {mod: {g: 100}});
  assertEquals(100, i100.exports.main());

  print("  i 300...");
  let i300 = new WebAssembly.Instance(module, {mod: {g: 300}});
  assertEquals(300, i300.exports.main());
})();

93 94 95
function TestExported(type, val, expected) {
  print("TestExported " + type + "(" + val +")" + " = " + expected);
  var builder = new WasmModuleBuilder();
96
  builder.addGlobal(kWasmI32);  // pad
97
  builder.addGlobal(type, false, val).exportAs("foo");
98
  builder.addGlobal(kWasmI32);  // pad
99 100

  var instance = builder.instantiate();
101
  assertEquals(expected, instance.exports.foo.value);
102 103
}

104 105 106 107
TestExported(kWasmI32, WasmInitExpr.I32Const(455.5), 455);
TestExported(kWasmF32, WasmInitExpr.F32Const(-999.34343),
             Math.fround(-999.34343));
TestExported(kWasmF64, WasmInitExpr.F64Const(87347.66666), 87347.66666);
108

109 110 111
(function TestI64Exported() {
  var builder = new WasmModuleBuilder();
  builder.addGlobal(kWasmI32);  // pad
112
  builder.addGlobal(kWasmI64, false, WasmInitExpr.I64Const(1234))
113 114 115
      .exportAs("foo");
  builder.addGlobal(kWasmI32);  // pad

116 117
  var instance = builder.instantiate();
  assertTrue(instance.exports.foo instanceof WebAssembly.Global);
118
  assertEquals(instance.exports.foo.value, 1234n);
119
})();
120 121

function TestImportedExported(type, val, expected) {
122
  print("TestImportedExported " + type + "(" + val + ")" + " = " + expected);
123
  var builder = new WasmModuleBuilder();
124
  var i = builder.addImportedGlobal("ttt", "foo", type);
125
  builder.addGlobal(kWasmI32);  // pad
126
  builder.addGlobal(type, false, WasmInitExpr.GlobalGet(i))
127
      .exportAs("bar");
128
  builder.addGlobal(kWasmI32);  // pad
129

130
  var instance = builder.instantiate({ttt: {foo: val}});
131
  assertEquals(expected, instance.exports.bar.value);
132 133
}

134 135 136
TestImportedExported(kWasmI32, 415.5, 415);
TestImportedExported(kWasmF32, -979.34343, Math.fround(-979.34343));
TestImportedExported(kWasmF64, 81347.66666, 81347.66666);
137 138 139 140

function TestGlobalIndexSpace(type, val) {
  print("TestGlobalIndexSpace(" + val + ") = " + val);
  var builder = new WasmModuleBuilder();
141
  var im = builder.addImportedGlobal("nnn", "foo", type);
142
  assertEquals(0, im);
143
  var def = builder.addGlobal(type, false, WasmInitExpr.GlobalGet(im));
144 145 146 147
  assertEquals(1, def.index);

  var sig = makeSig([], [type]);
  builder.addFunction("main", sig)
148
    .addBody([kExprGlobalGet, def.index])
149 150
    .exportAs("main");

151
  var instance = builder.instantiate({nnn: {foo: val}});
152 153 154
  assertEquals(val, instance.exports.main());
}

155 156 157
TestGlobalIndexSpace(kWasmI32, 123);
TestGlobalIndexSpace(kWasmF32, 54321.125);
TestGlobalIndexSpace(kWasmF64, 12345.678);
158 159 160 161 162 163 164 165 166 167 168

(function TestAccessesInBranch() {
  print("TestAccessesInBranches");

  var builder = new WasmModuleBuilder();

  let g = builder.addGlobal(kWasmI32, true);
  let h = builder.addGlobal(kWasmI32, true);
  let sig_index = builder.addType(kSig_i_i);
  builder.addFunction("get", sig_index)
    .addBody([
169
      kExprLocalGet, 0,
170
      kExprIf, kWasmI32,
171
      kExprGlobalGet, g.index,
172
      kExprElse,
173
      kExprGlobalGet, h.index,
174 175 176 177
      kExprEnd])
    .exportAs("get");
  builder.addFunction("set", kSig_v_ii)
    .addBody([
178
      kExprLocalGet, 0,
179
      kExprIf, kWasmVoid,
180
      kExprLocalGet, 1,
181
      kExprGlobalSet, g.index,
182
      kExprElse,
183
      kExprLocalGet, 1,
184
      kExprGlobalSet, h.index,
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      kExprEnd])
    .exportAs("set");

  let module = new WebAssembly.Module(builder.toBuffer());

  let a = new WebAssembly.Instance(module);
  let get = a.exports.get;
  let set = a.exports.set;

  assertEquals(0, get(0));
  assertEquals(0, get(1));
  set(0, 1);
  assertEquals(1, get(0));
  assertEquals(0, get(1));

  set(0, 7);
  assertEquals(7, get(0));
  assertEquals(0, get(1));

  set(1, 9);
  assertEquals(7, get(0));
  assertEquals(9, get(1));

})();
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

(function testAssignUndefinedToGlobal() {
  print(arguments.callee.name);
  let i32_global = new WebAssembly.Global({mutable: true, value: 'i32'});
  i32_global.value = undefined;
  assertSame(0, i32_global.value);
  let i64_global = new WebAssembly.Global({mutable: true, value: 'i64'});
  assertThrows(() => {
    i64_global.value = undefined;
  }, TypeError);
  let f32_global = new WebAssembly.Global({mutable: true, value: 'f32'});
  f32_global.value = undefined;
  assertSame(NaN, f32_global.value);
  let f64_global = new WebAssembly.Global({mutable: true, value: 'f64'});
  f64_global.value = undefined;
  assertSame(NaN, f64_global.value);
})();