Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java: allocate execute result in C #558

Merged
merged 1 commit into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member Author

@axic axic Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still broken because it just copies the evmc_result struct, but the pointers to output are left loose. We need to move to a proper Java Result class, i.e. #555.

However, the PR does not change semantics.

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