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

Allow enum to be used for native constants #1930

Open
dmlloyd opened this issue Oct 25, 2023 · 0 comments
Open

Allow enum to be used for native constants #1930

dmlloyd opened this issue Oct 25, 2023 · 0 comments
Labels
kind: enhancement ✨ A new feature or use case

Comments

@dmlloyd
Copy link
Collaborator

dmlloyd commented Oct 25, 2023

It should be possible to use an enum to represent some native constants. The driving use case is errno. Since errno values are integers, one naively expects to be able to use them with switch; however, this is disallowed in practice because the values are non-constant as far as Java is concerned. Note that the newer switch statements in Java 21 and later might allow this though.

The constants should be probed lazily as they are today.

Example:

@constant
public enum errno_t {
    EACCES,
    EAGAIN,
    EBADF,
    EINTR,
    EIO,
    //...
}

And then:

    int fd = open(utf8z("/dev/null"), O_RDRW);
    if (fd == -1) {
        switch (errno) {
            case ENOENT -> throw new IOException("No dev null");
            case EINTR -> continue; // restart syscall
            default -> throw new IOException("I/O error");
        }
    }

Internally we could intercept calls to e.g. java.lang.runtime.SwitchBootstraps#enumSwitch and replace them with constant integer switches through a small number of transformations.

@dmlloyd dmlloyd added the kind: enhancement ✨ A new feature or use case label Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement ✨ A new feature or use case
Projects
None yet
Development

No branches or pull requests

1 participant