-
Notifications
You must be signed in to change notification settings - Fork 46
[JExtract] Generate JNI code for classes (static methods and initializers) #298
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
908bfec
add jni sample app
madsodgaard d28959a
import classes with static methods
madsodgaard e1f0a8c
generate java bindings for initializers
madsodgaard 57fa106
generate swift thunks for initializers
madsodgaard 42ea1e3
delete duplicate main file
madsodgaard 2377ddd
pull request feedback
madsodgaard a3aadd0
load lib in classes
madsodgaard 42cbfd5
fix swift tests
madsodgaard 9abac42
move jni type signature to jextract instead of JavaTypes
madsodgaard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
Samples/JExtractJNISampleApp/Sources/MySwiftLibrary/MySwiftClass.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
public class MySwiftClass { | ||
let x: Int64 | ||
let y: Int64 | ||
|
||
public static func method() { | ||
p("Hello from static method in a class!") | ||
} | ||
|
||
public init(x: Int64, y: Int64) { | ||
self.x = x | ||
self.y = y | ||
p("\(self)") | ||
} | ||
|
||
public init() { | ||
self.x = 10 | ||
self.y = 5 | ||
} | ||
|
||
deinit { | ||
p("deinit called!") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
set -x | ||
set -e | ||
|
||
./gradlew run | ||
./gradlew run | ||
./gradlew test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
Samples/JExtractJNISampleApp/src/test/java/com/example/swift/MySwiftClassTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
package com.example.swift; | ||
|
||
import com.example.swift.MySwiftLibrary; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class MySwiftClassTest { | ||
@Test | ||
void init_noParameters() { | ||
MySwiftClass c = MySwiftClass.init(); | ||
assertNotNull(c); | ||
} | ||
|
||
@Test | ||
void init_withParameters() { | ||
MySwiftClass c = MySwiftClass.init(1337, 42); | ||
assertNotNull(c); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
Samples/JExtractJNISampleApp/src/test/java/com/example/swift/MySwiftLibraryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
package com.example.swift; | ||
|
||
import com.example.swift.MySwiftLibrary; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class MySwiftLibraryTest { | ||
@Test | ||
void call_helloWorld() { | ||
MySwiftLibrary.helloWorld(); | ||
} | ||
|
||
@Test | ||
void call_globalTakeInt() { | ||
MySwiftLibrary.globalTakeInt(12); | ||
} | ||
|
||
@Test | ||
void call_globalMakeInt() { | ||
long i = MySwiftLibrary.globalMakeInt(); | ||
assertEquals(42, i); | ||
} | ||
|
||
@Test | ||
void call_globalTakeIntInt() { | ||
MySwiftLibrary.globalTakeIntInt(1337, 42); | ||
} | ||
|
||
@Test | ||
void call_writeString_jextract() { | ||
var string = "Hello Swift!"; | ||
long reply = MySwiftLibrary.globalWriteString(string); | ||
|
||
assertEquals(string.length(), reply); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Sources/JExtractSwiftLib/Convenience/JavaType+Extensions.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import JavaTypes | ||
|
||
extension JavaType { | ||
var jniTypeSignature: String { | ||
switch self { | ||
case .boolean: "Z" | ||
case .byte: "B" | ||
case .char: "C" | ||
case .short: "S" | ||
case .int: "I" | ||
case .long: "J" | ||
case .float: "F" | ||
case .double: "D" | ||
case .class(let package, let name): | ||
if let package { | ||
"L\(package.replacingOccurrences(of: ".", with: "/"))/\(name);" | ||
} else { | ||
"L\(name);" | ||
} | ||
case .array(let javaType): "[\(javaType.jniTypeSignature)" | ||
case .void: fatalError("There is no type signature for 'void'") | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.