InstanceTokenizer

object InstanceTokenizer

Object that returns unique strings for any Kotlin/JVM instances, and then allows the original instances to be accessed given the token strings.

Uses WeakReferences, so should not cause memory leaks on its own.

Combined with TokenizingJson in ScriptingProtocol, allows scripts to handle unserializable objects, and use them in property/key/index assignments and function calls.

Functions

clean
Link copied to clipboard
fun clean()

Remove all tokens and WeakReferences whose instances have already been garbage-collected.

equals
Link copied to clipboard
open operator fun equals(other: Any?): Boolean
getReal
Link copied to clipboard
fun getReal(token: Any?): Any?

Detokenize a token string into the real Kotlin/JVM instance it represents.

getToken
Link copied to clipboard
fun getToken(obj: Any?): String
hashCode
Link copied to clipboard
open fun hashCode(): Int
isToken
Link copied to clipboard
private fun isToken(value: Any?): Boolean
tokenFromInstance
Link copied to clipboard
private fun tokenFromInstance(value: Any?): String

Generate a distinctive token string to represent a Kotlin/JVM object.

toString
Link copied to clipboard
open fun toString(): String
tryClean
Link copied to clipboard
fun tryClean()

Properties

forceCleanChance
Link copied to clipboard
private const val forceCleanChance: Double = 0.001
forceCleanThreshold
Link copied to clipboard
private const val forceCleanThreshold: Int
instancesByTokens
Link copied to clipboard
private val instancesByTokens: MutableMap<String, WeakReference<Any>>

Map of currently known token strings to WeakReferences of the Kotlin/JVM instances they represent. Used for basic functionality of tracking tokens and transforming them back into arbitrary instances.

lastTokenCountLog
Link copied to clipboard
private var lastTokenCountLog: Int = 0
tokenCountLogBase
Link copied to clipboard
private const val tokenCountLogBase: Float = 1.3f
tokenMaxLength
Link copied to clipboard
private const val tokenMaxLength: Int = 100

Length to clip generated token strings to. Here in case token string generation uses the instance's .toString(), which it currently does.

tokenPrefix
Link copied to clipboard
private val tokenPrefix: String

Prefix that all generated token strings should start with.

tokenReuse
Link copied to clipboard
private const val tokenReuse: Boolean = true
tokensByInstances
Link copied to clipboard
private val tokensByInstances: WeakIdentityMap<Any?, String>

Sources

jvm source
Link copied to clipboard