Class: Furnace::AVM2::Decompiler

Inherits:
Object
  • Object
show all
Includes:
Furnace::AST, ABC, Tokens
Defined in:
lib/furnace-avm2/source/decompiler.rb

Defined Under Namespace

Classes: ExpressionNotRecognized

Constant Summary collapse

StaticProperty =
Matcher.new do
  [ either[:set_property, :init_property],
    [:find_property, capture(:property)],
    backref(:property),
    capture(:value)
  ]
end
KnownPushScopeMatcher =
AST::Matcher.new do
  [:push_scope,
    either[
      [:get_local, capture(:get_local)],
      [:set_local, capture(:set_activation_local),
        [:new_activation]]
    ]
  ]
end
GetSlot =
Matcher.new do
  [:get_slot,
    capture(:index),
    either[
      [:get_scope_object, capture(:scope_pos)],
      [:get_global_scope]
    ]
  ]
end
IMMEDIATE_TYPE_MAP =
{
  :any       => '*',
  :integer   => 'int',
  :unsigned  => 'uint',
  :string    => 'String',
  :double    => 'Number',
  :object    => 'Object',
  :boolean   => 'Boolean',
  :true      => 'Boolean',
  :false     => 'Boolean',
}
XmlLiteralPreMatcher =
Matcher.new do
  [:coerce, [:q, "XML"], any]
end
SetSlot =
Matcher.new do
  [:set_slot,
    capture(:index),
    either[
      [:get_scope_object, capture(:scope_pos)],
      [:get_global_scope],
      [:push_scope,
        [:set_local, capture(:catch_local),
          [:new_catch, capture(:catch_id)]]]
    ],
    capture(:value)
  ]
end
ExceptionVariable =
Matcher.new do
  [:exception_variable, capture(:variable)]
end
INPLACE_OPERATOR_MAP =

Arithmetics

{
  :inc_local   => :"++",
  :dec_local   => :"--",
}
PSEUDO_OPERATOR_MAP =
{
  :increment   => [:"+", 1],
  :decrement   => [:"-", 1],
}
PrePostIncDecSlot =
Matcher.new do
  [any,
    capture(:index),
    either[
      [:get_global_scope],
      [:get_scope_object, capture(:scope_pos)]
    ]
  ]
end
OPERATOR_MAP =
{
  :and         => :"&&",
  :or          => :"||",

  :add         => :"+",
  :subtract    => :"-",
  :multiply    => :"*",
  :divide      => :"/",
  :modulo      => :"%",
  :multiply    => :"*",
  :negate      => :"-",

  :!           => :!,
  :>           => :>,
  :>=          => :>=,
  :<           => :<,
  :<=          => :<=,
  :==          => :==,
  :===         => :===,
  :!=          => :!=,
  :"!=="       => :"!==",

  :bit_and     => :"&",
  :bit_or      => :"|",
  :bit_xor     => :"^",
  :bit_not     => :"~",
  :lshift      => :"<<",
  :rshift      => :">>",
  :urshift     => :">>>",
}
This =

Properties and objects

Matcher.new do
  [:get_local, 0]
end
PropertyGlobal =
Matcher.new do
  [any,
    either_multi[
      [
        [ either[:find_property, :find_property_strict],
              capture(:multiname)],
        backref(:multiname),
      ],
      [
        [:find_property_strict,
          [:m, [:set, any], any]],
        capture(:multiname)
      ],
      [
        either[
          [:get_scope_object, capture(:scope)],
          [:get_global_scope]
        ],
        capture(:multiname)
      ],
    ],
    capture_rest(:arguments)]
end
CallThisGlobal =

See /src/java/macromedia/asc/semantics/CodeGenerator.java If this looks stupid to you, that’s because it IS stupid.

Matcher.new do
  [:get_global_scope]
end
CallThisLocal =
Matcher.new do
  [ either[:get_scope_object, :get_local], 0 ]
end
XmlLiteralMatcher =

FFFUUUUUUUUUU~~~

Matcher.new do
  [:coerce, [:q, "XML"],
    [:construct,
      either[
        [:get_lex, [:q, "XML"]],
        [:get_property,
          [:find_property_strict, [:q, "XML"]], [:q, "XML"]]
      ],
      capture(:body),
    ]
  ]
end
ALCHEMY_BINARY_MAP =

Alchemy opcodes

{
  :alchemy_store_int8    => AS3AlchemyStoreInt8,
  :alchemy_store_int16   => AS3AlchemyStoreInt16,
  :alchemy_store_int32   => AS3AlchemyStoreInt32,
  :alchemy_store_float32 => AS3AlchemyStoreFloat32,
  :alchemy_store_float64 => AS3AlchemyStoreFloat64
}
ALCHEMY_UNARY_MAP =
{
  :alchemy_load_int8    => AS3AlchemyLoadInt8,
  :alchemy_load_int16   => AS3AlchemyLoadInt16,
  :alchemy_load_int32   => AS3AlchemyLoadInt32,

  :alchemy_extend1      => AS3AlchemyExtend1,
  :alchemy_extend8      => AS3AlchemyExtend8,
  :alchemy_extend16     => AS3AlchemyExtend16
}
ALCHEMY_FLOAT_MAP =
{
  :alchemy_load_float32 => AS3AlchemyLoadFloat32,
  :alchemy_load_float64 => AS3AlchemyLoadFloat64
}

Constants included from ABC

ABC::AST, ABC::CFG

Instance Method Summary collapse

Constructor Details

#initialize(body, options) ⇒ Decompiler

Returns a new instance of Decompiler.



15
16
17
18
19
20
21
22
# File 'lib/furnace-avm2/source/decompiler.rb', line 15

def initialize(body, options)
  @body, @method, @options = body, body.method, options.dup
  @closure = @options.delete(:closure)

  @scopes       = []
  @metascopes   = []
  @catch_scopes = {}
end

Instance Method Details

#decompileObject



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/furnace-avm2/source/decompiler.rb', line 24

def decompile
  begin
    @locals = Set.new([0]) + (1..@method.param_count).to_a

    @closure_locals = Set.new

    @closure_slots  = {}
    @body.slot_traits.each do |trait|
      @closure_slots[trait.idx] = trait
    end

    @global_slots = @options[:global_slots] || {}

    @scopes << :this if @options[:global_code]

    stmt_block (@nf || @body.code_to_nf),
      function: !@options[:global_code],
      closure:  @closure

  rescue Exception => e
    @failed = true

    comment = "'Ouch!' cried I, then died.\n" +
      "#{e.class}: #{e.message}\n" +
      "#{e.backtrace[0..5].map { |l| "    #{l}\n"}.join}" +
      "      ... et cetera\n"

    token(ScopeToken, [
      token(CommentToken, comment)
    ], function: !@options[:global_code],
       closure:  @closure)

  ensure
    if stat = @options[:stat]
      stat[:total] += 1

      if @failed
        stat[:failed]  += 1
      elsif @partial
        stat[:partial] += 1
      else
        stat[:success] += 1
      end
    end
  end
end

#decompose_static_initializerObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/furnace-avm2/source/decompiler.rb', line 79

def decompose_static_initializer
  properties = {}
  matches = []

  @nf = @body.code_to_nf

  StaticProperty.find_all(@nf.children) do |match, captures|
    matches.push match

    begin
      token = handle_expression(captures[:value])
    rescue ExpressionNotRecognized => e
      token = token(CommentToken, "Unrecognized static initializer:\n#{e.opcode.inspect}")
    end

    properties[captures[:property]] = token
  end

  @nf.children -= matches

  properties
end

#expr_alchemy_binary_asm(node) ⇒ Object Also known as: expr_alchemy_store_int8, expr_alchemy_store_int16, expr_alchemy_store_int32, expr_alchemy_store_float32, expr_alchemy_store_float64



1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
# File 'lib/furnace-avm2/source/decompiler.rb', line 1142

def expr_alchemy_binary_asm(node)
  value, address = node.children
  opcode = ALCHEMY_BINARY_MAP[node.type]

  token(CallToken, [
    token(AsmToken),
    token(ArgumentsToken, [
      token(AsmPushToken, [ expr(value) ]),
      token(AsmPushToken, [ expr(address) ]),
      token(SupplementaryCommentToken, node.type.to_s, [
        token(AsmOpToken, opcode)
      ])
    ])
  ])
end

#expr_alchemy_float_asm(node) ⇒ Object Also known as: expr_alchemy_load_float32, expr_alchemy_load_float64



1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
# File 'lib/furnace-avm2/source/decompiler.rb', line 1203

def expr_alchemy_float_asm(node)
  value, = node.children
  opcode = ALCHEMY_FLOAT_MAP[node.type]

  token(CallToken, [
    token(XAsmToken, [
      token(ImmediateTypenameToken, "Number")
    ]),
    token(ArgumentsToken, [
      token(AsmPushToken, [ expr(value) ]),
      token(SupplementaryCommentToken, node.type.to_s, [
        token(AsmOpToken, opcode)
      ])
    ])
  ])
end

#expr_alchemy_unary_asm(node) ⇒ Object Also known as: expr_alchemy_load_int8, expr_alchemy_load_int16, expr_alchemy_load_int32, expr_alchemy_extend1, expr_alchemy_extend8, expr_alchemy_extend16



1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
# File 'lib/furnace-avm2/source/decompiler.rb', line 1174

def expr_alchemy_unary_asm(node)
  value, = node.children
  opcode = ALCHEMY_UNARY_MAP[node.type]

  token(CallToken, [
    token(XAsmToken, [
      token(ImmediateTypenameToken, "uint")
    ]),
    token(ArgumentsToken, [
      token(AsmPushToken, [ expr(value) ]),
      token(SupplementaryCommentToken, node.type.to_s, [
        token(AsmOpToken, opcode)
      ])
    ])
  ])
end

#expr_apply_type(opcode) ⇒ Object



1041
1042
1043
1044
1045
1046
1047
1048
# File 'lib/furnace-avm2/source/decompiler.rb', line 1041

def expr_apply_type(opcode)
  base, *args = opcode.children
  token(GenericTypeToken, [
    expr(base),
    token(GenericSpecializersToken,
      exprs(args))
  ])
end

#expr_arithmetic(opcode) ⇒ Object Also known as: expr_and, expr_or, expr_add, expr_subtract, expr_multiply, expr_divide, expr_modulo, expr_negate, expr_!, expr_>, expr_>=, expr_<, expr_<=, expr_==, expr_===, expr_!=, expr_!==, expr_bit_and, expr_bit_or, expr_bit_xor, expr_bit_not, expr_lshift, expr_rshift, expr_urshift



777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'lib/furnace-avm2/source/decompiler.rb', line 777

def expr_arithmetic(opcode)
  if OPERATOR_MAP.include?(opcode.type)
    insides = parenthesize_each(exprs(opcode.children))

    if insides.count == 1
      token(UnaryOperatorToken, insides.first, OPERATOR_MAP[opcode.type])
    elsif insides.count == 2
      token(BinaryOperatorToken, insides, OPERATOR_MAP[opcode.type])
    else
      token(CommentToken, "Unexpected #{insides.count}-ary operator")
    end
  end
end

#expr_as_type_late(opcode) ⇒ Object

Types



1025
1026
1027
# File 'lib/furnace-avm2/source/decompiler.rb', line 1025

def expr_as_type_late(opcode)
  token(AsToken, parenthesize_each(exprs(opcode.children)))
end

#expr_call(opcode) ⇒ Object



995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'lib/furnace-avm2/source/decompiler.rb', line 995

def expr_call(opcode)
  subject, this, *args = opcode.children

  subject_token = token(AccessToken, [
    parenthesize(expr(subject)),
    token(PropertyNameToken, "call")
  ])

  if CallThisGlobal.match(this) || CallThisLocal.match(this)
    # FUCK YOU!
    token(CallToken, [
      subject_token,
      token(ArgumentsToken, [
        local_token(0),
        *exprs(args)
      ])
    ])
  else
    token(CallToken, [
      subject_token,
      token(ArgumentsToken, exprs([
        this,
        *args
      ]))
    ])
  end
end

#expr_call_property(opcode) ⇒ Object Also known as: expr_call_property_lex



940
941
942
# File 'lib/furnace-avm2/source/decompiler.rb', line 940

def expr_call_property(opcode)
  expr_do_property(opcode, CallToken, true)
end

#expr_call_super(opcode) ⇒ Object



945
946
947
948
949
950
951
952
953
# File 'lib/furnace-avm2/source/decompiler.rb', line 945

def expr_call_super(opcode)
  subject, multiname, *args = opcode.children
  if This.match subject
    token(CallToken, [
      get_name(token(SuperToken), multiname),
      token(ArgumentsToken, exprs(args))
    ])
  end
end

#expr_check_filter(node) ⇒ Object



1127
1128
1129
1130
# File 'lib/furnace-avm2/source/decompiler.rb', line 1127

def expr_check_filter(node)
  content, = node.children
  expr(content)
end

#expr_coerce(opcode) ⇒ Object



1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
# File 'lib/furnace-avm2/source/decompiler.rb', line 1090

def expr_coerce(opcode)
  if captures = XmlLiteralMatcher.match(opcode)
    # Oh, shit...
    token(XmlLiteralToken,
      xml_expr(captures[:body]))
  else
    typename, subject, = opcode.children
    expr(subject)
  end
end

#expr_construct(opcode) ⇒ Object



961
962
963
964
965
966
967
# File 'lib/furnace-avm2/source/decompiler.rb', line 961

def expr_construct(opcode)
  type, *args = opcode.children
  token(NewToken, [
    parenthesize(expr(type)),
    token(ArgumentsToken, exprs(args))
  ])
end

#expr_construct_property(opcode) ⇒ Object

Object creation



957
958
959
# File 'lib/furnace-avm2/source/decompiler.rb', line 957

def expr_construct_property(opcode)
  expr_do_property(opcode, NewToken, true)
end

#expr_construct_super(opcode) ⇒ Object



969
970
971
972
973
974
975
976
977
# File 'lib/furnace-avm2/source/decompiler.rb', line 969

def expr_construct_super(opcode)
  subject, *args = opcode.children
  if This.match subject
    token(CallToken, [
      token(SuperToken),
      token(ArgumentsToken, exprs(args))
    ])
  end
end

#expr_convert(opcode) ⇒ Object



1054
1055
1056
1057
1058
1059
1060
1061
1062
# File 'lib/furnace-avm2/source/decompiler.rb', line 1054

def expr_convert(opcode)
  type, subject = opcode.children
  token(CallToken, [
    token(ImmediateTypenameToken, IMMEDIATE_TYPE_MAP[type]),
    token(ArgumentsToken, [
      expr(subject)
    ])
  ])
end

#expr_delete_property(opcode) ⇒ Object



936
937
938
# File 'lib/furnace-avm2/source/decompiler.rb', line 936

def expr_delete_property(opcode)
  expr_do_property(opcode, DeleteToken, false)
end

#expr_do_property(opcode, klass, has_args) ⇒ Object



920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
# File 'lib/furnace-avm2/source/decompiler.rb', line 920

def expr_do_property(opcode, klass, has_args)
  if captures = PropertyGlobal.match(opcode)
    return if !captures[:multiname] && !pseudo_global_scope?(captures[:scope])
    token(klass, [
      get_name(nil, captures[:multiname]),
      (token(ArgumentsToken, exprs(captures[:arguments])) if has_args)
    ])
  else
    subject, multiname, *args = opcode.children
    token(klass, [
      get_name(expr(subject), multiname),
      (token(ArgumentsToken, exprs(args)) if has_args)
    ])
  end
end

#expr_get_lex(opcode) ⇒ Object



857
858
859
860
# File 'lib/furnace-avm2/source/decompiler.rb', line 857

def expr_get_lex(opcode)
  multiname, = opcode.children
  get_name(nil, multiname)
end

#expr_get_local(opcode) ⇒ Object



456
457
458
459
# File 'lib/furnace-avm2/source/decompiler.rb', line 456

def expr_get_local(opcode)
  index, = opcode.children
  local_token(index)
end

#expr_get_property(opcode) ⇒ Object



862
863
864
865
866
867
868
869
870
# File 'lib/furnace-avm2/source/decompiler.rb', line 862

def expr_get_property(opcode)
  if captures = PropertyGlobal.match(opcode)
    return if !captures[:multiname] && !pseudo_global_scope?(captures[:scope])
    get_name(nil, captures[:multiname])
  else
    subject, multiname, = opcode.children
    get_name(expr(subject), multiname)
  end
end

#expr_get_slot(opcode) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/furnace-avm2/source/decompiler.rb', line 471

def expr_get_slot(opcode)
  if captures = GetSlot.match(opcode)
    scope = @scopes[captures[:scope_pos] || 0]

    if scope.is_a? Hash
      # treat as an inline scope, probably from an eh
      if scope[captures[:index]]
        var = scope[captures[:index]]
        token(VariableNameToken, var.[:origin].name)
      end
    elsif @closure_slots && scope == :activation
      # treat as a local variable
      slot = @closure_slots[captures[:index]]
      token(VariableNameToken, slot.name.name)
    elsif scope == :this
      # treat as a global property
      if slot = @global_slots[captures[:index]]
        get_name(nil, slot.name.to_astlet)
      else
        token(PropertyNameToken,
          "$__GLOBAL_#{captures[:index]}")
      end
    end
  end
end

#expr_get_super(opcode) ⇒ Object



872
873
874
875
876
877
878
879
880
881
882
883
884
# File 'lib/furnace-avm2/source/decompiler.rb', line 872

def expr_get_super(opcode)
  subject, multiname, = opcode.children

  stmt = get_name(token(SuperToken), multiname)

  unless This.match subject
    stmt = token(SupplementaryCommentToken,
      "subject != this: #{subject.inspect}",
      [ stmt ])
  end

  stmt
end

#expr_imm(opcode) ⇒ Object Also known as: expr_integer, expr_double, expr_null, expr_undefined, expr_true, expr_false

Immediates



396
397
398
399
400
401
402
403
# File 'lib/furnace-avm2/source/decompiler.rb', line 396

def expr_imm(opcode)
  case opcode.type
  when :integer, :double
    token(ImmediateToken, *opcode.children)
  when :null, :undefined, :true, :false
    token(ImmediateToken, opcode.type)
  end
end

#expr_in(opcode) ⇒ Object



819
820
821
# File 'lib/furnace-avm2/source/decompiler.rb', line 819

def expr_in(opcode)
  token(InToken, parenthesize_each(exprs(opcode.children)))
end

#expr_inplace_arithmetic(opcode) ⇒ Object Also known as: expr_inc_local, expr_dec_local



662
663
664
665
666
667
668
# File 'lib/furnace-avm2/source/decompiler.rb', line 662

def expr_inplace_arithmetic(opcode)
  index, = opcode.children

  token(UnaryPostOperatorToken,
    local_token(index),
    INPLACE_OPERATOR_MAP[opcode.type])
end

#expr_instance_of(opcode) ⇒ Object



1033
1034
1035
# File 'lib/furnace-avm2/source/decompiler.rb', line 1033

def expr_instance_of(opcode)
  token(InstanceOfToken, parenthesize_each(exprs(opcode.children)))
end

#expr_is_type_late(opcode) ⇒ Object



1029
1030
1031
# File 'lib/furnace-avm2/source/decompiler.rb', line 1029

def expr_is_type_late(opcode)
  token(IsToken, parenthesize_each(exprs(opcode.children)))
end

#expr_nan(opcode) ⇒ Object



416
417
418
# File 'lib/furnace-avm2/source/decompiler.rb', line 416

def expr_nan(opcode)
  token(ImmediateToken, "NaN")
end

#expr_new_array(opcode) ⇒ Object



420
421
422
# File 'lib/furnace-avm2/source/decompiler.rb', line 420

def expr_new_array(opcode)
  token(ArrayToken, exprs(opcode.children))
end

#expr_new_class(opcode) ⇒ Object



1050
1051
1052
# File 'lib/furnace-avm2/source/decompiler.rb', line 1050

def expr_new_class(opcode)
  throw :skip
end

#expr_new_function(opcode) ⇒ Object

Closures



1066
1067
1068
1069
1070
1071
1072
# File 'lib/furnace-avm2/source/decompiler.rb', line 1066

def expr_new_function(opcode)
  index, = opcode.children
  body = @method.root.method_body_at(index)

  token(ClosureToken,
    body)
end

#expr_new_object(opcode) ⇒ Object



424
425
426
427
428
429
430
431
432
# File 'lib/furnace-avm2/source/decompiler.rb', line 424

def expr_new_object(opcode)
  token(ObjectToken, opcode.children.
    each_slice(2).map do |key, value|
      token(ObjectPairToken, [
        expr(key),
        expr(value)
      ])
    end)
end

#expr_prepost_incdec_local(opcode) ⇒ Object Also known as: expr_post_increment_local, expr_post_decrement_local, expr_pre_increment_local, expr_pre_decrement_local



692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/furnace-avm2/source/decompiler.rb', line 692

def expr_prepost_incdec_local(opcode)
  index, = opcode.children
  lvar = local_token(index)

  if opcode.type == :post_increment_local
    token(UnaryPostOperatorToken, lvar, "++")
  elsif opcode.type == :post_decrement_local
    token(UnaryPostOperatorToken, lvar, "--")
  elsif opcode.type == :pre_increment_local
    token(UnaryOperatorToken, lvar, "++")
  elsif opcode.type == :pre_decrement_local
    token(UnaryOperatorToken, lvar, "--")
  end
end

#expr_prepost_incdec_slot(opcode) ⇒ Object Also known as: expr_post_increment_slot, expr_post_decrement_slot, expr_pre_increment_slot, expr_pre_decrement_slot



721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# File 'lib/furnace-avm2/source/decompiler.rb', line 721

def expr_prepost_incdec_slot(opcode)
  if captures = PrePostIncDecSlot.match(opcode)
    scope = @scopes[captures[:scope_pos] || 0]

    if @closure_slots && scope == :activation
      slot_trait = @closure_slots[captures[:index]]
      slot = token(VariableNameToken, slot_trait.name.name)

      if opcode.type == :post_increment_slot
        token(UnaryPostOperatorToken, slot, "++")
      elsif opcode.type == :post_decrement_slot
        token(UnaryPostOperatorToken, slot, "--")
      elsif opcode.type == :pre_increment_slot
        token(UnaryOperatorToken, slot, "++")
      elsif opcode.type == :pre_decrement_slot
        token(UnaryOperatorToken, slot, "--")
      end
    end
  end
end

#expr_pseudo_arithmetic(opcode) ⇒ Object Also known as: expr_increment, expr_decrement



678
679
680
681
682
683
684
685
686
687
# File 'lib/furnace-avm2/source/decompiler.rb', line 678

def expr_pseudo_arithmetic(opcode)
  inside = expr(*opcode.children)
  inside = token(ParenthesesToken, [inside]) if inside.complex?

  operator, other_value = PSEUDO_OPERATOR_MAP[opcode.type]
  token(BinaryOperatorToken, [
    inside,
    token(ImmediateToken, other_value)
  ], operator)
end

#expr_set_local(opcode, toplevel = false) ⇒ Object



556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/furnace-avm2/source/decompiler.rb', line 556

def expr_set_local(opcode, toplevel=false)
  index, value = opcode.children
  if IMMEDIATE_TYPE_MAP.include?(value.type)
    type = token(TypeToken, [
      token(ImmediateTypenameToken, IMMEDIATE_TYPE_MAP[value.type])
    ])
  elsif XmlLiteralPreMatcher.match value
    # XML literals work through expr_coerce
    type  = type_token(value.children.first)
    value = value
  elsif value.type == :coerce || value.type == :convert
    # Don't emit spurious coercion; typed variables already
    # imply it
    type  = type_token(value.children.first)
    value = value.children.last
  end

  expr_set_var(local_token(index), value, type, !@locals.include?(index), toplevel)
ensure
  @locals.add index if index
end

#expr_set_local_toplevel(opcode) ⇒ Object



578
579
580
# File 'lib/furnace-avm2/source/decompiler.rb', line 578

def expr_set_local_toplevel(opcode)
  expr_set_local(opcode, true)
end

#expr_set_property(opcode) ⇒ Object Also known as: expr_init_property



886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
# File 'lib/furnace-avm2/source/decompiler.rb', line 886

def expr_set_property(opcode)
  if captures = PropertyGlobal.match(opcode)
    return if !captures[:multiname] && !pseudo_global_scope?(captures[:scope])
    token(AssignmentToken, [
      get_name(nil, captures[:multiname]),
      parenthesize(expr(*captures[:arguments]))
    ])
  else
    subject, multiname, value, = opcode.children
    token(AssignmentToken, [
      get_name(expr(subject), multiname),
      parenthesize(expr(value))
    ])
  end
end

#expr_set_slot(opcode, toplevel = false) ⇒ Object



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/furnace-avm2/source/decompiler.rb', line 600

def expr_set_slot(opcode, toplevel=false)
  if captures = SetSlot.match(opcode)
    scope = @scopes[captures[:scope_pos] || 0]

    if captures[:catch_id]
      stmt = nil

      unless ExceptionVariable.match captures[:value], captures
        stmt = token(SupplementaryCommentToken,
          "Non-matching catch_id and catch id", [])
      end

      scope = {
        captures[:index] => captures[:variable]
      }

      @catch_scopes[captures[:catch_local]] = scope
      @scopes << scope

      throw :skip unless stmt
      stmt
    elsif @closure_slots && scope == :activation
      # treat as a local variable
      index, value = captures.values_at(:index, :value)
      slot = @closure_slots[index]

      type = type_token(slot.type.to_astlet) if slot.type
      expr = expr_set_var(token(VariableNameToken, slot.name.name),
            value, type,
            !@closure_locals.include?(index), toplevel)
      @closure_locals.add index

      expr
    elsif scope == :this
      # treat as a global property
      index, value = captures.values_at(:index, :value)

      if slot = @global_slots[index]
        name = get_name(nil, slot.name.to_astlet)
      else
        name = token(PropertyNameToken, "$__GLOBAL_#{index}")
      end

      token(AssignmentToken, [
        name,
        parenthesize(expr(value))
      ])
    end
  end
end

#expr_set_slot_toplevel(opcode) ⇒ Object



651
652
653
# File 'lib/furnace-avm2/source/decompiler.rb', line 651

def expr_set_slot_toplevel(opcode)
  expr_set_slot(opcode, true)
end

#expr_set_super(opcode) ⇒ Object



903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
# File 'lib/furnace-avm2/source/decompiler.rb', line 903

def expr_set_super(opcode)
  subject, multiname, value = opcode.children

  stmt = token(AssignmentToken, [
    get_name(token(SuperToken), multiname),
    parenthesize(expr(value))
  ])

  unless This.match subject
    stmt = token(SupplementaryCommentToken,
      "subject != this: #{subject.inspect}",
      [ stmt ])
  end

  stmt
end

#expr_set_var(var, value, type, declare, toplevel) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/furnace-avm2/source/decompiler.rb', line 525

def expr_set_var(var, value, type, declare, toplevel)
  if declare
    declaration =
      token(LocalVariableToken, [
        var,
        type
      ])
  end

  if declare && toplevel
    declaration.children <<
      token(InitializationToken, [
        expr(value)
      ])

    declaration
  else
    if declare
      @collected_vars <<
        token(StatementToken, [
          declaration
        ])
    end

    token(AssignmentToken, [
      var,
      parenthesize(expr(value))
    ])
  end
end

#expr_string(opcode) ⇒ Object



411
412
413
414
# File 'lib/furnace-avm2/source/decompiler.rb', line 411

def expr_string(opcode)
  string, = opcode.children
  token(ImmediateToken, string.inspect)
end

#expr_ternary(opcode) ⇒ Object

Control flow



981
982
983
# File 'lib/furnace-avm2/source/decompiler.rb', line 981

def expr_ternary(opcode)
  token(TernaryOperatorToken, parenthesize_each(exprs(opcode.children)))
end

#expr_type_of(opcode) ⇒ Object



1037
1038
1039
# File 'lib/furnace-avm2/source/decompiler.rb', line 1037

def expr_type_of(opcode)
  token(TypeOfToken, exprs(opcode.children))
end

#expression(opcode, toplevel = false) ⇒ Object Also known as: expr



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/furnace-avm2/source/decompiler.rb', line 370

def expression(opcode, toplevel=false)
  if toplevel
    handler = :"expr_#{opcode.type}_toplevel"
    if respond_to?(handler) && node = send(handler, opcode)
      return node
    end
  end

  handler = :"expr_#{opcode.type}"
  if respond_to?(handler) && node = send(handler, opcode)
    node
  else
    raise ExpressionNotRecognized.new(nil, opcode)
  end
end

#expressions(opcodes) ⇒ Object Also known as: exprs



387
388
389
390
391
# File 'lib/furnace-avm2/source/decompiler.rb', line 387

def expressions(opcodes)
  opcodes.map do |opcode|
    expression opcode
  end
end

#handle_expression(opcode) ⇒ Object

Expressions



364
365
366
367
368
# File 'lib/furnace-avm2/source/decompiler.rb', line 364

def handle_expression(opcode)
  expression(opcode, true)
rescue ExpressionNotRecognized => e
  raise ExpressionNotRecognized.new(opcode, e.opcode)
end

#local_token(index) ⇒ Object

Locals



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/furnace-avm2/source/decompiler.rb', line 436

def local_token(index)
  if index < 0
    token(VariableNameToken, "sp#{-index}")
  elsif index == 0
    if @options[:static]
      token(VariableNameToken, @options[:instance].name.name)
    else
      token(ThisToken)
    end
  elsif index <= @method.param_count
    if @method.has_param_names?
      token(VariableNameToken, @method.param_names[index - 1])
    else
      token(VariableNameToken, "param#{index - 1}")
    end
  else
    token(VariableNameToken, "local#{index - @method.param_count - 1}")
  end
end

#pseudo_global_scope?(scope) ⇒ Boolean

Returns:

  • (Boolean)


853
854
855
# File 'lib/furnace-avm2/source/decompiler.rb', line 853

def pseudo_global_scope?(scope)
  [:this, :with].include?(@scopes[scope || 0])
end

#stmt_begin(opcode, nodes) ⇒ Object



146
147
148
# File 'lib/furnace-avm2/source/decompiler.rb', line 146

def stmt_begin(opcode, nodes)
  nodes << stmt_block(opcode)
end

#stmt_block(block, options = {}) ⇒ Object

Statements



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/furnace-avm2/source/decompiler.rb', line 104

def stmt_block(block, options={})
  nodes = []
  last_index = 0

  block.children.each_with_index do |opcode, index|
    last_index = index

    if respond_to?(:"stmt_#{opcode.type}")
      send :"stmt_#{opcode.type}", opcode, nodes
    else
      catch(:skip) do
        @collected_vars = []
        stmt = token(StatementToken, [
          handle_expression(opcode)
        ])

        nodes.concat @collected_vars
        nodes.push stmt
      end
    end
  end

rescue ExpressionNotRecognized => e
  @partial = true

  comment = "Well, this is embarassing.\n\n" +
    "Expression recognizer failed at:\n" +
    "#{e.opcode.inspect}\n"

  comment << "\nRest of the code in this block:\n"
  block.children[last_index..-1].each do |opcode|
    comment << "#{opcode.inspect}\n"
  end

  nodes << CommentToken.new(@body, comment, @options)

ensure
  if $!.nil? || $!.is_a?(ExpressionNotRecognized)
    return token(ScopeToken, nodes, options)
  end
end

#stmt_break(opcode, nodes) ⇒ Object



222
223
224
225
226
227
# File 'lib/furnace-avm2/source/decompiler.rb', line 222

def stmt_break(opcode, nodes)
  label, = opcode.children
  nodes << token(BreakToken, [
    (token(LabelNameToken, label) if label)
  ])
end

#stmt_case(opcode, nodes) ⇒ Object



291
292
293
294
# File 'lib/furnace-avm2/source/decompiler.rb', line 291

def stmt_case(opcode, nodes)
  value, = opcode.children
  nodes << token(CaseToken, handle_expression(value))
end

#stmt_continue(opcode, nodes) ⇒ Object



229
230
231
232
233
234
# File 'lib/furnace-avm2/source/decompiler.rb', line 229

def stmt_continue(opcode, nodes)
  label, = opcode.children
  nodes << token(ContinueToken, [
    (token(LabelNameToken, label) if label)
  ])
end

#stmt_default(opcode, nodes) ⇒ Object



287
288
289
# File 'lib/furnace-avm2/source/decompiler.rb', line 287

def stmt_default(opcode, nodes)
  nodes << token(CaseToken, nil)
end

#stmt_do_while(opcode, nodes) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/furnace-avm2/source/decompiler.rb', line 184

def stmt_do_while(opcode, nodes)
  condition, body = opcode.children

  nodes << token(DoWhileToken,
    stmt_block(body, continuation: true),
    handle_expression(condition))
end

#stmt_for(opcode, nodes) ⇒ Object Also known as: stmt_for_in, stmt_for_each_in



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/furnace-avm2/source/decompiler.rb', line 192

def stmt_for(opcode, nodes)
  value_reg, value_type, object_reg, body = opcode.children

  @locals.add(value_reg)

  if opcode.type == :for_in
    klass = ForToken
  elsif opcode.type == :for_each_in
    klass = ForEachToken
  end

  if @activation_local
    name = token(VariableNameToken, @closure_slots[value_reg].name.name)
  else
    name = local_token(value_reg)
  end

  nodes << token(klass,
    token(InToken, [
      token(LocalVariableToken, [
        name,
        type_token(value_type)
      ]),
      local_token(object_reg),
    ]),
    stmt_block(body))
end

#stmt_if(opcode, nodes) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/furnace-avm2/source/decompiler.rb', line 150

def stmt_if(opcode, nodes)
  condition, if_true, if_false = opcode.children

  nodes << token(IfToken, handle_expression(condition),
    stmt_block(if_true, continuation: !if_false.nil?))

  if if_false
    first_child = if_false.children.first
    if if_false.children.count == 1 &&
          first_child.type == :if
      nodes << token(ElseToken,
        nil)
      stmt_if(first_child, nodes)
    else
      nodes << token(ElseToken,
        stmt_block(if_false))
    end
  end
end

#stmt_label(opcode, nodes) ⇒ Object



170
171
172
173
174
# File 'lib/furnace-avm2/source/decompiler.rb', line 170

def stmt_label(opcode, nodes)
  name, = opcode.children

  nodes << token(LabelDeclarationToken, name)
end

#stmt_pop_scope(opcode, nodes) ⇒ Object



341
342
343
344
345
346
347
348
349
# File 'lib/furnace-avm2/source/decompiler.rb', line 341

def stmt_pop_scope(opcode, nodes)
  if @options[:global_code]
    @scopes.pop
  elsif @scopes.any?
    @scopes.pop
  else
    raise "popscope with empty stack"
  end
end

#stmt_push_scope(opcode, nodes) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/furnace-avm2/source/decompiler.rb', line 315

def stmt_push_scope(opcode, nodes)
  if @options[:global_code]
    @scopes.push opcode.children.first
  elsif captures = KnownPushScopeMatcher.match(opcode)
    if captures[:get_local] == 0
      @scopes << :this
    elsif !@activation_local.nil? &&
        captures[:get_local] == @activation_local
      @scopes << :activation
    elsif @catch_scopes.include? captures[:get_local]
      @scopes << @catch_scopes[captures[:get_local]]
    elsif captures[:set_activation_local]
      if @activation_local
        raise "more than one activation per function is not supported"
      end

      @scopes << :activation
      @activation_local = captures[:set_activation_local]
    else
      raise "abnormal matched pushscope in nonglobal code: #{captures.inspect}"
    end
  else
    raise "abnormal pushscope in nonglobal code"
  end
end

#stmt_return(opcode, nodes) ⇒ Object Also known as: stmt_return_value, stmt_return_void



240
241
242
# File 'lib/furnace-avm2/source/decompiler.rb', line 240

def stmt_return(opcode, nodes)
  nodes << token(ReturnToken, exprs(opcode.children))
end

#stmt_switch(opcode, nodes) ⇒ Object



279
280
281
282
283
284
285
# File 'lib/furnace-avm2/source/decompiler.rb', line 279

def stmt_switch(opcode, nodes)
  condition, body = opcode.children

  nodes << token(SwitchToken,
    handle_expression(condition),
    stmt_block(body))
end

#stmt_throw(opcode, nodes) ⇒ Object



236
237
238
# File 'lib/furnace-avm2/source/decompiler.rb', line 236

def stmt_throw(opcode, nodes)
  nodes << token(ThrowToken, exprs(opcode.children))
end

#stmt_try(opcode, nodes) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/furnace-avm2/source/decompiler.rb', line 246

def stmt_try(opcode, nodes)
  body, *handlers = opcode.children

  nodes << token(TryToken, [
    stmt_block(body, continuation: true),
  ])

  handlers.each_with_index do |handler, index|
    block = within_meta_scope do
      stmt_block(handler.children.last, continuation: index < handlers.size - 1)
    end

    if handler.type == :catch
      type, variable, = handler.children

      if type
        filter_node = token(CatchFilterToken, [
          token(MultinameToken, variable.[:origin]),
          token(MultinameToken, type.[:origin])
        ])
      else
        filter_node = token(MultinameToken, variable.[:origin])
      end

      nodes << token(CatchToken, filter_node, block)
    elsif handler.type == :finally
      nodes << token(FinallyToken, block)
    else
      raise "unknown handler type #{handler.type}"
    end
  end
end

#stmt_while(opcode, nodes) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/furnace-avm2/source/decompiler.rb', line 176

def stmt_while(opcode, nodes)
  condition, body = opcode.children

  nodes << token(WhileToken,
    handle_expression(condition),
    stmt_block(body))
end

#stmt_with(opcode, nodes) ⇒ Object



351
352
353
354
355
356
357
358
359
360
# File 'lib/furnace-avm2/source/decompiler.rb', line 351

def stmt_with(opcode, nodes)
  object, scope = opcode.children

  @scopes << :with
  nodes << token(WithToken,
      expr(object),
      stmt_block(scope))
ensure
  @scopes.pop
end

#type_token(type) ⇒ Object



509
510
511
512
513
514
515
516
517
518
519
# File 'lib/furnace-avm2/source/decompiler.rb', line 509

def type_token(type)
  if IMMEDIATE_TYPE_MAP.include?(type)
    token(TypeToken, [
      token(ImmediateTypenameToken, IMMEDIATE_TYPE_MAP[type])
    ])
  else
    token(TypeToken, [
      token(MultinameToken, type.[:origin])
    ])
  end
end

#within_meta_scopeObject



296
297
298
299
300
301
302
303
# File 'lib/furnace-avm2/source/decompiler.rb', line 296

def within_meta_scope
  @metascopes.push @scopes
  @scopes = []

  yield
ensure
  @scopes = @metascopes.pop
end

#xml_add(node) ⇒ Object



1113
1114
1115
1116
1117
# File 'lib/furnace-avm2/source/decompiler.rb', line 1113

def xml_add(node)
  node.children.map do |child|
    xml_expr child
  end.join
end

#xml_esc_xattr(node) ⇒ Object



1119
1120
1121
# File 'lib/furnace-avm2/source/decompiler.rb', line 1119

def xml_esc_xattr(node)
  xml_expr(node.children.first)
end

#xml_esc_xelem(node) ⇒ Object



1123
1124
1125
# File 'lib/furnace-avm2/source/decompiler.rb', line 1123

def xml_esc_xelem(node)
  xml_expr(node.children.first)
end

#xml_expr(node) ⇒ Object



1101
1102
1103
1104
1105
1106
1107
# File 'lib/furnace-avm2/source/decompiler.rb', line 1101

def xml_expr(node)
  if respond_to?(:"xml_#{node.type}")
    send :"xml_#{node.type}", node
  else
    "{#{expr(node).to_text}}"
  end
end

#xml_string(node) ⇒ Object



1109
1110
1111
# File 'lib/furnace-avm2/source/decompiler.rb', line 1109

def xml_string(node)
  node.children.first
end