Skip to content
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
8 changes: 4 additions & 4 deletions libpkl/src/main/c/pkl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
*
* @param length The length the message bytes
* @param message The message itself
* @param payload User-defined data passed to pkl_init.
* @param userData User-defined data passed in from pkl_init.
*/
typedef void (*PklMessageResponseHandler)(int length, char *message, void *payload);
typedef void (*PklMessageResponseHandler)(int length, char *message, void *userData);

/**
* Initialises and allocates a Pkl executor.
*
* @param handler The callback that gets called when a message is received from Pkl.
* @param payload User-defined data that gets passed to handler.
* @param userData User-defined data that gets passed to handler.
*
* @return -1 on failure, 0 on success.
*/
int pkl_init(PklMessageResponseHandler handler, void *payload);
int pkl_init(PklMessageResponseHandler handler, void *userData);

/**
* Send a message to Pkl, providing the length and a pointer to the first byte.
Expand Down
112 changes: 0 additions & 112 deletions libpkl/src/nativeTest/kotlin/org/pkl/libpkl/JNATestClient.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import com.sun.jna.Native
import com.sun.jna.Pointer

@Suppress("FunctionName")
interface LibPklLibrary : Library {
interface LibPklJNA : Library {
companion object {
val INSTANCE: LibPklLibrary = Native.load("pkl", LibPklLibrary::class.java)
val INSTANCE: LibPklJNA = Native.load("pkl", LibPklJNA::class.java)
}

interface PklMessageResponseHandler : Callback {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.libpkl

import com.sun.jna.Pointer
import org.assertj.core.api.Assertions.assertThat
import org.msgpack.core.MessagePack
import org.pkl.core.messaging.Message
import org.pkl.core.messaging.MessageTransports
import org.pkl.server.ServerMessagePackDecoder
import org.pkl.server.ServerMessagePackEncoder

class LibPklMessageTransport : MessageTransports.AbstractMessageTransport({}) {
private val messageResponseHandler: LibPklJNA.PklMessageResponseHandler =
object : LibPklJNA.PklMessageResponseHandler {
override fun invoke(length: Int, message: Pointer, userData: Pointer?) {
val message = decode(message.getByteArray(0, length))
accept(message)
}
}

override fun doStart() {
assertThat(LibPklJNA.INSTANCE.pkl_init(messageResponseHandler, Pointer.NULL)).isEqualTo(0)
}

override fun doClose() {
assertThat(LibPklJNA.INSTANCE.pkl_close()).isEqualTo(0)
}

override fun doSend(message: Message) {
val bytes = encode(message)
LibPklJNA.INSTANCE.pkl_send_message(bytes.size, bytes)
}

private fun encode(message: Message): ByteArray {
val packer = MessagePack.newDefaultBufferPacker()
val encoder = ServerMessagePackEncoder(packer)
encoder.encode(message)
return packer.toByteArray()
}

private fun decode(receivedBytes: ByteArray): Message {
val unpacker = MessagePack.newDefaultUnpacker(receivedBytes)
return ServerMessagePackDecoder(unpacker).decode()!!
}
}
46 changes: 46 additions & 0 deletions libpkl/src/nativeTest/kotlin/org/pkl/libpkl/LibPklTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.libpkl

import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.pkl.commons.test.server.AbstractServerTest
import org.pkl.commons.test.server.TestTransport

/**
* Tests libpkl bindings by using JNA (see [LibPklJNA] and [LibPklMessageTransport]).
*
* To run these tests in IntelliJ, add
* `-Djna.library.path=$ProjectFileDir$/libpkl/build/native-libs/<os>-<arch>` to the run
* configuration.
*
* You can modify the IntelliJ JUnit configuration template so that this flag gets added
* automatically. See https://www.jetbrains.com/help/idea/run-debug-configuration.html#templates for
* more details.
*/
class LibPklTest : AbstractServerTest() {
override lateinit var client: TestTransport

@BeforeEach
fun beforeEach() {
client = TestTransport(LibPklMessageTransport()).also { it.start() }
}

@AfterEach
fun afterEach() {
client.close()
}
}

This file was deleted.

Loading