[前][次][番号順一覧][スレッド一覧]

ruby-changes:58625

From: Koichi <ko1@a...>
Date: Fri, 8 Nov 2019 09:09:49 +0900 (JST)
Subject: [ruby-changes:58625] e2a45cb984 (master): use builtin for TracePoint.

https://git.ruby-lang.org/ruby.git/commit/?id=e2a45cb984

From e2a45cb984ba75083a577b38ee9643800579a280 Mon Sep 17 00:00:00 2001
From: Koichi Sasada <ko1@a...>
Date: Thu, 7 Nov 2019 18:22:08 +0900
Subject: use builtin for TracePoint.

Define TracePoint in trace_point.rb and use __builtin_ syntax.

diff --git a/.document b/.document
index 2116ca6..64c56be 100644
--- a/.document
+++ b/.document
@@ -9,9 +9,10 @@ https://github.com/ruby/ruby/blob/trunk/.document#L9
 
 # prelude
 prelude.rb
-
 rbconfig.rb
 
+trace_point.rb
+
 # the lib/ directory (which has its own .document file)
 lib
 
diff --git a/common.mk b/common.mk
index 42d3223..40cef70 100644
--- a/common.mk
+++ b/common.mk
@@ -1094,11 +1094,14 @@ preludes: {$(VPATH)}prelude.c https://github.com/ruby/ruby/blob/trunk/common.mk#L1094
 preludes: {$(VPATH)}miniprelude.c
 preludes: {$(srcdir)}golf_prelude.c
 
-BUILTIN_RB_SRCS =
+BUILTIN_RB_SRCS = $(srcdir)/trace_point.rb
 
 builtin_binary.inc: $(PREP) $(BUILTIN_RB_SRCS) $(srcdir)/tool/mk_builtin_binary.rb
 	$(Q) $(MINIRUBY) $(srcdir)/tool/mk_builtin_binary.rb
 
+load_trace_point.inc: $(srcdir)/trace_point.rb $(srcdir)/tool/mk_builtin_loader.rb
+	$(Q) $(BASERUBY) $(srcdir)/tool/mk_builtin_loader.rb $(srcdir)/trace_point.rb
+
 $(srcdir)/revision.h:
 	$(Q)$(gnumake:yes=#) $(RM) $(@F)
 	$(Q)$(gnumake:yes=#) exit > $@ || exit > $(@F)
@@ -3389,6 +3392,7 @@ vm_trace.$(OBJEXT): $(CCAN_DIR)/str/str.h https://github.com/ruby/ruby/blob/trunk/common.mk#L3392
 vm_trace.$(OBJEXT): $(hdrdir)/ruby.h
 vm_trace.$(OBJEXT): $(hdrdir)/ruby/ruby.h
 vm_trace.$(OBJEXT): {$(VPATH)}assert.h
+vm_trace.$(OBJEXT): {$(VPATH)}builtin.h
 vm_trace.$(OBJEXT): {$(VPATH)}config.h
 vm_trace.$(OBJEXT): {$(VPATH)}debug.h
 vm_trace.$(OBJEXT): {$(VPATH)}debug_counter.h
@@ -3400,6 +3404,7 @@ vm_trace.$(OBJEXT): {$(VPATH)}intern.h https://github.com/ruby/ruby/blob/trunk/common.mk#L3404
 vm_trace.$(OBJEXT): {$(VPATH)}internal.h
 vm_trace.$(OBJEXT): {$(VPATH)}io.h
 vm_trace.$(OBJEXT): {$(VPATH)}iseq.h
+vm_trace.$(OBJEXT): {$(VPATH)}load_trace_point.inc
 vm_trace.$(OBJEXT): {$(VPATH)}method.h
 vm_trace.$(OBJEXT): {$(VPATH)}missing.h
 vm_trace.$(OBJEXT): {$(VPATH)}mjit.h
diff --git a/inits.c b/inits.c
index 11b5fa5..8a9789d 100644
--- a/inits.c
+++ b/inits.c
@@ -65,11 +65,12 @@ rb_call_inits(void) https://github.com/ruby/ruby/blob/trunk/inits.c#L65
     CALL(Rational);
     CALL(Complex);
     CALL(version);
-    CALL(vm_trace);
     CALL(vm_stack_canary);
     CALL(ast);
     CALL(gc_stress);
 
     CALL(builtin);
+
+    CALL(vm_trace);
 }
 #undef CALL
diff --git a/prelude.rb b/prelude.rb
index 4e23aba..7cc2d26 100644
--- a/prelude.rb
+++ b/prelude.rb
@@ -136,69 +136,6 @@ class IO https://github.com/ruby/ruby/blob/trunk/prelude.rb#L136
   end
 end
 
-class TracePoint
-  # call-seq:
-  #    trace.enable(target: nil, target_line: nil, target_thread: nil)    -> true or false
-  #    trace.enable(target: nil, target_line: nil, target_thread: nil) { block }  -> obj
-  #
-  # Activates the trace.
-  #
-  # Returns +true+ if trace was enabled.
-  # Returns +false+ if trace was disabled.
-  #
-  #   trace.enabled?  #=> false
-  #   trace.enable    #=> false (previous state)
-  #                   #   trace is enabled
-  #   trace.enabled?  #=> true
-  #   trace.enable    #=> true (previous state)
-  #                   #   trace is still enabled
-  #
-  # If a block is given, the trace will only be enabled within the scope of the
-  # block.
-  #
-  #    trace.enabled?
-  #    #=> false
-  #
-  #    trace.enable do
-  #      trace.enabled?
-  #      # only enabled for this block
-  #    end
-  #
-  #    trace.enabled?
-  #    #=> false
-  #
-  # +target+, +target_line+ and +target_thread+ parameters are used to
-  # limit tracing only to specified code objects. +target+ should be a
-  # code object for which RubyVM::InstructionSequence.of will return
-  # an instruction sequence.
-  #
-  #    t = TracePoint.new(:line) { |tp| p tp }
-  #
-  #    def m1
-  #      p 1
-  #    end
-  #
-  #    def m2
-  #      p 2
-  #    end
-  #
-  #    t.enable(target: method(:m1))
-  #
-  #    m1
-  #    # prints #<TracePoint:line@t...:5 in `m1'>
-  #    m2
-  #    # prints nothing
-  #
-  # Note: You cannot access event hooks within the +enable+ block.
-  #
-  #    trace.enable { p tp.lineno }
-  #    #=> RuntimeError: access from outside
-  #
-  def enable target: nil, target_line: nil, target_thread: nil, &blk
-    self.__enable target, target_line, target_thread, &blk
-  end
-end
-
 class Binding
   # :nodoc:
   def irb
diff --git a/test/ruby/test_settracefunc.rb b/test/ruby/test_settracefunc.rb
index 1138427..dba940b 100644
--- a/test/ruby/test_settracefunc.rb
+++ b/test/ruby/test_settracefunc.rb
@@ -468,7 +468,6 @@ class TestSetTraceFunc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_settracefunc.rb#L468
 
     answer_events = [
      #
-     [:c_return, 1, "xyzzy", TracePoint,  :trace,           TracePoint,  :outer,  trace],
      [:line,     4, 'xyzzy', self.class,  method,           self,        :outer, :nothing],
      [:c_call,   4, 'xyzzy', Integer,     :times,           1,           :outer, :nothing],
      [:line,     4, 'xyzzy', self.class,  method,           self,        nil,    :nothing],
@@ -517,12 +516,45 @@ class TestSetTraceFunc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_settracefunc.rb#L516
      [:c_call,  20, "xyzzy", Module,      :===,             RuntimeError,:outer, :nothing],
      [:c_return,20, "xyzzy", Module,      :===,             RuntimeError,:outer, true],
      [:line,    21, "xyzzy", TestSetTraceFunc, method,      self,        :outer, :nothing],
-     [:c_call,  21, "xyzzy", TracePoint,  :disable,         trace,       :outer, :nothing],
      ]
 
     return events, answer_events
   end
 
+  def test_tracepoint
+    events1, answer_events = *trace_by_tracepoint(:line, :class, :end, :call, :return, :c_call, :c_return, :raise)
+
+    ms = [events1, answer_events].map{|evs|
+      evs.map{|e|
+        "#{e[0]} - #{e[2]}:#{e[1]} id: #{e[4]}"
+      }
+    }
+
+    if false # show all events
+      printf(" %-60s | %-60s\n", "actual", "expected")
+      ms[0].zip(ms[1]){|a, b|
+        printf("%s%-60s | %-60s\n", a==b ? ' ' : '!', a, b)
+      }
+    end
+
+    mesg = ms[0].zip(ms[1]).map{|a, b|
+      if a != b
+        "actual: #{a} <-> expected: #{b}"
+      end
+    }.compact.join("\n")
+
+    answer_events.zip(events1){|answer, event|
+      assert_equal answer, event, mesg
+    }
+
+    [:line, :class, :end, :call, :return, :c_call, :c_return, :raise].each{|event|
+      events1, answer_events = *trace_by_tracepoint(event)
+      answer_events.find_all{|e| e[0] == event}.zip(events1){|answer_line, event_line|
+        assert_equal answer_line, event_line
+      }
+    }
+  end
+
   def trace_by_set_trace_func
     events = []
     trace = nil
@@ -530,6 +562,9 @@ class TestSetTraceFunc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_settracefunc.rb#L562
     xyzzy = nil
     xyzzy = xyzzy
     _local_var = :outer
+    method = :trace_by_set_trace_func
+    raised_exc = nil
+
     eval <<-EOF.gsub(/^.*?: /, ""), nil, 'xyzzy'
     1: set_trace_func(lambda{|event, file, line, id, binding, klass|
     2:   events << [event, line, file, klass, id, binding.eval('self'), binding.eval("_local_var")] if file == 'xyzzy'
@@ -554,44 +589,73 @@ class TestSetTraceFunc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_settracefunc.rb#L589
    21: set_trace_func(nil)
     EOF
     self.class.class_eval{remove_const(:XYZZY)}
-    return events
-  end
-
-  def test_tracepoint
-    events1, answer_events = *trace_by_tracepoint(:line, :class, :end, :call, :return, :c_call, :c_return, :raise)
-
-    ms = [events1, answer_events].map{|evs|
-      evs.map{|e|
-        "#{e[0]} - #{e[2]}:#{e[1]} id: #{e[4]}"
-      }
-    }
 
-    mesg = ms[0].zip(ms[1]).map{|a, b|
-      if a != b
-        "#{a} <-> #{b}"
-      end
-    }.compact.join("\n")
-
-    answer_events.zip(events1){|answer, event|
-      assert_equal answer, event, mesg
-    }
+    answer_events = [
+     #
+     [:c_return, 1, "xyzzy", TracePoint,  :trace,           TracePoint,  :outer,  trace],
+     [:line,     4, 'xyzzy', self.class,  method,           self,        :outer, :nothing],
+     [:c_call,   4, 'xyzzy', Integer,     :times,           1,           :outer, :nothing],
+     [:line,     4, 'xyzzy', self.class,  method,           self,        nil,    :nothing],
+     [:line,     5, 'xyzzy', self.class,  method,           self,        :inner, :nothing],
+     [:c_call,   5, 'xyzzy', Kernel,      :tap,             self,        :inner, :nothing],
+     [:c_return, 5, "xyzzy", Kernel,      :tap,             self,        :inner, self],
+     [:c_return, 4, "xyzzy", Integer,     :times,           1,           :outer, 1],
+     [:line,     7, 'xyzzy', self.class,  method,           self,        :outer, :nothing],
+     [:c_call,   7, "xyzzy", Class,       :inherited,       Object,      :outer, :nothing],
+     [:c_return, 7, "xyzzy", Class,       :inherited,       Object,      :outer, nil],
+     [:class,    7, "xyzzy", nil,         nil,              xyzzy.class, nil,    :nothing],
+     [:line,     8, "xyzzy", nil,         nil,              xyzzy.class, nil,    :nothing],
+     [:line,     9, "xyzzy", nil,         nil,              xyzzy.class, :XYZZY_outer, :nothing],
+     [:c_call,   9, "xyzzy", Module,      :method_added,    xyzzy.class, :XYZZY_outer, :nothing],
+     [:c_return, 9, "xyzzy", Module,      :method_added,    xyzzy.class, :XYZZY_outer, nil],
+     [:line,    13, "xyzzy", nil,         nil,              xyzzy.class, :XYZZY_outer, :nothing],
+     [:c_call,  13, "xyzzy", Module,      :method_added,    xyzzy.class, :XYZZY_outer, :nothing],
+     [:c_return,13, "xyzzy", Module,      :method_added,    xyzzy.class, :XYZZY_outer, nil],
+     [:end,     17, "xyzzy", nil,         nil,              xyzzy.class, :XYZZY_outer, (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]