Skip to content

Commit 799278b

Browse files
authored
Merge pull request #558 from ethereum/java-execute-alloc
java: allocate execute result in C
2 parents 56650cd + ee5bad7 commit 799278b

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

bindings/java/c/evmc-vm.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,27 @@ JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_destroy(JNIEnv* jenv,
7878
evmc_destroy(evm);
7979
}
8080

81-
JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_execute(JNIEnv* jenv,
82-
jclass jcls,
83-
jobject jevm,
84-
jobject jcontext,
85-
jint jrev,
86-
jobject jmsg,
87-
jobject jcode,
88-
jobject jresult)
81+
static jobject AllocateDirect(JNIEnv* jenv, size_t capacity)
82+
{
83+
const char java_class_name[] = "java/nio/ByteBuffer";
84+
const char java_method_name[] = "allocateDirect";
85+
const char java_method_signature[] = "(I)Ljava/nio/ByteBuffer;";
86+
87+
jclass jcls = (*jenv)->FindClass(jenv, java_class_name);
88+
assert(jcls != NULL);
89+
jmethodID method =
90+
(*jenv)->GetStaticMethodID(jenv, jcls, java_method_name, java_method_signature);
91+
assert(method != NULL);
92+
return (*jenv)->CallStaticObjectMethod(jenv, jcls, method, capacity);
93+
}
94+
95+
JNIEXPORT jobject JNICALL Java_org_ethereum_evmc_EvmcVm_execute(JNIEnv* jenv,
96+
jclass jcls,
97+
jobject jevm,
98+
jobject jcontext,
99+
jint jrev,
100+
jobject jmsg,
101+
jobject jcode)
89102
{
90103
(void)jcls;
91104
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,
95108
struct evmc_vm* evm = (struct evmc_vm*)(*jenv)->GetDirectBufferAddress(jenv, jevm);
96109
assert(evm != NULL);
97110
const struct evmc_host_interface* host = evmc_java_get_host_interface();
111+
jobject jresult = AllocateDirect(jenv, sizeof(struct evmc_result));
112+
assert(jresult != NULL);
98113
struct evmc_result* result =
99114
(struct evmc_result*)(*jenv)->GetDirectBufferAddress(jenv, jresult);
100115
assert(result != NULL);
101116
*result = evmc_execute(evm, host, (struct evmc_host_context*)jcontext, (enum evmc_revision)jrev,
102117
msg, code, code_size);
118+
return jresult;
103119
}
104120

105121
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,
130146
(*jenv)->ReleaseStringUTFChars(jenv, jval, value);
131147
return (jint)option_result;
132148
}
133-
134-
JNIEXPORT jint JNICALL Java_org_ethereum_evmc_EvmcVm_get_1result_1size(JNIEnv* jenv, jclass jcls)
135-
{
136-
(void)jenv;
137-
(void)jcls;
138-
return sizeof(struct evmc_result);
139-
}

bindings/java/java/src/main/java/org/ethereum/evmc/EvmcVm.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,8 @@ public String version() {
143143
*
144144
* <p>This is a mandatory method and MUST NOT be set to NULL.
145145
*/
146-
private static native void execute(
147-
ByteBuffer nativeVm,
148-
HostContext context,
149-
int rev,
150-
ByteBuffer msg,
151-
ByteBuffer code,
152-
ByteBuffer result);
146+
private static native ByteBuffer execute(
147+
ByteBuffer nativeVm, HostContext context, int rev, ByteBuffer msg, ByteBuffer code);
153148

154149
/**
155150
* Function is a wrapper around native execute.
@@ -158,10 +153,7 @@ private static native void execute(
158153
*/
159154
public synchronized ByteBuffer execute(
160155
HostContext context, int rev, ByteBuffer msg, ByteBuffer code) {
161-
int resultSize = get_result_size();
162-
ByteBuffer result = ByteBuffer.allocateDirect(resultSize);
163-
execute(nativeVm, context, rev, msg, code, result);
164-
return result;
156+
return execute(nativeVm, context, rev, msg, code);
165157
}
166158

167159
/**
@@ -193,9 +185,6 @@ public int set_option(String name, String value) {
193185
return set_option(nativeVm, name, value);
194186
}
195187

196-
/** get size of result struct */
197-
private static native int get_result_size();
198-
199188
/** This method cleans up resources. */
200189
@Override
201190
public void close() {

0 commit comments

Comments
 (0)