js compiler: module dependency

This commit is contained in:
develar
2012-06-19 19:23:05 +04:00
parent cd067cb32c
commit 06ecb45004
13 changed files with 165 additions and 73 deletions
@@ -151,10 +151,10 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
@NotNull
private static Config getConfig(@NotNull K2JSCompilerArguments arguments, @NotNull Project project) {
EcmaVersion ecmaVersion = EcmaVersion.fromString(arguments.target);
if (arguments.libzip == null) {
if (arguments.libraryFiles == null) {
// lets discover the JS library definitions on the classpath
return new ClassPathLibraryDefintionsConfig(project, ecmaVersion);
}
return new ZippedLibrarySourcesConfig(project, arguments.libzip, ecmaVersion);
return new LibrarySourcesConfig(project, arguments.libraryFiles, ecmaVersion);
}
}
@@ -36,8 +36,8 @@ public class K2JSCompilerArguments extends CompilerArguments {
public String outputFile;
//NOTE: may well be a subject to change soon
@Argument(value = "libzip", description = "Path to zipped lib sources")
public String libzip;
@Argument(value = "libraryFiles", description = "Path to zipped lib sources or kotlin files")
public String[] libraryFiles;
@Argument(value = "srcdir", description = "Sources directory")
public String srcdir;
@@ -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;
@@ -122,9 +124,9 @@ public final class K2JSCompiler implements TranslatingCompiler {
}
VirtualFile outDir = context.getModuleOutputDirectory(module);
String outFile = outDir == null ? null : K2JSRunnerUtils.constructPathToGeneratedFile(context.getProject(), outDir.getPath());
String outFile = outDir == null ? null : outDir.getPath() + "/" + module.getName() + ".js";
String[] commandLineArgs = constructArguments(context.getProject(), outFile, roots[0]);
String[] commandLineArgs = constructArguments(module, outFile, roots[0]);
Object rc = invokeExecMethod(environment, out, context, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler");
if (outDir != null && !ApplicationManager.getApplication().isUnitTestMode()) {
@@ -134,20 +136,67 @@ public final class K2JSCompiler implements TranslatingCompiler {
}
@NotNull
private static String[] constructArguments(@NotNull Project project, @Nullable String outFile, @NotNull VirtualFile srcDir) {
ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version");
private static String[] constructArguments(@NotNull Module module, @Nullable String outFile, @NotNull VirtualFile srcDir) {
ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version", "-mainCall", "mainWithArgs");
addPathToSourcesDir(args, srcDir);
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
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 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()) {
VirtualFile[] files = CompilerManager.getInstance(module.getProject()).createModulesCompileScope(
modules.toArray(new Module[modules.size()]), false).getFiles(JetFileType.INSTANCE, true);
for (VirtualFile file : files) {
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);
@@ -19,14 +19,14 @@ 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());
}
@@ -57,15 +57,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
@@ -18,6 +18,7 @@ package org.jetbrains.k2js.config;
import com.google.common.collect.Lists;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -27,6 +28,7 @@ import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
@@ -36,33 +38,53 @@ import java.util.zip.ZipFile;
/**
* @author Pavel Talanov
*/
public class ZippedLibrarySourcesConfig extends Config {
@Nullable
protected final String pathToLibZip;
public class LibrarySourcesConfig extends Config {
public static final Key<Boolean> EXTERNAL_LIB = new Key<Boolean>("externalLib");
public ZippedLibrarySourcesConfig(@NotNull Project project, @Nullable String pathToZip, @NotNull EcmaVersion ecmaVersion) {
@Nullable
private final String[] files;
public LibrarySourcesConfig(@NotNull Project project, @Nullable String[] files, @NotNull EcmaVersion ecmaVersion) {
super(project, ecmaVersion);
pathToLibZip = pathToZip;
this.files = files;
}
@NotNull
@Override
public List<JetFile> generateLibFiles() {
if (pathToLibZip == null) {
if (files == null) {
return Collections.emptyList();
}
try {
File file = new File(pathToLibZip);
ZipFile zipFile = new ZipFile(file);
List<JetFile> jetFiles = new ArrayList<JetFile>();
for (String path : files) {
File file = new File(path);
try {
return traverseArchive(zipFile);
String name = file.getName();
if (name.endsWith(".jar") || name.endsWith(".zip")) {
jetFiles.addAll(readZip(file));
}
else {
JetFile psiFile = JetFileUtils.createPsiFile(path, FileUtil.loadFile(file), getProject());
psiFile.putUserData(EXTERNAL_LIB, true);
jetFiles.add(psiFile);
}
}
finally {
zipFile.close();
catch (IOException e) {
throw new RuntimeException(e);
}
}
catch (IOException e) {
return Collections.emptyList();
return jetFiles;
}
private List<JetFile> readZip(File file) throws IOException {
ZipFile zipFile = new ZipFile(file);
try {
return traverseArchive(zipFile);
}
finally {
zipFile.close();
}
}
@@ -76,6 +98,7 @@ public class ZippedLibrarySourcesConfig extends Config {
InputStream stream = file.getInputStream(entry);
String text = FileUtil.loadTextAndClose(stream);
JetFile jetFile = JetFileUtils.createPsiFile(entry.getName(), text, getProject());
jetFile.putUserData(EXTERNAL_LIB, true);
result.add(jetFile);
}
}
@@ -96,13 +96,11 @@ public final class K2JSTranslator {
throws TranslationException {
JetStandardLibrary.initialize(config.getProject());
BindingContext bindingContext = AnalyzerFacadeForJS.analyzeFilesAndCheckErrors(filesToTranslate, config);
Collection<JetFile> files = AnalyzerFacadeForJS.withJsLibAdded(filesToTranslate, config);
return Translation.generateAst(bindingContext, Lists.newArrayList(files), mainCallParameters, config.getTarget(), rawStatements);
return Translation.generateAst(bindingContext, filesToTranslate, mainCallParameters, config.getTarget(), rawStatements);
}
@NotNull
private Project getProject() {
return config.getProject();
}
}
}
@@ -144,7 +144,7 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
@NotNull
public List<JsPropertyInitializer> classDeclarationsForNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
List<JsPropertyInitializer> result = Lists.newArrayList();
for (ClassDescriptor classDescriptor : getAllClassesDefinedInNamespace(namespaceDescriptor)) {
for (ClassDescriptor classDescriptor : getAllClassesDefinedInNamespace(namespaceDescriptor, bindingContext())) {
result.add(getClassNameToClassObject(classDescriptor));
}
return result;
@@ -31,6 +31,7 @@ import org.jetbrains.k2js.translate.utils.JsAstUtils;
import org.jetbrains.k2js.translate.utils.JsDescriptorUtils;
import org.jetbrains.k2js.translate.utils.TranslationUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@@ -64,33 +65,28 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
private List<ClassDescriptor> getAllClasses() {
List<ClassDescriptor> result = Lists.newArrayList();
for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
result.addAll(getAllClassesDefinedInNamespace(namespaceDescriptor));
result.addAll(getAllClassesDefinedInNamespace(namespaceDescriptor, context().bindingContext()));
}
return result;
}
@NotNull
private List<JsStatement> translate() {
List<JsStatement> result = classesDeclarations();
result.addAll(namespacesDeclarations());
List<JsStatement> result = new ArrayList<JsStatement>();
classesDeclarations(result);
namespacesDeclarations(result);
return result;
}
@NotNull
private List<JsStatement> classesDeclarations() {
List<JsStatement> result = Lists.newArrayList();
private void classesDeclarations(List<JsStatement> statements) {
classDeclarationTranslator.generateDeclarations();
result.add(classDeclarationTranslator.getDeclarationsStatement());
return result;
statements.add(classDeclarationTranslator.getDeclarationsStatement());
}
@NotNull
private List<JsStatement> namespacesDeclarations() {
List<JsStatement> result = Lists.newArrayList();
private void namespacesDeclarations(List<JsStatement> statements) {
List<NamespaceTranslator> namespaceTranslators = getTranslatorsForNonEmptyNamespaces();
result.addAll(declarationStatements(namespaceTranslators, context()));
result.addAll(initializeStatements(namespaceTranslators));
return result;
statements.addAll(declarationStatements(namespaceTranslators, context()));
statements.addAll(initializeStatements());
}
@NotNull
@@ -121,7 +117,7 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
}
@NotNull
private List<JsStatement> initializeStatements(@NotNull List<NamespaceTranslator> namespaceTranslators) {
private List<JsStatement> initializeStatements() {
List<JsStatement> result = Lists.newArrayList();
for (NamespaceDescriptor descriptor : filterNonEmptyNamespaces(namespaceDescriptors)) {
JsNameRef initializeMethodReference = Namer.initializeMethodReference();
@@ -144,10 +140,10 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
}
@NotNull
private static List<NamespaceDescriptor> filterNonEmptyNamespaces(@NotNull List<NamespaceDescriptor> namespaceDescriptors) {
private List<NamespaceDescriptor> filterNonEmptyNamespaces(@NotNull List<NamespaceDescriptor> namespaceDescriptors) {
List<NamespaceDescriptor> result = Lists.newArrayList();
for (NamespaceDescriptor descriptor : namespaceDescriptors) {
if (!JsDescriptorUtils.isNamespaceEmpty(descriptor)) {
if (!JsDescriptorUtils.isNamespaceEmpty(descriptor, context().bindingContext())) {
result.add(descriptor);
}
}
@@ -128,7 +128,7 @@ public final class NamespaceTranslator extends AbstractTranslator {
return Collections.emptyList();
}
List<JsPropertyInitializer> result = Lists.newArrayList();
List<NamespaceDescriptor> nestedNamespaces = JsDescriptorUtils.getNestedNamespaces(descriptor);
List<NamespaceDescriptor> nestedNamespaces = JsDescriptorUtils.getNestedNamespaces(descriptor, context().bindingContext());
for (NamespaceDescriptor nestedNamespace : nestedNamespaces) {
NamespaceTranslator nestedNamespaceTranslator = new NamespaceTranslator(nestedNamespace, classDeclarationTranslator, context());
result.add(nestedNamespaceTranslator.getDeclarationAsInitializer());
@@ -110,7 +110,7 @@ public final class BindingUtils {
public static List<JetDeclaration> getDeclarationsForNamespace(@NotNull BindingContext bindingContext,
@NotNull NamespaceDescriptor namespace) {
List<JetDeclaration> declarations = new ArrayList<JetDeclaration>();
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespace)) {
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespace, bindingContext)) {
if (descriptor instanceof NamespaceDescriptor) {
continue;
}
@@ -17,14 +17,19 @@
package org.jetbrains.k2js.translate.utils;
import com.google.common.collect.Lists;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
import org.jetbrains.k2js.config.LibrarySourcesConfig;
import org.jetbrains.k2js.translate.context.Namer;
import java.util.ArrayList;
@@ -193,22 +198,24 @@ public final class JsDescriptorUtils {
}
@NotNull
public static List<ClassDescriptor> getAllClassesDefinedInNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
public static List<ClassDescriptor> getAllClassesDefinedInNamespace(@NotNull NamespaceDescriptor namespaceDescriptor,
@NotNull BindingContext context) {
List<ClassDescriptor> classDescriptors = Lists.newArrayList();
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor)) {
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor, context)) {
if (descriptor instanceof ClassDescriptor) {
classDescriptors.add((ClassDescriptor)descriptor);
classDescriptors.add((ClassDescriptor) descriptor);
}
}
return classDescriptors;
}
@NotNull
public static List<NamespaceDescriptor> getNestedNamespaces(@NotNull NamespaceDescriptor namespaceDescriptor) {
public static List<NamespaceDescriptor> getNestedNamespaces(@NotNull NamespaceDescriptor namespaceDescriptor,
@NotNull BindingContext context) {
List<NamespaceDescriptor> result = Lists.newArrayList();
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor)) {
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor, context)) {
if (descriptor instanceof NamespaceDescriptor) {
result.add((NamespaceDescriptor)descriptor);
result.add((NamespaceDescriptor) descriptor);
}
}
return result;
@@ -227,22 +234,29 @@ public final class JsDescriptorUtils {
}
@NotNull
public static List<DeclarationDescriptor> getContainedDescriptorsWhichAreNotPredefined(@NotNull NamespaceDescriptor namespace) {
public static List<DeclarationDescriptor> getContainedDescriptorsWhichAreNotPredefined(@NotNull NamespaceDescriptor namespace,
@NotNull BindingContext context) {
List<DeclarationDescriptor> result = Lists.newArrayList();
for (DeclarationDescriptor descriptor : namespace.getMemberScope().getAllDescriptors()) {
if (!AnnotationsUtils.isPredefinedObject(descriptor)) {
result.add(descriptor);
PsiElement psiElement = BindingContextUtils.descriptorToDeclaration(context, descriptor);
if (psiElement != null) {
PsiFile file = psiElement.getContainingFile();
if (file.getUserData(LibrarySourcesConfig.EXTERNAL_LIB) == null) {
result.add(descriptor);
}
}
}
}
return result;
}
//TODO: at the moment this check is very ineffective
public static boolean isNamespaceEmpty(@NotNull NamespaceDescriptor namespace) {
List<DeclarationDescriptor> containedDescriptors = getContainedDescriptorsWhichAreNotPredefined(namespace);
public static boolean isNamespaceEmpty(@NotNull NamespaceDescriptor namespace, @NotNull BindingContext context) {
List<DeclarationDescriptor> containedDescriptors = getContainedDescriptorsWhichAreNotPredefined(namespace, context);
for (DeclarationDescriptor descriptor : containedDescriptors) {
if (descriptor instanceof NamespaceDescriptor) {
if (!isNamespaceEmpty((NamespaceDescriptor)descriptor)) {
if (!isNamespaceEmpty((NamespaceDescriptor) descriptor, context)) {
return false;
}
}
+5
View File
@@ -14,6 +14,11 @@
* limitations under the License.
*/
// todo org.jetbrains.k2js.test.semantics.WebDemoExamples2Test#testBuilder
var kotlin = {set:function (receiver, key, value) {
return receiver.put(key, value);
}};
(function () {
"use strict";