Skip to content

Commit

Permalink
java: introduce GetDirectBuffer helper
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Oct 12, 2020
1 parent 75376e2 commit a10ebed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 2 additions & 3 deletions bindings/java/c/evmc-vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_execute(JNIEnv* jenv,
(void)jcls;
struct evmc_message* msg = (struct evmc_message*)(*jenv)->GetDirectBufferAddress(jenv, jmsg);
assert(msg != NULL);
size_t code_size = (size_t)(*jenv)->GetDirectBufferCapacity(jenv, jcode);
const uint8_t* code = (uint8_t*)(*jenv)->GetDirectBufferAddress(jenv, jcode);
assert(code != NULL);
size_t code_size;
const uint8_t* code = GetDirectBuffer(jenv, jcode, &code_size);
struct evmc_host_context context = {jcontext_index};
struct evmc_vm* evm = (struct evmc_vm*)(*jenv)->GetDirectBufferAddress(jenv, jevm);
assert(evm != NULL);
Expand Down
5 changes: 2 additions & 3 deletions bindings/java/c/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ static jbyteArray CopyDataToJava(JNIEnv* jenv, const void* ptr, size_t size)

static void CopyFromByteBuffer(JNIEnv* jenv, jobject src, void* dst, size_t size)
{
size_t src_size = (size_t)(*jenv)->GetDirectBufferCapacity(jenv, src);
size_t src_size;
const void* ptr = GetDirectBuffer(jenv, src, &src_size);
if (src_size != size)
{
jclass exception_class = (*jenv)->FindClass(jenv, "java/lang/IllegalArgumentException");
assert(exception_class != NULL);
(*jenv)->ThrowNew(jenv, exception_class, "Unexpected length.");
}
void* ptr = (*jenv)->GetDirectBufferAddress(jenv, src);
assert(ptr != NULL);
memcpy(dst, ptr, size);
}

Expand Down
11 changes: 11 additions & 0 deletions bindings/java/c/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ struct evmc_host_context
int evmc_java_set_jvm(JNIEnv*);
const struct evmc_host_interface* evmc_java_get_host_interface();

static inline void* GetDirectBuffer(JNIEnv* jenv, jobject buf, size_t* size)
{
void* ret = (uint8_t*)(*jenv)->GetDirectBufferAddress(jenv, buf);
assert(ret != NULL);
jlong jsize = (*jenv)->GetDirectBufferCapacity(jenv, buf);
assert(jsize != -1);
if (size)
*size = (size_t)jsize;
return ret;
}

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit a10ebed

Please sign in to comment.