class Opal::Nodes::ResBodyNode

def compile

def compile
  push "if ("
  if rescue_exprs.empty?
    # if no expressions are given, then catch all errors
    push "true"
  else
    push "Opal.rescue($err, ["
    rescue_exprs.each_with_index do |rexpr, idx|
      push ', ' unless idx == 0
      push expr(rexpr)
    end
    push "])"
  end
  push ") {"
  if variable = rescue_variable
    variable[2] = s(:js_tmp, '$err')
    push expr(variable), ';'
  end
  # Need to ensure we clear the current exception out after the rescue block ends
  line "try {"
  indent do
    line process(rescue_body, @level)
  end
  line "} finally {"
  indent do
    line 'Opal.gvars["!"] = Opal.exceptions.pop() || Opal.nil;'
  end
  line "}"
  line "}"
end

def rescue_body

def rescue_body
  body_code = (body || s(:nil))
  body_code = compiler.returns(body_code) if !stmt?
  body_code
end

def rescue_exprs

def rescue_exprs
  exprs = args.dup
  exprs.pop if rescue_variable?(exprs.last)
  exprs.children
end

def rescue_variable

def rescue_variable
  rescue_variable?(args.last) ? args.last.dup : nil
end

def rescue_variable?(variable)

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