Class: Furnace::AVM2::ABC::Record

Inherits:
Binary::Record show all
Defined in:
lib/furnace-avm2/abc/primitives/record.rb

Instance Attribute Summary

Attributes inherited from Binary::Record

#root

Class Method Summary collapse

Methods inherited from Binary::Record

#byte_length, codegen, codegen_each, inherited, #inspect, method_missing, register, #to_hash, trace, trace_scope, trace_value

Class Method Details

.abc_array_of(name, type, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/furnace-avm2/abc/primitives/record.rb', line 3

def self.abc_array_of(name, type, options={})
  field_size, field_array = :"#{name}_count", options.delete(:plural) || :"#{name}s"
  klass = options.delete(:class)

  vuint30  field_size,  { :value   => lambda { send(field_array).count } }.merge(options)
  array    field_array, { :type    => type, :initial_length => field_size,
                          :options => { :class => klass } }.merge(options)
end

.flag(name, field, constant) ⇒ Object

Flags



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/furnace-avm2/abc/primitives/record.rb', line 119

def self.flag(name, field, constant)
  define_method(:"#{name}?") do
    instance_variable_get(:"@#{field}") & constant != 0
  end

  define_method(:"#{name}=") do |is_set|
    flags = instance_variable_get(:"@#{field}")

    if is_set
      instance_variable_set(:"@#{field}", flags | constant)
    else
      instance_variable_set(:"@#{field}", flags & ~constant)
    end
  end
end

.pool_array(pool, name, type, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/furnace-avm2/abc/primitives/record.rb', line 46

def self.pool_array(pool, name, type, options={})
  field, type_plural = :"#{name}_raw", options.delete(:plural) || :"#{type}s"

  array field, { :type => :vuint30 }.merge(options)

  if pool == :root
    define_method(name) do
      send(field).map do |element|
        root.send(type_plural)[element]
      end
    end
  elsif pool == :const
    define_method(name) do
      send(field).map do |element|
        if element == 0
          nil
        else
          root.constant_pool.send(type_plural)[element - 1]
        end
      end
    end
  end
end

.pool_array_of(pool, name, type, options = {}) ⇒ Object



70
71
72
73
74
75
# File 'lib/furnace-avm2/abc/primitives/record.rb', line 70

def self.pool_array_of(pool, name, type, options={})
  field_size, field_array = :"#{name}_count", options.delete(:plural) || :"#{name}s"

  vuint30          field_size,        { :value => lambda { send(field_array).count } }.merge(options)
  pool_array pool, field_array, type, { :initial_length => field_size }.merge(options)
end

.pool_ref(pool, name, type = name, options = {}) ⇒ Object

Pools



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/furnace-avm2/abc/primitives/record.rb', line 14

def self.pool_ref(pool, name, type=name, options={})
  field, array = :"#{name}_idx", options.delete(:plural) || :"#{type}s"

  vuint30 field, options

  if pool == :root
    define_method(name) do
      root.send(array)[send(field)]
    end
  elsif pool == :const
    define_method(name) do
      index = send(field)
      if index == 0
        nil
      else
        root.constant_pool.send(array)[index - 1]
      end
    end

    define_method(:"#{name}=") do |value|
      pool = root.constant_pool.send(array)
      if value.nil?
        send(:"#{field}=", 0)
      elsif index = pool.index(value)
        send(:"#{field}=", index + 1)
      else
        raise "cpool setter: no such object in cpool"
      end
    end
  end
end

.subset(name, array, selector) ⇒ Object

Subsetting



137
138
139
140
141
# File 'lib/furnace-avm2/abc/primitives/record.rb', line 137

def self.subset(name, array, selector)
  define_method(:"#{name}") do
    send(array).select &selector
  end
end

.xlat_directObject

Xlat



89
90
91
# File 'lib/furnace-avm2/abc/primitives/record.rb', line 89

def self.xlat_direct
  @xlat_direct  ||= const_get(:XlatTable).invert
end

.xlat_field(name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/furnace-avm2/abc/primitives/record.rb', line 97

def self.xlat_field(name)
  define_method(:"#{name}") do
    value = instance_variable_get :"@#{name}_raw"

    unless self.class.xlat_direct.has_key? value
      raise ArgumentError, "Unknown xlat value #{value} for #{self.class}"
    end

    self.class.xlat_direct[value]
  end

  define_method(:"#{name}=") do |new_value|
    unless self.class.xlat_inverse.has_key? new_value
      raise ArgumentError, "Unknown xlat identifier #{new_value} for #{self.class}"
    end

    instance_variable_set :"@#{name}_raw", self.class.xlat_inverse[new_value]
  end
end

.xlat_inverseObject



93
94
95
# File 'lib/furnace-avm2/abc/primitives/record.rb', line 93

def self.xlat_inverse
  @xlat_inverse ||= const_get(:XlatTable)
end