Skip to content

Commit 7c19bf3

Browse files
committed
Remove from the autoload map after the autoload is resolved
1 parent 49ae2a4 commit 7c19bf3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ public void initialize(NativeConfiguration nativeConfiguration, TruffleNFIPlatfo
8282
}
8383

8484
public void addAutoload(RubyConstant autoloadConstant) {
85-
final String basename = basenameWithoutExtension(StringOperations.getString(autoloadConstant.getAutoloadPath()));
85+
final String autoloadPath = StringOperations.getString(autoloadConstant.getAutoloadPath());
86+
final String basename = basenameWithoutExtension(autoloadPath);
8687

8788
registeredAutoloadsLock.lock();
8889
try {
8990
final Map<String, RubyConstant> constants = ConcurrentOperations.getOrCompute(registeredAutoloads, basename, k -> new HashMap<>());
90-
constants.put(StringOperations.getString(autoloadConstant.getAutoloadPath()), autoloadConstant);
91+
constants.put(autoloadPath, autoloadConstant);
9192
} finally {
9293
registeredAutoloadsLock.unlock();
9394
}
@@ -117,6 +118,19 @@ public RubyConstant isAutoloadPath(String expandedPath) {
117118
return null;
118119
}
119120

121+
public void removeAutoload(RubyConstant constant) {
122+
final String autoloadPath = StringOperations.getString(constant.getAutoloadPath());
123+
final String basename = basenameWithoutExtension(autoloadPath);
124+
125+
registeredAutoloadsLock.lock();
126+
try {
127+
final Map<String, RubyConstant> constantsMap = registeredAutoloads.get(basename);
128+
constantsMap.remove(autoloadPath, constant);
129+
} finally {
130+
registeredAutoloadsLock.unlock();
131+
}
132+
}
133+
120134
private String basenameWithoutExtension(String path) {
121135
final String basename = new File(path).getName();
122136
int i = basename.lastIndexOf('.');

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ private boolean requireConsideringAutoload(String feature, String expandedPath,
130130

131131
boolean[] result = new boolean[1];
132132
Runnable require = () -> result[0] = doRequire(feature, expandedPath, pathString);
133-
getConstantNode.autoloadConstant(LexicalScope.IGNORE, autoloadConstant.getDeclaringModule(), autoloadConstant.getName(), autoloadConstant, lookupConstantNode, require);
133+
try {
134+
getConstantNode.autoloadConstant(LexicalScope.IGNORE, autoloadConstant.getDeclaringModule(), autoloadConstant.getName(), autoloadConstant, lookupConstantNode, require);
135+
} finally {
136+
featureLoader.removeAutoload(autoloadConstant);
137+
}
134138
return result[0];
135139
} else {
136140
return doRequire(feature, expandedPath, pathString);

0 commit comments

Comments
 (0)