From 7f6664c0ccb50b52f92b9a0a59ad63d54534aa69 Mon Sep 17 00:00:00 2001 From: Pavel Marek Date: Tue, 4 Jul 2023 18:27:47 +0200 Subject: [PATCH] EnsoBigInteger is an interop bigInteger type --- .../builtin/meta/IsValueOfTypeNode.java | 8 ++-- .../expression/builtin/meta/TypeOfNode.java | 4 +- .../number/utils/ToEnsoNumberNode.java | 7 ++- .../node/expression/literal/LiteralNode.java | 3 +- .../runtime/number/EnsoBigInteger.java | 47 +++++-------------- .../enso/interpreter/test/BigNumberTest.java | 6 --- 6 files changed, 25 insertions(+), 50 deletions(-) diff --git a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/IsValueOfTypeNode.java b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/IsValueOfTypeNode.java index 8a9cc4c401cb8..1e3b677a1a7d2 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/IsValueOfTypeNode.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/IsValueOfTypeNode.java @@ -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. */ @@ -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); } diff --git a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/TypeOfNode.java b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/TypeOfNode.java index f75cedc83b7e6..f9a5cb4456b09 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/TypeOfNode.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/TypeOfNode.java @@ -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(); diff --git a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/number/utils/ToEnsoNumberNode.java b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/number/utils/ToEnsoNumberNode.java index 515af3d4be45e..ceb7fa070008b 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/number/utils/ToEnsoNumberNode.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/number/utils/ToEnsoNumberNode.java @@ -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() { @@ -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 diff --git a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/literal/LiteralNode.java b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/literal/LiteralNode.java index 228b2436301e3..014f0ce8eb120 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/literal/LiteralNode.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/literal/LiteralNode.java @@ -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. */ @@ -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); } /** diff --git a/engine/runtime/src/main/java/org/enso/interpreter/runtime/number/EnsoBigInteger.java b/engine/runtime/src/main/java/org/enso/interpreter/runtime/number/EnsoBigInteger.java index 0c061f742672d..e9ddf83711a92 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/runtime/number/EnsoBigInteger.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/runtime/number/EnsoBigInteger.java @@ -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; @@ -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(); diff --git a/engine/runtime/src/test/java/org/enso/interpreter/test/BigNumberTest.java b/engine/runtime/src/test/java/org/enso/interpreter/test/BigNumberTest.java index 668b6c5acca1c..0563f947fecd1 100644 --- a/engine/runtime/src/test/java/org/enso/interpreter/test/BigNumberTest.java +++ b/engine/runtime/src/test/java/org/enso/interpreter/test/BigNumberTest.java @@ -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