Files
kotlin-fork/compiler/testData/codegen/boxWasmJsInterop/jsToKotlinAdapters.kt
T
Svyatoslav Kuzmich 1e91fe155b [Wasm] Restrict types allowed in JS interop
- Prohibit Any, Array and other unsupported non-external types in JS
  interop context
- Add K1 diagnostic
- Update BE testdata

^KT-57136 Fixed
2023-03-16 09:12:07 +00:00

135 lines
2.8 KiB
Kotlin
Vendored

// IGNORE_BACKEND: JS_IR, JS
inline fun checkNPE(body: () -> Unit) {
var throwed = false
try {
body()
} catch (e: NullPointerException) {
throwed = true
}
check(throwed)
}
fun notNullString(): String = js("'abc'")
fun notNull2String(): String = js("null")
fun nullString(): String? = js("'abc'")
fun null2String(): String? = js("null")
fun testString() {
check(notNullString() == "abc")
checkNPE { notNull2String() }
check(nullString() == "abc")
check(null2String() == null)
}
external interface ExternRef
fun notNullExternRef(): ExternRef = js("'abc'")
fun notNull2ExternRef(): ExternRef = js("null")
fun nullExternRef(): ExternRef? = js("'abc'")
fun null2ExternRef(): ExternRef? = js("null")
fun testExterRef() {
check(notNullExternRef() != null)
checkNPE { notNull2ExternRef() }
check(nullExternRef() != null)
check(null2ExternRef() == null)
}
class DataRefImpl
typealias DataRef = JsHandle<DataRefImpl>
fun notNullDataRef(x: DataRef): DataRef = js("x")
fun notNull2DataRef(x: DataRef): DataRef = js("null")
fun nullDataRef(x: DataRef): DataRef? = js("x")
fun null2DataRef(x: DataRef): DataRef? = js("null")
fun testDataRef() {
val dataRef = DataRefImpl().toJsHandle()
check(notNullDataRef(dataRef) == dataRef)
checkNPE { notNull2DataRef(dataRef) }
check (nullDataRef(dataRef) == dataRef)
check (null2DataRef(dataRef) == null)
}
fun notNullInt(): Int = js("123")
fun notNull2Int(): Int = js("null")
fun nullInt(): Int? = js("123")
fun null2Int(): Int? = js("null")
fun testInt() {
check(notNullInt() == 123)
check(notNull2Int() == 0)
check(nullInt() == 123)
check(null2Int() == null)
}
fun notNullBoolean(): Boolean = js("true")
fun notNull2Boolean(): Boolean = js("null")
fun nullBoolean(): Boolean? = js("true")
fun null2Boolean(): Boolean? = js("null")
fun testBoolean() {
check(notNullBoolean() == true)
check(notNull2Boolean() == false)
check(nullBoolean() == true)
check(null2Boolean() == null)
}
fun notNullShort(): Short = js("123")
fun notNull2Short(): Short = js("null")
fun nullShort(): Short? = js("123")
fun null2Short(): Short? = js("null")
fun testShort() {
check(notNullShort() == 123.toShort())
check(notNull2Short() == 0.toShort())
check(nullShort() == 123.toShort())
check(null2Short() == null)
}
fun notNullFloat(): Float = js("123.5")
fun notNull2Float(): Float = js("null")
fun nullFloat(): Float? = js("123.5")
fun null2Float(): Float? = js("null")
fun testFloat() {
check(notNullFloat() == 123.5f)
check(notNull2Float() == 0.0f)
check(nullFloat() == 123.5f)
check(null2Float() == null)
}
fun box(): String {
testString()
testExterRef()
testDataRef()
testInt()
testBoolean()
testShort()
testFloat()
return "OK"
}