Skip to content

Commit 3a844cf

Browse files
committed
Pass RubySource around instead of Pair<Source, TStringWithEncoding>
1 parent 233d5f6 commit 3a844cf

File tree

8 files changed

+32
-77
lines changed

8 files changed

+32
-77
lines changed

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -577,21 +577,17 @@ protected RootCallTarget parse(ParsingRequest request) {
577577

578578
final ParsingParameters parsingParameters = parsingRequestParams.get();
579579
if (parsingParameters != null) { // from #require or core library
580-
assert parsingParameters.getSource().equals(source);
581-
final RubySource rubySource = new RubySource(
582-
source,
583-
parsingParameters.getPath(),
584-
parsingParameters.getTStringWithEnc());
580+
assert parsingParameters.rubySource.getSource().equals(source);
585581
final ParserContext parserContext = MIME_TYPE_MAIN_SCRIPT.equals(source.getMimeType())
586582
? ParserContext.TOP_LEVEL_FIRST
587583
: ParserContext.TOP_LEVEL;
588584
final LexicalScope lexicalScope = contextIfSingleContext.map(RubyContext::getRootLexicalScope).orElse(null);
589585
return getCurrentContext().getCodeLoader().parse(
590-
rubySource,
586+
parsingParameters.rubySource,
591587
parserContext,
592588
null,
593589
lexicalScope,
594-
parsingParameters.getCurrentNode());
590+
parsingParameters.currentNode);
595591
}
596592

597593
RootNode root;

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import com.oracle.truffle.api.RootCallTarget;
2626
import com.oracle.truffle.api.exception.AbstractTruffleException;
27-
import org.graalvm.collections.Pair;
2827
import org.jcodings.transcode.EConvFlags;
2928
import org.truffleruby.RubyContext;
3029
import org.truffleruby.RubyLanguage;
@@ -45,7 +44,6 @@
4544
import org.truffleruby.core.module.RubyModule;
4645
import org.truffleruby.core.numeric.BigIntegerOps;
4746
import org.truffleruby.core.numeric.RubyBignum;
48-
import org.truffleruby.core.string.TStringWithEncoding;
4947
import org.truffleruby.debug.BindingLocalVariablesObject;
5048
import org.truffleruby.debug.GlobalVariablesObject;
5149
import org.truffleruby.debug.TopScopeObject;
@@ -64,6 +62,7 @@
6462
import org.truffleruby.language.objects.ForeignClassNode;
6563
import org.truffleruby.language.objects.SingletonClassNode;
6664
import org.truffleruby.parser.ParserContext;
65+
import org.truffleruby.parser.RubySource;
6766
import org.truffleruby.parser.TranslatorDriver;
6867
import org.truffleruby.parser.ast.RootParseNode;
6968
import org.truffleruby.platform.NativeConfiguration;
@@ -761,10 +760,9 @@ public void loadRubyCoreLibraryAndPostBoot() {
761760
state = State.LOADED;
762761
}
763762

764-
var sourceTStringPair = loadCoreFileSource(language.coreLoadPath + file);
765-
final Source source = sourceTStringPair.getLeft();
766-
final RootCallTarget callTarget = context.getCodeLoader().parseTopLevelWithCache(sourceTStringPair,
767-
node);
763+
var rubySource = loadCoreFileSource(language.coreLoadPath + file);
764+
final Source source = rubySource.getSource();
765+
final RootCallTarget callTarget = context.getCodeLoader().parseTopLevelWithCache(rubySource, node);
768766

769767
final CodeLoader.DeferredCall deferredCall = context.getCodeLoader().prepareExecute(
770768
callTarget,
@@ -787,13 +785,13 @@ public void loadRubyCoreLibraryAndPostBoot() {
787785
}
788786
}
789787

790-
public Pair<Source, TStringWithEncoding> loadCoreFileSource(String path) throws IOException {
788+
public RubySource loadCoreFileSource(String path) throws IOException {
791789
if (path.startsWith(RubyLanguage.RESOURCE_SCHEME)) {
792790
if (TruffleOptions.AOT || ParserCache.INSTANCE != null) {
793791
final RootParseNode rootParseNode = ParserCache.INSTANCE.get(path);
794-
return Pair.create(rootParseNode.getSource(), null);
792+
return new RubySource(rootParseNode.getSource(), path);
795793
} else {
796-
return Pair.create(ResourceLoader.loadResource(path, language.options.CORE_AS_INTERNAL), null);
794+
return new RubySource(ResourceLoader.loadResource(path, language.options.CORE_AS_INTERNAL), path);
797795
}
798796
} else {
799797
final FileLoader fileLoader = new FileLoader(context, language);

src/main/java/org/truffleruby/core/kernel/TruffleKernelNodes.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import com.oracle.truffle.api.frame.Frame;
1919
import com.oracle.truffle.api.nodes.Node;
2020
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
21-
import com.oracle.truffle.api.source.Source;
2221
import com.oracle.truffle.api.strings.TruffleString;
23-
import org.graalvm.collections.Pair;
2422
import org.truffleruby.Layouts;
2523
import org.truffleruby.annotations.CoreMethod;
2624
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
@@ -32,7 +30,6 @@
3230
import org.truffleruby.core.kernel.TruffleKernelNodesFactory.GetSpecialVariableStorageNodeGen;
3331
import org.truffleruby.core.module.RubyModule;
3432
import org.truffleruby.core.proc.RubyProc;
35-
import org.truffleruby.core.string.TStringWithEncoding;
3633
import org.truffleruby.core.symbol.RubySymbol;
3734
import org.truffleruby.language.LexicalScope;
3835
import org.truffleruby.language.Nil;
@@ -89,13 +86,12 @@ boolean load(Object file, Nil wrapModule,
8986
@Cached @Shared RubyStringLibrary strings,
9087
@Cached @Shared IndirectCallNode callNode) {
9188
final String feature = RubyGuards.getJavaString(file);
92-
final Pair<Source, TStringWithEncoding> sourceTStringPair = getSourceTStringPair(feature);
89+
final RubySource rubySource = getRubySource(feature);
9390

9491
final DeclarationContext declarationContext = DeclarationContext.topLevel(getContext());
9592
final LexicalScope lexicalScope = getContext().getRootLexicalScope();
9693
final Object self = getContext().getCoreLibrary().mainObject;
97-
final RootCallTarget callTarget = getContext().getCodeLoader().parseTopLevelWithCache(sourceTStringPair,
98-
this);
94+
final RootCallTarget callTarget = getContext().getCodeLoader().parseTopLevelWithCache(rubySource, this);
9995

10096
final CodeLoader.DeferredCall deferredCall = getContext().getCodeLoader().prepareExecute(
10197
callTarget,
@@ -116,7 +112,7 @@ boolean load(Object file, RubyModule wrapModule,
116112
@Cached @Shared RubyStringLibrary strings,
117113
@Cached @Shared IndirectCallNode callNode) {
118114
final String feature = RubyGuards.getJavaString(file);
119-
final Pair<Source, TStringWithEncoding> sourceTStringPair = getSourceTStringPair(feature);
115+
final RubySource rubySource = getRubySource(feature);
120116

121117
final DeclarationContext declarationContext = DeclarationContext.topLevel(wrapModule);
122118
final LexicalScope lexicalScope = new LexicalScope(getContext().getRootLexicalScope(), wrapModule);
@@ -127,10 +123,6 @@ boolean load(Object file, RubyModule wrapModule,
127123
DispatchNode.getUncached().call(self, "extend", wrapModule);
128124

129125
// callTarget
130-
final RubySource rubySource = new RubySource(
131-
sourceTStringPair.getLeft(),
132-
feature,
133-
sourceTStringPair.getRight());
134126
final RootCallTarget callTarget = getContext()
135127
.getCodeLoader()
136128
.parse(rubySource, ParserContext.TOP_LEVEL, null, lexicalScope, this);
@@ -148,7 +140,7 @@ boolean load(Object file, RubyModule wrapModule,
148140
return true;
149141
}
150142

151-
private Pair<Source, TStringWithEncoding> getSourceTStringPair(String feature) {
143+
private RubySource getRubySource(String feature) {
152144
try {
153145
final FileLoader fileLoader = new FileLoader(getContext(), getLanguage());
154146
return fileLoader.loadFile(feature);

src/main/java/org/truffleruby/language/TruffleBootNodes.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.oracle.truffle.api.RootCallTarget;
1919
import com.oracle.truffle.api.nodes.NodeUtil;
2020
import com.oracle.truffle.api.strings.TruffleString;
21-
import org.graalvm.collections.Pair;
2221
import org.graalvm.options.OptionDescriptor;
2322
import org.truffleruby.RubyContext;
2423
import org.truffleruby.RubyLanguage;
@@ -122,21 +121,17 @@ int main(int argc, long argv, String kind, String toExecute) {
122121

123122
// Need to set $0 before loading required libraries
124123
// Also, a non-existing main script file errors out before loading required libraries
125-
final RubySource source = loadMainSourceSettingDollarZero(kind, toExecute.intern()); //intern() to improve footprint
124+
final RubySource rubySource = loadMainSourceSettingDollarZero(kind, toExecute.intern()); //intern() to improve footprint
126125

127126
// Load libraries required from the command line (-r LIBRARY)
128127
for (String requiredLibrary : getContext().getOptions().REQUIRED_LIBRARIES) {
129128
requireNode.call(coreLibrary().mainObject, "require", utf8(requiredLibrary));
130129
}
131130

132131
if (getContext().getOptions().SYNTAX_CHECK) {
133-
checkSyntax.call(coreLibrary().truffleBootModule, "check_syntax", source);
132+
checkSyntax.call(coreLibrary().truffleBootModule, "check_syntax", rubySource);
134133
} else {
135-
var tstringWithEncoding = source.getTStringWithEncoding();
136-
var sourceTStringPair = Pair.create(source.getSource(), tstringWithEncoding);
137-
final RootCallTarget callTarget = getContext()
138-
.getCodeLoader()
139-
.parseTopLevelWithCache(sourceTStringPair, null);
134+
var callTarget = getContext().getCodeLoader().parseTopLevelWithCache(rubySource, null);
140135

141136
final CodeLoader.DeferredCall deferredCall = getContext().getCodeLoader().prepareExecute(
142137
callTarget,

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
package org.truffleruby.language.loader;
1111

1212
import com.oracle.truffle.api.source.Source;
13-
import org.graalvm.collections.Pair;
1413
import org.truffleruby.RubyContext;
1514
import org.truffleruby.RubyLanguage;
1615
import org.truffleruby.core.module.RubyModule;
17-
import org.truffleruby.core.string.TStringWithEncoding;
1816
import org.truffleruby.language.LexicalScope;
1917
import org.truffleruby.language.Nil;
2018
import org.truffleruby.language.RubyNode;
@@ -53,22 +51,20 @@ public CodeLoader(RubyLanguage language, RubyContext context) {
5351
}
5452

5553
@TruffleBoundary
56-
public RootCallTarget parseTopLevelWithCache(Pair<Source, TStringWithEncoding> sourceTStringPair,
57-
Node currentNode) {
58-
final Source source = sourceTStringPair.getLeft();
59-
final TStringWithEncoding tstringWithEncoding = sourceTStringPair.getRight();
54+
public RootCallTarget parseTopLevelWithCache(RubySource rubySource, Node currentNode) {
55+
final Source source = rubySource.getSource();
56+
var tstringWithEncoding = rubySource.getTStringWithEncoding();
6057

6158
final String path = RubyLanguage.getPath(source);
6259
if (language.singleContext && !alreadyLoadedInContext.add(language.getPathRelativeToHome(path))) {
6360
/* Duplicate load of the same file in the same context, we cannot use the cache because it would re-assign
6461
* the live modules of static LexicalScopes and we cannot/do not want to invalidate static LexicalScopes, so
6562
* there the static lexical scope and its module are constants and need no checks in single context (e.g.,
6663
* in LookupConstantWithLexicalScopeNode). */
67-
final RubySource rubySource = new RubySource(source, path, tstringWithEncoding);
6864
return parse(rubySource, ParserContext.TOP_LEVEL, null, context.getRootLexicalScope(), currentNode);
6965
}
7066

71-
language.parsingRequestParams.set(new ParsingParameters(currentNode, tstringWithEncoding, source));
67+
language.parsingRequestParams.set(new ParsingParameters(currentNode, rubySource));
7268
try {
7369
return (RootCallTarget) context.getEnv().parseInternal(source);
7470
} finally {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
import java.io.IOException;
1414

1515
import com.oracle.truffle.api.nodes.Node;
16-
import org.graalvm.collections.Pair;
1716
import org.truffleruby.RubyContext;
1817
import org.truffleruby.RubyLanguage;
1918
import org.truffleruby.core.encoding.Encodings;
2019
import org.truffleruby.core.string.TStringWithEncoding;
2120
import org.truffleruby.language.control.RaiseException;
21+
import org.truffleruby.parser.RubySource;
2222
import org.truffleruby.parser.lexer.RubyLexer;
2323
import org.truffleruby.shared.TruffleRuby;
2424

@@ -56,7 +56,7 @@ public static void ensureReadable(RubyContext context, TruffleFile file, Node cu
5656
}
5757
}
5858

59-
public Pair<Source, TStringWithEncoding> loadFile(String path) throws IOException {
59+
public RubySource loadFile(String path) throws IOException {
6060
if (context.getOptions().LOG_LOAD) {
6161
RubyLanguage.LOGGER.info("loading " + path);
6262
}
@@ -73,7 +73,7 @@ public Pair<Source, TStringWithEncoding> loadFile(String path) throws IOExceptio
7373
var sourceTString = RubyLexer.createSourceTStringBasedOnMagicEncodingComment(sourceBytes, Encodings.UTF_8);
7474

7575
final Source source = buildSource(file, path, sourceTString, isInternal(path), false);
76-
return Pair.create(source, sourceTString);
76+
return new RubySource(source, path, sourceTString);
7777
}
7878

7979
public static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext context, String path) {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919

2020
import com.oracle.truffle.api.RootCallTarget;
2121
import com.oracle.truffle.api.dsl.Cached;
22-
import com.oracle.truffle.api.source.Source;
23-
import org.graalvm.collections.Pair;
2422
import org.truffleruby.RubyContext;
2523
import org.truffleruby.RubyLanguage;
2624
import org.truffleruby.cext.ValueWrapperManager;
2725
import org.truffleruby.core.cast.BooleanCastNode;
2826
import org.truffleruby.core.string.StringUtils;
29-
import org.truffleruby.core.string.TStringWithEncoding;
3027
import org.truffleruby.language.RubyBaseNode;
3128
import org.truffleruby.language.RubyConstant;
3229
import org.truffleruby.language.RubyGuards;
@@ -38,6 +35,7 @@
3835
import org.truffleruby.language.methods.DeclarationContext;
3936
import org.truffleruby.language.methods.TranslateExceptionNode;
4037
import org.truffleruby.parser.ParserContext;
38+
import org.truffleruby.parser.RubySource;
4139
import org.truffleruby.shared.Metrics;
4240

4341
import com.oracle.truffle.api.CompilerDirectives;
@@ -216,15 +214,15 @@ private boolean parseAndCall(String feature, String expandedPath) {
216214
requireCExtension(feature, expandedPath, this);
217215
} else {
218216
// All other files are assumed to be Ruby, the file type detection is not enough
219-
final Pair<Source, TStringWithEncoding> sourceTStringPair;
217+
final RubySource rubySource;
220218
try {
221219
final FileLoader fileLoader = new FileLoader(getContext(), getLanguage());
222-
sourceTStringPair = fileLoader.loadFile(expandedPath);
220+
rubySource = fileLoader.loadFile(expandedPath);
223221
} catch (IOException e) {
224222
return false;
225223
}
226224

227-
final RootCallTarget callTarget = getContext().getCodeLoader().parseTopLevelWithCache(sourceTStringPair,
225+
final RootCallTarget callTarget = getContext().getCodeLoader().parseTopLevelWithCache(rubySource,
228226
this);
229227

230228
final CodeLoader.DeferredCall deferredCall = getContext().getCodeLoader().prepareExecute(

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,16 @@
1010
package org.truffleruby.parser;
1111

1212
import com.oracle.truffle.api.nodes.Node;
13-
import com.oracle.truffle.api.source.Source;
14-
import org.truffleruby.RubyLanguage;
15-
import org.truffleruby.core.string.TStringWithEncoding;
1613

1714
public final class ParsingParameters {
1815

1916
/** For exceptions during parsing */
20-
private final Node currentNode;
21-
private final TStringWithEncoding tstringWithEnc;
22-
private final Source source;
17+
public final Node currentNode;
18+
public final RubySource rubySource;
2319

24-
public ParsingParameters(Node currentNode, TStringWithEncoding tstringWithEnc, Source source) {
20+
public ParsingParameters(Node currentNode, RubySource rubySource) {
2521
this.currentNode = currentNode;
26-
this.tstringWithEnc = tstringWithEnc;
27-
this.source = source;
22+
this.rubySource = rubySource;
2823
}
2924

30-
public Node getCurrentNode() {
31-
return currentNode;
32-
}
33-
34-
public String getPath() {
35-
return RubyLanguage.getPath(source);
36-
}
37-
38-
public TStringWithEncoding getTStringWithEnc() {
39-
return tstringWithEnc;
40-
}
41-
42-
public Source getSource() {
43-
return source;
44-
}
4525
}

0 commit comments

Comments
 (0)