Skip to content

Commit

Permalink
Showing 9 changed files with 27 additions and 14 deletions.
10 changes: 6 additions & 4 deletions truffle/src/main/java/org/jruby/truffle/JRubyTruffleImpl.java
Original file line number Diff line number Diff line change
@@ -37,8 +37,7 @@ public JRubyTruffleImpl(Ruby runtime) {
}

try {
context = (RubyContext) engine.eval(Source.fromText("Truffle::Boot.context", "context")
.withMimeType(RubyLanguage.MIME_TYPE)).get();
context = (RubyContext) engine.eval(loadSource("Truffle::Boot.context", "context")).get();
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -55,8 +54,7 @@ public Object execute(org.jruby.ast.RootNode rootNode) {
context.getJRubyInterop().setOriginalInputFile(rootNode.getPosition().getFile());

try {
return engine.eval(Source.fromText("Truffle::Boot.run_jruby_root", "run_jruby_root")
.withMimeType(RubyLanguage.MIME_TYPE)).get();
return engine.eval(loadSource("Truffle::Boot.run_jruby_root", "run_jruby_root")).get();
} catch (IOException e) {
if (e.getCause() instanceof ExitException) {
final ExitException exit = (ExitException) e.getCause();
@@ -71,5 +69,9 @@ public Object execute(org.jruby.ast.RootNode rootNode) {
public void dispose() {
engine.dispose();
}

private Source loadSource(String source, String name) {
return Source.fromText(source, name).withMimeType(RubyLanguage.MIME_TYPE);
}

}
8 changes: 7 additions & 1 deletion truffle/src/main/java/org/jruby/truffle/RubyContext.java
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.instrumentation.Instrumenter;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.source.Source;
import org.jruby.Ruby;
import org.jruby.truffle.builtins.PrimitiveManager;
import org.jruby.truffle.core.CoreLibrary;
@@ -76,7 +77,8 @@ public class RubyContext extends ExecutionContext {
private final TraceManager traceManager;
private final ObjectSpaceManager objectSpaceManager = new ObjectSpaceManager(this);
private final AtExitManager atExitManager = new AtExitManager(this);
private final SourceCache sourceCache = new SourceCache(new SourceLoader(this));
private final SourceLoader sourceLoader = new SourceLoader(this);
private final SourceCache sourceCache = new SourceCache(sourceLoader);
private final CallStackManager callStack = new CallStackManager(this);
private final CoreStrings coreStrings = new CoreStrings(this);
private final FrozenStrings frozenStrings = new FrozenStrings(this);
@@ -305,6 +307,10 @@ public AttachmentsManager getAttachmentsManager() {
return attachmentsManager;
}

public SourceLoader getSourceLoader() {
return sourceLoader;
}

public SourceCache getSourceCache() {
return sourceCache;
}
Original file line number Diff line number Diff line change
@@ -171,7 +171,7 @@ public Object instanceEval(VirtualFrame frame, Object receiver, DynamicObject st

// TODO (pitr 15-Oct-2015): fix this ugly hack, required for AS, copy-paste
final String space = new String(new char[Math.max(line - 1, 0)]).replace("\0", "\n");
final Source source = Source.fromText(space + code.toString(), StringOperations.rope(fileName).toString());
final Source source = getContext().getSourceLoader().loadFragment(space + code.toString(), StringOperations.rope(fileName).toString());

final RubyRootNode rootNode = getContext().getCodeLoader().parse(source, code.getEncoding(), ParserContext.EVAL, null, true, this);
final CodeLoader.DeferredCall deferredCall = getContext().getCodeLoader().prepareExecute(ParserContext.EVAL, DeclarationContext.INSTANCE_EVAL, rootNode, null, receiver);
Original file line number Diff line number Diff line change
@@ -652,7 +652,7 @@ private CodeLoader.DeferredCall doEvalX(DynamicObject rubySource,
// TODO (pitr 15-Oct-2015): fix this ugly hack, required for AS, copy-paste
final String space = new String(new char[Math.max(line - 1, 0)]).replace("\0", "\n");
// TODO CS 14-Apr-15 concat space + code as a rope, otherwise the string will be copied after the rope is converted
final Source source = Source.fromText(space + RopeOperations.decodeRope(code), filename);
final Source source = getContext().getSourceLoader().loadFragment(space + RopeOperations.decodeRope(code), filename);

final MaterializedFrame frame = Layouts.BINDING.getFrame(binding);
final DeclarationContext declarationContext = RubyArguments.getDeclarationContext(frame);
@@ -669,7 +669,7 @@ protected RootNodeWrapper compileSource(VirtualFrame frame, DynamicObject source
final MaterializedFrame parentFrame = Layouts.BINDING.getFrame(callerBinding);

final Encoding encoding = Layouts.STRING.getRope(sourceText).getEncoding();
final Source source = Source.fromText(sourceText.toString(), "(eval)");
final Source source = getContext().getSourceLoader().loadFragment(sourceText.toString(), "(eval)");

final TranslatorDriver translator = new TranslatorDriver(getContext());

Original file line number Diff line number Diff line change
@@ -624,7 +624,7 @@ private CodeLoader.DeferredCall classEvalSource(DynamicObject module, DynamicObj

// TODO (pitr 15-Oct-2015): fix this ugly hack, required for AS, copy-paste
final String space = new String(new char[Math.max(line - 1, 0)]).replace("\0", "\n");
Source source = Source.fromText(space + code, file);
Source source = getContext().getSourceLoader().loadFragment(space + code, file);

final RubyRootNode rootNode = getContext().getCodeLoader().parse(source, encoding, ParserContext.MODULE, callerFrame, true, this);
return getContext().getCodeLoader().prepareExecute(ParserContext.MODULE, DeclarationContext.CLASS_EVAL, rootNode, callerFrame, module);
Original file line number Diff line number Diff line change
@@ -55,9 +55,7 @@ public Object execute(VirtualFrame frame, String expression, Object... arguments
parameterFrameSlots[n] = frameDescriptor.findOrAddFrameSlot(parameters[n]);
}

final Source source = Source.fromText(
StringOperations.createByteList(this.expression),
"(snippet)");
final Source source = getContext().getSourceLoader().loadFragment(this.expression, "(snippet)");

final RubyRootNode rootNode = getContext().getCodeLoader().parse(
source,
Original file line number Diff line number Diff line change
@@ -130,7 +130,7 @@ public void ensureCExtImplementationLoaded(VirtualFrame frame, IndirectCallNode
private CallTarget getCExtLibRuby() {
final String path = context.getJRubyRuntime().getJRubyHome() + "/lib/ruby/truffle/cext/ruby.su";
try {
return parseSource(Source.fromFileName(path));
return parseSource(context.getSourceLoader().load(path));
} catch (IOException e) {
throw new RuntimeException(e);
}
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
import com.oracle.truffle.api.source.Source;
import org.jruby.Ruby;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.string.StringOperations;

import java.io.File;
import java.io.FileNotFoundException;
@@ -48,6 +49,12 @@ public Source load(String canonicalPath) throws IOException {
}
}

public Source loadFragment(String fragment, String name) {
return Source.fromText(
StringOperations.createByteList(fragment),
name);
}

private Source loadInlineScript() {
return Source.fromText(new String(context.getJRubyRuntime().getInstanceConfig().inlineScript(),
StandardCharsets.UTF_8), "-e");
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ public void run(MaterializedFrame frame, Node currentNode) {
default: {
try {
final RubyRootNode rootNode = context.getCodeLoader().parse(
Source.fromText(shellLine, "shell"),
context.getSourceLoader().loadFragment(shellLine, "(shell)"),
UTF8Encoding.INSTANCE,
ParserContext.EVAL,
currentFrame,

0 comments on commit ee1d56f

Please sign in to comment.