Class: Minjs::ECMA262::ECMA262String

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

Overview

Class of ECMA262 String element

See Also:

Instance Attribute Summary collapse

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods included from Ctype

#decimal_digit?, #hex_digit?, #identifier_part?, #identifier_start?, #idname?, #line_terminator?, #octal_digit?, #white_space?

Methods inherited from Literal

#lt?, #priority, #ws?

Methods inherited from Base

#add_remove_paren, #concat, #replace

Constructor Details

#initialize(val) ⇒ ECMA262String

Returns a new instance of ECMA262String.



402
403
404
# File 'lib/minjs/ecma262/literal.rb', line 402

def initialize(val)
  @val = val
end

Instance Attribute Details

#valObject (readonly)

Returns the value of attribute val.



400
401
402
# File 'lib/minjs/ecma262/literal.rb', line 400

def val
  @val
end

Instance Method Details

#==(obj) ⇒ Object

compare object



420
421
422
# File 'lib/minjs/ecma262/literal.rb', line 420

def ==(obj)
  self.class == obj.class and @val == obj.val
end

#deep_dupObject

duplicate object

See Also:



408
409
410
# File 'lib/minjs/ecma262/literal.rb', line 408

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

#ecma262_typeofSymbol

return results of ‘typeof’ operator.

Returns:

  • (Symbol)

    :string



614
615
616
# File 'lib/minjs/ecma262/literal.rb', line 614

def ecma262_typeof
  :string
end

#left_hand_side_exp?Boolean

Returns true if expression is kind of LeftHandSideExpression.

Returns:

  • (Boolean)

    true if expression is kind of LeftHandSideExpression.



472
473
474
# File 'lib/minjs/ecma262/literal.rb', line 472

def left_hand_side_exp?
  true
end

#side_effect?Boolean

Returns this node has side effect or not.

Returns:



620
621
622
# File 'lib/minjs/ecma262/literal.rb', line 620

def side_effect?
  return false
end

#to_ecma262_booleanBoolean

Returns results of ToBoolean()

Returns true or false if trivial, otherwise nil.

Returns:

See Also:



484
485
486
487
488
489
490
# File 'lib/minjs/ecma262/literal.rb', line 484

def to_ecma262_boolean
  if @val.length == 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:



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
# 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_stringNumeric

Returns results of ToString()

Returns string if value is trivial, otherwise nil.

Returns:

  • (Numeric)

See Also:



500
501
502
# File 'lib/minjs/ecma262/literal.rb', line 500

def to_ecma262_string
  @val.dup
end

#to_js(options = {}) ⇒ Object

Returns a ECMAScript string containg the representation of element.

See Also:



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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# 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| ... } ⇒ Object

Traverses this children and itself with given block.

Yields:

Yield Parameters:

See Also:



415
416
417
# File 'lib/minjs/ecma262/literal.rb', line 415

def traverse(parent)
  yield parent, self
end