[go: nahoru, domu]

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

Support raw mode for posix in native image #186

Merged
merged 1 commit into from
Jul 9, 2024

Conversation

hubvd
Copy link
Contributor
@hubvd hubvd commented Jul 8, 2024

The issue with Expected Object but got Word when compiling the native image comes from the fact that the kotlin compiler insert null checks if the used types are not nullable.

For example, if we declare tcgetattr as

@CFunction("tcgetattr")
external fun tcgetattr(fd: Int, termios: termios): Int

Every call to it will be compiled as

Intrinsics.checkNotNull(termios);
tcgetattr(..., termios);

It is possible to disable those checks with the following compiler parameters:
-Xno-param-assertions, -Xno-call-assertions,-Xno-receiver-assertions.

But in this case, it is enough to simply mark the termios parameter as nullable.

The issue with `Expected Object but got Word` when compiling the native
image comes from the fact that the kotlin compiler insert null checks if
the used types are not nullable.

For example, if we declare tcgetattr as

```kotlin
@cfunction("tcgetattr")
external fun tcgetattr(fd: Int, termios: termios): Int
```

Every call to it will be compiled as

```kotlin
Intrinsics.checkNotNull(termios);
tcgetattr(..., termios);
```

It is possible to disable those checks with the following compiler
parameters:
-Xno-param-assertions, -Xno-call-assertions,-Xno-receiver-assertions.

But in this case, it is enough to simply mark the termios parameter as
nullable.
@@ -39,15 +39,6 @@ class GraalSmokeTest {
future.get(1000, TimeUnit.MILLISECONDS)
}

@Ignore("Raw mode is currently unsupported on native-image")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we remove the assertThrows, this test fails with Inappropriate ioctl for device

Actually, when using the native-build-tools plugins, every test is run two times. Once on the JVM to discover the tests, and a second time in native-image mode. It first fails when using JNA

com.sun.jna.LastErrorException: [25] Inappropriate ioctl for device
	at app//com.sun.jna.Native.invokeVoid(Native Method)
	at app//com.sun.jna.Function.invoke(Function.java:418)
	at app//com.sun.jna.Function.invoke(Function.java:364)
	at app//com.sun.jna.Library$Handler.invoke(Library.java:270)
	at app//com.github.ajalt.mordant.internal.syscalls.jna.$Proxy16.tcgetattr(Unknown Source)
	at app//com.github.ajalt.mordant.internal.syscalls.jna.SyscallHandlerJnaLinux.getStdinTermios(SyscallHandler.jna.linux.kt:86)
	at app//com.github.ajalt.mordant.internal.syscalls.SyscallHandlerPosix.enterRawMode(SyscallHandler.posix.kt:162)
	at app//com.github.ajalt.mordant.input.RawModeKt.enterRawMode(RawMode.kt:21)
	at app//com.github.ajalt.mordant.input.RawModeKt.enterRawMode$default(RawMode.kt:17)
	at app//com.github.ajalt.mordant.graalvm.GraalSmokeTest.raw mode test(GraalSmokeTest.kt:45)
	at java.base@21.0.3/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base@21.0.3/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base@21.0.3/java.util.ArrayList.forEach(ArrayList.java:1596)

But if we rewrite the test as

    @Test
    fun `raw mode test`() {
        val imageCode = System.getProperty("org.graalvm.nativeimage.imagecode")
        val isNativeImage = imageCode == "buildtime" || imageCode == "runtime"
        if (!isNativeImage) {
            return
        }
        val t = Terminal(interactive = true)
        t.enterRawMode().use {}
    }

If fails the same way Error reading terminal attributes

I don't think it's possible to run this from a gradle test, so I removed it

@hubvd
Copy link
Contributor Author
hubvd commented Jul 8, 2024

My bad for not mentioning this in the initial native-image PR, it also took me quite a while to figure this out.
I don't have a windows computer setup right now, but if you want, I can try to check the windows part this weekend ?

@ajalt
Copy link
Owner
ajalt commented Jul 9, 2024

Amazing! I have no idea how you figured this out. Thank you!

The tests are failing because a struct definition is wrong on macos, but that's my fault, not yours. I'll merge now and fix it is a separate PR.

@ajalt ajalt merged commit 0a54a13 into ajalt:master Jul 9, 2024
3 of 4 checks passed
ajalt added a commit that referenced this pull request Jul 9, 2024
This is a follow on to #186 that fixes the `termios` struct definition on macos.
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.

None yet

2 participants