JS: make enum valueOf() throw if not found.
This commit is contained in:
@@ -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
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-7
@@ -7515,13 +7515,7 @@ 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")
|
||||||
|
|||||||
@@ -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());
|
||||||
|
|||||||
+12
-1
@@ -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
@@ -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.
|
||||||
|
|||||||
Reference in New Issue
Block a user