Sha256: 9c3ffb95e0c2a999719ecce959649499ba79a57c181f6aee839fd4adf0de02e1

Contents?: true

Size: 1.5 KB

Versions: 40

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'opal/nodes/base'

module Opal
  module Nodes
    class BaseYieldNode < Base
      def compile_call
        yielding_scope = find_yielding_scope

        yielding_scope.uses_block!
        yielding_scope.block_name ||= '$yield'

        block_name = yielding_scope.block_name

        if yields_single_arg?(children)
          push expr(children.first)
          wrap "Opal.yield1(#{block_name}, ", ')'
        else
          push expr(s(:arglist, *children))

          if uses_splat?(children)
            wrap "Opal.yieldX(#{block_name}, ", ')'
          else
            wrap "Opal.yieldX(#{block_name}, [", '])'
          end
        end
      end

      def find_yielding_scope
        working = scope
        while working
          if working.block_name || working.def?
            break
          end
          working = working.parent
        end

        working
      end

      def yields_single_arg?(children)
        !uses_splat?(children) && children.size == 1
      end

      def uses_splat?(children)
        children.any? { |child| child.type == :splat }
      end
    end

    class YieldNode < BaseYieldNode
      handle :yield

      def compile
        compile_call
      end
    end

    # Created by `#returns()` for when a yield statement should return
    # it's value (its last in a block etc).
    class ReturnableYieldNode < BaseYieldNode
      handle :returnable_yield

      def compile
        compile_call

        wrap 'return ', ';'
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 lib/opal/nodes/yield.rb
opal-1.8.2 lib/opal/nodes/yield.rb
opal-1.8.1 lib/opal/nodes/yield.rb
opal-1.8.0 lib/opal/nodes/yield.rb
opal-1.8.0.beta1 lib/opal/nodes/yield.rb
opal-1.7.4 lib/opal/nodes/yield.rb
opal-1.8.0.alpha1 lib/opal/nodes/yield.rb
opal-1.7.3 lib/opal/nodes/yield.rb
opal-1.7.2 lib/opal/nodes/yield.rb
opal-1.7.1 lib/opal/nodes/yield.rb
opal-1.7.0 lib/opal/nodes/yield.rb
opal-1.7.0.rc1 lib/opal/nodes/yield.rb
opal-1.6.1 lib/opal/nodes/yield.rb
opal-1.6.0 lib/opal/nodes/yield.rb
opal-1.6.0.rc1 lib/opal/nodes/yield.rb
opal-1.6.0.alpha1 lib/opal/nodes/yield.rb
opal-1.5.1 lib/opal/nodes/yield.rb
opal-1.5.0 lib/opal/nodes/yield.rb
opal-1.5.0.rc1 lib/opal/nodes/yield.rb
opal-1.4.1 lib/opal/nodes/yield.rb