Project

General

Profile

Actions

Feature #15777

closed

autoload?(cname, inherit=true)

Added by fxn (Xavier Noria) over 5 years ago. Updated over 5 years ago.

Status:
Closed
Target version:
-
[ruby-core:92336]

Description

Zeitwerk needs to be able to check if a given class or module has an autoload defined for a certain constant name, that would be autoload?(cname, false), similar to const_defined?(cname, false).

Actions #1

Updated by fxn (Xavier Noria) over 5 years ago

  • Description updated (diff)

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

Changing the signature of rb_autoload_p could break API compatibility.
I think we'll need another function for it.
Maybe rb_autoload_at_p like as rb_const_defined_at?

Updated by byroot (Jean Boussier) over 5 years ago

@nobu (Nobuyoshi Nakada) I edited the patch the way you suggested. Let me know if you desire further changes.

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

Is it necessary to export rb_autoload_at_p?
Do you have the plan to use it in extension libraries?

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

byroot (Jean Boussier) wrote:

Let me know if you desire further changes.

Fix the bug first.
In rb_autoload_at_p, this condition is wrong.

-    while (!autoload_defined_p(mod, id)) {
+    while (RTEST(recur) && autoload_defined_p(mod, id)) {

Leave it unchanged but return nil if not recur in that loop.

Updated by byroot (Jean Boussier) over 5 years ago

Is it necessary to export rb_autoload_at_p?
Do you have the plan to use it in extension libraries?

No I have no such plan. I did so because it's defined in variable.c but called from load.c so without that declaration it won't compile:

load.c:1180:12: error: implicit declaration of function 'rb_autoload_at_p' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    return rb_autoload_at_p(mod, id, recur);

I'm quite a MRI & C beginner though, so if there is a better way to do it I'll gladly change it.

@Eregon (Benoit Daloze) told me which place to declare internal functions. It's now in internal.h

return nil if not recur in that loop.

Done.

Updated by Eregon (Benoit Daloze) over 5 years ago

  • Assignee set to nobu (Nobuyoshi Nakada)

The new patch looks fine to me.
@nobu (Nobuyoshi Nakada) I'll let you merge it since you did most of the review.

Actions #9

Updated by byroot (Jean Boussier) over 5 years ago

  • Status changed from Open to Closed

Applied in changeset git|fb85a428605265a8fd449b0702a4dd88cb6f3b20.


Add an optional inherit argument to Module#autoload?

[Feature #15777]

Closes: https://github.com/ruby/ruby/pull/2173

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0