Skip to content

Commit

Permalink
Tiny fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Mar 23, 2021
1 parent 106186d commit 5007dea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
24 changes: 15 additions & 9 deletions bindings/java/java/src/main/java/org/ethereum/evmc/EvmcVm.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@ public final class EvmcVm implements AutoCloseable {
private static final Throwable errorLoadingEvmc;
private ByteBuffer nativeVm;

// Load the dynamic library containing the JNI bindings to EVMC.
static {
Throwable error = null;

// First try loading from global path.
try {
// Load DLL containing the JNI bindings to EVMC.
System.loadLibrary("libevmc-java");
} catch (UnsatisfiedLinkError e) {
} catch (UnsatisfiedLinkError globalLoadingError) {
String extension = null;
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
extension = "dll";
} else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) {
extension = "so";
} else if (os.contains("mac")|| os.contains("darwin")) {
} else if (os.contains("mac") || os.contains("darwin")) {
extension = "dylib";
} else {
error = e;
// Give up, because we are unsure what system we are running on.
error = globalLoadingError;
}

// Try loading the binding from the package.
if (extension != null) {
try {
Path evmcLib = Files.createTempFile("libevmc-java", extension);
Expand All @@ -43,13 +48,14 @@ public final class EvmcVm implements AutoCloseable {
evmcLib,
StandardCopyOption.REPLACE_EXISTING);
evmcLib.toFile().deleteOnExit();
// We are somewhat certain about the file, try loading it.
try {
System.load(evmcLib.toAbsolutePath().toString());
} catch (UnsatisfiedLinkError e1) {
error = e1;
} catch (UnsatisfiedLinkError packageLoadingError) {
error = packageLoadingError;
}
} catch (IOException ex) {
error = ex;
} catch (IOException packageCreationError) {
error = packageCreationError;
}
}
}
Expand All @@ -73,7 +79,7 @@ public static boolean isAvailable() {
*/
public static EvmcVm create(String filename) throws EvmcLoaderException {
if (!isAvailable()) {
throw new EvmcLoaderException("Cannot load EVMC native library", errorLoadingEvmc);
throw new EvmcLoaderException("EVMC JNI binding library failed to load", errorLoadingEvmc);
}
return new EvmcVm(filename);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import org.junit.jupiter.api.Test;

final class EvmcTest {
private static final String extension;
private static final String exampleVmPath;

static {
String extension = null;
String os = System.getProperty("os.name", "generic").toLowerCase();
if (os.contains("mac") || os.contains("darwin")) {
extension = "dylib";
Expand Down

0 comments on commit 5007dea

Please sign in to comment.