Skip to content

Commit 54da828

Browse files
committed
Make core-load-path a language option
1 parent 310f055 commit 54da828

32 files changed

+117
-113
lines changed

src/main/java/org/truffleruby/RubyContext.java

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,6 @@ private boolean compatibleOptions(Options oldOptions, Options newOptions, boolea
351351
return false;
352352
}
353353

354-
if (!newOptions.CORE_LOAD_PATH.equals(OptionsCatalog.CORE_LOAD_PATH_KEY.getDefaultValue())) {
355-
RubyLanguage.LOGGER.fine(notReusingContext + "--core-load-path is set: " + newOptions.CORE_LOAD_PATH);
356-
return false; // Should load the specified core files
357-
}
358-
359354
if (hadHome != hasHome) {
360355
RubyLanguage.LOGGER.fine(notReusingContext + "Ruby home is " + (hasHome ? "set" : "unset"));
361356
return false;
@@ -807,34 +802,6 @@ public WeakValueCache<RegexpCacheKey, Regex> getRegexpCache() {
807802
return regexpCache;
808803
}
809804

810-
/** {@link #getSourcePath(Source)} should be used instead whenever possible (i.e., when we can access the context).
811-
*
812-
* Returns the path of a Source. Returns the short, potentially relative, path for the main script. Note however
813-
* that the path of {@code eval(code, nil, filename)} is just {@code filename} and might not be absolute. */
814-
public static String getPath(Source source) {
815-
final String path = source.getPath();
816-
if (path != null) {
817-
return path;
818-
} else {
819-
// non-file sources: eval(), main_boot_source, etc
820-
final String name = source.getName();
821-
assert name != null;
822-
return name;
823-
}
824-
}
825-
826-
/** {@link RubyContext#getPath(Source)} but also handles core library sources. Ideally this method would be static
827-
* but for now the core load path is an option and it also depends on the current working directory. Once we have
828-
* Source metadata in Truffle we could use that to identify core library sources without needing the context. */
829-
public String getSourcePath(Source source) {
830-
final String path = RubyContext.getPath(source);
831-
if (path.startsWith(coreLibrary.coreLoadPath)) {
832-
return "<internal:core> " + path.substring(coreLibrary.coreLoadPath.length() + 1);
833-
} else {
834-
return RubyContext.getPath(source);
835-
}
836-
}
837-
838805
public String getPathRelativeToHome(String path) {
839806
if (path.startsWith(rubyHome) && path.length() > rubyHome.length()) {
840807
return path.substring(rubyHome.length() + 1);
@@ -868,7 +835,7 @@ public static String fileLine(SourceSection section) {
868835
if (section == null) {
869836
return "no source section";
870837
} else {
871-
final String path = getPath(section.getSource());
838+
final String path = RubyLanguage.getPath(section.getSource());
872839

873840
if (section.isAvailable()) {
874841
return path + ":" + section.getStartLine();
@@ -883,7 +850,7 @@ public static String filenameLine(SourceSection section) {
883850
if (section == null) {
884851
return "no source section";
885852
} else {
886-
final String path = getPath(section.getSource());
853+
final String path = RubyLanguage.getPath(section.getSource());
887854
final String filename = new File(path).getName();
888855

889856
if (section.isAvailable()) {

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
package org.truffleruby;
1111

12+
import java.io.File;
13+
import java.io.IOException;
1214
import java.util.Arrays;
1315
import java.util.Objects;
1416
import java.util.concurrent.atomic.AtomicLong;
@@ -18,6 +20,7 @@
1820
import com.oracle.truffle.api.CompilerDirectives;
1921
import com.oracle.truffle.api.instrumentation.AllocationReporter;
2022
import com.oracle.truffle.api.object.Shape;
23+
import com.oracle.truffle.api.source.Source;
2124
import org.graalvm.options.OptionDescriptors;
2225
import org.jcodings.Encoding;
2326
import org.truffleruby.builtins.PrimitiveManager;
@@ -153,6 +156,8 @@ public final class RubyLanguage extends TruffleLanguage<RubyContext> {
153156
.getRuntime()
154157
.createAssumption("SafepointManager");
155158

159+
@CompilationFinal public String coreLoadPath;
160+
@CompilationFinal public String corePath;
156161
public final CoreMethodAssumptions coreMethodAssumptions;
157162
public final CoreStrings coreStrings;
158163
public final CoreSymbols coreSymbols;
@@ -280,6 +285,8 @@ public RubyContext createContext(Env env) {
280285
}
281286
if (this.options == null) {
282287
this.options = new LanguageOptions(env, env.getOptions());
288+
this.coreLoadPath = buildCoreLoadPath();
289+
this.corePath = coreLoadPath + File.separator + "core" + File.separator;
283290
primitiveManager.loadCoreMethodNodes(this.options);
284291
}
285292
}
@@ -481,4 +488,51 @@ protected boolean areOptionsCompatible(OptionValues firstOptions, OptionValues n
481488
return LanguageOptions.areOptionsCompatible(firstOptions, newOptions);
482489
}
483490

491+
/** {@link RubyLanguage#getSourcePath(Source)} should be used instead whenever possible (i.e., when we can access
492+
* the context).
493+
*
494+
* Returns the path of a Source. Returns the short, potentially relative, path for the main script. Note however
495+
* that the path of {@code eval(code, nil, filename)} is just {@code filename} and might not be absolute. */
496+
public static String getPath(Source source) {
497+
final String path = source.getPath();
498+
if (path != null) {
499+
return path;
500+
} else {
501+
// non-file sources: eval(), main_boot_source, etc
502+
final String name = source.getName();
503+
assert name != null;
504+
return name;
505+
}
506+
}
507+
508+
/** {@link RubyLanguage#getPath(Source)} but also handles core library sources. Ideally this method would be static
509+
* but for now the core load path is an option and it also depends on the current working directory. Once we have
510+
* Source metadata in Truffle we could use that to identify core library sources without needing the context. */
511+
public String getSourcePath(Source source) {
512+
final String path = getPath(source);
513+
if (path.startsWith(coreLoadPath)) {
514+
return "<internal:core> " + path.substring(coreLoadPath.length() + 1);
515+
} else {
516+
return getPath(source);
517+
}
518+
}
519+
520+
private String buildCoreLoadPath() {
521+
String path = options.CORE_LOAD_PATH;
522+
523+
while (path.endsWith("/")) {
524+
path = path.substring(0, path.length() - 1);
525+
}
526+
527+
if (path.startsWith(RubyLanguage.RESOURCE_SCHEME)) {
528+
return path;
529+
}
530+
531+
try {
532+
return new File(path).getCanonicalPath();
533+
} catch (IOException e) {
534+
throw CompilerDirectives.shouldNotReachHere(e);
535+
}
536+
}
537+
484538
}

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ public abstract static class SourceFileNode extends CoreMethodArrayArgumentsNode
767767
@Specialization
768768
protected RubyString sourceFile() {
769769
final SourceSection sourceSection = getTopUserSourceSection("rb_sourcefile");
770-
final String file = getContext().getSourcePath(sourceSection.getSource());
770+
final String file = getLanguage().getSourcePath(sourceSection.getSource());
771771

772772
return makeStringNode.executeMake(file, UTF8Encoding.INSTANCE, CodeRange.CR_UNKNOWN);
773773
}

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
package org.truffleruby.core;
1111

12-
import java.io.File;
1312
import java.io.IOException;
1413
import java.nio.file.FileVisitResult;
1514
import java.nio.file.Files;
@@ -253,9 +252,6 @@ public class CoreLibrary {
253252

254253
private final ConcurrentMap<String, Boolean> patchFiles;
255254

256-
public final String coreLoadPath;
257-
public final String corePath;
258-
259255
@TruffleBoundary
260256
private static SourceSection initCoreSourceSection() {
261257
final Source.SourceBuilder builder = Source.newBuilder(TruffleRuby.LANGUAGE_ID, "", "(core)");
@@ -271,24 +267,6 @@ private static SourceSection initCoreSourceSection() {
271267
return source.createUnavailableSection();
272268
}
273269

274-
private String buildCoreLoadPath() {
275-
String path = context.getOptions().CORE_LOAD_PATH;
276-
277-
while (path.endsWith("/")) {
278-
path = path.substring(0, path.length() - 1);
279-
}
280-
281-
if (path.startsWith(RubyLanguage.RESOURCE_SCHEME)) {
282-
return path;
283-
}
284-
285-
try {
286-
return new File(path).getCanonicalPath();
287-
} catch (IOException e) {
288-
throw CompilerDirectives.shouldNotReachHere(e);
289-
}
290-
}
291-
292270
private enum State {
293271
INITIALIZING,
294272
LOADING_RUBY_CORE,
@@ -302,8 +280,6 @@ private enum State {
302280
public CoreLibrary(RubyContext context, RubyLanguage language) {
303281
this.context = context;
304282
this.language = language;
305-
this.coreLoadPath = buildCoreLoadPath();
306-
this.corePath = coreLoadPath + File.separator + "core" + File.separator;
307283
this.node = SingletonClassNode.getUncached();
308284

309285
// Nothing in this constructor can use RubyContext.getCoreLibrary() as we are building it!
@@ -780,7 +756,7 @@ public void loadRubyCoreLibraryAndPostBoot() {
780756
state = State.LOADED;
781757
}
782758

783-
final RubySource source = loadCoreFile(coreLoadPath + file);
759+
final RubySource source = loadCoreFile(language.coreLoadPath + file);
784760
final RubyRootNode rootNode = context
785761
.getCodeLoader()
786762
.parse(source, ParserContext.TOP_LEVEL, null, null, true, node);

src/main/java/org/truffleruby/core/binding/BindingNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ protected Object sourceLocation(RubyBinding binding,
468468
return nil;
469469
} else {
470470
final RubyString file = makeStringNode.executeMake(
471-
getContext().getSourcePath(sourceSection.getSource()),
471+
getLanguage().getSourcePath(sourceSection.getSource()),
472472
UTF8Encoding.INSTANCE,
473473
CodeRange.CR_UNKNOWN);
474474
return createArray(new Object[]{ file, sourceSection.getStartLine() });

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ protected RubyString getCallerPath(Object feature,
308308
coreExceptions().loadError("cannot infer basepath", featureString, this));
309309
}
310310

311-
String sourcePath = getContext().getSourcePath(sourceSection.getSource());
311+
String sourcePath = getLanguage().getSourcePath(sourceSection.getSource());
312312

313313
sourcePath = getContext().getFeatureLoader().canonicalize(sourcePath);
314314

src/main/java/org/truffleruby/core/method/MethodNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ protected Object sourceLocation(RubyMethod method) {
211211
return nil;
212212
} else {
213213
RubyString file = makeStringNode.executeMake(
214-
getContext().getSourcePath(sourceSection.getSource()),
214+
getLanguage().getSourcePath(sourceSection.getSource()),
215215
UTF8Encoding.INSTANCE,
216216
CodeRange.CR_UNKNOWN);
217217
return createArray(new Object[]{ file, sourceSection.getStartLine() });

src/main/java/org/truffleruby/core/method/UnboundMethodNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ protected Object sourceLocation(RubyUnboundMethod unboundMethod) {
184184
return nil;
185185
} else {
186186
RubyString file = makeStringNode.executeMake(
187-
getContext().getSourcePath(sourceSection.getSource()),
187+
getLanguage().getSourcePath(sourceSection.getSource()),
188188
UTF8Encoding.INSTANCE,
189189
CodeRange.CR_UNKNOWN);
190190
Object[] objects = new Object[]{ file, sourceSection.getStartLine() };

src/main/java/org/truffleruby/core/module/ModuleNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ private Object getLocation(ConstantLookupResult lookupResult) {
11471147
return createEmptyArray();
11481148
} else {
11491149
final RubyString file = makeStringNode.executeMake(
1150-
getContext().getSourcePath(sourceSection.getSource()),
1150+
getLanguage().getSourcePath(sourceSection.getSource()),
11511151
UTF8Encoding.INSTANCE,
11521152
CodeRange.CR_UNKNOWN);
11531153
return createArray(new Object[]{ file, sourceSection.getStartLine() });

src/main/java/org/truffleruby/core/proc/ProcNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public abstract static class SourceLocationNode extends CoreMethodArrayArguments
241241
protected Object sourceLocation(RubyProc proc) {
242242
SourceSection sourceSection = proc.sharedMethodInfo.getSourceSection();
243243

244-
final String sourcePath = getContext().getSourcePath(sourceSection.getSource());
244+
final String sourcePath = getLanguage().getSourcePath(sourceSection.getSource());
245245
if (!sourceSection.isAvailable() || sourcePath.endsWith("/lib/truffle/truffle/cext.rb")) {
246246
return nil;
247247
} else {

0 commit comments

Comments
 (0)