Class: AppMap::Hook::Method

Inherits:
Object show all
Defined in:
lib/appmap/hook/method.rb,
lib/appmap/hook/method/ruby2.rb,
lib/appmap/hook/method/ruby3.rb

Overview

Delegation methods for Ruby 3.

Constant Summary collapse

HOOK_DISABLE_KEY =
'AppMap::Hook.disable'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hook_package, hook_class, hook_method) ⇒ Method

Returns a new instance of Method.



32
33
34
35
36
37
38
# File 'lib/appmap/hook/method.rb', line 32

def initialize(hook_package, hook_class, hook_method)
  @hook_package = hook_package
  @hook_class = hook_class
  @hook_method = hook_method
  @parameters = hook_method.parameters
  @arity = hook_method.arity
end

Instance Attribute Details

#arityObject (readonly)

Returns the value of attribute arity.



28
29
30
# File 'lib/appmap/hook/method.rb', line 28

def arity
  @arity
end

#hook_classObject (readonly)

Returns the value of attribute hook_class.



28
29
30
# File 'lib/appmap/hook/method.rb', line 28

def hook_class
  @hook_class
end

#hook_methodObject (readonly)

Returns the value of attribute hook_method.



28
29
30
# File 'lib/appmap/hook/method.rb', line 28

def hook_method
  @hook_method
end

#hook_packageObject (readonly)

Returns the value of attribute hook_package.



28
29
30
# File 'lib/appmap/hook/method.rb', line 28

def hook_package
  @hook_package
end

#parametersObject (readonly)

Returns the value of attribute parameters.



28
29
30
# File 'lib/appmap/hook/method.rb', line 28

def parameters
  @parameters
end

Instance Method Details

#activateObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/appmap/hook/method.rb', line 40

def activate
  if Hook::LOG
    msg = if method_display_name
            "#{method_display_name}"
          else
            "#{hook_method.name} (class resolution deferred)"
          end
    warn "AppMap: Hooking #{msg} at line #{(hook_method.source_location || []).join(':')}"
  end

  hook_method_parameters = hook_method.parameters.dup.freeze
  SIGNATURES[[ hook_class, hook_method.name ]] = hook_method_parameters

  # irb(main):001:0> Kernel.public_instance_method(:system)
  # (irb):1:in `public_instance_method': method `system' for module `Kernel' is  private (NameError)
  if hook_class == Kernel
    hook_class.define_method_with_arity(hook_method.name, hook_method.arity, hook_method_def)
  else
    cls = defining_class(hook_class)
    if cls
      cls.define_method_with_arity(hook_method.name, hook_method.arity, hook_method_def)
    end
  end
end

#call(receiver, *args, **kwargs, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/appmap/hook/method/ruby3.rb', line 9

def call(receiver, *args, **kwargs, &block)
  call_event = false
  if trace?
    call_event, elapsed_before = with_disabled_hook { before_hook receiver, *args, **kwargs }
  end
  # note we can't short-circuit directly to do_call because then the call stack
  # depth changes and eval handler doesn't work correctly
  trace_call call_event, elapsed_before, receiver, *args, **kwargs, &block
end