class Minjs::ECMA262::ECMA262String

Class of ECMA262 String element

@see www.ecma-international.org/ecma-262 ECMA262 7.8.4

Attributes

val[R]

Public Class Methods

new(val) click to toggle source
# File lib/minjs/ecma262/literal.rb, line 402
def initialize(val)
  @val = val
end

Public Instance Methods

==(obj) click to toggle source

compare object

# File lib/minjs/ecma262/literal.rb, line 420
def ==(obj)
  self.class == obj.class and @val == obj.val
end
deep_dup() click to toggle source

duplicate object @see Base#deep_dup

# File lib/minjs/ecma262/literal.rb, line 408
def deep_dup
  self.class.new(@val)
end
ecma262_typeof() click to toggle source

return results of ‘typeof’ operator.

@return [Symbol] :string

# File lib/minjs/ecma262/literal.rb, line 614
def ecma262_typeof
  :string
end
left_hand_side_exp?() click to toggle source

@return [Boolean] true if expression is kind of LeftHandSideExpression.

# File lib/minjs/ecma262/literal.rb, line 472
def left_hand_side_exp?
  true
end
side_effect?() click to toggle source

Returns this node has side effect or not. @return [Boolean]

# File lib/minjs/ecma262/literal.rb, line 620
def side_effect?
  return false
end
to_ecma262_boolean() click to toggle source

Returns results of ToBoolean()

Returns true or false if trivial, otherwise nil.

@return [Boolean]

@see www.ecma-international.org/ecma-262 ECMA262 9.2

# File lib/minjs/ecma262/literal.rb, line 484
def to_ecma262_boolean
  if @val.length == 0
    false
  else
    true
  end
end
to_ecma262_number() click to toggle source

Returns results of ToNumber()

Returns number if value is trivial, otherwise nil.

@return [Numeric]

@see www.ecma-international.org/ecma-262 ECMA262 9.3

# File lib/minjs/ecma262/literal.rb, line 511
def to_ecma262_number
  begin
    pos1 = pos0 = pos = 0
    v = @val.codepoints
    while true
      return 0 if v[pos].nil? # ToInteger(empty string) => 0
      if white_space?(v[pos]) or line_terminator?(v[pos])
        pos += 1
      else
        break
      end
    end
    #hex
    if v[pos] == 0x30 and (v[pos+1] == 0x78 || v[pos+1] == 0x58) and hex_digit?(v[pos+2])
      base = 16
      pos += 2
      pos0 = pos
      while true
        break if v[pos].nil?
        if hex_digit?(v[pos])
          pos += 1
        else
          break
        end
      end
    #decimal
    else
      base = 10
      sign = 1
      pos0 = pos
      if v[pos].nil?
        raise :error
      elsif v[pos] == 0x2b #+
        pos += 1
      elsif v[pos] == 0x2d #-
        sign = -1
        pos += 1
      end
      has_decimal = false
      has_exp = false

      while true
        break if v[pos].nil?
        if v[pos] >= 0x30 and v[pos] <= 0x39
          pos += 1
        elsif v[pos] == 0x2e #.
          pos += 1
          has_decimal = true
          break;
        else
          break
        end
      end
      if has_decimal
        while true
          break if v[pos].nil?
          if v[pos] >= 0x30 and v[pos] <= 0x39
            pos += 1
          elsif v[pos] == 0x45 or v[pos] == 0x65 #E/e
            pos += 1
            has_exp = true
            break;
          else
            break
          end
        end
      end
      if has_exp
        if v[pos] == 0x2b #+
          pos += 1
        else v[pos] == 0x2d #-
          pos += 1
        end
        while true
          break if v[pos].nil?
          if v[pos] >= 0x30 and v[pos] <= 0x39
            pos += 1
          else
            break
          end
        end
      end
    end
    pos1 = pos
    while white_space?(v[pos]) or line_terminator?(v[pos])
      raise :error if v[pos].nil?
      pos += 1
    end
    raise :error unless v[pos].nil?
    if base == 16
      ret = v[pos0...pos1].pack("U*").to_i(base)
    else
      ret = v[pos0...pos1].pack("U*").to_f
    end
  rescue => e
    ret = nil #Float::NAN
  end
  ret
end
to_ecma262_string() click to toggle source

Returns results of ToString()

Returns string if value is trivial, otherwise nil.

@return [Numeric]

@see www.ecma-international.org/ecma-262 ECMA262 9.8

# File lib/minjs/ecma262/literal.rb, line 500
def to_ecma262_string
  @val.dup
end
to_js(options = {}) click to toggle source

Returns a ECMAScript string containg the representation of element. @see Base#to_js

# File lib/minjs/ecma262/literal.rb, line 426
def to_js(options = {})
  dq = @val.to_s.each_codepoint.select{|x| x == 0x22}.length
  sq = @val.to_s.each_codepoint.select{|x| x == 0x27}.length
  if dq <= sq
    t = "\"".dup
  else
    t = "\'".dup
  end

  @val.to_s.each_codepoint do |c|
    if c == 0x5c
      t << ('\\\\')
    elsif c == 0x22 and dq <= sq
      t << ('\"')
    elsif c == 0x27 and dq > sq
      t << ('\\\'')
    elsif c >= 0x20 and c <= 0x7f
      t << ("%c" % c)
    elsif c == 8
      t << '\\b'
    elsif c == 9
      t << '\\t'
    elsif c == 0xa
      t << '\\n'
    elsif c == 0xb
      t << '\\v'
    elsif c == 0xc
      t << '\\v'
    elsif c == 0xd
      t << '\\r'
    elsif c == 0
      t << '\\0'
    elsif c < 0x20
      t << "\\x#{"%02x" % c}"
    else
      t << [c].pack("U*")
    end
  end
  if dq <= sq
    t << "\""
  else
    t << "\'"
  end
end
traverse(parent) { |parent, self| ... } click to toggle source

Traverses this children and itself with given block.

@see Base#traverse

# File lib/minjs/ecma262/literal.rb, line 415
def traverse(parent)
  yield parent, self
end