JS backend: refactor Configs.

This commit is contained in:
Zalim Bashorov
2014-10-03 23:46:43 +04:00
parent dd1fffae40
commit c8278fd887
5 changed files with 125 additions and 297 deletions
@@ -24,131 +24,14 @@ import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Base class representing a configuration of translator.
*/
public abstract class Config {
//NOTE: a hacky solution to be able to rerun code samples with lib loaded only once: used by tests and web demo
@NotNull
public static final String REWRITABLE_MODULE_NAME = "JS_TESTS";
private boolean inlineEnabled;
@NotNull
public static Config getEmptyConfig(@NotNull Project project, @NotNull EcmaVersion ecmaVersion) {
return new Config(project, "main", ecmaVersion, false, true) {
@NotNull
@Override
protected List<JetFile> generateLibFiles() {
return Collections.emptyList();
}
};
}
//NOTE: used by mvn build
@SuppressWarnings("UnusedDeclaration")
@NotNull
public static Config getEmptyConfig(@NotNull Project project) {
return getEmptyConfig(project, EcmaVersion.defaultVersion());
}
@NotNull
public static final List<String> LIB_FILES_WITH_DECLARATIONS = Arrays.asList(
"/core/annotations.kt",
"/core/core.kt",
"/core/date.kt",
"/core/dom.kt",
"/core/javaio.kt",
"/core/javalang.kt",
"/core/javautil.kt",
"/core/javautilCollections.kt",
"/core/json.kt",
"/core/kotlin.kt",
"/core/math.kt",
"/core/string.kt",
"/core/htmlDom.kt",
"/html5/canvas.kt",
"/jquery/common.kt",
"/jquery/ui.kt",
"/junit/core.kt",
"/qunit/core.kt",
"/stdlib/browser.kt"
);
@NotNull
public static final List<String> LIB_FILES_WITH_CODE = Arrays.asList(
"/stdlib/TuplesCode.kt",
"/core/javautilCollectionsCode.kt"
);
@NotNull
public static final List<String> LIB_FILE_NAMES = Lists.newArrayList();
static {
LIB_FILE_NAMES.addAll(LIB_FILES_WITH_DECLARATIONS);
LIB_FILE_NAMES.addAll(LIB_FILES_WITH_CODE);
}
/**
* the library files which depend on the STDLIB files to be able to compile
*/
@NotNull
public static final List<String> LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList(
"/core/stringsCode.kt",
"/stdlib/domCode.kt",
"/core/javautilCode.kt",
"/stdlib/jutilCode.kt",
"/stdlib/testCode.kt"
);
public static final String LIBRARIES_LOCATION = "js/js.libraries/src";
public static final String REFLECTION_LIB_LOCATION = "core/reflection/src/kotlin/reflect";
/**
* The file names in the standard library to compile
*/
@NotNull
public static final List<String> STDLIB_FILE_NAMES = Arrays.asList(
"/kotlin/Preconditions.kt",
"/kotlin/Functions.kt",
"/kotlin/collections/JUtil.kt",
"/kotlin/collections/Iterators.kt",
"/kotlin/collections/Arrays.kt",
"/kotlin/collections/Maps.kt",
"/kotlin/collections/Exceptions.kt",
"/kotlin/collections/MutableCollections.kt",
"/kotlin/collections/Stream.kt",
"/kotlin/collections/AbstractIterator.kt",
"/generated/_Aggregates.kt",
"/generated/_Arrays.kt",
"/generated/_DownTo.kt",
"/generated/_Elements.kt",
"/generated/_Filtering.kt",
"/generated/_Generators.kt",
"/generated/_Guards.kt",
"/generated/_Mapping.kt",
"/generated/_Numeric.kt",
"/generated/_Ordering.kt",
"/generated/_Snapshots.kt",
"/generated/_Strings.kt",
"/generated/_Streams.kt",
"/kotlin/Standard.kt",
"/kotlin/Ranges.kt",
"/kotlin/Numbers.kt",
"/kotlin/text/Strings.kt",
"/kotlin/text/StringBuilder.kt",
"/kotlin/dom/Dom.kt",
"/kotlin/test/Test.kt"
);
/**
* The location of the stdlib sources
*/
public static final String STDLIB_LOCATION = "libraries/stdlib/src";
private final boolean inlineEnabled;
@NotNull
private final Project project;
@@ -0,0 +1,124 @@
/*
* Copyright 2010-2013 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.test.config;
import com.google.common.base.Predicates;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.utils.PathUtil;
import org.jetbrains.k2js.analyze.TopDownAnalyzerFacadeForJS;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.config.LibrarySourcesConfig;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class LibrarySourcesConfigWithCaching extends LibrarySourcesConfig {
public static final List<String> JS_STDLIB =
Arrays.asList("@" + "kotlin_lib_compiled", PathUtil.getKotlinPathsForDistDirectory().getJsLibJarPath().getAbsolutePath());
private static List<JetFile> jsLibFiles;
private static AnalyzeExhaust exhaust;
private BindingContext libraryContext;
private ModuleDescriptor libraryModule;
private final boolean isUnitTestConfig;
public LibrarySourcesConfigWithCaching(
@NotNull Project project,
@NotNull String moduleId,
@NotNull EcmaVersion ecmaVersion,
boolean sourcemap,
boolean inlineEnabled,
boolean isUnitTestConfig
) {
super(project, moduleId, JS_STDLIB, ecmaVersion, sourcemap, inlineEnabled);
this.isUnitTestConfig = isUnitTestConfig;
}
@NotNull
@Override
public List<JetFile> generateLibFiles() {
if (jsLibFiles == null) {
//noinspection AssignmentToStaticFieldFromInstanceMethod
jsLibFiles = super.generateLibFiles();
}
return jsLibFiles;
}
@Nullable
@Override
public ModuleDescriptor getLibraryModule() {
if (libraryModule == null) {
libraryModule = getExhaust().getModuleDescriptor();
}
return libraryModule;
}
@Nullable
@Override
public BindingContext getLibraryContext() {
if (libraryContext == null) {
//TODO check errors?
// TopDownAnalyzerFacadeForJS.checkForErrors(allLibFiles, exhaust.getBindingContext());
libraryContext = getExhaust().getBindingContext();
}
return libraryContext;
}
@Override
public boolean isTestConfig() {
return isUnitTestConfig;
}
private AnalyzeExhaust getExhaust() {
if (exhaust == null) {
//noinspection AssignmentToStaticFieldFromInstanceMethod
exhaust = TopDownAnalyzerFacadeForJS.analyzeFiles(
generateLibFiles(),
Predicates.<PsiFile>alwaysFalse(),
createConfigWithoutLibFiles(getProject(), getModuleId(), getTarget())
);
}
return exhaust;
}
@NotNull
private static Config createConfigWithoutLibFiles(@NotNull Project project, @NotNull String moduleId, @NotNull EcmaVersion ecmaVersion) {
return new Config(project, moduleId, ecmaVersion, /* generate sourcemaps = */ false, /* inlineEnabled = */ false) {
@NotNull
@Override
protected List<JetFile> generateLibFiles() {
return Collections.emptyList();
}
};
}
}
@@ -1,93 +0,0 @@
/*
* Copyright 2010-2013 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.test.config;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import java.util.List;
public class TestConfig extends Config {
@NotNull
public static TestConfigFactory FACTORY_WITHOUT_SOURCEMAP = new TestConfigFactory() {
@Override
public TestConfig create(@NotNull Project project,
@NotNull EcmaVersion version,
@NotNull List<JetFile> files,
@NotNull BindingContext libraryContext,
@NotNull ModuleDescriptor module) {
return new TestConfig(project, version, files, libraryContext, module, false, true);
}
};
public static TestConfigFactory FACTORY_WITH_SOURCEMAP = new TestConfigFactory() {
@Override
public TestConfig create(@NotNull Project project,
@NotNull EcmaVersion version,
@NotNull List<JetFile> files,
@NotNull BindingContext libraryContext,
@NotNull ModuleDescriptor module) {
return new TestConfig(project, version, files, libraryContext, module, true, true);
}
};
@NotNull
private final List<JetFile> jsLibFiles;
@NotNull
private final BindingContext libraryContext;
@NotNull
private final ModuleDescriptor libraryModule;
public TestConfig(
@NotNull Project project,
@NotNull EcmaVersion version,
@NotNull List<JetFile> files,
@NotNull BindingContext libraryContext,
@NotNull ModuleDescriptor module,
boolean sourcemap,
boolean inlineEnabled
) {
super(project, REWRITABLE_MODULE_NAME, version, sourcemap, inlineEnabled);
jsLibFiles = files;
this.libraryContext = libraryContext;
libraryModule = module;
}
@NotNull
@Override
public BindingContext getLibraryContext() {
return libraryContext;
}
@NotNull
@Override
public ModuleDescriptor getLibraryModule() {
return libraryModule;
}
@Override
@NotNull
public List<JetFile> generateLibFiles() {
return jsLibFiles;
}
}
@@ -1,31 +0,0 @@
/*
* Copyright 2010-2013 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.test.config;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.k2js.config.EcmaVersion;
import java.util.List;
public interface TestConfigFactory {
TestConfig create(@NotNull Project project, @NotNull EcmaVersion version,
@NotNull List<JetFile> files, @NotNull BindingContext libraryContext, @NotNull ModuleDescriptor module);
}
@@ -1,55 +0,0 @@
/*
* Copyright 2010-2013 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.test.config;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.k2js.config.EcmaVersion;
import java.util.List;
public class TestConfigWithUnitTests extends TestConfig {
@NotNull
public static TestConfigFactory FACTORY = new TestConfigFactory() {
@Override
public TestConfig create(@NotNull Project project,
@NotNull EcmaVersion version,
@NotNull List<JetFile> files,
@NotNull BindingContext libraryContext,
@NotNull ModuleDescriptor module) {
return new TestConfigWithUnitTests(project, version, files, libraryContext, module);
}
};
public TestConfigWithUnitTests(@NotNull Project project,
@NotNull EcmaVersion version,
@NotNull List<JetFile> files,
@NotNull BindingContext libraryContext,
@NotNull ModuleDescriptor module) {
super(project, version, files, libraryContext, module, false, true);
}
@Override
public boolean isTestConfig() {
return true;
}
}