nativeMain.com.dokar.quickjs.converter.castValueOr.native.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quickjs-kt Show documentation
Show all versions of quickjs-kt Show documentation
A QuickJS binding for idiomatic Kotlin, with Async/DSL/ES Modules support.
The newest version!
package com.dokar.quickjs.converter
import kotlin.reflect.KClass
import kotlin.reflect.KType
@Suppress("unchecked_cast")
@PublishedApi
internal actual fun castValueOr(
value: Any?,
expectedType: KType,
fallback: (value: Any) -> T
): T {
val nonNull = value ?: return null as T
val expectedClass = expectedType.classifier as KClass<*>
if (expectedClass.isInstance(value)) {
return nonNull as T
}
when (expectedClass) {
Byte::class -> {
if (value is Long) {
return safeCastToByteOrThrow(value) as T
}
}
Short::class -> {
if (value is Long) {
return safeCastToShortOrThrow(value) as T
}
}
Int::class -> {
when (value) {
is Long -> {
// Long -> int
return safeCastToIntOrThrow(value) as T
}
}
}
Float::class -> {
when (value) {
is Double -> {
// Double -> float
return safeCastToFloatOrThrow(value) as T
}
is Long -> {
// Long -> float
return value.toFloat() as T
}
}
}
Double::class -> {
if (value is Long) {
// Long -> double
return value.toDouble() as T
}
}
}
return fallback(value)
}