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, #type

Instance Method Summary collapse

Methods inherited from Base

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

Methods included from Helpers

#conditional_send, #current_indent, #empty_line, #indent, #js_falsy, #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



54
55
56
57
58
59
60
# File 'opal/lib/opal/nodes/rescue.rb', line 54

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

#compileObject



11
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
# File 'opal/lib/opal/nodes/rescue.rb', line 11

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

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

#ensr_sexpObject



62
63
64
# File 'opal/lib/opal/nodes/rescue.rb', line 62

def ensr_sexp
  ensr || s(:nil)
end

#rescue_else_codeObject



70
71
72
73
74
# File 'opal/lib/opal/nodes/rescue.rb', line 70

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)


66
67
68
# File 'opal/lib/opal/nodes/rescue.rb', line 66

def wrap_in_closure?
  recv? or expr? or has_rescue_else?
end