Class: JvmBytecode::ConstantPool

Inherits:
Object
  • Object
show all
Defined in:
lib/jvm_bytecode/constant_pool.rb

Instance Method Summary collapse

Constructor Details

#initializeConstantPool

Returns a new instance of ConstantPool.



5
6
7
# File 'lib/jvm_bytecode/constant_pool.rb', line 5

def initialize
  @constants = []
end

Instance Method Details

#add(const) ⇒ Object



53
54
55
# File 'lib/jvm_bytecode/constant_pool.rb', line 53

def add(const)
  @constants.push(const).length
end

#add_class(name) ⇒ Object



22
23
24
# File 'lib/jvm_bytecode/constant_pool.rb', line 22

def add_class(name)
  add(Constants::Class.new(index_or_utf8(name)))
end

#add_method_ref(class_index, name_and_type_index) ⇒ Object



32
33
34
# File 'lib/jvm_bytecode/constant_pool.rb', line 32

def add_method_ref(class_index, name_and_type_index)
  add(Constants::MethodRef.new(class_index, name_and_type_index))
end

#add_name_and_type(name, descriptor) ⇒ Object



26
27
28
29
30
# File 'lib/jvm_bytecode/constant_pool.rb', line 26

def add_name_and_type(name, descriptor)
  add(
    Constants::NameAndType.new(index_or_utf8(name), index_or_utf8(descriptor))
  )
end

#bytecodeObject



61
62
63
# File 'lib/jvm_bytecode/constant_pool.rb', line 61

def bytecode
  @constants.join_bytecodes { @constants.length + 1 }
end

#class_index(name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/jvm_bytecode/constant_pool.rb', line 36

def class_index(name)
  name_index = utf8(name)
  
  index = nil
  each_constants_of Constants::Class.tag do |klass, i|
    index = i if klass.name_index == name_index
  end

  index || raise(Errors::ConstantError, "Class constant named \"#{name}\" doesn't exist")
end

#constant(index) ⇒ Object



9
10
11
# File 'lib/jvm_bytecode/constant_pool.rb', line 9

def constant(index)
  @constants[index - 1]
end

#decode(io) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/jvm_bytecode/constant_pool.rb', line 65

def decode(io)
  entries = io.read(2).unpack('S>').first - 1

  @constants.clear
  entries.times do
    tag = io.read(1).unpack('C').first
    @constants << Constants::Constant.fetch(tag).decode(io)
  end
end

#each_constants_of(tag) ⇒ Object



47
48
49
50
51
# File 'lib/jvm_bytecode/constant_pool.rb', line 47

def each_constants_of(tag)
  @constants.each.with_index do |const, i|
    yield const, i + 1 if const.class.tag == tag
  end
end

#index_or_utf8(v) ⇒ Object



57
58
59
# File 'lib/jvm_bytecode/constant_pool.rb', line 57

def index_or_utf8(v)
  v.is_a?(String) ? utf8(v) : v.to_i
end

#to_hashObject



75
76
77
78
79
80
81
82
# File 'lib/jvm_bytecode/constant_pool.rb', line 75

def to_hash
  @constants.map.with_index do |const, i|
    { 
      index: i + 1,
      type: const.class.name.split('::').last.gsub('Constant', '')
    }.merge(const.to_hash)
  end
end

#utf8(str) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/jvm_bytecode/constant_pool.rb', line 13

def utf8(str)
  index = nil
  each_constants_of Constants::Utf8.tag do |utf8, i|
    index = i if utf8.to_s == str
  end

  index || add(Constants::Utf8.new(str))
end