[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 isObjectSymbol = getInternalFunction("isObject")
|
||||
|
||||
val isNumberSymbol = getInternalFunction("isNumber")
|
||||
val isComparableSymbol = getInternalFunction("isComparable")
|
||||
|
||||
val isPrimitiveArray = mapOf(
|
||||
PrimitiveType.BOOLEAN to getInternalFunction("isBooleanArray"),
|
||||
PrimitiveType.BYTE to getInternalFunction("isByteArray"),
|
||||
|
||||
+8
-1
@@ -141,7 +141,6 @@ class TypeOperatorLowering(val context: JsIrBackendContext) : FileLoweringPass {
|
||||
type.isInt() ||
|
||||
type.isFloat() ||
|
||||
type.isDouble() ||
|
||||
type.isNumber() ||
|
||||
type.isBoolean() ||
|
||||
type.isFunctionOrKFunction() ||
|
||||
type.isString()
|
||||
@@ -217,6 +216,8 @@ class TypeOperatorLowering(val context: JsIrBackendContext) : FileLoweringPass {
|
||||
toType.isAny() -> generateIsObjectCheck(argument)
|
||||
isTypeOfCheckingType(toType) -> generateTypeOfCheck(argument, toType)
|
||||
// toType.isChar() -> generateCheckForChar(argument)
|
||||
toType.isNumber() -> generateNumberCheck(argument)
|
||||
toType.isComparable() -> generateComparableCheck(argument)
|
||||
toType.isArray() -> generateGenericArrayCheck(argument)
|
||||
toType.isPrimitiveArray() -> generatePrimitiveArrayTypeCheck(argument, toType)
|
||||
toType.isTypeParameter() -> generateTypeCheckWithTypeParameter(argument, toType)
|
||||
@@ -272,6 +273,12 @@ class TypeOperatorLowering(val context: JsIrBackendContext) : FileLoweringPass {
|
||||
private fun generateGenericArrayCheck(argument: IrExpression) =
|
||||
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 {
|
||||
val f = context.intrinsics.isPrimitiveArray[toType.getPrimitiveArrayElementType()]!!
|
||||
return JsIrBuilder.buildCall(f).apply { putValueArgument(0, argument) }
|
||||
|
||||
@@ -69,6 +69,8 @@ private val runtimeSourcesCommon = listOfKtFilesFrom(
|
||||
// Replaced with irRuntime/kotlinHacks.kt
|
||||
"libraries/stdlib/js/src/kotlin/kotlin.kt",
|
||||
|
||||
"libraries/stdlib/js/src/kotlin/currentBeMisc.kt",
|
||||
|
||||
// Full version is defined in stdlib
|
||||
// This file is useful for smaller subset of runtime sources
|
||||
"libraries/stdlib/js/irRuntime/rangeExtensions.kt",
|
||||
|
||||
+7
-2
@@ -1,6 +1,11 @@
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
// 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()
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
// EXPECTED_REACHABLE_NODES: 1286
|
||||
package foo
|
||||
|
||||
|
||||
@@ -6,5 +6,5 @@
|
||||
package kotlin.js
|
||||
|
||||
// TODO: Polyfill
|
||||
fun imul(a: dynamic, b: dynamic) =
|
||||
js("((a & 0xffff0000) * (b & 0xffff) + (a & 0xffff) * (b | 0)) | 0")
|
||||
internal fun imul(a: Int, b: Int) =
|
||||
js("((a & 0xffff0000) * (b & 0xffff) + (a & 0xffff) * (b | 0)) | 0").unsafeCast<Int>()
|
||||
@@ -150,4 +150,16 @@ public fun jsIsType(obj: dynamic, jsClass: dynamic): Boolean {
|
||||
}
|
||||
|
||||
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