Skip to content

Commit

Permalink
EnsoBigInteger is an interop bigInteger type
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Jul 4, 2023
1 parent 15244d2 commit 7f6664c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.enso.interpreter.runtime.callable.UnresolvedSymbol;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;
import org.enso.interpreter.runtime.number.EnsoBigInteger;
import org.enso.interpreter.runtime.type.TypesGen;

/** An implementation of the payload check against the expected panic type. */
Expand Down Expand Up @@ -94,8 +93,11 @@ boolean doDoubleCheck(Type expectedType, double payload) {
return checkParentTypes(numbers.getDecimal(), expectedType);
}

@Specialization
boolean doBigIntegerCheck(Type expectedType, EnsoBigInteger value) {
@Specialization(guards = "interop.fitsInBigInteger(value)")
boolean doBigIntegerCheck(
Type expectedType,
Object value,
@Shared("interop") @CachedLibrary(limit = "3") InteropLibrary interop) {
var numbers = EnsoContext.get(this).getBuiltins().number();
return checkParentTypes(numbers.getBigInteger(), expectedType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ Type doPolyglotNumber(
Object value,
@Shared("interop") @CachedLibrary(limit = "3") InteropLibrary interop) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
if (interop.fitsInInt(value)) {
if (interop.fitsInBigInteger(value)) {
return builtins.number().getBigInteger();
} else if (interop.fitsInInt(value)) {
return builtins.number().getInteger();
} else if (interop.fitsInDouble(value)) {
return builtins.number().getDecimal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
import com.oracle.truffle.api.dsl.ReportPolymorphism;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.profiles.CountingConditionProfile;
import java.math.BigInteger;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@ReportPolymorphism
@NodeInfo(description = "Takes a big integer and casts it to a long, if the operation is safe.")
public class ToEnsoNumberNode extends Node {
private final ConditionProfile fitsProfile = ConditionProfile.createCountingProfile();
private final CountingConditionProfile fitsProfile = CountingConditionProfile.create();

/** @return a new instance of this node. */
public static ToEnsoNumberNode build() {
Expand All @@ -29,7 +28,7 @@ public Object execute(BigInteger bigInteger) {
if (fitsProfile.profile(BigIntegerOps.fitsInLong(bigInteger))) {
return toLong(bigInteger);
}
return new EnsoBigInteger(bigInteger);
return bigInteger;
}

@CompilerDirectives.TruffleBoundary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.enso.compiler.core.IR;
import org.enso.interpreter.node.ExpressionNode;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.number.EnsoBigInteger;
import org.enso.interpreter.runtime.tag.Patchable;

/** Generic literal node. */
Expand Down Expand Up @@ -57,7 +56,7 @@ public static LiteralNode build(double value) {
* @return a node representing the literal given by {@code value}
*/
public static LiteralNode build(BigInteger value) {
return new LiteralNode(new EnsoBigInteger(value));
return new LiteralNode(value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
Expand Down Expand Up @@ -52,71 +51,51 @@ boolean isNumber() {
}

@ExportMessage
final boolean fitsInByte() {
return false;
}

@ExportMessage
final boolean fitsInShort() {
return false;
}

@ExportMessage
final boolean fitsInInt() {
return false;
}

@ExportMessage
final boolean fitsInLong() {
return false;
}

@ExportMessage
final boolean fitsInFloat() {
return false;
}

@ExportMessage
final boolean fitsInDouble() {
return false;
boolean fitsInBigInteger() {
return true;
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final byte asByte() throws UnsupportedMessageException {
byte asByte() {
return byteValue();
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final short asShort() throws UnsupportedMessageException {
short asShort() {
return shortValue();
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final int asInt() throws UnsupportedMessageException {
int asInt() {
return intValue();
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final long asLong() throws UnsupportedMessageException {
long asLong() {
return longValue();
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final float asFloat() throws UnsupportedMessageException {
float asFloat() {
return floatValue();
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
final double asDouble() throws UnsupportedMessageException {
double asDouble() {
return doubleValue();
}

@ExportMessage
BigInteger asBigInteger() {
return value;
}

@ExportMessage
Type getMetaObject(@CachedLibrary("this") InteropLibrary thisLib) {
return EnsoContext.get(thisLib).getBuiltins().number().getBigInteger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ private Value evalCode(final String code, final String methodName) throws URISyn

@Test
public void averageOfMixedArrayOverDouble() throws Exception {
boolean assertsOn = false;
assert assertsOn = true;
if (assertsOn) {
// skip this test when asserts are on
return;
}
var code = """
from Standard.Base.Data.Vector import Vector
polyglot java import org.enso.example.TestClass
Expand Down

0 comments on commit 7f6664c

Please sign in to comment.