Further remove duplication in intrinsics.
This commit is contained in:
+5
-2
@@ -25,7 +25,7 @@ import org.jetbrains.jet.lang.types.JetType;
|
|||||||
import org.jetbrains.k2js.translate.context.TemporaryVariable;
|
import org.jetbrains.k2js.translate.context.TemporaryVariable;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
import org.jetbrains.k2js.translate.general.Translation;
|
import org.jetbrains.k2js.translate.general.Translation;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.string.LengthIntrinsic;
|
import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic;
|
||||||
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -70,7 +70,10 @@ public final class ArrayForTranslator extends ForTranslator {
|
|||||||
private ArrayForTranslator(@NotNull JetForExpression forExpression, @NotNull TranslationContext context) {
|
private ArrayForTranslator(@NotNull JetForExpression forExpression, @NotNull TranslationContext context) {
|
||||||
super(forExpression, context);
|
super(forExpression, context);
|
||||||
loopRange = context.declareTemporary(Translation.translateAsExpression(getLoopRange(expression), context));
|
loopRange = context.declareTemporary(Translation.translateAsExpression(getLoopRange(expression), context));
|
||||||
JsExpression length = LengthIntrinsic.INSTANCE.apply(loopRange.reference(), Collections.<JsExpression>emptyList(), context());
|
FunctionIntrinsic lengthPropertyIntrinsic = context().intrinsics().getLengthPropertyIntrinsic();
|
||||||
|
JsExpression length = lengthPropertyIntrinsic.apply(loopRange.reference(),
|
||||||
|
Collections.<JsExpression>emptyList(),
|
||||||
|
context());
|
||||||
end = context().declareTemporary(length);
|
end = context().declareTemporary(length);
|
||||||
index = context().declareTemporary(program().getNumberLiteral(0));
|
index = context().declareTemporary(program().getNumberLiteral(0));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,11 +28,10 @@ import org.jetbrains.jet.lang.types.PrimitiveType;
|
|||||||
import org.jetbrains.jet.lexer.JetToken;
|
import org.jetbrains.jet.lexer.JetToken;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.array.ArrayGetIntrinsic;
|
import org.jetbrains.k2js.translate.intrinsic.array.ArrayGetIntrinsic;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.array.ArraySetIntrinsic;
|
import org.jetbrains.k2js.translate.intrinsic.array.ArraySetIntrinsic;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.array.ArraySizeIntrinsic;
|
import org.jetbrains.k2js.translate.intrinsic.array.BuiltInPropertyIntrinsic;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.array.CallStandardMethodIntrinsic;
|
import org.jetbrains.k2js.translate.intrinsic.array.CallStandardMethodIntrinsic;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.primitive.*;
|
import org.jetbrains.k2js.translate.intrinsic.primitive.*;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.string.CharAtIntrinsic;
|
import org.jetbrains.k2js.translate.intrinsic.string.CharAtIntrinsic;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.string.LengthIntrinsic;
|
|
||||||
import org.jetbrains.k2js.translate.intrinsic.tuple.TupleAccessIntrinsic;
|
import org.jetbrains.k2js.translate.intrinsic.tuple.TupleAccessIntrinsic;
|
||||||
import org.jetbrains.k2js.translate.operation.OperatorTable;
|
import org.jetbrains.k2js.translate.operation.OperatorTable;
|
||||||
import org.jetbrains.k2js.translate.utils.DescriptorUtils;
|
import org.jetbrains.k2js.translate.utils.DescriptorUtils;
|
||||||
@@ -64,6 +63,9 @@ public final class Intrinsics {
|
|||||||
private final Map<FunctionDescriptor, CompareToIntrinsic> compareToIntrinsics =
|
private final Map<FunctionDescriptor, CompareToIntrinsic> compareToIntrinsics =
|
||||||
new HashMap<FunctionDescriptor, CompareToIntrinsic>();
|
new HashMap<FunctionDescriptor, CompareToIntrinsic>();
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private final FunctionIntrinsic lengthPropertyIntrinsic = new BuiltInPropertyIntrinsic("length");
|
||||||
|
|
||||||
public static Intrinsics standardLibraryIntrinsics(@NotNull JetStandardLibrary library) {
|
public static Intrinsics standardLibraryIntrinsics(@NotNull JetStandardLibrary library) {
|
||||||
return new Intrinsics(library);
|
return new Intrinsics(library);
|
||||||
}
|
}
|
||||||
@@ -79,6 +81,11 @@ public final class Intrinsics {
|
|||||||
declareArrayIntrinsics();
|
declareArrayIntrinsics();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public FunctionIntrinsic getLengthPropertyIntrinsic() {
|
||||||
|
return lengthPropertyIntrinsic;
|
||||||
|
}
|
||||||
|
|
||||||
private void declareTuplesIntrinsics() {
|
private void declareTuplesIntrinsics() {
|
||||||
for (int tupleSize = 0; tupleSize < JetStandardClasses.TUPLE_COUNT; ++tupleSize) {
|
for (int tupleSize = 0; tupleSize < JetStandardClasses.TUPLE_COUNT; ++tupleSize) {
|
||||||
declareTupleIntrinsics(tupleSize);
|
declareTupleIntrinsics(tupleSize);
|
||||||
@@ -109,7 +116,8 @@ public final class Intrinsics {
|
|||||||
FunctionDescriptor getFunction = getFunctionByName(arrayMemberScope, "get");
|
FunctionDescriptor getFunction = getFunctionByName(arrayMemberScope, "get");
|
||||||
functionIntrinsics.put(getFunction, ArrayGetIntrinsic.INSTANCE);
|
functionIntrinsics.put(getFunction, ArrayGetIntrinsic.INSTANCE);
|
||||||
PropertyDescriptor sizeProperty = getPropertyByName(arrayMemberScope, "size");
|
PropertyDescriptor sizeProperty = getPropertyByName(arrayMemberScope, "size");
|
||||||
functionIntrinsics.put(sizeProperty.getGetter(), ArraySizeIntrinsic.INSTANCE);
|
functionIntrinsics.put(sizeProperty.getGetter(), lengthPropertyIntrinsic);
|
||||||
|
//TODO: excessive object creation
|
||||||
PropertyDescriptor indicesProperty = getPropertyByName(arrayMemberScope, "indices");
|
PropertyDescriptor indicesProperty = getPropertyByName(arrayMemberScope, "indices");
|
||||||
functionIntrinsics.put(indicesProperty.getGetter(), new CallStandardMethodIntrinsic("Kotlin.arrayIndices", true, 0));
|
functionIntrinsics.put(indicesProperty.getGetter(), new CallStandardMethodIntrinsic("Kotlin.arrayIndices", true, 0));
|
||||||
FunctionDescriptor iteratorFunction = getFunctionByName(arrayMemberScope, "iterator");
|
FunctionDescriptor iteratorFunction = getFunctionByName(arrayMemberScope, "iterator");
|
||||||
@@ -139,7 +147,7 @@ public final class Intrinsics {
|
|||||||
private void declareStringIntrinsics() {
|
private void declareStringIntrinsics() {
|
||||||
PropertyDescriptor lengthProperty =
|
PropertyDescriptor lengthProperty =
|
||||||
getPropertyByName(library.getCharSequence().getDefaultType().getMemberScope(), "length");
|
getPropertyByName(library.getCharSequence().getDefaultType().getMemberScope(), "length");
|
||||||
functionIntrinsics.put(lengthProperty.getGetter(), LengthIntrinsic.INSTANCE);
|
functionIntrinsics.put(lengthProperty.getGetter(), new BuiltInPropertyIntrinsic("length"));
|
||||||
FunctionDescriptor getFunction =
|
FunctionDescriptor getFunction =
|
||||||
getFunctionByName(library.getString().getDefaultType().getMemberScope(), "get");
|
getFunctionByName(library.getString().getDefaultType().getMemberScope(), "get");
|
||||||
functionIntrinsics.put(getFunction, CharAtIntrinsic.INSTANCE);
|
functionIntrinsics.put(getFunction, CharAtIntrinsic.INSTANCE);
|
||||||
|
|||||||
+8
-20
@@ -1,19 +1,3 @@
|
|||||||
/*
|
|
||||||
* Copyright 2000-2012 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.k2js.translate.intrinsic.array;
|
package org.jetbrains.k2js.translate.intrinsic.array;
|
||||||
|
|
||||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||||
@@ -31,10 +15,14 @@ import static org.jetbrains.k2js.translate.utils.JsAstUtils.setQualifier;
|
|||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
public enum ArraySizeIntrinsic implements FunctionIntrinsic {
|
public final class BuiltInPropertyIntrinsic implements FunctionIntrinsic {
|
||||||
|
|
||||||
INSTANCE;
|
@NotNull
|
||||||
|
private final String propertyName;
|
||||||
|
|
||||||
|
public BuiltInPropertyIntrinsic(@NotNull String propertyName) {
|
||||||
|
this.propertyName = propertyName;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
@@ -43,8 +31,8 @@ public enum ArraySizeIntrinsic implements FunctionIntrinsic {
|
|||||||
assert receiver != null;
|
assert receiver != null;
|
||||||
assert arguments.isEmpty() : "Length expression must have zero arguments.";
|
assert arguments.isEmpty() : "Length expression must have zero arguments.";
|
||||||
//TODO: provide better way
|
//TODO: provide better way
|
||||||
JsNameRef lengthProperty = AstUtil.newQualifiedNameRef("length");
|
JsNameRef lengthProperty = AstUtil.newQualifiedNameRef(propertyName);
|
||||||
setQualifier(lengthProperty, receiver);
|
setQualifier(lengthProperty, receiver);
|
||||||
return lengthProperty;
|
return lengthProperty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
-50
@@ -1,50 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2000-2012 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.k2js.translate.intrinsic.string;
|
|
||||||
|
|
||||||
|
|
||||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
|
||||||
import com.google.dart.compiler.backend.js.ast.JsNameRef;
|
|
||||||
import com.google.dart.compiler.util.AstUtil;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
|
||||||
import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.jetbrains.k2js.translate.utils.JsAstUtils.setQualifier;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Pavel Talanov
|
|
||||||
*/
|
|
||||||
public enum LengthIntrinsic implements FunctionIntrinsic {
|
|
||||||
|
|
||||||
INSTANCE;
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
|
|
||||||
@NotNull TranslationContext context) {
|
|
||||||
assert receiver != null;
|
|
||||||
assert arguments.isEmpty() : "Length expression must have zero arguments.";
|
|
||||||
//TODO: provide better way
|
|
||||||
JsNameRef lengthProperty = AstUtil.newQualifiedNameRef("length");
|
|
||||||
setQualifier(lengthProperty, receiver);
|
|
||||||
return lengthProperty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user