class JvmBytecode::Attributes::Code

Public Class Methods

new(cp) click to toggle source
Calls superclass method JvmBytecode::Attributes::Attribute::new
# File lib/jvm_bytecode/attributes/code.rb, line 12
def initialize(cp)
  super(cp)

  @max_stack = 0
  @max_locals = 0
  @instructions = []
end

Public Instance Methods

additional_bytecode() click to toggle source
# File lib/jvm_bytecode/attributes/code.rb, line 28
def additional_bytecode
  code = @instructions.map(&:bytecode).join('')
  [@max_stack, @max_locals, code.length].pack('S>2I>') + code + [0, 0].pack('S>2')
end
decode(io) click to toggle source
# File lib/jvm_bytecode/attributes/code.rb, line 33
def decode(io)
  io.read(4) # discard length

  @max_stack, @max_locals, len = io.read(8).unpack('S>2I>')
  
  @instructions.clear
  while len > 0
    opcode = io.read(1).unpack('C').first
    inst = Instructions::Instruction.fetch(opcode)
    @instructions << inst.new(cp).tap { |i| i.decode(io) }

    len -= inst.size
  end

  io.read(4) # discard exceptions and attributes
end
max_locals(n) click to toggle source
# File lib/jvm_bytecode/attributes/code.rb, line 24
def max_locals(n)
  @max_locals = n
end
max_stack(n) click to toggle source
# File lib/jvm_bytecode/attributes/code.rb, line 20
def max_stack(n)
  @max_stack = n
end
to_hash() click to toggle source
# File lib/jvm_bytecode/attributes/code.rb, line 50
def to_hash
  {
    type: self.class.name.split('::').last,
    max_stack: @max_stack,
    max_local_variables: @max_locals,
    instructions: @instructions.map(&:to_hash)
  }
end