Refactoring of K2JSTranslator and neighboring classes

Drop GenerationUtils
Introduce MainCallParameters
Generate calls to main function together with the other code in contrast to as text afterwards
Enhance tests a bit
This commit is contained in:
pTalanov
2012-05-05 18:50:02 +04:00
parent 9681a61fd2
commit 869a8ab1ba
12 changed files with 210 additions and 149 deletions
@@ -93,8 +93,8 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
private static ExitCode translateAndGenerateOutputFile(@NotNull PrintingMessageCollector messageCollector,
@NotNull JetCoreEnvironment environmentForJS, @NotNull Config config, @NotNull String outputFile) {
try {
K2JSTranslator.translateWithCallToMainAndSaveToFile(environmentForJS.getSourceFiles(), outputFile, config,
environmentForJS.getProject());
K2JSTranslator.translateWithCallToMainAndSaveToFile(environmentForJS.getSourceFiles(), outputFile, config
);
}
catch (Exception e) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Exception while translating:\n" + e.getMessage(),
@@ -29,16 +29,11 @@ public class JetMainDetector {
private JetMainDetector() {
}
public static boolean hasMain(List<JetDeclaration> declarations) {
for (JetDeclaration declaration : declarations) {
if (declaration instanceof JetNamedFunction) {
if (isMain((JetNamedFunction) declaration)) return true;
}
}
return false;
public static boolean hasMain(@NotNull List<JetDeclaration> declarations) {
return findMainFunction(declarations) != null;
}
public static boolean isMain(JetNamedFunction function) {
public static boolean isMain(@NotNull JetNamedFunction function) {
if ("main".equals(function.getName())) {
List<JetParameter> parameters = function.getValueParameters();
if (parameters.size() == 1) {
@@ -52,10 +47,24 @@ public class JetMainDetector {
}
@Nullable
public static JetFile getFileWithMain(@NotNull List<JetFile> files) {
public static JetNamedFunction getMainFunction(@NotNull List<JetFile> files) {
for (JetFile file : files) {
if (hasMain(file.getDeclarations())) {
return file;
JetNamedFunction mainFunction = findMainFunction(file.getDeclarations());
if (mainFunction != null) {
return mainFunction;
}
}
return null;
}
@Nullable
private static JetNamedFunction findMainFunction(@NotNull List<JetDeclaration> declarations) {
for (JetDeclaration declaration : declarations) {
if (declaration instanceof JetNamedFunction) {
JetNamedFunction candidateFunction = (JetNamedFunction) declaration;
if (isMain(candidateFunction)) {
return candidateFunction;
}
}
}
return null;
@@ -65,8 +65,8 @@ public final class K2JSRunnerUtils {
String outputFilePath = constructPathToGeneratedFile(project, outputDirPath);
K2JSTranslator.translateWithCallToMainAndSaveToFile(kotlinFiles,
outputFilePath,
new IDEAConfig(project),
project);
new IDEAConfig(project)
);
notifySuccess(outputDirPath);
}
@@ -87,14 +87,14 @@ public final class K2JSRunnerUtils {
@NotNull
private static List<JetFile> getJetFiles(@NotNull Collection<VirtualFile> virtualFiles,
@NotNull Project project) {
@NotNull Project project) {
List<JetFile> kotlinFiles = Lists.newArrayList();
PsiManager psiManager = PsiManager.getInstance(project);
for (VirtualFile virtualFile : virtualFiles) {
PsiFile psiFile = psiManager.findFile(virtualFile);
if (psiFile instanceof JetFile) {
kotlinFiles.add((JetFile)psiFile);
kotlinFiles.add((JetFile) psiFile);
}
}
return kotlinFiles;
@@ -17,6 +17,7 @@
package org.jetbrains.k2js.test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker;
import java.util.List;
@@ -36,11 +37,11 @@ public abstract class MultipleFilesTranslationTest extends BasicTest {
protected void generateJsFromDir(@NotNull String dirName) throws Exception {
List<String> fullFilePaths = getAllFilesInDir(getInputFilePath(dirName));
translateFiles(getProject(), fullFilePaths, getOutputFilePath(dirName + ".kt"));
translateFiles(getProject(), fullFilePaths, getOutputFilePath(dirName + ".kt"), MainCallParameters.noCall());
}
protected void runMultiFileTest(@NotNull String dirName, @NotNull String namespaceName,
@NotNull String functionName, @NotNull Object expectedResult) throws Exception {
@NotNull String functionName, @NotNull Object expectedResult) throws Exception {
generateJsFromDir(dirName);
runRhinoTest(withAdditionalFiles(getOutputFilePath(dirName + ".kt")),
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
@@ -16,13 +16,13 @@
package org.jetbrains.k2js.test;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker;
import org.jetbrains.k2js.test.rhino.RhinoSystemOutputChecker;
import org.jetbrains.k2js.test.utils.TranslationUtils;
import java.util.Arrays;
import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest;
import static org.jetbrains.k2js.test.utils.JsTestUtils.readFile;
@@ -37,14 +37,14 @@ public abstract class SingleFileTranslationTest extends BasicTest {
}
public void runFunctionOutputTest(String filename, String namespaceName,
String functionName, Object expectedResult) throws Exception {
generateJsFromFile(filename);
String functionName, Object expectedResult) throws Exception {
generateJsFromFile(filename, MainCallParameters.noCall());
runRhinoTest(withAdditionalFiles(getOutputFilePath(filename)),
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
}
protected void generateJsFromFile(@NotNull String filename) throws Exception {
TranslationUtils.translateFile(getProject(), getInputFilePath(filename), getOutputFilePath(filename));
protected void generateJsFromFile(@NotNull String filename, @NotNull MainCallParameters mainCallParameters) throws Exception {
TranslationUtils.translateFile(getProject(), getInputFilePath(filename), getOutputFilePath(filename), mainCallParameters);
}
public void checkFooBoxIsTrue(@NotNull String filename) throws Exception {
@@ -56,9 +56,9 @@ public abstract class SingleFileTranslationTest extends BasicTest {
}
protected void checkOutput(@NotNull String filename, @NotNull String expectedResult, @NotNull String... args) throws Exception {
generateJsFromFile(filename);
generateJsFromFile(filename, MainCallParameters.mainWithArguments(Lists.newArrayList(args)));
runRhinoTest(withAdditionalFiles(getOutputFilePath(filename)),
new RhinoSystemOutputChecker(expectedResult, Arrays.asList(args)));
new RhinoSystemOutputChecker(expectedResult));
}
protected void performTestWithMain(@NotNull String testName, @NotNull String testId, @NotNull String... args) throws Exception {
@@ -17,13 +17,9 @@
package org.jetbrains.k2js.test.rhino;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.translate.context.Namer;
import org.jetbrains.k2js.utils.GenerationUtils;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import java.util.List;
import static org.junit.Assert.assertTrue;
/**
@@ -31,39 +27,30 @@ import static org.junit.Assert.assertTrue;
*/
public final class RhinoSystemOutputChecker implements RhinoResultChecker {
private final List<String> arguments;
private final String expectedResult;
public RhinoSystemOutputChecker(String expectedResult, List<String> arguments) {
public RhinoSystemOutputChecker(@NotNull String expectedResult) {
this.expectedResult = expectedResult;
this.arguments = arguments;
}
@Override
public void runChecks(@NotNull Context context, @NotNull Scriptable scope)
throws Exception {
runMain(context, scope);
String result = getSystemOutput(context, scope);
String trimmedExpected = trimSpace(expectedResult);
String trimmedActual = trimSpace(result);
// System.out.println(trimmedActual);
// System.out.println(trimmedExpected);
assertTrue("Returned:\n" + trimmedActual + "END_OF_RETURNED\nExpected:\n" + trimmedExpected
+ "END_OF_EXPECTED\n", trimmedExpected.equals(trimmedActual));
}
private String getSystemOutput(@NotNull Context context, @NotNull Scriptable scope) {
private static String getSystemOutput(@NotNull Context context, @NotNull Scriptable scope) {
Object output = context.evaluateString(scope, "Kotlin.System.output()", "test", 0, null);
assertTrue("Output should be a string.", output instanceof String);
return (String) output;
}
private void runMain(Context context, Scriptable scope) {
String callToMain = GenerationUtils.generateCallToMain(Namer.getRootNamespaceName(), arguments);
context.evaluateString(scope, callToMain, "function call", 0, null);
}
public String trimSpace(String s) {
public static String trimSpace(@NotNull String s) {
String[] choppedUpString = s.trim().split("\\s");
StringBuilder sb = new StringBuilder();
for (String word : choppedUpString) {
@@ -22,6 +22,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.facade.K2JSTranslator;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.generate.CodeGenerator;
import org.jetbrains.k2js.test.config.TestConfig;
@@ -44,19 +45,21 @@ public final class TranslationUtils {
private static /*var*/ K2JSTranslator translator = null;
public static void translateFile(@NotNull Project project, @NotNull String inputFile,
@NotNull String outputFile) throws Exception {
translateFiles(project, Collections.singletonList(inputFile), outputFile);
@NotNull String outputFile, @NotNull MainCallParameters mainCallParameters) throws Exception {
translateFiles(project, Collections.singletonList(inputFile), outputFile, mainCallParameters);
}
public static void translateFiles(@NotNull Project project, @NotNull List<String> inputFiles,
@NotNull String outputFile) throws Exception {
@NotNull String outputFile, @NotNull MainCallParameters mainCallParameters) throws Exception {
List<JetFile> psiFiles = createPsiFileList(inputFiles, project);
JsProgram program = getTranslator(project).generateProgram(psiFiles);
JsProgram program = getTranslator(project).generateProgram(psiFiles, mainCallParameters);
FileWriter writer = new FileWriter(new File(outputFile));
try {
writer.write("\"use strict\";\n");
writer.write(CodeGenerator.toString(program));
} finally {
}
finally {
writer.close();
}
}
@@ -24,20 +24,17 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import org.jetbrains.jet.plugin.JetMainDetector;
import org.jetbrains.k2js.analyze.AnalyzerFacadeForJS;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.generate.CodeGenerator;
import org.jetbrains.k2js.translate.general.Translation;
import org.jetbrains.k2js.utils.GenerationUtils;
import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import static org.jetbrains.k2js.translate.utils.PsiUtils.getNamespaceName;
//TODO: clean up the code
/**
@@ -48,21 +45,19 @@ import static org.jetbrains.k2js.translate.utils.PsiUtils.getNamespaceName;
public final class K2JSTranslator {
public static void translateWithCallToMainAndSaveToFile(@NotNull List<JetFile> files,
@NotNull String outputPath,
@NotNull Config config,
@NotNull Project project) throws Exception {
@NotNull String outputPath,
@NotNull Config config) throws Exception {
K2JSTranslator translator = new K2JSTranslator(config);
String programCode = translator.generateProgramCode(files) + "\n";
JetFile fileWithMain = JetMainDetector.getFileWithMain(files);
if (fileWithMain == null) {
throw new RuntimeException("No file with main detected.");
}
String callToMain = generateCallToMain(fileWithMain, "");
String programCode = translator.generateProgramCode(files, MainCallParameters.mainWithoutArguments()) + "\n";
writeCodeToFile(outputPath, programCode);
}
private static void writeCodeToFile(@NotNull String outputPath, @NotNull String programCode) throws IOException {
File file = new File(outputPath);
FileUtil.createParentDirs(file);
FileWriter writer = new FileWriter(file);
try {
writer.write(programCode + callToMain);
writer.write(programCode);
}
finally {
writer.close();
@@ -77,46 +72,37 @@ public final class K2JSTranslator {
this.config = config;
}
//TODO: refactor
//TODO: web demo related method
@SuppressWarnings("UnusedDeclaration")
@NotNull
public String translateStringWithCallToMain(@NotNull String programText, @NotNull String argumentsString) {
JetFile file = JetFileUtils.createPsiFile("test", programText, getProject());
String programCode = generateProgramCode(file) + "\n";
String programCode = generateProgramCode(file, MainCallParameters.mainWithArguments(parseString(argumentsString))) + "\n";
String flushOutput = "Kotlin.System.flush();\n";
String callToMain = generateCallToMain(file, argumentsString);
String programOutput = "Kotlin.System.output();\n";
return programCode + flushOutput + callToMain + programOutput;
return flushOutput + programCode + programOutput;
}
@NotNull
public String generateProgramCode(@NotNull JetFile psiFile) {
JsProgram program = generateProgram(Arrays.asList(psiFile));
public String generateProgramCode(@NotNull JetFile file, @NotNull MainCallParameters mainCallParameters) {
JsProgram program = generateProgram(Arrays.asList(file), mainCallParameters);
CodeGenerator generator = new CodeGenerator();
return generator.generateToString(program);
}
@NotNull
public String generateProgramCode(@NotNull List<JetFile> files) {
JsProgram program = generateProgram(files);
public String generateProgramCode(@NotNull List<JetFile> files, @NotNull MainCallParameters mainCallParameters) {
JsProgram program = generateProgram(files, mainCallParameters);
CodeGenerator generator = new CodeGenerator();
return generator.generateToString(program);
}
@NotNull
public JsProgram generateProgram(@NotNull List<JetFile> filesToTranslate) {
public JsProgram generateProgram(@NotNull List<JetFile> filesToTranslate, @NotNull MainCallParameters mainCallParameters) {
JetStandardLibrary.initialize(config.getProject());
BindingContext bindingContext = AnalyzerFacadeForJS.analyzeFilesAndCheckErrors(filesToTranslate, config);
Collection<JetFile> files = AnalyzerFacadeForJS.withJsLibAdded(filesToTranslate, config);
return Translation.generateAst(bindingContext, Lists.newArrayList(files));
}
@NotNull
public static String generateCallToMain(@NotNull JetFile file, @NotNull String argumentString) {
String namespaceName = getNamespaceName(file);
List<String> arguments = parseString(argumentString);
return GenerationUtils.generateCallToMain(namespaceName, arguments);
return Translation.generateAst(bindingContext, Lists.newArrayList(files), mainCallParameters);
}
//TODO: util
@@ -0,0 +1,85 @@
/*
* 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.facade;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
/**
* @author Pavel Talanov
*/
public abstract class MainCallParameters {
@NotNull
public static MainCallParameters noCall() {
return new MainCallParameters() {
@NotNull
@Override
public List<String> arguments() {
throw new UnsupportedOperationException("#arguments");
}
@Override
public boolean shouldBeGenerated() {
return false;
}
};
}
@NotNull
public static MainCallParameters mainWithoutArguments() {
return new MainCallParameters() {
@NotNull
@Override
public List<String> arguments() {
return Collections.emptyList();
}
@Override
public boolean shouldBeGenerated() {
return true;
}
};
}
@NotNull
public static MainCallParameters mainWithArguments(@NotNull final List<String> parameters) {
return new MainCallParameters() {
@NotNull
@Override
public List<String> arguments() {
return parameters;
}
@Override
public boolean shouldBeGenerated() {
return true;
}
};
}
public abstract boolean shouldBeGenerated();
@NotNull
public abstract List<String> arguments();
}
@@ -20,10 +20,12 @@ import com.google.dart.compiler.backend.js.JsNamer;
import com.google.dart.compiler.backend.js.JsPrettyNamer;
import com.google.dart.compiler.backend.js.ast.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.translate.context.StaticContext;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.declaration.ClassTranslator;
@@ -34,14 +36,18 @@ import org.jetbrains.k2js.translate.expression.PatternTranslator;
import org.jetbrains.k2js.translate.expression.WhenTranslator;
import org.jetbrains.k2js.translate.initializer.ClassInitializerTranslator;
import org.jetbrains.k2js.translate.initializer.NamespaceInitializerTranslator;
import org.jetbrains.k2js.translate.reference.CallBuilder;
import org.jetbrains.k2js.translate.utils.JsAstUtils;
import org.jetbrains.k2js.translate.utils.dangerous.DangerousData;
import org.jetbrains.k2js.translate.utils.dangerous.DangerousTranslator;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.convertToExpression;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.convertToStatement;
import static org.jetbrains.jet.plugin.JetMainDetector.getMainFunction;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getFunctionDescriptor;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.*;
import static org.jetbrains.k2js.translate.utils.dangerous.DangerousData.collect;
/**
@@ -57,7 +63,7 @@ public final class Translation {
@NotNull
public static FunctionTranslator functionTranslator(@NotNull JetDeclarationWithBody function,
@NotNull TranslationContext context) {
@NotNull TranslationContext context) {
return FunctionTranslator.newInstance(function, context);
}
@@ -68,8 +74,8 @@ public final class Translation {
@NotNull
public static JsInvocation translateClassDeclaration(@NotNull JetClass classDeclaration,
@NotNull Map<JsName, JsName> aliasingMap,
@NotNull TranslationContext context) {
@NotNull Map<JsName, JsName> aliasingMap,
@NotNull TranslationContext context) {
return ClassTranslator.generateClassCreationExpression(classDeclaration, aliasingMap, context);
}
@@ -99,48 +105,77 @@ public final class Translation {
@NotNull
public static JsExpression translateAsExpression(@NotNull JetExpression expression,
@NotNull TranslationContext context) {
@NotNull TranslationContext context) {
return convertToExpression(translateExpression(expression, context));
}
@NotNull
public static JsStatement translateAsStatement(@NotNull JetExpression expression,
@NotNull TranslationContext context) {
@NotNull TranslationContext context) {
return convertToStatement(translateExpression(expression, context));
}
@NotNull
public static JsNode translateWhenExpression(@NotNull JetWhenExpression expression,
@NotNull TranslationContext context) {
@NotNull TranslationContext context) {
return WhenTranslator.translateWhenExpression(expression, context);
}
//TODO: see if generate*Initializer methods fit somewhere else
@NotNull
public static JsPropertyInitializer generateClassInitializerMethod(@NotNull JetClassOrObject classDeclaration,
@NotNull TranslationContext context) {
@NotNull TranslationContext context) {
final ClassInitializerTranslator classInitializerTranslator = new ClassInitializerTranslator(classDeclaration, context);
return classInitializerTranslator.generateInitializeMethod();
}
@NotNull
public static JsPropertyInitializer generateNamespaceInitializerMethod(@NotNull NamespaceDescriptor namespace,
@NotNull TranslationContext context) {
@NotNull TranslationContext context) {
final NamespaceInitializerTranslator namespaceInitializerTranslator = new NamespaceInitializerTranslator(namespace, context);
return namespaceInitializerTranslator.generateInitializeMethod();
}
@NotNull
public static JsProgram generateAst(@NotNull BindingContext bindingContext,
@NotNull List<JetFile> files) {
@NotNull List<JetFile> files, @NotNull MainCallParameters mainCallParameters) {
//TODO: move some of the code somewhere
JetStandardLibrary standardLibrary = JetStandardLibrary.getInstance();
StaticContext staticContext = StaticContext.generateStaticContext(standardLibrary, bindingContext);
JsBlock block = staticContext.getProgram().getFragmentBlock(0);
TranslationContext context = TranslationContext.rootContext(staticContext);
block.getStatements().addAll(translateFiles(files, context));
if (mainCallParameters.shouldBeGenerated()) {
block.getStatements().add(generateCallToMain(context, files, mainCallParameters.arguments()));
}
JsNamer namer = new JsPrettyNamer();
namer.exec(context.program());
return context.program();
}
@NotNull
private static JsStatement generateCallToMain(@NotNull TranslationContext context, @NotNull List<JetFile> files,
@NotNull List<String> arguments) {
JetNamedFunction mainFunction = getMainFunction(files);
//TODO: throw correct exception
assert mainFunction != null;
JsInvocation translatedCall = generateInvocation(context, mainFunction);
setArguments(context, arguments, translatedCall);
return translatedCall.makeStmt();
}
@NotNull
private static JsInvocation generateInvocation(@NotNull TranslationContext context, @NotNull JetNamedFunction mainFunction) {
FunctionDescriptor functionDescriptor = getFunctionDescriptor(context.bindingContext(), mainFunction);
JsExpression translatedCall = CallBuilder.build(context).descriptor(functionDescriptor).translate();
assert translatedCall instanceof JsInvocation;
return (JsInvocation) translatedCall;
}
private static void setArguments(@NotNull TranslationContext context, @NotNull List<String> arguments,
@NotNull JsInvocation translatedCall) {
JsArrayLiteral arrayLiteral = new JsArrayLiteral();
arrayLiteral.getExpressions().addAll(toStringLiteralList(arguments, context.program()));
JsAstUtils.setArguments(translatedCall, Collections.<JsExpression>singletonList(arrayLiteral));
}
}
@@ -21,10 +21,7 @@ import com.google.dart.compiler.backend.js.ast.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
/**
* @author Pavel Talanov
@@ -83,11 +80,6 @@ public final class JsAstUtils {
return new JsPrefixOperation(JsUnaryOperator.NOT, expression);
}
@NotNull
public static JsStatement newAssignmentStatement(@NotNull JsNameRef nameRef, @NotNull JsExpression expr) {
return convertToStatement(new JsBinaryOperation(JsBinaryOperator.ASG, nameRef, expr));
}
@NotNull
public static JsBinaryOperation and(@NotNull JsExpression op1, @NotNull JsExpression op2) {
return new JsBinaryOperation(JsBinaryOperator.AND, op1, op2);
@@ -165,9 +157,9 @@ public final class JsAstUtils {
@NotNull
public static JsFor generateForExpression(@NotNull JsVars initExpression,
@NotNull JsExpression condition,
@NotNull JsExpression incrExpression,
@NotNull JsStatement body) {
@NotNull JsExpression condition,
@NotNull JsExpression incrExpression,
@NotNull JsStatement body) {
JsFor result = new JsFor();
result.setInitVars(initExpression);
result.setCondition(condition);
@@ -271,4 +263,13 @@ public final class JsAstUtils {
correspondingFunction.setBody(new JsBlock());
return correspondingFunction;
}
@NotNull
public static List<JsExpression> toStringLiteralList(@NotNull List<String> strings, @NotNull JsProgram program) {
ArrayList<JsExpression> result = Lists.newArrayList();
for (String str : strings) {
result.add(program.getStringLiteral(str));
}
return result;
}
}
@@ -1,46 +0,0 @@
/*
* 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.utils;
import org.jetbrains.annotations.NotNull;
import java.util.List;
//TODO: very thin class
/**
* @author Pavel Talanov
*/
public final class GenerationUtils {
private GenerationUtils() {
}
@NotNull
public static String generateCallToMain(@NotNull String namespaceName, @NotNull List<String> arguments) {
String constructArguments = "var args = [];\n";
int index = 0;
for (String argument : arguments) {
constructArguments = constructArguments + "args[" + index + "]= \"" + argument + "\";\n";
index++;
}
String callMain = namespaceName + ".main(args);\n";
return constructArguments + callMain;
}
}