[Wasm] Rename JsHandle to JsReference

Decision to do this was made at Kotlin/Wasm interop design meeting
This commit is contained in:
Svyatoslav Kuzmich
2023-04-19 15:38:47 +02:00
committed by teamcity
parent 7ba8f7cce2
commit 933f47aaf9
8 changed files with 29 additions and 29 deletions
@@ -98,7 +98,7 @@ external fun is123Array(x: EI): Boolean
external fun create123Array(): EI
data class DC(val x: Int, val y: Int)
typealias JSDC = JsHandle<DC>
typealias JSDC = JsReference<DC>
external fun extenalWithLambda(
x: (Boolean, Byte, Short, Char, Int, Long, Float, Double, String, EI, JSDC) -> Unit,
@@ -179,7 +179,7 @@ fun box(): String {
test(string == "S")
test(is123Array(ei))
test(dc.x == 100 && dc.y == 200)
}, DC(100, 200).toJsHandle())
}, DC(100, 200).toJsReference())
if (extenalWithLambdasCount != 11) return "Fail 1"
@@ -195,7 +195,7 @@ fun box(): String {
double = { 600.5 },
string = { "700" },
ei = { create123Array() },
dc = { DC(800, 800).toJsHandle() },
dc = { DC(800, 800).toJsReference() },
dcGetY = { it.get().y }
)
if (externalWithLambdas2Count != 11) return "Fail externalWithLambdas2"
@@ -212,7 +212,7 @@ fun box(): String {
{ 600.5 },
{ "700" },
{ create123Array() },
{ DC(800, 800).toJsHandle() },
{ DC(800, 800).toJsReference() },
{ it.get().y }
)
if (externalWithLambdas2RefCount != 11) return "Fail externalWithLambdas2"
@@ -230,7 +230,7 @@ fun box(): String {
600.5,
"700",
create123Array(),
DC(800, 800).toJsHandle(),
DC(800, 800).toJsReference(),
{ it.get().y }
)
if (jsLambdaCount != 11)
+4 -4
View File
@@ -5,10 +5,10 @@
class C(val x: Int)
@JsExport
fun makeC(x: Int): JsHandle<C> = C(x).toJsHandle()
fun makeC(x: Int): JsReference<C> = C(x).toJsReference()
@JsExport
fun getX(c: JsHandle<C>): Int = c.get().x
fun getX(c: JsReference<C>): Int = c.get().x
@JsExport
fun getString(s: String): String = "Test string $s";
@@ -19,10 +19,10 @@ fun isEven(x: Int): Boolean = x % 2 == 0
external interface EI
@JsExport
fun eiAsAny(ei: EI): JsHandle<Any> = ei.toJsHandle()
fun eiAsAny(ei: EI): JsReference<Any> = ei.toJsReference()
@JsExport
fun anyAsEI(any: JsHandle<Any>): EI = any.get() as EI
fun anyAsEI(any: JsReference<Any>): EI = any.get() as EI
fun box(): String = "OK"
@@ -44,7 +44,7 @@ fun testExterRef() {
}
class DataRefImpl
typealias DataRef = JsHandle<DataRefImpl>
typealias DataRef = JsReference<DataRefImpl>
fun notNullDataRef(x: DataRef): DataRef = js("x")
@@ -55,7 +55,7 @@ fun nullDataRef(x: DataRef): DataRef? = js("x")
fun null2DataRef(x: DataRef): DataRef? = js("null")
fun testDataRef() {
val dataRef = DataRefImpl().toJsHandle()
val dataRef = DataRefImpl().toJsReference()
check(notNullDataRef(dataRef) == dataRef)
checkNPE { notNull2DataRef(dataRef) }
check (nullDataRef(dataRef) == dataRef)
+6 -6
View File
@@ -49,13 +49,13 @@ fun box(): String {
assertTrue(jsArray[1] == "element1".toJsString())
assertTrue(jsRepresentation(jsArray) == "object:element0,element1,element2")
// JsHandle
val jsHandle: JsHandle<Int> = 10.toJsHandle()
assertTrue(jsHandle.get() == 10)
assertTrue(jsHandle.toJsHandle().get() == jsHandle)
// JsReference
val jsReference: JsReference<Int> = 10.toJsReference()
assertTrue(jsReference.get() == 10)
assertTrue(jsReference.toJsReference().get() == jsReference)
val c = listOf(1)
assertTrue(c.toJsHandle().get() === c.toJsHandle().get())
assertTrue(c.toJsHandle() === c.toJsHandle())
assertTrue(c.toJsReference().get() === c.toJsReference().get())
assertTrue(c.toJsReference() === c.toJsReference())
return "OK"
}
@@ -126,8 +126,8 @@ private fun externrefEquals(lhs: ExternalInterfaceType, rhs: ExternalInterfaceTy
private external fun tryGetOrSetExternrefBox(
ref: ExternalInterfaceType,
ifNotCached: JsHandle<JsExternalBox>
): JsHandle<JsExternalBox>?
ifNotCached: JsReference<JsExternalBox>
): JsReference<JsExternalBox>?
@WasmNoOpCast
@Suppress("unused")
@@ -174,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).toJsHandle())
return tryGetOrSetExternrefBox(ref, JsExternalBox(ref).toJsReference())
}
+2 -2
View File
@@ -16,8 +16,8 @@ public external interface Dynamic : JsAny
/**
* Reinterprets this value as a value of the Dynamic type.
*/
@Deprecated("If value is a subtype of JsAny, use JsAny instead. Otherwise, use toJsHandle", level = DeprecationLevel.ERROR)
fun Any.asDynamic(): JsAny = this.toJsHandle()
@Deprecated("If value is a subtype of JsAny, use JsAny instead. Otherwise, use toJsReference", level = DeprecationLevel.ERROR)
fun Any.asDynamic(): JsAny = this.toJsReference()
/**
* Reinterprets this value as a value of the Dynamic type.
@@ -10,20 +10,20 @@ import kotlin.wasm.internal.implementedAsIntrinsic
import kotlin.wasm.internal.returnArgumentIfItIsKotlinAny
/**
* JavaScript value that can serve as a handle for any Kotlin value.
* JavaScript value that can serve as a reference 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
public sealed external interface JsReference<out T : Any> : JsAny
@WasmOp(WasmOp.EXTERN_EXTERNALIZE)
public fun <T : Any> T.toJsHandle(): JsHandle<T> =
public fun <T : Any> T.toJsReference(): JsReference<T> =
implementedAsIntrinsic
/** Retrieve original Kotlin value from JsHandle */
public fun <T : Any> JsHandle<T>.get(): T {
/** Retrieve original Kotlin value from JsReference */
public fun <T : Any> JsReference<T>.get(): T {
returnArgumentIfItIsKotlinAny(this)
throw ClassCastException("JsHandle doesn't contain a Kotlin type")
throw ClassCastException("JsReference doesn't contain a Kotlin type")
}
+1 -1
View File
@@ -57,6 +57,6 @@ class AsyncTest {
@Test
fun testJsValueToThrowableOrNull2() {
val e = MyThrowable()
assertEquals((e.toJsHandle()).toThrowableOrNull(), e)
assertEquals((e.toJsReference()).toThrowableOrNull(), e)
}
}