Check for undefined whenever check for null

This commit is contained in:
pTalanov
2012-06-15 17:25:59 +04:00
parent 5f3c3d7e2b
commit 1ba6470e8d
7 changed files with 69 additions and 9 deletions
@@ -67,4 +67,8 @@ public final class NativeInteropTest extends SingleFileTranslationTest {
public void testClassObject() throws Exception {
fooBoxTest();
}
public void testSimpleUndefined() throws Exception {
fooBoxTest();
}
}
@@ -26,6 +26,8 @@ import java.util.List;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.equality;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.inequality;
import static org.jetbrains.k2js.translate.utils.TranslationUtils.isNullLiteral;
import static org.jetbrains.k2js.translate.utils.TranslationUtils.nullCheck;
/**
* @author Pavel Talanov
@@ -40,17 +42,24 @@ public final class PrimitiveEqualsIntrinsic extends EqualsIntrinsic {
private PrimitiveEqualsIntrinsic() {
}
@Override
@NotNull
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
@NotNull TranslationContext context) {
assert arguments.size() == 1 : "Equals operation should have one argument";
assert receiver != null;
JsExpression argument = arguments.get(0);
if (isNullLiteral(context, argument)) {
return nullCheck(context, receiver, !isNegated());
}
if (isNullLiteral(context, receiver)) {
return nullCheck(context, argument, !isNegated());
}
if (isNegated()) {
return inequality(receiver, arguments.get(0));
return inequality(receiver, argument);
}
else {
return equality(receiver, arguments.get(0));
return equality(receiver, argument);
}
}
}
@@ -41,10 +41,8 @@ public final class OperatorTable {
.put(JetTokens.GTEQ, JsBinaryOperator.GTE)
.put(JetTokens.LT, JsBinaryOperator.LT)
.put(JetTokens.LTEQ, JsBinaryOperator.LTE)
.put(JetTokens.EQEQ, JsBinaryOperator.REF_EQ)
.put(JetTokens.ANDAND, JsBinaryOperator.AND)
.put(JetTokens.OROR, JsBinaryOperator.OR)
.put(JetTokens.EXCLEQ, JsBinaryOperator.NEQ)
.put(JetTokens.PERC, JsBinaryOperator.MOD)
.put(JetTokens.PLUSEQ, JsBinaryOperator.ASG_ADD)
.put(JetTokens.MINUSEQ, JsBinaryOperator.ASG_SUB)
@@ -127,12 +127,12 @@ public final class JsAstUtils {
@NotNull
public static JsBinaryOperation equality(@NotNull JsExpression arg1, @NotNull JsExpression arg2) {
return new JsBinaryOperation(JsBinaryOperator.EQ, arg1, arg2);
return new JsBinaryOperation(JsBinaryOperator.REF_EQ, arg1, arg2);
}
@NotNull
public static JsBinaryOperation inequality(@NotNull JsExpression arg1, @NotNull JsExpression arg2) {
return new JsBinaryOperation(JsBinaryOperator.NEQ, arg1, arg2);
return new JsBinaryOperation(JsBinaryOperator.REF_NEQ, arg1, arg2);
}
@NotNull
@@ -17,6 +17,7 @@
package org.jetbrains.k2js.translate.utils;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
@@ -42,6 +43,8 @@ import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getExpectedRe
*/
public final class TranslationUtils {
private static JsNameRef UNDEFINED_LITERAL = AstUtil.newQualifiedNameRef("undefined");
private TranslationUtils() {
}
@@ -73,14 +76,29 @@ public final class TranslationUtils {
public static JsBinaryOperation notNullCheck(@NotNull TranslationContext context,
@NotNull JsExpression expressionToCheck) {
JsNullLiteral nullLiteral = context.program().getNullLiteral();
return inequality(expressionToCheck, nullLiteral);
JsBinaryOperation notNull = inequality(expressionToCheck, nullLiteral);
JsBinaryOperation notUndefined = inequality(expressionToCheck, UNDEFINED_LITERAL);
return and(notNull, notUndefined);
}
@NotNull
public static JsBinaryOperation isNullCheck(@NotNull TranslationContext context,
@NotNull JsExpression expressionToCheck) {
JsNullLiteral nullLiteral = context.program().getNullLiteral();
return equality(expressionToCheck, nullLiteral);
JsBinaryOperation isNull = equality(expressionToCheck, nullLiteral);
JsBinaryOperation isUndefined = equality(expressionToCheck, UNDEFINED_LITERAL);
return or(isNull, isUndefined);
}
@NotNull
public static JsBinaryOperation nullCheck(@NotNull TranslationContext context,
@NotNull JsExpression expressionToCheck, boolean shouldBeNull) {
if (shouldBeNull) {
return isNullCheck(context, expressionToCheck);
}
else {
return notNullCheck(context, expressionToCheck);
}
}
@NotNull
@@ -237,4 +255,8 @@ public final class TranslationUtils {
DeclarationDescriptor expectedThisDescriptor = getDeclarationDescriptorForReceiver(thisObject);
return getThisObject(context, expectedThisDescriptor);
}
public static boolean isNullLiteral(@NotNull TranslationContext context, @NotNull JsExpression expression) {
return expression.equals(context.program().getNullLiteral());
}
}
@@ -0,0 +1,11 @@
package foo
import js.*
native
val c: Any? = js.noImpl
fun box(): Boolean {
if (c != null) return false
return (c == null)
}
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2012 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.
*/
var c = undefined;