xref: /aosp_15_r20/external/kotlinx.serialization/core/jsMain/src/kotlinx/serialization/internal/Platform.kt (revision 57b5a4a64c534cf7f27ac9427ceab07f3d8ed3d8)
1 /*
2  * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.serialization.internal
6 
7 import kotlinx.serialization.*
8 import kotlin.reflect.*
9 
getCheckednull10 internal actual fun <T> Array<T>.getChecked(index: Int): T {
11     if (index !in indices) throw IndexOutOfBoundsException("Index $index out of bounds $indices")
12     return get(index)
13 }
14 
getCheckednull15 internal actual fun BooleanArray.getChecked(index: Int): Boolean {
16     if (index !in indices) throw IndexOutOfBoundsException("Index $index out of bounds $indices")
17     return get(index)
18 }
19 
compiledSerializerImplnull20 internal actual fun <T : Any> KClass<T>.compiledSerializerImpl(): KSerializer<T>? =
21     this.constructSerializerForGivenTypeArgs() ?: (
22         if (this === Nothing::class) NothingSerializer // Workaround for KT-51333
23         else this.js.asDynamic().Companion?.serializer()
24         ) as? KSerializer<T>
25 
26 internal actual fun <T> createCache(factory: (KClass<*>) -> KSerializer<T>?): SerializerCache<T> {
27     return object: SerializerCache<T> {
28         override fun get(key: KClass<Any>): KSerializer<T>? {
29             return factory(key)
30         }
31     }
32 }
33 
createParametrizedCachenull34 internal actual fun <T> createParametrizedCache(factory: (KClass<Any>, List<KType>) -> KSerializer<T>?): ParametrizedSerializerCache<T> {
35     return object: ParametrizedSerializerCache<T> {
36         override fun get(key: KClass<Any>, types: List<KType>): Result<KSerializer<T>?> {
37             return kotlin.runCatching { factory(key, types) }
38         }
39     }
40 }
41 
toNativeArrayImplnull42 internal actual fun <T : Any, E : T?> ArrayList<E>.toNativeArrayImpl(eClass: KClass<T>): Array<E> = toTypedArray()
43 
44 internal actual fun KClass<*>.platformSpecificSerializerNotRegistered(): Nothing {
45     throw SerializationException(
46         notRegisteredMessage() +
47                 "To get enum serializer on Kotlin/JS, it should be annotated with @Serializable annotation."
48     )
49 }
50 
51 @Suppress("UNCHECKED_CAST", "DEPRECATION_ERROR")
52 @OptIn(ExperimentalAssociatedObjects::class)
constructSerializerForGivenTypeArgsnull53 internal actual fun <T : Any> KClass<T>.constructSerializerForGivenTypeArgs(vararg args: KSerializer<Any?>): KSerializer<T>? =
54     try {
55         val assocObject = findAssociatedObject<SerializableWith>()
56         when {
57             assocObject is KSerializer<*> -> assocObject as KSerializer<T>
58             assocObject is SerializerFactory -> assocObject.serializer(*args) as KSerializer<T>
59             this.isInterface -> PolymorphicSerializer(this)
60             else -> null
61         }
62     } catch (e: dynamic) {
63         null
64     }
65 
isReferenceArraynull66 internal actual fun isReferenceArray(rootClass: KClass<Any>): Boolean = rootClass == Array::class
67 
68 /**
69  * WARNING: may be broken in arbitrary time in the future without notice
70  *
71  * Should be eventually replaced with compiler intrinsics
72  */
73 private val KClass<*>.isInterface
74     get(): Boolean = js.asDynamic().`$metadata$`?.kind == "interface"
75