Sha256: 4aaf4763acd3c70faac1ad6bdf802bc5f32f3895ad84a0016f6f9a0ebd6bb593

Contents?: true

Size: 1.41 KB

Versions: 18

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require 'opal/nodes/base'

module Opal
  module Nodes
    class ConstNode < Base
      handle :const

      children :const_scope, :name

      def compile
        if magical_data_const?
          push('$__END__')
        elsif const_scope
          push '$$$(', recv(const_scope), ", '#{name}')"
        elsif compiler.eval?
          push "$$($nesting, '#{name}')"
        else
          push "$$($nesting, '#{name}')"
        end
      end

      # 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
      def magical_data_const?
        const_scope.nil? && name == :DATA && compiler.eof_content
      end
    end

    # ::CONST
    # s(:const, s(:cbase), :CONST)
    class CbaseNode < Base
      handle :cbase

      def compile
        push "'::'"
      end
    end

    class ConstAssignNode < Base
      handle :casgn

      children :base, :name, :value

      def compile
        if base
          push 'Opal.const_set(', expr(base), ", '#{name}', ", expr(value), ')'
        else
          push "Opal.const_set($nesting[0], '#{name}', ", expr(value), ')'
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
opal-1.3.2 lib/opal/nodes/constants.rb
opal-1.3.1 lib/opal/nodes/constants.rb
opal-1.3.0 lib/opal/nodes/constants.rb
opal-1.3.0.rc1 lib/opal/nodes/constants.rb
opal-1.3.0.alpha1 lib/opal/nodes/constants.rb
opal-1.2.0 lib/opal/nodes/constants.rb
opal-1.2.0.beta1 lib/opal/nodes/constants.rb
opal-1.1.1 lib/opal/nodes/constants.rb
opal-1.1.1.rc1 lib/opal/nodes/constants.rb
opal-1.1.0 lib/opal/nodes/constants.rb
opal-1.1.0.rc1 lib/opal/nodes/constants.rb
opal-1.0.5 lib/opal/nodes/constants.rb
opal-1.0.4 lib/opal/nodes/constants.rb
opal-1.0.3 lib/opal/nodes/constants.rb
opal-1.0.2 lib/opal/nodes/constants.rb
opal-1.0.1 lib/opal/nodes/constants.rb
opal-1.0.0 lib/opal/nodes/constants.rb
opal-1.0.0.beta1 lib/opal/nodes/constants.rb