Skip to content

Commit

Permalink
Use the caller DefaultDefinee/cref for Kernel.{autoload,autoload?} li…
Browse files Browse the repository at this point in the history
…ke CRuby
  • Loading branch information
eregon committed Jun 12, 2023
1 parent 3c5d0d1 commit 7dceedd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/main/java/org/truffleruby/core/klass/ClassNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ protected Object getSuperClass(RubyClass rubyClass) {
}
}

@Primitive(name = "class_non_singleton_class")
public abstract static class NonSingletonClassNode extends PrimitiveArrayArgumentsNode {
@Specialization
protected RubyClass nonSingletonClass(RubyClass rubyClass) {
return rubyClass.nonSingletonClass;
}
}

@CoreMethod(names = { "__allocate__", "__layout_allocate__" }, constructor = true, visibility = Visibility.PRIVATE)
public abstract static class AllocateNode extends CoreMethodArrayArgumentsNode {
@Specialization
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/truffleruby/core/module/ModuleNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,20 @@ protected Object name(RubyModule module) {
}
}

@Primitive(name = "caller_declaration_context")
public abstract static class CallerDeclarationContextNode extends PrimitiveNode {
@TruffleBoundary
@Specialization
protected RubyModule callerDeclarationContext() {
var frame = getContext().getCallStack().getCallerFrame(FrameAccess.READ_ONLY);
if (frame == null) {
return coreLibrary().objectClass;
}
DeclarationContext declarationContext = RubyArguments.getDeclarationContext(frame.getArguments());
return declarationContext.getModuleToDefineMethods();
}
}

@Primitive(name = "caller_nesting")
public abstract static class CallerNestingNode extends PrimitiveArrayArgumentsNode {
@Specialization
Expand Down
7 changes: 3 additions & 4 deletions src/main/ruby/truffleruby/core/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ def abort(msg = nil)
module_function :abort

def autoload(name, file)
nesting = Primitive.caller_nesting
mod = nesting.first || Object
mod = Primitive.caller_declaration_context
mod = Primitive.class_non_singleton_class(mod) if Primitive.is_a?(mod, Class)
if Primitive.equal?(mod, self)
super(name, file) # Avoid recursion
else
Expand All @@ -213,8 +213,7 @@ def autoload(name, file)
module_function :autoload

def autoload?(name)
nesting = Primitive.caller_nesting
mod = nesting.first || Object
mod = Primitive.caller_declaration_context
if Primitive.equal?(mod, self)
super(name) # Avoid recursion
else
Expand Down

0 comments on commit 7dceedd

Please sign in to comment.