JS: prepare all necessary abstractions for new pipeline
This commit is contained in:
@@ -731,11 +731,6 @@ public class JsToStringGenerationVisitor extends JsVisitor {
|
|||||||
p.print("<JsProgram>");
|
p.print("<JsProgram>");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitProgramFragment(@NotNull JsProgramFragment x) {
|
|
||||||
p.print("<JsProgramFragment>");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitRegExp(@NotNull JsRegExp x) {
|
public void visitRegExp(@NotNull JsRegExp x) {
|
||||||
slash();
|
slash();
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2017 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.kotlin.js.backend.ast
|
||||||
|
|
||||||
|
class JsClassModel(val name: JsName, val superName: JsName?)
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2017 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.kotlin.js.backend.ast
|
||||||
|
|
||||||
|
class JsImportedModule(val externalName: String, val internalName: JsName, val plainReference: JsExpression?) {
|
||||||
|
val key = JsImportedModuleKey(externalName, plainReference?.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
data class JsImportedModuleKey(val baseName: String, val plainName: String?)
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2017 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.kotlin.js.backend.ast
|
||||||
|
|
||||||
|
class JsNameBinding(val key: String, val name: JsName)
|
||||||
@@ -17,8 +17,7 @@ import java.util.Map;
|
|||||||
* A JavaScript program.
|
* A JavaScript program.
|
||||||
*/
|
*/
|
||||||
public final class JsProgram extends SourceInfoAwareJsNode {
|
public final class JsProgram extends SourceInfoAwareJsNode {
|
||||||
|
private final JsGlobalBlock globalBlock = new JsGlobalBlock();
|
||||||
private JsProgramFragment[] fragments;
|
|
||||||
|
|
||||||
private final TDoubleObjectHashMap<JsDoubleLiteral> doubleLiteralMap = new TDoubleObjectHashMap<JsDoubleLiteral>();
|
private final TDoubleObjectHashMap<JsDoubleLiteral> doubleLiteralMap = new TDoubleObjectHashMap<JsDoubleLiteral>();
|
||||||
private final TIntObjectHashMap<JsIntLiteral> intLiteralMap = new TIntObjectHashMap<JsIntLiteral>();
|
private final TIntObjectHashMap<JsIntLiteral> intLiteralMap = new TIntObjectHashMap<JsIntLiteral>();
|
||||||
@@ -30,18 +29,10 @@ public final class JsProgram extends SourceInfoAwareJsNode {
|
|||||||
public JsProgram() {
|
public JsProgram() {
|
||||||
rootScope = new JsRootScope(this);
|
rootScope = new JsRootScope(this);
|
||||||
topScope = new JsObjectScope(rootScope, "Global");
|
topScope = new JsObjectScope(rootScope, "Global");
|
||||||
setFragmentCount(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsBlock getFragmentBlock(int fragment) {
|
public JsGlobalBlock getGlobalBlock() {
|
||||||
if (fragment < 0 || fragment >= fragments.length) {
|
return globalBlock;
|
||||||
throw new IllegalArgumentException("Invalid fragment: " + fragment);
|
|
||||||
}
|
|
||||||
return fragments[fragment].getGlobalBlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
public JsBlock getGlobalBlock() {
|
|
||||||
return getFragmentBlock(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsNumberLiteral getNumberLiteral(double value) {
|
public JsNumberLiteral getNumberLiteral(double value) {
|
||||||
@@ -94,13 +85,6 @@ public final class JsProgram extends SourceInfoAwareJsNode {
|
|||||||
return literal;
|
return literal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFragmentCount(int fragments) {
|
|
||||||
this.fragments = new JsProgramFragment[fragments];
|
|
||||||
for (int i = 0; i < fragments; i++) {
|
|
||||||
this.fragments[i] = new JsProgramFragment();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void accept(JsVisitor v) {
|
public void accept(JsVisitor v) {
|
||||||
v.visitProgram(this);
|
v.visitProgram(this);
|
||||||
@@ -108,17 +92,13 @@ public final class JsProgram extends SourceInfoAwareJsNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void acceptChildren(JsVisitor visitor) {
|
public void acceptChildren(JsVisitor visitor) {
|
||||||
for (JsProgramFragment fragment : fragments) {
|
visitor.accept(globalBlock);
|
||||||
visitor.accept(fragment);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void traverse(JsVisitorWithContext v, JsContext ctx) {
|
public void traverse(JsVisitorWithContext v, JsContext ctx) {
|
||||||
if (v.visit(this, ctx)) {
|
if (v.visit(this, ctx)) {
|
||||||
for (JsProgramFragment fragment : fragments) {
|
v.accept(globalBlock);
|
||||||
v.accept(fragment);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
v.endVisit(this, ctx);
|
v.endVisit(this, ctx);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,41 +6,59 @@ package org.jetbrains.kotlin.js.backend.ast;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
/**
|
import java.util.*;
|
||||||
* One independently loadable fragment of a {@link JsProgram}.
|
|
||||||
*/
|
|
||||||
public class JsProgramFragment extends SourceInfoAwareJsNode {
|
|
||||||
private final JsGlobalBlock globalBlock;
|
|
||||||
|
|
||||||
public JsProgramFragment() {
|
public class JsProgramFragment {
|
||||||
globalBlock = new JsGlobalBlock();
|
private final JsScope scope;
|
||||||
}
|
private final List<JsImportedModule> importedModules = new ArrayList<JsImportedModule>();
|
||||||
|
private final Map<String, JsExpression> imports = new LinkedHashMap<String, JsExpression>();
|
||||||
|
private final JsGlobalBlock declarationBlock = new JsGlobalBlock();
|
||||||
|
private final JsGlobalBlock exportBlock = new JsGlobalBlock();
|
||||||
|
private final JsGlobalBlock initializerBlock = new JsGlobalBlock();
|
||||||
|
private final List<JsNameBinding> nameBindings = new ArrayList<JsNameBinding>();
|
||||||
|
private final Map<JsName, JsClassModel> classes = new LinkedHashMap<JsName, JsClassModel>();
|
||||||
|
|
||||||
public JsBlock getGlobalBlock() {
|
public JsProgramFragment(@NotNull JsScope scope) {
|
||||||
return globalBlock;
|
this.scope = scope;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void accept(JsVisitor v) {
|
|
||||||
v.visitProgramFragment(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void acceptChildren(JsVisitor visitor) {
|
|
||||||
visitor.accept(globalBlock);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void traverse(JsVisitorWithContext v, JsContext ctx) {
|
|
||||||
if (v.visit(this, ctx)) {
|
|
||||||
v.acceptStatement(globalBlock);
|
|
||||||
}
|
|
||||||
v.endVisit(this, ctx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
public JsScope getScope() {
|
||||||
public JsProgramFragment deepCopy() {
|
return scope;
|
||||||
throw new UnsupportedOperationException();
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public List<JsImportedModule> getImportedModules() {
|
||||||
|
return importedModules;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Map<String, JsExpression> getImports() {
|
||||||
|
return imports;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public JsBlock getDeclarationBlock() {
|
||||||
|
return declarationBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public JsGlobalBlock getExportBlock() {
|
||||||
|
return exportBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public JsGlobalBlock getInitializerBlock() {
|
||||||
|
return initializerBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public List<JsNameBinding> getNameBindings() {
|
||||||
|
return nameBindings;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Map<JsName, JsClassModel> getClasses() {
|
||||||
|
return classes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,9 +120,6 @@ abstract class JsVisitor {
|
|||||||
open fun visitProgram(x: JsProgram): Unit =
|
open fun visitProgram(x: JsProgram): Unit =
|
||||||
visitElement(x)
|
visitElement(x)
|
||||||
|
|
||||||
open fun visitProgramFragment(x: JsProgramFragment): Unit =
|
|
||||||
visitElement(x)
|
|
||||||
|
|
||||||
open fun visitPropertyInitializer(x: JsPropertyInitializer): Unit =
|
open fun visitPropertyInitializer(x: JsPropertyInitializer): Unit =
|
||||||
visitElement(x)
|
visitElement(x)
|
||||||
|
|
||||||
|
|||||||
@@ -152,9 +152,6 @@ public abstract class JsVisitorWithContext {
|
|||||||
public void endVisit(@NotNull JsProgram x, @NotNull JsContext ctx) {
|
public void endVisit(@NotNull JsProgram x, @NotNull JsContext ctx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void endVisit(@NotNull JsProgramFragment x, @NotNull JsContext ctx) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public void endVisit(@NotNull JsPropertyInitializer x, @NotNull JsContext ctx) {
|
public void endVisit(@NotNull JsPropertyInitializer x, @NotNull JsContext ctx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,10 +309,6 @@ public abstract class JsVisitorWithContext {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean visit(@NotNull JsProgramFragment x, @NotNull JsContext ctx) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean visit(@NotNull JsPropertyInitializer x, @NotNull JsContext ctx) {
|
public boolean visit(@NotNull JsPropertyInitializer x, @NotNull JsContext ctx) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -399,7 +399,6 @@ internal open class JsExpressionVisitor() : JsVisitorWithContextImpl() {
|
|||||||
override fun visit(x: JsFunction, ctx: JsContext<JsNode>): Boolean = false
|
override fun visit(x: JsFunction, ctx: JsContext<JsNode>): Boolean = false
|
||||||
override fun visit(x: JsObjectLiteral, ctx: JsContext<JsNode>): Boolean = false
|
override fun visit(x: JsObjectLiteral, ctx: JsContext<JsNode>): Boolean = false
|
||||||
override fun visit(x: JsPropertyInitializer, ctx: JsContext<JsNode>): Boolean = false
|
override fun visit(x: JsPropertyInitializer, ctx: JsContext<JsNode>): Boolean = false
|
||||||
override fun visit(x: JsProgramFragment, ctx: JsContext<JsNode>): Boolean = false
|
|
||||||
override fun visit(x: JsProgram, ctx: JsContext<JsNode>): Boolean = false
|
override fun visit(x: JsProgram, ctx: JsContext<JsNode>): Boolean = false
|
||||||
override fun visit(x: JsParameter, ctx: JsContext<JsNode>): Boolean = false
|
override fun visit(x: JsParameter, ctx: JsContext<JsNode>): Boolean = false
|
||||||
override fun visit(x: JsCatch, ctx: JsContext<JsNode>): Boolean = false
|
override fun visit(x: JsCatch, ctx: JsContext<JsNode>): Boolean = false
|
||||||
|
|||||||
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.js.facade;
|
package org.jetbrains.kotlin.js.facade;
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.js.backend.ast.JsImportedModule;
|
||||||
|
import org.jetbrains.kotlin.js.backend.ast.JsProgram;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
|
import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
|
||||||
@@ -97,7 +99,7 @@ public final class K2JSTranslator {
|
|||||||
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
|
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
|
||||||
|
|
||||||
List<String> importedModules = new ArrayList<>();
|
List<String> importedModules = new ArrayList<>();
|
||||||
for (StaticContext.ImportedModule module : context.getImportedModules()) {
|
for (JsImportedModule module : context.getImportedModules()) {
|
||||||
importedModules.add(module.getExternalName());
|
importedModules.add(module.getExternalName());
|
||||||
}
|
}
|
||||||
return new TranslationResult.Success(config, files, program, diagnostics, importedModules, moduleDescriptor,
|
return new TranslationResult.Success(config, files, program, diagnostics, importedModules, moduleDescriptor,
|
||||||
|
|||||||
@@ -37,11 +37,6 @@ public class JsSourceGenerationVisitor extends JsToStringGenerationVisitor imple
|
|||||||
out.setOutListener(this);
|
out.setOutListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitProgramFragment(@NotNull JsProgramFragment x) {
|
|
||||||
x.acceptChildren(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void newLined() {
|
public void newLined() {
|
||||||
if (sourceMapBuilder != null) {
|
if (sourceMapBuilder != null) {
|
||||||
|
|||||||
@@ -125,9 +125,9 @@ public final class StaticContext {
|
|||||||
private final Map<DeclarationDescriptor, JsExpression> fqnCache = new HashMap<>();
|
private final Map<DeclarationDescriptor, JsExpression> fqnCache = new HashMap<>();
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Map<ImportedModuleKey, ImportedModule> importedModules = new LinkedHashMap<>();
|
private final Map<JsImportedModuleKey, JsImportedModule> importedModules = new LinkedHashMap<>();
|
||||||
|
|
||||||
private Collection<ImportedModule> readOnlyImportedModules;
|
private Collection<JsImportedModule> readOnlyImportedModules;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final JsScope rootPackageScope;
|
private final JsScope rootPackageScope;
|
||||||
@@ -175,8 +175,8 @@ public final class StaticContext {
|
|||||||
rootPackageScope = new JsObjectScope(rootScope, "<root package>");
|
rootPackageScope = new JsObjectScope(rootScope, "<root package>");
|
||||||
|
|
||||||
JsName kotlinName = rootScope.declareName(Namer.KOTLIN_NAME);
|
JsName kotlinName = rootScope.declareName(Namer.KOTLIN_NAME);
|
||||||
importedModules.put(new ImportedModuleKey(Namer.KOTLIN_LOWER_NAME, null),
|
importedModules.put(new JsImportedModuleKey(Namer.KOTLIN_LOWER_NAME, null),
|
||||||
new ImportedModule(Namer.KOTLIN_LOWER_NAME, kotlinName, null));
|
new JsImportedModule(Namer.KOTLIN_LOWER_NAME, kotlinName, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -205,7 +205,7 @@ public final class StaticContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Collection<ImportedModule> getImportedModules() {
|
public Collection<JsImportedModule> getImportedModules() {
|
||||||
if (readOnlyImportedModules == null) {
|
if (readOnlyImportedModules == null) {
|
||||||
readOnlyImportedModules = Collections.unmodifiableCollection(importedModules.values());
|
readOnlyImportedModules = Collections.unmodifiableCollection(importedModules.values());
|
||||||
}
|
}
|
||||||
@@ -286,7 +286,7 @@ public final class StaticContext {
|
|||||||
if (config.getModuleKind() != ModuleKind.PLAIN) {
|
if (config.getModuleKind() != ModuleKind.PLAIN) {
|
||||||
String moduleName = AnnotationsUtils.getModuleName(suggested.getDescriptor());
|
String moduleName = AnnotationsUtils.getModuleName(suggested.getDescriptor());
|
||||||
if (moduleName != null) {
|
if (moduleName != null) {
|
||||||
return JsAstUtils.pureFqn(getImportedModule(moduleName, suggested.getDescriptor()).internalName, null);
|
return JsAstUtils.pureFqn(getImportedModule(moduleName, suggested.getDescriptor()).getInternalName(), null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,7 +308,7 @@ public final class StaticContext {
|
|||||||
if (isNativeObject(suggested.getDescriptor()) && DescriptorUtils.isTopLevelDeclaration(suggested.getDescriptor())) {
|
if (isNativeObject(suggested.getDescriptor()) && DescriptorUtils.isTopLevelDeclaration(suggested.getDescriptor())) {
|
||||||
String fileModuleName = AnnotationsUtils.getFileModuleName(getBindingContext(), suggested.getDescriptor());
|
String fileModuleName = AnnotationsUtils.getFileModuleName(getBindingContext(), suggested.getDescriptor());
|
||||||
if (fileModuleName != null) {
|
if (fileModuleName != null) {
|
||||||
JsName moduleJsName = getImportedModule(fileModuleName, null).internalName;
|
JsName moduleJsName = getImportedModule(fileModuleName, null).getInternalName();
|
||||||
expression = pureFqn(moduleJsName, expression);
|
expression = pureFqn(moduleJsName, expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -484,8 +484,8 @@ public final class StaticContext {
|
|||||||
private JsName localOrImportedName(@NotNull DeclarationDescriptor descriptor, @NotNull String suggestedName) {
|
private JsName localOrImportedName(@NotNull DeclarationDescriptor descriptor, @NotNull String suggestedName) {
|
||||||
ModuleDescriptor module = DescriptorUtilsKt.getModule(descriptor);
|
ModuleDescriptor module = DescriptorUtilsKt.getModule(descriptor);
|
||||||
JsName name = module != currentModule ?
|
JsName name = module != currentModule ?
|
||||||
importDeclaration(suggestedName, getQualifiedReference(descriptor)) :
|
importDeclaration(suggestedName, getQualifiedReference(descriptor)) :
|
||||||
rootFunction.getScope().declareTemporaryName(suggestedName);
|
rootFunction.getScope().declareTemporaryName(suggestedName);
|
||||||
MetadataProperties.setDescriptor(name, descriptor);
|
MetadataProperties.setDescriptor(name, descriptor);
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@@ -667,15 +667,14 @@ public final class StaticContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private ImportedModule getImportedModule(@NotNull String baseName, @Nullable DeclarationDescriptor descriptor) {
|
private JsImportedModule getImportedModule(@NotNull String baseName, @Nullable DeclarationDescriptor descriptor) {
|
||||||
JsName plainName = descriptor != null && config.getModuleKind() == ModuleKind.UMD ?
|
String plainName = descriptor != null && config.getModuleKind() == ModuleKind.UMD ? getPlainId(descriptor) : null;
|
||||||
rootScope.declareName(getPlainId(descriptor)) : null;
|
JsImportedModuleKey key = new JsImportedModuleKey(baseName, plainName);
|
||||||
ImportedModuleKey key = new ImportedModuleKey(baseName, plainName);
|
|
||||||
|
|
||||||
ImportedModule module = importedModules.get(key);
|
JsImportedModule module = importedModules.get(key);
|
||||||
if (module == null) {
|
if (module == null) {
|
||||||
JsName internalName = rootScope.declareTemporaryName(Namer.LOCAL_MODULE_PREFIX + Namer.suggestedModuleName(baseName));
|
JsName internalName = rootScope.declareTemporaryName(Namer.LOCAL_MODULE_PREFIX + Namer.suggestedModuleName(baseName));
|
||||||
module = new ImportedModule(baseName, internalName, plainName != null ? pureFqn(plainName, null) : null);
|
module = new JsImportedModule(baseName, internalName, plainName != null ? pureFqn(plainName, null) : null);
|
||||||
importedModules.put(key, module);
|
importedModules.put(key, module);
|
||||||
}
|
}
|
||||||
return module;
|
return module;
|
||||||
@@ -692,7 +691,7 @@ public final class StaticContext {
|
|||||||
if (descriptor instanceof FunctionDescriptor ||
|
if (descriptor instanceof FunctionDescriptor ||
|
||||||
descriptor instanceof PackageFragmentDescriptor ||
|
descriptor instanceof PackageFragmentDescriptor ||
|
||||||
descriptor instanceof ClassDescriptor
|
descriptor instanceof ClassDescriptor
|
||||||
) {
|
) {
|
||||||
MetadataProperties.setSideEffects(expression, SideEffectKind.PURE);
|
MetadataProperties.setSideEffects(expression, SideEffectKind.PURE);
|
||||||
}
|
}
|
||||||
return expression;
|
return expression;
|
||||||
@@ -805,69 +804,4 @@ public final class StaticContext {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ImportedModule {
|
|
||||||
@NotNull
|
|
||||||
private final String externalName;
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private final JsName internalName;
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private final JsExpression plainReference;
|
|
||||||
|
|
||||||
ImportedModule(@NotNull String externalName, @NotNull JsName internalName, @Nullable JsExpression plainReference) {
|
|
||||||
this.externalName = externalName;
|
|
||||||
this.internalName = internalName;
|
|
||||||
this.plainReference = plainReference;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public String getExternalName() {
|
|
||||||
return externalName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public JsName getInternalName() {
|
|
||||||
return internalName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public JsExpression getPlainReference() {
|
|
||||||
return plainReference;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class ImportedModuleKey {
|
|
||||||
@NotNull
|
|
||||||
private final String baseName;
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private final JsName plainName;
|
|
||||||
|
|
||||||
public ImportedModuleKey(@NotNull String baseName, @Nullable JsName plainName) {
|
|
||||||
this.baseName = baseName;
|
|
||||||
this.plainName = plainName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) return true;
|
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
|
||||||
|
|
||||||
ImportedModuleKey key = (ImportedModuleKey) o;
|
|
||||||
|
|
||||||
if (!baseName.equals(key.baseName)) return false;
|
|
||||||
if (plainName != null ? !plainName.equals(key.plainName) : key.plainName != null) return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
int result = baseName.hashCode();
|
|
||||||
result = 31 * result + (plainName != null ? plainName.hashCode() : 0);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -126,7 +126,7 @@ public class TranslationContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Collection<StaticContext.ImportedModule> getImportedModules() {
|
public Collection<JsImportedModule> getImportedModules() {
|
||||||
return staticContext.getImportedModules();
|
return staticContext.getImportedModules();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -544,7 +544,7 @@ public class TranslationContext {
|
|||||||
if (result == null &&
|
if (result == null &&
|
||||||
classOrConstructor instanceof ConstructorDescriptor &&
|
classOrConstructor instanceof ConstructorDescriptor &&
|
||||||
((ConstructorDescriptor) classOrConstructor).isPrimary()
|
((ConstructorDescriptor) classOrConstructor).isPrimary()
|
||||||
) {
|
) {
|
||||||
result = staticContext.getClassOrConstructorClosure((ClassDescriptor) classOrConstructor.getContainingDeclaration());
|
result = staticContext.getClassOrConstructorClosure((ClassDescriptor) classOrConstructor.getContainingDeclaration());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
+8
-9
@@ -18,13 +18,12 @@ package org.jetbrains.kotlin.js.translate.general
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.js.backend.ast.*
|
import org.jetbrains.kotlin.js.backend.ast.*
|
||||||
import org.jetbrains.kotlin.js.translate.context.Namer
|
import org.jetbrains.kotlin.js.translate.context.Namer
|
||||||
import org.jetbrains.kotlin.js.translate.context.StaticContext
|
|
||||||
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils
|
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils
|
||||||
import org.jetbrains.kotlin.serialization.js.ModuleKind
|
import org.jetbrains.kotlin.serialization.js.ModuleKind
|
||||||
|
|
||||||
object ModuleWrapperTranslation {
|
object ModuleWrapperTranslation {
|
||||||
@JvmStatic fun wrapIfNecessary(
|
@JvmStatic fun wrapIfNecessary(
|
||||||
moduleId: String, function: JsExpression, importedModules: List<StaticContext.ImportedModule>,
|
moduleId: String, function: JsExpression, importedModules: List<JsImportedModule>,
|
||||||
program: JsProgram, kind: ModuleKind
|
program: JsProgram, kind: ModuleKind
|
||||||
): List<JsStatement> {
|
): List<JsStatement> {
|
||||||
return when (kind) {
|
return when (kind) {
|
||||||
@@ -37,7 +36,7 @@ object ModuleWrapperTranslation {
|
|||||||
|
|
||||||
private fun wrapUmd(
|
private fun wrapUmd(
|
||||||
moduleId: String, function: JsExpression,
|
moduleId: String, function: JsExpression,
|
||||||
importedModules: List<StaticContext.ImportedModule>, program: JsProgram
|
importedModules: List<JsImportedModule>, program: JsProgram
|
||||||
): List<JsStatement> {
|
): List<JsStatement> {
|
||||||
val scope = program.scope
|
val scope = program.scope
|
||||||
val defineName = scope.declareName("define")
|
val defineName = scope.declareName("define")
|
||||||
@@ -79,7 +78,7 @@ object ModuleWrapperTranslation {
|
|||||||
|
|
||||||
private fun wrapAmd(
|
private fun wrapAmd(
|
||||||
function: JsExpression,
|
function: JsExpression,
|
||||||
importedModules: List<StaticContext.ImportedModule>, program: JsProgram
|
importedModules: List<JsImportedModule>, program: JsProgram
|
||||||
): List<JsStatement> {
|
): List<JsStatement> {
|
||||||
val scope = program.scope
|
val scope = program.scope
|
||||||
val defineName = scope.declareName("define")
|
val defineName = scope.declareName("define")
|
||||||
@@ -94,7 +93,7 @@ object ModuleWrapperTranslation {
|
|||||||
|
|
||||||
private fun wrapCommonJs(
|
private fun wrapCommonJs(
|
||||||
function: JsExpression,
|
function: JsExpression,
|
||||||
importedModules: List<StaticContext.ImportedModule>,
|
importedModules: List<JsImportedModule>,
|
||||||
program: JsProgram
|
program: JsProgram
|
||||||
): List<JsStatement> {
|
): List<JsStatement> {
|
||||||
val scope = program.scope
|
val scope = program.scope
|
||||||
@@ -108,7 +107,7 @@ object ModuleWrapperTranslation {
|
|||||||
|
|
||||||
private fun wrapPlain(
|
private fun wrapPlain(
|
||||||
moduleId: String, function: JsExpression,
|
moduleId: String, function: JsExpression,
|
||||||
importedModules: List<StaticContext.ImportedModule>, program: JsProgram
|
importedModules: List<JsImportedModule>, program: JsProgram
|
||||||
): List<JsStatement> {
|
): List<JsStatement> {
|
||||||
val invocation = makePlainInvocation(moduleId, function, importedModules, program)
|
val invocation = makePlainInvocation(moduleId, function, importedModules, program)
|
||||||
val statements = mutableListOf<JsStatement>()
|
val statements = mutableListOf<JsStatement>()
|
||||||
@@ -130,7 +129,7 @@ object ModuleWrapperTranslation {
|
|||||||
private fun addModuleValidation(
|
private fun addModuleValidation(
|
||||||
currentModuleId: String,
|
currentModuleId: String,
|
||||||
program: JsProgram,
|
program: JsProgram,
|
||||||
module: StaticContext.ImportedModule
|
module: JsImportedModule
|
||||||
): JsStatement {
|
): JsStatement {
|
||||||
val moduleRef = makePlainModuleRef(module, program)
|
val moduleRef = makePlainModuleRef(module, program)
|
||||||
val moduleExistsCond = JsAstUtils.typeOfIs(moduleRef, program.getStringLiteral("undefined"))
|
val moduleExistsCond = JsAstUtils.typeOfIs(moduleRef, program.getStringLiteral("undefined"))
|
||||||
@@ -144,7 +143,7 @@ object ModuleWrapperTranslation {
|
|||||||
private fun makePlainInvocation(
|
private fun makePlainInvocation(
|
||||||
moduleId: String,
|
moduleId: String,
|
||||||
function: JsExpression,
|
function: JsExpression,
|
||||||
importedModules: List<StaticContext.ImportedModule>,
|
importedModules: List<JsImportedModule>,
|
||||||
program: JsProgram
|
program: JsProgram
|
||||||
): JsInvocation {
|
): JsInvocation {
|
||||||
val invocationArgs = importedModules.map { makePlainModuleRef(it, program) }
|
val invocationArgs = importedModules.map { makePlainModuleRef(it, program) }
|
||||||
@@ -155,7 +154,7 @@ object ModuleWrapperTranslation {
|
|||||||
return JsInvocation(function, listOf(selfArg) + invocationArgs)
|
return JsInvocation(function, listOf(selfArg) + invocationArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun makePlainModuleRef(module: StaticContext.ImportedModule, program: JsProgram): JsExpression {
|
private fun makePlainModuleRef(module: JsImportedModule, program: JsProgram): JsExpression {
|
||||||
return module.plainReference ?: makePlainModuleRef(module.externalName, program)
|
return module.plainReference ?: makePlainModuleRef(module.externalName, program)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -283,9 +283,9 @@ public final class Translation {
|
|||||||
|
|
||||||
// Invoke function passing modules as arguments
|
// Invoke function passing modules as arguments
|
||||||
// This should help minifier tool to recognize references to these modules as local variables and make them shorter.
|
// This should help minifier tool to recognize references to these modules as local variables and make them shorter.
|
||||||
List<StaticContext.ImportedModule> importedModuleList = new ArrayList<>();
|
List<JsImportedModule> importedModuleList = new ArrayList<>();
|
||||||
|
|
||||||
for (StaticContext.ImportedModule importedModule : staticContext.getImportedModules()) {
|
for (JsImportedModule importedModule : staticContext.getImportedModules()) {
|
||||||
rootFunction.getParameters().add(new JsParameter(importedModule.getInternalName()));
|
rootFunction.getParameters().add(new JsParameter(importedModule.getInternalName()));
|
||||||
importedModuleList.add(importedModule);
|
importedModuleList.add(importedModule);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user