-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Select cleaner implementations for Android, java.lang.ref and fallbac…
…k to JNA
- Loading branch information
Showing
8 changed files
with
93 additions
and
64 deletions.
There are no files selected for viewing
49 changes: 0 additions & 49 deletions
49
uniffi_bindgen/src/bindings/kotlin/templates/AndroidCleaner.kt
This file was deleted.
Oops, something went wrong.
This file contains 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
40 changes: 40 additions & 0 deletions
40
uniffi_bindgen/src/bindings/kotlin/templates/ObjectCleanerHelper.kt
This file contains 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,40 @@ | ||
|
||
// The cleaner interface for Object finalization code to run. | ||
// This is the entry point to any implementation that we're using. | ||
// | ||
// The cleaner registers objects and returns cleanables, so now we are | ||
// defining a `UniffiCleaner` with a `UniffiClenaer.Cleanable` to abstract the | ||
// different implmentations available at compile time. | ||
interface UniffiCleaner { | ||
interface Cleanable { | ||
fun clean() | ||
} | ||
|
||
fun register(value: Any, cleanUpTask: Runnable): UniffiCleaner.Cleanable | ||
|
||
companion object | ||
} | ||
|
||
// The fallback Jna cleaner, which is available for both Android, and the JVM. | ||
private class UniffiJnaCleaner : UniffiCleaner { | ||
private val cleaner = com.sun.jna.internal.Cleaner.getCleaner() | ||
|
||
override fun register(value: Any, cleanUpTask: Runnable): UniffiCleaner.Cleanable = | ||
UniffiJnaCleanable(cleaner.register(value, cleanUpTask)) | ||
} | ||
|
||
private class UniffiJnaCleanable( | ||
private val cleanable: com.sun.jna.internal.Cleaner.Cleanable, | ||
) : UniffiCleaner.Cleanable { | ||
override fun clean() = cleanable.clean() | ||
} | ||
|
||
// We decide at uniffi binding generation time whether we were | ||
// using Android or not. | ||
// There are further runtime checks to chose the correct implementation | ||
// of the cleaner. | ||
{% if config.android_cleaner() %} | ||
{%- include "ObjectCleanerHelperAndroid.kt" %} | ||
{%- else %} | ||
{%- include "ObjectCleanerHelperJvm.kt" %} | ||
{%- endif %} |
22 changes: 22 additions & 0 deletions
22
uniffi_bindgen/src/bindings/kotlin/templates/ObjectCleanerHelperAndroid.kt
This file contains 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,22 @@ | ||
{{- self.add_import("android.os.Build") }} | ||
|
||
private fun UniffiCleaner.Companion.create(): UniffiCleaner = | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
AndroidSystemCleaner() | ||
} else { | ||
UniffiJnaCleaner() | ||
} | ||
|
||
// The SystemCleaner, available from API Level 33. | ||
private class AndroidSystemCleaner : UniffiCleaner { | ||
val cleaner = android.system.SystemCleaner.cleaner() | ||
|
||
override fun register(value: Any, cleanUpTask: Runnable): UniffiCleaner.Cleanable = | ||
AndroidSystemCleanable(cleaner.register(value, cleanUpTask)) | ||
} | ||
|
||
private class AndroidSystemCleanable( | ||
private val cleanable: java.lang.ref.Cleaner.Cleanable, | ||
) : UniffiCleaner.Cleanable { | ||
override fun clean() = cleanable.clean() | ||
} |
25 changes: 25 additions & 0 deletions
25
uniffi_bindgen/src/bindings/kotlin/templates/ObjectCleanerHelperJvm.kt
This file contains 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,25 @@ | ||
private fun UniffiCleaner.Companion.create(): UniffiCleaner = | ||
try { | ||
// For safety's sake: if the library hasn't been run in android_cleaner = true | ||
// mode, but is being run on Android, then we still need to think about | ||
// Android API versions. | ||
// So we check if java.lang.ref.Cleaner is there, and use that… | ||
Class.`forName`("java.lang.ref.Cleaner") | ||
JavaLangRefCleaner() | ||
} catch (e: ClassNotFoundException) { | ||
// … otherwise, fallback to the JNA cleaner. | ||
UniffiJnaCleaner() | ||
} | ||
|
||
private class JavaLangRefCleaner : UniffiCleaner { | ||
val cleaner = java.lang.ref.Cleaner.create() | ||
|
||
override fun register(value: Any, cleanUpTask: Runnable): UniffiCleaner.Cleanable = | ||
JavaLangRefCleanable(cleaner.register(value, cleanUpTask)) | ||
} | ||
|
||
private class JavaLangRefCleanable( | ||
val cleanable: java.lang.ref.Cleaner.Cleanable | ||
) : UniffiCleaner.Cleanable { | ||
override fun clean() = cleanable.clean() | ||
} |
This file contains 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 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 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