Skip to content
Snippets Groups Projects
Commit 8f6da8c8 authored by Lukas Stadler's avatar Lukas Stadler
Browse files

unbox foreign objects when they enter the R value space

parent 19860151
No related branches found
No related tags found
No related merge requests found
...@@ -99,9 +99,6 @@ public final class PreinitialPhaseBuilder extends InitialPhaseBuilder<Object> { ...@@ -99,9 +99,6 @@ public final class PreinitialPhaseBuilder extends InitialPhaseBuilder<Object> {
/** /**
* Determines whether foreign arrays are implicitly casted to a R vector/list or not. <br> * Determines whether foreign arrays are implicitly casted to a R vector/list or not. <br>
* The default setting is <code>true</code>. * The default setting is <code>true</code>.
*
* @param flag
* @return
*/ */
public PreinitialPhaseBuilder castForeignObjects(boolean flag) { public PreinitialPhaseBuilder castForeignObjects(boolean flag) {
pipelineBuilder().getPipelineConfig().setCastForeignObjects(flag); pipelineBuilder().getPipelineConfig().setCastForeignObjects(flag);
......
...@@ -22,17 +22,27 @@ ...@@ -22,17 +22,27 @@
*/ */
package com.oracle.truffle.r.runtime.interop; package com.oracle.truffle.r.runtime.interop;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.ImportStatic; import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.ForeignAccess;
import com.oracle.truffle.api.interop.Message; import com.oracle.truffle.api.interop.Message;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.r.runtime.RInternalError;
import com.oracle.truffle.r.runtime.RRuntime; import com.oracle.truffle.r.runtime.RRuntime;
import com.oracle.truffle.r.runtime.data.RNull; import com.oracle.truffle.r.runtime.data.RNull;
import com.oracle.truffle.r.runtime.nodes.RBaseNode; import com.oracle.truffle.r.runtime.nodes.RBaseNode;
@ImportStatic(Message.class) @ImportStatic({RRuntime.class})
public abstract class Foreign2R extends RBaseNode { public abstract class Foreign2R extends RBaseNode {
@Child private Foreign2R recursive;
@Child private Node isBoxed;
@Child private Node unbox;
public static Foreign2R createForeign2R() { public static Foreign2R createForeign2R() {
return Foreign2RNodeGen.create(); return Foreign2RNodeGen.create();
} }
...@@ -74,6 +84,37 @@ public abstract class Foreign2R extends RBaseNode { ...@@ -74,6 +84,37 @@ public abstract class Foreign2R extends RBaseNode {
return RNull.instance; return RNull.instance;
} }
@Specialization(guards = "isForeignObject(obj)")
public Object doUnbox(TruffleObject obj) {
/*
* For the time being, we have to ask "IS_BOXED" all the time (instead of simply trying
* UNBOX first), because some TruffleObjects return bogus values from UNBOX when IS_BOXED is
* false.
*/
if (isBoxed == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
isBoxed = insert(Message.IS_BOXED.createNode());
}
if (ForeignAccess.sendIsBoxed(isBoxed, obj)) {
if (unbox == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
unbox = insert(Message.UNBOX.createNode());
}
try {
Object unboxed = ForeignAccess.sendUnbox(unbox, obj);
if (recursive == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
recursive = insert(Foreign2RNodeGen.create());
}
return recursive.execute(unboxed);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreter();
throw RInternalError.shouldNotReachHere(e, "object does not support UNBOX message even though IS_BOXED == true: " + obj.getClass().getSimpleName());
}
}
return obj;
}
@Fallback @Fallback
public Object doObject(Object obj) { public Object doObject(Object obj) {
return obj; return obj;
......
...@@ -192,9 +192,6 @@ public abstract class ForeignArray2R extends RBaseNode { ...@@ -192,9 +192,6 @@ public abstract class ForeignArray2R extends RBaseNode {
/** /**
* Converts the elements collected from a foreign array or java iterable into a vector or list. * Converts the elements collected from a foreign array or java iterable into a vector or list.
*
* @param ce
* @return
*/ */
public static RAbstractVector asAbstractVector(CollectedElements ce) { public static RAbstractVector asAbstractVector(CollectedElements ce) {
InteropTypeCheck.RType type = ce.typeCheck.getType(); InteropTypeCheck.RType type = ce.typeCheck.getType();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment