Skip to content

Commit

Permalink
More standard coercion of primitive values. Don't touch TruffleObjects.
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Sep 8, 2022
1 parent ade1383 commit ef71c20
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.enso.interpreter.epb.node;

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.GenerateUncached;
import com.oracle.truffle.api.dsl.ReportPolymorphism;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;

@GenerateUncached
Expand All @@ -32,45 +29,53 @@ public static CoercePrimitiveNode build() {
*/
public abstract Object execute(Object value);

@Specialization(guards = "bools.isBoolean(bool)")
boolean doBoolean(Object bool, @CachedLibrary(limit = "5") InteropLibrary bools) {
try {
return bools.asBoolean(bool);
} catch (UnsupportedMessageException e) {
throw new IllegalStateException("Impossible, `bool` already checked to be a boolean");
}
@Specialization
boolean doBoolean(boolean bool) {
return bool;
}

@Specialization
long doByte(byte n) {
return n;
}

@Specialization
long doShort(short n) {
return n;
}

@Specialization(guards = {"numbers.isNumber(integer)", "numbers.fitsInLong(integer)"})
long doInteger(Object integer, @CachedLibrary(limit = "5") InteropLibrary numbers) {
try {
return numbers.asLong(integer);
} catch (UnsupportedMessageException e) {
throw new IllegalStateException("Impossible, `integer` is checked to be a long");
}
@Specialization
long doInt(int n) {
return n;
}

@Specialization(
guards = {
"numbers.isNumber(decimal)",
"!numbers.fitsInLong(decimal)",
"numbers.fitsInDouble(decimal)"
})
double doDecimal(Object decimal, @CachedLibrary(limit = "5") InteropLibrary numbers) {
try {
return numbers.asDouble(decimal);
} catch (UnsupportedMessageException e) {
throw new IllegalStateException("Impossible, `decimal` is checked to be a long");
}
@Specialization
long doLong(long n) {
return n;
}

@Specialization
double doFloat(float n) {
return n;
}

@Specialization
double doDouble(double n) {
return n;
}

@Specialization
long doChar(char character) {
return character;
}

@Fallback
Object doNonPrimitive(Object value) {
@Specialization
String doString(String s) {
return s;
}

@Specialization
Object doNonPrimitive(TruffleObject value) {
return value;
}
}
2 changes: 1 addition & 1 deletion test/Visualization_Tests/src/Table_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ visualization_spec connection =

g = t.aggregate [Group_By "A", Group_By "B", Average "C"] . at "Average C"
vis2 = Visualization.prepare_visualization g 1
json2 = make_json header=["Average C"] data=[[4]] all_rows=2 ixes_header=[] ixes=[]
json2 = make_json header=["Average C"] data=[[4.0]] all_rows=2 ixes_header=[] ixes=[]
vis2 . should_equal json2

t2 = Dataframe_Table.new [["A", [1, 2, 3]], ["B", [4, 5, 6]], ["C", [7, 8, 9]]]
Expand Down

0 comments on commit ef71c20

Please sign in to comment.