Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit 03a4fe6

Browse files
committed
java: introduce GetDirectBuffer helper
1 parent 75376e2 commit 03a4fe6

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

bindings/java/c/evmc-vm.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_execute(JNIEnv* jenv,
9595
(void)jcls;
9696
struct evmc_message* msg = (struct evmc_message*)(*jenv)->GetDirectBufferAddress(jenv, jmsg);
9797
assert(msg != NULL);
98-
size_t code_size = (size_t)(*jenv)->GetDirectBufferCapacity(jenv, jcode);
99-
const uint8_t* code = (uint8_t*)(*jenv)->GetDirectBufferAddress(jenv, jcode);
100-
assert(code != NULL);
98+
size_t code_size;
99+
const uint8_t* code = GetDirectBuffer(jenv, jcode, &code_size);
101100
struct evmc_host_context context = {jcontext_index};
102101
struct evmc_vm* evm = (struct evmc_vm*)(*jenv)->GetDirectBufferAddress(jenv, jevm);
103102
assert(evm != NULL);

bindings/java/c/host.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ static jbyteArray CopyDataToJava(JNIEnv* jenv, const void* ptr, size_t size)
3737

3838
static void CopyFromByteBuffer(JNIEnv* jenv, jobject src, void* dst, size_t size)
3939
{
40-
size_t src_size = (size_t)(*jenv)->GetDirectBufferCapacity(jenv, src);
40+
size_t src_size;
41+
const void* ptr = GetDirectBuffer(jenv, src, &src_size);
4142
if (src_size != size)
4243
{
4344
jclass exception_class = (*jenv)->FindClass(jenv, "java/lang/IllegalArgumentException");
4445
assert(exception_class != NULL);
4546
(*jenv)->ThrowNew(jenv, exception_class, "Unexpected length.");
4647
}
47-
void* ptr = (*jenv)->GetDirectBufferAddress(jenv, src);
48-
assert(ptr != NULL);
4948
memcpy(dst, ptr, size);
5049
}
5150

bindings/java/c/host.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright 2019-2020 The EVMC Authors.
33
* Licensed under the Apache License, Version 2.0.
44
*/
5+
#inckude <assert.h>
56
#include "evmc/evmc.h"
67
#include <jni.h>
78

@@ -19,6 +20,17 @@ struct evmc_host_context
1920
int evmc_java_set_jvm(JNIEnv*);
2021
const struct evmc_host_interface* evmc_java_get_host_interface();
2122

23+
static inline void* GetDirectBuffer(JNIEnv* jenv, jobject buf, size_t* size)
24+
{
25+
void* ret = (uint8_t*)(*jenv)->GetDirectBufferAddress(jenv, buf);
26+
assert(ret != NULL);
27+
jlong buf_size = (*jenv)->GetDirectBufferCapacity(jenv, buf);
28+
assert(buf_size != -1);
29+
if (size)
30+
*size = (size_t)buf_size;
31+
return ret;
32+
}
33+
2234
#ifdef __cplusplus
2335
}
2436
#endif

0 commit comments

Comments
 (0)