Skip to content

Commit 09285e0

Browse files
committed
Adopt new YARP Loader.load() signature
1 parent 1eb5bc5 commit 09285e0

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

src/main/java/org/truffleruby/debug/TruffleDebugNodes.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
import org.truffleruby.parser.RubySource;
112112
import org.truffleruby.parser.TranslatorDriver;
113113
import org.truffleruby.parser.TranslatorEnvironment;
114+
import org.truffleruby.parser.YARPTranslatorDriver;
114115
import org.truffleruby.parser.parser.ParserConfiguration;
115116
import org.truffleruby.parser.scope.StaticScope;
116117
import org.truffleruby.platform.Platform;
@@ -290,7 +291,8 @@ protected Object parse(Object code,
290291

291292
byte[] serialized = yarpSerialize(getLanguage(), source);
292293

293-
var ast = Loader.load(source, serialized);
294+
var yarpSource = YARPTranslatorDriver.createYARPSource(source, YARPTranslatorDriver.createRubySource(code));
295+
var ast = Loader.load(serialized, yarpSource);
294296

295297
return createString(fromJavaStringNode, ast.toString(), Encodings.UTF_8);
296298
}

src/main/java/org/truffleruby/language/loader/CodeLoader.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
import org.truffleruby.core.string.TStringWithEncoding;
1818
import org.truffleruby.language.LexicalScope;
1919
import org.truffleruby.language.Nil;
20-
import org.truffleruby.language.RubyGuards;
2120
import org.truffleruby.language.RubyNode;
2221
import org.truffleruby.language.RubyRootNode;
2322
import org.truffleruby.annotations.Visibility;
2423
import org.truffleruby.language.arguments.EmptyArgumentsDescriptor;
2524
import org.truffleruby.language.arguments.RubyArguments;
26-
import org.truffleruby.language.library.RubyStringLibrary;
2725
import org.truffleruby.language.methods.DeclarationContext;
2826
import org.truffleruby.language.methods.InternalMethod;
2927
import org.truffleruby.language.methods.SharedMethodInfo;
@@ -95,11 +93,7 @@ public RootCallTarget parseWithYARP(Object code,
9593
MaterializedFrame parentFrame,
9694
LexicalScope lexicalScope,
9795
Node currentNode) {
98-
var tstringWithEnc = new TStringWithEncoding(RubyGuards.asTruffleStringUncached(code),
99-
RubyStringLibrary.getUncached().getEncoding(code));
100-
var charSequence = new ByteBasedCharSequence(tstringWithEnc);
101-
Source source = Source.newBuilder("ruby", charSequence, "<parse_ast>").build();
102-
var rubySource = new RubySource(source, source.getName(), tstringWithEnc);
96+
RubySource rubySource = YARPTranslatorDriver.createRubySource(code);
10397
final YARPTranslatorDriver translator = new YARPTranslatorDriver(context, rubySource);
10498
return translator.parse(rubySource, parserContext, null, parentFrame, lexicalScope, currentNode);
10599
}

src/main/java/org/truffleruby/parser/YARPTranslatorDriver.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@
5757
import org.truffleruby.core.kernel.ChompLoopNode;
5858
import org.truffleruby.core.kernel.KernelGetsNode;
5959
import org.truffleruby.core.kernel.KernelPrintLastLineNode;
60+
import org.truffleruby.core.string.TStringWithEncoding;
6061
import org.truffleruby.language.EmitWarningsNode;
6162
import org.truffleruby.language.LexicalScope;
6263
import org.truffleruby.language.RubyEvalRootNode;
64+
import org.truffleruby.language.RubyGuards;
6365
import org.truffleruby.language.RubyNode;
6466
import org.truffleruby.language.RubyRootNode;
6567
import org.truffleruby.language.RubyTopLevelRootNode;
@@ -70,6 +72,8 @@
7072
import org.truffleruby.language.arguments.RubyArguments;
7173
import org.truffleruby.language.control.WhileNode;
7274
import org.truffleruby.language.control.WhileNodeFactory;
75+
import org.truffleruby.language.library.RubyStringLibrary;
76+
import org.truffleruby.language.loader.ByteBasedCharSequence;
7377
import org.truffleruby.language.locals.FrameDescriptorNamesIterator;
7478
import org.truffleruby.language.locals.WriteLocalVariableNode;
7579
import org.truffleruby.language.methods.Arity;
@@ -79,6 +83,7 @@
7983
import org.truffleruby.platform.Platform;
8084
import org.truffleruby.shared.Metrics;
8185
import org.yarp.Loader;
86+
import org.yarp.Nodes;
8287
import org.yarp.Parser;
8388

8489
import java.util.ArrayList;
@@ -399,8 +404,8 @@ public static org.yarp.Nodes.Node parseToYARPAST(RubyContext context, RubyLangua
399404
org.yarp.Parser.loadLibrary(language.getRubyHome() + "/lib/libyarp" + Platform.LIB_SUFFIX);
400405
byte[] serializedBytes = Parser.parseAndSerialize(sourceBytes);
401406

402-
return Loader.load(sourceBytes, serializedBytes);
403-
407+
var yarpSource = createYARPSource(sourceBytes, rubySource);
408+
return Loader.load(serializedBytes, yarpSource);
404409
// YARP end
405410

406411
// TODO: handle syntax errors
@@ -446,6 +451,24 @@ public static org.yarp.Nodes.Node parseToYARPAST(RubyContext context, RubyLangua
446451
// return (RootParseNode) result.getAST();
447452
}
448453

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+
449472
private TranslatorEnvironment environmentForFrame(RubyContext context, MaterializedFrame frame, int blockDepth) {
450473
if (frame == null) {
451474
return null;

0 commit comments

Comments
 (0)