[Wasm] Use JsAny, JsHandle and other Js* types in stdlib and kotlin-test

This commit is contained in:
Svyatoslav Kuzmich
2023-03-03 16:40:35 +01:00
committed by Space Team
parent d6886d69ec
commit 1208a26fc4
17 changed files with 233 additions and 31 deletions
+1 -1
View File
@@ -152,7 +152,7 @@ internal fun <T> Dynamic.getAny(index: Int): T? = dynamicGetAny(this, index.toSt
/**
* Represents unversal type for JS interoperability.
*/
external interface Dynamic
external interface Dynamic : JsAny
/**
* Reinterprets this value as a value of the Dynamic type.
@@ -0,0 +1,11 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.js
/**
* Any JavaScript value except null or undefined
*/
public external interface JsAny
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.js
/** JavaScript Array */
@JsName("Array")
public external class JsArray<T : JsAny?> : JsAny {
val length: Int
}
public operator fun <T : JsAny?> JsArray<T>.get(index: Int): T? =
jsArrayGet(this, index)
public operator fun <T : JsAny?> JsArray<T>.set(index: Int, value: T) {
jsArraySet(this, index, value)
}
@Suppress("RedundantNullableReturnType", "UNUSED_PARAMETER")
private fun <T : JsAny?> jsArrayGet(array: JsArray<T>, index: Int): T? =
js("array[index]")
@Suppress("UNUSED_PARAMETER")
private fun <T : JsAny?> jsArraySet(array: JsArray<T>, index: Int, value: T) {
js("array[index] = value")
}
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.js
import kotlin.wasm.internal.JsPrimitive
import kotlin.wasm.internal.externRefToKotlinBooleanAdapter
import kotlin.wasm.internal.kotlinBooleanToExternRefAdapter
/** JavaScript primitive boolean */
@JsPrimitive("boolean")
public external class JsBoolean internal constructor() : JsAny
public fun JsBoolean.toBoolean(): Boolean =
externRefToKotlinBooleanAdapter(this)
public fun Boolean.toJsBoolean(): JsBoolean =
kotlinBooleanToExternRefAdapter(this)
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.js
import kotlin.wasm.internal.WasmOp
import kotlin.wasm.internal.implementedAsIntrinsic
import kotlin.wasm.internal.returnArgumentIfItIsKotlinAny
/**
* JavaScript value that can serve as a handle for any Kotlin value.
*
* In JavaScript, it behaves like an immutable empty object with a null prototype.
* When passed back to Kotlin/Wasm, the original value can be retrieved using the [get] method.
*/
@Suppress("WRONG_JS_INTEROP_TYPE") // Exception to the rule
public sealed external interface JsHandle<out T : Any> : JsAny
@WasmOp(WasmOp.EXTERN_EXTERNALIZE)
public fun <T : Any> T.toJsHandle(): JsHandle<T> =
implementedAsIntrinsic
/** Retrieve original Kotlin value from JsHandle */
public fun <T : Any> JsHandle<T>.get(): T {
returnArgumentIfItIsKotlinAny(this)
throw ClassCastException("JsHandle doesn't contain a Kotlin type")
}
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.js
import kotlin.wasm.internal.JsPrimitive
import kotlin.wasm.internal.externRefToKotlinDoubleAdapter
import kotlin.wasm.internal.externRefToKotlinIntAdapter
import kotlin.wasm.internal.kotlinDoubleToExternRefAdapter
import kotlin.wasm.internal.kotlinIntToExternRefAdapter
/** JavaScript primitive number */
@JsPrimitive("number")
public external class JsNumber internal constructor() : JsAny
public fun JsNumber.toDouble(): Double =
externRefToKotlinDoubleAdapter(this)
public fun Double.toJsNumber(): JsNumber =
kotlinDoubleToExternRefAdapter(this)
public fun JsNumber.toInt(): Int =
externRefToKotlinIntAdapter(this)
public fun Int.toJsNumber(): JsNumber =
kotlinIntToExternRefAdapter(this)
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.js
import kotlin.wasm.internal.JsPrimitive
import kotlin.wasm.internal.kotlinToJsStringAdapter
/** JavaScript primitive string */
@JsPrimitive("string")
public external class JsString internal constructor() : JsAny
public fun String.toJsString(): JsString =
kotlinToJsStringAdapter(this)!!
@@ -8,7 +8,7 @@ package kotlin.js
/**
* Exposes the JavaScript [Promise object](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise) to Kotlin.
*/
public external class Promise<out T>(executor: (resolve: (Dynamic?) -> Unit, reject: (Dynamic) -> Unit) -> Unit) {
public external class Promise<out T : JsAny?>(executor: (resolve: (Dynamic?) -> Unit, reject: (Dynamic) -> Unit) -> Unit) : JsAny {
public fun then(onFulfilled: (Dynamic?) -> Dynamic?): Promise<Dynamic?>
public fun then(onFulfilled: (Dynamic?) -> Dynamic?, onRejected: (Dynamic) -> Dynamic?): Promise<Dynamic?>
public fun catch(onRejected: (Dynamic) -> Dynamic?): Promise<Dynamic?>
@@ -34,6 +34,7 @@ import kotlin.wasm.internal.ExcludedFromCodegen
* ```
*/
@ExcludedFromCodegen
@Suppress("WRONG_JS_INTEROP_TYPE")
public external val definedExternally: Nothing
/**
@@ -5,12 +5,12 @@
package org.w3c.dom
public external interface ItemArrayLike<out T> {
public external interface ItemArrayLike<out T : JsAny?> : JsAny {
val length: Int
fun item(index: Int): T?
}
public fun <T> ItemArrayLike<T>.asList(): List<T> = object : AbstractList<T>() {
public fun <T : JsAny?> ItemArrayLike<T>.asList(): List<T> = object : AbstractList<T>() {
override val size: Int get() = this@asList.length
@Suppress("UNCHECKED_CAST")