Class: Bootsnap::CompileCache::ISeq::Compiler

Inherits:
Object
  • Object
show all
Includes:
PatchRubyBug22023
Defined in:
lib/bootsnap/compile_cache/iseq.rb

Defined Under Namespace

Modules: PatchRubyBug22023

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PatchRubyBug22023

#compile_file, #compile_file_prism

Constructor Details

#initialize(namespace = nil, compile_options = nil) ⇒ Compiler

Returns a new instance of Compiler.



25
26
27
28
# File 'lib/bootsnap/compile_cache/iseq.rb', line 25

def initialize(namespace = nil, compile_options = nil)
  @namespace = namespace
  @compile_options = compile_options
end

Instance Attribute Details

#compile_optionsObject (readonly)

Returns the value of attribute compile_options.



23
24
25
# File 'lib/bootsnap/compile_cache/iseq.rb', line 23

def compile_options
  @compile_options
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



23
24
25
# File 'lib/bootsnap/compile_cache/iseq.rb', line 23

def namespace
  @namespace
end

Instance Method Details

#input_to_output(source, path, _kwargs) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/bootsnap/compile_cache/iseq.rb', line 96

def input_to_output(source, path, _kwargs)
  RubyVM::InstructionSequence.compile(
    source.force_encoding(Encoding.default_external),
    path,
    path,
    nil,
    @compile_options,
  )
end

#input_to_storage(_, path) ⇒ Object



69
70
71
72
73
74
# File 'lib/bootsnap/compile_cache/iseq.rb', line 69

def input_to_storage(_, path)
  iseq = RubyVM::InstructionSequence.compile_file(path, @compile_options)
  iseq.to_binary
rescue TypeError, SyntaxError # Ruby [Bug #18250] & [Bug #22023]
  UNCOMPILABLE
end

#storage_to_output(binary, _args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bootsnap/compile_cache/iseq.rb', line 83

def storage_to_output(binary, _args)
  iseq = RubyVM::InstructionSequence.load_from_binary(binary)
  binary.clear
  iseq
rescue RuntimeError => error
  if error.message == "broken binary format"
    $stderr.puts("[Bootsnap::CompileCache] warning: rejecting broken binary")
    nil
  else
    raise
  end
end