Class: Minjs::ECMA262::ECMA262Numeric

Inherits:
Literal show all
Defined in:
lib/minjs/ecma262/literal.rb

Overview

Class of ECMA262 Numeric element

ECMA262 say:

The Number type has exactly 18437736874454810627 (that is, 264−253+3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic

To simplify the implementation, Minjs assumes that ruby has IEEE754 dobule precision.

See Also:

Instance Attribute Summary collapse

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Literal

#lt?, #priority, #ws?

Methods inherited from Base

#add_remove_paren, #concat, #replace

Constructor Details

#initialize(integer, decimal = nil, exp = nil) ⇒ ECMA262Numeric

Returns a new instance of ECMA262Numeric.



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
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
711
712
713
714
715
# File 'lib/minjs/ecma262/literal.rb', line 650

def initialize(integer, decimal = nil, exp = nil)
  if integer == :nan or integer == "NaN"
    @number = Float::NAN
    @integer = "NaN"
    @decimal = nil
    @exp = nil
  elsif integer == :infinity or integer == Float::INFINITY or integer == "Infinity"
    @number = Float::INFINITY
    @integer = "Infinity"
    @decimal = nil
    @exp = nil
  elsif integer == -Float::INFINITY or integer == "-Infinity"
    @number = -Float::INFINITY
    @integer = "-Infinity"
    @decimal = nil
    @exp = nil
  elsif integer.kind_of? String
    @integer = integer.to_s #String
    @decimal = decimal.to_s #String
    @exp = exp ? exp.to_i : nil
    if @decimal == ""
      d = ""
    else
      d = ".#{@decimal}"
    end
    if @exp
      @number = "#{integer}#{d}e#{exp}".to_f
    else
      @number = "#{integer}#{d}".to_f
    end
    if @number.kind_of? Float and @number.nan?
      @integer = "NaN"
      @decimal = nil
      @exp = nil
    elsif @number == Float::INFINITY
      @integer = "Infinity"
      @decimal = nil
      @exp = nil
    elsif @number == -Float::INFINITY
      @integer = "-Infinity"
      @decimal = nil
      @exp = nil
    end
  elsif integer.kind_of? Numeric
    if integer.kind_of? Float and integer.nan?
      @number = Float::NAN
      @decimal = nil
      @exp = nil
    elsif integer == Float::INFINITY
      @number = Float::INFINITY
      @decimal = nil
      @exp = nil
    elsif integer == -Float::INFINITY
      @number = -Float::INFINITY
      @decimal = nil
      @exp = nil
    else
      @number = integer
      @integer = @number.to_i.to_s
      @decimal = (@number - @integer.to_i).to_s.sub(/0\.?/, '')
      @exp = nil
    end
  else
    raise 'internal error'
  end
end

Instance Attribute Details

#decimalObject (readonly)

Returns the value of attribute decimal.



640
641
642
# File 'lib/minjs/ecma262/literal.rb', line 640

def decimal
  @decimal
end

#expObject (readonly)

Returns the value of attribute exp.



640
641
642
# File 'lib/minjs/ecma262/literal.rb', line 640

def exp
  @exp
end

#integerObject (readonly)

Returns the value of attribute integer.



640
641
642
# File 'lib/minjs/ecma262/literal.rb', line 640

def integer
  @integer
end

#numberObject (readonly)

Returns the value of attribute number.



640
641
642
# File 'lib/minjs/ecma262/literal.rb', line 640

def number
  @number
end

Instance Method Details

#==(obj) ⇒ Object

compare object



731
732
733
# File 'lib/minjs/ecma262/literal.rb', line 731

def ==(obj)
  self.class == obj.class and self.to_ecma262_string == obj.to_ecma262_string
end

#deep_dupObject

duplicate object

See Also:



719
720
721
# File 'lib/minjs/ecma262/literal.rb', line 719

def deep_dup
  self.class.new(@number)
end

#ecma262_typeofSymbol

return results of ‘typeof’ operator.

Returns:

  • (Symbol)

    :number



896
897
898
# File 'lib/minjs/ecma262/literal.rb', line 896

def ecma262_typeof
  :number
end

#infinity?Boolean

True if number is Infinity

Returns:



796
797
798
# File 'lib/minjs/ecma262/literal.rb', line 796

def infinity?
  @number == Float::INFINITY || @number == -Float::INFINITY
end

#left_hand_side_exp?Boolean

Returns true if expression is kind of LeftHandSideExpression.

Returns:

  • (Boolean)

    true if expression is kind of LeftHandSideExpression.



776
777
778
# File 'lib/minjs/ecma262/literal.rb', line 776

def left_hand_side_exp?
  true
end

#nan?Boolean

True if number is NaN

Returns:



791
792
793
# File 'lib/minjs/ecma262/literal.rb', line 791

def nan?
  @number.kind_of? Float and @number.nan?
end

#number?Boolean

True if number not Infinity nor NaN

Returns:



801
802
803
# File 'lib/minjs/ecma262/literal.rb', line 801

def number?
  !nan? and !infinity?
end

#side_effect?Boolean

Returns this node has side effect or not.

Returns:



869
870
871
# File 'lib/minjs/ecma262/literal.rb', line 869

def side_effect?
  return false
end

#to_ecma262_booleanBoolean

Returns results of ToBoolean()

Returns true or false if value is trivial, otherwise nil.

Returns:

See Also:



859
860
861
862
863
864
865
# File 'lib/minjs/ecma262/literal.rb', line 859

def to_ecma262_boolean
  if @val == :nan or to_ecma262_string == "0"
    false
  else
    true
  end
end

#to_ecma262_numberNumeric

Returns results of ToNumber()

Returns number if value is trivial, otherwise nil.

Returns:

  • (Numeric)

See Also:



881
882
883
884
885
886
887
888
889
890
891
# File 'lib/minjs/ecma262/literal.rb', line 881

def to_ecma262_number
  if nan?
    nil
  elsif @number == Float::INFINITY
    nil
  elsif @number == -Float::INFINITY
    nil
  else
    @number
  end
end

#to_ecma262_stringNumeric

Returns results of ToString()

Returns string if value is trivial, otherwise nil.

Returns:

  • (Numeric)

See Also:



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
839
840
841
842
843
844
845
846
847
848
849
# File 'lib/minjs/ecma262/literal.rb', line 813

def to_ecma262_string
  if nan?
    "NaN"
  elsif @number == Float::INFINITY
    "Infinity"
  elsif @number == -Float::INFINITY
    "-Infinity"
  elsif @integer == '0' and @decimal.nil? and @exp.nil?
    "0"
  else
    f = @number.to_f.to_s
    _n, _e = f.split('e')
    _i, _d = _n.split('.')

    e = _e.to_i
    if(e == 0)
      if _d.to_i != 0
        return _n
      else
        return _i
      end
    elsif(e > 0 && e < 21)
      _n = _i + _d
      _n += '0' * (21 - _n.length)
      return _n
    elsif(e < 0 && e >= -6)
      _n = "0." + ('0' * (-e-1)) + _i + _d
      return _n
    else
      if e<0
        return "#{_i}.#{_d}e#{e}"
      else
        return "#{_i}.#{_d}e+#{e}"
      end
    end
  end
end

#to_fObject

to float



786
787
788
# File 'lib/minjs/ecma262/literal.rb', line 786

def to_f
  to_ecma262_string.to_f
end

#to_iObject

to integer



781
782
783
# File 'lib/minjs/ecma262/literal.rb', line 781

def to_i
  to_ecma262_string.to_i
end

#to_js(options = {}) ⇒ Object

Returns a ECMAScript string containg the representation of element.

See Also:



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
765
766
767
768
769
770
771
772
773
# File 'lib/minjs/ecma262/literal.rb', line 737

def to_js(options = {})
  if nan?
    return "NaN"
  elsif @number == Float::INFINITY
    return "Infinity"
  elsif @number == -Float::INFINITY
    return "-Infinity"
  end
  t0 = to_ecma262_string
  t0.sub!(/^0\./, '.')

  t = @integer.nil? ? "" : @integer.dup.to_s

  d = @decimal.to_s
  if d == '0'
    d = ''
  end
  if d.length > 0
    if @integer == '0'
      t = ".#{d}"
    else
      t << ".#{d}"
    end
  end
  if @exp
    t << "e#{@exp}"
  end

  if !t.match(/e/) and !t.match(/\./) and t.match(/0{3,}$/)
    len = $&.length
    t.sub!(/0+$/, "e#{len}")
  end
  t.sub!(/e\+/, 'e')
  t0.sub!(/e\+/, 'e')

  t.length <= t0.length ? t : t0
end

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

Traverses this children and itself with given block.

Yields:

Yield Parameters:

See Also:



726
727
728
# File 'lib/minjs/ecma262/literal.rb', line 726

def traverse(parent, &block)
  yield parent, self
end