Sha256: 1e1feed2030833f5fc8c234f824db9155070e648482825e44681c3d96d6d42b9

Contents?: true

Size: 1.05 KB

Versions: 20

Compression:

Stored size: 1.05 KB

Contents

require 'opal/nodes/scope'

module Opal
  module Nodes
    class ModuleNode < ScopeNode
      handle :module

      children :cid, :body

      def compile
        name, base = name_and_base
        helper :module

        push "(function($base) {"
        line "  var $#{name}, self = $#{name} = $module($base, '#{name}');"

        in_scope do
          scope.name = name
          add_temp "#{scope.proto} = self.$$proto"
          add_temp '$scope = self.$$scope'

          body_code = stmt(body || s(:nil))
          empty_line

          line scope.to_vars
          line body_code
        end

        line "})(", base, ")"
      end

      def name_and_base
        if cid.type == :const
          # Using $scope.base, not self in case a class is created inside an anonymous class or block
          [cid[1].to_s, '$scope.base']
        elsif cid.type == :colon2
          [cid[2].to_s, expr(cid[1])]
        elsif cid.type == :colon3
          [cid[1].to_s, 'Opal.Object']
        else
          raise "Bad receiver in module"
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
opal-0.10.6 lib/opal/nodes/module.rb
opal-0.10.6.beta lib/opal/nodes/module.rb
opal-0.10.5 lib/opal/nodes/module.rb
opal-0.10.4 lib/opal/nodes/module.rb
opal-0.10.3 lib/opal/nodes/module.rb
opal-0.10.2 lib/opal/nodes/module.rb
opal-0.10.1 lib/opal/nodes/module.rb
opal-0.10.0 lib/opal/nodes/module.rb
opal-0.10.0.rc2 lib/opal/nodes/module.rb
opal-0.9.4 lib/opal/nodes/module.rb
opal-0.9.3 lib/opal/nodes/module.rb
opal-0.10.0.rc1 lib/opal/nodes/module.rb
opal-0.10.0.beta5 lib/opal/nodes/module.rb
opal-0.10.0.beta4 lib/opal/nodes/module.rb
opal-0.10.0.beta3 lib/opal/nodes/module.rb
opal-0.10.0.beta2 lib/opal/nodes/module.rb
opal-0.10.0.beta1 lib/opal/nodes/module.rb
opal-0.9.2 lib/opal/nodes/module.rb
opal-0.9.0 lib/opal/nodes/module.rb
opal-0.9.0.rc1 lib/opal/nodes/module.rb