JS: make enum valueOf() throw if not found.

This commit is contained in:
Anton Bannykh
2016-12-05 16:46:43 +03:00
parent e0c75f1fb8
commit 1957ac347a
7 changed files with 36 additions and 12 deletions
@@ -1,6 +1,3 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
enum class Empty enum class Empty
fun box(): String { fun box(): String {
+11
View File
@@ -3,9 +3,20 @@ enum class Color {
BLUE BLUE
} }
fun throwsOnGreen(): Boolean {
try {
Color.valueOf("GREEN")
return false
}
catch (e: Exception) {
return true
}
}
fun box() = if( fun box() = if(
Color.valueOf("RED") == Color.RED Color.valueOf("RED") == Color.RED
&& Color.valueOf("BLUE") == Color.BLUE && Color.valueOf("BLUE") == Color.BLUE
&& Color.values()[0] == Color.RED && Color.values()[0] == Color.RED
&& Color.values()[1] == Color.BLUE && Color.values()[1] == Color.BLUE
&& throwsOnGreen()
) "OK" else "fail" ) "OK" else "fail"
@@ -9,4 +9,5 @@ public enum class Color {
public final class ValueofKt { public final class ValueofKt {
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
public final static method throwsOnGreen(): boolean
} }
@@ -7515,14 +7515,8 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
@TestMetadata("emptyEnumValuesValueOf.kt") @TestMetadata("emptyEnumValuesValueOf.kt")
public void testEmptyEnumValuesValueOf() throws Exception { public void testEmptyEnumValuesValueOf() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/emptyEnumValuesValueOf.kt"); String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/emptyEnumValuesValueOf.kt");
try {
doTest(fileName); doTest(fileName);
} }
catch (Throwable ignore) {
return;
}
throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.");
}
@TestMetadata("enumInheritedFromTrait.kt") @TestMetadata("enumInheritedFromTrait.kt")
public void testEnumInheritedFromTrait() throws Exception { public void testEnumInheritedFromTrait() throws Exception {
@@ -97,6 +97,7 @@ public final class Namer {
private static final String THROW_NPE_FUN_NAME = "throwNPE"; private static final String THROW_NPE_FUN_NAME = "throwNPE";
private static final String THROW_CLASS_CAST_EXCEPTION_FUN_NAME = "throwCCE"; private static final String THROW_CLASS_CAST_EXCEPTION_FUN_NAME = "throwCCE";
private static final String THROW_ILLEGAL_STATE_EXCEPTION_FUN_NAME = "throwISE";
private static final String PROTOTYPE_NAME = "prototype"; private static final String PROTOTYPE_NAME = "prototype";
private static final String CAPTURED_VAR_FIELD = "v"; private static final String CAPTURED_VAR_FIELD = "v";
@@ -329,6 +330,11 @@ public final class Namer {
return new JsNameRef(THROW_CLASS_CAST_EXCEPTION_FUN_NAME, kotlinObject()); return new JsNameRef(THROW_CLASS_CAST_EXCEPTION_FUN_NAME, kotlinObject());
} }
@NotNull
public static JsExpression throwIllegalStateExcpetionFunRef() {
return new JsNameRef(THROW_ILLEGAL_STATE_EXCEPTION_FUN_NAME, kotlinObject());
}
@NotNull @NotNull
public static JsNameRef kotlin(@NotNull JsName name) { public static JsNameRef kotlin(@NotNull JsName name) {
return pureFqn(name, kotlinObject()); return pureFqn(name, kotlinObject());
@@ -19,10 +19,12 @@ package org.jetbrains.kotlin.js.translate.declaration
import com.google.dart.compiler.backend.js.ast.* import com.google.dart.compiler.backend.js.ast.*
import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.js.translate.context.Namer
import org.jetbrains.kotlin.js.translate.context.TranslationContext import org.jetbrains.kotlin.js.translate.context.TranslationContext
import org.jetbrains.kotlin.js.translate.general.AbstractTranslator import org.jetbrains.kotlin.js.translate.general.AbstractTranslator
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils import org.jetbrains.kotlin.js.translate.utils.JsAstUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
class EnumTranslator( class EnumTranslator(
context: TranslationContext, context: TranslationContext,
@@ -54,8 +56,17 @@ class EnumTranslator(
} }
} }
val message = JsBinaryOperation(JsBinaryOperator.ADD,
context().program().getStringLiteral("No enum constant ${descriptor.fqNameSafe}."),
nameParam.makeRef())
val throwStatement = JsExpressionStatement(JsInvocation(Namer.throwIllegalStateExcpetionFunRef(), message))
if (clauses.isNotEmpty()) { if (clauses.isNotEmpty()) {
function.body.statements += JsSwitch(nameParam.makeRef(), clauses) val defaultCase = JsDefault().apply { statements += throwStatement }
function.body.statements += JsSwitch(nameParam.makeRef(), clauses + defaultCase)
}
else {
function.body.statements += throwStatement
} }
} }
+4
View File
@@ -207,6 +207,10 @@ Kotlin.throwCCE = function () {
throw new Kotlin.kotlin.ClassCastException("Illegal cast"); throw new Kotlin.kotlin.ClassCastException("Illegal cast");
}; };
Kotlin.throwISE = function (message) {
throw new Kotlin.kotlin.IllegalStateException(message);
};
/** @const */ /** @const */
var POW_2_32 = 4294967296; var POW_2_32 = 4294967296;
// TODO: consider switching to Symbol type once we are on ES6. // TODO: consider switching to Symbol type once we are on ES6.