Skip to content

support info consts ClassOrModule #855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/debug/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ def register_default_command
# * `info ivars <expr>` shows the instance variables of the result of `<expr>`.
# * `i[nfo] c or consts or constants`
# * Show information about accessible constants except toplevel constants.
# * `info consts <expr>` shows the constants of a class/module of the result of `<expr>`
# * `i[nfo] g or globals or global_variables`
# * Show information about global variables
# * `i[nfo] th or threads`
Expand Down Expand Up @@ -800,7 +801,7 @@ def register_default_command
when :ivars
request_tc [:show, :ivars, pat, opt]
when :consts
request_tc [:show, :consts, pat]
request_tc [:show, :consts, pat, opt]
when :globals
request_tc [:show, :globals, pat]
when :threads
Expand Down
56 changes: 37 additions & 19 deletions lib/debug/thread_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,6 @@ def show_locals pat
end

def show_ivars pat, expr = nil

if expr && !expr.empty?
_self = frame_eval(expr);
elsif _self = current_frame&.self
Expand All @@ -585,18 +584,39 @@ def show_ivars pat, expr = nil
end
end

def show_consts pat, only_self: false
if s = current_frame&.self
def iter_consts c, names = {}
c.constants(false).sort.each{|name|
next if names.has_key? name
names[name] = nil
begin
value = c.const_get(name)
rescue Exception => e
value = e
end
yield name, value
}
end

def get_consts expr = nil, only_self: false, &block
if expr && !expr.empty?
_self = frame_eval(expr)
if M_KIND_OF_P.bind_call(_self, Module)
iter_consts _self, &block
return
else
raise "#{_self.inspect} (by #{expr}) is not a Module."
end
elsif _self = current_frame&.self
cs = {}
if M_KIND_OF_P.bind_call(s, Module)
cs[s] = :self
if M_KIND_OF_P.bind_call(_self, Module)
cs[_self] = :self
else
s = M_CLASS.bind_call(s)
cs[s] = :self unless only_self
_self = M_CLASS.bind_call(_self)
cs[_self] = :self unless only_self
end

unless only_self
s.ancestors.each{|c| break if c == Object; cs[c] = :ancestors}
_self.ancestors.each{|c| break if c == Object; cs[c] = :ancestors}
if b = current_frame&.binding
b.eval('::Module.nesting').each{|c| cs[c] = :nesting unless cs.has_key? c}
end
Expand All @@ -605,20 +625,17 @@ def show_consts pat, only_self: false
names = {}

cs.each{|c, _|
c.constants(false).sort.each{|name|
next if names.has_key? name
names[name] = nil
begin
value = c.const_get(name)
rescue Exception => e
value = e
end
puts_variable_info name, value, pat
}
iter_consts c, names, &block
}
end
end

def show_consts pat, expr = nil, only_self: false
get_consts expr, only_self: only_self do |name, value|
puts_variable_info name, value, pat
end
end

SKIP_GLOBAL_LIST = %i[$= $KCODE $-K $SAFE].freeze
def show_globals pat
global_variables.sort.each{|name|
Expand Down Expand Up @@ -1105,7 +1122,8 @@ def wait_next_action_

when :consts
pat = args.shift
show_consts pat
expr = args.shift
show_consts pat, expr

when :globals
pat = args.shift
Expand Down