@@ -84,13 +84,8 @@ public final class JavaVirtualMachine: @unchecked Sendable {
84
84
vmArgs. nOptions = jint ( optionsBuffer. count)
85
85
86
86
// Create the JVM instance.
87
- let createResult = JNI_CreateJavaVM ( & jvm, & environment, & vmArgs)
88
- if createResult != JNI_OK {
89
- if createResult == JNI_EEXIST {
90
- throw VMError . existingVM
91
- }
92
-
93
- throw VMError . failedToCreateVM
87
+ if let createError = VMError ( fromJNIError: JNI_CreateJavaVM ( & jvm, & environment, & vmArgs) ) {
88
+ throw createError
94
89
}
95
90
96
91
self . jvm = jvm!
@@ -100,8 +95,8 @@ public final class JavaVirtualMachine: @unchecked Sendable {
100
95
deinit {
101
96
if destroyOnDeinit {
102
97
// Destroy the JVM.
103
- if jvm. pointee!. pointee. DestroyJavaVM ( jvm) != JNI_OK {
104
- fatalError ( " Failed to destroy the JVM. " )
98
+ if let resultError = VMError ( fromJNIError : jvm. pointee!. pointee. DestroyJavaVM ( jvm) ) {
99
+ fatalError ( " Failed to destroy the JVM: \( resultError ) " )
105
100
}
106
101
}
107
102
}
@@ -136,19 +131,19 @@ extension JavaVirtualMachine {
136
131
attachResult = jvm. pointee!. pointee. AttachCurrentThread ( jvm, & environment, nil )
137
132
}
138
133
139
- if attachResult == JNI_OK, let environment {
140
- return environment. assumingMemoryBound ( to: JNIEnv ? . self)
134
+ // If we failed to attach, report that.
135
+ if let attachError = VMError ( fromJNIError: attachResult) {
136
+ throw attachError
141
137
}
142
138
143
- throw VMError . failedToAttachThread
139
+ return environment! . assumingMemoryBound ( to : JNIEnv ? . self )
144
140
}
145
141
146
142
/// Detach the current thread from the Java Virtual Machine. All Java
147
143
/// threads waiting for this thread to die are notified.
148
144
public func detachCurrentThread( ) throws {
149
- let result = jvm. pointee!. pointee. DetachCurrentThread ( jvm)
150
- if result != JNI_OK {
151
- throw VMError . failedToDetachThread
145
+ if let resultError = VMError ( fromJNIError: jvm. pointee!. pointee. DetachCurrentThread ( jvm) ) {
146
+ throw resultError
152
147
}
153
148
}
154
149
}
@@ -234,11 +229,36 @@ extension JavaVirtualMachine {
234
229
}
235
230
236
231
extension JavaVirtualMachine {
232
+ /// Describes the kinds of errors that can occur when interacting with JNI.
237
233
enum VMError : Error {
238
- case failedToCreateVM
239
- case failedToAttachThread
240
- case failedToDetachThread
241
- case failedToQueryVM
234
+ /// There is already a Java Virtual Machine.
242
235
case existingVM
236
+
237
+ /// JNI version mismatch error.
238
+ case jniVersion
239
+
240
+ /// Thread is detached from the VM.
241
+ case threadDetached
242
+
243
+ /// Out of memory.
244
+ case outOfMemory
245
+
246
+ /// Invalid arguments.
247
+ case invalidArguments
248
+
249
+ /// Unknown JNI error.
250
+ case unknown( jint )
251
+
252
+ init ? ( fromJNIError error: jint ) {
253
+ switch error {
254
+ case JNI_OK: return nil
255
+ case JNI_EDETACHED: self = . threadDetached
256
+ case JNI_EVERSION: self = . jniVersion
257
+ case JNI_ENOMEM: self = . outOfMemory
258
+ case JNI_EEXIST: self = . existingVM
259
+ case JNI_EINVAL: self = . invalidArguments
260
+ default : self = . unknown( error)
261
+ }
262
+ }
243
263
}
244
264
}
0 commit comments