Skip to content

Commit

Permalink
Pass RubySource around instead of Pair<Source, TStringWithEncoding>
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Nov 22, 2023
1 parent 233d5f6 commit 3a844cf
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 77 deletions.
10 changes: 3 additions & 7 deletions src/main/java/org/truffleruby/RubyLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -577,21 +577,17 @@ protected RootCallTarget parse(ParsingRequest request) {

final ParsingParameters parsingParameters = parsingRequestParams.get();
if (parsingParameters != null) { // from #require or core library
assert parsingParameters.getSource().equals(source);
final RubySource rubySource = new RubySource(
source,
parsingParameters.getPath(),
parsingParameters.getTStringWithEnc());
assert parsingParameters.rubySource.getSource().equals(source);
final ParserContext parserContext = MIME_TYPE_MAIN_SCRIPT.equals(source.getMimeType())
? ParserContext.TOP_LEVEL_FIRST
: ParserContext.TOP_LEVEL;
final LexicalScope lexicalScope = contextIfSingleContext.map(RubyContext::getRootLexicalScope).orElse(null);
return getCurrentContext().getCodeLoader().parse(
rubySource,
parsingParameters.rubySource,
parserContext,
null,
lexicalScope,
parsingParameters.getCurrentNode());
parsingParameters.currentNode);
}

RootNode root;
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/org/truffleruby/core/CoreLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.exception.AbstractTruffleException;
import org.graalvm.collections.Pair;
import org.jcodings.transcode.EConvFlags;
import org.truffleruby.RubyContext;
import org.truffleruby.RubyLanguage;
Expand All @@ -45,7 +44,6 @@
import org.truffleruby.core.module.RubyModule;
import org.truffleruby.core.numeric.BigIntegerOps;
import org.truffleruby.core.numeric.RubyBignum;
import org.truffleruby.core.string.TStringWithEncoding;
import org.truffleruby.debug.BindingLocalVariablesObject;
import org.truffleruby.debug.GlobalVariablesObject;
import org.truffleruby.debug.TopScopeObject;
Expand All @@ -64,6 +62,7 @@
import org.truffleruby.language.objects.ForeignClassNode;
import org.truffleruby.language.objects.SingletonClassNode;
import org.truffleruby.parser.ParserContext;
import org.truffleruby.parser.RubySource;
import org.truffleruby.parser.TranslatorDriver;
import org.truffleruby.parser.ast.RootParseNode;
import org.truffleruby.platform.NativeConfiguration;
Expand Down Expand Up @@ -761,10 +760,9 @@ public void loadRubyCoreLibraryAndPostBoot() {
state = State.LOADED;
}

var sourceTStringPair = loadCoreFileSource(language.coreLoadPath + file);
final Source source = sourceTStringPair.getLeft();
final RootCallTarget callTarget = context.getCodeLoader().parseTopLevelWithCache(sourceTStringPair,
node);
var rubySource = loadCoreFileSource(language.coreLoadPath + file);
final Source source = rubySource.getSource();
final RootCallTarget callTarget = context.getCodeLoader().parseTopLevelWithCache(rubySource, node);

final CodeLoader.DeferredCall deferredCall = context.getCodeLoader().prepareExecute(
callTarget,
Expand All @@ -787,13 +785,13 @@ public void loadRubyCoreLibraryAndPostBoot() {
}
}

public Pair<Source, TStringWithEncoding> loadCoreFileSource(String path) throws IOException {
public RubySource loadCoreFileSource(String path) throws IOException {
if (path.startsWith(RubyLanguage.RESOURCE_SCHEME)) {
if (TruffleOptions.AOT || ParserCache.INSTANCE != null) {
final RootParseNode rootParseNode = ParserCache.INSTANCE.get(path);
return Pair.create(rootParseNode.getSource(), null);
return new RubySource(rootParseNode.getSource(), path);
} else {
return Pair.create(ResourceLoader.loadResource(path, language.options.CORE_AS_INTERNAL), null);
return new RubySource(ResourceLoader.loadResource(path, language.options.CORE_AS_INTERNAL), path);
}
} else {
final FileLoader fileLoader = new FileLoader(context, language);
Expand Down
16 changes: 4 additions & 12 deletions src/main/java/org/truffleruby/core/kernel/TruffleKernelNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.strings.TruffleString;
import org.graalvm.collections.Pair;
import org.truffleruby.Layouts;
import org.truffleruby.annotations.CoreMethod;
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
Expand All @@ -32,7 +30,6 @@
import org.truffleruby.core.kernel.TruffleKernelNodesFactory.GetSpecialVariableStorageNodeGen;
import org.truffleruby.core.module.RubyModule;
import org.truffleruby.core.proc.RubyProc;
import org.truffleruby.core.string.TStringWithEncoding;
import org.truffleruby.core.symbol.RubySymbol;
import org.truffleruby.language.LexicalScope;
import org.truffleruby.language.Nil;
Expand Down Expand Up @@ -89,13 +86,12 @@ boolean load(Object file, Nil wrapModule,
@Cached @Shared RubyStringLibrary strings,
@Cached @Shared IndirectCallNode callNode) {
final String feature = RubyGuards.getJavaString(file);
final Pair<Source, TStringWithEncoding> sourceTStringPair = getSourceTStringPair(feature);
final RubySource rubySource = getRubySource(feature);

final DeclarationContext declarationContext = DeclarationContext.topLevel(getContext());
final LexicalScope lexicalScope = getContext().getRootLexicalScope();
final Object self = getContext().getCoreLibrary().mainObject;
final RootCallTarget callTarget = getContext().getCodeLoader().parseTopLevelWithCache(sourceTStringPair,
this);
final RootCallTarget callTarget = getContext().getCodeLoader().parseTopLevelWithCache(rubySource, this);

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

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

// callTarget
final RubySource rubySource = new RubySource(
sourceTStringPair.getLeft(),
feature,
sourceTStringPair.getRight());
final RootCallTarget callTarget = getContext()
.getCodeLoader()
.parse(rubySource, ParserContext.TOP_LEVEL, null, lexicalScope, this);
Expand All @@ -148,7 +140,7 @@ boolean load(Object file, RubyModule wrapModule,
return true;
}

private Pair<Source, TStringWithEncoding> getSourceTStringPair(String feature) {
private RubySource getRubySource(String feature) {
try {
final FileLoader fileLoader = new FileLoader(getContext(), getLanguage());
return fileLoader.loadFile(feature);
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/org/truffleruby/language/TruffleBootNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.nodes.NodeUtil;
import com.oracle.truffle.api.strings.TruffleString;
import org.graalvm.collections.Pair;
import org.graalvm.options.OptionDescriptor;
import org.truffleruby.RubyContext;
import org.truffleruby.RubyLanguage;
Expand Down Expand Up @@ -122,21 +121,17 @@ int main(int argc, long argv, String kind, String toExecute) {

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

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

if (getContext().getOptions().SYNTAX_CHECK) {
checkSyntax.call(coreLibrary().truffleBootModule, "check_syntax", source);
checkSyntax.call(coreLibrary().truffleBootModule, "check_syntax", rubySource);
} else {
var tstringWithEncoding = source.getTStringWithEncoding();
var sourceTStringPair = Pair.create(source.getSource(), tstringWithEncoding);
final RootCallTarget callTarget = getContext()
.getCodeLoader()
.parseTopLevelWithCache(sourceTStringPair, null);
var callTarget = getContext().getCodeLoader().parseTopLevelWithCache(rubySource, null);

final CodeLoader.DeferredCall deferredCall = getContext().getCodeLoader().prepareExecute(
callTarget,
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/org/truffleruby/language/loader/CodeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
package org.truffleruby.language.loader;

import com.oracle.truffle.api.source.Source;
import org.graalvm.collections.Pair;
import org.truffleruby.RubyContext;
import org.truffleruby.RubyLanguage;
import org.truffleruby.core.module.RubyModule;
import org.truffleruby.core.string.TStringWithEncoding;
import org.truffleruby.language.LexicalScope;
import org.truffleruby.language.Nil;
import org.truffleruby.language.RubyNode;
Expand Down Expand Up @@ -53,22 +51,20 @@ public CodeLoader(RubyLanguage language, RubyContext context) {
}

@TruffleBoundary
public RootCallTarget parseTopLevelWithCache(Pair<Source, TStringWithEncoding> sourceTStringPair,
Node currentNode) {
final Source source = sourceTStringPair.getLeft();
final TStringWithEncoding tstringWithEncoding = sourceTStringPair.getRight();
public RootCallTarget parseTopLevelWithCache(RubySource rubySource, Node currentNode) {
final Source source = rubySource.getSource();
var tstringWithEncoding = rubySource.getTStringWithEncoding();

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

language.parsingRequestParams.set(new ParsingParameters(currentNode, tstringWithEncoding, source));
language.parsingRequestParams.set(new ParsingParameters(currentNode, rubySource));
try {
return (RootCallTarget) context.getEnv().parseInternal(source);
} finally {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/truffleruby/language/loader/FileLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import java.io.IOException;

import com.oracle.truffle.api.nodes.Node;
import org.graalvm.collections.Pair;
import org.truffleruby.RubyContext;
import org.truffleruby.RubyLanguage;
import org.truffleruby.core.encoding.Encodings;
import org.truffleruby.core.string.TStringWithEncoding;
import org.truffleruby.language.control.RaiseException;
import org.truffleruby.parser.RubySource;
import org.truffleruby.parser.lexer.RubyLexer;
import org.truffleruby.shared.TruffleRuby;

Expand Down Expand Up @@ -56,7 +56,7 @@ public static void ensureReadable(RubyContext context, TruffleFile file, Node cu
}
}

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

final Source source = buildSource(file, path, sourceTString, isInternal(path), false);
return Pair.create(source, sourceTString);
return new RubySource(source, path, sourceTString);
}

public static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext context, String path) {
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/truffleruby/language/loader/RequireNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@

import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.source.Source;
import org.graalvm.collections.Pair;
import org.truffleruby.RubyContext;
import org.truffleruby.RubyLanguage;
import org.truffleruby.cext.ValueWrapperManager;
import org.truffleruby.core.cast.BooleanCastNode;
import org.truffleruby.core.string.StringUtils;
import org.truffleruby.core.string.TStringWithEncoding;
import org.truffleruby.language.RubyBaseNode;
import org.truffleruby.language.RubyConstant;
import org.truffleruby.language.RubyGuards;
Expand All @@ -38,6 +35,7 @@
import org.truffleruby.language.methods.DeclarationContext;
import org.truffleruby.language.methods.TranslateExceptionNode;
import org.truffleruby.parser.ParserContext;
import org.truffleruby.parser.RubySource;
import org.truffleruby.shared.Metrics;

import com.oracle.truffle.api.CompilerDirectives;
Expand Down Expand Up @@ -216,15 +214,15 @@ private boolean parseAndCall(String feature, String expandedPath) {
requireCExtension(feature, expandedPath, this);
} else {
// All other files are assumed to be Ruby, the file type detection is not enough
final Pair<Source, TStringWithEncoding> sourceTStringPair;
final RubySource rubySource;
try {
final FileLoader fileLoader = new FileLoader(getContext(), getLanguage());
sourceTStringPair = fileLoader.loadFile(expandedPath);
rubySource = fileLoader.loadFile(expandedPath);
} catch (IOException e) {
return false;
}

final RootCallTarget callTarget = getContext().getCodeLoader().parseTopLevelWithCache(sourceTStringPair,
final RootCallTarget callTarget = getContext().getCodeLoader().parseTopLevelWithCache(rubySource,
this);

final CodeLoader.DeferredCall deferredCall = getContext().getCodeLoader().prepareExecute(
Expand Down
28 changes: 4 additions & 24 deletions src/main/java/org/truffleruby/parser/ParsingParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,16 @@
package org.truffleruby.parser;

import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.Source;
import org.truffleruby.RubyLanguage;
import org.truffleruby.core.string.TStringWithEncoding;

public final class ParsingParameters {

/** For exceptions during parsing */
private final Node currentNode;
private final TStringWithEncoding tstringWithEnc;
private final Source source;
public final Node currentNode;
public final RubySource rubySource;

public ParsingParameters(Node currentNode, TStringWithEncoding tstringWithEnc, Source source) {
public ParsingParameters(Node currentNode, RubySource rubySource) {
this.currentNode = currentNode;
this.tstringWithEnc = tstringWithEnc;
this.source = source;
this.rubySource = rubySource;
}

public Node getCurrentNode() {
return currentNode;
}

public String getPath() {
return RubyLanguage.getPath(source);
}

public TStringWithEncoding getTStringWithEnc() {
return tstringWithEnc;
}

public Source getSource() {
return source;
}
}

0 comments on commit 3a844cf

Please sign in to comment.