ErrCode wraps a compact uint32_t error code used throughout WasmEdge. Note: Parameterized error messages (e.g., embedding function names or indices into error strings) are intentionally NOT stored in this class. Adding a std::string member would prevent ErrCode from being constexpr and introduce implicit heap allocations on every copy in Expected return paths, causing significant perf
| 64 | /// ~16% debug). See PR #4625 for benchmarks and discussion. |
| 65 | /// Contextual debug information should be logged separately via spdlog instead. |
| 66 | class ErrCode { |
| 67 | public: |
| 68 | /// Error code C++ enumeration class. |
| 69 | enum class Value : uint32_t { |
| 70 | #define UseErrCode |
| 71 | #define Line(NAME, VALUE, STRING) NAME = VALUE, |
| 72 | #include "enum.inc" |
| 73 | #undef Line |
| 74 | #undef UseErrCode |
| 75 | }; |
| 76 | |
| 77 | constexpr ErrCategory getCategory() const noexcept { |
| 78 | return static_cast<ErrCategory>(static_cast<uint8_t>(Inner.Num >> 24)); |
| 79 | } |
| 80 | constexpr uint32_t getCode() const noexcept { |
| 81 | return Inner.Num & 0x00FFFFFFU; |
| 82 | } |
| 83 | constexpr ErrCode::Value getEnum() const noexcept { |
| 84 | return getCategory() != ErrCategory::WASM |
| 85 | ? ErrCode::Value::UserDefError |
| 86 | : static_cast<ErrCode::Value>(getCode()); |
| 87 | } |
| 88 | constexpr WasmPhase getErrCodePhase() const noexcept { |
| 89 | return getCategory() != ErrCategory::WASM |
| 90 | ? WasmPhase::UserDefined |
| 91 | : static_cast<WasmPhase>((getCode() >> 8) & 0x0FU); |
| 92 | } |
| 93 | |
| 94 | constexpr ErrCode() noexcept : Inner(0) {} |
| 95 | constexpr ErrCode(const ErrCode &E) noexcept : Inner(E.Inner.Num) {} |
| 96 | constexpr ErrCode(const ErrCode::Value E) noexcept : Inner(E) {} |
| 97 | constexpr ErrCode(const uint32_t N) noexcept |
| 98 | : Inner((static_cast<uint32_t>(ErrCategory::UserLevelError) << 24) + |
| 99 | (N & 0x00FFFFFFU)) {} |
| 100 | constexpr ErrCode(const ErrCategory C, const uint32_t N) noexcept |
| 101 | : Inner((static_cast<uint32_t>(C) << 24) + (N & 0x00FFFFFFU)) {} |
| 102 | |
| 103 | friend constexpr bool operator==(const ErrCode &LHS, |
| 104 | const ErrCode::Value &RHS) noexcept { |
| 105 | return LHS.Inner.Code == RHS; |
| 106 | } |
| 107 | friend constexpr bool operator==(const ErrCode::Value &LHS, |
| 108 | const ErrCode &RHS) noexcept { |
| 109 | return RHS.Inner.Code == LHS; |
| 110 | } |
| 111 | friend constexpr bool operator==(const ErrCode &LHS, |
| 112 | const ErrCode &RHS) noexcept { |
| 113 | return LHS.Inner.Num == RHS.Inner.Num; |
| 114 | } |
| 115 | friend constexpr bool operator!=(const ErrCode &LHS, |
| 116 | const ErrCode::Value &RHS) noexcept { |
| 117 | return !(LHS == RHS); |
| 118 | } |
| 119 | friend constexpr bool operator!=(const ErrCode::Value &LHS, |
| 120 | const ErrCode &RHS) noexcept { |
| 121 | return !(LHS == RHS); |
| 122 | } |
| 123 | friend constexpr bool operator!=(const ErrCode &LHS, |