added array intrinsic declaration and reannotated intrinsic receiver to be Nullable instead of NotNull
This commit is contained in:
Generated
+665
-204
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,7 @@ package org.jetbrains.k2js.translate.intrinsic;
|
||||
|
||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
|
||||
import java.util.List;
|
||||
@@ -12,6 +13,6 @@ import java.util.List;
|
||||
public interface Intrinsic {
|
||||
|
||||
@NotNull
|
||||
JsExpression apply(@NotNull JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
@NotNull TranslationContext context);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.types.JetStandardClasses;
|
||||
import org.jetbrains.jet.lang.types.JetStandardLibrary;
|
||||
import org.jetbrains.jet.lexer.JetToken;
|
||||
import org.jetbrains.k2js.translate.intrinsic.array.ArrayGetIntrinsic;
|
||||
import org.jetbrains.k2js.translate.intrinsic.array.ArrayNullConstructorIntrinsic;
|
||||
import org.jetbrains.k2js.translate.intrinsic.array.ArraySetIntrinsic;
|
||||
import org.jetbrains.k2js.translate.intrinsic.primitive.*;
|
||||
import org.jetbrains.k2js.translate.intrinsic.string.LengthIntrinsic;
|
||||
import org.jetbrains.k2js.translate.intrinsic.tuple.TupleAccessIntrinsic;
|
||||
@@ -17,6 +20,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.*;
|
||||
import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getFunctionByName;
|
||||
import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getPropertyByName;
|
||||
|
||||
/**
|
||||
@@ -48,6 +52,17 @@ public final class Intrinsics {
|
||||
declareOperatorIntrinsics();
|
||||
declareStringIntrinsics();
|
||||
declareTuplesIntrinsics();
|
||||
declareArrayIntrinsics();
|
||||
}
|
||||
|
||||
private void declareArrayIntrinsics() {
|
||||
JetScope arrayMemberScope = library.getArray().getDefaultType().getMemberScope();
|
||||
FunctionDescriptor setFunction = getFunctionByName(arrayMemberScope, "set");
|
||||
functionIntrinsics.put(setFunction, ArraySetIntrinsic.INSTANCE);
|
||||
FunctionDescriptor getFunction = getFunctionByName(arrayMemberScope, "get");
|
||||
functionIntrinsics.put(getFunction, ArrayGetIntrinsic.INSTANCE);
|
||||
FunctionDescriptor nullArrayConstructor = getFunctionByName(library.getLibraryScope(), "Array");
|
||||
functionIntrinsics.put(nullArrayConstructor, ArrayNullConstructorIntrinsic.INSTANCE);
|
||||
}
|
||||
|
||||
private void declareTuplesIntrinsics() {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package org.jetbrains.k2js.translate.intrinsic.array;
|
||||
|
||||
import com.google.dart.compiler.backend.js.ast.JsConditional;
|
||||
import com.google.dart.compiler.backend.js.ast.JsArrayAccess;
|
||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
import com.google.dart.compiler.util.AstUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.k2js.translate.context.TemporaryVariable;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic;
|
||||
|
||||
@@ -19,13 +18,15 @@ public enum ArrayGetIntrinsic implements FunctionIntrinsic {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsExpression apply(@NotNull JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
@NotNull TranslationContext context) {
|
||||
assert receiver != null;
|
||||
assert arguments.size() == 1 : "Array get expression must have one argument.";
|
||||
JsExpression indexExpression = arguments.get(0);
|
||||
TemporaryVariable arrayExpression = context.declareTemporary(receiver);
|
||||
JsConditional indexInBoundsCheck =
|
||||
IntrinsicArrayUtils.indexInBoundsCheck(indexExpression, arrayExpression, context);
|
||||
return AstUtil.newSequence(arrayExpression.assignmentExpression(), AstUtil.newArrayAccess(receiver, indexExpression));
|
||||
// TemporaryVariable arrayExpression = context.declareTemporary(receiver);
|
||||
// JsConditional indexInBoundsCheck =
|
||||
// IntrinsicArrayUtils.indexInBoundsCheck(indexExpression, arrayExpression, context);
|
||||
// return AstUtil.newSequence(arrayExpression.assignmentExpression(), AstUtil.newArrayAccess(receiver, indexExpression));
|
||||
return new JsArrayAccess(receiver, indexExpression);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@ package org.jetbrains.k2js.translate.intrinsic.array;
|
||||
import com.google.dart.compiler.backend.js.ast.JsArrayLiteral;
|
||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic;
|
||||
|
||||
@@ -18,7 +19,7 @@ public enum ArrayNullConstructorIntrinsic implements FunctionIntrinsic {
|
||||
//TODO: implement function passing to array constructor
|
||||
@NotNull
|
||||
@Override
|
||||
public JsExpression apply(@NotNull JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
@NotNull TranslationContext context) {
|
||||
return new JsArrayLiteral();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.google.dart.compiler.backend.js.ast.JsArrayAccess;
|
||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
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;
|
||||
|
||||
@@ -18,8 +19,9 @@ public enum ArraySetIntrinsic implements FunctionIntrinsic {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsExpression apply(@NotNull JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
@NotNull TranslationContext context) {
|
||||
assert receiver != null;
|
||||
assert arguments.size() == 2 : "Array set expression must have two arguments.";
|
||||
JsExpression indexExpression = arguments.get(0);
|
||||
JsExpression value = arguments.get(1);
|
||||
|
||||
@@ -8,6 +8,8 @@ import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
|
||||
import static org.jetbrains.k2js.translate.utils.TranslationUtils.zeroLiteral;
|
||||
|
||||
//TODO: decide if needed
|
||||
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
|
||||
+2
-1
@@ -4,6 +4,7 @@ import com.google.dart.compiler.backend.js.ast.JsBinaryOperation;
|
||||
import com.google.dart.compiler.backend.js.ast.JsBinaryOperator;
|
||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lexer.JetToken;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic;
|
||||
@@ -31,7 +32,7 @@ public final class PrimitiveBinaryOperationIntrinsic implements FunctionIntrinsi
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsExpression apply(@NotNull JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
@NotNull TranslationContext context) {
|
||||
assert arguments.size() == 1 : "Binary operator should have a receiver and one argument";
|
||||
return new JsBinaryOperation(operator, receiver, arguments.get(0));
|
||||
|
||||
+2
-1
@@ -4,6 +4,7 @@ import com.google.dart.compiler.backend.js.ast.JsBinaryOperation;
|
||||
import com.google.dart.compiler.backend.js.ast.JsBinaryOperator;
|
||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
import org.jetbrains.k2js.translate.intrinsic.CompareToIntrinsic;
|
||||
import org.jetbrains.k2js.translate.operation.OperatorTable;
|
||||
@@ -24,7 +25,7 @@ public final class PrimitiveCompareToIntrinsic extends CompareToIntrinsic {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsExpression apply(@NotNull JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
@NotNull TranslationContext context) {
|
||||
assert arguments.size() == 1 : "Equals operation should have one argument";
|
||||
JsBinaryOperator operator = OperatorTable.getBinaryOperator(getComparisonToken());
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@ package org.jetbrains.k2js.translate.intrinsic.primitive;
|
||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
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.EqualsIntrinsic;
|
||||
|
||||
@@ -22,7 +23,7 @@ public final class PrimitiveEqualsIntrinsic extends EqualsIntrinsic {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsExpression apply(@NotNull JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
@NotNull TranslationContext context) {
|
||||
assert arguments.size() == 1 : "Equals operation should have one argument";
|
||||
if (isNegated()) {
|
||||
|
||||
+2
-1
@@ -6,6 +6,7 @@ import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
import com.google.dart.compiler.backend.js.ast.JsNew;
|
||||
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;
|
||||
|
||||
@@ -27,7 +28,7 @@ public final class PrimitiveRangeToIntrinsic implements FunctionIntrinsic {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsExpression apply(@NotNull JsExpression rangeStart, @NotNull List<JsExpression> arguments,
|
||||
public JsExpression apply(@Nullable JsExpression rangeStart, @NotNull List<JsExpression> arguments,
|
||||
@NotNull TranslationContext context) {
|
||||
assert arguments.size() == 1 : "RangeTo must have one argument.";
|
||||
JsExpression rangeEnd = arguments.get(0);
|
||||
|
||||
+3
-1
@@ -4,6 +4,7 @@ import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
import com.google.dart.compiler.backend.js.ast.JsPrefixOperation;
|
||||
import com.google.dart.compiler.backend.js.ast.JsUnaryOperator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lexer.JetToken;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic;
|
||||
@@ -31,8 +32,9 @@ public final class PrimitiveUnaryOperationIntrinsic implements FunctionIntrinsic
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsExpression apply(@NotNull JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
@NotNull TranslationContext context) {
|
||||
assert receiver != null;
|
||||
assert arguments.size() == 0 : "Unary operator should not have arguments.";
|
||||
//NOTE: cannot use this for increment/decrement
|
||||
return new JsPrefixOperation(operator, receiver);
|
||||
|
||||
@@ -5,6 +5,7 @@ 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;
|
||||
|
||||
@@ -19,8 +20,9 @@ public enum LengthIntrinsic implements FunctionIntrinsic {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsExpression apply(@NotNull JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
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");
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@ package org.jetbrains.k2js.translate.intrinsic.tuple;
|
||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
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;
|
||||
|
||||
@@ -21,7 +22,7 @@ public final class TupleAccessIntrinsic implements FunctionIntrinsic {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsExpression apply(@NotNull JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments,
|
||||
@NotNull TranslationContext context) {
|
||||
assert arguments.isEmpty() : "Tuple access expression should not have any arguments.";
|
||||
return AstUtil.newArrayAccess(receiver, context.program().getNumberLiteral(elementIndex));
|
||||
|
||||
@@ -296,7 +296,6 @@ public final class CallTranslator extends AbstractTranslator {
|
||||
FunctionIntrinsic functionIntrinsic =
|
||||
context().intrinsics().getFunctionIntrinsic((FunctionDescriptor) descriptor);
|
||||
JsExpression receiverExpression = thisObject();
|
||||
assert receiverExpression != null : "Intrinsic function should have a receiver.";
|
||||
return functionIntrinsic.apply(receiverExpression, arguments, context());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user