tools: Add output stream operator for evmc_status_code

This commit is contained in:
Paweł Bylica 2019-12-09 20:42:54 +01:00
parent f0bb4af926
commit fa9e3d3b30
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
2 changed files with 78 additions and 0 deletions

View File

@ -3,6 +3,7 @@
// Licensed under the Apache License, Version 2.0.
#include "utils.hpp"
#include <ostream>
#include <stdexcept>
namespace evmc
@ -45,4 +46,75 @@ std::string hex(const uint8_t* data, size_t size)
return str;
}
std::ostream& operator<<(std::ostream& os, evmc_status_code status_code)
{
const char* s = nullptr;
switch (status_code)
{
case EVMC_SUCCESS:
s = "success";
break;
case EVMC_FAILURE:
s = "failure";
break;
case EVMC_REVERT:
s = "revert";
break;
case EVMC_OUT_OF_GAS:
s = "out of gas";
break;
case EVMC_INVALID_INSTRUCTION:
s = "invalid instruction";
break;
case EVMC_UNDEFINED_INSTRUCTION:
s = "undefined instruction";
break;
case EVMC_STACK_OVERFLOW:
s = "stack overflow";
break;
case EVMC_STACK_UNDERFLOW:
s = "stack underflow";
break;
case EVMC_BAD_JUMP_DESTINATION:
s = "bad jump destination";
break;
case EVMC_INVALID_MEMORY_ACCESS:
s = "invalid memory access";
break;
case EVMC_CALL_DEPTH_EXCEEDED:
s = "call depth exceeded";
break;
case EVMC_STATIC_MODE_VIOLATION:
s = "static mode violation";
break;
case EVMC_PRECOMPILE_FAILURE:
s = "precompile failure";
break;
case EVMC_CONTRACT_VALIDATION_FAILURE:
s = "contract validation failure";
break;
case EVMC_ARGUMENT_OUT_OF_RANGE:
s = "argument out of range";
break;
case EVMC_WASM_UNREACHABLE_INSTRUCTION:
s = "wasm unreachable instruction";
break;
case EVMC_WASM_TRAP:
s = "wasm trap";
break;
case EVMC_INTERNAL_ERROR:
s = "internal error";
break;
case EVMC_REJECTED:
s = "rejected";
break;
case EVMC_OUT_OF_MEMORY:
s = "out of memory";
break;
default:
throw std::invalid_argument{"invalid EVMC status code: " + std::to_string(status_code)};
}
return os << s;
}
} // namespace evmc

View File

@ -3,7 +3,9 @@
// Licensed under the Apache License, Version 2.0.
#pragma once
#include <evmc/evmc.h>
#include <cstdint>
#include <iosfwd>
#include <string>
namespace evmc
@ -27,4 +29,8 @@ bytes from_hex(const std::string& hex);
/// Encodes bytes as hex string.
std::string hex(const uint8_t* data, size_t size);
/// Output stream operator for evmc_status_code.
std::ostream& operator<<(std::ostream& os, evmc_status_code status_code);
} // namespace evmc