From 61d533aad7d6e8fc80dd6624744d24e305c83d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 22 Sep 2017 18:50:20 +0200 Subject: [PATCH] EVM-C: Move ABI version from factory to instance --- examples/capi.c | 6 +++--- examples/examplevm.c | 12 ++++++++---- include/evm.h | 12 +++++++----- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/examples/capi.c b/examples/capi.c index 7a67c45..a8092f7 100644 --- a/examples/capi.c +++ b/examples/capi.c @@ -128,10 +128,10 @@ static const struct evm_host example_host = { /// Example how the API is supposed to be used. int main(int argc, char *argv[]) { 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"; const size_t code_size = sizeof(code); diff --git a/examples/examplevm.c b/examples/examplevm.c index 7840db0..1f938b7 100644 --- a/examples/examplevm.c +++ b/examples/examplevm.c @@ -112,17 +112,21 @@ static struct evm_result execute(struct evm_instance* instance, 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 evm_instance* interface = &vm->instance; - interface->destroy = evm_destroy; - interface->execute = execute; - interface->set_option = evm_set_option; + memcpy(interface, &init, sizeof(init)); vm->host = host; return interface; } struct evm_factory examplevm_get_factory() { - struct evm_factory factory = {EVM_ABI_VERSION, evm_create}; + struct evm_factory factory = {evm_create}; return factory; } diff --git a/include/evm.h b/include/evm.h index 406e020..acf30d9 100644 --- a/include/evm.h +++ b/include/evm.h @@ -437,6 +437,13 @@ typedef void (*evm_prepare_code_fn)(struct evm_instance* instance, /// /// Defines the base struct of the EVM implementation. 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. evm_destroy_fn destroy; @@ -463,11 +470,6 @@ struct evm_instance { /// /// Provides ABI protection and method to create an EVM instance. 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. evm_create_fn create;