|
57 | 57 | import org.truffleruby.core.kernel.ChompLoopNode;
|
58 | 58 | import org.truffleruby.core.kernel.KernelGetsNode;
|
59 | 59 | import org.truffleruby.core.kernel.KernelPrintLastLineNode;
|
| 60 | +import org.truffleruby.core.string.TStringWithEncoding; |
60 | 61 | import org.truffleruby.language.EmitWarningsNode;
|
61 | 62 | import org.truffleruby.language.LexicalScope;
|
62 | 63 | import org.truffleruby.language.RubyEvalRootNode;
|
| 64 | +import org.truffleruby.language.RubyGuards; |
63 | 65 | import org.truffleruby.language.RubyNode;
|
64 | 66 | import org.truffleruby.language.RubyRootNode;
|
65 | 67 | import org.truffleruby.language.RubyTopLevelRootNode;
|
|
70 | 72 | import org.truffleruby.language.arguments.RubyArguments;
|
71 | 73 | import org.truffleruby.language.control.WhileNode;
|
72 | 74 | import org.truffleruby.language.control.WhileNodeFactory;
|
| 75 | +import org.truffleruby.language.library.RubyStringLibrary; |
| 76 | +import org.truffleruby.language.loader.ByteBasedCharSequence; |
73 | 77 | import org.truffleruby.language.locals.FrameDescriptorNamesIterator;
|
74 | 78 | import org.truffleruby.language.locals.WriteLocalVariableNode;
|
75 | 79 | import org.truffleruby.language.methods.Arity;
|
|
79 | 83 | import org.truffleruby.platform.Platform;
|
80 | 84 | import org.truffleruby.shared.Metrics;
|
81 | 85 | import org.yarp.Loader;
|
| 86 | +import org.yarp.Nodes; |
82 | 87 | import org.yarp.Parser;
|
83 | 88 |
|
84 | 89 | import java.util.ArrayList;
|
@@ -399,8 +404,8 @@ public static org.yarp.Nodes.Node parseToYARPAST(RubyContext context, RubyLangua
|
399 | 404 | org.yarp.Parser.loadLibrary(language.getRubyHome() + "/lib/libyarp" + Platform.LIB_SUFFIX);
|
400 | 405 | byte[] serializedBytes = Parser.parseAndSerialize(sourceBytes);
|
401 | 406 |
|
402 |
| - return Loader.load(sourceBytes, serializedBytes); |
403 |
| - |
| 407 | + var yarpSource = createYARPSource(sourceBytes, rubySource); |
| 408 | + return Loader.load(serializedBytes, yarpSource); |
404 | 409 | // YARP end
|
405 | 410 |
|
406 | 411 | // TODO: handle syntax errors
|
@@ -446,6 +451,24 @@ public static org.yarp.Nodes.Node parseToYARPAST(RubyContext context, RubyLangua
|
446 | 451 | // return (RootParseNode) result.getAST();
|
447 | 452 | }
|
448 | 453 |
|
| 454 | + public static RubySource createRubySource(Object code) { |
| 455 | + var tstringWithEnc = new TStringWithEncoding(RubyGuards.asTruffleStringUncached(code), |
| 456 | + RubyStringLibrary.getUncached().getEncoding(code)); |
| 457 | + var charSequence = new ByteBasedCharSequence(tstringWithEnc); |
| 458 | + Source source = Source.newBuilder("ruby", charSequence, "<parse_ast>").build(); |
| 459 | + return new RubySource(source, source.getName(), tstringWithEnc); |
| 460 | + } |
| 461 | + |
| 462 | + public static Nodes.Source createYARPSource(byte[] sourceBytes, RubySource rubySource) { |
| 463 | + Source source = rubySource.getSource(); |
| 464 | + int[] lineOffsets = new int[source.getLineCount()]; |
| 465 | + for (int line = 1; line <= source.getLineCount(); line++) { |
| 466 | + lineOffsets[line - 1] = source.getLineStartOffset(line); |
| 467 | + } |
| 468 | + |
| 469 | + return new Nodes.Source(sourceBytes, lineOffsets); |
| 470 | + } |
| 471 | + |
449 | 472 | private TranslatorEnvironment environmentForFrame(RubyContext context, MaterializedFrame frame, int blockDepth) {
|
450 | 473 | if (frame == null) {
|
451 | 474 | return null;
|
|
0 commit comments