Sha256: 85b6e4374e5fc5027f47eaaf6eb11bac2797cd14b71ace4187088da694f96987

Contents?: true

Size: 1.38 KB

Versions: 11

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'opal/nodes/base'

module Opal
  module Nodes
    class WhileNode < Base
      handle :while

      children :test, :body

      def compile
        with_temp do |redo_var|
          test_code = js_truthy(test)

          compiler.in_while do
            while_loop[:closure] = true if wrap_in_closure?
            while_loop[:redo_var] = redo_var

            body_code = stmt(body)

            if uses_redo?
              push "#{redo_var} = false; #{while_open}#{redo_var} || "
              push test_code
              push while_close
            else
              push while_open, test_code, while_close
            end

            push "#{redo_var} = false;" if uses_redo?
            line body_code
          end
          line '}'
        end

        wrap '(function() {', '; return nil; })()' if wrap_in_closure?
      end

      def while_open
        'while ('
      end

      def while_close
        ') {'
      end

      def uses_redo?
        while_loop[:use_redo]
      end

      def wrap_in_closure?
        expr? || recv?
      end
    end

    class WhilePostNode < WhileNode
      handle :while_post
    end

    class UntilNode < WhileNode
      handle :until

      def while_open
        'while (!('
      end

      def while_close
        ')) {'
      end
    end

    class UntilPostNode < UntilNode
      handle :until_post
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
opal-1.1.1 lib/opal/nodes/while.rb
opal-1.1.1.rc1 lib/opal/nodes/while.rb
opal-1.1.0 lib/opal/nodes/while.rb
opal-1.1.0.rc1 lib/opal/nodes/while.rb
opal-1.0.5 lib/opal/nodes/while.rb
opal-1.0.4 lib/opal/nodes/while.rb
opal-1.0.3 lib/opal/nodes/while.rb
opal-1.0.2 lib/opal/nodes/while.rb
opal-1.0.1 lib/opal/nodes/while.rb
opal-1.0.0 lib/opal/nodes/while.rb
opal-1.0.0.beta1 lib/opal/nodes/while.rb