JS backend: fixed the division operation for integral types.

#KT-2342 fixed
This commit is contained in:
Zalim Bashorov
2013-07-22 20:37:58 +04:00
parent e8b95ca074
commit bf60cb2fee
3 changed files with 28 additions and 8 deletions
@@ -45,6 +45,11 @@ public final class NumberTest extends SingleFileTranslationTest {
fooBoxIsValue("SUCCESS");
}
// KT-2342 Type mismatch on Int division (JavaScript back-end)
public void testKt2342() throws Exception {
checkFooBoxIsOk();
}
public void testHexademicalConstant() throws Exception {
try {
fooBoxTest();
@@ -27,7 +27,6 @@ import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
import org.jetbrains.jet.lexer.JetToken;
import org.jetbrains.k2js.translate.context.TemporaryVariable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.DescriptorPredicate;
@@ -38,7 +37,7 @@ import java.util.List;
import static org.jetbrains.k2js.translate.intrinsic.functions.factories.NumberConversionFIF.INTEGER_NUMBER_TYPES;
import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.*;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.setArguments;
public enum PrimitiveBinaryOperationFIF implements FunctionIntrinsicFactory {
INSTANCE;
@@ -69,13 +68,10 @@ public enum PrimitiveBinaryOperationFIF implements FunctionIntrinsicFactory {
@NotNull List<JsExpression> arguments,
@NotNull TranslationContext context) {
assert receiver != null;
TemporaryVariable left = context.declareTemporary(receiver);
assert arguments.size() == 1;
TemporaryVariable right = context.declareTemporary(arguments.get(0));
JsBinaryOperation divRes = new JsBinaryOperation(JsBinaryOperator.DIV, left.reference(), right.reference());
JsBinaryOperation modRes = new JsBinaryOperation(JsBinaryOperator.MOD, left.reference(), right.reference());
JsBinaryOperation fractionalPart = new JsBinaryOperation(JsBinaryOperator.DIV, modRes, right.reference());
return AstUtil.newSequence(left.assignmentExpression(), right.assignmentExpression(), subtract(divRes, fractionalPart));
JsBinaryOperation div = new JsBinaryOperation(JsBinaryOperator.DIV, receiver, arguments.get(0));
JsBinaryOperation toInt32 = new JsBinaryOperation(JsBinaryOperator.BIT_OR, div, context.program().getNumberLiteral(0));
return toInt32;
}
};
@@ -0,0 +1,19 @@
package foo
fun test(a: Int, b: Int, expected: Int): String {
val result = a / b
if (expected == result) return "OK"
return "$a / $b = $result. Expected $expected"
}
fun box(): String {
var r = test(10, 3, 3)
if (r != "OK") return r
r = test(49, 6, 8)
if (r != "OK") return r
if (2133 / 3 / 7 / (91 / 5) != 5) return "2133 / 3 / 7 / (91 / 5) != 5"
return "OK"
}