Class: Furnace::AVM2::Decompiler
- Inherits:
-
Object
- Object
- Furnace::AVM2::Decompiler
- Defined in:
- lib/furnace-avm2/source/decompiler.rb
Defined Under Namespace
Classes: ExpressionNotRecognized
Constant Summary collapse
- ActivationPrologue =
Matcher.new do [:begin, [:push_scope, [:get_local, 0]], [:set_local, -1, [:new_activation]], [:set_local, capture(:activation_local), [:get_local, -1]], [:push_scope, [:get_local, -1]], skip ] end
- RegularPrologue =
Matcher.new do [:begin, [:push_scope, [:get_local, 0]], skip ] end
- ActivationGetSlot =
Matcher.new do [:get_slot, capture(:index), [:get_scope_object, 1]] end
- GlobalGetSlot =
Matcher.new do [:get_slot, capture(:index), either[ [:get_global_scope], # normalized form [:get_scope_object, 0] # not emitted by ASC ]] end
- IMMEDIATE_TYPE_MAP =
{ :any => '*', :integer => 'int', :unsigned => 'uint', :string => 'String', :double => 'Number', :object => 'Object', :bool => 'Boolean', :true => 'Boolean', :false => 'Boolean', }
- XmlLiteralPreMatcher =
Matcher.new do [:coerce, [:q, "XML"], any] end
- ActivationSetSlot =
Matcher.new do [:set_slot, capture(:index), [:get_scope_object, 1], capture(:value)] end
- GlobalSetSlot =
Matcher.new do [:set_slot, capture(:index), either[ [:get_global_scope], # normalized form [:get_scope_object, 0] # not emitted by ASC ], capture(:value)] end
- INPLACE_OPERATOR_MAP =
Arithmetics
{ :inc_local => :"++", :inc_local_i => :"++", :dec_local => :"--", :dec_local_i => :"--", }
- PSEUDO_OPERATOR_MAP =
{ :increment => [:"+", 1], :increment_i => [:"+", 1], :decrement => [:"-", 1], :decrement_i => [:"-", 1], }
- OPERATOR_MAP =
{ :and => :"&&", :or => :"||", :add => :"+", :add_i => :"+", :subtract => :"-", :subtract_i => :"-", :multiply => :"*", :multiply_i => :"*", :divide => :"/", :modulo => :"%", :multiply => :"*", :negate => :"-", :negate_i => :"-", :! => :!, :> => :>, :>= => :>=, :< => :<, :<= => :<=, :== => :==, :=== => :===, :!= => :!=, :"!==" => :"!==", :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]], capture(:multiname) ], [ [:get_scope_object, 0], 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
Constants included from ABC
Instance Method Summary collapse
- #decompile ⇒ Object
- #expr_apply_type(opcode) ⇒ Object
- #expr_arithmetic(opcode) ⇒ Object (also: #expr_and, #expr_or, #expr_add, #expr_add_i, #expr_subtract, #expr_subtract_i, #expr_multiply, #expr_multiply_i, #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)
-
#expr_as_type_late(opcode) ⇒ Object
Types.
- #expr_call(opcode) ⇒ Object
- #expr_call_property(opcode) ⇒ Object (also: #expr_call_property_lex)
- #expr_call_super(opcode) ⇒ Object
- #expr_coerce(opcode) ⇒ Object
- #expr_construct(opcode) ⇒ Object
-
#expr_construct_property(opcode) ⇒ Object
Object creation.
- #expr_construct_super(opcode) ⇒ Object
- #expr_convert(opcode) ⇒ Object
- #expr_delete_property(opcode) ⇒ Object
- #expr_do_property(opcode, klass) ⇒ Object
- #expr_get_lex(opcode) ⇒ Object
- #expr_get_local(opcode) ⇒ Object
- #expr_get_property(opcode) ⇒ Object
- #expr_get_slot(opcode) ⇒ Object
- #expr_get_super(opcode) ⇒ Object
-
#expr_imm(opcode) ⇒ Object
(also: #expr_integer, #expr_double, #expr_null, #expr_undefined, #expr_true, #expr_false)
Immediates.
- #expr_in(opcode) ⇒ Object
- #expr_inplace_arithmetic(opcode) ⇒ Object (also: #expr_inc_local, #expr_inc_local_i, #expr_dec_local, #expr_dec_local_i)
- #expr_instance_of(opcode) ⇒ Object
- #expr_is_type_late(opcode) ⇒ Object
- #expr_nan(opcode) ⇒ Object
- #expr_new_array(opcode) ⇒ Object
- #expr_new_class(opcode) ⇒ Object
-
#expr_new_function(opcode) ⇒ Object
Closures.
- #expr_new_object(opcode) ⇒ Object
- #expr_pseudo_arithmetic(opcode) ⇒ Object (also: #expr_increment, #expr_increment_i, #expr_decrement, #expr_decrement_i)
- #expr_set_local(opcode) ⇒ Object
- #expr_set_property(opcode) ⇒ Object (also: #expr_init_property)
- #expr_set_slot(opcode) ⇒ Object
- #expr_set_super(opcode) ⇒ Object
- #expr_set_var(name, value, type, declare) ⇒ Object
- #expr_string(opcode) ⇒ Object
-
#expr_ternary(opcode) ⇒ Object
Control flow.
- #expr_type_of(opcode) ⇒ Object
- #expression(opcode) ⇒ Object (also: #expr)
- #expressions(opcodes) ⇒ Object (also: #exprs)
-
#handle_expression(opcode) ⇒ Object
Expressions.
-
#initialize(body, options) ⇒ Decompiler
constructor
A new instance of Decompiler.
-
#local_name(index) ⇒ Object
Locals.
-
#stmt_block(block, options = {}) ⇒ Object
Statements.
- #stmt_break(opcode, nodes) ⇒ Object
- #stmt_continue(opcode, nodes) ⇒ Object
- #stmt_for(opcode, nodes) ⇒ Object (also: #stmt_for_in, #stmt_for_each_in)
- #stmt_if(opcode, nodes) ⇒ Object
- #stmt_label(opcode, nodes) ⇒ Object
- #stmt_pop_scope(opcode, nodes) ⇒ Object
- #stmt_push_scope(opcode, nodes) ⇒ Object
- #stmt_return(opcode, nodes) ⇒ Object (also: #stmt_return_value, #stmt_return_void)
- #stmt_throw(opcode, nodes) ⇒ Object
- #stmt_while(opcode, nodes) ⇒ Object
- #type_token(type) ⇒ Object
- #xml_add(node) ⇒ Object
- #xml_esc_xattr(node) ⇒ Object
- #xml_esc_xelem(node) ⇒ Object
- #xml_expr(node) ⇒ Object
- #xml_string(node) ⇒ Object
Constructor Details
#initialize(body, options) ⇒ Decompiler
Returns a new instance of Decompiler.
15 16 17 18 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 15 def initialize(body, ) @body, @method, @options = body, body.method, .dup @closure = @options.delete(:closure) end |
Instance Method Details
#decompile ⇒ Object
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 42 def decompile begin @locals = Set.new([0]) + (1..@method.param_count).to_a @scopes = [] @nf = @body.code_to_nf if captures = ActivationPrologue.match(@nf) @closure_slots = {} @body.slot_traits.each do |trait| @closure_slots[trait.idx] = trait end @closure_locals = Set.new @nf.children.slice! 0...4 else if RegularPrologue.match @nf @nf.children.slice! 0...1 else # No prologue; probably a closure. end end @global_slots = @options[:global_slots] stmt_block @nf, function: !@options[:global_code], closure: @closure rescue Exception => e @failed = true comment = "'Ouch!' cried I, then died.\n" + "#{e.class}: #{e.}\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 |
#expr_apply_type(opcode) ⇒ Object
797 798 799 800 801 802 803 804 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 797 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_add_i, expr_subtract, expr_subtract_i, expr_multiply, expr_multiply_i, 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
539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 539 def expr_arithmetic(opcode) if OPERATOR_MAP.include?(opcode.type) insides = parenthesize_each(exprs(opcode.children)) if insides.count == 1 token(UnaryOperatorToken, insides, 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
781 782 783 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 781 def expr_as_type_late(opcode) token(AsToken, parenthesize_each(exprs(opcode.children))) end |
#expr_call(opcode) ⇒ Object
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 751 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, [ token(VariableNameToken, local_name(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
696 697 698 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 696 def expr_call_property(opcode) expr_do_property(opcode, CallToken) end |
#expr_call_super(opcode) ⇒ Object
701 702 703 704 705 706 707 708 709 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 701 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_coerce(opcode) ⇒ Object
846 847 848 849 850 851 852 853 854 855 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 846 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
717 718 719 720 721 722 723 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 717 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
713 714 715 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 713 def expr_construct_property(opcode) expr_do_property(opcode, NewToken) end |
#expr_construct_super(opcode) ⇒ Object
725 726 727 728 729 730 731 732 733 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 725 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
810 811 812 813 814 815 816 817 818 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 810 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
676 677 678 679 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 676 def expr_delete_property(opcode) subject, multiname, = opcode.children token(DeleteToken, [get_name(expr(subject), multiname)]) end |
#expr_do_property(opcode, klass) ⇒ Object
681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 681 def expr_do_property(opcode, klass) if captures = PropertyGlobal.match(opcode) token(klass, [ get_name(nil, captures[:multiname]), token(ArgumentsToken, exprs(captures[:arguments])) ]) else subject, multiname, *args = opcode.children token(klass, [ get_name(expr(subject), multiname), token(ArgumentsToken, exprs(args)) ]) end end |
#expr_get_lex(opcode) ⇒ Object
615 616 617 618 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 615 def expr_get_lex(opcode) multiname, = opcode.children get_name(nil, multiname) end |
#expr_get_local(opcode) ⇒ Object
310 311 312 313 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 310 def expr_get_local(opcode) index, = opcode.children token(VariableNameToken, local_name(index)) end |
#expr_get_property(opcode) ⇒ Object
620 621 622 623 624 625 626 627 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 620 def expr_get_property(opcode) if captures = PropertyGlobal.match(opcode) get_name(nil, captures[:multiname]) else subject, multiname, = opcode.children get_name(expr(subject), multiname) end end |
#expr_get_slot(opcode) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 330 def expr_get_slot(opcode) if @closure_slots && captures = ActivationGetSlot.match(opcode) # treat as a local variable slot = @closure_slots[captures[:index]] token(VariableNameToken, slot.name.name) elsif captures = GlobalGetSlot.match(opcode) # treat as a global property if @global_slots slot = @global_slots[captures[:index]] get_name(nil, slot.name.to_astlet) else token(PropertyNameToken, "$__GLOBAL_#{captures[:index]}") end end end |
#expr_get_super(opcode) ⇒ Object
629 630 631 632 633 634 635 636 637 638 639 640 641 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 629 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
250 251 252 253 254 255 256 257 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 250 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
584 585 586 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 584 def expr_in(opcode) token(InToken, parenthesize_each(exprs(opcode.children))) end |
#expr_inplace_arithmetic(opcode) ⇒ Object Also known as: expr_inc_local, expr_inc_local_i, expr_dec_local, expr_dec_local_i
470 471 472 473 474 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 470 def expr_inplace_arithmetic(opcode) token(UnaryPostOperatorToken, [ token(VariableNameToken, local_name(*opcode.children)), ], INPLACE_OPERATOR_MAP[opcode.type]) end |
#expr_instance_of(opcode) ⇒ Object
789 790 791 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 789 def expr_instance_of(opcode) token(InstanceOfToken, parenthesize_each(exprs(opcode.children))) end |
#expr_is_type_late(opcode) ⇒ Object
785 786 787 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 785 def expr_is_type_late(opcode) token(IsToken, parenthesize_each(exprs(opcode.children))) end |
#expr_nan(opcode) ⇒ Object
270 271 272 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 270 def expr_nan(opcode) token(ImmediateToken, "NaN") end |
#expr_new_array(opcode) ⇒ Object
274 275 276 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 274 def expr_new_array(opcode) token(ArrayToken, exprs(opcode.children)) end |
#expr_new_class(opcode) ⇒ Object
806 807 808 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 806 def expr_new_class(opcode) throw :skip end |
#expr_new_function(opcode) ⇒ Object
Closures
822 823 824 825 826 827 828 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 822 def expr_new_function(opcode) index, = opcode.children body = @method.root.method_body_at(index) token(ClosureToken, body) end |
#expr_new_object(opcode) ⇒ Object
278 279 280 281 282 283 284 285 286 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 278 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_pseudo_arithmetic(opcode) ⇒ Object Also known as: expr_increment, expr_increment_i, expr_decrement, expr_decrement_i
488 489 490 491 492 493 494 495 496 497 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 488 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) ⇒ Object
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 392 def expr_set_local(opcode) 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_name(index), value, type, !@locals.include?(index)) ensure @locals.add index if index end |
#expr_set_property(opcode) ⇒ Object Also known as: expr_init_property
643 644 645 646 647 648 649 650 651 652 653 654 655 656 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 643 def expr_set_property(opcode) if captures = PropertyGlobal.match(opcode) token(AssignmentToken, [ get_name(nil, captures[:multiname]), expr(*captures[:arguments]) ]) else subject, multiname, value, = opcode.children token(AssignmentToken, [ get_name(expr(subject), multiname), expr(value) ]) end end |
#expr_set_slot(opcode) ⇒ Object
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 431 def expr_set_slot(opcode) if @closure_slots && captures = ActivationSetSlot.match(opcode) # 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(slot.name.name, value, type, !@closure_locals.include?(index)) @closure_locals.add index expr elsif captures = GlobalSetSlot.match(opcode) # treat as a global property index, value = captures.values_at(:index, :value) if @global_slots slot = @global_slots[index] name = get_name(nil, slot.name.to_astlet) else token(PropertyNameToken, "$__GLOBAL_#{index}") end token(AssignmentToken, [ name, expr(value) ]) end end |
#expr_set_super(opcode) ⇒ Object
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 659 def expr_set_super(opcode) subject, multiname, value = opcode.children stmt = token(AssignmentToken, [ get_name(token(SuperToken), multiname), expr(value) ]) unless This.match subject stmt = token(SupplementaryCommentToken, "subject != this: #{subject.inspect}", [ stmt ]) end stmt end |
#expr_set_var(name, value, type, declare) ⇒ Object
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 375 def expr_set_var(name, value, type, declare) if declare token(LocalVariableToken, [ token(VariableNameToken, name), type, token(InitializationToken, [ expr(value) ]) ]) else token(AssignmentToken, [ token(VariableNameToken, name), expr(value) ]) end end |
#expr_string(opcode) ⇒ Object
265 266 267 268 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 265 def expr_string(opcode) string, = opcode.children token(ImmediateToken, string.inspect) end |
#expr_ternary(opcode) ⇒ Object
Control flow
737 738 739 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 737 def expr_ternary(opcode) token(TernaryOperatorToken, parenthesize_each(exprs(opcode.children))) end |
#expr_type_of(opcode) ⇒ Object
793 794 795 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 793 def expr_type_of(opcode) token(TypeOfToken, exprs(opcode.children)) end |
#expression(opcode) ⇒ Object Also known as: expr
231 232 233 234 235 236 237 238 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 231 def expression(opcode) 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
241 242 243 244 245 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 241 def expressions(opcodes) opcodes.map do |opcode| expression opcode end end |
#handle_expression(opcode) ⇒ Object
Expressions
225 226 227 228 229 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 225 def handle_expression(opcode) expression(opcode) rescue ExpressionNotRecognized => e raise ExpressionNotRecognized.new(opcode, e.opcode) end |
#local_name(index) ⇒ Object
Locals
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 290 def local_name(index) if index < 0 "sp#{-index}" elsif index == 0 if @options[:static] @options[:instance].name.name else "this" end elsif index <= @method.param_count if @method.has_param_names? @method.param_names[index - 1] else "param#{index - 1}" end else "local#{index - @method.param_count - 1}" end end |
#stmt_block(block, options = {}) ⇒ Object
Statements
102 103 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 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 102 def stmt_block(block, ={}) nodes = [] block.children.each do |opcode| if respond_to?(:"stmt_#{opcode.type}") send :"stmt_#{opcode.type}", opcode, nodes else catch(:skip) do nodes << token(StatementToken, [ handle_expression(opcode) ]) end end end rescue ExpressionNotRecognized => e @partial = true comment = "Well, this is embarassing.\n\n" + "Expression recognizer failed at:\n" + "#{e.opcode.inspect}\n" if e.context != e.opcode comment << "\nOpcode at the top of stack:\n" + "#{e.context.inspect}\n" end nodes << CommentToken.new(@body, comment, @options) ensure if $!.nil? || $!.is_a?(ExpressionNotRecognized) return token(ScopeToken, nodes, ) end end |
#stmt_break(opcode, nodes) ⇒ Object
189 190 191 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 189 def stmt_break(opcode, nodes) nodes << token(ReturnToken, exprs(opcode.children)) end |
#stmt_continue(opcode, nodes) ⇒ Object
193 194 195 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 193 def stmt_continue(opcode, nodes) nodes << token(ContinueToken, exprs(opcode.children)) end |
#stmt_for(opcode, nodes) ⇒ Object Also known as: stmt_for_in, stmt_for_each_in
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 159 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 @closure_slots name = token(VariableNameToken, @closure_slots[value_reg].name.name) else name = token(VariableNameToken, local_name(value_reg)) end nodes << token(klass, token(InToken, [ token(LocalVariableToken, [ name, type_token(value_type) ]), token(VariableNameToken, local_name(object_reg)), ]), stmt_block(body)) end |
#stmt_if(opcode, nodes) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 137 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?)) nodes << token(ElseToken, stmt_block(if_false)) if if_false end |
#stmt_label(opcode, nodes) ⇒ Object
146 147 148 149 150 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 146 def stmt_label(opcode, nodes) name, = opcode.children nodes << token(LabelDeclarationToken, name) end |
#stmt_pop_scope(opcode, nodes) ⇒ Object
215 216 217 218 219 220 221 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 215 def stmt_pop_scope(opcode, nodes) if @options[:global_code] @scopes.pop else raise "popscope in nonglobal code" end end |
#stmt_push_scope(opcode, nodes) ⇒ Object
207 208 209 210 211 212 213 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 207 def stmt_push_scope(opcode, nodes) if @options[:global_code] @scopes.push opcode.children.first else raise "pushscope in nonglobal code" end end |
#stmt_return(opcode, nodes) ⇒ Object Also known as: stmt_return_value, stmt_return_void
201 202 203 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 201 def stmt_return(opcode, nodes) nodes << token(ReturnToken, exprs(opcode.children)) end |
#stmt_throw(opcode, nodes) ⇒ Object
197 198 199 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 197 def stmt_throw(opcode, nodes) nodes << token(ThrowToken, exprs(opcode.children)) end |
#stmt_while(opcode, nodes) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 152 def stmt_while(opcode, nodes) condition, body = opcode.children nodes << token(WhileToken, handle_expression(condition), stmt_block(body)) end |
#type_token(type) ⇒ Object
359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 359 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 |
#xml_add(node) ⇒ Object
869 870 871 872 873 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 869 def xml_add(node) node.children.map do |child| xml_expr child end.join end |
#xml_esc_xattr(node) ⇒ Object
875 876 877 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 875 def xml_esc_xattr(node) xml_expr(node.children.first) end |
#xml_esc_xelem(node) ⇒ Object
879 880 881 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 879 def xml_esc_xelem(node) xml_expr(node.children.first) end |
#xml_expr(node) ⇒ Object
857 858 859 860 861 862 863 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 857 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
865 866 867 |
# File 'lib/furnace-avm2/source/decompiler.rb', line 865 def xml_string(node) node.children.first end |