class Opal::Nodes::ConstNode

Constants

OPTIMIZED_ACCESS_CONSTS

Public Instance Methods

compile() click to toggle source
# File lib/opal/nodes/constants.rb, line 12
def compile
  if magical_data_const?
    push('$__END__')
  elsif optimized_access?
    helper :"#{name}"
    push "$#{name}"
  elsif const_scope == s(:cbase)
    push "#{top_scope.absolute_const}('#{name}')"
  elsif const_scope
    push "#{top_scope.absolute_const}(", recv(const_scope), ", '#{name}')"
  elsif compiler.eval?
    push "#{scope.relative_access}('#{name}')"
  else
    push "#{scope.relative_access}('#{name}')"
  end
end
magical_data_const?() click to toggle source

Ruby has a magical const DATA that should be processed in a different way:

  1. When current file contains __END__ in the end of the file DATA const should be resolved to the string located after __END__

  2. When current file doesn’t have __END__ section DATA const should be resolved to a regular ::DATA constant

# File lib/opal/nodes/constants.rb, line 35
def magical_data_const?
  const_scope.nil? && name == :DATA && compiler.eof_content
end
optimized_access?() click to toggle source

For a certain case of calls like ‘::Opal.coerce_to?` we can optimize the calls. We can be sure they are defined from the beginning.

# File lib/opal/nodes/constants.rb, line 46
def optimized_access?
  const_scope == s(:cbase) && OPTIMIZED_ACCESS_CONSTS.include?(name)
end