JS backend: make MutableMap.set as function intrinsic.

Fixed test WebDemoExamples2Test#builder.
This commit is contained in:
develar
2013-08-06 17:24:48 +04:00
committed by Zalim Bashorov
parent 63a72b1095
commit f1c258f601
5 changed files with 55 additions and 31 deletions
+4
View File
@@ -2,6 +2,10 @@ package java.util
import java.lang.*
/** Provides [] access to maps */
native
public fun <K, V> MutableMap<K, V>.set(key: K, value: V): Unit = noImpl
library("collectionsMax")
public fun max<T>(col : Collection<T>, comp : Comparator<T>) : T = js.noImpl
-5
View File
@@ -2,11 +2,6 @@ package kotlin
import java.util.HashMap
/** Provides [] access to maps */
public fun <K, V> MutableMap<K, V>.set(key : K, value : V): Unit {
this.put(key, value)
}
/**
* Returns a new [[HashMap]] populated with the given pairs where the first value in each pair
* is the key and the second value is the value
@@ -33,9 +33,7 @@ public final class WebDemoExamples2Test extends SingleFileTranslationTest {
performTestWithMain("life", "", "2");
}
//TODO: fails because it need code from stdlib
@SuppressWarnings("UnusedDeclaration")
public void _testBuilder() throws Exception {
public void testBuilder() throws Exception {
performTestWithMain("builder", "");
performTestWithMain("builder", "1", "over9000");
}
@@ -17,6 +17,7 @@
package org.jetbrains.k2js.translate.intrinsic.functions.factories;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsInvocation;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -25,6 +26,7 @@ import org.jetbrains.k2js.translate.intrinsic.functions.basic.BuiltInFunctionInt
import org.jetbrains.k2js.translate.intrinsic.functions.basic.CallStandardMethodIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder;
import java.util.List;
@@ -56,5 +58,15 @@ public final class TopLevelFIF extends CompositeFIF {
add(pattern("String|Boolean|Char|Number.equals"), EQUALS);
add(pattern("arrayOfNulls"), new CallStandardMethodIntrinsic(new JsNameRef("nullArray", "Kotlin"), false, 1));
add(pattern("iterator"), RETURN_RECEIVER_INTRINSIC);
add(PatternBuilder.create("java", "util", "set").receiverParameterExists(true), new FunctionIntrinsic() {
@NotNull
@Override
public JsExpression apply(
@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments, @NotNull TranslationContext context
) {
return new JsInvocation(new JsNameRef("put", receiver), arguments);
}
});
}
}
@@ -123,32 +123,47 @@ public final class PatternBuilder {
}
@NotNull
public static DescriptorPredicate create(@NotNull final String... names) {
return new DescriptorPredicate() {
@Override
public boolean apply(@Nullable FunctionDescriptor functionDescriptor) {
if (functionDescriptor == null) {
return false;
}
DeclarationDescriptor descriptor = functionDescriptor;
int nameIndex = names.length - 1;
do {
if (!descriptor.getName().asString().equals(names[nameIndex--])) {
return false;
}
descriptor = descriptor.getContainingDeclaration();
if (nameIndex == -1) {
return isRootNamespace(descriptor);
}
}
while (descriptor != null && !isRootNamespace(descriptor));
return false;
}
};
public static DescriptorPredicateImpl create(@NotNull String... names) {
return new DescriptorPredicateImpl(names);
}
private static boolean isRootNamespace(DeclarationDescriptor declarationDescriptor) {
return declarationDescriptor instanceof NamespaceDescriptor && DescriptorUtils.isRootNamespace((NamespaceDescriptor) declarationDescriptor);
}
public static class DescriptorPredicateImpl implements DescriptorPredicate {
private final String[] names;
private boolean receiverParameterExists;
public DescriptorPredicateImpl(String... names) {
this.names = names;
}
public DescriptorPredicateImpl receiverParameterExists(boolean receiverParameterExists) {
this.receiverParameterExists = receiverParameterExists;
return this;
}
@Override
public boolean apply(@Nullable FunctionDescriptor functionDescriptor) {
if (functionDescriptor == null || (functionDescriptor.getReceiverParameter() == null) == receiverParameterExists) {
return false;
}
DeclarationDescriptor descriptor = functionDescriptor;
int nameIndex = names.length - 1;
do {
if (!descriptor.getName().asString().equals(names[nameIndex--])) {
return false;
}
descriptor = descriptor.getContainingDeclaration();
if (nameIndex == -1) {
return isRootNamespace(descriptor);
}
}
while (descriptor != null && !isRootNamespace(descriptor));
return false;
}
}
}