diff --git a/libevmjit/Runtime.cpp b/libevmjit/Runtime.cpp index 71ec944da..eb70e01d1 100644 --- a/libevmjit/Runtime.cpp +++ b/libevmjit/Runtime.cpp @@ -26,6 +26,7 @@ bytes_ref Runtime::getReturnData() const if (data < m_memory.data() || data >= m_memory.data() + m_memory.size() || size == 0) { assert(size == 0); // data can be an invalid pointer only if size is 0 + m_data.callData = nullptr; return {}; } diff --git a/libevmjit/RuntimeData.h b/libevmjit/RuntimeData.h index 846f7a238..58d68db8a 100644 --- a/libevmjit/RuntimeData.h +++ b/libevmjit/RuntimeData.h @@ -47,7 +47,7 @@ struct RuntimeData i256 difficulty; i256 gasLimit; uint64_t number = 0; - uint64_t timestamp = 0; + int64_t timestamp = 0; byte const* code = nullptr; uint64_t codeSize = 0; }; diff --git a/libevmjit/interface.cpp b/libevmjit/interface.cpp index f8fa27408..506647d13 100644 --- a/libevmjit/interface.cpp +++ b/libevmjit/interface.cpp @@ -1,34 +1,36 @@ -#include "interface.h" -#include #include "ExecutionEngine.h" extern "C" { -evmjit_result evmjit_run(void* _data, void* _env) -{ - using namespace dev::eth::jit; +using namespace dev::eth::jit; - auto data = static_cast(_data); +void* evmjit_create() noexcept +{ + return new(std::nothrow) ExecutionEngine; +} - ExecutionEngine engine; +void evmjit_destroy(ExecutionEngine* _engine) noexcept +{ + delete _engine; +} - auto codePtr = data->code; - auto codeSize = data->codeSize; - bytes bytecode; - bytecode.insert(bytecode.end(), codePtr, codePtr + codeSize); +int evmjit_run(ExecutionEngine* _engine, RuntimeData* _data, Env* _env) noexcept +{ + try + { + auto codePtr = _data->code; + auto codeSize = _data->codeSize; + bytes bytecode; + bytecode.insert(bytecode.end(), codePtr, codePtr + codeSize); - auto returnCode = engine.run(bytecode, data, static_cast(_env)); - evmjit_result result = {static_cast(returnCode), 0, nullptr}; - if (returnCode == ReturnCode::Return && std::get<0>(engine.returnData)) + auto returnCode = _engine->run(bytecode, _data, _env); + return static_cast(returnCode); + } + catch(...) { - // TODO: Optimized returning data. Allocating memory on client side by callback function might be a good idea - result.returnDataSize = std::get<1>(engine.returnData); - result.returnData = std::malloc(result.returnDataSize); - std::memcpy(result.returnData, std::get<0>(engine.returnData), result.returnDataSize); + return static_cast(ReturnCode::UnexpectedException); } - - return result; } } diff --git a/libevmjit/interface.h b/libevmjit/interface.h index 4cac78d56..4f4d56610 100644 --- a/libevmjit/interface.h +++ b/libevmjit/interface.h @@ -1,19 +1,12 @@ -#include -#include #ifdef __cplusplus extern "C" { #endif -typedef struct evmjit_result -{ - int32_t returnCode; - uint64_t returnDataSize; - void* returnData; +void* evmjit_create(); +int evmjit_run(void* _jit, void* _data, void* _env); +void evmjit_destroy(void* _jit); -} evmjit_result; - -evmjit_result evmjit_run(void* _data, void* _env); #ifdef __cplusplus }