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 bindings refactoring #490

Merged
merged 4 commits into from
May 7, 2020
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
33 changes: 17 additions & 16 deletions bindings/java/c/evmc-vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include "evmc/loader.h"
#include "host.h"

static void throw_java_assert(JNIEnv* jenv, const char* msg)
{
jclass jcls = (*jenv)->FindClass(jenv, "java/lang/AssertionError");
(*jenv)->ThrowNew(jenv, jcls, msg);
}

JNIEXPORT jobject JNICALL Java_org_ethereum_evmc_EvmcVm_init(JNIEnv* jenv,
jclass jcls,
jstring jfilename)
Expand All @@ -15,23 +21,16 @@ JNIEXPORT jobject JNICALL Java_org_ethereum_evmc_EvmcVm_init(JNIEnv* jenv,
jint rs = set_jvm(jenv);
assert(rs == JNI_OK);
// load the EVM
const char* filename = (*jenv)->GetStringUTFChars(jenv, jfilename, 0);
if (filename != NULL)
{
enum evmc_loader_error_code loader_error;
evm = evmc_load_and_create(filename, &loader_error);
if (evm == NULL || loader_error != EVMC_LOADER_SUCCESS)
{
const char* error_msg = evmc_last_error_msg();
jclass jclazz = (*jenv)->FindClass(jenv, "java/lang/AssertionError");
(*jenv)->ThrowNew(jenv, jclazz, error_msg ? error_msg : "Loading EVMC VM failed");
}
(*jenv)->ReleaseStringUTFChars(jenv, jfilename, filename);
}
else
const char* filename = (*jenv)->GetStringUTFChars(jenv, jfilename, NULL);
if (filename == NULL)
throw_java_assert(jenv, "JNI Error: filename cannot be NULL");
enum evmc_loader_error_code loader_error;
evm = evmc_load_and_create(filename, &loader_error);
(*jenv)->ReleaseStringUTFChars(jenv, jfilename, filename);
if (loader_error != EVMC_LOADER_SUCCESS)
{
jclass jclazz = (*jenv)->FindClass(jenv, "java/lang/AssertionError");
(*jenv)->ThrowNew(jenv, jclazz, "JNI Error: filename cannot be NULL. \n");
const char* error_msg = evmc_last_error_msg();
throw_java_assert(jenv, error_msg ? error_msg : "Loading EVMC VM failed");
}
jobject jresult = (*jenv)->NewDirectByteBuffer(jenv, (void*)evm, sizeof(struct evmc_vm));
assert(jresult != NULL);
Expand Down Expand Up @@ -151,6 +150,8 @@ JNIEXPORT jint JNICALL Java_org_ethereum_evmc_EvmcVm_set_1option(JNIEnv* jenv,
assert(evm != NULL);
const char* name = (*jenv)->GetStringUTFChars(jenv, jname, 0);
const char* value = (*jenv)->GetStringUTFChars(jenv, jvalue, 0);
assert(name != NULL);
assert(value != NULL);
enum evmc_set_option_result option_result = evmc_set_option(evm, name, value);
(*jenv)->ReleaseStringUTFChars(jenv, jname, name);
(*jenv)->ReleaseStringUTFChars(jenv, jvalue, value);
Expand Down
Loading