Module: RequireHooks::Iseq

Defined in:
lib/require-hooks/iseq.rb

Class Method Summary collapse

Class Method Details

.compile_with_coverage(ctx, path) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/require-hooks/iseq.rb', line 6

def compile_with_coverage(ctx, path)
  iseq =
    if ctx.source_transform? || ctx.hijack?
      new_contents = ctx.perform_source_transform(path)

      RequireHooks.setup_path_coverage(path, new_contents)

      hijacked = ctx.try_hijack_load(path, new_contents)

      if hijacked
        raise TypeError, "Unsupported bytecode format for #{path}: #{hijack.class}" unless hijacked.is_a?(::RubyVM::InstructionSequence)
        hijacked
      elsif new_contents
        RubyVM::InstructionSequence.compile(new_contents, path, path, 1)
      end
    end

  RequireHooks.setup_path_coverage(path, new_contents)

  iseq ||= yield if block_given?

  iseq ||= RubyVM::InstructionSequence.compile_file(path)

  iseq
end