summaryrefslogtreecommitdiff
path: root/deps/v8/test/wasm-api-tests/globals.cc
blob: d47f326cc4a50ed9be6bdf5e274cebc3670b5647 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2019 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.

#include "test/wasm-api-tests/wasm-api-test.h"

namespace v8 {
namespace internal {
namespace wasm {

using ::wasm::GlobalType;

TEST_F(WasmCapiTest, Globals) {
  const bool kMutable = true;
  const bool kImmutable = false;

  // Define imported and exported globals in the module.
  const uint32_t cfi_index =
      builder()->AddGlobalImport(CStrVector("const f32"), kWasmF32, kImmutable);
  const uint32_t cii_index =
      builder()->AddGlobalImport(CStrVector("const i64"), kWasmI64, kImmutable);
  const uint32_t vfi_index =
      builder()->AddGlobalImport(CStrVector("var f32"), kWasmF32, kMutable);
  const uint32_t vii_index =
      builder()->AddGlobalImport(CStrVector("var i64"), kWasmI64, kMutable);
  const int kNumImported = 4;

  const uint32_t cfe_index =
      kNumImported + builder()->AddExportedGlobal(kWasmF32, kImmutable,
                                                  WasmInitExpr(5.f),
                                                  CStrVector("const f32"));
  const uint32_t cie_index =
      kNumImported + builder()->AddExportedGlobal(kWasmI64, kImmutable,
                                                  WasmInitExpr(int64_t{6}),
                                                  CStrVector("const i64"));
  const uint32_t vfe_index =
      kNumImported + builder()->AddExportedGlobal(kWasmF32, kMutable,
                                                  WasmInitExpr(7.f),
                                                  CStrVector("var f32"));
  const uint32_t vie_index =
      kNumImported + builder()->AddExportedGlobal(kWasmI64, kMutable,
                                                  WasmInitExpr(int64_t{8}),
                                                  CStrVector("var i64"));

  // Define functions for inspecting globals.
  ValueType f32_type[] = {kWasmF32};
  ValueType i64_type[] = {kWasmI64};
  FunctionSig return_f32(1, 0, f32_type);
  FunctionSig return_i64(1, 0, i64_type);
  byte gcfi[] = {WASM_GET_GLOBAL(cfi_index)};
  AddExportedFunction(CStrVector("get const f32 import"), gcfi, sizeof(gcfi),
                      &return_f32);
  byte gcii[] = {WASM_GET_GLOBAL(cii_index)};
  AddExportedFunction(CStrVector("get const i64 import"), gcii, sizeof(gcii),
                      &return_i64);
  byte gvfi[] = {WASM_GET_GLOBAL(vfi_index)};
  AddExportedFunction(CStrVector("get var f32 import"), gvfi, sizeof(gvfi),
                      &return_f32);
  byte gvii[] = {WASM_GET_GLOBAL(vii_index)};
  AddExportedFunction(CStrVector("get var i64 import"), gvii, sizeof(gvii),
                      &return_i64);

  byte gcfe[] = {WASM_GET_GLOBAL(cfe_index)};
  AddExportedFunction(CStrVector("get const f32 export"), gcfe, sizeof(gcfe),
                      &return_f32);
  byte gcie[] = {WASM_GET_GLOBAL(cie_index)};
  AddExportedFunction(CStrVector("get const i64 export"), gcie, sizeof(gcie),
                      &return_i64);
  byte gvfe[] = {WASM_GET_GLOBAL(vfe_index)};
  AddExportedFunction(CStrVector("get var f32 export"), gvfe, sizeof(gvfe),
                      &return_f32);
  byte gvie[] = {WASM_GET_GLOBAL(vie_index)};
  AddExportedFunction(CStrVector("get var i64 export"), gvie, sizeof(gvie),
                      &return_i64);

  // Define functions for manipulating globals.
  FunctionSig param_f32(0, 1, f32_type);
  FunctionSig param_i64(0, 1, i64_type);
  byte svfi[] = {WASM_SET_GLOBAL(vfi_index, WASM_GET_LOCAL(0))};
  AddExportedFunction(CStrVector("set var f32 import"), svfi, sizeof(svfi),
                      &param_f32);
  byte svii[] = {WASM_SET_GLOBAL(vii_index, WASM_GET_LOCAL(0))};
  AddExportedFunction(CStrVector("set var i64 import"), svii, sizeof(svii),
                      &param_i64);
  byte svfe[] = {WASM_SET_GLOBAL(vfe_index, WASM_GET_LOCAL(0))};
  AddExportedFunction(CStrVector("set var f32 export"), svfe, sizeof(svfe),
                      &param_f32);
  byte svie[] = {WASM_SET_GLOBAL(vie_index, WASM_GET_LOCAL(0))};
  AddExportedFunction(CStrVector("set var i64 export"), svie, sizeof(svie),
                      &param_i64);

  // Create imported globals.
  own<GlobalType*> const_f32_type =
      GlobalType::make(ValType::make(::wasm::F32), ::wasm::CONST);
  own<GlobalType*> const_i64_type =
      GlobalType::make(ValType::make(::wasm::I64), ::wasm::CONST);
  own<GlobalType*> var_f32_type =
      GlobalType::make(ValType::make(::wasm::F32), ::wasm::VAR);
  own<GlobalType*> var_i64_type =
      GlobalType::make(ValType::make(::wasm::I64), ::wasm::VAR);
  own<Global*> const_f32_import =
      Global::make(store(), const_f32_type.get(), Val::f32(1));
  own<Global*> const_i64_import =
      Global::make(store(), const_i64_type.get(), Val::i64(2));
  own<Global*> var_f32_import =
      Global::make(store(), var_f32_type.get(), Val::f32(3));
  own<Global*> var_i64_import =
      Global::make(store(), var_i64_type.get(), Val::i64(4));
  Extern* imports[] = {const_f32_import.get(), const_i64_import.get(),
                       var_f32_import.get(), var_i64_import.get()};

  Instantiate(imports);

  // Extract exports.
  size_t i = 0;
  Global* const_f32_export = GetExportedGlobal(i++);
  Global* const_i64_export = GetExportedGlobal(i++);
  Global* var_f32_export = GetExportedGlobal(i++);
  Global* var_i64_export = GetExportedGlobal(i++);
  Func* get_const_f32_import = GetExportedFunction(i++);
  Func* get_const_i64_import = GetExportedFunction(i++);
  Func* get_var_f32_import = GetExportedFunction(i++);
  Func* get_var_i64_import = GetExportedFunction(i++);
  Func* get_const_f32_export = GetExportedFunction(i++);
  Func* get_const_i64_export = GetExportedFunction(i++);
  Func* get_var_f32_export = GetExportedFunction(i++);
  Func* get_var_i64_export = GetExportedFunction(i++);
  Func* set_var_f32_import = GetExportedFunction(i++);
  Func* set_var_i64_import = GetExportedFunction(i++);
  Func* set_var_f32_export = GetExportedFunction(i++);
  Func* set_var_i64_export = GetExportedFunction(i++);

  // Check initial values.
  EXPECT_EQ(1.f, const_f32_import->get().f32());
  EXPECT_EQ(2, const_i64_import->get().i64());
  EXPECT_EQ(3.f, var_f32_import->get().f32());
  EXPECT_EQ(4, var_i64_import->get().i64());
  EXPECT_EQ(5.f, const_f32_export->get().f32());
  EXPECT_EQ(6, const_i64_export->get().i64());
  EXPECT_EQ(7.f, var_f32_export->get().f32());
  EXPECT_EQ(8, var_i64_export->get().i64());
  Val result[1];
  get_const_f32_import->call(nullptr, result);
  EXPECT_EQ(1.f, result[0].f32());
  get_const_i64_import->call(nullptr, result);
  EXPECT_EQ(2, result[0].i64());
  get_var_f32_import->call(nullptr, result);
  EXPECT_EQ(3.f, result[0].f32());
  get_var_i64_import->call(nullptr, result);
  EXPECT_EQ(4, result[0].i64());
  get_const_f32_export->call(nullptr, result);
  EXPECT_EQ(5.f, result[0].f32());
  get_const_i64_export->call(nullptr, result);
  EXPECT_EQ(6, result[0].i64());
  get_var_f32_export->call(nullptr, result);
  EXPECT_EQ(7.f, result[0].f32());
  get_var_i64_export->call(nullptr, result);
  EXPECT_EQ(8, result[0].i64());

  // Modify variables through the API and check again.
  var_f32_import->set(Val::f32(33));
  var_i64_import->set(Val::i64(34));
  var_f32_export->set(Val::f32(35));
  var_i64_export->set(Val::i64(36));

  EXPECT_EQ(33.f, var_f32_import->get().f32());
  EXPECT_EQ(34, var_i64_import->get().i64());
  EXPECT_EQ(35.f, var_f32_export->get().f32());
  EXPECT_EQ(36, var_i64_export->get().i64());

  get_var_f32_import->call(nullptr, result);
  EXPECT_EQ(33.f, result[0].f32());
  get_var_i64_import->call(nullptr, result);
  EXPECT_EQ(34, result[0].i64());
  get_var_f32_export->call(nullptr, result);
  EXPECT_EQ(35.f, result[0].f32());
  get_var_i64_export->call(nullptr, result);
  EXPECT_EQ(36, result[0].i64());

  // Modify variables through calls and check again.
  Val args[1];
  args[0] = Val::f32(73);
  set_var_f32_import->call(args, nullptr);
  args[0] = Val::i64(74);
  set_var_i64_import->call(args, nullptr);
  args[0] = Val::f32(75);
  set_var_f32_export->call(args, nullptr);
  args[0] = Val::i64(76);
  set_var_i64_export->call(args, nullptr);

  EXPECT_EQ(73.f, var_f32_import->get().f32());
  EXPECT_EQ(74, var_i64_import->get().i64());
  EXPECT_EQ(75.f, var_f32_export->get().f32());
  EXPECT_EQ(76, var_i64_export->get().i64());

  get_var_f32_import->call(nullptr, result);
  EXPECT_EQ(73.f, result[0].f32());
  get_var_i64_import->call(nullptr, result);
  EXPECT_EQ(74, result[0].i64());
  get_var_f32_export->call(nullptr, result);
  EXPECT_EQ(75.f, result[0].f32());
  get_var_i64_export->call(nullptr, result);
  EXPECT_EQ(76, result[0].i64());
}

}  // namespace wasm
}  // namespace internal
}  // namespace v8