Skip to content

Commit

Permalink
Add direct mapping and SKIP EXTERN tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcprux committed Nov 28, 2023
1 parent 13a0a59 commit 1a483dd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ let package = Package(
defaultLocalization: "en",
platforms: [.iOS(.v16), .macOS(.v13), .tvOS(.v16), .watchOS(.v9), .macCatalyst(.v16)],
products: [
.library(name: "SkipFFI", type: .dynamic, targets: ["SkipFFI"]),
.library(name: "SkipFFI", targets: ["SkipFFI"]),
],
dependencies: [
.package(url: "https://source.skip.tools/skip.git", from: "0.7.16"),
.package(url: "https://source.skip.tools/skip-lib.git", from: "0.5.0")
.package(url: "https://source.skip.tools/skip.git", from: "0.7.30"),
.package(url: "https://source.skip.tools/skip-foundation.git", from: "0.3.7")
],
targets: [
.target(name: "SkipFFI", dependencies: [.product(name: "SkipLib", package: "skip-lib")], plugins: [.plugin(name: "skipstone", package: "skip")]),
.target(name: "SkipFFI", dependencies: [.product(name: "SkipFoundation", package: "skip-foundation")], plugins: [.plugin(name: "skipstone", package: "skip")]),
.testTarget(name: "SkipFFITests", dependencies: ["SkipFFI", .product(name: "SkipTest", package: "skip")], plugins: [.plugin(name: "skipstone", package: "skip")]),
]
)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ On the Kotlin side, SkipFFI uses the Java Native Access ([JNA](https://github.co
library to simulate Swift types like `Swift.OpaquePointer` as `com.sun.jna.Pointer` pointer references,
and implements `Swift.withUnsafeMutablePointer` using a `com.sun.jna.ptr.PointerByReference` on the Java side.

This capability is used by Skip frameworks like [SkipScript](https://source.skip.tools/skip-script) to
This capability is used by Skip frameworks like [SkipSQL](https://source.skip.tools/skip-sql) and [SkipScript](https://source.skip.tools/skip-script) to
provide a unified API to underlying native C APIs on both Darwin and Android.

## Example
Expand Down
1 change: 1 addition & 0 deletions Sources/SkipFFI/SkipFFI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public func String(cString: OpaquePointer) -> String {
cString.getString(0)
}


#endif
23 changes: 23 additions & 0 deletions Tests/SkipFFITests/SkipFFITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ final class SkipFFITests: XCTestCase {
XCTAssertNil(Darwin.getenv("PATH_DOES_NOT_EXIST"), "non-existent key should not return a value for getenv")
}

func testDarwinDirectMappingJNA() throws {
let dd = DarwinDirect()
XCTAssertEqual(12, dd.abs(-12))
}

func testSQLiteJNA() throws {
#if SKIP
// You may set the system property jna.debug_load=true to make JNA print the steps of its library search to the console.
Expand Down Expand Up @@ -117,6 +122,23 @@ final class SkipFFITests: XCTestCase {
}
}


final class DarwinDirect {
init() {
#if SKIP
// Android JNA error: "java.lang.IllegalStateException: The SecurityManager implementation on this platform is broken; you must explicitly provide the class to register"
// com.sun.jna.Native.register("c")
com.sun.jna.Native.register((DarwinDirect.self as kotlin.reflect.KClass).java, "c")
#endif
}

// @JvmName is needed for test cases, since otherwise it is mangled to 'abs$SkipFFI_debugUnitTest'
// SKIP INSERT: @JvmName("abs")
// SKIP EXTERN
func abs(_ value: Int32) -> Int32 { Darwin.abs(value) }
}


#if SKIP

// MARK: BionicDarwin
Expand All @@ -135,6 +157,7 @@ private protocol BionicDarwin : com.sun.jna.Library {
}



// MARK: LibXMLLibrary

private func LibXMLLibrary() -> LibXMLLibrary {
Expand Down

0 comments on commit 1a483dd

Please sign in to comment.