Implement StableObjPtr for Kotlin N
This commit is contained in:
committed by
SvyatoslavScherbina
parent
d07295d732
commit
a50b38da5a
@@ -1,5 +1,51 @@
|
||||
package kotlinx.cinterop
|
||||
|
||||
/**
|
||||
* This class provides a way to create a stable handle to any Kotlin object.
|
||||
* Its [value] can be safely passed to native code e.g. to be received in a Kotlin callback.
|
||||
*
|
||||
* Any [StableObjPtr] should be manually [disposed][dispose]
|
||||
*/
|
||||
data class StableObjPtr private constructor(val value: COpaquePointer) {
|
||||
|
||||
companion object {
|
||||
|
||||
/**
|
||||
* Creates a handle for given object.
|
||||
*/
|
||||
fun create(any: Any) = fromValue(createStablePointer(any))
|
||||
|
||||
/**
|
||||
* Creates [StableObjPtr] from given raw value.
|
||||
*
|
||||
* @param value must be a [value] of some [StableObjPtr]
|
||||
*/
|
||||
fun fromValue(value: COpaquePointer) = StableObjPtr(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Disposes the handle. It must not be [used][get] after that.
|
||||
*/
|
||||
fun dispose() {
|
||||
disposeStablePointer(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object this handle was [created][create] for.
|
||||
*/
|
||||
fun get(): Any = derefStablePointer(value)
|
||||
|
||||
}
|
||||
|
||||
@SymbolName("Kotlin_Interop_createStablePointer")
|
||||
private external fun createStablePointer(any: Any): COpaquePointer
|
||||
|
||||
@SymbolName("Kotlin_Interop_disposeStablePointer")
|
||||
private external fun disposeStablePointer(pointer: COpaquePointer)
|
||||
|
||||
@SymbolName("Kotlin_Interop_derefStablePointer")
|
||||
private external fun derefStablePointer(pointer: COpaquePointer): Any
|
||||
|
||||
/**
|
||||
* The type of C function which can be constructed from the appropriate Kotlin function without using any adapter.
|
||||
*/
|
||||
|
||||
@@ -107,7 +107,7 @@ fun callWithVarargs(codePtr: NativePtr, returnValuePtr: NativePtr, returnTypeKin
|
||||
fixedArguments.size, totalArgumentsNumber)
|
||||
}
|
||||
|
||||
@SymbolName("callWithVarargs")
|
||||
@SymbolName("Kotlin_Interop_callWithVarargs")
|
||||
private external fun callWithVarargs(codePtr: NativePtr, returnValuePtr: NativePtr, returnTypeKind: FfiTypeKind,
|
||||
arguments: NativePtr, argumentTypeKinds: NativePtr,
|
||||
fixedArgumentsNumber: Int, totalArgumentsNumber: Int)
|
||||
Reference in New Issue
Block a user