[Wasm] Use JsAny, JsHandle and other Js* types in stdlib and kotlin-test
This commit is contained in:
committed by
Space Team
parent
d6886d69ec
commit
1208a26fc4
@@ -11,7 +11,7 @@ import kotlin.wasm.internal.reftypes.anyref
|
||||
import kotlin.wasm.unsafe.withScopedMemoryAllocator
|
||||
import kotlin.wasm.unsafe.UnsafeWasmMemoryApi
|
||||
|
||||
internal external interface ExternalInterfaceType
|
||||
internal typealias ExternalInterfaceType = JsAny
|
||||
|
||||
internal class JsExternalBox @WasmPrimitiveConstructor constructor(val ref: ExternalInterfaceType) {
|
||||
override fun toString(): String =
|
||||
@@ -106,7 +106,7 @@ private fun externrefToFloat(ref: ExternalInterfaceType): Float =
|
||||
private fun externrefToDouble(ref: ExternalInterfaceType): Double =
|
||||
js("Number(ref)")
|
||||
|
||||
private fun intToExternref(x: Int): ExternalInterfaceType =
|
||||
private fun intToExternref(x: Int): JsNumber =
|
||||
js("x")
|
||||
|
||||
private fun longToExternref(x: Long): ExternalInterfaceType =
|
||||
@@ -118,13 +118,16 @@ private fun booleanToExternref(x: Boolean): ExternalInterfaceType =
|
||||
private fun floatToExternref(x: Float): ExternalInterfaceType =
|
||||
js("x")
|
||||
|
||||
private fun doubleToExternref(x: Double): ExternalInterfaceType =
|
||||
private fun doubleToExternref(x: Double): JsNumber =
|
||||
js("x")
|
||||
|
||||
private fun externrefEquals(lhs: ExternalInterfaceType, rhs: ExternalInterfaceType): Boolean =
|
||||
js("lhs === rhs")
|
||||
|
||||
private external fun tryGetOrSetExternrefBox(ref: ExternalInterfaceType, ifNotCached: JsExternalBox): JsExternalBox?
|
||||
private external fun tryGetOrSetExternrefBox(
|
||||
ref: ExternalInterfaceType,
|
||||
ifNotCached: JsHandle<JsExternalBox>
|
||||
): JsHandle<JsExternalBox>?
|
||||
|
||||
@WasmNoOpCast
|
||||
@Suppress("unused")
|
||||
@@ -171,7 +174,7 @@ internal fun externRefToAny(ref: ExternalInterfaceType): Any? {
|
||||
// If we have Null in notNullRef -- return null
|
||||
// If we already have a box -- return it,
|
||||
// otherwise -- remember new box and return it.
|
||||
return tryGetOrSetExternrefBox(ref, JsExternalBox(ref))
|
||||
return tryGetOrSetExternrefBox(ref, JsExternalBox(ref).toJsHandle())
|
||||
}
|
||||
|
||||
|
||||
@@ -187,7 +190,7 @@ internal fun stringLength(x: ExternalInterfaceType): Int =
|
||||
|
||||
// kotlin string to js string export
|
||||
// TODO Uint16Array may work with byte endian different with Wasm (i.e. little endian)
|
||||
internal fun importStringFromWasm(address: Int, length: Int, prefix: ExternalInterfaceType?): ExternalInterfaceType {
|
||||
internal fun importStringFromWasm(address: Int, length: Int, prefix: ExternalInterfaceType?): JsString {
|
||||
js("""
|
||||
const mem16 = new Uint16Array(wasmExports.memory.buffer, address, length);
|
||||
const str = String.fromCharCode.apply(null, mem16);
|
||||
@@ -195,7 +198,7 @@ internal fun importStringFromWasm(address: Int, length: Int, prefix: ExternalInt
|
||||
""")
|
||||
}
|
||||
|
||||
internal fun kotlinToJsStringAdapter(x: String?): ExternalInterfaceType? {
|
||||
internal fun kotlinToJsStringAdapter(x: String?): JsString? {
|
||||
// Using nullable String to represent default value
|
||||
// for parameters with default values
|
||||
if (x == null) return null
|
||||
@@ -269,13 +272,13 @@ internal fun jsToKotlinStringAdapter(x: ExternalInterfaceType): String {
|
||||
}
|
||||
|
||||
|
||||
private fun getJsEmptyString(): ExternalInterfaceType =
|
||||
private fun getJsEmptyString(): JsString =
|
||||
js("''")
|
||||
|
||||
private fun getJsTrue(): ExternalInterfaceType =
|
||||
private fun getJsTrue(): JsBoolean =
|
||||
js("true")
|
||||
|
||||
private fun getJsFalse(): ExternalInterfaceType =
|
||||
private fun getJsFalse(): JsBoolean =
|
||||
js("false")
|
||||
|
||||
private val jsEmptyString by lazy(::getJsEmptyString)
|
||||
@@ -310,11 +313,10 @@ internal fun externRefToKotlinFloatAdapter(x: ExternalInterfaceType): Float =
|
||||
internal fun externRefToKotlinDoubleAdapter(x: ExternalInterfaceType): Double =
|
||||
externrefToDouble(x)
|
||||
|
||||
|
||||
internal fun kotlinIntToExternRefAdapter(x: Int): ExternalInterfaceType =
|
||||
internal fun kotlinIntToExternRefAdapter(x: Int): JsNumber =
|
||||
intToExternref(x)
|
||||
|
||||
internal fun kotlinBooleanToExternRefAdapter(x: Boolean): ExternalInterfaceType =
|
||||
internal fun kotlinBooleanToExternRefAdapter(x: Boolean): JsBoolean =
|
||||
if (x) jsTrue else jsFalse
|
||||
|
||||
internal fun kotlinLongToExternRefAdapter(x: Long): ExternalInterfaceType =
|
||||
@@ -323,7 +325,7 @@ internal fun kotlinLongToExternRefAdapter(x: Long): ExternalInterfaceType =
|
||||
internal fun kotlinFloatToExternRefAdapter(x: Float): ExternalInterfaceType =
|
||||
floatToExternref(x)
|
||||
|
||||
internal fun kotlinDoubleToExternRefAdapter(x: Double): ExternalInterfaceType =
|
||||
internal fun kotlinDoubleToExternRefAdapter(x: Double): JsNumber =
|
||||
doubleToExternref(x)
|
||||
|
||||
internal fun kotlinByteToExternRefAdapter(x: Byte): ExternalInterfaceType =
|
||||
|
||||
@@ -295,10 +295,12 @@ internal external fun wasm_f32_truncate(a: Float): Float
|
||||
internal external fun wasm_f32_abs(a: Float): Float
|
||||
|
||||
@WasmOp(WasmOp.REF_IS_NULL)
|
||||
internal external fun wasm_ref_is_null(a: Any?): Boolean
|
||||
internal fun wasm_ref_is_null(a: Any?): Boolean =
|
||||
implementedAsIntrinsic
|
||||
|
||||
@WasmOp(WasmOp.REF_EQ)
|
||||
internal external fun wasm_ref_eq(a: Any?, b: Any?): Boolean
|
||||
internal fun wasm_ref_eq(a: Any?, b: Any?): Boolean =
|
||||
implementedAsIntrinsic
|
||||
|
||||
// ---
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user