Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Truffle] Implement polyglot eval inefficiently and with an ugly hack.
Browse files Browse the repository at this point in the history
chrisseaton committed Nov 7, 2015
1 parent c2ceee9 commit 37d5a00
Showing 2 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/JRubyTruffleImpl.java
Original file line number Diff line number Diff line change
@@ -20,13 +20,17 @@

public class JRubyTruffleImpl implements JRubyTruffleInterface {

public static PolyglotEngine mostRecentPolyglot;

private final PolyglotEngine engine;
private final RubyContext context;

// Run by reflection from Ruby#loadTruffle
public JRubyTruffleImpl(Ruby runtime) {
engine = PolyglotEngine.buildNew().globalSymbol(JRubyTruffleInterface.RUNTIME_SYMBOL, new RubyLanguage.JRubyContextWrapper(runtime)).build();

mostRecentPolyglot = engine;

try {
context = (RubyContext) engine.eval(Source.fromText("Truffle::Primitive.context", "context").withMimeType(RubyLanguage.MIME_TYPE)).get();
} catch (IOException e) {
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.FrameInstance;
import com.oracle.truffle.api.frame.FrameInstanceVisitor;
@@ -22,11 +23,13 @@
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.vm.PolyglotEngine;
import jnr.posix.SpawnFileAction;
import org.jcodings.specific.UTF8Encoding;
import org.jruby.Ruby;
import org.jruby.RubyGC;
import org.jruby.ext.rbconfig.RbConfigLibrary;
import org.jruby.truffle.JRubyTruffleImpl;
import org.jruby.truffle.nodes.RubyGuards;
import org.jruby.truffle.runtime.NotProvided;
import org.jruby.truffle.runtime.RubyCallStack;
@@ -781,4 +784,45 @@ public Object runJRubyRootNode() {
}
}

@CoreMethod(names = "polyglot_eval_supported", onSingleton = true)
public abstract static class PolyglotEvalSupportedNode extends CoreMethodArrayArgumentsNode {

public PolyglotEvalSupportedNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@TruffleBoundary
@Specialization
public DynamicObject polyglotEvalSupported() {
final String[] languages = JRubyTruffleImpl.mostRecentPolyglot.getLanguages().keySet().toArray(new String[]{});

final Object[] mimeTypes = new Object[languages.length];

for (int n = 0; n < languages.length; n++) {
mimeTypes[n] = createString(StringOperations.encodeByteList(languages[n], UTF8Encoding.INSTANCE));
}

return Layouts.ARRAY.createArray(getContext().getCoreLibrary().getArrayFactory(), mimeTypes, mimeTypes.length);
}
}

@CoreMethod(names = "polyglot_eval", onSingleton = true, required = 2)
public abstract static class PolyglotEvalNode extends CoreMethodArrayArgumentsNode {

public PolyglotEvalNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@TruffleBoundary
@Specialization(guards = {"isRubyString(mimeType)", "isRubyString(source)"})
public Object polyglotEval(DynamicObject mimeType, DynamicObject source) {
try {
return JRubyTruffleImpl.mostRecentPolyglot.eval(
Source.fromNamedText(source.toString(), "(eval)").withMimeType(mimeType.toString())).get();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

}

0 comments on commit 37d5a00

Please sign in to comment.