JS: add intrinsic for !!. Use it instead of ternary conditionals

See KT-2976
This commit is contained in:
Alexey Andreev
2017-10-24 16:02:13 +03:00
parent 8631e898a7
commit aae67e154a
3 changed files with 6 additions and 16 deletions
+3 -1
View File
@@ -109,4 +109,6 @@ function imul(a, b) {
}
})();
Kotlin.ensureNotNull = function(x) {
return x != null ? x : Kotlin.throwNPE();
};
@@ -86,10 +86,10 @@ public final class Namer {
private static final String RECEIVER_PARAMETER_NAME = "$receiver";
public static final String ANOTHER_THIS_PARAMETER_NAME = "$this";
public static final String THROW_NPE_FUN_NAME = "throwNPE";
public static final String THROW_CLASS_CAST_EXCEPTION_FUN_NAME = "throwCCE";
public static final String THROW_ILLEGAL_STATE_EXCEPTION_FUN_NAME = "throwISE";
public static final String THROW_UNINITIALIZED_PROPERTY_ACCESS_EXCEPTION = "throwUPAE";
public static final String NULL_CHECK_INTRINSIC_NAME = "ensureNotNull";
private static final String PROTOTYPE_NAME = "prototype";
private static final String CAPTURED_VAR_FIELD = "v";
@@ -310,20 +310,8 @@ public final class TranslationUtils {
}
@NotNull
public static JsConditional sure(@NotNull JsExpression expression, @NotNull TranslationContext context) {
JsInvocation throwNPE = new JsInvocation(context.getReferenceToIntrinsic(Namer.THROW_NPE_FUN_NAME));
JsConditional ensureNotNull = notNullConditional(expression, throwNPE, context);
JsExpression thenExpression = ensureNotNull.getThenExpression();
if (thenExpression instanceof JsNameRef) {
JsName name = ((JsNameRef) thenExpression).getName();
if (name != null) {
// associate(cache) ensureNotNull expression to new TemporaryConstVariable with same name.
context.associateExpressionToLazyValue(ensureNotNull, new TemporaryConstVariable(name, ensureNotNull));
}
}
return ensureNotNull;
public static JsExpression sure(@NotNull JsExpression expression, @NotNull TranslationContext context) {
return new JsInvocation(context.getReferenceToIntrinsic(Namer.NULL_CHECK_INTRINSIC_NAME), expression);
}
public static boolean isSimpleNameExpressionNotDelegatedLocalVar(@Nullable KtExpression expression, @NotNull TranslationContext context) {