Skip to content

Commit 7dceedd

Browse files
committed
Use the caller DefaultDefinee/cref for Kernel.{autoload,autoload?} like CRuby
1 parent 3c5d0d1 commit 7dceedd

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

Diff for: src/main/java/org/truffleruby/core/klass/ClassNodes.java

+8
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ protected Object getSuperClass(RubyClass rubyClass) {
335335
}
336336
}
337337

338+
@Primitive(name = "class_non_singleton_class")
339+
public abstract static class NonSingletonClassNode extends PrimitiveArrayArgumentsNode {
340+
@Specialization
341+
protected RubyClass nonSingletonClass(RubyClass rubyClass) {
342+
return rubyClass.nonSingletonClass;
343+
}
344+
}
345+
338346
@CoreMethod(names = { "__allocate__", "__layout_allocate__" }, constructor = true, visibility = Visibility.PRIVATE)
339347
public abstract static class AllocateNode extends CoreMethodArrayArgumentsNode {
340348
@Specialization

Diff for: src/main/java/org/truffleruby/core/module/ModuleNodes.java

+14
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,20 @@ protected Object name(RubyModule module) {
17441744
}
17451745
}
17461746

1747+
@Primitive(name = "caller_declaration_context")
1748+
public abstract static class CallerDeclarationContextNode extends PrimitiveNode {
1749+
@TruffleBoundary
1750+
@Specialization
1751+
protected RubyModule callerDeclarationContext() {
1752+
var frame = getContext().getCallStack().getCallerFrame(FrameAccess.READ_ONLY);
1753+
if (frame == null) {
1754+
return coreLibrary().objectClass;
1755+
}
1756+
DeclarationContext declarationContext = RubyArguments.getDeclarationContext(frame.getArguments());
1757+
return declarationContext.getModuleToDefineMethods();
1758+
}
1759+
}
1760+
17471761
@Primitive(name = "caller_nesting")
17481762
public abstract static class CallerNestingNode extends PrimitiveArrayArgumentsNode {
17491763
@Specialization

Diff for: src/main/ruby/truffleruby/core/kernel.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ def abort(msg = nil)
202202
module_function :abort
203203

204204
def autoload(name, file)
205-
nesting = Primitive.caller_nesting
206-
mod = nesting.first || Object
205+
mod = Primitive.caller_declaration_context
206+
mod = Primitive.class_non_singleton_class(mod) if Primitive.is_a?(mod, Class)
207207
if Primitive.equal?(mod, self)
208208
super(name, file) # Avoid recursion
209209
else
@@ -213,8 +213,7 @@ def autoload(name, file)
213213
module_function :autoload
214214

215215
def autoload?(name)
216-
nesting = Primitive.caller_nesting
217-
mod = nesting.first || Object
216+
mod = Primitive.caller_declaration_context
218217
if Primitive.equal?(mod, self)
219218
super(name) # Avoid recursion
220219
else

0 commit comments

Comments
 (0)