[JS IR BE] Support 'is Number' and 'is Comparable<>'
This commit is contained in:
@@ -115,6 +115,9 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
|
|||||||
// val isCharSymbol = getInternalFunction("isChar")
|
// val isCharSymbol = getInternalFunction("isChar")
|
||||||
val isObjectSymbol = getInternalFunction("isObject")
|
val isObjectSymbol = getInternalFunction("isObject")
|
||||||
|
|
||||||
|
val isNumberSymbol = getInternalFunction("isNumber")
|
||||||
|
val isComparableSymbol = getInternalFunction("isComparable")
|
||||||
|
|
||||||
val isPrimitiveArray = mapOf(
|
val isPrimitiveArray = mapOf(
|
||||||
PrimitiveType.BOOLEAN to getInternalFunction("isBooleanArray"),
|
PrimitiveType.BOOLEAN to getInternalFunction("isBooleanArray"),
|
||||||
PrimitiveType.BYTE to getInternalFunction("isByteArray"),
|
PrimitiveType.BYTE to getInternalFunction("isByteArray"),
|
||||||
|
|||||||
+8
-1
@@ -141,7 +141,6 @@ class TypeOperatorLowering(val context: JsIrBackendContext) : FileLoweringPass {
|
|||||||
type.isInt() ||
|
type.isInt() ||
|
||||||
type.isFloat() ||
|
type.isFloat() ||
|
||||||
type.isDouble() ||
|
type.isDouble() ||
|
||||||
type.isNumber() ||
|
|
||||||
type.isBoolean() ||
|
type.isBoolean() ||
|
||||||
type.isFunctionOrKFunction() ||
|
type.isFunctionOrKFunction() ||
|
||||||
type.isString()
|
type.isString()
|
||||||
@@ -217,6 +216,8 @@ class TypeOperatorLowering(val context: JsIrBackendContext) : FileLoweringPass {
|
|||||||
toType.isAny() -> generateIsObjectCheck(argument)
|
toType.isAny() -> generateIsObjectCheck(argument)
|
||||||
isTypeOfCheckingType(toType) -> generateTypeOfCheck(argument, toType)
|
isTypeOfCheckingType(toType) -> generateTypeOfCheck(argument, toType)
|
||||||
// toType.isChar() -> generateCheckForChar(argument)
|
// toType.isChar() -> generateCheckForChar(argument)
|
||||||
|
toType.isNumber() -> generateNumberCheck(argument)
|
||||||
|
toType.isComparable() -> generateComparableCheck(argument)
|
||||||
toType.isArray() -> generateGenericArrayCheck(argument)
|
toType.isArray() -> generateGenericArrayCheck(argument)
|
||||||
toType.isPrimitiveArray() -> generatePrimitiveArrayTypeCheck(argument, toType)
|
toType.isPrimitiveArray() -> generatePrimitiveArrayTypeCheck(argument, toType)
|
||||||
toType.isTypeParameter() -> generateTypeCheckWithTypeParameter(argument, toType)
|
toType.isTypeParameter() -> generateTypeCheckWithTypeParameter(argument, toType)
|
||||||
@@ -272,6 +273,12 @@ class TypeOperatorLowering(val context: JsIrBackendContext) : FileLoweringPass {
|
|||||||
private fun generateGenericArrayCheck(argument: IrExpression) =
|
private fun generateGenericArrayCheck(argument: IrExpression) =
|
||||||
JsIrBuilder.buildCall(isArraySymbol).apply { putValueArgument(0, argument) }
|
JsIrBuilder.buildCall(isArraySymbol).apply { putValueArgument(0, argument) }
|
||||||
|
|
||||||
|
private fun generateNumberCheck(argument: IrExpression) =
|
||||||
|
JsIrBuilder.buildCall(context.intrinsics.isNumberSymbol).apply { putValueArgument(0, argument) }
|
||||||
|
|
||||||
|
private fun generateComparableCheck(argument: IrExpression) =
|
||||||
|
JsIrBuilder.buildCall(context.intrinsics.isComparableSymbol).apply { putValueArgument(0, argument) }
|
||||||
|
|
||||||
private fun generatePrimitiveArrayTypeCheck(argument: IrExpression, toType: IrType): IrExpression {
|
private fun generatePrimitiveArrayTypeCheck(argument: IrExpression, toType: IrType): IrExpression {
|
||||||
val f = context.intrinsics.isPrimitiveArray[toType.getPrimitiveArrayElementType()]!!
|
val f = context.intrinsics.isPrimitiveArray[toType.getPrimitiveArrayElementType()]!!
|
||||||
return JsIrBuilder.buildCall(f).apply { putValueArgument(0, argument) }
|
return JsIrBuilder.buildCall(f).apply { putValueArgument(0, argument) }
|
||||||
|
|||||||
@@ -69,6 +69,8 @@ private val runtimeSourcesCommon = listOfKtFilesFrom(
|
|||||||
// Replaced with irRuntime/kotlinHacks.kt
|
// Replaced with irRuntime/kotlinHacks.kt
|
||||||
"libraries/stdlib/js/src/kotlin/kotlin.kt",
|
"libraries/stdlib/js/src/kotlin/kotlin.kt",
|
||||||
|
|
||||||
|
"libraries/stdlib/js/src/kotlin/currentBeMisc.kt",
|
||||||
|
|
||||||
// Full version is defined in stdlib
|
// Full version is defined in stdlib
|
||||||
// This file is useful for smaller subset of runtime sources
|
// This file is useful for smaller subset of runtime sources
|
||||||
"libraries/stdlib/js/irRuntime/rangeExtensions.kt",
|
"libraries/stdlib/js/irRuntime/rangeExtensions.kt",
|
||||||
|
|||||||
+7
-2
@@ -1,6 +1,11 @@
|
|||||||
// IGNORE_BACKEND: JS_IR
|
|
||||||
// EXPECTED_REACHABLE_NODES: 1523
|
// EXPECTED_REACHABLE_NODES: 1523
|
||||||
fun imul32(a: Int, b: Int): Int = js("Kotlin").imulEmulated(a, b)
|
|
||||||
|
//@library
|
||||||
|
//@JsName("imulEmulated")
|
||||||
|
//internal fun imul(Int, Int): Int
|
||||||
|
|
||||||
|
@Suppress("INVISIBLE_MEMBER")
|
||||||
|
fun imul32(a: Int, b: Int): Int = imul(a, b)
|
||||||
|
|
||||||
fun imul64(a: Int, b: Int): Int = (a.toLong() * b.toLong()).toInt()
|
fun imul64(a: Int, b: Int): Int = (a.toLong() * b.toLong()).toInt()
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JS_IR
|
|
||||||
// EXPECTED_REACHABLE_NODES: 1286
|
// EXPECTED_REACHABLE_NODES: 1286
|
||||||
package foo
|
package foo
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,5 @@
|
|||||||
package kotlin.js
|
package kotlin.js
|
||||||
|
|
||||||
// TODO: Polyfill
|
// TODO: Polyfill
|
||||||
fun imul(a: dynamic, b: dynamic) =
|
internal fun imul(a: Int, b: Int) =
|
||||||
js("((a & 0xffff0000) * (b & 0xffff) + (a & 0xffff) * (b | 0)) | 0")
|
js("((a & 0xffff0000) * (b & 0xffff) + (a & 0xffff) * (b | 0)) | 0").unsafeCast<Int>()
|
||||||
@@ -151,3 +151,15 @@ public fun jsIsType(obj: dynamic, jsClass: dynamic): Boolean {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isNumber(a: dynamic) = jsTypeOf(a) == "number" || a is Long
|
||||||
|
|
||||||
|
fun isComparable(value: dynamic): Boolean {
|
||||||
|
var type = jsTypeOf(value)
|
||||||
|
|
||||||
|
return type == "string" ||
|
||||||
|
type == "boolean" ||
|
||||||
|
isNumber(value) ||
|
||||||
|
isInterface(value, Comparable::class.js)
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 JetBrains s.r.o. 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
|
||||||
|
|
||||||
|
// Mirrors signature from JS IR BE
|
||||||
|
// Used for js.translator/testData/box/number/mulInt32.kt
|
||||||
|
@library
|
||||||
|
@JsName("imulEmulated")
|
||||||
|
internal fun imul(x: Int, y: Int): Int = definedExternally
|
||||||
Reference in New Issue
Block a user