Files
kotlin-fork/compiler/testData/codegen/boxWasmJsInterop/kotlinToJsAdapters.kt
T

179 lines
3.0 KiB
Kotlin
Vendored

fun notNullString(x: String) {
js("if (x !== 'abc') throw 'error'")
}
fun nullString(x: String?) {
js("if (x !== 'abc') throw 'error'")
}
fun null2String(x: String?) {
js("if (x !== null) throw 'error'")
}
fun testString() {
notNullString("abc")
nullString("abc")
null2String(null)
}
external interface ExternRef
fun notNullExternRef(x: ExternRef) {
js("if (x !== 'abc') throw 'error'")
}
fun nullExternRef(x: ExternRef?) {
js("if (x !== 'abc') throw 'error'")
}
fun null2ExternRef(x: ExternRef?) {
js("if (x !== null) throw 'error'")
}
fun getExternRef(): ExternRef =
js("'abc'")
fun testExterRef() {
val externRef = getExternRef()
notNullExternRef(externRef)
nullExternRef(externRef)
null2ExternRef(null)
}
class DataRef
fun notNullDataRef(x: DataRef) {
js("if (x === null) throw 'error'")
}
fun nullDataRef(x: DataRef?) {
js("if (x === null) throw 'error'")
}
fun null2DataRef(x: DataRef?) {
js("if (x !== null) throw 'error'")
}
fun testDataRef() {
val dataRef = DataRef()
notNullDataRef(dataRef)
nullDataRef(dataRef)
null2DataRef(null)
}
fun notNullInt(x: Int) {
js("if (x !== 123) throw 'error'")
}
fun nullInt(x: Int?) {
js("if (x !== 123) throw 'error'")
}
fun null2Int(x: Int?) {
js("if (x !== null) throw 'error'")
}
fun testInt() {
notNullInt(123)
nullInt(123)
null2Int(null)
}
fun notNullBoolean(x: Boolean) {
js("if (x !== true) throw 'error'")
}
fun nullBoolean(x: Boolean?) {
js("if (x !== true) throw 'error'")
}
fun null2Boolean(x: Boolean?) {
js("if (x !== null) throw 'error'")
}
fun testBoolean() {
notNullBoolean(true)
nullBoolean(true)
null2Boolean(null)
}
fun notNullShort(x: Short) {
js("x == 123")
}
fun nullShort(x: Short?) {
js("if (x !== 123) throw 'error'")
}
fun null2Short(x: Short?) {
js("if (x !== null) throw 'error'")
}
fun testShort() {
notNullShort(123.toShort())
nullShort(123.toShort())
null2Short(null)
}
fun notNullFloat(x: Float) {
js("if (x !== 123.5) throw 'error'")
}
fun nullFloat(x: Float?) {
js("if (x !== 123.5) throw 'error'")
}
fun null2Float(x: Float?) {
js("if (x !== null) throw 'error'")
}
fun testFloat() {
notNullFloat(123.5f)
nullFloat(123.5f)
null2Float(null)
}
fun notNullNumber(x: Number) {
js("if (x !== 123.5) throw 'error'")
}
fun nullNumber(x: Number?) {
js("if (x !== 123.5) throw 'error'")
}
fun null2Number(x: Number?) {
js("if (x !== null) throw 'error'")
}
fun byte2Number(x: Number) {
js("if (x !== 123) throw 'error'")
}
fun notNullByte2Number(x: Number?) {
js("if (x !== 123) throw 'error'")
}
fun nullByte2Number(x: Number?) {
js("if (x !== null) throw 'error'")
}
fun testNumber() {
notNullNumber(123.5)
nullNumber(123.5)
null2Number(null)
byte2Number(123)
notNullByte2Number(123)
nullByte2Number(null)
}
fun box(): String {
testString()
testExterRef()
testDataRef()
testInt()
testBoolean()
testShort()
testFloat()
testNumber()
return "OK"
}