Skip to content

Commit

Permalink
java: allocate execute result in C
Browse files Browse the repository at this point in the history
  • Loading branch information
axic authored and chfast committed Apr 26, 2021
1 parent 56650cd commit ee5bad7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
39 changes: 24 additions & 15 deletions bindings/java/c/evmc-vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
17 changes: 3 additions & 14 deletions bindings/java/java/src/main/java/org/ethereum/evmc/EvmcVm.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,8 @@ public String version() {
*
* <p>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.
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit ee5bad7

Please sign in to comment.