From 3cd406a155d2d7c7a52803591a96f57b3a7192ef Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Wed, 18 Jul 2012 13:31:37 +0400 Subject: [PATCH] Integer conversions (to* methods). Add NumberTest, NumberConversionFIF. --- .../test/semantics/NativeInteropTest.java | 2 +- .../k2js/test/semantics/NumberTest.java | 32 +++++++++ .../functions/FunctionIntrinsics.java | 1 + .../factories/NumberConversionFIF.java | 69 +++++++++++++++++++ .../functions/patterns/PatternBuilder.java | 7 ++ .../testFiles/number/cases/intConversions.kt | 21 ++++++ 6 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java create mode 100644 js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/NumberConversionFIF.java create mode 100644 js/js.translator/testFiles/number/cases/intConversions.kt diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java index abfe1f76f38..aaf68d3835f 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NativeInteropTest.java @@ -32,7 +32,7 @@ public final class NativeInteropTest extends SingleFileTranslationTest { private static final String NATIVE = "native/"; public NativeInteropTest() { - super("native/"); + super(NATIVE); } @NotNull diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java new file mode 100644 index 00000000000..4bf7397f930 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2012 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.test.semantics; + +import org.jetbrains.k2js.test.SingleFileTranslationTest; + +/** + * @author Pavel Talanov + */ +public final class NumberTest extends SingleFileTranslationTest { + public NumberTest() { + super("number/"); + } + + public void testIntConversions() throws Exception { + fooBoxTest(); + } +} 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 fe17f2beada..e2178883a64 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 @@ -67,6 +67,7 @@ public final class FunctionIntrinsics { register(StringOperationFIF.INSTANCE); register(ArrayFIF.INSTANCE); register(TopLevelFIF.INSTANCE); + register(NumberConversionFIF.INSTANCE); } private boolean 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 new file mode 100644 index 00000000000..7d34ee3f8f0 --- /dev/null +++ b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/NumberConversionFIF.java @@ -0,0 +1,69 @@ +/* + * Copyright 2010-2012 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 closurecompiler.internal.com.google.common.collect.Sets; +import com.google.dart.compiler.backend.js.ast.JsExpression; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.resolve.name.Name; +import org.jetbrains.k2js.translate.context.TranslationContext; +import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic; +import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NameChecker; + +import java.util.HashSet; +import java.util.List; + +import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.*; +import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern; + +/** + * @author Pavel Talanov + */ +public final class NumberConversionFIF { + @NotNull + private static final NameChecker CONVERSIONS; + + static { + HashSet supportedConversions = Sets.newHashSet(NUMBER_CONVERSIONS); + //TODO: support longs and chars + supportedConversions.remove(CHAR); + supportedConversions.remove(LONG); + CONVERSIONS = new NameChecker(supportedConversions); + } + + @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 FunctionIntrinsicFactory INSTANCE = FIFBuilder.start() + .add(pattern("Int", CONVERSIONS), RETURN_RECEIVER) + .build(); + + private NumberConversionFIF() { + } +} diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/patterns/PatternBuilder.java b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/patterns/PatternBuilder.java index f2c9f3404fc..80c56969cad 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/patterns/PatternBuilder.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/patterns/PatternBuilder.java @@ -42,6 +42,13 @@ public final class PatternBuilder { return pattern(checkers); } + @NotNull + public static Pattern pattern(@NotNull String stringWithPattern, @NotNull NameChecker checker) { + List checkers = Lists.newArrayList(parseStringAsCheckerList(stringWithPattern)); + checkers.add(checker); + return pattern(checkers); + } + @NotNull public static Pattern pattern(@NotNull String string) { List checkers = parseStringAsCheckerList(string); diff --git a/js/js.translator/testFiles/number/cases/intConversions.kt b/js/js.translator/testFiles/number/cases/intConversions.kt new file mode 100644 index 00000000000..e65ba95b700 --- /dev/null +++ b/js/js.translator/testFiles/number/cases/intConversions.kt @@ -0,0 +1,21 @@ +package foo + +fun box(): Boolean { + val c: Int = 3 + if (c.toDouble() != 3.0) { + return false + } + if (c.toFloat() != 3.toFloat()) { + return false + } + if (c.toByte() != 3.toByte()) { + return false + } + if (c.toInt() != 3) { + return false + } + if (c.toShort() != 3.toShort()) { + return false + } + return true +} \ No newline at end of file