You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: USER_GUIDE.md
+21-14Lines changed: 21 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,35 +61,43 @@ public struct BigInteger {
61
61
62
62
This Swift type wraps `java.math.BigInteger`, exposing its constructors, methods, and fields for use directly to Swift. Let's try using it!
63
63
64
-
### Creating a Java Virtual Machine instance from Swift
64
+
### Creating a `BigInteger` and determine whether it is probably prime
65
65
66
-
The `JavaVirtualMachine` class, which is part of the `JavaKitVM` module, provides the ability to create and query the Java Virtual Machine. One can create a shared instance of `JavaVirtualMachine` by calling `JavaVirtualMachine.shared()`, optionally passing along extra options to the JVM (such as the class path):
66
+
Now, we can go ahead and create a `BigInteger` instance from a Swift string like this:
If the JVM is already running, a `JavaVirtualMachine` instance will be created to reference that existing JVM. Given a `JavaVirtualMachine` instance, one can query the JNI environment for the currently-active thread by calling `environment()`, e.g.,
72
+
And then call methods on it. For example, check whether the big integer is a probable prime with some certainty:
73
73
74
74
```swift
75
-
let jniEnvironment =try javaVirtualMachine.environment()
75
+
if bigInt.isProbablePrime(10) {
76
+
print("\(bigInt.toString()) is probably prime")
77
+
}
76
78
```
77
79
78
-
This JNI environment can be used to create instances of Java objects. For example, we can create a `BigInteger` instance from a Swift string like this:
80
+
Swift ensures that the Java garbage collector will keep the object alive until `bigInt` (and any copies of it) are been destroyed.
81
+
82
+
### Creating a Java Virtual Machine instance from Swift
83
+
84
+
When JavaKit requires a running Java Virtual Machine to use an operation (for example, to create an instance of `BigInteger`), it will query to determine if one is running and, if not, create one. To exercise more control over the creation and configuration of the Java virtual machine, use the `JavaVirtualMachine` class, which provides creation and query operations. One can create a shared instance by calling `JavaVirtualMachine.shared()`, optionally passing along extra options to the JVM (such as the class path):
And then call methods on it. For example, check whether the big integer is a probable prime with some certainty:
90
+
If the JVM is already running, a `JavaVirtualMachine` instance will be created to reference that existing JVM. Given a `JavaVirtualMachine` instance, one can query the JNI environment for the currently-active thread by calling `environment()`, e.g.,
85
91
86
92
```swift
87
-
if bigInt.isProbablePrime(10) {
88
-
print("\(bigInt.toString()) is probably prime")
89
-
}
93
+
let jniEnvironment =try javaVirtualMachine.environment()
90
94
```
91
95
92
-
Swift ensures that the Java garbage collector will keep the object alive until `bigInt` (and any copies of it) are been destroyed.
96
+
This JNI environment can be used to create instances of Java objects in a specific JNI environment. For example, we can pass this environment along when we create the `BigInteger` instance from a Swift string, like this:
97
+
98
+
```swift
99
+
let bigInt =BigInteger(veryBigNumber, environment: jniEnvironment)
100
+
```
93
101
94
102
### Importing a Jar file into Swift
95
103
@@ -134,7 +142,7 @@ The resulting configuration file will look something like this:
134
142
}
135
143
```
136
144
137
-
As with the previous `JavaProbablyPrime` sample, the `JavaSieve` target in `Package.swift` should depend on the `swift-java` package modules (`JavaKit`, `JavaKitVM`) and apply the `Java2Swift` plugin. This makes all of the Java classes found in the Jar file available to Swift within the `JavaSieve` target.
145
+
As with the previous `JavaProbablyPrime` sample, the `JavaSieve` target in `Package.swift` should depend on the `swift-java` package modules (`JavaKit`) and apply the `Java2Swift` plugin. This makes all of the Java classes found in the Jar file available to Swift within the `JavaSieve` target.
138
146
139
147
If you inspect the build output, there are a number of warnings that look like this:
140
148
@@ -197,7 +205,6 @@ Putting it all together, we can define a main program in `Sources/JavaSieve/main
197
205
198
206
```swift
199
207
importJavaKit
200
-
importJavaKitVM
201
208
202
209
let jvm =try JavaVirtualMachine.shared(classPath: ["QuadraticSieve-1.0.jar"])
0 commit comments