blob: 1a9d6fbfbe9ceeed3b1e84382382e286929f5f56 [file] [log] [blame]
Kaido Kertf309f9a2021-04-30 12:09:15 -07001// Copyright 2019 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "test/wasm-api-tests/wasm-api-test.h"
6
7#include "src/execution/isolate.h"
8#include "src/heap/heap.h"
9#include "src/wasm/c-api.h"
10
11namespace v8 {
12namespace internal {
13namespace wasm {
14
15using ::wasm::ExportType;
16using ::wasm::GlobalType;
17using ::wasm::MemoryType;
18using ::wasm::TableType;
19
20namespace {
21
22const char* kFuncName = "func1";
23const char* kGlobalName = "global2";
24const char* kTableName = "table3";
25const char* kMemoryName = "memory4";
26
27void ExpectName(const char* expected, const ::wasm::Name& name) {
28 size_t len = strlen(expected);
29 EXPECT_EQ(len, name.size());
30 EXPECT_EQ(0, strncmp(expected, name.get(), len));
31}
32
33} // namespace
34
35TEST_F(WasmCapiTest, Reflect) {
36 // Create a module exporting a function, a global, a table, and a memory.
37 byte code[] = {WASM_UNREACHABLE};
38 ValueType types[] = {kWasmI32, kWasmExternRef, kWasmI32,
39 kWasmI64, kWasmF32, kWasmF64};
40 FunctionSig sig(2, 4, types);
41 AddExportedFunction(CStrVector(kFuncName), code, sizeof(code), &sig);
42
43 builder()->AddExportedGlobal(kWasmF64, false, WasmInitExpr(0.0),
44 CStrVector(kGlobalName));
45
46 builder()->AllocateIndirectFunctions(12);
47 builder()->AddExport(CStrVector(kTableName), kExternalTable, 0);
48
49 builder()->SetMinMemorySize(1);
50 builder()->AddExport(CStrVector(kMemoryName), kExternalMemory, 0);
51
52 Instantiate(nullptr);
53
54 ownvec<ExportType> export_types = module()->exports();
55 const ownvec<Extern>& exports = this->exports();
56 EXPECT_EQ(exports.size(), export_types.size());
57 EXPECT_EQ(4u, exports.size());
58 for (size_t i = 0; i < exports.size(); i++) {
59 ::wasm::ExternKind kind = exports[i]->kind();
60 const ::wasm::ExternType* extern_type = export_types[i]->type();
61 EXPECT_EQ(kind, extern_type->kind());
62 if (kind == ::wasm::EXTERN_FUNC) {
63 ExpectName(kFuncName, export_types[i]->name());
64 const FuncType* type = extern_type->func();
65 const ownvec<ValType>& params = type->params();
66 EXPECT_EQ(4u, params.size());
67 EXPECT_EQ(::wasm::I32, params[0]->kind());
68 EXPECT_EQ(::wasm::I64, params[1]->kind());
69 EXPECT_EQ(::wasm::F32, params[2]->kind());
70 EXPECT_EQ(::wasm::F64, params[3]->kind());
71 const ownvec<ValType>& results = type->results();
72 EXPECT_EQ(2u, results.size());
73 EXPECT_EQ(::wasm::I32, results[0]->kind());
74 EXPECT_EQ(::wasm::ANYREF, results[1]->kind());
75
76 const Func* func = exports[i]->func();
77 EXPECT_EQ(4u, func->param_arity());
78 EXPECT_EQ(2u, func->result_arity());
79
80 } else if (kind == ::wasm::EXTERN_GLOBAL) {
81 ExpectName(kGlobalName, export_types[i]->name());
82 const GlobalType* type = extern_type->global();
83 EXPECT_EQ(::wasm::F64, type->content()->kind());
84 EXPECT_EQ(::wasm::CONST, type->mutability());
85
86 } else if (kind == ::wasm::EXTERN_TABLE) {
87 ExpectName(kTableName, export_types[i]->name());
88 const TableType* type = extern_type->table();
89 EXPECT_EQ(::wasm::FUNCREF, type->element()->kind());
90 ::wasm::Limits limits = type->limits();
91 EXPECT_EQ(12u, limits.min);
92 EXPECT_EQ(12u, limits.max);
93
94 } else if (kind == ::wasm::EXTERN_MEMORY) {
95 ExpectName(kMemoryName, export_types[i]->name());
96 const MemoryType* type = extern_type->memory();
97 ::wasm::Limits limits = type->limits();
98 EXPECT_EQ(1u, limits.min);
99 EXPECT_EQ(std::numeric_limits<uint32_t>::max(), limits.max);
100
101 } else {
102 UNREACHABLE();
103 }
104 }
105}
106
107} // namespace wasm
108} // namespace internal
109} // namespace v8