mirror of https://github.com/status-im/evmc.git
EVM-C: Add examples of evm_result usages
This commit is contained in:
parent
1fe60a02ba
commit
beac0977f8
|
@ -1,8 +1,8 @@
|
||||||
add_compile_options("-Wno-extra") # Override -Wextra, I don't know better option.
|
|
||||||
|
|
||||||
add_library(example-vm STATIC examplevm.c)
|
add_library(example-vm STATIC examplevm.c)
|
||||||
target_include_directories(example-vm PRIVATE ../include)
|
target_include_directories(example-vm PRIVATE ../include)
|
||||||
|
target_compile_options(example-vm PRIVATE -Wno-extra)
|
||||||
|
|
||||||
add_executable(example-capi capi.c)
|
add_executable(example-capi capi.c)
|
||||||
target_include_directories(example-capi PRIVATE ../include)
|
target_include_directories(example-capi PRIVATE ../include)
|
||||||
target_link_libraries(example-capi PRIVATE example-vm)
|
target_link_libraries(example-capi PRIVATE example-vm)
|
||||||
|
target_compile_options(example-capi PRIVATE -Wno-extra)
|
||||||
|
|
|
@ -32,6 +32,11 @@ static void evm_release_result(struct evm_result const* result)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void free_result_output_data(struct evm_result const* result)
|
||||||
|
{
|
||||||
|
free((uint8_t*)result->output_data);
|
||||||
|
}
|
||||||
|
|
||||||
static struct evm_result evm_execute(struct evm_instance* instance,
|
static struct evm_result evm_execute(struct evm_instance* instance,
|
||||||
struct evm_env* env,
|
struct evm_env* env,
|
||||||
enum evm_mode mode,
|
enum evm_mode mode,
|
||||||
|
@ -44,8 +49,36 @@ static struct evm_result evm_execute(struct evm_instance* instance,
|
||||||
struct evm_uint256be value)
|
struct evm_uint256be value)
|
||||||
{
|
{
|
||||||
struct evm_result ret = {};
|
struct evm_result ret = {};
|
||||||
|
if (code_size == 0) {
|
||||||
|
// In case of empty code return a fancy error message.
|
||||||
|
const char* msg = mode == EVM_METROPOLIS ?
|
||||||
|
"Welcome to Metropolis!" : "Hello Ethereum!";
|
||||||
|
ret.output_data = (const uint8_t*)msg;
|
||||||
|
ret.output_size = strlen(msg);
|
||||||
|
ret.code = EVM_FAILURE;
|
||||||
|
ret.release = NULL; // We don't need to release the constant messages.
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
// Execute code and refer to callbacks: instance->query_fn()
|
struct examplevm* vm = (struct examplevm*)instance;
|
||||||
|
|
||||||
|
// Simulate executing the answering some questions from the code.
|
||||||
|
|
||||||
|
const char my_address_question[] = "What is my address?";
|
||||||
|
if (code_size == strlen(my_address_question) &&
|
||||||
|
strncmp((const char*)code, my_address_question, code_size)) {
|
||||||
|
union evm_variant query_result;
|
||||||
|
vm->query_fn(&query_result, env, EVM_ADDRESS, NULL);
|
||||||
|
static const size_t address_size = sizeof(query_result.address);
|
||||||
|
uint8_t* output_data = (uint8_t*)malloc(address_size);
|
||||||
|
memcpy(output_data, &query_result.address, address_size);
|
||||||
|
ret.code = EVM_SUCCESS;
|
||||||
|
ret.output_data = output_data;
|
||||||
|
ret.output_size = address_size;
|
||||||
|
ret.release = &free_result_output_data;
|
||||||
|
ret.internal_memory = NULL; // We don't need another pointer.
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
ret.release = evm_release_result;
|
ret.release = evm_release_result;
|
||||||
ret.code = EVM_FAILURE;
|
ret.code = EVM_FAILURE;
|
||||||
|
|
Loading…
Reference in New Issue