pull https://github.com/develar/kotlin ecma5-iter3 Make most of the tests work.
Thanks to develar.
This commit is contained in:
@@ -17,27 +17,29 @@
|
||||
package org.jetbrains.jet.plugin.compiler;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.openapi.application.AccessToken;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.compiler.CompileContext;
|
||||
import com.intellij.openapi.compiler.CompileScope;
|
||||
import com.intellij.openapi.compiler.CompilerMessageCategory;
|
||||
import com.intellij.openapi.compiler.TranslatingCompiler;
|
||||
import com.intellij.openapi.application.ReadAction;
|
||||
import com.intellij.openapi.compiler.*;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.ModuleOrderEntry;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
import com.intellij.openapi.roots.OrderEntry;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.Chunk;
|
||||
import com.intellij.util.StringBuilderSpinAllocator;
|
||||
import gnu.trove.THashSet;
|
||||
import jet.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.plugin.JetFileType;
|
||||
import org.jetbrains.jet.plugin.k2jsrun.K2JSRunnerUtils;
|
||||
import org.jetbrains.jet.plugin.project.JsModuleDetector;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.jetbrains.jet.plugin.compiler.CompilerUtils.invokeExecMethod;
|
||||
import static org.jetbrains.jet.plugin.compiler.CompilerUtils.outputCompilerMessagesAndHandleExitCode;
|
||||
@@ -46,7 +48,6 @@ import static org.jetbrains.jet.plugin.compiler.CompilerUtils.outputCompilerMess
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public final class K2JSCompiler implements TranslatingCompiler {
|
||||
|
||||
@Override
|
||||
public boolean isCompilableFile(VirtualFile file, CompileContext context) {
|
||||
if (!(file.getFileType() instanceof JetFileType)) {
|
||||
@@ -115,16 +116,9 @@ public final class K2JSCompiler implements TranslatingCompiler {
|
||||
@NotNull
|
||||
private static Integer doExec(@NotNull CompileContext context, @NotNull CompilerEnvironment environment, @NotNull PrintStream out,
|
||||
@NotNull Module module) throws Exception {
|
||||
VirtualFile[] roots = ModuleRootManager.getInstance(module).getSourceRoots();
|
||||
if (roots.length != 1) {
|
||||
context.addMessage(CompilerMessageCategory.ERROR, "K2JSCompiler does not support multiple module source roots.", null, -1, -1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
VirtualFile outDir = context.getModuleOutputDirectory(module);
|
||||
String outFile = outDir == null ? null : K2JSRunnerUtils.constructPathToGeneratedFile(context.getProject(), outDir.getPath());
|
||||
|
||||
String[] commandLineArgs = constructArguments(context.getProject(), outFile, roots[0]);
|
||||
String outFile = outDir == null ? null : outDir.getPath() + "/" + module.getName() + ".js";
|
||||
String[] commandLineArgs = constructArguments(module, outFile);
|
||||
Object rc = invokeExecMethod(environment, out, context, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler");
|
||||
|
||||
if (outDir != null && !ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
@@ -134,30 +128,96 @@ public final class K2JSCompiler implements TranslatingCompiler {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String[] constructArguments(@NotNull Project project, @Nullable String outFile, @NotNull VirtualFile srcDir) {
|
||||
private static String[] constructArguments(@NotNull Module module, @Nullable String outFile) {
|
||||
VirtualFile[] sourceFiles = getSourceFiles(module);
|
||||
|
||||
ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version");
|
||||
addPathToSourcesDir(args, srcDir);
|
||||
addPathToSourcesDir(sourceFiles, args);
|
||||
addOutputPath(outFile, args);
|
||||
addLibLocationAndTarget(project, args);
|
||||
addLibLocationAndTarget(module, args);
|
||||
return ArrayUtil.toStringArray(args);
|
||||
}
|
||||
|
||||
private static void addLibLocationAndTarget(@NotNull Project project, @NotNull ArrayList<String> args) {
|
||||
Pair<String, String> libLocationAndTarget = JsModuleDetector.getLibLocationAndTargetForProject(project);
|
||||
if (libLocationAndTarget.first != null) {
|
||||
args.add("-libzip");
|
||||
args.add(libLocationAndTarget.first);
|
||||
// we cannot use OrderEnumerator because it has critical bug — try https://gist.github.com/2953261, processor will never be called for module dependency
|
||||
// we don't use context.getCompileScope().getAffectedModules() because we want to know about linkage type (well, we ignore scope right now, but in future...)
|
||||
private static void collectModuleDependencies(Module dependentModule, Set<Module> modules) {
|
||||
for (OrderEntry entry : ModuleRootManager.getInstance(dependentModule).getOrderEntries()) {
|
||||
if (entry instanceof ModuleOrderEntry) {
|
||||
ModuleOrderEntry moduleEntry = (ModuleOrderEntry) entry;
|
||||
if (!moduleEntry.getScope().isForProductionCompile()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Module module = moduleEntry.getModule();
|
||||
if (module == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (modules.add(module) && moduleEntry.isExported()) {
|
||||
collectModuleDependencies(module, modules);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static VirtualFile[] getSourceFiles(@NotNull Module module) {
|
||||
return CompilerManager.getInstance(module.getProject()).createModuleCompileScope(module, false)
|
||||
.getFiles(JetFileType.INSTANCE, true);
|
||||
}
|
||||
|
||||
private static void addLibLocationAndTarget(@NotNull Module module, @NotNull ArrayList<String> args) {
|
||||
Pair<String[], String> libLocationAndTarget = JsModuleDetector.getLibLocationAndTargetForProject(module);
|
||||
|
||||
StringBuilder sb = StringBuilderSpinAllocator.alloc();
|
||||
AccessToken token = ReadAction.start();
|
||||
try {
|
||||
THashSet<Module> modules = new THashSet<Module>();
|
||||
collectModuleDependencies(module, modules);
|
||||
if (!modules.isEmpty()) {
|
||||
for (Module dependency : modules) {
|
||||
sb.append('@').append(dependency.getName()).append(',');
|
||||
|
||||
for (VirtualFile file : getSourceFiles(dependency)) {
|
||||
sb.append(file.getPath()).append(',');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (libLocationAndTarget.first != null) {
|
||||
for (String file : libLocationAndTarget.first) {
|
||||
sb.append(file).append(',');
|
||||
}
|
||||
}
|
||||
|
||||
if (sb.length() > 0) {
|
||||
args.add("-libraryFiles");
|
||||
args.add(sb.substring(0, sb.length() - 1));
|
||||
}
|
||||
}
|
||||
finally {
|
||||
token.finish();
|
||||
StringBuilderSpinAllocator.dispose(sb);
|
||||
}
|
||||
|
||||
if (libLocationAndTarget.second != null) {
|
||||
args.add("-target");
|
||||
args.add(libLocationAndTarget.second);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addPathToSourcesDir(@NotNull ArrayList<String> args, @NotNull VirtualFile srcDir) {
|
||||
String srcPath = srcDir.getPath();
|
||||
args.add("-srcdir");
|
||||
args.add(srcPath);
|
||||
private static void addPathToSourcesDir(@NotNull VirtualFile[] sourceFiles, @NotNull ArrayList<String> args) {
|
||||
args.add("-sourceFiles");
|
||||
|
||||
StringBuilder sb = StringBuilderSpinAllocator.alloc();
|
||||
try {
|
||||
for (VirtualFile file : sourceFiles) {
|
||||
sb.append(file.getPath()).append(',');
|
||||
}
|
||||
args.add(sb.substring(0, sb.length() - 1));
|
||||
}
|
||||
finally {
|
||||
StringBuilderSpinAllocator.dispose(sb);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addOutputPath(@Nullable String outFile, @NotNull ArrayList<String> args) {
|
||||
|
||||
@@ -19,15 +19,15 @@ package org.jetbrains.jet.plugin.project;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
import org.jetbrains.k2js.config.ZippedLibrarySourcesConfig;
|
||||
import org.jetbrains.k2js.config.LibrarySourcesConfig;
|
||||
|
||||
import static org.jetbrains.jet.plugin.project.JsModuleDetector.getLibLocationAndTargetForProject;
|
||||
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public final class IDEAConfig extends ZippedLibrarySourcesConfig {
|
||||
public final class IDEAConfig extends LibrarySourcesConfig {
|
||||
public IDEAConfig(@NotNull Project project) {
|
||||
super(project, getLibLocationAndTargetForProject(project).first, EcmaVersion.defaultVersion());
|
||||
super(project, "default", getLibLocationAndTargetForProject(project).first, EcmaVersion.defaultVersion());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,10 +36,6 @@ public final class JsModuleDetector {
|
||||
private JsModuleDetector() {
|
||||
}
|
||||
|
||||
public static boolean isJsProject(@NotNull Project project) {
|
||||
return getJSModule(project) != null;
|
||||
}
|
||||
|
||||
public static boolean isJsModule(@NotNull Module module) {
|
||||
return K2JSModuleComponent.getInstance(module).isJavaScriptModule();
|
||||
}
|
||||
@@ -57,15 +53,22 @@ public final class JsModuleDetector {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Pair<String, String> getLibLocationAndTargetForProject(@NotNull Project project) {
|
||||
public static Pair<String[], String> getLibLocationAndTargetForProject(@NotNull Project project) {
|
||||
Module module = getJSModule(project);
|
||||
if (module == null) {
|
||||
return Pair.empty();
|
||||
}
|
||||
else {
|
||||
return getLibLocationAndTargetForProject(module);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Pair<String[], String> getLibLocationAndTargetForProject(@NotNull Module module) {
|
||||
K2JSModuleComponent jsModuleComponent = K2JSModuleComponent.getInstance(module);
|
||||
String pathToJavaScriptLibrary = jsModuleComponent.getPathToJavaScriptLibrary();
|
||||
String basePath = ModuleRootManager.getInstance(module).getContentRoots()[0].getPath();
|
||||
return Pair.create(basePath + pathToJavaScriptLibrary, jsModuleComponent.getEcmaVersion().toString());
|
||||
return Pair.create(new String[] {basePath + pathToJavaScriptLibrary}, jsModuleComponent.getEcmaVersion().toString());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
Reference in New Issue
Block a user