[JS Legacy] Add an implicit cast to int for the mod operation

^KT-45620 Fixed
This commit is contained in:
Alexander Korepanov
2022-01-20 20:19:01 +03:00
committed by Space
parent 34ca38d1a0
commit 68117faa73
3 changed files with 18 additions and 2 deletions
@@ -7746,6 +7746,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
runTest("js/js.translator/testData/box/number/intIncDecOverflow.kt");
}
@Test
@TestMetadata("intMod.kt")
public void testIntMod() throws Exception {
runTest("js/js.translator/testData/box/number/intMod.kt");
}
@Test
@TestMetadata("intOverflow.kt")
public void testIntOverflow() throws Exception {
@@ -83,6 +83,14 @@ public enum PrimitiveBinaryOperationFIF implements FunctionIntrinsicFactory {
}
};
private static final BinaryOperationIntrinsicBase NUMBER_REM_INTRINSIC = new BinaryOperationIntrinsicBase() {
@NotNull
@Override
public JsExpression doApply(@NotNull JsExpression left, @NotNull JsExpression right, @NotNull TranslationContext context) {
return JsAstUtils.toInt32(JsAstUtils.mod(left, right));
}
};
@NotNull
private static final BinaryOperationIntrinsicBase BUILTINS_COMPARE_TO_INTRINSIC = new BinaryOperationIntrinsicBase() {
@NotNull
@@ -120,6 +128,7 @@ public enum PrimitiveBinaryOperationFIF implements FunctionIntrinsicFactory {
private static final DescriptorPredicate BOOLEAN_OPERATIONS = pattern("Boolean.or|and|xor");
private static final DescriptorPredicate STRING_PLUS = pattern("String.plus");
private static final DescriptorPredicate INT_MULTIPLICATION = pattern("Int.times(Int)");
private static final DescriptorPredicate NUMBER_REM = pattern("Byte|Short|Int.rem(Byte|Short|Int)");
private static final DescriptorPredicate CHAR_RANGE_TO = pattern("Char.rangeTo(Char)");
private static final DescriptorPredicate NUMBER_RANGE_TO = pattern("Byte|Short|Int.rangeTo(Byte|Short|Int)");
@@ -161,6 +170,9 @@ public enum PrimitiveBinaryOperationFIF implements FunctionIntrinsicFactory {
if (INT_MULTIPLICATION.test(descriptor)) {
return INT_MULTIPLICATION_INTRINSIC;
}
if (NUMBER_REM.test(descriptor)) {
return NUMBER_REM_INTRINSIC;
}
if (NUMBER_RANGE_TO.test(descriptor)) {
return new RangeToIntrinsic(descriptor);
}
-2
View File
@@ -1,5 +1,3 @@
// DONT_TARGET_EXACT_BACKEND: JS
fun testModNegativeInt() {
// wrapper prevents the constants folding
infix fun Int.myModInt(y: Int): Any = this % y