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: clean up the EvmcVm class a bit #549

Merged
merged 4 commits into from
Oct 13, 2020
Merged

java: clean up the EvmcVm class a bit #549

merged 4 commits into from
Oct 13, 2020

Conversation

axic
Copy link
Member

@axic axic commented Oct 9, 2020

  1. Make the native methods explicitly private (they are not supposed to be available due to requiring a VM pointer).
  2. Properly return exception on loading failures.

Some references:


/**
* Function to execute a code by the VM instance.
*
* <p>This is a mandatory method and MUST NOT be set to NULL.
*/
native void execute(
private static native void execute(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this static?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it only exists once, it is the native library which can only be loaded once.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, if it's purely functional, that may work.

// Licensed under the Apache License, Version 2.0.
package org.ethereum.evmc;

public class EvmcLoaderException extends Exception {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making it extend RuntimeException - this way it's an unchecked exception.

Copy link
Member Author

@axic axic Oct 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue this must be actually checked because it is an error which should not be ignored or bubbled up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But perhaps I don't fully understand the distinction between Exception and RuntimeException -- could you explain please?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing. In Java, you must catch checked exceptions, or the code doesn't compile. You can let unchecked exceptions "unchecked", meaning you don't need to catch them explicitly. They still happen and bubble up.
The net difference is that sometimes your Java code ends up looking like a bunch of try/catch statements or throws.
This is a subject of hot debate and frankly, I'm fine either way. I just prefer RuntimeExceptions myself.
https://stackoverflow.com/questions/6115896/understanding-checked-vs-unchecked-exceptions-in-java

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! My argument for checked exception here is that this EVMC-specific loading error should be handled at the place this is integrated in, likely it would be translated into some local exception in the project?

Though I don't have a very strong opinion, fine either way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let’s do it.

@atoulme
Copy link
Collaborator

atoulme commented Oct 9, 2020

Please don't make the methods static - this would only allow one EVM in a JVM at a time. Having multiple EVMs is a good thing.

@axic
Copy link
Member Author

axic commented Oct 9, 2020

Please don't make the methods static - this would only allow one EVM in a JVM at a time. Having multiple EVMs is a good thing.

The JNI binding (libevmc-java.so) should be loaded once. Multiple EVMC VMs can be loaded and that is distinguished by the nativeVM object which the JNI side looks up and dispatches.

@axic
Copy link
Member Author

axic commented Oct 12, 2020

@atoulme okay to merge this?

@axic
Copy link
Member Author

axic commented Oct 13, 2020

@atoulme merging this now to progress with the refactoring but we can revert certain changes if it turns out to be bad.

@axic axic merged commit ad09f98 into master Oct 13, 2020
@axic axic deleted the java-host-cleanup branch October 13, 2020 22:21
@atoulme
Copy link
Collaborator

atoulme commented Oct 14, 2020

All good. Sorry for the slow response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants