EVM-C: Move ABI version from factory to instance

This commit is contained in:
Paweł Bylica 2017-09-22 18:50:20 +02:00
parent c43a59ead0
commit 61d533aad7
3 changed files with 18 additions and 12 deletions

View File

@ -128,10 +128,10 @@ static const struct evm_host example_host = {
/// Example how the API is supposed to be used. /// Example how the API is supposed to be used.
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
struct evm_factory factory = examplevm_get_factory(); struct evm_factory factory = examplevm_get_factory();
if (factory.abi_version != EVM_ABI_VERSION)
return 1; // Incompatible ABI version.
struct evm_instance* jit = factory.create(&example_host); struct evm_instance* jit = factory.create();
if (jit->abi_version != EVM_ABI_VERSION)
return 1; // Incompatible ABI version.
uint8_t const code[] = "Place some EVM bytecode here"; uint8_t const code[] = "Place some EVM bytecode here";
const size_t code_size = sizeof(code); const size_t code_size = sizeof(code);

View File

@ -112,17 +112,21 @@ static struct evm_result execute(struct evm_instance* instance,
static struct evm_instance* evm_create(const struct evm_host* host) static struct evm_instance* evm_create(const struct evm_host* host)
{ {
struct evm_instance init = {
.abi_version = EVM_ABI_VERSION,
.destroy = evm_destroy,
.execute = execute,
.set_option = evm_set_option
};
struct examplevm* vm = calloc(1, sizeof(struct examplevm)); struct examplevm* vm = calloc(1, sizeof(struct examplevm));
struct evm_instance* interface = &vm->instance; struct evm_instance* interface = &vm->instance;
interface->destroy = evm_destroy; memcpy(interface, &init, sizeof(init));
interface->execute = execute;
interface->set_option = evm_set_option;
vm->host = host; vm->host = host;
return interface; return interface;
} }
struct evm_factory examplevm_get_factory() struct evm_factory examplevm_get_factory()
{ {
struct evm_factory factory = {EVM_ABI_VERSION, evm_create}; struct evm_factory factory = {evm_create};
return factory; return factory;
} }

View File

@ -437,6 +437,13 @@ typedef void (*evm_prepare_code_fn)(struct evm_instance* instance,
/// ///
/// Defines the base struct of the EVM implementation. /// Defines the base struct of the EVM implementation.
struct evm_instance { struct evm_instance {
/// EVM-C ABI version implemented by the EVM instance.
///
/// For future use to detect ABI incompatibilities. The EVM-C ABI version
/// represented by this file is in ::EVM_ABI_VERSION.
const int abi_version;
/// Pointer to function destroying the EVM instance. /// Pointer to function destroying the EVM instance.
evm_destroy_fn destroy; evm_destroy_fn destroy;
@ -463,11 +470,6 @@ struct evm_instance {
/// ///
/// Provides ABI protection and method to create an EVM instance. /// Provides ABI protection and method to create an EVM instance.
struct evm_factory { struct evm_factory {
/// EVM-C ABI version implemented by the EVM factory and instance.
///
/// For future use to detect ABI incompatibilities. The EVM-C ABI version
/// represented by this file is in ::EVM_ABI_VERSION.
int abi_version;
/// Pointer to function creating and initializing the EVM instance. /// Pointer to function creating and initializing the EVM instance.
evm_create_fn create; evm_create_fn create;