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

Tweaks to C++ API #445

Merged
merged 3 commits into from
Nov 6, 2019
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
2 changes: 1 addition & 1 deletion examples/example_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ extern "C" {

const evmc_host_interface* example_host_get_interface()
{
return evmc::Host::get_interface();
return &evmc::Host::get_interface();
}

evmc_host_context* example_host_create_context(evmc_tx_context tx_context)
Expand Down
33 changes: 28 additions & 5 deletions include/evmc/evmc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ class result : private evmc_result
}
};

class Host;

/// @copybrief evmc_vm
///
/// This is a RAII wrapper for evmc_vm and objects of this type
Expand Down Expand Up @@ -408,6 +410,13 @@ class VM
return result{m_instance->execute(m_instance, &host, ctx, rev, &msg, code, code_size)};
}

/// Convenient variant of the VM::execute() that takes reference to evmc::Host class.
inline result execute(Host& host,
gumb0 marked this conversation as resolved.
Show resolved Hide resolved
evmc_revision rev,
const evmc_message& msg,
const uint8_t* code,
size_t code_size) noexcept;

/// Executes code without the Host context.
///
/// The same as
Expand Down Expand Up @@ -501,7 +510,10 @@ class HostContext : public HostInterface
evmc_tx_context tx_context = {};

public:
/// Implicit converting constructor from evmc_host_context.
/// Default constructor for null Host context.
HostContext() = default;

/// Constructor from the EVMC Host primitives.
HostContext(const evmc_host_interface* interface, evmc_host_context* ctx) noexcept
: host{interface}, context{ctx}
{}
Expand Down Expand Up @@ -593,8 +605,8 @@ class Host : public HostInterface
{
public:
/// Provides access to the global host interface.
/// @returns Pointer to the host interface object.
static const evmc_host_interface* get_interface() noexcept;
/// @returns Reference to the host interface object.
static const evmc_host_interface& get_interface() noexcept;

/// Converts the Host object to the opaque host context pointer.
/// @returns Pointer to evmc_host_context.
Expand All @@ -615,6 +627,17 @@ class Host : public HostInterface
}
};


inline result VM::execute(Host& host,
evmc_revision rev,
const evmc_message& msg,
const uint8_t* code,
size_t code_size) noexcept
{
return execute(Host::get_interface(), host.to_context(), rev, msg, code, code_size);
}


namespace internal
{
inline bool account_exists(evmc_host_context* h, const evmc_address* addr) noexcept
Expand Down Expand Up @@ -695,7 +718,7 @@ inline void emit_log(evmc_host_context* h,
}
} // namespace internal

inline const evmc_host_interface* Host::get_interface() noexcept
inline const evmc_host_interface& Host::get_interface() noexcept
{
static constexpr evmc_host_interface interface{
::evmc::internal::account_exists, ::evmc::internal::get_storage,
Expand All @@ -704,7 +727,7 @@ inline const evmc_host_interface* Host::get_interface() noexcept
::evmc::internal::copy_code, ::evmc::internal::selfdestruct,
::evmc::internal::call, ::evmc::internal::get_tx_context,
::evmc::internal::get_block_hash, ::evmc::internal::emit_log};
return &interface;
return interface;
}
} // namespace evmc

Expand Down
3 changes: 2 additions & 1 deletion test/unittests/test_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,10 @@ TEST(cpp, host_call)
{
// Use example host to test Host::call() method.

auto host = evmc::HostContext{}; // Use default constructor.
auto* host_interface = example_host_get_interface();
auto* host_context = example_host_create_context(evmc_tx_context{});
auto host = evmc::HostContext{host_interface, host_context};
host = evmc::HostContext{host_interface, host_context};

EXPECT_EQ(host.call({}).gas_left, 0);

Expand Down