Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capi: Add static assertions for FizzyValueType* vs. fizzy:ValType::* #821

Merged
merged 3 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/fizzy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ target_sources(
asserts.hpp
cxx20/bit.hpp
cxx20/span.hpp
cxx23/utility.hpp
bytes.hpp
capi.cpp
constexpr_vector.hpp
Expand Down
8 changes: 8 additions & 0 deletions lib/fizzy/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "cxx20/bit.hpp"
#include "cxx23/utility.hpp"
#include "execute.hpp"
#include "instantiate.hpp"
#include "parser.hpp"
Expand Down Expand Up @@ -95,6 +96,13 @@ inline const fizzy::Module* unwrap(const FizzyModule* module) noexcept
}

static_assert(sizeof(FizzyValueType) == sizeof(fizzy::ValType));
static_assert(FizzyValueTypeI32 == fizzy::to_underlying(fizzy::ValType::i32));
static_assert(FizzyValueTypeI64 == fizzy::to_underlying(fizzy::ValType::i64));
static_assert(FizzyValueTypeF32 == fizzy::to_underlying(fizzy::ValType::f32));
static_assert(FizzyValueTypeF64 == fizzy::to_underlying(fizzy::ValType::f64));
static_assert(FizzyValueTypeVoid == 0);
static_assert(std::is_same_v<decltype(fizzy::to_underlying(fizzy::ValType::i32)),
std::remove_const<decltype(FizzyValueTypeI32)>::type>);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this overkill?


inline const FizzyValueType* wrap(const fizzy::ValType* value_types) noexcept
{
Expand Down
18 changes: 18 additions & 0 deletions lib/fizzy/cxx23/utility.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Fizzy: A fast WebAssembly interpreter
// Copyright 2022 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <type_traits>

namespace fizzy
{
/// C++14 version of the proposed C++23 feature
/// https://en.cppreference.com/w/cpp/utility/to_underlying
template <typename T>
constexpr std::underlying_type_t<T> to_underlying(T value) noexcept
{
return static_cast<std::underlying_type_t<T>>(value);
}
} // namespace fizzy
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ target_sources(
constexpr_vector_test.cpp
cxx20_bit_test.cpp
cxx20_span_test.cpp
cxx23_utility_test.cpp
end_to_end_test.cpp
execute_call_depth_test.cpp
execute_call_test.cpp
Expand Down
44 changes: 44 additions & 0 deletions test/unittests/cxx23_utility_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Fizzy: A fast WebAssembly interpreter
// Copyright 2022 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

#include "cxx23/utility.hpp"
#include "stack.hpp"
#include <gtest/gtest.h>
#include <array>
#include <vector>

using namespace fizzy;
using namespace testing;

TEST(cxx23_utility, to_underlying)
{
enum class A
{
A = 0xff
};
static_assert(std::is_same_v<decltype(to_underlying(A::A)), int>);
EXPECT_EQ(to_underlying(A::A), 0xff);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add `static_assert(std::is_same_v<decltype(to_underlying(A::A)), int>);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specs said it is at least int. So this may fail on some platforms?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also did you know that the C enum (decltype(C::A)) defaults to unsigned int?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be bigger than int only if some values don't fit int

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For scoped enumerations (enum class) the implicit underlying type is int. In case value do not fit, this is compile error. https://godbolt.org/z/1x8ffaadW.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we are adding C++ compiler compliance tests at this point 😅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to know if this returns the same type as in the enum declaration. If you want to be explicit, you can define it as enum class A : int.


enum class A8 : uint8_t
{
A = 0xff
};
static_assert(std::is_same_v<decltype(to_underlying(A8::A)), uint8_t>);
EXPECT_EQ(to_underlying(A8::A), 0xff);

enum class A32 : uint32_t
{
A = 0xff,
B = 0xffffffff
};
static_assert(std::is_same_v<decltype(to_underlying(A32::A)), uint32_t>);
EXPECT_EQ(to_underlying(A32::A), 0xff);
EXPECT_EQ(to_underlying(A32::B), 0xffffffff);

enum C
{
A = 0xff
};
EXPECT_EQ(to_underlying(C::A), 0xff);
}
axic marked this conversation as resolved.
Show resolved Hide resolved