|
17 | 17 | package org.jetbrains.k2js.translate.intrinsic.functions.factories;
|
18 | 18 |
|
19 | 19 | import closurecompiler.internal.com.google.common.collect.Sets;
|
| 20 | +import com.google.dart.compiler.backend.js.ast.JsBinaryOperation; |
| 21 | +import com.google.dart.compiler.backend.js.ast.JsBinaryOperator; |
20 | 22 | import com.google.dart.compiler.backend.js.ast.JsExpression;
|
| 23 | +import com.google.dart.compiler.util.AstUtil; |
21 | 24 | import org.jetbrains.annotations.NotNull;
|
22 | 25 | import org.jetbrains.annotations.Nullable;
|
23 | 26 | import org.jetbrains.jet.lang.resolve.name.Name;
|
24 | 27 | import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
|
| 28 | +import org.jetbrains.k2js.translate.context.TemporaryVariable; |
25 | 29 | import org.jetbrains.k2js.translate.context.TranslationContext;
|
26 |
| -import org.jetbrains.k2js.translate.intrinsic.functions.basic.CallStandardMethodIntrinsic; |
27 | 30 | import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
|
28 | 31 | import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
|
29 | 32 |
|
|
32 | 35 |
|
33 | 36 | import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.*;
|
34 | 37 | import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
|
| 38 | +import static org.jetbrains.k2js.translate.utils.JsAstUtils.subtract; |
35 | 39 |
|
36 | 40 | /**
|
37 | 41 | * @author Pavel Talanov
|
@@ -72,13 +76,27 @@ public JsExpression apply(@Nullable JsExpression receiver,
|
72 | 76 | public static final String INTEGER_NUMBER_TYPES = "Int|Byte|Short";
|
73 | 77 | //NOTE: treat Number as if it is floating point type
|
74 | 78 | @NotNull
|
75 |
| - public static final String FLOATING_POINT_NUMBER_TYPES = "Float|Double|Number"; |
| 79 | + private static final String FLOATING_POINT_NUMBER_TYPES = "Float|Double|Number"; |
| 80 | + @NotNull |
| 81 | + private static final FunctionIntrinsic GET_INTEGER_PART = new FunctionIntrinsic() { |
| 82 | + @NotNull |
| 83 | + @Override |
| 84 | + public JsExpression apply(@Nullable JsExpression receiver, |
| 85 | + @NotNull List<JsExpression> arguments, |
| 86 | + @NotNull TranslationContext context) { |
| 87 | + assert receiver != null; |
| 88 | + assert arguments.isEmpty(); |
| 89 | + TemporaryVariable toConvert = context.declareTemporary(receiver); |
| 90 | + JsBinaryOperation fractional = new JsBinaryOperation(JsBinaryOperator.MOD, toConvert.reference(), context.program().getNumberLiteral(1)); |
| 91 | + return AstUtil.newSequence(toConvert.assignmentExpression(), subtract(toConvert.reference(), fractional)); |
| 92 | + } |
| 93 | + }; |
76 | 94 | @NotNull
|
77 | 95 | public static final FunctionIntrinsicFactory INSTANCE = new NumberConversionFIF();
|
78 | 96 |
|
79 | 97 | private NumberConversionFIF() {
|
80 | 98 | add(pattern(INTEGER_NUMBER_TYPES, SUPPORTED_CONVERSIONS), RETURN_RECEIVER);
|
81 |
| - add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0)); |
| 99 | + add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), GET_INTEGER_PART); |
82 | 100 | add(pattern(FLOATING_POINT_NUMBER_TYPES, FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER);
|
83 | 101 | }
|
84 | 102 | }
|
0 commit comments