diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/Intrinsics.java b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/Intrinsics.java index 9893e8f489d..f6bf16525f3 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/Intrinsics.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/Intrinsics.java @@ -26,7 +26,10 @@ import org.jetbrains.jet.lang.types.JetStandardLibrary; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.PrimitiveType; import org.jetbrains.jet.lexer.JetToken; -import org.jetbrains.k2js.translate.intrinsic.array.*; +import org.jetbrains.k2js.translate.intrinsic.array.ArrayGetIntrinsic; +import org.jetbrains.k2js.translate.intrinsic.array.ArraySetIntrinsic; +import org.jetbrains.k2js.translate.intrinsic.array.ArraySizeIntrinsic; +import org.jetbrains.k2js.translate.intrinsic.array.CallStandardMethodIntrinsic; import org.jetbrains.k2js.translate.intrinsic.primitive.*; import org.jetbrains.k2js.translate.intrinsic.string.CharAtIntrinsic; import org.jetbrains.k2js.translate.intrinsic.string.LengthIntrinsic; @@ -94,9 +97,8 @@ public final class Intrinsics { } private void declareNullConstructorIntrinsic() { - //TODO: FunctionDescriptor nullArrayConstructor = library.getLibraryScope().getFunctions("Array").iterator().next(); - functionIntrinsics.put(nullArrayConstructor, ArrayNullConstructorIntrinsic.INSTANCE); + functionIntrinsics.put(nullArrayConstructor, new CallStandardMethodIntrinsic("Kotlin.nullArray", false, 1)); } //TODO: some dangerous operation unchecked here @@ -109,12 +111,12 @@ public final class Intrinsics { PropertyDescriptor sizeProperty = getPropertyByName(arrayMemberScope, "size"); functionIntrinsics.put(sizeProperty.getGetter(), ArraySizeIntrinsic.INSTANCE); PropertyDescriptor indicesProperty = getPropertyByName(arrayMemberScope, "indices"); - functionIntrinsics.put(indicesProperty.getGetter(), ArrayIndicesIntrinsic.INSTANCE); + functionIntrinsics.put(indicesProperty.getGetter(), new CallStandardMethodIntrinsic("Kotlin.arrayIndices", true, 0)); FunctionDescriptor iteratorFunction = getFunctionByName(arrayMemberScope, "iterator"); - functionIntrinsics.put(iteratorFunction, ArrayIteratorIntrinsic.INSTANCE); + functionIntrinsics.put(iteratorFunction, new CallStandardMethodIntrinsic("Kotlin.arrayIterator", true, 0)); ConstructorDescriptor arrayConstructor = ((ClassDescriptor) arrayMemberScope.getContainingDeclaration()).getConstructors().iterator().next(); - functionIntrinsics.put(arrayConstructor, ArrayFunctionConstructorIntrinsic.INSTANCE); + functionIntrinsics.put(arrayConstructor, new CallStandardMethodIntrinsic("Kotlin.arrayFromFun", false, 2)); } private List getLibraryArrayTypes() { diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayIndicesIntrinsic.java b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayIndicesIntrinsic.java deleted file mode 100644 index c9e9f9d1005..00000000000 --- a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayIndicesIntrinsic.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2000-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.array; - -import com.google.dart.compiler.backend.js.ast.JsExpression; -import com.google.dart.compiler.backend.js.ast.JsNameRef; -import com.google.dart.compiler.util.AstUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.k2js.translate.context.TranslationContext; -import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic; - -import java.util.List; - -/** - * @author Pavel Talanov - */ -public enum ArrayIndicesIntrinsic implements FunctionIntrinsic { - - INSTANCE; - - - @NotNull - @Override - public JsExpression apply(@Nullable JsExpression receiver, @NotNull List arguments, - @NotNull TranslationContext context) { - assert receiver != null; - assert arguments.size() == 0; - //TODO: provide better mechanism - JsNameRef iteratorFunName = AstUtil.newQualifiedNameRef("Kotlin.arrayIndices"); - return AstUtil.newInvocation(iteratorFunName, receiver); - } -} \ No newline at end of file diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayIteratorIntrinsic.java b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayIteratorIntrinsic.java deleted file mode 100644 index cc04aa26246..00000000000 --- a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayIteratorIntrinsic.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2000-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.array; - -import com.google.dart.compiler.backend.js.ast.JsExpression; -import com.google.dart.compiler.backend.js.ast.JsNameRef; -import com.google.dart.compiler.util.AstUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.k2js.translate.context.TranslationContext; -import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic; - -import java.util.List; - -/** - * @author Pavel Talanov - */ -public enum ArrayIteratorIntrinsic implements FunctionIntrinsic { - - INSTANCE; - - @NotNull - @Override - public JsExpression apply(@Nullable JsExpression receiver, @NotNull List arguments, - @NotNull TranslationContext context) { - assert receiver != null; - assert arguments.size() == 0; - //TODO: provide better mechanism - JsNameRef iteratorFunName = AstUtil.newQualifiedNameRef("Kotlin.arrayIterator"); - return AstUtil.newInvocation(iteratorFunName, receiver); - } -} diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayNullConstructorIntrinsic.java b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayNullConstructorIntrinsic.java deleted file mode 100644 index 3d590da0f2e..00000000000 --- a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayNullConstructorIntrinsic.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2000-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.array; - -import com.google.dart.compiler.backend.js.ast.JsExpression; -import com.google.dart.compiler.backend.js.ast.JsNameRef; -import com.google.dart.compiler.util.AstUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.k2js.translate.context.TranslationContext; -import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic; - -import java.util.List; - -import static org.jetbrains.k2js.translate.utils.JsAstUtils.newInvocation; - -/** - * @author Pavel Talanov - */ -public enum ArrayNullConstructorIntrinsic implements FunctionIntrinsic { - - INSTANCE; - - //TODO: implement function passing to array constructor - @NotNull - @Override - public JsExpression apply(@Nullable JsExpression receiver, @NotNull List arguments, - @NotNull TranslationContext context) { - assert receiver == null; - assert arguments.size() == 1; - //TODO: provide better mechanism - JsNameRef nullArrayFunName = AstUtil.newQualifiedNameRef("Kotlin.nullArray"); - return newInvocation(nullArrayFunName, arguments); - } -} diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayFunctionConstructorIntrinsic.java b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/CallStandardMethodIntrinsic.java similarity index 54% rename from js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayFunctionConstructorIntrinsic.java rename to js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/CallStandardMethodIntrinsic.java index 2a025280241..05d70508625 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/ArrayFunctionConstructorIntrinsic.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/array/CallStandardMethodIntrinsic.java @@ -16,6 +16,7 @@ package org.jetbrains.k2js.translate.intrinsic.array; +import com.google.common.collect.Lists; import com.google.dart.compiler.backend.js.ast.JsExpression; import com.google.dart.compiler.backend.js.ast.JsNameRef; import com.google.dart.compiler.util.AstUtil; @@ -31,20 +32,42 @@ import static org.jetbrains.k2js.translate.utils.JsAstUtils.newInvocation; /** * @author Pavel Talanov */ -public enum ArrayFunctionConstructorIntrinsic implements FunctionIntrinsic { +public final class CallStandardMethodIntrinsic implements FunctionIntrinsic { - INSTANCE; + @NotNull + private final String methodName; + private final boolean receiverShouldBeNotNull; + private final int expectedParamsNumber; + + public CallStandardMethodIntrinsic(@NotNull String methodName, boolean receiverShouldBeNotNull, int expectedParamsNumber) { + this.methodName = methodName; + this.receiverShouldBeNotNull = receiverShouldBeNotNull; + this.expectedParamsNumber = expectedParamsNumber; + } @NotNull @Override public JsExpression apply(@Nullable JsExpression receiver, @NotNull List arguments, @NotNull TranslationContext context) { - assert receiver == null; - assert arguments.size() == 2; - //TODO: provide better mechanism - JsNameRef iteratorFunName = AstUtil.newQualifiedNameRef("Kotlin.arrayFromFun"); - return newInvocation(iteratorFunName, arguments); + assert (receiver != null == receiverShouldBeNotNull); + assert arguments.size() == expectedParamsNumber; + List args = composeArguments(receiver, arguments); + JsNameRef iteratorFunName = AstUtil.newQualifiedNameRef(methodName); + return newInvocation(iteratorFunName, composeArguments(receiver, arguments)); } -} + + @NotNull + private static List composeArguments(@Nullable JsExpression receiver, @NotNull List arguments) { + if (receiver != null) { + List args = Lists.newArrayList(); + args.add(receiver); + args.addAll(arguments); + return args; + } + else { + return arguments; + } + } +} \ No newline at end of file