Class: Minjs::ECMA262::ExpNew

Inherits:
Expression show all
Defined in:
lib/minjs/ecma262/expression.rb

Overview

Class of the New expression element.

See Also:

Instance Attribute Summary collapse

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Expression

#reduce, #side_effect?

Methods inherited from Base

#add_remove_paren, #concat

Constructor Details

#initialize(name, args) ⇒ ExpNew

Returns a new instance of ExpNew.



695
696
697
698
# File 'lib/minjs/ecma262/expression.rb', line 695

def initialize(name, args)
  @name = name
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



693
694
695
# File 'lib/minjs/ecma262/expression.rb', line 693

def args
  @args
end

#nameObject (readonly)

Returns the value of attribute name.



693
694
695
# File 'lib/minjs/ecma262/expression.rb', line 693

def name
  @name
end

Instance Method Details

#==(obj) ⇒ Object

compare object



737
738
739
# File 'lib/minjs/ecma262/expression.rb', line 737

def ==(obj)
  self.class == obj.class and @name == obj.name and @args == obj.args
end

#add_parenObject

add parenthesis if need



775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'lib/minjs/ecma262/expression.rb', line 775

def add_paren
  if @name.priority > PRIORITY_LEFT_HAND_SIDE
    @name = ExpParen.new(@name)
  end
  if @args
    @args.map! do |arg|
      if arg.priority > PRIORITY_ASSIGNMENT
        ExpParen.new(arg)
      else
        arg
      end
    end
  end
  self
end

#deep_dupObject

duplicate object

See Also:



707
708
709
710
# File 'lib/minjs/ecma262/expression.rb', line 707

def deep_dup
  self.class.new(@name,
                 @args ? @args.collect{|x| x.deep_dup} : nil)
end

#left_hand_side_exp?Boolean

Returns true if expression is kind of LeftHandSideExpression.

Returns:

  • (Boolean)

    true if expression is kind of LeftHandSideExpression.



753
754
755
# File 'lib/minjs/ecma262/expression.rb', line 753

def left_hand_side_exp?
  true
end

#priorityFixnum

Returns expression priority.

Returns:

  • (Fixnum)

    expression priority



701
702
703
# File 'lib/minjs/ecma262/expression.rb', line 701

def priority
  PRIORITY_LEFT_HAND_SIDE + ((args == nil) ? 1 : 0)
end

#remove_parenObject

remove parenthesis if possible



758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/minjs/ecma262/expression.rb', line 758

def remove_paren
  if @name.kind_of? ExpParen and @name.val.priority <= PRIORITY_LEFT_HAND_SIDE
    @name = @name.val if @name.remove_paren?
  end
  if @args
    @args.map! do |arg|
      if arg.kind_of? ExpParen and arg.val.priority <= PRIORITY_ASSIGNMENT
        arg.val if arg.remove_paren?
      else
        arg
      end
    end
  end
  self
end

#replace(from, to) ⇒ Object

Replaces children object.

See Also:



714
715
716
717
718
719
720
721
722
# File 'lib/minjs/ecma262/expression.rb', line 714

def replace(from, to)
  if @name .eql? from
    @name = from
  elsif @args .eql? from
    @args = to
  elsif @args and (idx = @args.index(from))
    @args[idx] = to
  end
end

#to_js(options = {}) ⇒ Object

Returns a ECMAScript string containg the representation of element.

See Also:



743
744
745
746
747
748
749
750
# File 'lib/minjs/ecma262/expression.rb', line 743

def to_js(options = {})
  if @args
    args = @args.collect{|x| x.to_js(options)}.join(",")
    concat options, :new, @name, '(', args, ')'
  else
    concat options, :new, @name
  end
end

#traverse(parent) {|parent, _self| ... } ⇒ Object

Traverses this children and itself with given block.

Yields:

Yield Parameters:

See Also:



726
727
728
729
730
731
732
733
734
# File 'lib/minjs/ecma262/expression.rb', line 726

def traverse(parent, &block)
  @name.traverse(self, &block)
  if @args
    @args.each do |arg|
      arg.traverse(self, &block)
    end
  end
  yield parent, self
end