From 30186e56d113a37cbb3125703fe3359be73fa167 Mon Sep 17 00:00:00 2001 From: Julien Lopez <julien.lopez@lri.fr> Date: Thu, 1 Mar 2018 10:34:25 +0100 Subject: [PATCH] Improvement in executeR --- .../r/engine/TruffleRLanguageImpl.java | 24 +++++++++++++-- .../truffle/r/runtime/RValueFunction.java | 30 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RValueFunction.java diff --git a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguageImpl.java b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguageImpl.java index 4663ced2b8..672d86f9a5 100644 --- a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguageImpl.java +++ b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/TruffleRLanguageImpl.java @@ -34,13 +34,16 @@ import com.oracle.truffle.api.CallTarget; import com.oracle.truffle.api.CompilerAsserts; import com.oracle.truffle.api.TruffleLanguage; import com.oracle.truffle.api.frame.Frame; +import com.oracle.truffle.api.frame.MaterializedFrame; import com.oracle.truffle.api.instrumentation.Instrumenter; import com.oracle.truffle.api.instrumentation.ProvidedTags; import com.oracle.truffle.api.instrumentation.StandardTags; +import com.oracle.truffle.api.interop.TruffleObject; import com.oracle.truffle.api.metadata.ScopeProvider; import com.oracle.truffle.api.nodes.Node; import com.oracle.truffle.api.source.Source; import com.oracle.truffle.api.source.SourceSection; +import com.oracle.truffle.api.vm.EngineTruffleObject; import com.oracle.truffle.api.vm.PolyglotEngine; import com.oracle.truffle.api.vm.PolyglotEngine.Value; import com.oracle.truffle.r.engine.interop.RForeignAccessFactoryImpl; @@ -55,6 +58,7 @@ import com.oracle.truffle.r.runtime.RAccuracyInfo; import com.oracle.truffle.r.runtime.RDeparse; import com.oracle.truffle.r.runtime.RRuntime; import com.oracle.truffle.r.runtime.RValue; +import com.oracle.truffle.r.runtime.RValueFunction; import com.oracle.truffle.r.runtime.Utils; import com.oracle.truffle.r.runtime.context.Engine.IncompleteSourceException; import com.oracle.truffle.r.runtime.context.Engine.ParseException; @@ -244,6 +248,23 @@ public final class TruffleRLanguageImpl extends TruffleRLanguage implements Scop public static final Map<String, RValue> valueCache = new HashMap<>(); public static PolyglotEngine vm = null; + private static final Serializable getRValueFromTruffle(final Value v) { + final Object o = v.get(); + + if (o instanceof Serializable) + return (Serializable) o; + if (o instanceof EngineTruffleObject) { + final TruffleObject delegate = ((EngineTruffleObject) o).getDelegate(); + if (delegate instanceof RFunction) { + final RFunction fun = (RFunction) delegate; + final MaterializedFrame frame = fun.getEnclosingFrame(); + if (frame instanceof Serializable) + return new RValueFunction(fun.getName(), v.getSourceLocation().getCharacters().toString(), frame); + } + } + return v.getSourceLocation().getCharacters().toString(); + } + public static final RValue executeR(final String program) { if (vm == null) vm = PolyglotEngine.newBuilder().config("application/x-r", "REngine", null).build(); @@ -251,8 +272,7 @@ public final class TruffleRLanguageImpl extends TruffleRLanguage implements Scop return valueCache.get(program); try { final Value v = vm.eval(Source.newBuilder(program).name("RBuilder").mimeType(RRuntime.R_APP_MIME).build()); - final Object o = v.get(); - final RValue res = new RValue(o instanceof Serializable ? (Serializable) o : v.getSourceLocation().getCharacters().toString()); + final RValue res = new RValue(getRValueFromTruffle(v)); valueCache.put(program, res); return res; } catch (Exception e) { diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RValueFunction.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RValueFunction.java new file mode 100644 index 0000000000..3171b3ccc0 --- /dev/null +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RValueFunction.java @@ -0,0 +1,30 @@ +package com.oracle.truffle.r.runtime; + +import java.io.Serializable; + +import com.oracle.truffle.api.frame.MaterializedFrame; + +public class RValueFunction implements Serializable { + private static final long serialVersionUID = 1L; + private final String name; + private final String code; + private final MaterializedFrame frame; + + public RValueFunction(final String name, final String code, final MaterializedFrame frame) { + this.name = name; + this.code = code; + this.frame = frame; + } + + public final String getName() { + return name; + } + + public final String getCode() { + return code; + } + + public final MaterializedFrame getDependencies() { + return frame; + } +} -- GitLab