提交 6ee8a23d 编写于 作者: I Ilya Gorbunov

JS: Drop native map get/set intrinsics altogether.

MutableMap.set extension operator is now common among stdlib and stdlib-js.
#KT-2323
上级 e8c9e812
......@@ -7,7 +7,6 @@ public external fun eval(expr: String): dynamic = noImpl
public external val undefined: Nothing? = noImpl
inline operator fun <K, V> MutableMap<K, V>.set(key: K, value: V): V? = asDynamic().set(key, value)
@library
public fun println() {}
......
......@@ -5723,12 +5723,6 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
doTest(fileName);
}
@TestMetadata("kt2323.kt")
public void testKt2323() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/native/kt2323.kt");
doTest(fileName);
}
@TestMetadata("library.kt")
public void testLibrary() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/native/library.kt");
......
......@@ -91,53 +91,6 @@ public final class TopLevelFIF extends CompositeFIF {
}
};
private static final FunctionIntrinsic NATIVE_MAP_GET = new NativeMapGetSet() {
@NotNull
@Override
protected String operationName() {
return "get";
}
@Nullable
@Override
protected ExpressionReceiver getExpressionReceiver(@NotNull ResolvedCall<?> resolvedCall) {
ReceiverValue result = resolvedCall.getDispatchReceiver();
return result instanceof ExpressionReceiver ? (ExpressionReceiver) result : null;
}
@Override
protected JsExpression asArrayAccess(
@NotNull JsExpression receiver,
@NotNull List<JsExpression> arguments,
@NotNull TranslationContext context
) {
return ArrayFIF.GET_INTRINSIC.apply(receiver, arguments, context);
}
};
private static final FunctionIntrinsic NATIVE_MAP_SET = new NativeMapGetSet() {
@NotNull
@Override
protected String operationName() {
return "put";
}
@Nullable
@Override
protected ExpressionReceiver getExpressionReceiver(@NotNull ResolvedCall<?> resolvedCall) {
ReceiverValue result = resolvedCall.getExtensionReceiver();
return result instanceof ExpressionReceiver ? (ExpressionReceiver) result : null;
}
@Override
protected JsExpression asArrayAccess(
@NotNull JsExpression receiver,
@NotNull List<JsExpression> arguments,
@NotNull TranslationContext context
) {
return ArrayFIF.SET_INTRINSIC.apply(receiver, arguments, context);
}
};
private static final JsExpression getReferenceToOnlyTypeParameter(
@NotNull CallInfo callInfo, @NotNull TranslationContext context
......@@ -218,9 +171,6 @@ public final class TopLevelFIF extends CompositeFIF {
add(pattern("kotlin", "arrayOfNulls"), new KotlinFunctionIntrinsic("nullArray"));
add(pattern("kotlin", "iterator").isExtensionOf(FQ_NAMES.iterator.asString()), RETURN_RECEIVER_INTRINSIC);
add(pattern("kotlin.collections", "Map", "get").checkOverridden(), NATIVE_MAP_GET);
add(pattern("kotlin.js", "set").isExtensionOf(FQ_NAMES.mutableMap.asString()), NATIVE_MAP_SET);
add(pattern("kotlin.js", "Json", "get"), ArrayFIF.GET_INTRINSIC);
add(pattern("kotlin.js", "Json", "set"), ArrayFIF.SET_INTRINSIC);
......@@ -230,51 +180,4 @@ public final class TopLevelFIF extends CompositeFIF {
add(pattern("kotlin", "enumValueOf"), ENUM_VALUE_OF_INTRINSIC);
}
private abstract static class NativeMapGetSet extends CallParametersAwareFunctionIntrinsic {
@NotNull
protected abstract String operationName();
@Nullable
protected abstract ExpressionReceiver getExpressionReceiver(@NotNull ResolvedCall<?> resolvedCall);
protected abstract JsExpression asArrayAccess(
@NotNull JsExpression receiver,
@NotNull List<JsExpression> arguments,
@NotNull TranslationContext context
);
@NotNull
@Override
public JsExpression apply(@NotNull CallInfo callInfo, @NotNull List<JsExpression> arguments, @NotNull TranslationContext context) {
ExpressionReceiver expressionReceiver = getExpressionReceiver(callInfo.getResolvedCall());
JsExpression thisOrReceiver = getThisOrReceiverOrNull(callInfo);
assert thisOrReceiver != null;
if (expressionReceiver != null) {
KtExpression expression = expressionReceiver.getExpression();
KtReferenceExpression referenceExpression = null;
if (expression instanceof KtReferenceExpression) {
referenceExpression = (KtReferenceExpression) expression;
}
else if (expression instanceof KtQualifiedExpression) {
KtExpression candidate = ((KtQualifiedExpression) expression).getReceiverExpression();
if (candidate instanceof KtReferenceExpression) {
referenceExpression = (KtReferenceExpression) candidate;
}
}
if (referenceExpression != null) {
DeclarationDescriptor candidate = BindingUtils.getDescriptorForReferenceExpression(context.bindingContext(),
referenceExpression);
if (candidate instanceof PropertyDescriptor && AnnotationsUtils.isNativeObject(candidate)) {
return asArrayAccess(thisOrReceiver, arguments, context);
}
}
}
String mangledName = getStableMangledNameForDescriptor(JsPlatform.INSTANCE.getBuiltIns().getMutableMap(), operationName());
return new JsInvocation(JsAstUtils.pureFqn(mangledName, thisOrReceiver), arguments);
}
}
}
var classes = {"answer": 42}, classesMutable = {};
package foo
external val classes: Map<String, Any> = noImpl
external val classesMutable: HashMap<String, String> = noImpl
fun box(): String {
classesMutable.set("why", "?")
return if (classes.get("answer") == 42 && classesMutable.get("why") == "?") "OK" else "fail"
}
\ No newline at end of file
......@@ -130,6 +130,14 @@ public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map<out K, V>.
public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map<out K, V>.get(key: K): V?
= @Suppress("UNCHECKED_CAST") (this as Map<K, V>).get(key)
/**
* Allows to use the index operator for storing values in a mutable map.
*/
@kotlin.internal.InlineOnly
public inline operator fun <K, V> MutableMap<K, V>.set(key: K, value: V): Unit {
put(key, value)
}
/**
* Returns `true` if the map contains the specified [key].
*
......
......@@ -11,14 +11,7 @@ import java.util.SortedMap
import java.util.TreeMap
import java.util.concurrent.ConcurrentMap
/**
* Allows to use the index operator for storing values in a mutable map.
*/
// this code is JVM-specific, because JS has native set function
@kotlin.internal.InlineOnly
public inline operator fun <K, V> MutableMap<K, V>.set(key: K, value: V): Unit {
put(key, value)
}
/**
* Concurrent getOrPut, that is safe for concurrent maps.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册