Class: AppMap::Hook::Method

Inherits:
Object show all
Includes:
RecordAround
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"

Constants included from RecordAround

RecordAround::APPMAP_OUTPUT_DIR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RecordAround

#record_around?, #record_around_after, #record_around_before

Constructor Details

#initialize(hook_package, hook_class, hook_method, record_around: false) ⇒ Method

Returns a new instance of Method.



43
44
45
46
47
48
49
50
# File 'lib/appmap/hook/method.rb', line 43

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

Instance Attribute Details

#arityObject (readonly)

Returns the value of attribute arity.



39
40
41
# File 'lib/appmap/hook/method.rb', line 39

def arity
  @arity
end

#hook_classObject (readonly)

Returns the value of attribute hook_class.



39
40
41
# File 'lib/appmap/hook/method.rb', line 39

def hook_class
  @hook_class
end

#hook_methodObject (readonly)

Returns the value of attribute hook_method.



39
40
41
# File 'lib/appmap/hook/method.rb', line 39

def hook_method
  @hook_method
end

#hook_packageObject (readonly)

Returns the value of attribute hook_package.



39
40
41
# File 'lib/appmap/hook/method.rb', line 39

def hook_package
  @hook_package
end

#parametersObject (readonly)

Returns the value of attribute parameters.



39
40
41
# File 'lib/appmap/hook/method.rb', line 39

def parameters
  @parameters
end

#record_aroundObject (readonly)

Returns the value of attribute record_around.



39
40
41
# File 'lib/appmap/hook/method.rb', line 39

def record_around
  @record_around
end

Instance Method Details

#activateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/appmap/hook/method.rb', line 52

def activate
  if HookLog.enabled?
    msg = if method_display_name
      "#{method_display_name}"
    else
      "#{hook_method.name} (class resolution deferred)"
    end
    HookLog.log "Hooking #{msg} at line #{(hook_method.source_location || []).join(":")}"
  end

  hook_method_parameters = hook_method.parameters.dup.freeze
  hash_key = Hook.method_hash_key(hook_class, hook_method)
  SIGNATURES[hash_key] = hook_method_parameters if hash_key

  # 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



12
13
14
15
16
17
18
19
20
21
# File 'lib/appmap/hook/method/ruby3.rb', line 12

def call(receiver, *args, **kwargs, &block)
  call_event = false
  record_around_before
  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