Null check logic in TranslationUtils.

Adapted from https://github.com/develar/kotlin/commit/a9e0a42fb1347fa8e21c86b5a073ef8a7c873da0.
This commit is contained in:
Pavel V. Talanov
2012-08-13 13:39:01 +04:00
parent 2ca5c276ef
commit 226a01fa80
3 changed files with 17 additions and 13 deletions
@@ -110,8 +110,8 @@ public final class PatternTranslator extends AbstractTranslator {
}
@NotNull
private JsExpression addNullCheck(@NotNull JsExpression expressionToMatch, @NotNull JsInvocation isCheck) {
return or(TranslationUtils.isNullCheck(context(), expressionToMatch), isCheck);
private static JsExpression addNullCheck(@NotNull JsExpression expressionToMatch, @NotNull JsInvocation isCheck) {
return or(TranslationUtils.isNullCheck(expressionToMatch), isCheck);
}
private boolean isNullable(JetTypePattern pattern) {
@@ -42,7 +42,7 @@ public enum CallType {
@NotNull TranslationContext context) {
assert receiver != null;
TemporaryVariable temporaryVariable = context.declareTemporary(receiver);
JsBinaryOperation notNullCheck = TranslationUtils.notNullCheck(context, temporaryVariable.reference());
JsBinaryOperation notNullCheck = TranslationUtils.notNullConditionalTestExpression(temporaryVariable);
JsExpression methodCall = constructor.construct(temporaryVariable.reference());
JsConditional callMethodIfNotNullElseNull = new JsConditional(notNullCheck, methodCall, JsNullLiteral.NULL);
return newSequence(temporaryVariable.assignmentExpression(), callMethodIfNotNullElseNull);
@@ -24,6 +24,7 @@ import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.calls.ResolvedCall;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.k2js.translate.context.TemporaryVariable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.general.Translation;
@@ -66,19 +67,22 @@ public final class TranslationUtils {
}
@NotNull
public static JsBinaryOperation notNullCheck(@NotNull TranslationContext context,
@NotNull JsExpression expressionToCheck) {
JsBinaryOperation notNull = inequality(expressionToCheck, JsLiteral.NULL);
JsBinaryOperation notUndefined = inequality(expressionToCheck, UNDEFINED_LITERAL);
return and(notNull, notUndefined);
public static JsBinaryOperation isNullCheck(@NotNull JsExpression expressionToCheck) {
return nullCheck(expressionToCheck, false);
}
@NotNull
public static JsBinaryOperation isNullCheck(@NotNull TranslationContext context,
@NotNull JsExpression expressionToCheck) {
JsBinaryOperation isNull = equality(expressionToCheck, JsLiteral.NULL);
JsBinaryOperation isUndefined = equality(expressionToCheck, UNDEFINED_LITERAL);
return or(isNull, isUndefined);
public static JsBinaryOperation notNullConditionalTestExpression(@NotNull TemporaryVariable cachedValue) {
return and(inequality(cachedValue.assignmentExpression(), JsLiteral.NULL),
inequality(cachedValue.reference(), JsLiteral.UNDEFINED));
}
@NotNull
public static JsBinaryOperation nullCheck(@NotNull JsExpression expressionToCheck, boolean isNegated) {
JsBinaryOperator operator = isNegated ? JsBinaryOperator.REF_NEQ : JsBinaryOperator.REF_EQ;
return new JsBinaryOperation(isNegated ? JsBinaryOperator.AND : JsBinaryOperator.OR,
new JsBinaryOperation(operator, expressionToCheck, JsLiteral.NULL),
new JsBinaryOperation(operator, expressionToCheck, JsLiteral.UNDEFINED));
}
@NotNull