|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import JavaTypes |
| 16 | + |
| 17 | +// TODO: extension JavaValue: CustomStringConvertible where JNIType == jobject? { |
| 18 | +extension JavaObject: CustomStringConvertible { |
| 19 | + public var description: String { |
| 20 | + "\(toString())" |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +extension JavaInteger: JavaValue { |
| 25 | + public typealias JNIType = jobject? |
| 26 | + |
| 27 | + public static var jvalueKeyPath: WritableKeyPath<jvalue, JNIType> { \.l } |
| 28 | + |
| 29 | + public static var javaType: JavaType { |
| 30 | + .class(package: "java.lang", name: "Integer") |
| 31 | + } |
| 32 | + |
| 33 | + // FIXME: cannot implement in extension, need to fix source generator |
| 34 | +// public required init(fromJNI value: JNIType, in environment: JNIEnvironment) { |
| 35 | +// fatalError() |
| 36 | +// } |
| 37 | + |
| 38 | + |
| 39 | + public func getJNIValue(in environment: JNIEnvironment) -> JNIType { |
| 40 | + fatalError() |
| 41 | + } |
| 42 | + |
| 43 | + public static func jniMethodCall(in environment: JNIEnvironment) -> JNIMethodCall<JNIType> { |
| 44 | + environment.interface.CallObjectMethodA |
| 45 | + } |
| 46 | + |
| 47 | + public static func jniFieldGet(in environment: JNIEnvironment) -> JNIFieldGet<JNIType> { |
| 48 | + environment.interface.GetObjectField |
| 49 | + } |
| 50 | + |
| 51 | + public static func jniFieldSet(in environment: JNIEnvironment) -> JNIFieldSet<JNIType> { |
| 52 | + environment.interface.SetObjectField |
| 53 | + } |
| 54 | + |
| 55 | + public static func jniStaticMethodCall(in environment: JNIEnvironment) -> JNIStaticMethodCall<JNIType> { |
| 56 | + environment.interface.CallStaticObjectMethodA |
| 57 | + } |
| 58 | + |
| 59 | + public static func jniStaticFieldGet(in environment: JNIEnvironment) -> JNIStaticFieldGet<JNIType> { |
| 60 | + environment.interface.GetStaticObjectField |
| 61 | + } |
| 62 | + |
| 63 | + public static func jniStaticFieldSet(in environment: JNIEnvironment) -> JNIStaticFieldSet<JNIType> { |
| 64 | + environment.interface.SetStaticObjectField |
| 65 | + } |
| 66 | + |
| 67 | + public static func jniNewArray(in environment: JNIEnvironment) -> JNINewArray { |
| 68 | + return { environment, size in |
| 69 | + // FIXME: Introduce a JavaString class that we can use for this. |
| 70 | + let clazz = environment.interface.FindClass(environment, "java/lang/Integer") |
| 71 | + return environment.interface.NewObjectArray(environment, size, clazz, nil) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public static func jniGetArrayRegion(in environment: JNIEnvironment) -> JNIGetArrayRegion<JNIType> { |
| 76 | + return { environment, array, start, length, outPointer in |
| 77 | + let buffer = UnsafeMutableBufferPointer(start: outPointer, count: Int(length)) |
| 78 | + for i in 0..<length { |
| 79 | + buffer.initializeElement( |
| 80 | + at: Int(i), |
| 81 | + to: environment.interface.GetObjectArrayElement(environment, array, Int32(start + i)) |
| 82 | + ) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public static func jniSetArrayRegion(in environment: JNIEnvironment) -> JNISetArrayRegion<JNIType> { |
| 88 | + return { environment, array, start, length, outPointer in |
| 89 | + let buffer = UnsafeBufferPointer(start: outPointer, count: Int(length)) |
| 90 | + for i in start..<start + length { |
| 91 | + environment.interface.SetObjectArrayElement(environment, array, i, buffer[Int(i)]) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public static var jniPlaceholderValue: jobject? { |
| 97 | + nil |
| 98 | + } |
| 99 | +} |
0 commit comments