Module: RequireHooks::LoadIseq

Defined in:
lib/require-hooks/mode/load_iseq.rb

Instance Method Summary collapse

Instance Method Details

#load_iseq(path) ⇒ Object



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

def load_iseq(path)
  ctx = RequireHooks.context_for(path)

  # Early-return for non-trackable paths
  return if ctx.empty?

  ctx.run_around_load_callbacks(path) do
    iseq =
      if ctx.source_transform? || ctx.hijack?
        new_contents = ctx.perform_source_transform(path)
        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

    iseq ||= (defined?(super) ? super : RubyVM::InstructionSequence.compile_file(path))
    iseq.eval
    EMPTY_ISEQ
  end
end