diff --git a/bindings/java/c/evmc-vm.c b/bindings/java/c/evmc-vm.c index 53a8ddfc7..12177ddc9 100644 --- a/bindings/java/c/evmc-vm.c +++ b/bindings/java/c/evmc-vm.c @@ -78,14 +78,27 @@ JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_destroy(JNIEnv* jenv, evmc_destroy(evm); } -JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_execute(JNIEnv* jenv, - jclass jcls, - jobject jevm, - jobject jcontext, - jint jrev, - jobject jmsg, - jobject jcode, - jobject jresult) +static jobject AllocateDirect(JNIEnv* jenv, size_t capacity) +{ + const char java_class_name[] = "java/nio/ByteBuffer"; + const char java_method_name[] = "allocateDirect"; + const char java_method_signature[] = "(I)Ljava/nio/ByteBuffer;"; + + jclass jcls = (*jenv)->FindClass(jenv, java_class_name); + assert(jcls != NULL); + jmethodID method = + (*jenv)->GetStaticMethodID(jenv, jcls, java_method_name, java_method_signature); + assert(method != NULL); + return (*jenv)->CallStaticObjectMethod(jenv, jcls, method, capacity); +} + +JNIEXPORT jobject JNICALL Java_org_ethereum_evmc_EvmcVm_execute(JNIEnv* jenv, + jclass jcls, + jobject jevm, + jobject jcontext, + jint jrev, + jobject jmsg, + jobject jcode) { (void)jcls; struct evmc_message* msg = (struct evmc_message*)(*jenv)->GetDirectBufferAddress(jenv, jmsg); @@ -95,11 +108,14 @@ JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_execute(JNIEnv* jenv, struct evmc_vm* evm = (struct evmc_vm*)(*jenv)->GetDirectBufferAddress(jenv, jevm); assert(evm != NULL); const struct evmc_host_interface* host = evmc_java_get_host_interface(); + jobject jresult = AllocateDirect(jenv, sizeof(struct evmc_result)); + assert(jresult != NULL); struct evmc_result* result = (struct evmc_result*)(*jenv)->GetDirectBufferAddress(jenv, jresult); assert(result != NULL); *result = evmc_execute(evm, host, (struct evmc_host_context*)jcontext, (enum evmc_revision)jrev, msg, code, code_size); + return jresult; } JNIEXPORT jint JNICALL Java_org_ethereum_evmc_EvmcVm_get_1capabilities(JNIEnv* jenv, @@ -130,10 +146,3 @@ JNIEXPORT jint JNICALL Java_org_ethereum_evmc_EvmcVm_set_1option(JNIEnv* jenv, (*jenv)->ReleaseStringUTFChars(jenv, jval, value); return (jint)option_result; } - -JNIEXPORT jint JNICALL Java_org_ethereum_evmc_EvmcVm_get_1result_1size(JNIEnv* jenv, jclass jcls) -{ - (void)jenv; - (void)jcls; - return sizeof(struct evmc_result); -} diff --git a/bindings/java/java/src/main/java/org/ethereum/evmc/EvmcVm.java b/bindings/java/java/src/main/java/org/ethereum/evmc/EvmcVm.java index bbdaf3a77..c5de454b7 100644 --- a/bindings/java/java/src/main/java/org/ethereum/evmc/EvmcVm.java +++ b/bindings/java/java/src/main/java/org/ethereum/evmc/EvmcVm.java @@ -143,13 +143,8 @@ public String version() { * *

This is a mandatory method and MUST NOT be set to NULL. */ - private static native void execute( - ByteBuffer nativeVm, - HostContext context, - int rev, - ByteBuffer msg, - ByteBuffer code, - ByteBuffer result); + private static native ByteBuffer execute( + ByteBuffer nativeVm, HostContext context, int rev, ByteBuffer msg, ByteBuffer code); /** * Function is a wrapper around native execute. @@ -158,10 +153,7 @@ private static native void execute( */ public synchronized ByteBuffer execute( HostContext context, int rev, ByteBuffer msg, ByteBuffer code) { - int resultSize = get_result_size(); - ByteBuffer result = ByteBuffer.allocateDirect(resultSize); - execute(nativeVm, context, rev, msg, code, result); - return result; + return execute(nativeVm, context, rev, msg, code); } /** @@ -193,9 +185,6 @@ public int set_option(String name, String value) { return set_option(nativeVm, name, value); } - /** get size of result struct */ - private static native int get_result_size(); - /** This method cleans up resources. */ @Override public void close() {