Class: Opal::Nodes::EnsureNode

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

Instance Attribute Summary

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 Method Details

#body_sexpObject



61
62
63
64
65
66
67
# File 'opal/lib/opal/nodes/rescue.rb', line 61

def body_sexp
  if wrap_in_closure?
    compiler.returns(begn)
  else
    begn
  end
end

#compileObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'opal/lib/opal/nodes/rescue.rb', line 12

def compile
  push 'try {'

  in_ensure do
    line stmt(body_sexp)
  end

  line '} finally {'

  indent do
    if has_rescue_else?
      # $no_errors indicates thate there were no error raised
      unshift 'var $no_errors = true; '

      # when there's a begin;rescue;else;ensure;end statement,
      # ruby returns a result of the 'else' branch
      # but invokes it before 'ensure'.
      # so, here we
      # 1. save the result of calling else to $rescue_else_result
      # 2. call ensure
      # 2. return $rescue_else_result
      line 'var $rescue_else_result;'
      line 'if ($no_errors) { '
      indent do
        line '$rescue_else_result = (function() {'
        indent do
          line stmt(rescue_else_code)
        end
        line '})();'
      end
      line '}'
      line compiler.process(ensr_sexp, @level)
      line 'if ($no_errors) { return $rescue_else_result; }'
    else
      line compiler.process(ensr_sexp, @level)
    end
  end

  line '}'

  if wrap_in_closure?
    if scope.await_encountered
      wrap '(await (async function() { ', '; })())'
    else
      wrap '(function() { ', '; })()'
    end
  end
end

#ensr_sexpObject



69
70
71
# File 'opal/lib/opal/nodes/rescue.rb', line 69

def ensr_sexp
  ensr || s(:nil)
end

#rescue_else_codeObject



77
78
79
80
81
# File 'opal/lib/opal/nodes/rescue.rb', line 77

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

#wrap_in_closure?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'opal/lib/opal/nodes/rescue.rb', line 73

def wrap_in_closure?
  recv? || expr? || has_rescue_else?
end