Class: Opal::Nodes::RescueNode

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/nodes/rescue.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#compiler, #sexp, #type

Instance Method Summary collapse

Methods inherited from Base

#add_gvar, #add_ivar, #add_local, #add_temp, #children, children, #class_variable_owner, #class_variable_owner_nesting_level, #comments, #compile_to_fragments, #error, #expr, #expr?, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #source_location, #stmt, #stmt?, #top_scope, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Helpers

#current_indent, #empty_line, #indent, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?

Constructor Details

This class inherits a constructor from Opal::Nodes::Base

Instance Attribute Details

#retry_idObject (readonly)

Returns the value of attribute retry_id.



175
176
177
# File 'opal/lib/opal/nodes/rescue.rb', line 175

def retry_id
  @retry_id
end

Instance Method Details

#body_codeObject



152
153
154
155
156
# File 'opal/lib/opal/nodes/rescue.rb', line 152

def body_code
  body_code = (body.nil? || body.type == :resbody ? s(:nil) : body)
  body_code = compiler.returns(body_code) unless stmt?
  body_code
end

#compileObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'opal/lib/opal/nodes/rescue.rb', line 89

def compile
  scope.rescue_else_sexp = children[1..-1].detect { |sexp| sexp && sexp.type != :resbody }
  _has_rescue_handlers = false

  if handle_rescue_else_manually?
    line 'var $no_errors = true;'
  end

  in_rescue(self) do
    push 'try {'
    indent do
      line stmt(body_code)
    end
    line '} catch ($err) {'

    indent do
      if has_rescue_else?
        line '$no_errors = false;'
      end

      children[1..-1].each_with_index do |child, idx|
        # counting only rescue, ignoring rescue-else statement
        next unless child && child.type == :resbody
        _has_rescue_handlers = true

        push ' else ' unless idx == 0
        line process(child, @level)
      end

      # if no resbodys capture our error, then rethrow
      push ' else { throw $err; }'
    end

    line '}'

    if handle_rescue_else_manually?
      # here we must add 'finally' explicitly
      push 'finally {'
      indent do
        line 'if ($no_errors) { '
        indent do
          line stmt(rescue_else_code)
        end
        line '}'
      end
      push '}'
    end

    wrap "#{retry_id}: do { ", ' break; } while(1)' if retry_id
  end

  # Wrap a try{} catch{} into a function
  # when it's an expression
  # or when there's a method call after begin;rescue;end
  if expr? || recv?
    if scope.await_encountered
      wrap '(await (async function() { ', '})())'
    else
      wrap '(function() { ', '})()'
    end
  end
end

#gen_retry_idObject



171
172
173
# File 'opal/lib/opal/nodes/rescue.rb', line 171

def gen_retry_id
  @retry_id ||= scope.gen_retry_id
end

#handle_rescue_else_manually?Boolean

Returns true when there's no 'ensure' statement wrapping current rescue.

Returns:

  • (Boolean)


167
168
169
# File 'opal/lib/opal/nodes/rescue.rb', line 167

def handle_rescue_else_manually?
  !in_ensure? && has_rescue_else?
end

#rescue_else_codeObject



158
159
160
161
162
# File 'opal/lib/opal/nodes/rescue.rb', line 158

def rescue_else_code
  rescue_else_code = scope.rescue_else_sexp
  rescue_else_code = compiler.returns(rescue_else_code) unless stmt?
  rescue_else_code
end