summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/wasm/globals.js
blob: afd2dcc5ceeb8d5209882eaa41607582f6f8e6bd (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
// 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

load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");

function TestImported(type, val, expected) {
  print("TestImported " + type + "(" + val +")" + " = " + expected);
  var builder = new WasmModuleBuilder();
  var sig = makeSig([], [type]);
  var g = builder.addImportedGlobal("uuu", "foo", type);
  builder.addFunction("main", sig)
    .addBody([kExprGetGlobal, g])
    .exportAs("main");
  builder.addGlobal(kWasmI32);  // pad

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

TestImported(kWasmI32, 300.1, 300);
TestImported(kWasmF32, 87234.87238, Math.fround(87234.87238));
TestImported(kWasmF64, 77777.88888, 77777.88888);


function TestExported(type, val, expected) {
  print("TestExported " + type + "(" + val +")" + " = " + expected);
  var builder = new WasmModuleBuilder();
  var sig = makeSig([type], []);
  builder.addGlobal(kWasmI32);  // pad
  var g = builder.addGlobal(type, false)
      .exportAs("foo");
  g.init = val;
  builder.addGlobal(kWasmI32);  // pad

  var instance = builder.instantiate();
  assertEquals(expected, instance.exports.foo);
}

TestExported(kWasmI32, 455.5, 455);
TestExported(kWasmF32, -999.34343, Math.fround(-999.34343));
TestExported(kWasmF64, 87347.66666, 87347.66666);

(function TestI64Exported() {
  var builder = new WasmModuleBuilder();
  var sig = makeSig([kWasmI64], []);
  builder.addGlobal(kWasmI32);  // pad
  var g = builder.addGlobal(kWasmI64, false)
      .exportAs("foo");
  g.init = 1234;
  builder.addGlobal(kWasmI32);  // pad

  assertThrows(()=> {builder.instantiate()}, WebAssembly.LinkError);
})();

function TestImportedExported(type, val, expected) {
  print("TestImportedExported " + type + "(" + val +")" + " = " + expected);
  var builder = new WasmModuleBuilder();
  var sig = makeSig([type], []);
  var i = builder.addImportedGlobal("ttt", "foo", type);
  builder.addGlobal(kWasmI32);  // pad
  var o = builder.addGlobal(type, false)
      .exportAs("bar");
  o.init_index = i;
  builder.addGlobal(kWasmI32);  // pad

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

TestImportedExported(kWasmI32, 415.5, 415);
TestImportedExported(kWasmF32, -979.34343, Math.fround(-979.34343));
TestImportedExported(kWasmF64, 81347.66666, 81347.66666);

function TestGlobalIndexSpace(type, val) {
  print("TestGlobalIndexSpace(" + val + ") = " + val);
  var builder = new WasmModuleBuilder();
  var im = builder.addImportedGlobal("nnn", "foo", type);
  assertEquals(0, im);
  var def = builder.addGlobal(type, false);
  assertEquals(1, def.index);
  def.init_index = im;

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

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

TestGlobalIndexSpace(kWasmI32, 123);
TestGlobalIndexSpace(kWasmF32, 54321.125);
TestGlobalIndexSpace(kWasmF64, 12345.678);