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

Attributes included from Closure::NodeSupport

#closure

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_empty, #expr_or_nil, #fragment, handle, handlers, #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 Closure::NodeSupport

#closure_is?, #compile_catcher, #generate_thrower, #generate_thrower_without_catcher, #in_closure, #pop_closure, #push_closure, #select_closure, #thrower

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



65
66
67
68
69
70
71
# File 'opal/lib/opal/nodes/rescue.rb', line 65

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
60
61
62
63
# File 'opal/lib/opal/nodes/rescue.rb', line 12

def compile
  push_closure if wrap_in_closure?

  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 '}'

  pop_closure if wrap_in_closure?

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

#ensr_sexpObject



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

def ensr_sexp
  ensr || s(:nil)
end

#has_rescue_else?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'opal/lib/opal/nodes/rescue.rb', line 87

def has_rescue_else?
  @sexp.meta[:has_rescue_else]
end

#rescue_else_codeObject



81
82
83
84
85
# File 'opal/lib/opal/nodes/rescue.rb', line 81

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)


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

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