From a409e333a242a8c4984574e5e88dfce42cd993e4 Mon Sep 17 00:00:00 2001 From: Mike Blumtritt Date: Mon, 13 May 2024 05:37:36 +0000 Subject: [PATCH] Continue trace after printing vars (#4) - fix issue that trace stopped after printing vars of a traced object - minor speedup when generate a call signature - adapt README and example/foo.rb - bump version to v1.0.2 --- README.md | 67 ++++++++++++++++++++++++++---------------- examples/foo.rb | 67 ++++++++++++++++++++++++++---------------- lib/im-lost.rb | 36 +++++++++++++---------- lib/im-lost/version.rb | 2 +- 4 files changed, 106 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 796e754..b03b051 100644 --- a/README.md +++ b/README.md @@ -108,31 +108,48 @@ my_foo.foo(3) { puts _1 } ImLost.vars(my_foo) # output will look like -# > Foo.create(:foo!) -# /projects/foo.rb25 -# > Foo.new(*) -# /projects/foo.rb6 -# < Foo.new(*) -# = # -# < Foo.create(:foo!) -# = # -# > Foo#foo(1, *[], :none, **{}, &nil) -# /projects/foo.rb28 -# > Foo#bar() -# /projects/foo.rb15 -# < Foo#bar() -# = :bar -# < Foo#foo(1, *[], :none, **{}, &nil) -# = "1-none-[]-{}-bar" -# = /projects/foo.rb29 -# instance variables: -# @value: "1-none-[]-{}-bar" -# = /projects/foo.rb32 -# instance variables: -# @value: "2-some-[a,b,c]-{:name=>:value}-bar" -# = /projects/foo.rb35 -# instance variables: -# @value: "3--[]-{}-bar" +# > Foo.create(:foo!) +# /projects/foo.rb:25 +# > Foo.new(*) +# /projects/foo.rb:6 +# < Foo.new(*) +# = # +# < Foo.create(:foo!) +# = # +# > Foo#foo(1, *[], :none, **{}, &nil) +# /projects/foo.rb:28 +# > Foo#bar() +# /projects/foo.rb:15 +# < Foo#bar() +# = :bar +# < Foo#foo(1, *[], :none, **{}, &nil) +# = "1-none-[]-{}-bar" +# = /projects/foo.rb:29 +# instance variables: +# @value: "1-none-[]-{}-bar" +# > Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil) +# /projects/foo.rb:31 +# > Foo#bar() +# /projects/foo.rb:15 +# < Foo#bar() +# = :bar +# < Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil) +# = "2-some-[a,b,c]-{:name=>:value}-bar" +# = /projects/foo.rb:32 +# instance variables: +# @value: "2-some-[a,b,c]-{:name=>:value}-bar" +# > Foo#foo(3, *[], nil, **{}, &#) +# /projects/foo.rb:34 +# > Foo#bar() +# /projects/foo.rb:15 +# < Foo#bar() +# = :bar +# 3--[]-{}-bar +# < Foo#foo(3, *[], nil, **{}, &#) +# = nil +# = /projects/foo.rb:35 +# instance variables: +# @value: "3--[]-{}-bar" ``` See [examples dir](./examples) for moreā€¦ diff --git a/examples/foo.rb b/examples/foo.rb index 19e4004..27009aa 100644 --- a/examples/foo.rb +++ b/examples/foo.rb @@ -35,28 +35,45 @@ def bar = :bar ImLost.vars(my_foo) # output will look like -# > Foo.create(:foo!) -# /projects/foo.rb25 -# > Foo.new(*) -# /projects/foo.rb6 -# < Foo.new(*) -# = # -# < Foo.create(:foo!) -# = # -# > Foo#foo(1, *[], :none, **{}, &nil) -# /projects/foo.rb28 -# > Foo#bar() -# /projects/foo.rb15 -# < Foo#bar() -# = :bar -# < Foo#foo(1, *[], :none, **{}, &nil) -# = "1-none-[]-{}-bar" -# = /projects/foo.rb29 -# instance variables: -# @value: "1-none-[]-{}-bar" -# = /projects/foo.rb32 -# instance variables: -# @value: "2-some-[a,b,c]-{:name=>:value}-bar" -# = /projects/foo.rb35 -# instance variables: -# @value: "3--[]-{}-bar" +# > Foo.create(:foo!) +# /projects/foo.rb:25 +# > Foo.new(*) +# /projects/foo.rb:6 +# < Foo.new(*) +# = # +# < Foo.create(:foo!) +# = # +# > Foo#foo(1, *[], :none, **{}, &nil) +# /projects/foo.rb:28 +# > Foo#bar() +# /projects/foo.rb:15 +# < Foo#bar() +# = :bar +# < Foo#foo(1, *[], :none, **{}, &nil) +# = "1-none-[]-{}-bar" +# = /projects/foo.rb:29 +# instance variables: +# @value: "1-none-[]-{}-bar" +# > Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil) +# /projects/foo.rb:31 +# > Foo#bar() +# /projects/foo.rb:15 +# < Foo#bar() +# = :bar +# < Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil) +# = "2-some-[a,b,c]-{:name=>:value}-bar" +# = /projects/foo.rb:32 +# instance variables: +# @value: "2-some-[a,b,c]-{:name=>:value}-bar" +# > Foo#foo(3, *[], nil, **{}, &#) +# /projects/foo.rb:34 +# > Foo#bar() +# /projects/foo.rb:15 +# < Foo#bar() +# = :bar +# 3--[]-{}-bar +# < Foo#foo(3, *[], nil, **{}, &#) +# = nil +# = /projects/foo.rb:35 +# instance variables: +# @value: "3--[]-{}-bar" diff --git a/lib/im-lost.rb b/lib/im-lost.rb index c166124..0db728e 100644 --- a/lib/im-lost.rb +++ b/lib/im-lost.rb @@ -254,7 +254,7 @@ def vars(object) return unless object.respond_to?(:instance_variables) _vars(object, Kernel.caller_locations(1, 1)[0]) ensure - @trace[traced] = 1 if traced + @trace[traced] = traced if traced end protected @@ -272,30 +272,35 @@ def as_sig(prefix, info, args) private def _trace(arg) - @trace[arg.__id__] = 1 if self != arg && @output != arg + id = arg.__id__ + @trace[id] = id if __id__ != id && @output.__id__ != id arg end def _trace_all(args) args.each do |arg| - @trace[arg.__id__] = 1 if arg != self && @output != arg + arg = arg.__id__ + @trace[arg] = arg if __id__ != arg && @output.__id__ != arg end args end def _trace_b(arg) - @trace[id = arg.__id__] = 1 if self != arg && @output != arg - yield(arg) - ensure - @trace.delete(id) if id + id = arg.__id__ + return yield(arg) if __id__ == id || @output.__id__ == id + begin + @trace[id] = id + yield(arg) + ensure + @trace.delete(id) if id + end end def _trace_all_b(args) ids = args.filter_map do |arg| - next if self == arg || @output == arg - @trace[id = arg.__id__] = 1 - id + arg = arg.__id__ + @trace[arg] = arg if __id__ != arg && @output.__id__ != arg end yield(args) ensure @@ -332,9 +337,8 @@ def _local_vars(binding) end ARG_SIG = { rest: '*', keyrest: '**', block: '&' }.compare_by_identity.freeze - NO_NAME = %i[* ** &].freeze - EX_PREFIX = { raise: 'x', rescue: '!' }.freeze - private_constant :ARG_SIG, :NO_NAME, :EX_PREFIX + NO_NAME = { :* => 1, :** => 1, :& => 1 }.compare_by_identity.freeze + private_constant :ARG_SIG, :NO_NAME @trace = {}.compare_by_identity @caller_locations = true @@ -354,7 +358,7 @@ def _local_vars(binding) '>', tp, tp.parameters.map do |kind, name| - next name if NO_NAME.include?(name) + next name if NO_NAME.key?(name) "#{ARG_SIG[kind]}#{ctx.local_variable_get(name).inspect}" end ) @@ -392,7 +396,9 @@ def _local_vars(binding) @trace_exceptions = TracePoint.new(*supported) do |tp| ex = tp.raised_exception.inspect - @output.puts("#{EX_PREFIX[tp.event]} #{ex[0] == '#' ? ex[2..-2] : ex}") + @output.puts( + "#{tp.event == :raise ? 'x' : '!'} #{ex[0] == '#' ? ex[2..-2] : ex}" + ) @output.puts(" #{tp.path}:#{tp.lineno}") if @exception_locations end diff --git a/lib/im-lost/version.rb b/lib/im-lost/version.rb index e1744dc..7a01a3e 100644 --- a/lib/im-lost/version.rb +++ b/lib/im-lost/version.rb @@ -2,5 +2,5 @@ module ImLost # The version number of the gem. - VERSION = '1.0.1' + VERSION = '1.0.2' end