JS backend: added the ability to turn off the inline optimization.
This commit is contained in:
+3
@@ -38,6 +38,9 @@ public abstract class CommonCompilerArguments {
|
||||
@Argument(value = "X", description = "Print a synopsis of advanced options")
|
||||
public boolean extraHelp;
|
||||
|
||||
@Argument(value = "Xno-inline", description = "Disable method inlining")
|
||||
public boolean noInline;
|
||||
|
||||
public List<String> freeArgs = new SmartList<String>();
|
||||
|
||||
@NotNull
|
||||
|
||||
-3
@@ -63,9 +63,6 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments {
|
||||
@Argument(value = "Xno-param-assertions", description = "Don't generate not-null assertions on parameters of methods accessible from Java")
|
||||
public boolean noParamAssertions;
|
||||
|
||||
@Argument(value = "Xno-inline", description = "Disable method inlining")
|
||||
public boolean noInline;
|
||||
|
||||
@Argument(value = "Xno-optimize", description = "Disable optimizations")
|
||||
public boolean noOptimize;
|
||||
|
||||
|
||||
@@ -198,12 +198,14 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
|
||||
}
|
||||
EcmaVersion ecmaVersion = EcmaVersion.defaultVersion();
|
||||
String moduleId = FileUtil.getNameWithoutExtension(new File(arguments.outputFile));
|
||||
boolean inlineEnabled = !arguments.noInline;
|
||||
|
||||
if (arguments.libraryFiles != null) {
|
||||
return new LibrarySourcesConfig(project, moduleId, Arrays.asList(arguments.libraryFiles), ecmaVersion, arguments.sourceMap);
|
||||
return new LibrarySourcesConfig(project, moduleId, Arrays.asList(arguments.libraryFiles), ecmaVersion, arguments.sourceMap, inlineEnabled);
|
||||
}
|
||||
else {
|
||||
// lets discover the JS library definitions on the classpath
|
||||
return new ClassPathLibraryDefintionsConfig(project, moduleId, ecmaVersion, arguments.sourceMap);
|
||||
return new ClassPathLibraryDefintionsConfig(project, moduleId, ecmaVersion, arguments.sourceMap, inlineEnabled);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,10 +36,11 @@ 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) {
|
||||
return new Config(project, "main", ecmaVersion, false, true) {
|
||||
@NotNull
|
||||
@Override
|
||||
protected List<JetFile> generateLibFiles() {
|
||||
@@ -161,21 +162,28 @@ public abstract class Config {
|
||||
|
||||
private final boolean sourcemap;
|
||||
|
||||
public Config(@NotNull Project project, @NotNull String moduleId, @NotNull EcmaVersion ecmaVersion) {
|
||||
this(project, moduleId, ecmaVersion, false);
|
||||
}
|
||||
|
||||
public Config(@NotNull Project project, @NotNull String moduleId, @NotNull EcmaVersion ecmaVersion, boolean sourcemap) {
|
||||
public Config(
|
||||
@NotNull Project project,
|
||||
@NotNull String moduleId,
|
||||
@NotNull EcmaVersion ecmaVersion,
|
||||
boolean sourcemap,
|
||||
boolean inlineEnabled
|
||||
) {
|
||||
this.project = project;
|
||||
this.target = ecmaVersion;
|
||||
this.moduleId = moduleId;
|
||||
this.sourcemap = sourcemap;
|
||||
this.inlineEnabled = inlineEnabled;
|
||||
}
|
||||
|
||||
public boolean isSourcemap() {
|
||||
return sourcemap;
|
||||
}
|
||||
|
||||
public boolean isInlineEnabled() {
|
||||
return inlineEnabled;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Project getProject() {
|
||||
return project;
|
||||
|
||||
@@ -56,9 +56,10 @@ public class LibrarySourcesConfig extends Config {
|
||||
@NotNull String moduleId,
|
||||
@NotNull List<String> files,
|
||||
@NotNull EcmaVersion ecmaVersion,
|
||||
boolean sourcemap
|
||||
boolean sourcemap,
|
||||
boolean inlineEnabled
|
||||
) {
|
||||
super(project, moduleId, ecmaVersion, sourcemap);
|
||||
super(project, moduleId, ecmaVersion, sourcemap, inlineEnabled);
|
||||
this.files = files;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ public class TestConfig extends Config {
|
||||
@NotNull List<JetFile> files,
|
||||
@NotNull BindingContext libraryContext,
|
||||
@NotNull ModuleDescriptor module) {
|
||||
return new TestConfig(project, version, files, libraryContext, module, false);
|
||||
return new TestConfig(project, version, files, libraryContext, module, false, true);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -47,7 +47,7 @@ public class TestConfig extends Config {
|
||||
@NotNull List<JetFile> files,
|
||||
@NotNull BindingContext libraryContext,
|
||||
@NotNull ModuleDescriptor module) {
|
||||
return new TestConfig(project, version, files, libraryContext, module, true);
|
||||
return new TestConfig(project, version, files, libraryContext, module, true, true);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -58,9 +58,16 @@ public class TestConfig extends Config {
|
||||
@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) {
|
||||
super(project, REWRITABLE_MODULE_NAME, version, sourcemap);
|
||||
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;
|
||||
|
||||
@@ -45,7 +45,7 @@ public class TestConfigWithUnitTests extends TestConfig {
|
||||
@NotNull List<JetFile> files,
|
||||
@NotNull BindingContext libraryContext,
|
||||
@NotNull ModuleDescriptor module) {
|
||||
super(project, version, files, libraryContext, module, false);
|
||||
super(project, version, files, libraryContext, module, false, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+8
-2
@@ -30,8 +30,14 @@ public class ClassPathLibraryDefintionsConfig extends Config {
|
||||
@NotNull
|
||||
public static final String META_INF_SERVICES_FILE = "META-INF/services/org.jetbrains.kotlin.js.libraryDefinitions";
|
||||
|
||||
public ClassPathLibraryDefintionsConfig(@NotNull Project project, @NotNull String moduleId, @NotNull EcmaVersion version, boolean sourcemap) {
|
||||
super(project, moduleId, version, sourcemap);
|
||||
public ClassPathLibraryDefintionsConfig(
|
||||
@NotNull Project project,
|
||||
@NotNull String moduleId,
|
||||
@NotNull EcmaVersion version,
|
||||
boolean sourcemap,
|
||||
boolean inlineEnabled
|
||||
) {
|
||||
super(project, moduleId, version, sourcemap, inlineEnabled);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.jetbrains.jet.lang.reflect.ReflectionTypes;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.k2js.config.Config;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
import org.jetbrains.k2js.config.LibrarySourcesConfig;
|
||||
import org.jetbrains.k2js.translate.context.generator.Generator;
|
||||
@@ -48,12 +49,12 @@ import static org.jetbrains.k2js.translate.utils.TranslationUtils.getSuggestedNa
|
||||
*/
|
||||
public final class StaticContext {
|
||||
|
||||
public static StaticContext generateStaticContext(@NotNull BindingContext bindingContext, @NotNull EcmaVersion ecmaVersion, @NotNull ModuleDescriptor moduleDescriptor) {
|
||||
public static StaticContext generateStaticContext(@NotNull BindingContext bindingContext, @NotNull Config config, @NotNull ModuleDescriptor moduleDescriptor) {
|
||||
JsProgram program = new JsProgram("main");
|
||||
Namer namer = Namer.newInstance(program.getRootScope());
|
||||
Intrinsics intrinsics = new Intrinsics();
|
||||
StandardClasses standardClasses = StandardClasses.bindImplementations(namer.getKotlinScope());
|
||||
return new StaticContext(program, bindingContext, namer, intrinsics, standardClasses, program.getRootScope(), ecmaVersion, moduleDescriptor);
|
||||
return new StaticContext(program, bindingContext, namer, intrinsics, standardClasses, program.getRootScope(), config, moduleDescriptor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -90,20 +91,24 @@ public final class StaticContext {
|
||||
@NotNull
|
||||
private final Map<JsScope, JsFunction> scopeToFunction = Maps.newHashMap();
|
||||
|
||||
@NotNull
|
||||
private final Config config;
|
||||
|
||||
@NotNull
|
||||
private final EcmaVersion ecmaVersion;
|
||||
|
||||
//TODO: too many parameters in constructor
|
||||
private StaticContext(@NotNull JsProgram program, @NotNull BindingContext bindingContext,
|
||||
@NotNull Namer namer, @NotNull Intrinsics intrinsics,
|
||||
@NotNull StandardClasses standardClasses, @NotNull JsScope rootScope, @NotNull EcmaVersion ecmaVersion, @NotNull ModuleDescriptor moduleDescriptor) {
|
||||
@NotNull StandardClasses standardClasses, @NotNull JsScope rootScope, @NotNull Config config, @NotNull ModuleDescriptor moduleDescriptor) {
|
||||
this.program = program;
|
||||
this.bindingContext = bindingContext;
|
||||
this.namer = namer;
|
||||
this.intrinsics = intrinsics;
|
||||
this.rootScope = rootScope;
|
||||
this.standardClasses = standardClasses;
|
||||
this.ecmaVersion = ecmaVersion;
|
||||
this.config = config;
|
||||
this.ecmaVersion = config.getTarget();
|
||||
this.reflectionTypes = new ReflectionTypes(moduleDescriptor);
|
||||
}
|
||||
|
||||
@@ -214,6 +219,10 @@ public final class StaticContext {
|
||||
return result;
|
||||
}
|
||||
|
||||
public Config getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
private final class NameGenerator extends Generator<JsName> {
|
||||
|
||||
public NameGenerator() {
|
||||
|
||||
@@ -20,11 +20,15 @@ import com.google.dart.compiler.backend.js.ast.*;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.MemberDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||
import org.jetbrains.jet.lang.reflect.ReflectionTypes;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.k2js.config.Config;
|
||||
import org.jetbrains.k2js.translate.intrinsic.Intrinsics;
|
||||
import org.jetbrains.k2js.translate.utils.TranslationUtils;
|
||||
|
||||
@@ -246,6 +250,11 @@ public class TranslationContext {
|
||||
return staticContext.getProgram();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Config getConfig() {
|
||||
return staticContext.getConfig();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsScope scope() {
|
||||
return dynamicContext.getScope();
|
||||
|
||||
@@ -169,7 +169,7 @@ public final class Translation {
|
||||
throws TranslationException {
|
||||
try {
|
||||
JsProgram program = doGenerateAst(bindingContext, files, mainCallParameters, moduleDescriptor, config);
|
||||
return JsInliner.process(program);
|
||||
return config.isInlineEnabled() ? JsInliner.process(program) : program;
|
||||
}
|
||||
catch (UnsupportedOperationException e) {
|
||||
throw new UnsupportedFeatureException("Unsupported feature used.", e);
|
||||
@@ -184,7 +184,7 @@ public final class Translation {
|
||||
@NotNull MainCallParameters mainCallParameters,
|
||||
@NotNull ModuleDescriptor moduleDescriptor,
|
||||
@NotNull Config config) throws MainFunctionNotFoundException {
|
||||
StaticContext staticContext = StaticContext.generateStaticContext(bindingContext, config.getTarget(), moduleDescriptor);
|
||||
StaticContext staticContext = StaticContext.generateStaticContext(bindingContext, config, moduleDescriptor);
|
||||
JsProgram program = staticContext.getProgram();
|
||||
JsBlock block = program.getGlobalBlock();
|
||||
|
||||
|
||||
+2
@@ -53,6 +53,8 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
|
||||
}
|
||||
|
||||
public static boolean shouldBeInlined(@NotNull JetCallExpression expression, @NotNull TranslationContext context) {
|
||||
if (!context.getConfig().isInlineEnabled()) return false;
|
||||
|
||||
ResolvedCall<?> resolvedCall = CallUtilPackage.getResolvedCall(expression, context.bindingContext());
|
||||
assert resolvedCall != null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user