Commit 371dd417 authored by Jakob Kummerow's avatar Jakob Kummerow Committed by Commit Bot

[wasm-c-api] Add reflection test

Change-Id: I7bce20f1f8b0a2ca2fe86cd65905e9ea65134e27
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1691027
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62629}
parent 9c6cb515
......@@ -234,6 +234,7 @@ WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
global_imports_(zone),
global_exports_(zone),
memory_exports_(zone),
table_exports_(zone),
functions_(zone),
data_segments_(zone),
indirect_functions_(zone),
......@@ -348,6 +349,11 @@ void WasmModuleBuilder::AddExportedMemory(Vector<const char> name,
memory_exports_.push_back({name, index});
}
void WasmModuleBuilder::AddExportedTable(Vector<const char> name,
uint32_t index) {
table_exports_.push_back({name, index});
}
void WasmModuleBuilder::SetHasSharedMemory() { has_shared_memory_ = true; }
void WasmModuleBuilder::WriteTo(ZoneBuffer* buffer) const {
......@@ -501,7 +507,7 @@ void WasmModuleBuilder::WriteTo(ZoneBuffer* buffer) const {
// == emit exports ===========================================================
size_t num_exports = global_exports_.size() + function_exports_.size() +
memory_exports_.size();
memory_exports_.size() + table_exports_.size();
if (num_exports > 0) {
size_t start = EmitSection(kExportSectionCode, buffer);
buffer->write_size(num_exports);
......@@ -521,6 +527,11 @@ void WasmModuleBuilder::WriteTo(ZoneBuffer* buffer) const {
buffer->write_u8(kExternalMemory);
buffer->write_size(memory_export.memory_index);
}
for (auto table_export : table_exports_) {
buffer->write_string(table_export.name);
buffer->write_u8(kExternalTable);
buffer->write_size(table_export.table_index);
}
FixupSection(buffer, start);
}
......
......@@ -245,6 +245,7 @@ class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
const WasmInitExpr& init, Vector<const char> name);
void AddExportedImport(Vector<const char> name, int import_index);
void AddExportedMemory(Vector<const char> name, uint32_t memory_index);
void AddExportedTable(Vector<const char> name, uint32_t table_index);
void SetMinMemorySize(uint32_t value);
void SetMaxMemorySize(uint32_t value);
void SetHasSharedMemory();
......@@ -292,6 +293,11 @@ class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
uint32_t memory_index;
};
struct WasmTableExport {
Vector<const char> name;
uint32_t table_index;
};
struct WasmDataSegment {
ZoneVector<byte> data;
uint32_t dest;
......@@ -305,6 +311,7 @@ class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
ZoneVector<WasmGlobalImport> global_imports_;
ZoneVector<WasmGlobalExport> global_exports_;
ZoneVector<WasmMemoryExport> memory_exports_;
ZoneVector<WasmTableExport> table_exports_;
ZoneVector<WasmFunctionBuilder*> functions_;
ZoneVector<WasmDataSegment> data_segments_;
ZoneVector<uint32_t> indirect_functions_;
......
......@@ -33,6 +33,7 @@ v8_executable("wasm_api_tests") {
"finalize.cc",
"globals.cc",
"memory.cc",
"reflect.cc",
"run-all-wasm-api-tests.cc",
"wasm-api-test.h",
]
......
// 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"
#include "src/execution/isolate.h"
#include "src/heap/heap.h"
#include "src/wasm/c-api.h"
namespace v8 {
namespace internal {
namespace wasm {
using ::wasm::ExportType;
using ::wasm::GlobalType;
using ::wasm::MemoryType;
using ::wasm::TableType;
namespace {
const char* kFuncName = "func1";
const char* kGlobalName = "global2";
const char* kTableName = "table3";
const char* kMemoryName = "memory4";
void ExpectName(const char* expected, const ::wasm::Name& name) {
size_t len = strlen(expected);
EXPECT_EQ(len, name.size());
EXPECT_EQ(0, strncmp(expected, name.get(), len));
}
} // namespace
TEST_F(WasmCapiTest, Reflect) {
// Create a module exporting a function, a global, a table, and a memory.
byte code[] = {WASM_UNREACHABLE};
ValueType types[] = {kWasmI32, kWasmAnyRef, kWasmI32,
kWasmI64, kWasmF32, kWasmF64};
FunctionSig sig(2, 4, types);
AddExportedFunction(CStrVector(kFuncName), code, sizeof(code), &sig);
builder()->AddExportedGlobal(kWasmF64, false, WasmInitExpr(0.0),
CStrVector(kGlobalName));
builder()->AllocateIndirectFunctions(12);
builder()->AddExportedTable(CStrVector(kTableName), 0);
builder()->SetMinMemorySize(1);
builder()->AddExportedMemory(CStrVector(kMemoryName), 0);
Instantiate(nullptr);
vec<ExportType*> export_types = module()->exports();
const vec<Extern*>& exports = this->exports();
EXPECT_EQ(exports.size(), export_types.size());
EXPECT_EQ(4u, exports.size());
for (size_t i = 0; i < exports.size(); i++) {
::wasm::ExternKind kind = exports[i]->kind();
const ::wasm::ExternType* extern_type = export_types[i]->type();
EXPECT_EQ(kind, extern_type->kind());
if (kind == ::wasm::EXTERN_FUNC) {
ExpectName(kFuncName, export_types[i]->name());
const FuncType* type = extern_type->func();
const vec<ValType*>& params = type->params();
EXPECT_EQ(4u, params.size());
EXPECT_EQ(::wasm::I32, params[0]->kind());
EXPECT_EQ(::wasm::I64, params[1]->kind());
EXPECT_EQ(::wasm::F32, params[2]->kind());
EXPECT_EQ(::wasm::F64, params[3]->kind());
const vec<ValType*>& results = type->results();
EXPECT_EQ(2u, results.size());
EXPECT_EQ(::wasm::I32, results[0]->kind());
EXPECT_EQ(::wasm::ANYREF, results[1]->kind());
const Func* func = exports[i]->func();
EXPECT_EQ(4u, func->param_arity());
EXPECT_EQ(2u, func->result_arity());
} else if (kind == ::wasm::EXTERN_GLOBAL) {
ExpectName(kGlobalName, export_types[i]->name());
const GlobalType* type = extern_type->global();
EXPECT_EQ(::wasm::F64, type->content()->kind());
EXPECT_EQ(::wasm::CONST, type->mutability());
} else if (kind == ::wasm::EXTERN_TABLE) {
ExpectName(kTableName, export_types[i]->name());
const TableType* type = extern_type->table();
EXPECT_EQ(::wasm::FUNCREF, type->element()->kind());
::wasm::Limits limits = type->limits();
EXPECT_EQ(12u, limits.min);
EXPECT_EQ(12u, limits.max);
} else if (kind == ::wasm::EXTERN_MEMORY) {
ExpectName(kMemoryName, export_types[i]->name());
const MemoryType* type = extern_type->memory();
::wasm::Limits limits = type->limits();
EXPECT_EQ(1u, limits.min);
EXPECT_EQ(std::numeric_limits<uint32_t>::max(), limits.max);
} else {
UNREACHABLE();
}
}
}
} // namespace wasm
} // namespace internal
} // namespace v8
......@@ -125,6 +125,7 @@ class WasmCapiTest : public ::testing::Test {
WasmModuleBuilder* builder() { return &builder_; }
Store* store() { return store_.get(); }
Module* module() { return module_.get(); }
const vec<Extern*>& exports() { return exports_; }
FunctionSig* wasm_i_i_sig() { return &wasm_i_i_sig_; }
FuncType* cpp_i_i_sig() { return cpp_i_i_sig_.get(); }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment