Introduce evmc_set_option_result enum

This commit is contained in:
Alex Beregszaszi 2018-09-05 18:24:02 +01:00
parent 83a679c524
commit 50c254fb98
5 changed files with 31 additions and 18 deletions

View File

@ -15,9 +15,9 @@ package evmc
#include <stdlib.h>
#include <string.h>
static inline int set_option(struct evmc_instance* instance, char* name, char* value)
static inline enum evmc_set_option_result set_option(struct evmc_instance* instance, char* name, char* value)
{
int ret = evmc_set_option(instance, name, value);
enum evmc_set_option_result ret = evmc_set_option(instance, name, value);
free(name);
free(value);
return ret;
@ -188,7 +188,7 @@ func (instance *Instance) Version() string {
func (instance *Instance) SetOption(name string, value string) (err error) {
r := C.set_option(instance.handle, C.CString(name), C.CString(value))
if r != 1 {
if r != C.EVMC_SET_OPTION_SUCCESS {
err = fmt.Errorf("evmc: option '%s' not accepted", name)
}
return err

View File

@ -31,19 +31,21 @@ static void destroy(struct evmc_instance* evm)
/// Example options.
///
/// VMs are allowed to omit this function implementation.
static int set_option(struct evmc_instance* instance, char const* name, char const* value)
static enum evmc_set_option_result set_option(struct evmc_instance* instance,
char const* name,
char const* value)
{
struct example_vm* vm = (struct example_vm*)instance;
if (strcmp(name, "verbose") == 0)
{
long int v = strtol(value, NULL, 0);
if (v > INT_MAX || v < INT_MIN)
return 0;
return EVMC_SET_OPTION_INVALID_VALUE;
vm->verbose = (int)v;
return 1;
return EVMC_SET_OPTION_SUCCESS;
}
return 0;
return EVMC_SET_OPTION_INVALID_NAME;
}
static void release_result(struct evmc_result const* result)

View File

@ -654,6 +654,15 @@ struct evmc_instance;
*/
typedef void (*evmc_destroy_fn)(struct evmc_instance* evm);
/**
* Possible outcomes of evmc_set_option.
*/
enum evmc_set_option_result
{
EVMC_SET_OPTION_SUCCESS = 0,
EVMC_SET_OPTION_INVALID_NAME = 1,
EVMC_SET_OPTION_INVALID_VALUE = 2
};
/**
* Configures the EVM instance.
@ -666,9 +675,11 @@ typedef void (*evmc_destroy_fn)(struct evmc_instance* evm);
* @param evm The EVM instance to be configured.
* @param name The option name. NULL-terminated string. Cannot be NULL.
* @param value The new option value. NULL-terminated string. Cannot be NULL.
* @return 1 if the option set successfully, 0 otherwise.
* @return The outcome of the operation.
*/
typedef int (*evmc_set_option_fn)(struct evmc_instance* evm, char const* name, char const* value);
typedef enum evmc_set_option_result (*evmc_set_option_fn)(struct evmc_instance* evm,
char const* name,
char const* value);
/**

View File

@ -56,13 +56,13 @@ static inline void evmc_destroy(struct evmc_instance* instance)
*
* @see evmc_set_option_fn
*/
static inline int evmc_set_option(struct evmc_instance* instance,
char const* name,
char const* value)
static inline enum evmc_set_option_result evmc_set_option(struct evmc_instance* instance,
char const* name,
char const* value)
{
if (instance->set_option)
return instance->set_option(instance, name, value);
return 0;
return EVMC_SET_OPTION_INVALID_NAME;
}
/**

View File

@ -64,10 +64,10 @@ TEST_F(evmc_vm_test, set_option_unknown)
{
if (vm->set_option)
{
int r = vm->set_option(vm, "unknown_option_csk9twq", "v");
EXPECT_EQ(r, 0);
enum evmc_set_option_result r = vm->set_option(vm, "unknown_option_csk9twq", "v");
EXPECT_EQ(r, EVMC_SET_OPTION_INVALID_NAME);
r = vm->set_option(vm, "unknown_option_csk9twq", "x");
EXPECT_EQ(r, 0);
EXPECT_EQ(r, EVMC_SET_OPTION_INVALID_NAME);
}
}
@ -75,8 +75,8 @@ TEST_F(evmc_vm_test, set_option_empty_value)
{
if (vm->set_option)
{
int r = vm->set_option(vm, "unknown_option_csk9twq", nullptr);
EXPECT_EQ(r, 0);
enum evmc_set_option_result r = vm->set_option(vm, "unknown_option_csk9twq", nullptr);
EXPECT_EQ(r, EVMC_SET_OPTION_INVALID_NAME);
}
}