Class: Opal::Nodes::ResBodyNode

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

Constant Summary

Constants included from Helpers

Helpers::RESERVED

Instance Attribute Summary

Attributes inherited from Base

#compiler, #type

Instance Method Summary collapse

Methods inherited from Base

#add_ivar, #add_local, #add_temp, children, #children, #compile_to_fragments, #error, #expr, #expr?, #expr_or_nil, #fragment, handle, handlers, #helper, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #stmt, #stmt?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Helpers

#current_indent, #empty_line, #indent, #js_falsy, #js_truthy, #js_truthy_optimize, #line, #lvar_to_js, #mid_to_jsid, #property, #reserved?, #variable

Constructor Details

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

Instance Method Details

#compileObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'opal/lib/opal/nodes/rescue.rb', line 92

def compile
  push "if ("

  rescue_classes.each_with_index do |cls, idx|
    push ', ' unless idx == 0
    call = s(:call, cls, :===, s(:arglist, s(:js_tmp, '$err')))
    push expr(call)
  end

  # if no classes are given, then catch all errors
  push "true" if rescue_classes.empty?

  push ") {"

  if variable = rescue_variable
    variable[2] = s(:js_tmp, '$err')
    push expr(variable), ';'
  end

  line process(rescue_body, @level)
  line "}"
end

#rescue_bodyObject



129
130
131
# File 'opal/lib/opal/nodes/rescue.rb', line 129

def rescue_body
  body || s(:nil)
end

#rescue_classesObject



123
124
125
126
127
# File 'opal/lib/opal/nodes/rescue.rb', line 123

def rescue_classes
  classes = args.children
  classes.pop if classes.last and classes.last.type != :const
  classes
end

#rescue_variableObject



115
116
117
118
119
120
121
# File 'opal/lib/opal/nodes/rescue.rb', line 115

def rescue_variable
  variable = args.last

  if Sexp === variable and [:lasgn, :iasgn].include?(variable.type)
    variable.dup
  end
end