diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/FunctionIntrinsics.java b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/FunctionIntrinsics.java index bdb990103c9..63a929b06e5 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/FunctionIntrinsics.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/FunctionIntrinsics.java @@ -45,7 +45,7 @@ public final class FunctionIntrinsics { register(StringOperationFIF.INSTANCE); register(ArrayFIF.INSTANCE); register(TopLevelFIF.INSTANCE); - register(NumberConversionFIF.INSTANCE); + register(NumberConversionFIF.INSTANCE$); } private void register(@NotNull FunctionIntrinsicFactory instance) { diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/NumberConversionFIF.java b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/NumberConversionFIF.java deleted file mode 100644 index 43048f76560..00000000000 --- a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/NumberConversionFIF.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 2010-2013 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.k2js.translate.intrinsic.functions.factories; - -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; -import com.google.dart.compiler.backend.js.ast.JsExpression; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; -import org.jetbrains.k2js.translate.context.TranslationContext; -import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic; -import org.jetbrains.k2js.translate.utils.JsAstUtils; - -import java.util.List; - -import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern; - -//TODO: support longs and chars -public final class NumberConversionFIF extends CompositeFIF { - - @NotNull - private static final FunctionIntrinsic RETURN_RECEIVER = new FunctionIntrinsic() { - @NotNull - @Override - public JsExpression apply( - @Nullable JsExpression receiver, - @NotNull List arguments, - @NotNull TranslationContext context - ) { - assert receiver != null; - assert arguments.isEmpty(); - return receiver; - } - }; - - @NotNull - public static final String INTEGER_NUMBER_TYPES = "Int|Byte|Short"; - - @NotNull - private static final FunctionIntrinsic TO_INT32 = new FunctionIntrinsic() { - @NotNull - @Override - public JsExpression apply( - @Nullable JsExpression receiver, - @NotNull List arguments, - @NotNull TranslationContext context - ) { - assert receiver != null; - assert arguments.isEmpty(); - return JsAstUtils.toInt32(receiver, context); - } - }; - - @NotNull - private static final FunctionIntrinsic TO_SHORT = new FunctionIntrinsic() { - @NotNull - @Override - public JsExpression apply( - @Nullable JsExpression receiver, - @NotNull List arguments, - @NotNull TranslationContext context - ) { - assert receiver != null; - assert arguments.isEmpty(); - return JsAstUtils.toShort(receiver); - } - }; - - @NotNull - private static final FunctionIntrinsic TO_BYTE = new FunctionIntrinsic() { - @NotNull - @Override - public JsExpression apply( - @Nullable JsExpression receiver, - @NotNull List arguments, - @NotNull TranslationContext context - ) { - assert receiver != null; - assert arguments.isEmpty(); - return JsAstUtils.toByte(receiver); - } - }; - - @NotNull - private static final Predicate returnReceiverPredicate = Predicates.or( - pattern("Int.toInt|toFloat|toDouble"), - pattern("Short.toShort|toInt|toFloat|toDouble"), - pattern("Byte.toByte|toShort|toInt|toFloat|toDouble"), - pattern("Float|Double|Number.toFloat|toDouble") - ); - - @NotNull - public static final FunctionIntrinsicFactory INSTANCE = new NumberConversionFIF(); - - //NOTE: treat Number as if it is floating point type - private NumberConversionFIF() { - add(returnReceiverPredicate, RETURN_RECEIVER); - add(pattern("Float|Double|Number.toInt"), TO_INT32); - add(pattern("Int|Float|Double|Number.toShort"), TO_SHORT); - add(pattern("Short|Int|Float|Double|Number.toByte"), TO_BYTE); - } -} \ No newline at end of file diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/NumberConversionFIF.kt b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/NumberConversionFIF.kt new file mode 100644 index 00000000000..2c143245acd --- /dev/null +++ b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/NumberConversionFIF.kt @@ -0,0 +1,56 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.k2js.translate.intrinsic.functions.factories + +import com.google.dart.compiler.backend.js.ast.JsExpression +import org.jetbrains.k2js.translate.context.TranslationContext +import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic +import org.jetbrains.k2js.translate.utils.JsAstUtils + +import org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern + +//TODO: support longs and chars +public object NumberConversionFIF : CompositeFIF() { + + //NOTE: treat Number as if it is floating point type + private val convertOperations: MapJsExpression> = + mapOf( + "Int.toInt|toFloat|toDouble" to { (receiver, context) -> receiver }, + "Short.toShort|toInt|toFloat|toDouble" to { (receiver, context) -> receiver }, + "Byte.toByte|toShort|toInt|toFloat|toDouble" to { (receiver, context) -> receiver }, + "Float|Double|Number.toFloat|toDouble" to { (receiver, context) -> receiver }, + + "Float|Double|Number.toInt" to { (receiver, context) -> JsAstUtils.toInt32(receiver, context) }, + "Int|Float|Double|Number.toShort" to { (receiver, context) -> JsAstUtils.toShort(receiver) }, + "Short|Int|Float|Double|Number.toByte" to { (receiver, context) -> JsAstUtils.toByte(receiver) } + ) + + fun newUnaryIntrinsic(applyFun: (receiver: JsExpression, context: TranslationContext) -> JsExpression): FunctionIntrinsic = + object : FunctionIntrinsic() { + override fun apply(receiver: JsExpression?, arguments: List, context: TranslationContext): JsExpression { + assert(receiver != null) + assert(arguments.size() == 0) + return applyFun(receiver!!, context) + } + } + + { + for((stringPattern, applyFun) in convertOperations) { + add(pattern(stringPattern), newUnaryIntrinsic(applyFun)) + } + } +} diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/PrimitiveBinaryOperationFIF.java b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/PrimitiveBinaryOperationFIF.java index 9325dc6bcfd..cf3defa0e64 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/PrimitiveBinaryOperationFIF.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/PrimitiveBinaryOperationFIF.java @@ -37,7 +37,6 @@ import org.jetbrains.k2js.translate.utils.JsAstUtils; 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.*; @@ -124,7 +123,7 @@ public enum PrimitiveBinaryOperationFIF implements FunctionIntrinsicFactory { return NUMBER_COMPARE_TO_INTRINSIC; } - if (pattern(INTEGER_NUMBER_TYPES + ".div").apply(descriptor)) { + if (pattern("Int|Short|Byte.div").apply(descriptor)) { JetType resultType = descriptor.getReturnType(); if (!KotlinBuiltIns.getInstance().getFloatType().equals(resultType) && !KotlinBuiltIns.getInstance().getDoubleType().equals(resultType))