JS: include expandIsCalls in new translator pipeline
This commit is contained in:
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.js.inline.clean.RemoveUnusedImportsKt;
|
|||||||
import org.jetbrains.kotlin.js.inline.clean.ResolveTemporaryNamesKt;
|
import org.jetbrains.kotlin.js.inline.clean.ResolveTemporaryNamesKt;
|
||||||
import org.jetbrains.kotlin.js.translate.general.AstGenerationResult;
|
import org.jetbrains.kotlin.js.translate.general.AstGenerationResult;
|
||||||
import org.jetbrains.kotlin.js.translate.general.Translation;
|
import org.jetbrains.kotlin.js.translate.general.Translation;
|
||||||
|
import org.jetbrains.kotlin.js.translate.utils.ExpandIsCallsKt;
|
||||||
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus;
|
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus;
|
||||||
import org.jetbrains.kotlin.psi.KtFile;
|
import org.jetbrains.kotlin.psi.KtFile;
|
||||||
import org.jetbrains.kotlin.resolve.BindingTrace;
|
import org.jetbrains.kotlin.resolve.BindingTrace;
|
||||||
@@ -93,7 +94,7 @@ public final class K2JSTranslator {
|
|||||||
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
|
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
|
||||||
if (hasError(diagnostics)) return new TranslationResult.Fail(diagnostics);
|
if (hasError(diagnostics)) return new TranslationResult.Fail(diagnostics);
|
||||||
|
|
||||||
//expandIsCalls(program, context);
|
ExpandIsCallsKt.expandIsCalls(translationResult.getFragments());
|
||||||
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
|
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
|
||||||
|
|
||||||
List<String> importedModules = new ArrayList<>();
|
List<String> importedModules = new ArrayList<>();
|
||||||
|
|||||||
@@ -201,21 +201,16 @@ public final class Namer {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private final JsExpression callSetProperty;
|
private final JsExpression callSetProperty;
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private final JsName isTypeName;
|
|
||||||
|
|
||||||
private Namer(@NotNull JsScope rootScope) {
|
private Namer(@NotNull JsScope rootScope) {
|
||||||
kotlinScope = new JsObjectScope(rootScope, "Kotlin standard object");
|
kotlinScope = new JsObjectScope(rootScope, "Kotlin standard object");
|
||||||
|
|
||||||
callGetProperty = kotlin("callGetter");
|
callGetProperty = kotlin("callGetter");
|
||||||
callSetProperty = kotlin("callSetter");
|
callSetProperty = kotlin("callSetter");
|
||||||
|
|
||||||
isTypeName = kotlinScope.declareName("isType");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: get rid of this function
|
// TODO: get rid of this function
|
||||||
@NotNull
|
@NotNull
|
||||||
public static String getStableMangledNameForDescriptor(@NotNull ClassDescriptor descriptor, @NotNull String functionName) {
|
private static String getStableMangledNameForDescriptor(@NotNull ClassDescriptor descriptor, @NotNull String functionName) {
|
||||||
Collection<SimpleFunctionDescriptor> functions = descriptor.getDefaultType().getMemberScope().getContributedFunctions(
|
Collection<SimpleFunctionDescriptor> functions = descriptor.getDefaultType().getMemberScope().getContributedFunctions(
|
||||||
Name.identifier(functionName), NoLookupLocation.FROM_BACKEND);
|
Name.identifier(functionName), NoLookupLocation.FROM_BACKEND);
|
||||||
assert functions.size() == 1 : "Can't select a single function: " + functionName + " in " + descriptor;
|
assert functions.size() == 1 : "Can't select a single function: " + functionName + " in " + descriptor;
|
||||||
@@ -318,8 +313,8 @@ public final class Namer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public JsExpression isInstanceOf(@NotNull JsExpression instance, @NotNull JsExpression type) {
|
public static JsExpression isInstanceOf(@NotNull JsExpression instance, @NotNull JsExpression type) {
|
||||||
JsInvocation result = new JsInvocation(kotlin(isTypeName), instance, type);
|
JsInvocation result = new JsInvocation(new JsNameRef("isType", KOTLIN_NAME), instance, type);
|
||||||
MetadataProperties.setSideEffects(result, SideEffectKind.PURE);
|
MetadataProperties.setSideEffects(result, SideEffectKind.PURE);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,15 +20,19 @@ import org.jetbrains.kotlin.js.backend.ast.*
|
|||||||
import org.jetbrains.kotlin.js.backend.ast.metadata.TypeCheck
|
import org.jetbrains.kotlin.js.backend.ast.metadata.TypeCheck
|
||||||
import org.jetbrains.kotlin.js.backend.ast.metadata.typeCheck
|
import org.jetbrains.kotlin.js.backend.ast.metadata.typeCheck
|
||||||
import org.jetbrains.kotlin.js.inline.util.IdentitySet
|
import org.jetbrains.kotlin.js.inline.util.IdentitySet
|
||||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext
|
import org.jetbrains.kotlin.js.translate.context.Namer
|
||||||
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils.*
|
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
fun expandIsCalls(node: JsNode, context: TranslationContext) {
|
fun expandIsCalls(fragments: List<JsProgramFragment>) {
|
||||||
TypeCheckRewritingVisitor(context).accept(node)
|
val visitor = TypeCheckRewritingVisitor()
|
||||||
|
for (fragment in fragments) {
|
||||||
|
visitor.accept(fragment.declarationBlock)
|
||||||
|
visitor.accept(fragment.initializerBlock)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TypeCheckRewritingVisitor(private val context: TranslationContext) : JsVisitorWithContextImpl() {
|
private class TypeCheckRewritingVisitor : JsVisitorWithContextImpl() {
|
||||||
|
|
||||||
private val scopes = Stack<JsScope>()
|
private val scopes = Stack<JsScope>()
|
||||||
private val localVars = Stack<MutableSet<JsName>>().apply { push(mutableSetOf()) }
|
private val localVars = Stack<MutableSet<JsName>>().apply { push(mutableSetOf()) }
|
||||||
@@ -78,7 +82,7 @@ private class TypeCheckRewritingVisitor(private val context: TranslationContext)
|
|||||||
|
|
||||||
TypeCheck.INSTANCEOF -> {
|
TypeCheck.INSTANCEOF -> {
|
||||||
// `Kotlin.isInstanceOf(calleeArgument)(argument)` -> `argument instanceof calleeArgument`
|
// `Kotlin.isInstanceOf(calleeArgument)(argument)` -> `argument instanceof calleeArgument`
|
||||||
if (calleeArguments.size == 1) context.namer().isInstanceOf(argument, calleeArguments[0]) else null
|
if (calleeArguments.size == 1) Namer.isInstanceOf(argument, calleeArguments[0]) else null
|
||||||
}
|
}
|
||||||
|
|
||||||
TypeCheck.OR_NULL -> {
|
TypeCheck.OR_NULL -> {
|
||||||
|
|||||||
Reference in New Issue
Block a user