Module: Furnace::AVM2::Tokens::TokenWithTraits

Included in:
ClassToken, ScriptToken
Defined in:
lib/furnace-avm2/source/declaration_tokens/token_with_traits.rb

Instance Method Summary collapse

Instance Method Details

#transform_trait(trait, options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/furnace-avm2/source/declaration_tokens/token_with_traits.rb', line 51

def transform_trait(trait, options)
  case trait.kind
  when :Method
    MethodToken.new(trait, options)
  when :Getter
    MethodToken.new(trait, options.merge(type: :getter))
  when :Setter
    MethodToken.new(trait, options.merge(type: :setter))
  when :Slot
    SlotToken.new(trait, options.merge(const: false))
  when :Const
    SlotToken.new(trait, options.merge(const: true))
  when :Class
    # nothing
  else
    CommentToken.new(trait, "%%#{trait.kind}", options)
  end
end

#transform_traits(origin, options) ⇒ Object



3
4
5
6
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/furnace-avm2/source/declaration_tokens/token_with_traits.rb', line 3

def transform_traits(origin, options)
  tokens = []

  vars, methods = origin.traits.partition do |trait|
    [:Class, :Slot, :Const].include? trait.kind
  end

  if options[:environment] == :class && options[:static]
    if origin.initializer_body
      initializer_decompiler = Furnace::AVM2::Decompiler.new(
            origin.initializer_body, options.merge(global_code: true))

      begin
        properties = initializer_decompiler.decompose_static_initializer
      rescue Exception => e
        # This error will be caught when decompiling the rest of the
        # static initializer code. Ignore it here.
      end

      static_initialization = initializer_decompiler.decompile

      options = options.merge(property_values: properties)
    end
  end

  tokens += vars.map { |trait| transform_trait trait, options }

  if tokens.any?
    tokens << Furnace::Code::NewlineToken.new(origin, options)
  end

  if static_initialization && static_initialization.children.any?
    tokens << CommentToken.new(origin,
                "Method ##{origin.initializer_body.method_idx}",
              options) if options[:debug_funids]
    tokens << static_initialization
    tokens << Furnace::Code::NewlineToken.new(origin, options)
  end

  if options[:environment] == :class && !options[:static]
    tokens << ConstructorToken.new(origin, options)
  end

  tokens += methods.map { |trait| transform_trait trait, options }

  tokens
end