Skip to content

Commit 45189cb

Browse files
committed
Let LoadService own system-level file sourcing
This adds a simpler path for classloader-based loading of files when we just want to get the system classloader resources shipped with JRuby. This will reduce the chance of someone using an improper classloader (e.g. JRubyLibrary might have loaded the wrong file from a JRubyClassLoader rather than the one shipped with JRuby) or stumbling on to a null classloader (as sometimes happened to the loads from javasupport.Java when running in a bootstrap classloader).
1 parent de3580a commit 45189cb

5 files changed

Lines changed: 12 additions & 6 deletions

File tree

core/src/main/java/org/jruby/Ruby.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ public JavaSupport loadJavaSupport() {
15871587
}
15881588

15891589
private void loadBundler() {
1590-
loadService.loadFromClassLoader(getClassLoader(), "jruby/bundler/startup.rb", false);
1590+
loadService.loadFromClassLoader("jruby/bundler/startup.rb", false);
15911591
}
15921592

15931593
@SuppressWarnings("ReturnValueIgnored")
@@ -1781,15 +1781,15 @@ private void initJavaSupport(ThreadContext context) {
17811781

17821782
private void initRubyKernel() {
17831783
// load Ruby parts of core
1784-
loadService.loadFromClassLoader(getClassLoader(), "jruby/kernel.rb", false);
1784+
loadService.loadFromClassLoader("jruby/kernel.rb", false);
17851785
}
17861786

17871787
private void initRubyPreludes() {
17881788
// We cannot load any .rb and debug new parser features
17891789
if (RubyInstanceConfig.DEBUG_PARSER) return;
17901790

17911791
// load Ruby parts of core
1792-
loadService.loadFromClassLoader(getClassLoader(), "jruby/preludes.rb", false);
1792+
loadService.loadFromClassLoader("jruby/preludes.rb", false);
17931793
}
17941794

17951795
public IRManager getIRManager() {

core/src/main/java/org/jruby/ext/bigdecimal/BigDecimalLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ public void load(Ruby runtime, boolean wrap) throws IOException {
4343
RubyBigDecimal.createBigDecimal(context);
4444

4545
// using load since this file does not exist in MRI
46-
loadService(context).loadFromClassLoader(Ruby.getClassLoader(), "jruby/bigdecimal.rb", false);
46+
loadService(context).loadFromClassLoader("jruby/bigdecimal.rb", false);
4747
}
4848
}// BigDecimalLibrary

core/src/main/java/org/jruby/ext/jruby/JRubyLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void load(Ruby runtime, boolean wrap) {
7777
var Object = objectClass(context);
7878

7979
// load Ruby parts of the 'jruby' library
80-
loadService(context).loadFromClassLoader(runtime.getJRubyClassLoader(), "jruby/jruby.rb", false);
80+
loadService(context).loadFromClassLoader("jruby/jruby.rb", false);
8181

8282
var JRuby = defineModule(context, "JRuby").
8383
defineMethods(context, JRubyLibrary.class).defineMethods(context, JRubyUtilLibrary.class);

core/src/main/java/org/jruby/javasupport/Java.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void load(Ruby runtime, boolean wrap) {
139139
RubyClass objectClass = (RubyClass) getProxyClass(context, java.lang.Object.class);
140140

141141
// load Ruby parts of the 'java' library
142-
loadService(context).loadFromClassLoader(Ruby.getClassLoader(), "jruby/java.rb", false);
142+
loadService(context).loadFromClassLoader("jruby/java.rb", false);
143143

144144
// rewire ArrayJavaProxy superclass to point at Object, so it inherits Object behaviors
145145
Access.getClass(context, "ArrayJavaProxy").

core/src/main/java/org/jruby/runtime/load/LoadService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,15 @@ public String[] getSuffixes() {
186186
protected final Map<String, JarFile> jarFiles = new HashMap<>(0);
187187

188188
protected final Ruby runtime;
189+
protected final ClassLoader systemClassLoader;
189190
protected LibrarySearcher librarySearcher;
190191

191192
protected String mainScript;
192193
protected String mainScriptPath;
193194

194195
public LoadService(Ruby runtime) {
195196
this.runtime = runtime;
197+
this.systemClassLoader = Ruby.getClassLoader();
196198
if (RubyInstanceConfig.DEBUG_LOAD_TIMINGS) {
197199
loadTimer = new TracingLoadTimer();
198200
} else {
@@ -383,6 +385,10 @@ public void load(String file, boolean wrap) {
383385
}
384386
}
385387

388+
public void loadFromClassLoader(String file, boolean wrap) {
389+
loadFromClassLoader(systemClassLoader, file, wrap);
390+
}
391+
386392
public void loadFromClassLoader(ClassLoader classLoader, String file, boolean wrap) {
387393
long startTime = loadTimer.startLoad("classloader:" + file);
388394
int currentLine = runtime.getCurrentLine();

0 commit comments

Comments
 (0)