Class: Opal::Nodes::StringNode

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/nodes/literal.rb

Constant Summary collapse

ESCAPE_CHARS =
{
  'a' => '\\u0007',
  'e' => '\\u001b'
}.freeze
ESCAPE_REGEX =
/(\\+)([#{ ESCAPE_CHARS.keys.join('') }])/.freeze

Instance Attribute Summary

Attributes inherited from Base

#compiler, #sexp, #type

Attributes included from Closure::NodeSupport

#closure

Instance Method Summary collapse

Methods inherited from Base

#add_gvar, #add_ivar, #add_local, #add_temp, #children, children, #class_variable_owner, #class_variable_owner_nesting_level, #comments, #compile_to_fragments, #error, #expr, #expr?, #expr_or_empty, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #source_location, #stmt, #stmt?, #top_scope, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Closure::NodeSupport

#closure_is?, #compile_catcher, #generate_thrower, #generate_thrower_without_catcher, #in_closure, #pop_closure, #push_closure, #select_closure, #thrower

Methods included from Helpers

#current_indent, #empty_line, #indent, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?

Constructor Details

This class inherits a constructor from Opal::Nodes::Base

Instance Method Details

#compileObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'opal/lib/opal/nodes/literal.rb', line 60

def compile
  string_value = value

  sanitized_value = string_value.inspect.gsub(/\\u\{([0-9a-f]+)\}/) do
    code_point = Regexp.last_match(1).to_i(16)
    to_utf16(code_point)
  end
  push translate_escape_chars(sanitized_value)

  if RUBY_ENGINE != 'opal'
    encoding = string_value.encoding

    unless encoding == Encoding::UTF_8
      helper :enc
      wrap "$enc(", ", \"#{encoding.name}\")"
    end
  end

  unless value.valid_encoding?
    helper :binary
    wrap "$binary(", ")"
  end
end

#to_utf16(code_point) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'opal/lib/opal/nodes/literal.rb', line 85

def to_utf16(code_point)
  ten_bits = 0b1111111111
  u = ->(code_unit) { '\\u' + code_unit.to_s(16).upcase }

  return u.call(code_point) if code_point <= 0xFFFF

  code_point -= 0x10000

  # Shift right to get to most significant 10 bits
  lead_surrogate = 0xD800 + (code_point >> 10)

  # Mask to get least significant 10 bits
  tail_surrogate = 0xDC00 + (code_point & ten_bits)

  u.call(lead_surrogate) + u.call(tail_surrogate)
end

#translate_escape_chars(inspect_string) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'opal/lib/opal/nodes/literal.rb', line 50

def translate_escape_chars(inspect_string)
  inspect_string.gsub(ESCAPE_REGEX) do |original|
    if Regexp.last_match(1).length.even?
      original
    else
      Regexp.last_match(1).chop + ESCAPE_CHARS[Regexp.last_match(2)]
    end
  end
end