JS: support integer overflow semantics for unary minus operator
See KT-19290
This commit is contained in:
+18
@@ -61,6 +61,7 @@ public enum PrimitiveUnaryOperationFIF implements FunctionIntrinsicFactory {
|
|||||||
private static final DescriptorPredicate DEC_OPERATION_FOR_BYTE = pattern("Byte.dec");
|
private static final DescriptorPredicate DEC_OPERATION_FOR_BYTE = pattern("Byte.dec");
|
||||||
private static final DescriptorPredicate INC_OPERATION_FOR_SHORT = pattern("Short.inc");
|
private static final DescriptorPredicate INC_OPERATION_FOR_SHORT = pattern("Short.inc");
|
||||||
private static final DescriptorPredicate DEC_OPERATION_FOR_SHORT = pattern("Short.dec");
|
private static final DescriptorPredicate DEC_OPERATION_FOR_SHORT = pattern("Short.dec");
|
||||||
|
private static final DescriptorPredicate NEG_OPERATION_FOR_INT = pattern("Int.unaryMinus");
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static final DescriptorPredicate INC_OPERATION_FOR_PRIMITIVE_NUMBER = pattern("Float|Double.inc()");
|
private static final DescriptorPredicate INC_OPERATION_FOR_PRIMITIVE_NUMBER = pattern("Float|Double.inc()");
|
||||||
@@ -152,6 +153,20 @@ public enum PrimitiveUnaryOperationFIF implements FunctionIntrinsicFactory {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static final FunctionIntrinsicWithReceiverComputed NUMBER_NEG_INTRINSIC = new FunctionIntrinsicWithReceiverComputed() {
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public JsExpression apply(
|
||||||
|
@Nullable JsExpression receiver,
|
||||||
|
@NotNull List<? extends JsExpression> arguments,
|
||||||
|
@NotNull TranslationContext context
|
||||||
|
) {
|
||||||
|
assert receiver != null;
|
||||||
|
assert arguments.size() == 0;
|
||||||
|
return new JsPrefixOperation(JsUnaryOperator.NEG, receiver);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private static abstract class UnaryOperationInstrinsicBase extends FunctionIntrinsicWithReceiverComputed {
|
private static abstract class UnaryOperationInstrinsicBase extends FunctionIntrinsicWithReceiverComputed {
|
||||||
@NotNull
|
@NotNull
|
||||||
public abstract JsExpression doApply(@NotNull JsExpression receiver, @NotNull TranslationContext context);
|
public abstract JsExpression doApply(@NotNull JsExpression receiver, @NotNull TranslationContext context);
|
||||||
@@ -265,6 +280,9 @@ public enum PrimitiveUnaryOperationFIF implements FunctionIntrinsicFactory {
|
|||||||
return NUMBER_DEC_INTRINSIC;
|
return NUMBER_DEC_INTRINSIC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (NEG_OPERATION_FOR_INT.test(descriptor)) {
|
||||||
|
return new IntOverflowIntrinsic(NUMBER_NEG_INTRINSIC);
|
||||||
|
}
|
||||||
|
|
||||||
Name name = descriptor.getName();
|
Name name = descriptor.getName();
|
||||||
|
|
||||||
|
|||||||
+6
-1
@@ -26,5 +26,10 @@ fun box(): String {
|
|||||||
v = bigValue() * bigValue()
|
v = bigValue() * bigValue()
|
||||||
if (v != 16) return "fail6: $v"
|
if (v != 16) return "fail6: $v"
|
||||||
|
|
||||||
|
v = -minInt()
|
||||||
|
if (v != Int.MIN_VALUE) return "fail7: $v"
|
||||||
|
|
||||||
return "OK"
|
return "OK"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun minInt() = Int.MIN_VALUE
|
||||||
Reference in New Issue
Block a user