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