JS: expand calls, generated to curry reified is checks
This commit is contained in:
+6
@@ -34,3 +34,9 @@ public var JsFunction.isLocal: Boolean by MetadataProperty(default = false)
|
|||||||
|
|
||||||
public var JsParameter.hasDefaultValue: Boolean by MetadataProperty(default = false)
|
public var JsParameter.hasDefaultValue: Boolean by MetadataProperty(default = false)
|
||||||
|
|
||||||
|
public var JsInvocation.typeCheck: TypeCheck? by MetadataProperty(default = null)
|
||||||
|
|
||||||
|
public enum class TypeCheck {
|
||||||
|
TYPEOF
|
||||||
|
INSTANCEOF
|
||||||
|
}
|
||||||
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.js.inline.util
|
|||||||
|
|
||||||
import com.google.dart.compiler.backend.js.ast.JsLiteral.*
|
import com.google.dart.compiler.backend.js.ast.JsLiteral.*
|
||||||
import com.google.dart.compiler.backend.js.ast.*
|
import com.google.dart.compiler.backend.js.ast.*
|
||||||
|
import com.google.dart.compiler.backend.js.ast.metadata.typeCheck
|
||||||
import org.jetbrains.kotlin.js.translate.utils.ast.any
|
import org.jetbrains.kotlin.js.translate.utils.ast.any
|
||||||
|
|
||||||
public fun JsExpression.canHaveSideEffect(): Boolean =
|
public fun JsExpression.canHaveSideEffect(): Boolean =
|
||||||
@@ -44,5 +45,6 @@ public fun JsExpression.shouldHaveOwnAlias(): Boolean =
|
|||||||
is JsConditional,
|
is JsConditional,
|
||||||
is JsBinaryOperation,
|
is JsBinaryOperation,
|
||||||
is JsArrayLiteral -> true
|
is JsArrayLiteral -> true
|
||||||
|
is JsInvocation -> if (typeCheck == null) canHaveSideEffect() else false
|
||||||
else -> canHaveOwnSideEffect()
|
else -> canHaveOwnSideEffect()
|
||||||
}
|
}
|
||||||
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.diagnostics.DiagnosticUtils.hasError;
|
import static org.jetbrains.kotlin.diagnostics.DiagnosticUtils.hasError;
|
||||||
|
import static org.jetbrains.kotlin.js.translate.utils.UtilsPackage.expandIsCalls;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An entry point of translator.
|
* An entry point of translator.
|
||||||
@@ -79,6 +80,7 @@ public final class K2JSTranslator {
|
|||||||
JsProgram program = JsInliner.process(context);
|
JsProgram program = JsInliner.process(context);
|
||||||
if (hasError(diagnostics)) return new TranslationResult.Fail(diagnostics);
|
if (hasError(diagnostics)) return new TranslationResult.Fail(diagnostics);
|
||||||
|
|
||||||
|
expandIsCalls(program, context);
|
||||||
return new TranslationResult.Success(config, files, program, diagnostics, moduleDescriptor);
|
return new TranslationResult.Success(config, files, program, diagnostics, moduleDescriptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,8 @@
|
|||||||
package org.jetbrains.kotlin.js.translate.context;
|
package org.jetbrains.kotlin.js.translate.context;
|
||||||
|
|
||||||
import com.google.dart.compiler.backend.js.ast.*;
|
import com.google.dart.compiler.backend.js.ast.*;
|
||||||
|
import com.google.dart.compiler.backend.js.ast.metadata.TypeCheck;
|
||||||
|
import com.google.dart.compiler.backend.js.ast.metadata.MetadataPackage;
|
||||||
import com.intellij.openapi.util.text.StringUtil;
|
import com.intellij.openapi.util.text.StringUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||||
@@ -375,12 +377,21 @@ public final class Namer {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public JsExpression isTypeOf(@NotNull JsExpression type) {
|
public JsExpression isTypeOf(@NotNull JsExpression type) {
|
||||||
return new JsInvocation(kotlin("isTypeOf"), type);
|
JsInvocation invocation = new JsInvocation(kotlin("isTypeOf"), type);
|
||||||
|
MetadataPackage.setTypeCheck(invocation, TypeCheck.TYPEOF);
|
||||||
|
return invocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public JsExpression isInstanceOf(@NotNull JsExpression type) {
|
public JsExpression isInstanceOf(@NotNull JsExpression type) {
|
||||||
return new JsInvocation(kotlin("isInstanceOf"), type);
|
JsInvocation invocation = new JsInvocation(kotlin("isInstanceOf"), type);
|
||||||
|
MetadataPackage.setTypeCheck(invocation, TypeCheck.INSTANCEOF);
|
||||||
|
return invocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public JsExpression isInstanceOf(@NotNull JsExpression instance, @NotNull JsExpression type) {
|
||||||
|
return new JsInvocation(kotlin(isTypeName), instance, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2015 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.translate.utils
|
||||||
|
|
||||||
|
import com.google.dart.compiler.backend.js.ast.*
|
||||||
|
import com.google.dart.compiler.backend.js.ast.metadata.TypeCheck
|
||||||
|
import com.google.dart.compiler.backend.js.ast.metadata.typeCheck
|
||||||
|
import org.jetbrains.kotlin.js.translate.context.TranslationContext
|
||||||
|
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils.*
|
||||||
|
|
||||||
|
public fun expandIsCalls(node: JsNode, context: TranslationContext) {
|
||||||
|
val visitor = object : JsVisitorWithContextImpl() {
|
||||||
|
override fun visit(x: JsInvocation, ctx: JsContext<*>): Boolean {
|
||||||
|
val callee = x.getQualifier() as? JsInvocation
|
||||||
|
val instance = x.getArguments().firstOrNull()
|
||||||
|
val type = callee?.getArguments()?.firstOrNull()
|
||||||
|
|
||||||
|
val replacement = when (callee?.typeCheck) {
|
||||||
|
TypeCheck.TYPEOF -> typeof(instance!!, type as JsStringLiteral)
|
||||||
|
TypeCheck.INSTANCEOF -> context.namer().isInstanceOf(instance!!, type!!)
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (replacement != null) {
|
||||||
|
ctx.replaceMe(replacement)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.visit(x, ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
visitor.accept(node)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user