001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.k2js.translate.intrinsic.functions.basic;
018    
019    import com.google.dart.compiler.backend.js.ast.JsExpression;
020    import com.google.dart.compiler.backend.js.ast.JsInvocation;
021    import com.google.dart.compiler.backend.js.ast.JsNameRef;
022    import com.intellij.util.SmartList;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.annotations.Nullable;
025    import org.jetbrains.k2js.translate.context.TranslationContext;
026    
027    import java.util.List;
028    
029    import static org.jetbrains.k2js.translate.utils.ErrorReportingUtils.atLocation;
030    
031    public final class CallStandardMethodIntrinsic extends FunctionIntrinsic {
032        @NotNull
033        private final JsNameRef methodName;
034    
035        private final boolean receiverShouldBeNotNull;
036        private final int expectedParamsNumber;
037    
038        public CallStandardMethodIntrinsic(@NotNull JsNameRef methodName, boolean receiverShouldBeNotNull, int expectedParamsNumber) {
039            this.methodName = methodName;
040            this.receiverShouldBeNotNull = receiverShouldBeNotNull;
041            this.expectedParamsNumber = expectedParamsNumber;
042        }
043    
044        @NotNull
045        @Override
046        public JsExpression apply(@Nullable JsExpression receiver,
047                @NotNull List<JsExpression> arguments,
048                @NotNull TranslationContext context) {
049            assert (receiver != null == receiverShouldBeNotNull);
050            assert arguments.size() == expectedParamsNumber : errorMessage(receiver, arguments);
051            return new JsInvocation(methodName, composeArguments(receiver, arguments));
052        }
053    
054        @NotNull
055        private String errorMessage(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments) {
056            return "Incorrect number of arguments " + arguments.size() + " when expected " + expectedParamsNumber + " on method " + methodName + " " +
057                   atLocation(receiver, arguments);
058        }
059    
060        @NotNull
061        private static List<JsExpression> composeArguments(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments) {
062            if (receiver != null) {
063                List<JsExpression> args = new SmartList<JsExpression>(receiver);
064                args.addAll(arguments);
065                return args;
066            }
067            else {
068                return arguments;
069            }
070        }
071    }