Class: Minjs::Compressor

Inherits:
Object
  • Object
show all
Includes:
Exp, Func, Literal, Program, Statement
Defined in:
lib/minjs/compressor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Program

#source_element, #source_elements

Methods included from Func

#formal_parameter_list, #func_body, #func_declaration, #func_exp

Methods included from Exp

#additive_exp, #arguments, #array_literal, #assignment_exp, #bitwise_and_exp, #bitwise_or_exp, #bitwise_xor_exp, #call_exp, #cond_exp, #equality_exp, #exp, #identifier, #left_hand_side_exp, #logical_and_exp, #logical_or_exp, #member_exp, #multiplicative_exp, #new_exp, #object_literal, #postfix_exp, #primary_exp, #property_name, #property_name_and_value_list, #property_set_parameter_list, #relational_exp, #shift_exp, #unary_exp

Methods included from Statement

#block, #break_statement, #case_block, #continue_statement, #debugger_statement, #do_while_statement, #empty_statement, #exp_statement, #for_statement, #if_statement, #initialiser, #iteration_statement, #labelled_statement, #return_statement, #semicolon, #statement, #statement_list, #switch_statement, #throw_statement, #try_catch, #try_finally, #try_statement, #var_decl, #var_decl_list, #var_statement, #while_statement, #with_statement

Methods included from Literal

#boolean_literal, #literal, #null_literal

Constructor Details

#initialize(options = {}) ⇒ Compressor

Returns a new instance of Compressor.



23
24
25
26
27
28
29
30
31
32
# File 'lib/minjs/compressor.rb', line 23

def initialize(options = {})
  @logger = options[:logger]
  if !@logger
    @logger = Logger.new(STDERR)
    @logger.level = (options[:debug_level] || Logger::WARN)
    @logger.formatter = proc{|severity, datetime, progname, message|
      "#{message}\n"
    }
  end
end

Instance Attribute Details

#progObject (readonly)

Returns the value of attribute prog.



21
22
23
# File 'lib/minjs/compressor.rb', line 21

def prog
  @prog
end

Instance Method Details

#assignment_after_var(node = @prog) ⇒ Object



766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# File 'lib/minjs/compressor.rb', line 766

def assignment_after_var(node = @prog)
  def rewrite_var(var_st, name, initializer)
    var_st.normalization
    i = 0
    var_st.vars.each do |_name, _initializer|
      if _name == name and _initializer.nil?
        var_st.vars[i] = [name, initializer]
        var_st.normalization
        return true
      end
      i += 1
    end
    false
  end

  retry_flag = true
  while retry_flag
    retry_flag = false
    node.traverse(nil) {|st, parent|
      if st.kind_of? ECMA262::StVar and parent.kind_of? ECMA262::SourceElements
        catch(:break){
          idx = parent.index(st) + 1
          while true
            st2 = parent[idx]
            if st2.kind_of? ECMA262::StEmpty or (st2.kind_of? ECMA262::StFunc and st2.decl?)
              idx +=1
              next
            elsif st2.kind_of? ECMA262::StExp and st2.exp.kind_of? ECMA262::ExpAssign
              if rewrite_var(st, st2.exp.val, st2.exp.val2)
                parent.replace(st2, ECMA262::StEmpty.new())
                retry_flag = true
              else
                throw :break
              end
              idx += 1
              next
            elsif st2.kind_of? ECMA262::StFor and st2.exp1.kind_of? ECMA262::ExpAssign
              if rewrite_var(st, st2.exp1.val, st2.exp1.val2)
                st2.replace(st2.exp1, nil)
                retry_flag = true
              else
                throw :break
              end
              throw  :break
            elsif st2.kind_of? ECMA262::StExp and st2.exp.kind_of? ECMA262::ExpComma
              exp_parent = st2
              exp = st2.exp

              while exp.val.kind_of? ECMA262::ExpComma
                exp_parent = exp
                exp = exp.val
              end

              if exp.val.kind_of? ECMA262::ExpAssign
                if rewrite_var(st, exp.val.val, exp.val.val2)
                  exp_parent.replace(exp, exp.val2)
                  retry_flag = true
                else
                  throw :break
                end
              else
                throw :break
              end
            else
              throw :break
            end
          end
        }
      end
    }
  end
  self
end

#block_to_statement(node = @prog) ⇒ Object



298
299
300
301
302
303
304
305
306
307
# File 'lib/minjs/compressor.rb', line 298

def block_to_statement(node = @prog)
  node.traverse(nil) {|st, parent|
    if st.kind_of? ECMA262::StBlock and !parent.kind_of?(ECMA262::StTry) and !parent.kind_of?(ECMA262::StIf)
        if st.to_statement?
          parent.replace(st, st.to_statement)
        end
    end
  }
  if_block_to_statement
end

#c2i(c) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/minjs/compressor.rb', line 108

def c2i(c)
  c = c.ord
  if c >= 0x30 and c <= 0x39
    c = c - 0x30
  elsif c >= 0x61 and c <= 0x7a
    c = c - 0x61 + 10
  elsif c >= 0x41 and c <= 0x5a
    c = c - 0x41 + 10 + 26
  elsif c == 0x5f
    c = 62
  elsif c == 0x24
    c = 63
  end
end

#compress(data, options = {}) ⇒ Object



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
# File 'lib/minjs/compressor.rb', line 52

def compress(data, options = {})
  @logger.info '* parse'
  parse(data)

  if options[:only_parse]
    return
  end

  algo = [
    :reorder_function_decl,
    :simple_replacement,
    :reorder_var,
    :assignment_after_var,
    :grouping_statement,
    :reduce_if,
    :block_to_statement,
    :if_to_cond,
    :optimize_if_return,
    :compress_var,
    :reduce_exp,
    :grouping_statement,
    :block_to_statement,
    :if_to_cond,
    :optimize_if_return2,
    :remove_paren,
  ]
  algo.each do |a|
    if (options.empty? || options[:all] || options[a]) && !options[("no_" + a.to_s).to_sym]
      @logger.info "* #{a}"
      __send__(a, @prog)
    end
  end

  @heading_comments.reverse.each do |c|
    @prog.source_elements.source_elements.unshift(c)
  end
  self
end

#compress_var(node = @prog) ⇒ Object



472
473
474
475
# File 'lib/minjs/compressor.rb', line 472

def compress_var(node = @prog)
  #compress_var_sub(@prog, :longer => true)
  compress_var_sub
end

#compress_var_sub(node = @prog, options = {}) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
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
650
651
652
653
654
# File 'lib/minjs/compressor.rb', line 477

def compress_var_sub(node = @prog, options = {})
  #
  #traverse all statemtns and expression
  #
  scopes = []
  node.traverse(nil) {|st, parent|
    if st.kind_of? ECMA262::StFunc
      _context = st.context
      _parent = parent
      _st = st
      scopes.push([st, parent, _parent, _context, _st])
    end
  }
  scopes.reverse!
  scopes.each {|st, parent, _parent, _context, _st|
    var_sym = :a
    if _parent and _context and _st
      #
      # collect and counting all variables under this function/catch
      # collect and counting all var-variables under this function/catch
      #
      all_vars = {}
      var_vars = {}
      var_vars_list = []
      outer_vars = {}
      nesting_vars = {}
      nesting_vars_list = []

      _st.traverse(_parent) {|st2|
        #
        # In this function,
        #
        # 1. outer_vars:
        #    Variables which locate out of this function(or global variable)
        #    Them name cannot be renamed
        # 2. nesting_vars:
        #    Variables which locate in the function of this function.
        #    Them name cannot be renamed
        # 3. var_vars:
        #    Variables which have same scope in this function.
        #    Them name can be renamed under the following conditions
        #
        #   a. If the new name is not used, the name can be renamed to it.
        #   b. If the new name belongs to var_vars, the name cannot be renamed.
        #   c. If the new name belongs to outer_vars the name cannot be renamed.
        #   d. If the new name belongs to nesting_vars, the name can be rename
        #      to it after rename nesting_vars's name to another name.
        #
        if st2.kind_of? ECMA262::IdentifierName
          var_name = st2.val.to_sym
          st2_env = st2.binding_env
          all_vars[var_name] ||= 0
          all_vars[var_name] += 1
          if st2_env == nil #global
            outer_vars[var_name] ||= 0
            outer_vars[var_name] += 1
          elsif st2_env == @global_context.var_env #global
            outer_vars[var_name] ||= 0
            outer_vars[var_name] += 1
          elsif st2_env == st.context.var_env
            var_vars[var_name] ||= 0
            var_vars[var_name] += 1
            var_vars_list.push(st2)
          else
            e = st2.binding_env
            while e
              e = e.outer
              if e == st.context.var_env
                nesting_vars[var_name] ||= 0
                nesting_vars[var_name] += 1
                nesting_vars_list.push(st2)
                break
              end
              if e.nil?
                outer_vars[var_name] ||= 0
                outer_vars[var_name] += 1
                break
              end
            end
          end
        end
      }
      unless var_vars[:eval]
        eval_flag = false
        _st.traverse(_parent) {|st2|
          if st2.kind_of? ECMA262::ExpCall and st2.name.to_js({}) == "eval"
            eval_flag = true
            break
          end
        }
        if eval_flag
          next
        end
      end
      #
      # sort var_vars
      #
      var_vars_array = var_vars.sort {|(k1,v1), (k2,v2)| v2 <=> v1}
      #
      # create renaming table
      #
      rename_table = {}
      var_vars_array.each {|name, count|
        if name.nil?
          next #bug?
        end
        while outer_vars[var_sym] or var_vars[var_sym]
          var_sym = next_sym(var_sym)
        end
        #rename nesting_vars
        if nesting_vars[var_sym]
          nesting_vars_list.each do |x|
            raise 'error' if x.binding_env(:var).nil?
            raise 'error' if x.binding_env(:lex).nil?
          end

          var_sym2 = "abc#{var_sym.to_s}".to_sym
          while all_vars[var_sym2]
            var_sym2 = next_sym(var_sym2)
          end
          rl = {}
          nesting_vars_list.each do |x|
            if x.val.to_sym == var_sym
              _var_env = x.binding_env(:var)
              _lex_env = x.binding_env(:lex)
              rl[_var_env] = true
              rl[_lex_env] = true
            end
          end
          rl.keys.each do |_env|
            if _env && _env.record.binding[var_sym]
              _env.record.binding[var_sym2] = _env.record.binding[var_sym]
              _env.record.binding.delete var_sym
            end
          end

          nesting_vars_list.each do |x|
            if x.val.to_sym == var_sym
              x.instance_eval{
                @val = var_sym2
              }
            end
            raise 'error' if x.binding_env(:var).nil?
            raise 'error' if x.binding_env(:lex).nil?
          end
        end
        rename_table[name] = var_sym
        var_sym = next_sym(var_sym)
      }
      var_vars_list.each {|st2|
        raise 'error' if st2.binding_env(:var).nil?
        raise 'error' if st2.binding_env(:lex).nil?
      }

      rename_table.each do |name, new_name|
        if name != new_name
          if st.context.var_env.record.binding[name]
            st.context.var_env.record.binding[new_name] = st.context.var_env.record.binding[name]
            st.context.var_env.record.binding.delete(name)
          end
          if st.context.lex_env.record.binding[name]
            st.context.lex_env.record.binding[new_name] = st.context.lex_env.record.binding[name]
            st.context.lex_env.record.binding.delete(name)
          end
        end
      end

      var_vars_list.each {|st2|
        st2.instance_eval{
          @val = rename_table[@val]
        }
        raise 'error' if st2.binding_env(:var).nil?
        raise 'error' if st2.binding_env(:lex).nil?
      }
    end
  }
  self
end

#debugObject



34
35
36
# File 'lib/minjs/compressor.rb', line 34

def debug
  puts @prog.to_js()
end

#grouping_statement(node = @prog) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/minjs/compressor.rb', line 163

def grouping_statement(node = @prog)
  node.traverse(nil) {|st, parent|
    if st.kind_of? ECMA262::StatementList
      st.grouping
    end
  }
  remove_paren
  self
end

#i2c(c) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/minjs/compressor.rb', line 122

def i2c(c)
  if c < 10
    c = "%c" % (0x30 + c)
  elsif c < 10 + 26
    c = "%c" % (0x61 + c - 10)
  elsif c < 10 + 26 + 26
    c = "%c" % (0x41 + c - 10 -  26)
  elsif c < 63
    c = "_"
  elsif c < 64
    c = "$"
  end
end

#if_block_to_statement(node = @prog) ⇒ Object

To determine removing “if block” is available or not is difficult. For example, next codes block must not be removed, because “else” cluase combined to second “if” statement.

if(a){ //<= this block must not be removed
  while(true)
    if(b){
      ;
    }
}
else{
 ;
}


324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/minjs/compressor.rb', line 324

def if_block_to_statement(node = @prog)
  # The "else" cluase's block can be removed always
  node.traverse(nil) {|st, parent|
    if st.kind_of? ECMA262::StIf
      if st.else_st and st.else_st.kind_of? ECMA262::StBlock
        st.else_st.remove_empty_statement
      end

      if st.else_st and st.else_st.kind_of? ECMA262::StBlock and st.else_st.to_statement?
        st.replace(st.else_st, st.else_st.to_statement)
      end
    end
  }
  node.traverse(nil) {|st0, parent|
    st = st0.deep_dup
    if st.kind_of? ECMA262::StIf
      if st.then_st and st.then_st.kind_of? ECMA262::StBlock
        st.then_st.remove_empty_statement
      end

      if st.then_st and st.then_st.kind_of? ECMA262::StBlock and st.then_st.to_statement?
        st.replace(st.then_st, st.then_st.to_statement)
      end

      _lex = Minjs::Lex.new(st.to_js)
      _context = ECMA262::Context.new
      _if = if_statement(_lex, _context)
      if _if == st #
        parent.replace(st0, st)
      end
    end
  }
  self
end

#if_to_cond(node = @prog) ⇒ Object

if(a)b;else c;

>

a?b:c

if(a)b

>

a&&b;



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/minjs/compressor.rb', line 368

def if_to_cond(node = @prog)
  node.traverse(nil) {|st, parent|
    if st.kind_of? ECMA262::StIf
      if st.to_exp?
        t = ECMA262::StExp.new(st.to_exp({}))
        remove_paren(t)
        if t.to_js.length <= st.to_js.length
          parent.replace(st, t)
        end
      end
    end
  }
  if_to_return(node)
  self
end

#if_to_return(node = @prog) ⇒ Object

if(a)return b;else return c;

> return a?b:c;



387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/minjs/compressor.rb', line 387

def if_to_return(node = @prog)
  node.traverse(nil) {|st, parent|
    if st.kind_of? ECMA262::StIf
      if st.to_return?
        t = st.to_return
        remove_paren(t)
        if t.to_js.length <= st.to_js.length
          parent.replace(st, t)
        end
      end
    end
  }
  self
end

#next_sym(s) ⇒ Object



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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/minjs/compressor.rb', line 107

def next_sym(s)
  def c2i(c)
    c = c.ord
    if c >= 0x30 and c <= 0x39
      c = c - 0x30
    elsif c >= 0x61 and c <= 0x7a
      c = c - 0x61 + 10
    elsif c >= 0x41 and c <= 0x5a
      c = c - 0x41 + 10 + 26
    elsif c == 0x5f
      c = 62
    elsif c == 0x24
      c = 63
    end
  end
  def i2c(c)
    if c < 10
      c = "%c" % (0x30 + c)
    elsif c < 10 + 26
      c = "%c" % (0x61 + c - 10)
    elsif c < 10 + 26 + 26
      c = "%c" % (0x41 + c - 10 -  26)
    elsif c < 63
      c = "_"
    elsif c < 64
      c = "$"
    end
  end

  v = 0
  s.to_s.split("").each do |x|
    v *= 64
    v += c2i(x)
  end

  while true
    v += 1
    ret = []
    vv = v
    while vv > 0
      ret.unshift(i2c(vv % 64))
      vv /= 64
    end
    ret = ret.join("")
    if ECMA262::IdentifierName.reserved?(ret.to_sym)
      ;
    elsif ret.to_s.match(/^\d/)
      ;
    else
      break
    end
  end
  ret.to_sym

end

#optimize_if_return(node = @prog) ⇒ Object

if(a)return b; return c;

> if(a)return b;else return c;

> return a?b:c;



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/minjs/compressor.rb', line 409

def optimize_if_return(node = @prog)
  retry_flag = true
  while retry_flag
    retry_flag = false
    node.traverse(nil) {|st0, parent0|
      if st0.kind_of? ECMA262::StIf and parent0.kind_of? ECMA262::StatementList
        i = parent0.index(st0)
        break if i.nil?
        parent = parent0.deep_dup
        st = parent[i]
        #
        if parent[i+1].nil? and !parent.kind_of?(ECMA262::SourceElements)
          next
        end
        if parent[i+1].nil? or parent[i+1].to_return?
          s = st
          while s.kind_of? ECMA262::StIf and s.else_st and s.then_st.to_return?
            s = s.else_st
          end
          if s and s.kind_of? ECMA262::StIf and s.then_st.to_return?
            if parent[i+1]
              s.replace(s.else_st, parent[i+1])
              parent.replace(parent[i+1], ECMA262::StEmpty.new)
            else
              s.replace(s.else_st, ECMA262::StReturn.new(ECMA262::ExpVoid.new(ECMA262::ECMA262Numeric.new(0))))
            end
            if_to_cond(parent)
            if parent.to_js(:no_debug => true).length <= parent0.to_js(:no_debug => true).length
              parent0.replace(st0, st)
              if parent[i+1]
                parent0.replace(parent0[i+1], ECMA262::StEmpty.new)
              end
              retry_flag = true
              node = parent0
            end
          end
        end
      end
    }
  end
  self
end

#optimize_if_return2(node = @prog) ⇒ Object

if(a)return b;else c;

>

if(a)return b;c;



457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/minjs/compressor.rb', line 457

def optimize_if_return2(node = @prog)
  node.traverse(nil) {|st, parent|
    if st.kind_of? ECMA262::StIf and st.else_st and parent.kind_of? ECMA262::StatementList
      st.remove_empty_statement
      if (st.then_st.kind_of? ECMA262::StBlock and st.then_st[-1].kind_of? ECMA262::StReturn) or
         st.then_st.kind_of? ECMA262::StReturn
        idx = parent.index(st)
        parent[idx+1..0] = st.else_st
        st.replace(st.else_st, nil)
      end
    end
  }
  self
end

#parse(data) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/minjs/compressor.rb', line 91

def parse(data)
  @lex = Minjs::Lex.new(data, :logger => @logger)
  @global_context = ECMA262::Context.new
  @heading_comments = []
  @lex.eval_lit{
    while a = @lex.ws_lit
      @heading_comments.push(a)
    end
    nil
  }
  @prog = source_elements(@lex, @global_context)

  remove_empty_statement
  self
end

#reduce_exp(node = @prog) ⇒ Object



656
657
658
659
660
661
662
663
# File 'lib/minjs/compressor.rb', line 656

def reduce_exp(node = @prog)
  node.traverse(nil) {|st, parent|
    if st.kind_of? ECMA262::Exp
      st.reduce(parent)
    end
  }
  self
end

#reduce_if(node = @prog) ⇒ Object

reduce_if



715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/minjs/compressor.rb', line 715

def reduce_if(node = @prog)
  retry_flag = true
  while(retry_flag)
    retry_flag = false
    node.traverse(nil) {|st, parent|
      if st.kind_of? ECMA262::StIf
        # if(a)
        #   if(b) ...;
        # if(a && b) ...;
        #
        if st.else_st.nil? and
          st.then_st.kind_of? ECMA262::StIf and st.then_st.else_st.nil?
          st.replace(st.cond, ECMA262::ExpLogicalAnd.new(st.cond, st.then_st.cond))
          st.replace(st.then_st, st.then_st.then_st)
        end
        #if(a);
        # => a
        #if(a){}
        # => a
        if st.then_st.empty? and st.else_st.nil?
          parent.replace(st, ECMA262::StExp.new(st.cond))
          retry_flag = true
        end
        #if(a)z;else;
        #if(a)z;else{}
        # => {if(a)z;}
        if st.else_st and st.else_st.empty?
          st.replace(st.else_st, nil)
          parent.replace(st, ECMA262::StBlock.new([st]))
          retry_flag = true
        end

        #if(a);else z;
        #=>if(!a)z;
        #if(a){}else z;
        #=>if(!a)z;
        if st.then_st.empty? and st.else_st
          st.replace(st.cond, ECMA262::ExpLogicalNot.new(st.cond));
          else_st = st.else_st
          st.replace(st.else_st, nil)
          st.replace(st.then_st, else_st)
          parent.replace(st, ECMA262::StBlock.new([st]))
          retry_flag = true
        end
      end
    }
    block_to_statement if retry_flag
  end
  self
end

#remove_block_in_block(node = @prog) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/minjs/compressor.rb', line 280

def remove_block_in_block(node = @prog)
  while true
    _retry = false
    node.traverse(nil) {|st, parent|
      if parent.kind_of? ECMA262::StatementList and st.kind_of? ECMA262::StBlock
        idx = parent.index(st)
        parent.statement_list[idx..idx] = st.statement_list.statement_list
        _retry = true
        break
      elsif st.kind_of? ECMA262::StBlock
        ;
      end
    }
    break if !_retry
  end
  self
end

#remove_empty_statement(node = @prog) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/minjs/compressor.rb', line 43

def remove_empty_statement(node = @prog)
  node.traverse(nil) {|st, parent|
    if st.kind_of? ECMA262::StatementList
      st.remove_empty_statement
    end
  }
  self
end

#remove_paren(node = @prog) ⇒ Object



270
271
272
273
274
275
276
277
278
# File 'lib/minjs/compressor.rb', line 270

def remove_paren(node = @prog)
  node.traverse(nil) {|st, parent|
    if st.respond_to? :remove_paren
      st.remove_paren
      st.add_paren
    end
  }
  self
end

#reorder_function_decl(node = @prog) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/minjs/compressor.rb', line 173

def reorder_function_decl(node = @prog)
  flist = []
  node.traverse(nil) {|st, parent|
    if st.kind_of? ECMA262::StFunc and parent.kind_of? ECMA262::StatementList and st.decl?
      if parent.index(st)
        flist.push([st, parent])
      end
    end
  }
  flist.reverse.each do |st, parent|
    parent.remove(st)
    parent.statement_list.unshift(st)
  end
  self
end

#reorder_var(node = @prog) ⇒ Object



189
190
191
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/minjs/compressor.rb', line 189

def reorder_var(node = @prog)
  node.traverse(nil) {|st, parent|
    if st.kind_of? ECMA262::Prog
      vars = nil
      context = st.context
      #
      # collect all of var variable in this function
      #
      var_vars = {}
      context.var_env.record.binding.each do|k, v|
        if v and v[:_parameter_list].nil? and !v[:value].kind_of?(ECMA262::StFunc)
          var_vars[k] = true
        end
      end
      #
      # traverse block and convert var statement to assignment expression
      # if variable has initializer
      #
      st.traverse(parent){|st2, parent2|
        if st2.kind_of? ECMA262::StVar and st2.context.var_env == context.var_env
          exp = nil
          st2.vars.each do |name, initializer|
            if initializer
              if exp.nil?
                exp = ECMA262::ExpAssign.new(name, initializer)
              else
                exp = ECMA262::ExpComma.new(exp, ECMA262::ExpAssign.new(name, initializer))
              end
            end
          end
          if exp
            parent2.replace(st2, ECMA262::StExp.new(exp))
          else
            parent2.replace(st2, ECMA262::StEmpty.new())
          end
        elsif st2.kind_of? ECMA262::StForVar and st2.context.var_env == context.var_env
          parent2.replace(st2, st2.to_st_for)
        elsif st2.kind_of? ECMA262::StForInVar and st2.context.var_env == context.var_env
          parent2.replace(st2, st2.to_st_for_in)
        end
      }
      if var_vars.length > 0
        elems = st.source_elements.source_elements
        v = ECMA262::StVar.new(
          context,
          var_vars.collect do |k, v|
            [ECMA262::IdentifierName.new(context, k)]
          end
        )

        idx = 0
        elems.each do |e|
          next if e.kind_of? ECMA262::StFunc and e.decl?
          found = false
          if e.kind_of? ECMA262::StFunc and e.decl?
            ;
          else
            e.traverse(nil){|ee, pp|
              if ee.kind_of? ECMA262::IdentifierName and var_vars[ee.val.to_sym]
                found = true
                break
              end
            }
          end
          break if found
          idx += 1
        end

        if idx == 0
          elems.unshift(v)
        else
          elems[idx..0] = v
        end
        st.source_elements.remove_empty_statement
      end
    end
    self
  }
  self
end

#rewrite_var(var_st, name, initializer) ⇒ Object



767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/minjs/compressor.rb', line 767

def rewrite_var(var_st, name, initializer)
  var_st.normalization
  i = 0
  var_st.vars.each do |_name, _initializer|
    if _name == name and _initializer.nil?
      var_st.vars[i] = [name, initializer]
      var_st.normalization
      return true
    end
    i += 1
  end
  false
end

#simple_replacement(node = @prog) ⇒ Object



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'lib/minjs/compressor.rb', line 665

def simple_replacement(node = @prog)
  retry_flag = false
  node.traverse(nil) {|st, parent|
    #
    #true => !0
    #false => !1
    #
    if st.kind_of? ECMA262::Boolean
      if st.true?
        parent.replace(st, ECMA262::ExpParen.new(ECMA262::ExpLogicalNot.new(ECMA262::ECMA262Numeric.new(0))))
      else
        parent.replace(st, ECMA262::ExpParen.new(ECMA262::ExpLogicalNot.new(ECMA262::ECMA262Numeric.new(1))))
      end
    #
    #if(true){<then>}else{<else>} => then
    #
    elsif st.kind_of? ECMA262::StIf
      if st.cond.respond_to? :to_ecma262_boolean
        if st.cond.to_ecma262_boolean
          parent.replace(st, st.then_st)
        elsif st.else_st
          parent.replace(st, st.else_st)
        else
          parent.replace(st, ECMA262::StEmpty.new())
        end
      end
    #
    # while(true) => for(;;)
    # while(false) => remove
    #
    elsif st.kind_of? ECMA262::StWhile and st.exp.respond_to? :to_ecma262_boolean
      if st.exp.to_ecma262_boolean
        parent.replace(st, ECMA262::StFor.new(nil,nil,nil, st.statement))
      else
        parent.replace(st, ECMA262::StEmpty.new)
      end
    #
    # new A() => (new A)
    #
    elsif st.kind_of? ECMA262::ExpNew and st.args and st.args.length == 0
      st.replace(st.args, nil)
      parent.add_paren.remove_paren
    end
  }
  self
end

#to_js(options = {}) ⇒ Object



38
39
40
41
# File 'lib/minjs/compressor.rb', line 38

def to_js(options = {})
  remove_empty_statement
  @prog.to_js(options).sub(/;;\Z/, ";")
end